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 Shader indexing (arrays, vector, matrices) tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fShaderIndexingTests.hpp"
25 #include "glsShaderRenderCase.hpp"
26 #include "gluShaderUtil.hpp"
27 #include "tcuStringTemplate.hpp"
34 #include "glwEnums.hpp"
35 #include "glwFunctions.hpp"
40 using namespace deqp::gls;
51 INDEXACCESS_STATIC = 0,
53 INDEXACCESS_STATIC_LOOP,
54 INDEXACCESS_DYNAMIC_LOOP,
59 static const char* getIndexAccessTypeName (IndexAccessType accessType)
61 static const char* s_names[INDEXACCESS_LAST] =
69 DE_ASSERT(deInBounds32((int)accessType, 0, INDEXACCESS_LAST));
70 return s_names[(int)accessType];
79 SUBSCRIPT_STATIC_LOOP,
80 SUBSCRIPT_DYNAMIC_LOOP,
85 static const char* getVectorAccessTypeName (VectorAccessType accessType)
87 static const char* s_names[VECTORACCESS_LAST] =
93 "static_loop_subscript",
94 "dynamic_loop_subscript"
97 DE_ASSERT(deInBounds32((int)accessType, 0, VECTORACCESS_LAST));
98 return s_names[(int)accessType];
101 void evalArrayCoordsFloat (ShaderEvalContext& c) { c.color.x() = 1.875f * c.coords.x(); }
102 void evalArrayCoordsVec2 (ShaderEvalContext& c) { c.color.xy() = 1.875f * c.coords.swizzle(0,1); }
103 void evalArrayCoordsVec3 (ShaderEvalContext& c) { c.color.xyz() = 1.875f * c.coords.swizzle(0,1,2); }
104 void evalArrayCoordsVec4 (ShaderEvalContext& c) { c.color = 1.875f * c.coords; }
106 static ShaderEvalFunc getArrayCoordsEvalFunc (DataType dataType)
108 if (dataType == TYPE_FLOAT) return evalArrayCoordsFloat;
109 else if (dataType == TYPE_FLOAT_VEC2) return evalArrayCoordsVec2;
110 else if (dataType == TYPE_FLOAT_VEC3) return evalArrayCoordsVec3;
111 else if (dataType == TYPE_FLOAT_VEC4) return evalArrayCoordsVec4;
113 DE_FATAL("Invalid data type.");
117 void evalArrayUniformFloat (ShaderEvalContext& c) { c.color.x() = 1.875f * c.constCoords.x(); }
118 void evalArrayUniformVec2 (ShaderEvalContext& c) { c.color.xy() = 1.875f * c.constCoords.swizzle(0,1); }
119 void evalArrayUniformVec3 (ShaderEvalContext& c) { c.color.xyz() = 1.875f * c.constCoords.swizzle(0,1,2); }
120 void evalArrayUniformVec4 (ShaderEvalContext& c) { c.color = 1.875f * c.constCoords; }
122 static ShaderEvalFunc getArrayUniformEvalFunc (DataType dataType)
124 if (dataType == TYPE_FLOAT) return evalArrayUniformFloat;
125 else if (dataType == TYPE_FLOAT_VEC2) return evalArrayUniformVec2;
126 else if (dataType == TYPE_FLOAT_VEC3) return evalArrayUniformVec3;
127 else if (dataType == TYPE_FLOAT_VEC4) return evalArrayUniformVec4;
129 DE_FATAL("Invalid data type.");
133 // ShaderIndexingCase
135 class ShaderIndexingCase : public ShaderRenderCase
138 ShaderIndexingCase (Context& context, const char* name, const char* description, bool isVertexCase, DataType varType, ShaderEvalFunc evalFunc, const char* vertShaderSource, const char* fragShaderSource);
139 virtual ~ShaderIndexingCase (void);
142 ShaderIndexingCase (const ShaderIndexingCase&); // not allowed!
143 ShaderIndexingCase& operator= (const ShaderIndexingCase&); // not allowed!
145 virtual void setup (int programID);
146 virtual void setupUniforms (int programID, const Vec4& constCoords);
151 ShaderIndexingCase::ShaderIndexingCase (Context& context, const char* name, const char* description, bool isVertexCase, DataType varType, ShaderEvalFunc evalFunc, const char* vertShaderSource, const char* fragShaderSource)
152 : ShaderRenderCase(context.getTestContext(), context.getRenderContext(), context.getContextInfo(), name, description, isVertexCase, evalFunc)
155 m_vertShaderSource = vertShaderSource;
156 m_fragShaderSource = fragShaderSource;
159 ShaderIndexingCase::~ShaderIndexingCase (void)
163 void ShaderIndexingCase::setup (int programID)
168 void ShaderIndexingCase::setupUniforms (int programID, const Vec4& constCoords)
170 const glw::Functions& gl = m_renderCtx.getFunctions();
172 DE_UNREF(constCoords);
174 int arrLoc = gl.getUniformLocation(programID, "u_arr");
177 //int scalarSize = getDataTypeScalarSize(m_varType);
178 if (m_varType == TYPE_FLOAT)
181 arr[0] = constCoords.x();
182 arr[1] = constCoords.x() * 0.5f;
183 arr[2] = constCoords.x() * 0.25f;
184 arr[3] = constCoords.x() * 0.125f;
185 gl.uniform1fv(arrLoc, 4, &arr[0]);
187 else if (m_varType == TYPE_FLOAT_VEC2)
190 arr[0] = constCoords.swizzle(0,1);
191 arr[1] = constCoords.swizzle(0,1) * 0.5f;
192 arr[2] = constCoords.swizzle(0,1) * 0.25f;
193 arr[3] = constCoords.swizzle(0,1) * 0.125f;
194 gl.uniform2fv(arrLoc, 4, arr[0].getPtr());
196 else if (m_varType == TYPE_FLOAT_VEC3)
199 arr[0] = constCoords.swizzle(0,1,2);
200 arr[1] = constCoords.swizzle(0,1,2) * 0.5f;
201 arr[2] = constCoords.swizzle(0,1,2) * 0.25f;
202 arr[3] = constCoords.swizzle(0,1,2) * 0.125f;
203 gl.uniform3fv(arrLoc, 4, arr[0].getPtr());
205 else if (m_varType == TYPE_FLOAT_VEC4)
208 arr[0] = constCoords.swizzle(0,1,2,3);
209 arr[1] = constCoords.swizzle(0,1,2,3) * 0.5f;
210 arr[2] = constCoords.swizzle(0,1,2,3) * 0.25f;
211 arr[3] = constCoords.swizzle(0,1,2,3) * 0.125f;
212 gl.uniform4fv(arrLoc, 4, arr[0].getPtr());
215 throw tcu::TestError("u_arr should not have location assigned in this test case");
221 static ShaderIndexingCase* createVaryingArrayCase (Context& context, const char* caseName, const char* description, DataType varType, IndexAccessType vertAccess, IndexAccessType fragAccess)
223 std::ostringstream vtx;
224 vtx << "#version 300 es\n";
225 vtx << "in highp vec4 a_position;\n";
226 vtx << "in highp vec4 a_coords;\n";
227 if (vertAccess == INDEXACCESS_DYNAMIC)
228 vtx << "uniform mediump int ui_zero, ui_one, ui_two, ui_three;\n";
229 else if (vertAccess == INDEXACCESS_DYNAMIC_LOOP)
230 vtx << "uniform mediump int ui_four;\n";
231 vtx << "out ${PRECISION} ${VAR_TYPE} var[${ARRAY_LEN}];\n";
233 vtx << "void main()\n";
235 vtx << " gl_Position = a_position;\n";
236 if (vertAccess == INDEXACCESS_STATIC)
238 vtx << " var[0] = ${VAR_TYPE}(a_coords);\n";
239 vtx << " var[1] = ${VAR_TYPE}(a_coords) * 0.5;\n";
240 vtx << " var[2] = ${VAR_TYPE}(a_coords) * 0.25;\n";
241 vtx << " var[3] = ${VAR_TYPE}(a_coords) * 0.125;\n";
243 else if (vertAccess == INDEXACCESS_DYNAMIC)
245 vtx << " var[ui_zero] = ${VAR_TYPE}(a_coords);\n";
246 vtx << " var[ui_one] = ${VAR_TYPE}(a_coords) * 0.5;\n";
247 vtx << " var[ui_two] = ${VAR_TYPE}(a_coords) * 0.25;\n";
248 vtx << " var[ui_three] = ${VAR_TYPE}(a_coords) * 0.125;\n";
250 else if (vertAccess == INDEXACCESS_STATIC_LOOP)
252 vtx << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(a_coords);\n";
253 vtx << " for (int i = 0; i < 4; i++)\n";
255 vtx << " var[i] = ${VAR_TYPE}(coords);\n";
256 vtx << " coords = coords * 0.5;\n";
261 DE_ASSERT(vertAccess == INDEXACCESS_DYNAMIC_LOOP);
262 vtx << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(a_coords);\n";
263 vtx << " for (int i = 0; i < ui_four; i++)\n";
265 vtx << " var[i] = ${VAR_TYPE}(coords);\n";
266 vtx << " coords = coords * 0.5;\n";
271 std::ostringstream frag;
272 frag << "#version 300 es\n";
273 frag << "precision mediump int;\n";
274 frag << "layout(location = 0) out mediump vec4 o_color;\n";
275 if (fragAccess == INDEXACCESS_DYNAMIC)
276 frag << "uniform mediump int ui_zero, ui_one, ui_two, ui_three;\n";
277 else if (fragAccess == INDEXACCESS_DYNAMIC_LOOP)
278 frag << "uniform int ui_four;\n";
279 frag << "in ${PRECISION} ${VAR_TYPE} var[${ARRAY_LEN}];\n";
281 frag << "void main()\n";
283 frag << " ${PRECISION} ${VAR_TYPE} res = ${VAR_TYPE}(0.0);\n";
284 if (fragAccess == INDEXACCESS_STATIC)
286 frag << " res += var[0];\n";
287 frag << " res += var[1];\n";
288 frag << " res += var[2];\n";
289 frag << " res += var[3];\n";
291 else if (fragAccess == INDEXACCESS_DYNAMIC)
293 frag << " res += var[ui_zero];\n";
294 frag << " res += var[ui_one];\n";
295 frag << " res += var[ui_two];\n";
296 frag << " res += var[ui_three];\n";
298 else if (fragAccess == INDEXACCESS_STATIC_LOOP)
300 frag << " for (int i = 0; i < 4; i++)\n";
301 frag << " res += var[i];\n";
305 DE_ASSERT(fragAccess == INDEXACCESS_DYNAMIC_LOOP);
306 frag << " for (int i = 0; i < ui_four; i++)\n";
307 frag << " res += var[i];\n";
309 frag << " o_color = vec4(res${PADDING});\n";
312 // Fill in shader templates.
313 map<string, string> params;
314 params.insert(pair<string, string>("VAR_TYPE", getDataTypeName(varType)));
315 params.insert(pair<string, string>("ARRAY_LEN", "4"));
316 params.insert(pair<string, string>("PRECISION", "mediump"));
318 if (varType == TYPE_FLOAT)
319 params.insert(pair<string, string>("PADDING", ", 0.0, 0.0, 1.0"));
320 else if (varType == TYPE_FLOAT_VEC2)
321 params.insert(pair<string, string>("PADDING", ", 0.0, 1.0"));
322 else if (varType == TYPE_FLOAT_VEC3)
323 params.insert(pair<string, string>("PADDING", ", 1.0"));
325 params.insert(pair<string, string>("PADDING", ""));
327 StringTemplate vertTemplate(vtx.str().c_str());
328 StringTemplate fragTemplate(frag.str().c_str());
329 string vertexShaderSource = vertTemplate.specialize(params);
330 string fragmentShaderSource = fragTemplate.specialize(params);
332 ShaderEvalFunc evalFunc = getArrayCoordsEvalFunc(varType);
333 return new ShaderIndexingCase(context, caseName, description, true, varType, evalFunc, vertexShaderSource.c_str(), fragmentShaderSource.c_str());
336 static ShaderIndexingCase* createUniformArrayCase (Context& context, const char* caseName, const char* description, bool isVertexCase, DataType varType, IndexAccessType readAccess)
338 std::ostringstream vtx;
339 std::ostringstream frag;
340 std::ostringstream& op = isVertexCase ? vtx : frag;
342 vtx << "#version 300 es\n";
343 frag << "#version 300 es\n";
345 vtx << "in highp vec4 a_position;\n";
346 vtx << "in highp vec4 a_coords;\n";
347 frag << "layout(location = 0) out mediump vec4 o_color;\n";
351 vtx << "out mediump vec4 v_color;\n";
352 frag << "in mediump vec4 v_color;\n";
356 vtx << "out mediump vec4 v_coords;\n";
357 frag << "in mediump vec4 v_coords;\n";
360 if (readAccess == INDEXACCESS_DYNAMIC)
361 op << "uniform mediump int ui_zero, ui_one, ui_two, ui_three;\n";
362 else if (readAccess == INDEXACCESS_DYNAMIC_LOOP)
363 op << "uniform mediump int ui_four;\n";
365 op << "uniform ${PRECISION} ${VAR_TYPE} u_arr[${ARRAY_LEN}];\n";
368 vtx << "void main()\n";
370 vtx << " gl_Position = a_position;\n";
373 frag << "void main()\n";
377 op << " ${PRECISION} ${VAR_TYPE} res = ${VAR_TYPE}(0.0);\n";
378 if (readAccess == INDEXACCESS_STATIC)
380 op << " res += u_arr[0];\n";
381 op << " res += u_arr[1];\n";
382 op << " res += u_arr[2];\n";
383 op << " res += u_arr[3];\n";
385 else if (readAccess == INDEXACCESS_DYNAMIC)
387 op << " res += u_arr[ui_zero];\n";
388 op << " res += u_arr[ui_one];\n";
389 op << " res += u_arr[ui_two];\n";
390 op << " res += u_arr[ui_three];\n";
392 else if (readAccess == INDEXACCESS_STATIC_LOOP)
394 op << " for (int i = 0; i < 4; i++)\n";
395 op << " res += u_arr[i];\n";
399 DE_ASSERT(readAccess == INDEXACCESS_DYNAMIC_LOOP);
400 op << " for (int i = 0; i < ui_four; i++)\n";
401 op << " res += u_arr[i];\n";
406 vtx << " v_color = vec4(res${PADDING});\n";
407 frag << " o_color = v_color;\n";
411 vtx << " v_coords = a_coords;\n";
412 frag << " o_color = vec4(res${PADDING});\n";
418 // Fill in shader templates.
419 map<string, string> params;
420 params.insert(pair<string, string>("VAR_TYPE", getDataTypeName(varType)));
421 params.insert(pair<string, string>("ARRAY_LEN", "4"));
422 params.insert(pair<string, string>("PRECISION", "mediump"));
424 if (varType == TYPE_FLOAT)
425 params.insert(pair<string, string>("PADDING", ", 0.0, 0.0, 1.0"));
426 else if (varType == TYPE_FLOAT_VEC2)
427 params.insert(pair<string, string>("PADDING", ", 0.0, 1.0"));
428 else if (varType == TYPE_FLOAT_VEC3)
429 params.insert(pair<string, string>("PADDING", ", 1.0"));
431 params.insert(pair<string, string>("PADDING", ""));
433 StringTemplate vertTemplate(vtx.str().c_str());
434 StringTemplate fragTemplate(frag.str().c_str());
435 string vertexShaderSource = vertTemplate.specialize(params);
436 string fragmentShaderSource = fragTemplate.specialize(params);
438 ShaderEvalFunc evalFunc = getArrayUniformEvalFunc(varType);
439 return new ShaderIndexingCase(context, caseName, description, isVertexCase, varType, evalFunc, vertexShaderSource.c_str(), fragmentShaderSource.c_str());
442 static ShaderIndexingCase* createTmpArrayCase (Context& context, const char* caseName, const char* description, bool isVertexCase, DataType varType, IndexAccessType writeAccess, IndexAccessType readAccess)
444 std::ostringstream vtx;
445 std::ostringstream frag;
446 std::ostringstream& op = isVertexCase ? vtx : frag;
448 vtx << "#version 300 es\n";
449 frag << "#version 300 es\n";
451 vtx << "in highp vec4 a_position;\n";
452 vtx << "in highp vec4 a_coords;\n";
453 frag << "layout(location = 0) out mediump vec4 o_color;\n";
457 vtx << "out mediump vec4 v_color;\n";
458 frag << "in mediump vec4 v_color;\n";
462 vtx << "out mediump vec4 v_coords;\n";
463 frag << "in mediump vec4 v_coords;\n";
466 if (writeAccess == INDEXACCESS_DYNAMIC || readAccess == INDEXACCESS_DYNAMIC)
467 op << "uniform mediump int ui_zero, ui_one, ui_two, ui_three;\n";
469 if (writeAccess == INDEXACCESS_DYNAMIC_LOOP || readAccess == INDEXACCESS_DYNAMIC_LOOP)
470 op << "uniform mediump int ui_four;\n";
473 vtx << "void main()\n";
475 vtx << " gl_Position = a_position;\n";
478 frag << "void main()\n";
483 op << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(a_coords);\n";
485 op << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(v_coords);\n";
487 op << " ${PRECISION} ${VAR_TYPE} arr[${ARRAY_LEN}];\n";
488 if (writeAccess == INDEXACCESS_STATIC)
490 op << " arr[0] = ${VAR_TYPE}(coords);\n";
491 op << " arr[1] = ${VAR_TYPE}(coords) * 0.5;\n";
492 op << " arr[2] = ${VAR_TYPE}(coords) * 0.25;\n";
493 op << " arr[3] = ${VAR_TYPE}(coords) * 0.125;\n";
495 else if (writeAccess == INDEXACCESS_DYNAMIC)
497 op << " arr[ui_zero] = ${VAR_TYPE}(coords);\n";
498 op << " arr[ui_one] = ${VAR_TYPE}(coords) * 0.5;\n";
499 op << " arr[ui_two] = ${VAR_TYPE}(coords) * 0.25;\n";
500 op << " arr[ui_three] = ${VAR_TYPE}(coords) * 0.125;\n";
502 else if (writeAccess == INDEXACCESS_STATIC_LOOP)
504 op << " for (int i = 0; i < 4; i++)\n";
506 op << " arr[i] = ${VAR_TYPE}(coords);\n";
507 op << " coords = coords * 0.5;\n";
512 DE_ASSERT(writeAccess == INDEXACCESS_DYNAMIC_LOOP);
513 op << " for (int i = 0; i < ui_four; i++)\n";
515 op << " arr[i] = ${VAR_TYPE}(coords);\n";
516 op << " coords = coords * 0.5;\n";
521 op << " ${PRECISION} ${VAR_TYPE} res = ${VAR_TYPE}(0.0);\n";
522 if (readAccess == INDEXACCESS_STATIC)
524 op << " res += arr[0];\n";
525 op << " res += arr[1];\n";
526 op << " res += arr[2];\n";
527 op << " res += arr[3];\n";
529 else if (readAccess == INDEXACCESS_DYNAMIC)
531 op << " res += arr[ui_zero];\n";
532 op << " res += arr[ui_one];\n";
533 op << " res += arr[ui_two];\n";
534 op << " res += arr[ui_three];\n";
536 else if (readAccess == INDEXACCESS_STATIC_LOOP)
538 op << " for (int i = 0; i < 4; i++)\n";
539 op << " res += arr[i];\n";
543 DE_ASSERT(readAccess == INDEXACCESS_DYNAMIC_LOOP);
544 op << " for (int i = 0; i < ui_four; i++)\n";
545 op << " res += arr[i];\n";
550 vtx << " v_color = vec4(res${PADDING});\n";
551 frag << " o_color = v_color;\n";
555 vtx << " v_coords = a_coords;\n";
556 frag << " o_color = vec4(res${PADDING});\n";
562 // Fill in shader templates.
563 map<string, string> params;
564 params.insert(pair<string, string>("VAR_TYPE", getDataTypeName(varType)));
565 params.insert(pair<string, string>("ARRAY_LEN", "4"));
566 params.insert(pair<string, string>("PRECISION", "mediump"));
568 if (varType == TYPE_FLOAT)
569 params.insert(pair<string, string>("PADDING", ", 0.0, 0.0, 1.0"));
570 else if (varType == TYPE_FLOAT_VEC2)
571 params.insert(pair<string, string>("PADDING", ", 0.0, 1.0"));
572 else if (varType == TYPE_FLOAT_VEC3)
573 params.insert(pair<string, string>("PADDING", ", 1.0"));
575 params.insert(pair<string, string>("PADDING", ""));
577 StringTemplate vertTemplate(vtx.str().c_str());
578 StringTemplate fragTemplate(frag.str().c_str());
579 string vertexShaderSource = vertTemplate.specialize(params);
580 string fragmentShaderSource = fragTemplate.specialize(params);
582 ShaderEvalFunc evalFunc = getArrayCoordsEvalFunc(varType);
583 return new ShaderIndexingCase(context, caseName, description, isVertexCase, varType, evalFunc, vertexShaderSource.c_str(), fragmentShaderSource.c_str());
588 void evalSubscriptVec2 (ShaderEvalContext& c) { c.color.xyz() = Vec3(c.coords.x() + 0.5f*c.coords.y()); }
589 void evalSubscriptVec3 (ShaderEvalContext& c) { c.color.xyz() = Vec3(c.coords.x() + 0.5f*c.coords.y() + 0.25f*c.coords.z()); }
590 void evalSubscriptVec4 (ShaderEvalContext& c) { c.color.xyz() = Vec3(c.coords.x() + 0.5f*c.coords.y() + 0.25f*c.coords.z() + 0.125f*c.coords.w()); }
592 static ShaderEvalFunc getVectorSubscriptEvalFunc (DataType dataType)
594 if (dataType == TYPE_FLOAT_VEC2) return evalSubscriptVec2;
595 else if (dataType == TYPE_FLOAT_VEC3) return evalSubscriptVec3;
596 else if (dataType == TYPE_FLOAT_VEC4) return evalSubscriptVec4;
598 DE_FATAL("Invalid data type.");
602 static ShaderIndexingCase* createVectorSubscriptCase (Context& context, const char* caseName, const char* description, bool isVertexCase, DataType varType, VectorAccessType writeAccess, VectorAccessType readAccess)
604 std::ostringstream vtx;
605 std::ostringstream frag;
606 std::ostringstream& op = isVertexCase ? vtx : frag;
608 int vecLen = getDataTypeScalarSize(varType);
609 const char* vecLenName = getIntUniformName(vecLen);
611 vtx << "#version 300 es\n";
612 frag << "#version 300 es\n";
614 vtx << "in highp vec4 a_position;\n";
615 vtx << "in highp vec4 a_coords;\n";
616 frag << "layout(location = 0) out mediump vec4 o_color;\n";
620 vtx << "out mediump vec3 v_color;\n";
621 frag << "in mediump vec3 v_color;\n";
625 vtx << "out mediump vec4 v_coords;\n";
626 frag << "in mediump vec4 v_coords;\n";
629 if (writeAccess == SUBSCRIPT_DYNAMIC || readAccess == SUBSCRIPT_DYNAMIC)
631 op << "uniform mediump int ui_zero";
632 if (vecLen >= 2) op << ", ui_one";
633 if (vecLen >= 3) op << ", ui_two";
634 if (vecLen >= 4) op << ", ui_three";
638 if (writeAccess == SUBSCRIPT_DYNAMIC_LOOP || readAccess == SUBSCRIPT_DYNAMIC_LOOP)
639 op << "uniform mediump int " << vecLenName << ";\n";
642 vtx << "void main()\n";
644 vtx << " gl_Position = a_position;\n";
647 frag << "void main()\n";
652 op << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(a_coords);\n";
654 op << " ${PRECISION} ${VAR_TYPE} coords = ${VAR_TYPE}(v_coords);\n";
656 op << " ${PRECISION} ${VAR_TYPE} tmp;\n";
657 if (writeAccess == DIRECT)
658 op << " tmp = coords.${SWIZZLE} * vec4(1.0, 0.5, 0.25, 0.125).${SWIZZLE};\n";
659 else if (writeAccess == COMPONENT)
661 op << " tmp.x = coords.x;\n";
662 if (vecLen >= 2) op << " tmp.y = coords.y * 0.5;\n";
663 if (vecLen >= 3) op << " tmp.z = coords.z * 0.25;\n";
664 if (vecLen >= 4) op << " tmp.w = coords.w * 0.125;\n";
666 else if (writeAccess == SUBSCRIPT_STATIC)
668 op << " tmp[0] = coords.x;\n";
669 if (vecLen >= 2) op << " tmp[1] = coords.y * 0.5;\n";
670 if (vecLen >= 3) op << " tmp[2] = coords.z * 0.25;\n";
671 if (vecLen >= 4) op << " tmp[3] = coords.w * 0.125;\n";
673 else if (writeAccess == SUBSCRIPT_DYNAMIC)
675 op << " tmp[ui_zero] = coords.x;\n";
676 if (vecLen >= 2) op << " tmp[ui_one] = coords.y * 0.5;\n";
677 if (vecLen >= 3) op << " tmp[ui_two] = coords.z * 0.25;\n";
678 if (vecLen >= 4) op << " tmp[ui_three] = coords.w * 0.125;\n";
680 else if (writeAccess == SUBSCRIPT_STATIC_LOOP)
682 op << " for (int i = 0; i < " << vecLen << "; i++)\n";
684 op << " tmp[i] = coords.x;\n";
685 op << " coords = coords.${ROT_SWIZZLE} * 0.5;\n";
690 DE_ASSERT(writeAccess == SUBSCRIPT_DYNAMIC_LOOP);
691 op << " for (int i = 0; i < " << vecLenName << "; i++)\n";
693 op << " tmp[i] = coords.x;\n";
694 op << " coords = coords.${ROT_SWIZZLE} * 0.5;\n";
699 op << " ${PRECISION} float res = 0.0;\n";
700 if (readAccess == DIRECT)
701 op << " res = dot(tmp, ${VAR_TYPE}(1.0));\n";
702 else if (readAccess == COMPONENT)
704 op << " res += tmp.x;\n";
705 if (vecLen >= 2) op << " res += tmp.y;\n";
706 if (vecLen >= 3) op << " res += tmp.z;\n";
707 if (vecLen >= 4) op << " res += tmp.w;\n";
709 else if (readAccess == SUBSCRIPT_STATIC)
711 op << " res += tmp[0];\n";
712 if (vecLen >= 2) op << " res += tmp[1];\n";
713 if (vecLen >= 3) op << " res += tmp[2];\n";
714 if (vecLen >= 4) op << " res += tmp[3];\n";
716 else if (readAccess == SUBSCRIPT_DYNAMIC)
718 op << " res += tmp[ui_zero];\n";
719 if (vecLen >= 2) op << " res += tmp[ui_one];\n";
720 if (vecLen >= 3) op << " res += tmp[ui_two];\n";
721 if (vecLen >= 4) op << " res += tmp[ui_three];\n";
723 else if (readAccess == SUBSCRIPT_STATIC_LOOP)
725 op << " for (int i = 0; i < " << vecLen << "; i++)\n";
726 op << " res += tmp[i];\n";
730 DE_ASSERT(readAccess == SUBSCRIPT_DYNAMIC_LOOP);
731 op << " for (int i = 0; i < " << vecLenName << "; i++)\n";
732 op << " res += tmp[i];\n";
737 vtx << " v_color = vec3(res);\n";
738 frag << " o_color = vec4(v_color.rgb, 1.0);\n";
742 vtx << " v_coords = a_coords;\n";
743 frag << " o_color = vec4(vec3(res), 1.0);\n";
749 // Fill in shader templates.
750 static const char* s_swizzles[5] = { "", "x", "xy", "xyz", "xyzw" };
751 static const char* s_rotSwizzles[5] = { "", "x", "yx", "yzx", "yzwx" };
753 map<string, string> params;
754 params.insert(pair<string, string>("VAR_TYPE", getDataTypeName(varType)));
755 params.insert(pair<string, string>("PRECISION", "mediump"));
756 params.insert(pair<string, string>("SWIZZLE", s_swizzles[vecLen]));
757 params.insert(pair<string, string>("ROT_SWIZZLE", s_rotSwizzles[vecLen]));
759 StringTemplate vertTemplate(vtx.str().c_str());
760 StringTemplate fragTemplate(frag.str().c_str());
761 string vertexShaderSource = vertTemplate.specialize(params);
762 string fragmentShaderSource = fragTemplate.specialize(params);
764 ShaderEvalFunc evalFunc = getVectorSubscriptEvalFunc(varType);
765 return new ShaderIndexingCase(context, caseName, description, isVertexCase, varType, evalFunc, vertexShaderSource.c_str(), fragmentShaderSource.c_str());
770 void evalSubscriptMat2 (ShaderEvalContext& c) { c.color.xy() = c.coords.swizzle(0,1) + 0.5f*c.coords.swizzle(1,2); }
771 void evalSubscriptMat2x3 (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2) + 0.5f*c.coords.swizzle(1,2,3); }
772 void evalSubscriptMat2x4 (ShaderEvalContext& c) { c.color = c.coords.swizzle(0,1,2,3) + 0.5f*c.coords.swizzle(1,2,3,0); }
774 void evalSubscriptMat3x2 (ShaderEvalContext& c) { c.color.xy() = c.coords.swizzle(0,1) + 0.5f*c.coords.swizzle(1,2) + 0.25f*c.coords.swizzle(2,3); }
775 void evalSubscriptMat3 (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2) + 0.5f*c.coords.swizzle(1,2,3) + 0.25f*c.coords.swizzle(2,3,0); }
776 void evalSubscriptMat3x4 (ShaderEvalContext& c) { c.color = c.coords.swizzle(0,1,2,3) + 0.5f*c.coords.swizzle(1,2,3,0) + 0.25f*c.coords.swizzle(2,3,0,1); }
778 void evalSubscriptMat4x2 (ShaderEvalContext& c) { c.color.xy() = c.coords.swizzle(0,1) + 0.5f*c.coords.swizzle(1,2) + 0.25f*c.coords.swizzle(2,3) + 0.125f*c.coords.swizzle(3,0); }
779 void evalSubscriptMat4x3 (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2) + 0.5f*c.coords.swizzle(1,2,3) + 0.25f*c.coords.swizzle(2,3,0) + 0.125f*c.coords.swizzle(3,0,1); }
780 void evalSubscriptMat4 (ShaderEvalContext& c) { c.color = c.coords + 0.5f*c.coords.swizzle(1,2,3,0) + 0.25f*c.coords.swizzle(2,3,0,1) + 0.125f*c.coords.swizzle(3,0,1,2); }
782 static ShaderEvalFunc getMatrixSubscriptEvalFunc (DataType dataType)
786 case TYPE_FLOAT_MAT2: return evalSubscriptMat2;
787 case TYPE_FLOAT_MAT2X3: return evalSubscriptMat2x3;
788 case TYPE_FLOAT_MAT2X4: return evalSubscriptMat2x4;
789 case TYPE_FLOAT_MAT3X2: return evalSubscriptMat3x2;
790 case TYPE_FLOAT_MAT3: return evalSubscriptMat3;
791 case TYPE_FLOAT_MAT3X4: return evalSubscriptMat3x4;
792 case TYPE_FLOAT_MAT4X2: return evalSubscriptMat4x2;
793 case TYPE_FLOAT_MAT4X3: return evalSubscriptMat4x3;
794 case TYPE_FLOAT_MAT4: return evalSubscriptMat4;
797 DE_FATAL("Invalid data type.");
802 static ShaderIndexingCase* createMatrixSubscriptCase (Context& context, const char* caseName, const char* description, bool isVertexCase, DataType varType, IndexAccessType writeAccess, IndexAccessType readAccess)
804 std::ostringstream vtx;
805 std::ostringstream frag;
806 std::ostringstream& op = isVertexCase ? vtx : frag;
808 int numCols = getDataTypeMatrixNumColumns(varType);
809 int numRows = getDataTypeMatrixNumRows(varType);
810 const char* matSizeName = getIntUniformName(numCols);
811 DataType vecType = getDataTypeFloatVec(numRows);
813 vtx << "#version 300 es\n";
814 frag << "#version 300 es\n";
816 vtx << "in highp vec4 a_position;\n";
817 vtx << "in highp vec4 a_coords;\n";
818 frag << "layout(location = 0) out mediump vec4 o_color;\n";
822 vtx << "out mediump vec4 v_color;\n";
823 frag << "in mediump vec4 v_color;\n";
827 vtx << "out mediump vec4 v_coords;\n";
828 frag << "in mediump vec4 v_coords;\n";
831 if (writeAccess == INDEXACCESS_DYNAMIC || readAccess == INDEXACCESS_DYNAMIC)
833 op << "uniform mediump int ui_zero";
834 if (numCols >= 2) op << ", ui_one";
835 if (numCols >= 3) op << ", ui_two";
836 if (numCols >= 4) op << ", ui_three";
840 if (writeAccess == INDEXACCESS_DYNAMIC_LOOP || readAccess == INDEXACCESS_DYNAMIC_LOOP)
841 op << "uniform mediump int " << matSizeName << ";\n";
844 vtx << "void main()\n";
846 vtx << " gl_Position = a_position;\n";
849 frag << "void main()\n";
854 op << " ${PRECISION} vec4 coords = a_coords;\n";
856 op << " ${PRECISION} vec4 coords = v_coords;\n";
858 op << " ${PRECISION} ${MAT_TYPE} tmp;\n";
859 if (writeAccess == INDEXACCESS_STATIC)
861 op << " tmp[0] = ${VEC_TYPE}(coords);\n";
862 if (numCols >= 2) op << " tmp[1] = ${VEC_TYPE}(coords.yzwx) * 0.5;\n";
863 if (numCols >= 3) op << " tmp[2] = ${VEC_TYPE}(coords.zwxy) * 0.25;\n";
864 if (numCols >= 4) op << " tmp[3] = ${VEC_TYPE}(coords.wxyz) * 0.125;\n";
866 else if (writeAccess == INDEXACCESS_DYNAMIC)
868 op << " tmp[ui_zero] = ${VEC_TYPE}(coords);\n";
869 if (numCols >= 2) op << " tmp[ui_one] = ${VEC_TYPE}(coords.yzwx) * 0.5;\n";
870 if (numCols >= 3) op << " tmp[ui_two] = ${VEC_TYPE}(coords.zwxy) * 0.25;\n";
871 if (numCols >= 4) op << " tmp[ui_three] = ${VEC_TYPE}(coords.wxyz) * 0.125;\n";
873 else if (writeAccess == INDEXACCESS_STATIC_LOOP)
875 op << " for (int i = 0; i < " << numCols << "; i++)\n";
877 op << " tmp[i] = ${VEC_TYPE}(coords);\n";
878 op << " coords = coords.yzwx * 0.5;\n";
883 DE_ASSERT(writeAccess == INDEXACCESS_DYNAMIC_LOOP);
884 op << " for (int i = 0; i < " << matSizeName << "; i++)\n";
886 op << " tmp[i] = ${VEC_TYPE}(coords);\n";
887 op << " coords = coords.yzwx * 0.5;\n";
892 op << " ${PRECISION} ${VEC_TYPE} res = ${VEC_TYPE}(0.0);\n";
893 if (readAccess == INDEXACCESS_STATIC)
895 op << " res += tmp[0];\n";
896 if (numCols >= 2) op << " res += tmp[1];\n";
897 if (numCols >= 3) op << " res += tmp[2];\n";
898 if (numCols >= 4) op << " res += tmp[3];\n";
900 else if (readAccess == INDEXACCESS_DYNAMIC)
902 op << " res += tmp[ui_zero];\n";
903 if (numCols >= 2) op << " res += tmp[ui_one];\n";
904 if (numCols >= 3) op << " res += tmp[ui_two];\n";
905 if (numCols >= 4) op << " res += tmp[ui_three];\n";
907 else if (readAccess == INDEXACCESS_STATIC_LOOP)
909 op << " for (int i = 0; i < " << numCols << "; i++)\n";
910 op << " res += tmp[i];\n";
914 DE_ASSERT(readAccess == INDEXACCESS_DYNAMIC_LOOP);
915 op << " for (int i = 0; i < " << matSizeName << "; i++)\n";
916 op << " res += tmp[i];\n";
921 vtx << " v_color = vec4(res${PADDING});\n";
922 frag << " o_color = v_color;\n";
926 vtx << " v_coords = a_coords;\n";
927 frag << " o_color = vec4(res${PADDING});\n";
933 // Fill in shader templates.
934 map<string, string> params;
935 params.insert(pair<string, string>("MAT_TYPE", getDataTypeName(varType)));
936 params.insert(pair<string, string>("VEC_TYPE", getDataTypeName(vecType)));
937 params.insert(pair<string, string>("PRECISION", "mediump"));
940 params.insert(pair<string, string>("PADDING", ", 0.0, 1.0"));
941 else if (numRows == 3)
942 params.insert(pair<string, string>("PADDING", ", 1.0"));
944 params.insert(pair<string, string>("PADDING", ""));
946 StringTemplate vertTemplate(vtx.str().c_str());
947 StringTemplate fragTemplate(frag.str().c_str());
948 string vertexShaderSource = vertTemplate.specialize(params);
949 string fragmentShaderSource = fragTemplate.specialize(params);
951 ShaderEvalFunc evalFunc = getMatrixSubscriptEvalFunc(varType);
952 return new ShaderIndexingCase(context, caseName, description, isVertexCase, varType, evalFunc, vertexShaderSource.c_str(), fragmentShaderSource.c_str());
955 // ShaderIndexingTests.
957 ShaderIndexingTests::ShaderIndexingTests(Context& context)
958 : TestCaseGroup(context, "indexing", "Indexing Tests")
962 ShaderIndexingTests::~ShaderIndexingTests (void)
966 void ShaderIndexingTests::init (void)
968 static const ShaderType s_shaderTypes[] =
974 static const DataType s_floatAndVecTypes[] =
982 // Varying array access cases.
984 TestCaseGroup* varyingGroup = new TestCaseGroup(m_context, "varying_array", "Varying array access tests.");
985 addChild(varyingGroup);
987 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_floatAndVecTypes); typeNdx++)
989 DataType varType = s_floatAndVecTypes[typeNdx];
990 for (int vertAccess = 0; vertAccess < INDEXACCESS_LAST; vertAccess++)
992 for (int fragAccess = 0; fragAccess < INDEXACCESS_LAST; fragAccess++)
994 const char* vertAccessName = getIndexAccessTypeName((IndexAccessType)vertAccess);
995 const char* fragAccessName = getIndexAccessTypeName((IndexAccessType)fragAccess);
996 string name = string(getDataTypeName(varType)) + "_" + vertAccessName + "_write_" + fragAccessName + "_read";
997 string desc = string("Varying array with ") + vertAccessName + " write in vertex shader and " + fragAccessName + " read in fragment shader.";
998 varyingGroup->addChild(createVaryingArrayCase(m_context, name.c_str(), desc.c_str(), varType, (IndexAccessType)vertAccess, (IndexAccessType)fragAccess));
1004 // Uniform array access cases.
1006 TestCaseGroup* uniformGroup = new TestCaseGroup(m_context, "uniform_array", "Uniform array access tests.");
1007 addChild(uniformGroup);
1009 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_floatAndVecTypes); typeNdx++)
1011 DataType varType = s_floatAndVecTypes[typeNdx];
1012 for (int readAccess = 0; readAccess < INDEXACCESS_LAST; readAccess++)
1014 const char* readAccessName = getIndexAccessTypeName((IndexAccessType)readAccess);
1015 for (int shaderTypeNdx = 0; shaderTypeNdx < DE_LENGTH_OF_ARRAY(s_shaderTypes); shaderTypeNdx++)
1017 ShaderType shaderType = s_shaderTypes[shaderTypeNdx];
1018 const char* shaderTypeName = getShaderTypeName(shaderType);
1019 string name = string(getDataTypeName(varType)) + "_" + readAccessName + "_read_" + shaderTypeName;
1020 string desc = string("Uniform array with ") + readAccessName + " read in " + shaderTypeName + " shader.";
1021 bool isVertexCase = ((ShaderType)shaderType == SHADERTYPE_VERTEX);
1022 uniformGroup->addChild(createUniformArrayCase(m_context, name.c_str(), desc.c_str(), isVertexCase, varType, (IndexAccessType)readAccess));
1028 // Temporary array access cases.
1030 TestCaseGroup* tmpGroup = new TestCaseGroup(m_context, "tmp_array", "Temporary array access tests.");
1033 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_floatAndVecTypes); typeNdx++)
1035 DataType varType = s_floatAndVecTypes[typeNdx];
1036 for (int writeAccess = 0; writeAccess < INDEXACCESS_LAST; writeAccess++)
1038 for (int readAccess = 0; readAccess < INDEXACCESS_LAST; readAccess++)
1040 const char* writeAccessName = getIndexAccessTypeName((IndexAccessType)writeAccess);
1041 const char* readAccessName = getIndexAccessTypeName((IndexAccessType)readAccess);
1043 for (int shaderTypeNdx = 0; shaderTypeNdx < DE_LENGTH_OF_ARRAY(s_shaderTypes); shaderTypeNdx++)
1045 ShaderType shaderType = s_shaderTypes[shaderTypeNdx];
1046 const char* shaderTypeName = getShaderTypeName(shaderType);
1047 string name = string(getDataTypeName(varType)) + "_" + writeAccessName + "_write_" + readAccessName + "_read_" + shaderTypeName;
1048 string desc = string("Temporary array with ") + writeAccessName + " write and " + readAccessName + " read in " + shaderTypeName + " shader.";
1049 bool isVertexCase = ((ShaderType)shaderType == SHADERTYPE_VERTEX);
1050 tmpGroup->addChild(createTmpArrayCase(m_context, name.c_str(), desc.c_str(), isVertexCase, varType, (IndexAccessType)writeAccess, (IndexAccessType)readAccess));
1057 // Vector indexing with subscripts.
1059 TestCaseGroup* vecGroup = new TestCaseGroup(m_context, "vector_subscript", "Vector subscript indexing.");
1062 static const DataType s_vectorTypes[] =
1069 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_vectorTypes); typeNdx++)
1071 DataType varType = s_vectorTypes[typeNdx];
1072 for (int writeAccess = 0; writeAccess < VECTORACCESS_LAST; writeAccess++)
1074 for (int readAccess = 0; readAccess < VECTORACCESS_LAST; readAccess++)
1076 const char* writeAccessName = getVectorAccessTypeName((VectorAccessType)writeAccess);
1077 const char* readAccessName = getVectorAccessTypeName((VectorAccessType)readAccess);
1079 for (int shaderTypeNdx = 0; shaderTypeNdx < DE_LENGTH_OF_ARRAY(s_shaderTypes); shaderTypeNdx++)
1081 ShaderType shaderType = s_shaderTypes[shaderTypeNdx];
1082 const char* shaderTypeName = getShaderTypeName(shaderType);
1083 string name = string(getDataTypeName(varType)) + "_" + writeAccessName + "_write_" + readAccessName + "_read_" + shaderTypeName;
1084 string desc = string("Vector subscript access with ") + writeAccessName + " write and " + readAccessName + " read in " + shaderTypeName + " shader.";
1085 bool isVertexCase = ((ShaderType)shaderType == SHADERTYPE_VERTEX);
1086 vecGroup->addChild(createVectorSubscriptCase(m_context, name.c_str(), desc.c_str(), isVertexCase, varType, (VectorAccessType)writeAccess, (VectorAccessType)readAccess));
1093 // Matrix indexing with subscripts.
1095 TestCaseGroup* matGroup = new TestCaseGroup(m_context, "matrix_subscript", "Matrix subscript indexing.");
1098 static const DataType s_matrixTypes[] =
1111 for (int typeNdx = 0; typeNdx < DE_LENGTH_OF_ARRAY(s_matrixTypes); typeNdx++)
1113 DataType varType = s_matrixTypes[typeNdx];
1114 for (int writeAccess = 0; writeAccess < INDEXACCESS_LAST; writeAccess++)
1116 for (int readAccess = 0; readAccess < INDEXACCESS_LAST; readAccess++)
1118 const char* writeAccessName = getIndexAccessTypeName((IndexAccessType)writeAccess);
1119 const char* readAccessName = getIndexAccessTypeName((IndexAccessType)readAccess);
1121 for (int shaderTypeNdx = 0; shaderTypeNdx < DE_LENGTH_OF_ARRAY(s_shaderTypes); shaderTypeNdx++)
1123 ShaderType shaderType = s_shaderTypes[shaderTypeNdx];
1124 const char* shaderTypeName = getShaderTypeName(shaderType);
1125 string name = string(getDataTypeName(varType)) + "_" + writeAccessName + "_write_" + readAccessName + "_read_" + shaderTypeName;
1126 string desc = string("Vector subscript access with ") + writeAccessName + " write and " + readAccessName + " read in " + shaderTypeName + " shader.";
1127 bool isVertexCase = ((ShaderType)shaderType == SHADERTYPE_VERTEX);
1128 matGroup->addChild(createMatrixSubscriptCase(m_context, name.c_str(), desc.c_str(), isVertexCase, varType, (IndexAccessType)writeAccess, (IndexAccessType)readAccess));