1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL Shared Module
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 Shared shader constant expression test components
22 *//*--------------------------------------------------------------------*/
23
24 #include "glsShaderConstExprTests.hpp"
25 #include "glsShaderLibrary.hpp"
26 #include "glsShaderLibraryCase.hpp"
27
28 #include "tcuStringTemplate.hpp"
29
30 #include "deStringUtil.hpp"
31 #include "deMath.h"
32
33 namespace deqp
34 {
35 namespace gls
36 {
37 namespace ShaderConstExpr
38 {
39
addOutputVar(glu::sl::ValueBlock * dst,glu::DataType type,float output)40 static void addOutputVar(glu::sl::ValueBlock *dst, glu::DataType type, float output)
41 {
42 dst->outputs.push_back(glu::sl::Value());
43
44 {
45 glu::sl::Value &value = dst->outputs.back();
46
47 value.name = "out0";
48 value.type = glu::VarType(type, glu::PRECISION_LAST);
49 value.elements.resize(1);
50
51 switch (type)
52 {
53 case glu::TYPE_INT:
54 value.elements[0].int32 = (int)output;
55 break;
56
57 case glu::TYPE_UINT:
58 value.elements[0].int32 = (unsigned int)output;
59 break;
60
61 case glu::TYPE_BOOL:
62 value.elements[0].bool32 = output != 0.0f;
63 break;
64
65 case glu::TYPE_FLOAT:
66 value.elements[0].float32 = output;
67 break;
68
69 default:
70 DE_ASSERT(false);
71 }
72 }
73 }
74
createTests(tcu::TestContext & testContext,glu::RenderContext & renderContext,const glu::ContextInfo & contextInfo,const TestParams * cases,int numCases,glu::GLSLVersion version,TestShaderStage testStage)75 std::vector<tcu::TestNode *> createTests(tcu::TestContext &testContext, glu::RenderContext &renderContext,
76 const glu::ContextInfo &contextInfo, const TestParams *cases, int numCases,
77 glu::GLSLVersion version, TestShaderStage testStage)
78 {
79 using gls::ShaderLibraryCase;
80 using std::string;
81 using std::vector;
82
83 // Needed for autogenerating shader code for increased component counts
84 DE_STATIC_ASSERT(glu::TYPE_FLOAT + 1 == glu::TYPE_FLOAT_VEC2);
85 DE_STATIC_ASSERT(glu::TYPE_FLOAT + 2 == glu::TYPE_FLOAT_VEC3);
86 DE_STATIC_ASSERT(glu::TYPE_FLOAT + 3 == glu::TYPE_FLOAT_VEC4);
87
88 DE_STATIC_ASSERT(glu::TYPE_INT + 1 == glu::TYPE_INT_VEC2);
89 DE_STATIC_ASSERT(glu::TYPE_INT + 2 == glu::TYPE_INT_VEC3);
90 DE_STATIC_ASSERT(glu::TYPE_INT + 3 == glu::TYPE_INT_VEC4);
91
92 DE_STATIC_ASSERT(glu::TYPE_UINT + 1 == glu::TYPE_UINT_VEC2);
93 DE_STATIC_ASSERT(glu::TYPE_UINT + 2 == glu::TYPE_UINT_VEC3);
94 DE_STATIC_ASSERT(glu::TYPE_UINT + 3 == glu::TYPE_UINT_VEC4);
95
96 DE_STATIC_ASSERT(glu::TYPE_BOOL + 1 == glu::TYPE_BOOL_VEC2);
97 DE_STATIC_ASSERT(glu::TYPE_BOOL + 2 == glu::TYPE_BOOL_VEC3);
98 DE_STATIC_ASSERT(glu::TYPE_BOOL + 3 == glu::TYPE_BOOL_VEC4);
99
100 DE_ASSERT(testStage);
101
102 const char *shaderTemplateSrc = "#version ${GLES_VERSION}\n"
103 "precision highp float;\n"
104 "precision highp int;\n"
105 "${DECLARATIONS}\n"
106 "void main()\n"
107 "{\n"
108 " const ${CASE_BASE_TYPE} cval = ${CASE_EXPRESSION};\n"
109 " out0 = cval;\n"
110 " ${OUTPUT}\n"
111 "}\n";
112
113 const tcu::StringTemplate shaderTemplate(shaderTemplateSrc);
114 vector<tcu::TestNode *> ret;
115
116 for (int caseNdx = 0; caseNdx < numCases; caseNdx++)
117 {
118 std::map<string, string> shaderTemplateParams;
119 const int minComponents = cases[caseNdx].minComponents;
120 const int maxComponents = cases[caseNdx].maxComponents;
121 const DataType inType = cases[caseNdx].inType;
122 const DataType outType = cases[caseNdx].outType;
123 const string expression = cases[caseNdx].expression;
124 // Check for presence of func(vec, scalar) style specialization, use as gatekeeper for applying said specialization
125 const bool alwaysScalar = expression.find("${MT}") != string::npos;
126
127 shaderTemplateParams["GLES_VERSION"] = version == glu::GLSL_VERSION_300_ES ? "300 es" : "100";
128 shaderTemplateParams["CASE_BASE_TYPE"] = glu::getDataTypeName(outType);
129 shaderTemplateParams["DECLARATIONS"] = "${DECLARATIONS}";
130 shaderTemplateParams["OUTPUT"] = "${OUTPUT}";
131
132 for (int compCount = minComponents - 1; compCount < maxComponents; compCount++)
133 {
134 vector<tcu::TestNode *> children;
135 std::map<string, string> expressionTemplateParams;
136 string typeName = glu::getDataTypeName((glu::DataType)(
137 inType + compCount)); // results in float, vec2, vec3, vec4 progression (same for other primitive types)
138 const char *componentAccess[] = {"", ".y", ".z", ".w"};
139 const tcu::StringTemplate expressionTemplate(expression);
140 // Add type to case name if we are generating multiple versions
141 const string caseName =
142 string(cases[caseNdx].name) + (minComponents == maxComponents ? "" : ("_" + typeName));
143
144 // ${T} => final type, ${MT} => final type but with scalar version usable even when T is a vector
145 expressionTemplateParams["T"] = typeName;
146 expressionTemplateParams["MT"] = typeName;
147
148 shaderTemplateParams["CASE_EXPRESSION"] =
149 expressionTemplate.specialize(expressionTemplateParams) +
150 componentAccess[compCount]; // Add vector access to expression as needed
151
152 {
153 const string mapped = shaderTemplate.specialize(shaderTemplateParams);
154
155 if (testStage & SHADER_VERTEX)
156 {
157 glu::sl::ShaderCaseSpecification spec;
158
159 spec.targetVersion = version;
160 spec.expectResult = glu::sl::EXPECT_PASS;
161 spec.caseType = glu::sl::CASETYPE_VERTEX_ONLY;
162 spec.programs.resize(1);
163
164 spec.programs[0].sources << glu::VertexSource(mapped);
165
166 addOutputVar(&spec.values, outType, cases[caseNdx].output);
167
168 ret.push_back(new ShaderLibraryCase(testContext, renderContext, contextInfo,
169 (caseName + "_vertex").c_str(), "", spec));
170 }
171
172 if (testStage & SHADER_FRAGMENT)
173 {
174 glu::sl::ShaderCaseSpecification spec;
175
176 spec.targetVersion = version;
177 spec.expectResult = glu::sl::EXPECT_PASS;
178 spec.caseType = glu::sl::CASETYPE_FRAGMENT_ONLY;
179 spec.programs.resize(1);
180
181 spec.programs[0].sources << glu::FragmentSource(mapped);
182
183 addOutputVar(&spec.values, outType, cases[caseNdx].output);
184
185 ret.push_back(new ShaderLibraryCase(testContext, renderContext, contextInfo,
186 (caseName + "_fragment").c_str(), "", spec));
187 }
188 }
189
190 // Deal with functions that allways accept one ore more scalar parameters even when others are vectors
191 if (alwaysScalar && compCount > 0)
192 {
193 const string scalarCaseName =
194 string(cases[caseNdx].name) + "_" + typeName + "_" + glu::getDataTypeName(inType);
195
196 expressionTemplateParams["MT"] = glu::getDataTypeName(inType);
197 shaderTemplateParams["CASE_EXPRESSION"] =
198 expressionTemplate.specialize(expressionTemplateParams) + componentAccess[compCount];
199
200 {
201 const string mapped = shaderTemplate.specialize(shaderTemplateParams);
202
203 if (testStage & SHADER_VERTEX)
204 {
205 glu::sl::ShaderCaseSpecification spec;
206
207 spec.targetVersion = version;
208 spec.expectResult = glu::sl::EXPECT_PASS;
209 spec.caseType = glu::sl::CASETYPE_VERTEX_ONLY;
210 spec.programs.resize(1);
211
212 spec.programs[0].sources << glu::VertexSource(mapped);
213
214 addOutputVar(&spec.values, outType, cases[caseNdx].output);
215
216 ret.push_back(new ShaderLibraryCase(testContext, renderContext, contextInfo,
217 (scalarCaseName + "_vertex").c_str(), "", spec));
218 }
219
220 if (testStage & SHADER_FRAGMENT)
221 {
222 glu::sl::ShaderCaseSpecification spec;
223
224 spec.targetVersion = version;
225 spec.expectResult = glu::sl::EXPECT_PASS;
226 spec.caseType = glu::sl::CASETYPE_FRAGMENT_ONLY;
227 spec.programs.resize(1);
228
229 spec.programs[0].sources << glu::FragmentSource(mapped);
230
231 addOutputVar(&spec.values, outType, cases[caseNdx].output);
232
233 ret.push_back(new ShaderLibraryCase(testContext, renderContext, contextInfo,
234 (scalarCaseName + "_fragment").c_str(), "", spec));
235 }
236 }
237 }
238 }
239 }
240
241 return ret;
242 }
243
244 } // namespace ShaderConstExpr
245 } // namespace gls
246 } // namespace deqp
247