1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL Shared 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 Shared shader constant expression test components
22 *//*--------------------------------------------------------------------*/
24 #include "glsShaderConstExprTests.hpp"
25 #include "glsShaderLibrary.hpp"
26 #include "glsShaderLibraryCase.hpp"
28 #include "tcuStringTemplate.hpp"
30 #include "deStringUtil.hpp"
37 namespace ShaderConstExpr
40 static void addOutputVar (glu::sl::ValueBlock* dst, glu::DataType type, float output)
42 dst->outputs.push_back(glu::sl::Value());
45 glu::sl::Value& value = dst->outputs.back();
48 value.type = glu::VarType(type, glu::PRECISION_LAST);
49 value.elements.resize(1);
54 value.elements[0].int32 = (int)output;
58 value.elements[0].int32 = (unsigned int)output;
62 value.elements[0].bool32 = output!=0.0f;
66 value.elements[0].float32 = output;
75 std::vector<tcu::TestNode*> createTests (tcu::TestContext& testContext,
76 glu::RenderContext& renderContext,
77 const glu::ContextInfo& contextInfo,
78 const TestParams* cases,
80 glu::GLSLVersion version,
81 TestShaderStage testStage)
85 using gls::ShaderLibraryCase;
87 // Needed for autogenerating shader code for increased component counts
88 DE_STATIC_ASSERT(glu::TYPE_FLOAT+1 == glu::TYPE_FLOAT_VEC2);
89 DE_STATIC_ASSERT(glu::TYPE_FLOAT+2 == glu::TYPE_FLOAT_VEC3);
90 DE_STATIC_ASSERT(glu::TYPE_FLOAT+3 == glu::TYPE_FLOAT_VEC4);
92 DE_STATIC_ASSERT(glu::TYPE_INT+1 == glu::TYPE_INT_VEC2);
93 DE_STATIC_ASSERT(glu::TYPE_INT+2 == glu::TYPE_INT_VEC3);
94 DE_STATIC_ASSERT(glu::TYPE_INT+3 == glu::TYPE_INT_VEC4);
96 DE_STATIC_ASSERT(glu::TYPE_UINT+1 == glu::TYPE_UINT_VEC2);
97 DE_STATIC_ASSERT(glu::TYPE_UINT+2 == glu::TYPE_UINT_VEC3);
98 DE_STATIC_ASSERT(glu::TYPE_UINT+3 == glu::TYPE_UINT_VEC4);
100 DE_STATIC_ASSERT(glu::TYPE_BOOL+1 == glu::TYPE_BOOL_VEC2);
101 DE_STATIC_ASSERT(glu::TYPE_BOOL+2 == glu::TYPE_BOOL_VEC3);
102 DE_STATIC_ASSERT(glu::TYPE_BOOL+3 == glu::TYPE_BOOL_VEC4);
104 DE_ASSERT(testStage);
106 const char* shaderTemplateSrc =
107 "#version ${GLES_VERSION}\n"
108 "precision highp float;\n"
109 "precision highp int;\n"
113 " const ${CASE_BASE_TYPE} cval = ${CASE_EXPRESSION};\n"
118 const tcu::StringTemplate shaderTemplate (shaderTemplateSrc);
119 vector<tcu::TestNode*> ret;
121 for (int caseNdx = 0; caseNdx < numCases; caseNdx++)
123 std::map<string, string> shaderTemplateParams;
124 const int minComponents = cases[caseNdx].minComponents;
125 const int maxComponents = cases[caseNdx].maxComponents;
126 const DataType inType = cases[caseNdx].inType;
127 const DataType outType = cases[caseNdx].outType;
128 const string expression = cases[caseNdx].expression;
129 // Check for presence of func(vec, scalar) style specialization, use as gatekeeper for applying said specialization
130 const bool alwaysScalar = expression.find("${MT}")!=string::npos;
132 shaderTemplateParams["GLES_VERSION"] = version == glu::GLSL_VERSION_300_ES ? "300 es" : "100";
133 shaderTemplateParams["CASE_BASE_TYPE"] = glu::getDataTypeName(outType);
134 shaderTemplateParams["DECLARATIONS"] = "${DECLARATIONS}";
135 shaderTemplateParams["OUTPUT"] = "${OUTPUT}";
137 for (int compCount = minComponents-1; compCount < maxComponents; compCount++)
139 vector<tcu::TestNode*> children;
140 std::map<string, string> expressionTemplateParams;
141 string typeName = glu::getDataTypeName((glu::DataType)(inType + compCount)); // results in float, vec2, vec3, vec4 progression (same for other primitive types)
142 const char* componentAccess[] = {"", ".y", ".z", ".w"};
143 const tcu::StringTemplate expressionTemplate (expression);
144 // Add type to case name if we are generating multiple versions
145 const string caseName = string(cases[caseNdx].name) + (minComponents==maxComponents ? "" : ("_" + typeName));
147 // ${T} => final type, ${MT} => final type but with scalar version usable even when T is a vector
148 expressionTemplateParams["T"] = typeName;
149 expressionTemplateParams["MT"] = typeName;
151 shaderTemplateParams["CASE_EXPRESSION"] = expressionTemplate.specialize(expressionTemplateParams) + componentAccess[compCount]; // Add vector access to expression as needed
154 const string mapped = shaderTemplate.specialize(shaderTemplateParams);
156 if (testStage & SHADER_VERTEX)
158 glu::sl::ShaderCaseSpecification spec;
160 spec.targetVersion = version;
161 spec.expectResult = glu::sl::EXPECT_PASS;
162 spec.caseType = glu::sl::CASETYPE_VERTEX_ONLY;
163 spec.programs.resize(1);
165 spec.programs[0].sources << glu::VertexSource(mapped);
167 addOutputVar(&spec.values, outType, cases[caseNdx].output);
169 ret.push_back(new ShaderLibraryCase(testContext,
172 (caseName + "_vertex").c_str(),
177 if (testStage & SHADER_FRAGMENT)
179 glu::sl::ShaderCaseSpecification spec;
181 spec.targetVersion = version;
182 spec.expectResult = glu::sl::EXPECT_PASS;
183 spec.caseType = glu::sl::CASETYPE_FRAGMENT_ONLY;
184 spec.programs.resize(1);
186 spec.programs[0].sources << glu::FragmentSource(mapped);
188 addOutputVar(&spec.values, outType, cases[caseNdx].output);
190 ret.push_back(new ShaderLibraryCase(testContext,
193 (caseName + "_fragment").c_str(),
199 // Deal with functions that allways accept one ore more scalar parameters even when others are vectors
200 if (alwaysScalar && compCount > 0)
202 const string scalarCaseName = string(cases[caseNdx].name) + "_" + typeName + "_" + glu::getDataTypeName(inType);
204 expressionTemplateParams["MT"] = glu::getDataTypeName(inType);
205 shaderTemplateParams["CASE_EXPRESSION"] = expressionTemplate.specialize(expressionTemplateParams) + componentAccess[compCount];
208 const string mapped = shaderTemplate.specialize(shaderTemplateParams);
210 if (testStage & SHADER_VERTEX)
212 glu::sl::ShaderCaseSpecification spec;
214 spec.targetVersion = version;
215 spec.expectResult = glu::sl::EXPECT_PASS;
216 spec.caseType = glu::sl::CASETYPE_VERTEX_ONLY;
217 spec.programs.resize(1);
219 spec.programs[0].sources << glu::VertexSource(mapped);
221 addOutputVar(&spec.values, outType, cases[caseNdx].output);
223 ret.push_back(new ShaderLibraryCase(testContext,
226 (scalarCaseName + "_vertex").c_str(),
231 if (testStage & SHADER_FRAGMENT)
233 glu::sl::ShaderCaseSpecification spec;
235 spec.targetVersion = version;
236 spec.expectResult = glu::sl::EXPECT_PASS;
237 spec.caseType = glu::sl::CASETYPE_FRAGMENT_ONLY;
238 spec.programs.resize(1);
240 spec.programs[0].sources << glu::FragmentSource(mapped);
242 addOutputVar(&spec.values, outType, cases[caseNdx].output);
244 ret.push_back(new ShaderLibraryCase(testContext,
247 (scalarCaseName + "_fragment").c_str(),