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,deUint32 program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)31 void drawQuad (sglr::Context& ctx, deUint32 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,deUint32 program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)44 void drawQuadWithVaoBuffers (sglr::Context& ctx, deUint32 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[] =
49 	{
50 		p0.x(), p0.y(), p0.z(),	1.0f,
51 		p0.x(), p1.y(), hz,		1.0f,
52 		p1.x(), p0.y(), hz,		1.0f,
53 		p1.x(), p1.y(), p1.z(),	1.0f
54 	};
55 	const float coord[] =
56 	{
57 		0.0f, 0.0f,
58 		0.0f, 1.0f,
59 		1.0f, 0.0f,
60 		1.0f, 1.0f
61 	};
62 	const deUint16		indices[]	= { 0, 1, 2, 2, 1, 3 };
63 
64 	deInt32				posLoc		= ctx.getAttribLocation(program, "a_position");
65 	deInt32				coordLoc	= ctx.getAttribLocation(program, "a_coord");
66 	deUint32			vaoID;
67 	deUint32			bufIDs[2];
68 
69 	ctx.genVertexArrays(1, &vaoID);
70 	ctx.bindVertexArray(vaoID);
71 
72 	ctx.genBuffers(2, &bufIDs[0]);
73 
74 	ctx.useProgram(program);
75 	TCU_CHECK(posLoc >= 0);
76 	{
77 		ctx.bindBuffer(GL_ARRAY_BUFFER, bufIDs[0]);
78 		ctx.bufferData(GL_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(position)*sizeof(float), &position[0], GL_STATIC_DRAW);
79 
80 		ctx.enableVertexAttribArray(posLoc);
81 		ctx.vertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
82 
83 		ctx.bindBuffer(GL_ARRAY_BUFFER, 0);
84 	}
85 
86 	if (coordLoc >= 0)
87 	{
88 		ctx.bindBuffer(GL_ARRAY_BUFFER, bufIDs[1]);
89 		ctx.bufferData(GL_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(coord)*sizeof(float), &coord[0], GL_STATIC_DRAW);
90 
91 		ctx.enableVertexAttribArray(coordLoc);
92 		ctx.vertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
93 
94 		ctx.bindBuffer(GL_ARRAY_BUFFER, 0);
95 	}
96 
97 	{
98 		deUint32 ndxID;
99 		ctx.genBuffers(1, &ndxID);
100 
101 		ctx.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, ndxID);
102 		ctx.bufferData(GL_ELEMENT_ARRAY_BUFFER, DE_LENGTH_OF_ARRAY(indices)*sizeof(deUint16), &indices[0], GL_STATIC_DRAW);
103 
104 		ctx.drawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_SHORT, 0);
105 
106 		ctx.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
107 		ctx.deleteBuffers(1, &ndxID);
108 	}
109 
110 	ctx.deleteBuffers(2, &bufIDs[0]);
111 	ctx.deleteVertexArrays(1, &vaoID);
112 }
113 
drawQuadWithClientPointers(sglr::Context & ctx,deUint32 program,const tcu::Vec3 & p0,const tcu::Vec3 & p1)114 void drawQuadWithClientPointers (sglr::Context& ctx, deUint32 program, const tcu::Vec3& p0, const tcu::Vec3& p1)
115 {
116 	// Vertex data.
117 	float hz = (p0.z() + p1.z()) * 0.5f;
118 	float position[] =
119 	{
120 		p0.x(), p0.y(), p0.z(),	1.0f,
121 		p0.x(), p1.y(), hz,		1.0f,
122 		p1.x(), p0.y(), hz,		1.0f,
123 		p1.x(), p1.y(), p1.z(),	1.0f
124 	};
125 	const float coord[] =
126 	{
127 		0.0f, 0.0f,
128 		0.0f, 1.0f,
129 		1.0f, 0.0f,
130 		1.0f, 1.0f
131 	};
132 	const deUint16	indices[]	= { 0, 1, 2, 2, 1, 3 };
133 
134 	deInt32			posLoc		= ctx.getAttribLocation(program, "a_position");
135 	deInt32			coordLoc	= ctx.getAttribLocation(program, "a_coord");
136 
137 	ctx.useProgram(program);
138 	TCU_CHECK(posLoc >= 0);
139 	{
140 		ctx.enableVertexAttribArray(posLoc);
141 		ctx.vertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, &position[0]);
142 	}
143 
144 	if (coordLoc >= 0)
145 	{
146 		ctx.enableVertexAttribArray(coordLoc);
147 		ctx.vertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, &coord[0]);
148 	}
149 
150 	ctx.drawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_SHORT, &indices[0]);
151 
152 	if (posLoc >= 0)
153 		ctx.disableVertexAttribArray(posLoc);
154 
155 	if (coordLoc >= 0)
156 		ctx.disableVertexAttribArray(coordLoc);
157 }
158 
159 } //sglr
160