xref: /aosp_15_r20/external/deqp/framework/randomshaders/rsgUtils.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker  * drawElements Quality Program Random Shader Generator
3*35238bceSAndroid Build Coastguard Worker  * ----------------------------------------------------
4*35238bceSAndroid Build Coastguard Worker  *
5*35238bceSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker  *
7*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker  *
11*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker  *
13*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker  *
19*35238bceSAndroid Build Coastguard Worker  *//*!
20*35238bceSAndroid Build Coastguard Worker  * \file
21*35238bceSAndroid Build Coastguard Worker  * \brief Utilities.
22*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "rsgUtils.hpp"
25*35238bceSAndroid Build Coastguard Worker 
26*35238bceSAndroid Build Coastguard Worker #include <set>
27*35238bceSAndroid Build Coastguard Worker #include <string>
28*35238bceSAndroid Build Coastguard Worker 
29*35238bceSAndroid Build Coastguard Worker using std::set;
30*35238bceSAndroid Build Coastguard Worker using std::string;
31*35238bceSAndroid Build Coastguard Worker using std::vector;
32*35238bceSAndroid Build Coastguard Worker 
33*35238bceSAndroid Build Coastguard Worker namespace rsg
34*35238bceSAndroid Build Coastguard Worker {
35*35238bceSAndroid Build Coastguard Worker 
addNewUniforms(vector<const ShaderInput * > & uniforms,set<string> & addedUniforms,const Shader & shader)36*35238bceSAndroid Build Coastguard Worker void addNewUniforms(vector<const ShaderInput *> &uniforms, set<string> &addedUniforms, const Shader &shader)
37*35238bceSAndroid Build Coastguard Worker {
38*35238bceSAndroid Build Coastguard Worker     const vector<ShaderInput *> &shaderUniforms = shader.getUniforms();
39*35238bceSAndroid Build Coastguard Worker     for (vector<ShaderInput *>::const_iterator i = shaderUniforms.begin(); i != shaderUniforms.end(); i++)
40*35238bceSAndroid Build Coastguard Worker     {
41*35238bceSAndroid Build Coastguard Worker         const ShaderInput *uniform = *i;
42*35238bceSAndroid Build Coastguard Worker         if (addedUniforms.find(uniform->getVariable()->getName()) == addedUniforms.end())
43*35238bceSAndroid Build Coastguard Worker         {
44*35238bceSAndroid Build Coastguard Worker             addedUniforms.insert(uniform->getVariable()->getName());
45*35238bceSAndroid Build Coastguard Worker             uniforms.push_back(uniform);
46*35238bceSAndroid Build Coastguard Worker         }
47*35238bceSAndroid Build Coastguard Worker     }
48*35238bceSAndroid Build Coastguard Worker }
49*35238bceSAndroid Build Coastguard Worker 
computeUnifiedUniforms(const Shader & vertexShader,const Shader & fragmentShader,std::vector<const ShaderInput * > & uniforms)50*35238bceSAndroid Build Coastguard Worker void computeUnifiedUniforms(const Shader &vertexShader, const Shader &fragmentShader,
51*35238bceSAndroid Build Coastguard Worker                             std::vector<const ShaderInput *> &uniforms)
52*35238bceSAndroid Build Coastguard Worker {
53*35238bceSAndroid Build Coastguard Worker     set<string> addedUniforms;
54*35238bceSAndroid Build Coastguard Worker     addNewUniforms(uniforms, addedUniforms, vertexShader);
55*35238bceSAndroid Build Coastguard Worker     addNewUniforms(uniforms, addedUniforms, fragmentShader);
56*35238bceSAndroid Build Coastguard Worker }
57*35238bceSAndroid Build Coastguard Worker 
computeRandomValue(de::Random & rnd,ValueAccess dst,ConstValueRangeAccess valueRange)58*35238bceSAndroid Build Coastguard Worker void computeRandomValue(de::Random &rnd, ValueAccess dst, ConstValueRangeAccess valueRange)
59*35238bceSAndroid Build Coastguard Worker {
60*35238bceSAndroid Build Coastguard Worker     const VariableType &type = dst.getType();
61*35238bceSAndroid Build Coastguard Worker 
62*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
63*35238bceSAndroid Build Coastguard Worker     {
64*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
65*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
66*35238bceSAndroid Build Coastguard Worker         {
67*35238bceSAndroid Build Coastguard Worker             const float quantizeStep     = 1.0f / 8.0f;
68*35238bceSAndroid Build Coastguard Worker             float minVal                 = valueRange.component(ndx).getMin().asFloat();
69*35238bceSAndroid Build Coastguard Worker             float maxVal                 = valueRange.component(ndx).getMax().asFloat();
70*35238bceSAndroid Build Coastguard Worker             dst.component(ndx).asFloat() = getQuantizedFloat(rnd, minVal, maxVal, quantizeStep);
71*35238bceSAndroid Build Coastguard Worker         }
72*35238bceSAndroid Build Coastguard Worker         break;
73*35238bceSAndroid Build Coastguard Worker 
74*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
75*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
76*35238bceSAndroid Build Coastguard Worker         {
77*35238bceSAndroid Build Coastguard Worker             int minVal                  = valueRange.component(ndx).getMin().asBool() ? 1 : 0;
78*35238bceSAndroid Build Coastguard Worker             int maxVal                  = valueRange.component(ndx).getMin().asBool() ? 1 : 0;
79*35238bceSAndroid Build Coastguard Worker             dst.component(ndx).asBool() = rnd.getInt(minVal, maxVal) == 1;
80*35238bceSAndroid Build Coastguard Worker         }
81*35238bceSAndroid Build Coastguard Worker         break;
82*35238bceSAndroid Build Coastguard Worker 
83*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
84*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_SAMPLER_2D:
85*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_SAMPLER_CUBE:
86*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
87*35238bceSAndroid Build Coastguard Worker         {
88*35238bceSAndroid Build Coastguard Worker             int minVal                 = valueRange.component(ndx).getMin().asInt();
89*35238bceSAndroid Build Coastguard Worker             int maxVal                 = valueRange.component(ndx).getMax().asInt();
90*35238bceSAndroid Build Coastguard Worker             dst.component(ndx).asInt() = rnd.getInt(minVal, maxVal);
91*35238bceSAndroid Build Coastguard Worker         }
92*35238bceSAndroid Build Coastguard Worker         break;
93*35238bceSAndroid Build Coastguard Worker 
94*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_ARRAY:
95*35238bceSAndroid Build Coastguard Worker     {
96*35238bceSAndroid Build Coastguard Worker         int numElements = type.getNumElements();
97*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < numElements; ndx++)
98*35238bceSAndroid Build Coastguard Worker             computeRandomValue(rnd, dst.arrayElement(ndx), valueRange.arrayElement(ndx));
99*35238bceSAndroid Build Coastguard Worker         break;
100*35238bceSAndroid Build Coastguard Worker     }
101*35238bceSAndroid Build Coastguard Worker 
102*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_STRUCT:
103*35238bceSAndroid Build Coastguard Worker     {
104*35238bceSAndroid Build Coastguard Worker         int numMembers = (int)type.getMembers().size();
105*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < numMembers; ndx++)
106*35238bceSAndroid Build Coastguard Worker             computeRandomValue(rnd, dst.member(ndx), valueRange.member(ndx));
107*35238bceSAndroid Build Coastguard Worker         break;
108*35238bceSAndroid Build Coastguard Worker     }
109*35238bceSAndroid Build Coastguard Worker 
110*35238bceSAndroid Build Coastguard Worker     default:
111*35238bceSAndroid Build Coastguard Worker         TCU_FAIL("Invalid type");
112*35238bceSAndroid Build Coastguard Worker     }
113*35238bceSAndroid Build Coastguard Worker }
114*35238bceSAndroid Build Coastguard Worker 
computeUniformValues(de::Random & rnd,std::vector<VariableValue> & values,const std::vector<const ShaderInput * > & uniforms)115*35238bceSAndroid Build Coastguard Worker void computeUniformValues(de::Random &rnd, std::vector<VariableValue> &values,
116*35238bceSAndroid Build Coastguard Worker                           const std::vector<const ShaderInput *> &uniforms)
117*35238bceSAndroid Build Coastguard Worker {
118*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(values.empty());
119*35238bceSAndroid Build Coastguard Worker     for (vector<const ShaderInput *>::const_iterator i = uniforms.begin(); i != uniforms.end(); i++)
120*35238bceSAndroid Build Coastguard Worker     {
121*35238bceSAndroid Build Coastguard Worker         const ShaderInput *uniform = *i;
122*35238bceSAndroid Build Coastguard Worker         values.push_back(VariableValue(uniform->getVariable()));
123*35238bceSAndroid Build Coastguard Worker         computeRandomValue(rnd, values[values.size() - 1].getValue(), uniform->getValueRange());
124*35238bceSAndroid Build Coastguard Worker     }
125*35238bceSAndroid Build Coastguard Worker }
126*35238bceSAndroid Build Coastguard Worker 
isUndefinedValueRange(ConstValueRangeAccess valueRange)127*35238bceSAndroid Build Coastguard Worker bool isUndefinedValueRange(ConstValueRangeAccess valueRange)
128*35238bceSAndroid Build Coastguard Worker {
129*35238bceSAndroid Build Coastguard Worker     switch (valueRange.getType().getBaseType())
130*35238bceSAndroid Build Coastguard Worker     {
131*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
132*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
133*35238bceSAndroid Build Coastguard Worker     {
134*35238bceSAndroid Build Coastguard Worker         bool isFloat  = valueRange.getType().getBaseType() == VariableType::TYPE_FLOAT;
135*35238bceSAndroid Build Coastguard Worker         Scalar infMin = isFloat ? Scalar::min<float>() : Scalar::min<int>();
136*35238bceSAndroid Build Coastguard Worker         Scalar infMax = isFloat ? Scalar::max<float>() : Scalar::max<int>();
137*35238bceSAndroid Build Coastguard Worker 
138*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < valueRange.getType().getNumElements(); ndx++)
139*35238bceSAndroid Build Coastguard Worker         {
140*35238bceSAndroid Build Coastguard Worker             if (valueRange.getMin().component(ndx).asScalar() != infMin ||
141*35238bceSAndroid Build Coastguard Worker                 valueRange.getMax().component(ndx).asScalar() != infMax)
142*35238bceSAndroid Build Coastguard Worker                 return false;
143*35238bceSAndroid Build Coastguard Worker         }
144*35238bceSAndroid Build Coastguard Worker         return true;
145*35238bceSAndroid Build Coastguard Worker     }
146*35238bceSAndroid Build Coastguard Worker 
147*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
148*35238bceSAndroid Build Coastguard Worker         return false;
149*35238bceSAndroid Build Coastguard Worker 
150*35238bceSAndroid Build Coastguard Worker     default:
151*35238bceSAndroid Build Coastguard Worker         TCU_FAIL("Unsupported type");
152*35238bceSAndroid Build Coastguard Worker     }
153*35238bceSAndroid Build Coastguard Worker }
154*35238bceSAndroid Build Coastguard Worker 
computeRandomType(GeneratorState & state,int maxScalars)155*35238bceSAndroid Build Coastguard Worker VariableType computeRandomType(GeneratorState &state, int maxScalars)
156*35238bceSAndroid Build Coastguard Worker {
157*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(maxScalars >= 1);
158*35238bceSAndroid Build Coastguard Worker 
159*35238bceSAndroid Build Coastguard Worker     static const VariableType::Type baseTypes[] = {
160*35238bceSAndroid Build Coastguard Worker         VariableType::TYPE_BOOL, VariableType::TYPE_INT, VariableType::TYPE_FLOAT
161*35238bceSAndroid Build Coastguard Worker         // \todo [pyry] Other types
162*35238bceSAndroid Build Coastguard Worker     };
163*35238bceSAndroid Build Coastguard Worker 
164*35238bceSAndroid Build Coastguard Worker     VariableType::Type baseType = VariableType::TYPE_LAST;
165*35238bceSAndroid Build Coastguard Worker     state.getRandom().choose(baseTypes, baseTypes + DE_LENGTH_OF_ARRAY(baseTypes), &baseType, 1);
166*35238bceSAndroid Build Coastguard Worker 
167*35238bceSAndroid Build Coastguard Worker     switch (baseType)
168*35238bceSAndroid Build Coastguard Worker     {
169*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
170*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
171*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
172*35238bceSAndroid Build Coastguard Worker     {
173*35238bceSAndroid Build Coastguard Worker         const int minVecLength = 1;
174*35238bceSAndroid Build Coastguard Worker         const int maxVecLength = 4;
175*35238bceSAndroid Build Coastguard Worker         return VariableType(baseType, state.getRandom().getInt(minVecLength, de::min(maxScalars, maxVecLength)));
176*35238bceSAndroid Build Coastguard Worker     }
177*35238bceSAndroid Build Coastguard Worker 
178*35238bceSAndroid Build Coastguard Worker     default:
179*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
180*35238bceSAndroid Build Coastguard Worker         throw Exception("computeRandomType(): Unsupported type");
181*35238bceSAndroid Build Coastguard Worker     }
182*35238bceSAndroid Build Coastguard Worker }
183*35238bceSAndroid Build Coastguard Worker 
computeRandomValueRange(GeneratorState & state,ValueRangeAccess valueRange)184*35238bceSAndroid Build Coastguard Worker void computeRandomValueRange(GeneratorState &state, ValueRangeAccess valueRange)
185*35238bceSAndroid Build Coastguard Worker {
186*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
187*35238bceSAndroid Build Coastguard Worker     de::Random &rnd          = state.getRandom();
188*35238bceSAndroid Build Coastguard Worker 
189*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
190*35238bceSAndroid Build Coastguard Worker     {
191*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
192*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
193*35238bceSAndroid Build Coastguard Worker         {
194*35238bceSAndroid Build Coastguard Worker             bool minVal                                 = rnd.getBool();
195*35238bceSAndroid Build Coastguard Worker             bool maxVal                                 = minVal ? true : rnd.getBool();
196*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx).asBool() = minVal;
197*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx).asBool() = maxVal;
198*35238bceSAndroid Build Coastguard Worker         }
199*35238bceSAndroid Build Coastguard Worker         break;
200*35238bceSAndroid Build Coastguard Worker 
201*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
202*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
203*35238bceSAndroid Build Coastguard Worker         {
204*35238bceSAndroid Build Coastguard Worker             const int minIntVal   = -16;
205*35238bceSAndroid Build Coastguard Worker             const int maxIntVal   = 16;
206*35238bceSAndroid Build Coastguard Worker             const int maxRangeLen = maxIntVal - minIntVal;
207*35238bceSAndroid Build Coastguard Worker 
208*35238bceSAndroid Build Coastguard Worker             int rangeLen = rnd.getInt(0, maxRangeLen);
209*35238bceSAndroid Build Coastguard Worker             int minVal   = minIntVal + rnd.getInt(0, maxRangeLen - rangeLen);
210*35238bceSAndroid Build Coastguard Worker             int maxVal   = minVal + rangeLen;
211*35238bceSAndroid Build Coastguard Worker 
212*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx).asInt() = minVal;
213*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx).asInt() = maxVal;
214*35238bceSAndroid Build Coastguard Worker         }
215*35238bceSAndroid Build Coastguard Worker         break;
216*35238bceSAndroid Build Coastguard Worker 
217*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
218*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
219*35238bceSAndroid Build Coastguard Worker         {
220*35238bceSAndroid Build Coastguard Worker             const float step        = 0.1f;
221*35238bceSAndroid Build Coastguard Worker             const int maxSteps      = 320;
222*35238bceSAndroid Build Coastguard Worker             const float minFloatVal = -16.0f;
223*35238bceSAndroid Build Coastguard Worker 
224*35238bceSAndroid Build Coastguard Worker             int rangeLen = rnd.getInt(0, maxSteps);
225*35238bceSAndroid Build Coastguard Worker             int minStep  = rnd.getInt(0, maxSteps - rangeLen);
226*35238bceSAndroid Build Coastguard Worker 
227*35238bceSAndroid Build Coastguard Worker             float minVal = minFloatVal + step * (float)minStep;
228*35238bceSAndroid Build Coastguard Worker             float maxVal = minVal + step * (float)rangeLen;
229*35238bceSAndroid Build Coastguard Worker 
230*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx).asFloat() = minVal;
231*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx).asFloat() = maxVal;
232*35238bceSAndroid Build Coastguard Worker         }
233*35238bceSAndroid Build Coastguard Worker         break;
234*35238bceSAndroid Build Coastguard Worker 
235*35238bceSAndroid Build Coastguard Worker     default:
236*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
237*35238bceSAndroid Build Coastguard Worker         throw Exception("computeRandomValueRange(): Unsupported type");
238*35238bceSAndroid Build Coastguard Worker     }
239*35238bceSAndroid Build Coastguard Worker }
240*35238bceSAndroid Build Coastguard Worker 
getTypeConstructorDepth(const VariableType & type)241*35238bceSAndroid Build Coastguard Worker int getTypeConstructorDepth(const VariableType &type)
242*35238bceSAndroid Build Coastguard Worker {
243*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
244*35238bceSAndroid Build Coastguard Worker     {
245*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_STRUCT:
246*35238bceSAndroid Build Coastguard Worker     {
247*35238bceSAndroid Build Coastguard Worker         const vector<VariableType::Member> &members = type.getMembers();
248*35238bceSAndroid Build Coastguard Worker         int maxDepth                                = 0;
249*35238bceSAndroid Build Coastguard Worker         for (vector<VariableType::Member>::const_iterator i = members.begin(); i != members.end(); i++)
250*35238bceSAndroid Build Coastguard Worker         {
251*35238bceSAndroid Build Coastguard Worker             const VariableType &memberType = i->getType();
252*35238bceSAndroid Build Coastguard Worker             int depth                      = 0;
253*35238bceSAndroid Build Coastguard Worker             switch (memberType.getBaseType())
254*35238bceSAndroid Build Coastguard Worker             {
255*35238bceSAndroid Build Coastguard Worker             case VariableType::TYPE_STRUCT:
256*35238bceSAndroid Build Coastguard Worker                 depth = getTypeConstructorDepth(memberType);
257*35238bceSAndroid Build Coastguard Worker                 break;
258*35238bceSAndroid Build Coastguard Worker 
259*35238bceSAndroid Build Coastguard Worker             case VariableType::TYPE_BOOL:
260*35238bceSAndroid Build Coastguard Worker             case VariableType::TYPE_FLOAT:
261*35238bceSAndroid Build Coastguard Worker             case VariableType::TYPE_INT:
262*35238bceSAndroid Build Coastguard Worker                 depth = memberType.getNumElements() == 1 ? 1 : 2;
263*35238bceSAndroid Build Coastguard Worker                 break;
264*35238bceSAndroid Build Coastguard Worker 
265*35238bceSAndroid Build Coastguard Worker             default:
266*35238bceSAndroid Build Coastguard Worker                 DE_ASSERT(false);
267*35238bceSAndroid Build Coastguard Worker                 break;
268*35238bceSAndroid Build Coastguard Worker             }
269*35238bceSAndroid Build Coastguard Worker 
270*35238bceSAndroid Build Coastguard Worker             maxDepth = de::max(maxDepth, depth);
271*35238bceSAndroid Build Coastguard Worker         }
272*35238bceSAndroid Build Coastguard Worker         return maxDepth + 1;
273*35238bceSAndroid Build Coastguard Worker     }
274*35238bceSAndroid Build Coastguard Worker 
275*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
276*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
277*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
278*35238bceSAndroid Build Coastguard Worker         return 2; // One node for ctor, another for value
279*35238bceSAndroid Build Coastguard Worker 
280*35238bceSAndroid Build Coastguard Worker     default:
281*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
282*35238bceSAndroid Build Coastguard Worker         return 0;
283*35238bceSAndroid Build Coastguard Worker     }
284*35238bceSAndroid Build Coastguard Worker }
285*35238bceSAndroid Build Coastguard Worker 
getConservativeValueExprDepth(const GeneratorState & state,ConstValueRangeAccess valueRange)286*35238bceSAndroid Build Coastguard Worker int getConservativeValueExprDepth(const GeneratorState &state, ConstValueRangeAccess valueRange)
287*35238bceSAndroid Build Coastguard Worker {
288*35238bceSAndroid Build Coastguard Worker     // \todo [2011-03-22 pyry] Do a look-up into variable manager?
289*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
290*35238bceSAndroid Build Coastguard Worker     return getTypeConstructorDepth(valueRange.getType());
291*35238bceSAndroid Build Coastguard Worker }
292*35238bceSAndroid Build Coastguard Worker 
computeRangeLengthSum(ConstValueRangeAccess valueRange)293*35238bceSAndroid Build Coastguard Worker static float computeRangeLengthSum(ConstValueRangeAccess valueRange)
294*35238bceSAndroid Build Coastguard Worker {
295*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
296*35238bceSAndroid Build Coastguard Worker     float rangeLength        = 0.0f;
297*35238bceSAndroid Build Coastguard Worker 
298*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
299*35238bceSAndroid Build Coastguard Worker     {
300*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
301*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
302*35238bceSAndroid Build Coastguard Worker         {
303*35238bceSAndroid Build Coastguard Worker             float minVal = valueRange.component(ndx).getMin().asFloat();
304*35238bceSAndroid Build Coastguard Worker             float maxVal = valueRange.component(ndx).getMax().asFloat();
305*35238bceSAndroid Build Coastguard Worker             rangeLength += maxVal - minVal;
306*35238bceSAndroid Build Coastguard Worker         }
307*35238bceSAndroid Build Coastguard Worker         break;
308*35238bceSAndroid Build Coastguard Worker 
309*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
310*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
311*35238bceSAndroid Build Coastguard Worker         {
312*35238bceSAndroid Build Coastguard Worker             int minVal = valueRange.component(ndx).getMin().asBool() ? 1 : 0;
313*35238bceSAndroid Build Coastguard Worker             int maxVal = valueRange.component(ndx).getMin().asBool() ? 1 : 0;
314*35238bceSAndroid Build Coastguard Worker             rangeLength += (float)(maxVal - minVal);
315*35238bceSAndroid Build Coastguard Worker         }
316*35238bceSAndroid Build Coastguard Worker         break;
317*35238bceSAndroid Build Coastguard Worker 
318*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
319*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_SAMPLER_2D:
320*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_SAMPLER_CUBE:
321*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
322*35238bceSAndroid Build Coastguard Worker         {
323*35238bceSAndroid Build Coastguard Worker             int64_t minVal = valueRange.component(ndx).getMin().asInt();
324*35238bceSAndroid Build Coastguard Worker             int64_t maxVal = valueRange.component(ndx).getMax().asInt();
325*35238bceSAndroid Build Coastguard Worker             rangeLength += (float)((int)(maxVal - minVal));
326*35238bceSAndroid Build Coastguard Worker         }
327*35238bceSAndroid Build Coastguard Worker         break;
328*35238bceSAndroid Build Coastguard Worker 
329*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_ARRAY:
330*35238bceSAndroid Build Coastguard Worker     {
331*35238bceSAndroid Build Coastguard Worker         int numElements = type.getNumElements();
332*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < numElements; ndx++)
333*35238bceSAndroid Build Coastguard Worker             rangeLength += computeRangeLengthSum(valueRange.arrayElement(ndx));
334*35238bceSAndroid Build Coastguard Worker         break;
335*35238bceSAndroid Build Coastguard Worker     }
336*35238bceSAndroid Build Coastguard Worker 
337*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_STRUCT:
338*35238bceSAndroid Build Coastguard Worker     {
339*35238bceSAndroid Build Coastguard Worker         int numMembers = (int)type.getMembers().size();
340*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < numMembers; ndx++)
341*35238bceSAndroid Build Coastguard Worker             rangeLength += computeRangeLengthSum(valueRange.member(ndx));
342*35238bceSAndroid Build Coastguard Worker         break;
343*35238bceSAndroid Build Coastguard Worker     }
344*35238bceSAndroid Build Coastguard Worker 
345*35238bceSAndroid Build Coastguard Worker     default:
346*35238bceSAndroid Build Coastguard Worker         TCU_FAIL("Invalid type");
347*35238bceSAndroid Build Coastguard Worker     }
348*35238bceSAndroid Build Coastguard Worker 
349*35238bceSAndroid Build Coastguard Worker     return rangeLength;
350*35238bceSAndroid Build Coastguard Worker }
351*35238bceSAndroid Build Coastguard Worker 
computeDynamicRangeWeight(ConstValueRangeAccess valueRange)352*35238bceSAndroid Build Coastguard Worker float computeDynamicRangeWeight(ConstValueRangeAccess valueRange)
353*35238bceSAndroid Build Coastguard Worker {
354*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
355*35238bceSAndroid Build Coastguard Worker     float rangeLenSum        = computeRangeLengthSum(valueRange);
356*35238bceSAndroid Build Coastguard Worker     int numScalars           = type.getScalarSize();
357*35238bceSAndroid Build Coastguard Worker 
358*35238bceSAndroid Build Coastguard Worker     return rangeLenSum / (float)numScalars;
359*35238bceSAndroid Build Coastguard Worker }
360*35238bceSAndroid Build Coastguard Worker 
361*35238bceSAndroid Build Coastguard Worker } // namespace rsg
362