1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 Module
3 * -------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Instanced rendering tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fInstancedRenderingTests.hpp"
25 #include "gluPixelTransfer.hpp"
26 #include "gluShaderProgram.hpp"
27 #include "gluShaderUtil.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuSurface.hpp"
30 #include "tcuImageCompare.hpp"
31 #include "tcuVector.hpp"
32 #include "tcuRenderTarget.hpp"
33 #include "deRandom.hpp"
34 #include "deStringUtil.hpp"
49 static const int MAX_RENDER_WIDTH = 128;
50 static const int MAX_RENDER_HEIGHT = 128;
52 static const int QUAD_GRID_SIZE = 127;
54 // Attribute divisors for the attributes defining the color's RGB components.
55 static const int ATTRIB_DIVISOR_R = 3;
56 static const int ATTRIB_DIVISOR_G = 2;
57 static const int ATTRIB_DIVISOR_B = 1;
59 static const int OFFSET_COMPONENTS = 3; // \note Affects whether a float or a vecN is used in shader, but only first component is non-zero.
61 // Scale and bias values when converting float to integer, when attribute is of integer type.
62 static const float FLOAT_INT_SCALE = 100.0f;
63 static const float FLOAT_INT_BIAS = -50.0f;
64 static const float FLOAT_UINT_SCALE = 100.0f;
65 static const float FLOAT_UINT_BIAS = 0.0f;
67 // \note Non-anonymous namespace needed; VarComp is used as a template parameter.
77 VarComp(float v) : f32(v) {}
78 VarComp(deUint32 v) : u32(v) {}
79 VarComp(deInt32 v) : i32(v) {}
81 DE_STATIC_ASSERT(sizeof(VarComp) == sizeof(deUint32));
87 class InstancedRenderingCase : public TestCase
92 FUNCTION_DRAW_ARRAYS_INSTANCED = 0,
93 FUNCTION_DRAW_ELEMENTS_INSTANCED,
100 TYPE_INSTANCE_ID = 0,
107 InstancedRenderingCase (Context& context, const char* name, const char* description, DrawFunction function, InstancingType instancingType, glu::DataType rgbAttrType, int numInstances);
108 ~InstancedRenderingCase (void);
112 IterateResult iterate (void);
115 InstancedRenderingCase (const InstancedRenderingCase& other);
116 InstancedRenderingCase& operator= (const InstancedRenderingCase& other);
118 void pushVarCompAttrib (vector<VarComp>& vec, float val);
120 void setupVarAttribPointer (const void* attrPtr, int startLocation, int divisor);
121 void setupAndRender (void);
122 void computeReference (tcu::Surface& dst);
124 DrawFunction m_function;
125 InstancingType m_instancingType;
126 glu::DataType m_rgbAttrType; // \note Instance attribute types, color components only. Position offset attribute is always float/vecN.
129 vector<float> m_gridVertexPositions; // X and Y components per vertex.
130 vector<deUint16> m_gridIndices; // \note Only used if m_function is FUNCTION_DRAW_ELEMENTS_INSTANCED.
132 // \note Some or all of the following instance attribute parameters may be unused with TYPE_INSTANCE_ID or TYPE_MIXED.
133 vector<float> m_instanceOffsets; // Position offsets. OFFSET_COMPONENTS components per offset.
134 // Attribute data for float, int or uint (or respective vector types) color components.
135 vector<VarComp> m_instanceColorR;
136 vector<VarComp> m_instanceColorG;
137 vector<VarComp> m_instanceColorB;
139 glu::ShaderProgram* m_program;
142 InstancedRenderingCase::InstancedRenderingCase (Context& context, const char* name, const char* description, DrawFunction function, InstancingType instancingType, glu::DataType rgbAttrType, int numInstances)
143 : TestCase (context, name, description)
144 , m_function (function)
145 , m_instancingType (instancingType)
146 , m_rgbAttrType (rgbAttrType)
147 , m_numInstances (numInstances)
148 , m_program (DE_NULL)
152 InstancedRenderingCase::~InstancedRenderingCase (void)
154 InstancedRenderingCase::deinit();
157 // Helper function that does biasing and scaling when converting float to integer.
158 void InstancedRenderingCase::pushVarCompAttrib (vector<VarComp>& vec, float val)
160 bool isFloatCase = glu::isDataTypeFloatOrVec(m_rgbAttrType);
161 bool isIntCase = glu::isDataTypeIntOrIVec(m_rgbAttrType);
162 bool isUintCase = glu::isDataTypeUintOrUVec(m_rgbAttrType);
163 bool isMatCase = glu::isDataTypeMatrix(m_rgbAttrType);
165 if (isFloatCase || isMatCase)
166 vec.push_back(VarComp(val));
168 vec.push_back(VarComp((deInt32)(val*FLOAT_INT_SCALE + FLOAT_INT_BIAS)));
170 vec.push_back(VarComp((deUint32)(val*FLOAT_UINT_SCALE + FLOAT_UINT_BIAS)));
175 void InstancedRenderingCase::init (void)
177 bool isFloatCase = glu::isDataTypeFloatOrVec(m_rgbAttrType);
178 bool isIntCase = glu::isDataTypeIntOrIVec(m_rgbAttrType);
179 bool isUintCase = glu::isDataTypeUintOrUVec(m_rgbAttrType);
180 bool isMatCase = glu::isDataTypeMatrix(m_rgbAttrType);
181 int typeSize = glu::getDataTypeScalarSize(m_rgbAttrType);
182 bool isScalarCase = typeSize == 1;
183 string swizzleFirst = isScalarCase ? "" : ".x";
184 string typeName = glu::getDataTypeName(m_rgbAttrType);
186 string floatIntScaleStr = "(" + de::floatToString(FLOAT_INT_SCALE, 3) + ")";
187 string floatIntBiasStr = "(" + de::floatToString(FLOAT_INT_BIAS, 3) + ")";
188 string floatUintScaleStr = "(" + de::floatToString(FLOAT_UINT_SCALE, 3) + ")";
189 string floatUintBiasStr = "(" + de::floatToString(FLOAT_UINT_BIAS, 3) + ")";
191 DE_ASSERT(isFloatCase || isIntCase || isUintCase || isMatCase);
194 // \note For case TYPE_MIXED, vertex position offset and color red component get their values from instance id, while green and blue get their values from instanced attributes.
196 string numInstancesStr = de::toString(m_numInstances) + ".0";
198 string instanceAttribs;
199 string posExpression;
200 string colorRExpression;
201 string colorGExpression;
202 string colorBExpression;
204 if (m_instancingType == TYPE_INSTANCE_ID || m_instancingType == TYPE_MIXED)
206 posExpression = "a_position + vec4(float(gl_InstanceID) * 2.0 / " + numInstancesStr + ", 0.0, 0.0, 0.0)";
207 colorRExpression = "float(gl_InstanceID)/" + numInstancesStr;
209 if (m_instancingType == TYPE_INSTANCE_ID)
211 colorGExpression = "float(gl_InstanceID)*2.0/" + numInstancesStr;
212 colorBExpression = "1.0 - float(gl_InstanceID)/" + numInstancesStr;
216 if (m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED)
218 if (m_instancingType == TYPE_ATTRIB_DIVISOR)
220 posExpression = "a_position + vec4(a_instanceOffset";
222 DE_STATIC_ASSERT(OFFSET_COMPONENTS >= 1 && OFFSET_COMPONENTS <= 4);
224 for (int i = 0; i < 4-OFFSET_COMPONENTS; i++)
225 posExpression += ", 0.0";
226 posExpression += ")";
229 colorRExpression = "a_instanceR" + swizzleFirst;
231 colorRExpression = "(float(a_instanceR" + swizzleFirst + ") - " + floatIntBiasStr + ") / " + floatIntScaleStr;
233 colorRExpression = "(float(a_instanceR" + swizzleFirst + ") - " + floatUintBiasStr + ") / " + floatUintScaleStr;
235 colorRExpression = "a_instanceR[0][0]";
239 instanceAttribs += "in highp " + (OFFSET_COMPONENTS == 1 ? string("float") : "vec" + de::toString(OFFSET_COMPONENTS)) + " a_instanceOffset;\n";
240 instanceAttribs += "in mediump " + typeName + " a_instanceR;\n";
245 colorGExpression = "a_instanceG" + swizzleFirst;
246 colorBExpression = "a_instanceB" + swizzleFirst;
250 colorGExpression = "(float(a_instanceG" + swizzleFirst + ") - " + floatIntBiasStr + ") / " + floatIntScaleStr;
251 colorBExpression = "(float(a_instanceB" + swizzleFirst + ") - " + floatIntBiasStr + ") / " + floatIntScaleStr;
255 colorGExpression = "(float(a_instanceG" + swizzleFirst + ") - " + floatUintBiasStr + ") / " + floatUintScaleStr;
256 colorBExpression = "(float(a_instanceB" + swizzleFirst + ") - " + floatUintBiasStr + ") / " + floatUintScaleStr;
260 colorGExpression = "a_instanceG[0][0]";
261 colorBExpression = "a_instanceB[0][0]";
266 instanceAttribs += "in mediump " + typeName + " a_instanceG;\n";
267 instanceAttribs += "in mediump " + typeName + " a_instanceB;\n";
270 DE_ASSERT(!posExpression.empty());
271 DE_ASSERT(!colorRExpression.empty());
272 DE_ASSERT(!colorGExpression.empty());
273 DE_ASSERT(!colorBExpression.empty());
275 std::string vertShaderSourceStr =
277 "in highp vec4 a_position;\n" +
279 "out mediump vec4 v_color;\n"
283 " gl_Position = " + posExpression + ";\n"
284 " v_color.r = " + colorRExpression + ";\n"
285 " v_color.g = " + colorGExpression + ";\n"
286 " v_color.b = " + colorBExpression + ";\n"
287 " v_color.a = 1.0;\n"
290 static const char* fragShaderSource =
292 "layout(location = 0) out mediump vec4 o_color;\n"
293 "in mediump vec4 v_color;\n"
297 " o_color = v_color;\n"
300 // Create shader program and log it.
302 DE_ASSERT(!m_program);
303 m_program = new glu::ShaderProgram(m_context.getRenderContext(), glu::makeVtxFragSources(vertShaderSourceStr, fragShaderSource));
305 tcu::TestLog& log = m_testCtx.getLog();
309 if(!m_program->isOk())
310 TCU_FAIL("Failed to compile shader");
312 // Vertex shader attributes.
314 if (m_function == FUNCTION_DRAW_ELEMENTS_INSTANCED)
316 // Vertex positions. Positions form a vertical bar of width <screen width>/<number of instances>.
318 for (int y = 0; y < QUAD_GRID_SIZE + 1; y++)
319 for (int x = 0; x < QUAD_GRID_SIZE + 1; x++)
321 float fx = -1.0f + (float)x / (float)QUAD_GRID_SIZE * 2.0f / (float)m_numInstances;
322 float fy = -1.0f + (float)y / (float)QUAD_GRID_SIZE * 2.0f;
324 m_gridVertexPositions.push_back(fx);
325 m_gridVertexPositions.push_back(fy);
330 for (int y = 0; y < QUAD_GRID_SIZE; y++)
331 for (int x = 0; x < QUAD_GRID_SIZE; x++)
333 int ndx00 = y*(QUAD_GRID_SIZE + 1) + x;
334 int ndx10 = y*(QUAD_GRID_SIZE + 1) + x + 1;
335 int ndx01 = (y + 1)*(QUAD_GRID_SIZE + 1) + x;
336 int ndx11 = (y + 1)*(QUAD_GRID_SIZE + 1) + x + 1;
338 // Lower-left triangle of a quad.
339 m_gridIndices.push_back((deUint16)ndx00);
340 m_gridIndices.push_back((deUint16)ndx10);
341 m_gridIndices.push_back((deUint16)ndx01);
343 // Upper-right triangle of a quad.
344 m_gridIndices.push_back((deUint16)ndx11);
345 m_gridIndices.push_back((deUint16)ndx01);
346 m_gridIndices.push_back((deUint16)ndx10);
351 DE_ASSERT(m_function == FUNCTION_DRAW_ARRAYS_INSTANCED);
353 // Vertex positions. Positions form a vertical bar of width <screen width>/<number of instances>.
355 for (int y = 0; y < QUAD_GRID_SIZE; y++)
356 for (int x = 0; x < QUAD_GRID_SIZE; x++)
358 float fx0 = -1.0f + (float)(x+0) / (float)QUAD_GRID_SIZE * 2.0f / (float)m_numInstances;
359 float fx1 = -1.0f + (float)(x+1) / (float)QUAD_GRID_SIZE * 2.0f / (float)m_numInstances;
360 float fy0 = -1.0f + (float)(y+0) / (float)QUAD_GRID_SIZE * 2.0f;
361 float fy1 = -1.0f + (float)(y+1) / (float)QUAD_GRID_SIZE * 2.0f;
363 // Vertices of a quad's lower-left triangle: (fx0, fy0), (fx1, fy0) and (fx0, fy1)
364 m_gridVertexPositions.push_back(fx0);
365 m_gridVertexPositions.push_back(fy0);
366 m_gridVertexPositions.push_back(fx1);
367 m_gridVertexPositions.push_back(fy0);
368 m_gridVertexPositions.push_back(fx0);
369 m_gridVertexPositions.push_back(fy1);
371 // Vertices of a quad's upper-right triangle: (fx1, fy1), (fx0, fy1) and (fx1, fy0)
372 m_gridVertexPositions.push_back(fx1);
373 m_gridVertexPositions.push_back(fy1);
374 m_gridVertexPositions.push_back(fx0);
375 m_gridVertexPositions.push_back(fy1);
376 m_gridVertexPositions.push_back(fx1);
377 m_gridVertexPositions.push_back(fy0);
381 // Instanced attributes: position offset and color RGB components.
383 if (m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED)
385 if (m_instancingType == TYPE_ATTRIB_DIVISOR)
387 // Offsets are such that the vertical bars are drawn next to each other.
388 for (int i = 0; i < m_numInstances; i++)
390 m_instanceOffsets.push_back((float)i * 2.0f / (float)m_numInstances);
392 DE_STATIC_ASSERT(OFFSET_COMPONENTS >= 1 && OFFSET_COMPONENTS <= 4);
394 for (int j = 0; j < OFFSET_COMPONENTS-1; j++)
395 m_instanceOffsets.push_back(0.0f);
398 int rInstances = m_numInstances / ATTRIB_DIVISOR_R + (m_numInstances % ATTRIB_DIVISOR_R == 0 ? 0 : 1);
399 for (int i = 0; i < rInstances; i++)
401 pushVarCompAttrib(m_instanceColorR, (float)i / (float)rInstances);
403 for (int j = 0; j < typeSize - 1; j++)
404 pushVarCompAttrib(m_instanceColorR, 0.0f);
408 int gInstances = m_numInstances / ATTRIB_DIVISOR_G + (m_numInstances % ATTRIB_DIVISOR_G == 0 ? 0 : 1);
409 for (int i = 0; i < gInstances; i++)
411 pushVarCompAttrib(m_instanceColorG, (float)i*2.0f / (float)gInstances);
413 for (int j = 0; j < typeSize - 1; j++)
414 pushVarCompAttrib(m_instanceColorG, 0.0f);
417 int bInstances = m_numInstances / ATTRIB_DIVISOR_B + (m_numInstances % ATTRIB_DIVISOR_B == 0 ? 0 : 1);
418 for (int i = 0; i < bInstances; i++)
420 pushVarCompAttrib(m_instanceColorB, 1.0f - (float)i / (float)bInstances);
422 for (int j = 0; j < typeSize - 1; j++)
423 pushVarCompAttrib(m_instanceColorB, 0.0f);
428 void InstancedRenderingCase::deinit (void)
434 InstancedRenderingCase::IterateResult InstancedRenderingCase::iterate (void)
436 int width = deMin32(m_context.getRenderTarget().getWidth(), MAX_RENDER_WIDTH);
437 int height = deMin32(m_context.getRenderTarget().getHeight(), MAX_RENDER_HEIGHT);
439 int xOffsetMax = m_context.getRenderTarget().getWidth() - width;
440 int yOffsetMax = m_context.getRenderTarget().getHeight() - height;
442 de::Random rnd (deStringHash(getName()));
444 int xOffset = rnd.getInt(0, xOffsetMax);
445 int yOffset = rnd.getInt(0, yOffsetMax);
446 tcu::Surface referenceImg (width, height);
447 tcu::Surface resultImg (width, height);
451 glViewport(xOffset, yOffset, width, height);
455 glu::readPixels(m_context.getRenderContext(), xOffset, yOffset, resultImg.getAccess());
457 // Compute reference.
459 computeReference(referenceImg);
463 bool testOk = tcu::fuzzyCompare(m_testCtx.getLog(), "ComparisonResult", "Image comparison result", referenceImg, resultImg, 0.05f, tcu::COMPARE_LOG_RESULT);
465 m_testCtx.setTestResult(testOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
466 testOk ? "Pass" : "Fail");
471 void InstancedRenderingCase::setupVarAttribPointer (const void* attrPtr, int location, int divisor)
473 bool isFloatCase = glu::isDataTypeFloatOrVec(m_rgbAttrType);
474 bool isIntCase = glu::isDataTypeIntOrIVec(m_rgbAttrType);
475 bool isUintCase = glu::isDataTypeUintOrUVec(m_rgbAttrType);
476 bool isMatCase = glu::isDataTypeMatrix(m_rgbAttrType);
477 int typeSize = glu::getDataTypeScalarSize(m_rgbAttrType);
478 int numSlots = isMatCase ? glu::getDataTypeMatrixNumColumns(m_rgbAttrType) : 1; // Matrix uses as many attribute slots as it has columns.
480 for (int slotNdx = 0; slotNdx < numSlots; slotNdx++)
482 int curLoc = location + slotNdx;
484 glEnableVertexAttribArray(curLoc);
485 glVertexAttribDivisor(curLoc, divisor);
488 glVertexAttribPointer(curLoc, typeSize, GL_FLOAT, GL_FALSE, 0, attrPtr);
490 glVertexAttribIPointer(curLoc, typeSize, GL_INT, 0, attrPtr);
492 glVertexAttribIPointer(curLoc, typeSize, GL_UNSIGNED_INT, 0, attrPtr);
495 int numRows = glu::getDataTypeMatrixNumRows(m_rgbAttrType);
496 int numCols = glu::getDataTypeMatrixNumColumns(m_rgbAttrType);
498 glVertexAttribPointer(curLoc, numRows, GL_FLOAT, GL_FALSE, numCols*numRows*(int)sizeof(float), attrPtr);
505 void InstancedRenderingCase::setupAndRender (void)
507 deUint32 program = m_program->getProgram();
509 glUseProgram(program);
514 // Position attribute is non-instanced.
515 int positionLoc = glGetAttribLocation(program, "a_position");
516 glEnableVertexAttribArray(positionLoc);
517 glVertexAttribPointer(positionLoc, 2, GL_FLOAT, GL_FALSE, 0, &m_gridVertexPositions[0]);
519 if (m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED)
521 if (m_instancingType == TYPE_ATTRIB_DIVISOR)
523 // Position offset attribute is instanced with separate offset for every instance.
524 int offsetLoc = glGetAttribLocation(program, "a_instanceOffset");
525 glEnableVertexAttribArray(offsetLoc);
526 glVertexAttribDivisor(offsetLoc, 1);
527 glVertexAttribPointer(offsetLoc, OFFSET_COMPONENTS, GL_FLOAT, GL_FALSE, 0, &m_instanceOffsets[0]);
529 int rLoc = glGetAttribLocation(program, "a_instanceR");
530 setupVarAttribPointer((void*)&m_instanceColorR[0].u32, rLoc, ATTRIB_DIVISOR_R);
533 int gLoc = glGetAttribLocation(program, "a_instanceG");
534 setupVarAttribPointer((void*)&m_instanceColorG[0].u32, gLoc, ATTRIB_DIVISOR_G);
536 int bLoc = glGetAttribLocation(program, "a_instanceB");
537 setupVarAttribPointer((void*)&m_instanceColorB[0].u32, bLoc, ATTRIB_DIVISOR_B);
541 // Draw using appropriate function.
543 if (m_function == FUNCTION_DRAW_ARRAYS_INSTANCED)
545 const int numPositionComponents = 2;
546 glDrawArraysInstanced(GL_TRIANGLES, 0, ((int)m_gridVertexPositions.size() / numPositionComponents), m_numInstances);
549 glDrawElementsInstanced(GL_TRIANGLES, (int)m_gridIndices.size(), GL_UNSIGNED_SHORT, &m_gridIndices[0], m_numInstances);
554 void InstancedRenderingCase::computeReference (tcu::Surface& dst)
556 int wid = dst.getWidth();
557 int hei = dst.getHeight();
559 // Draw a rectangle (vertical bar) for each instance.
561 for (int instanceNdx = 0; instanceNdx < m_numInstances; instanceNdx++)
563 int xStart = instanceNdx * wid / m_numInstances;
564 int xEnd = (instanceNdx + 1) * wid / m_numInstances;
566 // Emulate attribute divisors if that is the case.
568 int clrNdxR = m_instancingType == TYPE_ATTRIB_DIVISOR ? instanceNdx / ATTRIB_DIVISOR_R : instanceNdx;
569 int clrNdxG = m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED ? instanceNdx / ATTRIB_DIVISOR_G : instanceNdx;
570 int clrNdxB = m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED ? instanceNdx / ATTRIB_DIVISOR_B : instanceNdx;
572 int rInstances = m_instancingType == TYPE_ATTRIB_DIVISOR ? m_numInstances / ATTRIB_DIVISOR_R + (m_numInstances % ATTRIB_DIVISOR_R == 0 ? 0 : 1) : m_numInstances;
573 int gInstances = m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED ? m_numInstances / ATTRIB_DIVISOR_G + (m_numInstances % ATTRIB_DIVISOR_G == 0 ? 0 : 1) : m_numInstances;
574 int bInstances = m_instancingType == TYPE_ATTRIB_DIVISOR || m_instancingType == TYPE_MIXED ? m_numInstances / ATTRIB_DIVISOR_B + (m_numInstances % ATTRIB_DIVISOR_B == 0 ? 0 : 1) : m_numInstances;
578 float r = (float)clrNdxR / (float)rInstances;
579 float g = (float)clrNdxG * 2.0f / (float)gInstances;
580 float b = 1.0f - (float)clrNdxB / (float)bInstances;
582 // Convert to integer and back if shader inputs are integers.
584 if (glu::isDataTypeIntOrIVec(m_rgbAttrType))
586 deInt32 intR = (deInt32)(r*FLOAT_INT_SCALE + FLOAT_INT_BIAS);
587 deInt32 intG = (deInt32)(g*FLOAT_INT_SCALE + FLOAT_INT_BIAS);
588 deInt32 intB = (deInt32)(b*FLOAT_INT_SCALE + FLOAT_INT_BIAS);
589 r = ((float)intR - FLOAT_INT_BIAS) / FLOAT_INT_SCALE;
590 g = ((float)intG - FLOAT_INT_BIAS) / FLOAT_INT_SCALE;
591 b = ((float)intB - FLOAT_INT_BIAS) / FLOAT_INT_SCALE;
593 else if(glu::isDataTypeUintOrUVec(m_rgbAttrType))
595 deUint32 uintR = (deInt32)(r*FLOAT_UINT_SCALE + FLOAT_UINT_BIAS);
596 deUint32 uintG = (deInt32)(g*FLOAT_UINT_SCALE + FLOAT_UINT_BIAS);
597 deUint32 uintB = (deInt32)(b*FLOAT_UINT_SCALE + FLOAT_UINT_BIAS);
598 r = ((float)uintR - FLOAT_UINT_BIAS) / FLOAT_UINT_SCALE;
599 g = ((float)uintG - FLOAT_UINT_BIAS) / FLOAT_UINT_SCALE;
600 b = ((float)uintB - FLOAT_UINT_BIAS) / FLOAT_UINT_SCALE;
605 for (int y = 0; y < hei; y++)
606 for (int x = xStart; x < xEnd; x++)
607 dst.setPixel(x, y, tcu::RGBA(tcu::Vec4(r, g, b, 1.0f)));
611 InstancedRenderingTests::InstancedRenderingTests (Context& context)
612 : TestCaseGroup(context, "instanced", "Instanced rendering tests")
616 InstancedRenderingTests::~InstancedRenderingTests (void)
620 void InstancedRenderingTests::init (void)
622 // Cases testing function, instancing method and instance count.
624 static const int instanceCounts[] = { 1, 2, 4, 20 };
626 for (int function = 0; function < (int)InstancedRenderingCase::FUNCTION_LAST; function++)
628 const char* functionName = function == (int)InstancedRenderingCase::FUNCTION_DRAW_ARRAYS_INSTANCED ? "draw_arrays_instanced"
629 : function == (int)InstancedRenderingCase::FUNCTION_DRAW_ELEMENTS_INSTANCED ? "draw_elements_instanced"
632 const char* functionDesc = function == (int)InstancedRenderingCase::FUNCTION_DRAW_ARRAYS_INSTANCED ? "Use glDrawArraysInstanced()"
633 : function == (int)InstancedRenderingCase::FUNCTION_DRAW_ELEMENTS_INSTANCED ? "Use glDrawElementsInstanced()"
636 DE_ASSERT(functionName != DE_NULL);
637 DE_ASSERT(functionDesc != DE_NULL);
639 TestCaseGroup* functionGroup = new TestCaseGroup(m_context, functionName, functionDesc);
640 addChild(functionGroup);
642 for (int instancingType = 0; instancingType < (int)InstancedRenderingCase::TYPE_LAST; instancingType++)
644 const char* instancingTypeName = instancingType == (int)InstancedRenderingCase::TYPE_INSTANCE_ID ? "instance_id"
645 : instancingType == (int)InstancedRenderingCase::TYPE_ATTRIB_DIVISOR ? "attribute_divisor"
646 : instancingType == (int)InstancedRenderingCase::TYPE_MIXED ? "mixed"
649 const char* instancingTypeDesc = instancingType == (int)InstancedRenderingCase::TYPE_INSTANCE_ID ? "Use gl_InstanceID for instancing"
650 : instancingType == (int)InstancedRenderingCase::TYPE_ATTRIB_DIVISOR ? "Use vertex attribute divisors for instancing"
651 : instancingType == (int)InstancedRenderingCase::TYPE_MIXED ? "Use both gl_InstanceID and vertex attribute divisors for instancing"
654 DE_ASSERT(instancingTypeName != DE_NULL);
655 DE_ASSERT(instancingTypeDesc != DE_NULL);
657 TestCaseGroup* instancingTypeGroup = new TestCaseGroup(m_context, instancingTypeName, instancingTypeDesc);
658 functionGroup->addChild(instancingTypeGroup);
660 for (int countNdx = 0; countNdx < DE_LENGTH_OF_ARRAY(instanceCounts); countNdx++)
662 std::string countName = de::toString(instanceCounts[countNdx]) + "_instances";
664 instancingTypeGroup->addChild(new InstancedRenderingCase(m_context, countName.c_str(), "",
665 (InstancedRenderingCase::DrawFunction)function,
666 (InstancedRenderingCase::InstancingType)instancingType,
668 instanceCounts[countNdx]));
673 // Data type specific cases.
675 static const glu::DataType s_testTypes[] =
678 glu::TYPE_FLOAT_VEC2,
679 glu::TYPE_FLOAT_VEC3,
680 glu::TYPE_FLOAT_VEC4,
681 glu::TYPE_FLOAT_MAT2,
682 glu::TYPE_FLOAT_MAT2X3,
683 glu::TYPE_FLOAT_MAT2X4,
684 glu::TYPE_FLOAT_MAT3X2,
685 glu::TYPE_FLOAT_MAT3,
686 glu::TYPE_FLOAT_MAT3X4,
687 glu::TYPE_FLOAT_MAT4X2,
688 glu::TYPE_FLOAT_MAT4X3,
689 glu::TYPE_FLOAT_MAT4,
702 const int typeTestNumInstances = 4;
704 TestCaseGroup* typesGroup = new TestCaseGroup(m_context, "types", "Tests for instanced attributes of particular data types");
705 addChild(typesGroup);
707 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_testTypes); typeNdx++)
709 glu::DataType type = s_testTypes[typeNdx];
711 typesGroup->addChild(new InstancedRenderingCase(m_context, glu::getDataTypeName(type), "",
712 InstancedRenderingCase::FUNCTION_DRAW_ARRAYS_INSTANCED,
713 InstancedRenderingCase::TYPE_ATTRIB_DIVISOR,
715 typeTestNumInstances));