xref: /aosp_15_r20/external/deqp/framework/opengl/simplereference/sglrContextUtil.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES Utilities
3  * ------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief SGLR Context utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "sglrContextUtil.hpp"
25 #include "sglrContext.hpp"
26 #include "glwEnums.hpp"
27 
28 namespace sglr
29 {
30 
drawQuad(sglr::Context & ctx,uint32_t program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)31 void drawQuad(sglr::Context &ctx, uint32_t program, const tcu::Vec3 &p0, const tcu::Vec3 &p1)
32 {
33     const glu::ContextType ctxType = ctx.getType();
34 
35     if (glu::isContextTypeGLCore(ctxType) || (contextSupports(ctxType, glu::ApiType::es(3, 1))))
36         drawQuadWithVaoBuffers(ctx, program, p0, p1);
37     else
38     {
39         DE_ASSERT(isContextTypeES(ctxType));
40         drawQuadWithClientPointers(ctx, program, p0, p1);
41     }
42 }
43 
drawQuadWithVaoBuffers(sglr::Context & ctx,uint32_t program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)44 void drawQuadWithVaoBuffers(sglr::Context &ctx, uint32_t program, const tcu::Vec3 &p0, const tcu::Vec3 &p1)
45 {
46     // Vertex data.
47     float hz                 = (p0.z() + p1.z()) * 0.5f;
48     float position[]         = {p0.x(), p0.y(), p0.z(), 1.0f, p0.x(), p1.y(), hz,     1.0f,
49                                 p1.x(), p0.y(), hz,     1.0f, p1.x(), p1.y(), p1.z(), 1.0f};
50     const float coord[]      = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f};
51     const uint16_t indices[] = {0, 1, 2, 2, 1, 3};
52 
53     int32_t posLoc   = ctx.getAttribLocation(program, "a_position");
54     int32_t coordLoc = ctx.getAttribLocation(program, "a_coord");
55     uint32_t vaoID;
56     uint32_t bufIDs[2];
57 
58     ctx.genVertexArrays(1, &vaoID);
59     ctx.bindVertexArray(vaoID);
60 
61     ctx.genBuffers(2, &bufIDs[0]);
62 
63     ctx.useProgram(program);
64     TCU_CHECK(posLoc >= 0);
65     {
66         ctx.bindBuffer(GL_ARRAY_BUFFER, bufIDs[0]);
67         ctx.bufferData(GL_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(position) * sizeof(float), &position[0], GL_STATIC_DRAW);
68 
69         ctx.enableVertexAttribArray(posLoc);
70         ctx.vertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
71 
72         ctx.bindBuffer(GL_ARRAY_BUFFER, 0);
73     }
74 
75     if (coordLoc >= 0)
76     {
77         ctx.bindBuffer(GL_ARRAY_BUFFER, bufIDs[1]);
78         ctx.bufferData(GL_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(coord) * sizeof(float), &coord[0], GL_STATIC_DRAW);
79 
80         ctx.enableVertexAttribArray(coordLoc);
81         ctx.vertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
82 
83         ctx.bindBuffer(GL_ARRAY_BUFFER, 0);
84     }
85 
86     {
87         uint32_t ndxID;
88         ctx.genBuffers(1, &ndxID);
89 
90         ctx.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, ndxID);
91         ctx.bufferData(GL_ELEMENT_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(indices) * sizeof(uint16_t), &indices[0],
92                        GL_STATIC_DRAW);
93 
94         ctx.drawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_SHORT, 0);
95 
96         ctx.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
97         ctx.deleteBuffers(1, &ndxID);
98     }
99 
100     ctx.deleteBuffers(2, &bufIDs[0]);
101     ctx.deleteVertexArrays(1, &vaoID);
102 }
103 
drawQuadWithClientPointers(sglr::Context & ctx,uint32_t program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)104 void drawQuadWithClientPointers(sglr::Context &ctx, uint32_t program, const tcu::Vec3 &p0, const tcu::Vec3 &p1)
105 {
106     // Vertex data.
107     float hz                 = (p0.z() + p1.z()) * 0.5f;
108     float position[]         = {p0.x(), p0.y(), p0.z(), 1.0f, p0.x(), p1.y(), hz,     1.0f,
109                                 p1.x(), p0.y(), hz,     1.0f, p1.x(), p1.y(), p1.z(), 1.0f};
110     const float coord[]      = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f};
111     const uint16_t indices[] = {0, 1, 2, 2, 1, 3};
112 
113     int32_t posLoc   = ctx.getAttribLocation(program, "a_position");
114     int32_t coordLoc = ctx.getAttribLocation(program, "a_coord");
115 
116     ctx.useProgram(program);
117     TCU_CHECK(posLoc >= 0);
118     {
119         ctx.enableVertexAttribArray(posLoc);
120         ctx.vertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, &position[0]);
121     }
122 
123     if (coordLoc >= 0)
124     {
125         ctx.enableVertexAttribArray(coordLoc);
126         ctx.vertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, &coord[0]);
127     }
128 
129     ctx.drawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_SHORT, &indices[0]);
130 
131     if (posLoc >= 0)
132         ctx.disableVertexAttribArray(posLoc);
133 
134     if (coordLoc >= 0)
135         ctx.disableVertexAttribArray(coordLoc);
136 }
137 
138 } // namespace sglr
139