xref: /aosp_15_r20/external/deqp/framework/randomshaders/rsgExpression.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 Expressions.
22*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "rsgExpression.hpp"
25*35238bceSAndroid Build Coastguard Worker #include "rsgVariableManager.hpp"
26*35238bceSAndroid Build Coastguard Worker #include "rsgBinaryOps.hpp"
27*35238bceSAndroid Build Coastguard Worker #include "rsgBuiltinFunctions.hpp"
28*35238bceSAndroid Build Coastguard Worker #include "rsgUtils.hpp"
29*35238bceSAndroid Build Coastguard Worker #include "deMath.h"
30*35238bceSAndroid Build Coastguard Worker 
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 
36*35238bceSAndroid Build Coastguard Worker namespace
37*35238bceSAndroid Build Coastguard Worker {
38*35238bceSAndroid Build Coastguard Worker 
39*35238bceSAndroid Build Coastguard Worker class IsReadableEntry
40*35238bceSAndroid Build Coastguard Worker {
41*35238bceSAndroid Build Coastguard Worker public:
42*35238bceSAndroid Build Coastguard Worker     typedef ValueEntryIterator<IsReadableEntry> Iterator;
43*35238bceSAndroid Build Coastguard Worker 
IsReadableEntry(uint32_t exprFlags)44*35238bceSAndroid Build Coastguard Worker     IsReadableEntry(uint32_t exprFlags) : m_exprFlags(exprFlags)
45*35238bceSAndroid Build Coastguard Worker     {
46*35238bceSAndroid Build Coastguard Worker     }
47*35238bceSAndroid Build Coastguard Worker 
operator ()(const ValueEntry * entry) const48*35238bceSAndroid Build Coastguard Worker     bool operator()(const ValueEntry *entry) const
49*35238bceSAndroid Build Coastguard Worker     {
50*35238bceSAndroid Build Coastguard Worker         if ((m_exprFlags & CONST_EXPR) && (entry->getVariable()->getStorage() != Variable::STORAGE_CONST))
51*35238bceSAndroid Build Coastguard Worker             return false;
52*35238bceSAndroid Build Coastguard Worker 
53*35238bceSAndroid Build Coastguard Worker         return true;
54*35238bceSAndroid Build Coastguard Worker     }
55*35238bceSAndroid Build Coastguard Worker 
56*35238bceSAndroid Build Coastguard Worker private:
57*35238bceSAndroid Build Coastguard Worker     uint32_t m_exprFlags;
58*35238bceSAndroid Build Coastguard Worker };
59*35238bceSAndroid Build Coastguard Worker 
60*35238bceSAndroid Build Coastguard Worker class IsReadableIntersectingEntry : public IsReadableEntry
61*35238bceSAndroid Build Coastguard Worker {
62*35238bceSAndroid Build Coastguard Worker public:
63*35238bceSAndroid Build Coastguard Worker     typedef ValueEntryIterator<IsReadableIntersectingEntry> Iterator;
64*35238bceSAndroid Build Coastguard Worker 
IsReadableIntersectingEntry(ConstValueRangeAccess valueRange,uint32_t exprFlags)65*35238bceSAndroid Build Coastguard Worker     IsReadableIntersectingEntry(ConstValueRangeAccess valueRange, uint32_t exprFlags)
66*35238bceSAndroid Build Coastguard Worker         : IsReadableEntry(exprFlags)
67*35238bceSAndroid Build Coastguard Worker         , m_valueRange(valueRange)
68*35238bceSAndroid Build Coastguard Worker     {
69*35238bceSAndroid Build Coastguard Worker     }
70*35238bceSAndroid Build Coastguard Worker 
operator ()(const ValueEntry * entry) const71*35238bceSAndroid Build Coastguard Worker     bool operator()(const ValueEntry *entry) const
72*35238bceSAndroid Build Coastguard Worker     {
73*35238bceSAndroid Build Coastguard Worker         if (!IsReadableEntry::operator()(entry))
74*35238bceSAndroid Build Coastguard Worker             return false;
75*35238bceSAndroid Build Coastguard Worker 
76*35238bceSAndroid Build Coastguard Worker         if (entry->getValueRange().getType() != m_valueRange.getType())
77*35238bceSAndroid Build Coastguard Worker             return false;
78*35238bceSAndroid Build Coastguard Worker 
79*35238bceSAndroid Build Coastguard Worker         if (!entry->getValueRange().intersects(m_valueRange))
80*35238bceSAndroid Build Coastguard Worker             return false;
81*35238bceSAndroid Build Coastguard Worker 
82*35238bceSAndroid Build Coastguard Worker         return true;
83*35238bceSAndroid Build Coastguard Worker     }
84*35238bceSAndroid Build Coastguard Worker 
85*35238bceSAndroid Build Coastguard Worker private:
86*35238bceSAndroid Build Coastguard Worker     ConstValueRangeAccess m_valueRange;
87*35238bceSAndroid Build Coastguard Worker };
88*35238bceSAndroid Build Coastguard Worker 
89*35238bceSAndroid Build Coastguard Worker class IsWritableIntersectingEntry : public IsWritableEntry
90*35238bceSAndroid Build Coastguard Worker {
91*35238bceSAndroid Build Coastguard Worker public:
92*35238bceSAndroid Build Coastguard Worker     typedef ValueEntryIterator<IsWritableIntersectingEntry> Iterator;
93*35238bceSAndroid Build Coastguard Worker 
IsWritableIntersectingEntry(ConstValueRangeAccess valueRange)94*35238bceSAndroid Build Coastguard Worker     IsWritableIntersectingEntry(ConstValueRangeAccess valueRange) : m_valueRange(valueRange)
95*35238bceSAndroid Build Coastguard Worker     {
96*35238bceSAndroid Build Coastguard Worker     }
97*35238bceSAndroid Build Coastguard Worker 
operator ()(const ValueEntry * entry) const98*35238bceSAndroid Build Coastguard Worker     bool operator()(const ValueEntry *entry) const
99*35238bceSAndroid Build Coastguard Worker     {
100*35238bceSAndroid Build Coastguard Worker         return IsWritableEntry::operator()(entry) && entry->getVariable()->getType() == m_valueRange.getType() &&
101*35238bceSAndroid Build Coastguard Worker                entry->getValueRange().intersects(m_valueRange);
102*35238bceSAndroid Build Coastguard Worker     }
103*35238bceSAndroid Build Coastguard Worker 
104*35238bceSAndroid Build Coastguard Worker private:
105*35238bceSAndroid Build Coastguard Worker     ConstValueRangeAccess m_valueRange;
106*35238bceSAndroid Build Coastguard Worker };
107*35238bceSAndroid Build Coastguard Worker 
108*35238bceSAndroid Build Coastguard Worker class IsWritableSupersetEntry : public IsWritableEntry
109*35238bceSAndroid Build Coastguard Worker {
110*35238bceSAndroid Build Coastguard Worker public:
111*35238bceSAndroid Build Coastguard Worker     typedef ValueEntryIterator<IsWritableSupersetEntry> Iterator;
112*35238bceSAndroid Build Coastguard Worker 
IsWritableSupersetEntry(ConstValueRangeAccess valueRange)113*35238bceSAndroid Build Coastguard Worker     IsWritableSupersetEntry(ConstValueRangeAccess valueRange) : m_valueRange(valueRange)
114*35238bceSAndroid Build Coastguard Worker     {
115*35238bceSAndroid Build Coastguard Worker     }
116*35238bceSAndroid Build Coastguard Worker 
operator ()(const ValueEntry * entry) const117*35238bceSAndroid Build Coastguard Worker     bool operator()(const ValueEntry *entry) const
118*35238bceSAndroid Build Coastguard Worker     {
119*35238bceSAndroid Build Coastguard Worker         return IsWritableEntry()(entry) && entry->getVariable()->getType() == m_valueRange.getType() &&
120*35238bceSAndroid Build Coastguard Worker                entry->getValueRange().isSupersetOf(m_valueRange);
121*35238bceSAndroid Build Coastguard Worker     }
122*35238bceSAndroid Build Coastguard Worker 
123*35238bceSAndroid Build Coastguard Worker private:
124*35238bceSAndroid Build Coastguard Worker     ConstValueRangeAccess m_valueRange;
125*35238bceSAndroid Build Coastguard Worker };
126*35238bceSAndroid Build Coastguard Worker 
127*35238bceSAndroid Build Coastguard Worker class IsSamplerEntry
128*35238bceSAndroid Build Coastguard Worker {
129*35238bceSAndroid Build Coastguard Worker public:
130*35238bceSAndroid Build Coastguard Worker     typedef ValueEntryIterator<IsSamplerEntry> Iterator;
131*35238bceSAndroid Build Coastguard Worker 
IsSamplerEntry(VariableType::Type type)132*35238bceSAndroid Build Coastguard Worker     IsSamplerEntry(VariableType::Type type) : m_type(type)
133*35238bceSAndroid Build Coastguard Worker     {
134*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(m_type == VariableType::TYPE_SAMPLER_2D || m_type == VariableType::TYPE_SAMPLER_CUBE);
135*35238bceSAndroid Build Coastguard Worker     }
136*35238bceSAndroid Build Coastguard Worker 
operator ()(const ValueEntry * entry) const137*35238bceSAndroid Build Coastguard Worker     bool operator()(const ValueEntry *entry) const
138*35238bceSAndroid Build Coastguard Worker     {
139*35238bceSAndroid Build Coastguard Worker         if (entry->getVariable()->getType() == VariableType(m_type, 1))
140*35238bceSAndroid Build Coastguard Worker         {
141*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(entry->getVariable()->getStorage() == Variable::STORAGE_UNIFORM);
142*35238bceSAndroid Build Coastguard Worker             return true;
143*35238bceSAndroid Build Coastguard Worker         }
144*35238bceSAndroid Build Coastguard Worker         else
145*35238bceSAndroid Build Coastguard Worker             return false;
146*35238bceSAndroid Build Coastguard Worker     }
147*35238bceSAndroid Build Coastguard Worker 
148*35238bceSAndroid Build Coastguard Worker private:
149*35238bceSAndroid Build Coastguard Worker     VariableType::Type m_type;
150*35238bceSAndroid Build Coastguard Worker };
151*35238bceSAndroid Build Coastguard Worker 
getWeightedBool(de::Random & random,float trueWeight)152*35238bceSAndroid Build Coastguard Worker inline bool getWeightedBool(de::Random &random, float trueWeight)
153*35238bceSAndroid Build Coastguard Worker {
154*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(de::inRange<float>(trueWeight, 0.0f, 1.0f));
155*35238bceSAndroid Build Coastguard Worker     return (random.getFloat() < trueWeight);
156*35238bceSAndroid Build Coastguard Worker }
157*35238bceSAndroid Build Coastguard Worker 
computeRandomValueRangeForInfElements(GeneratorState & state,ValueRangeAccess valueRange)158*35238bceSAndroid Build Coastguard Worker void computeRandomValueRangeForInfElements(GeneratorState &state, ValueRangeAccess valueRange)
159*35238bceSAndroid Build Coastguard Worker {
160*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
161*35238bceSAndroid Build Coastguard Worker     de::Random &rnd          = state.getRandom();
162*35238bceSAndroid Build Coastguard Worker 
163*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
164*35238bceSAndroid Build Coastguard Worker     {
165*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
166*35238bceSAndroid Build Coastguard Worker         // No need to handle bool as it will be false, true
167*35238bceSAndroid Build Coastguard Worker         break;
168*35238bceSAndroid Build Coastguard Worker 
169*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
170*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
171*35238bceSAndroid Build Coastguard Worker         {
172*35238bceSAndroid Build Coastguard Worker             if (valueRange.getMin().component(ndx).asScalar() != Scalar::min<int>() ||
173*35238bceSAndroid Build Coastguard Worker                 valueRange.getMax().component(ndx).asScalar() != Scalar::max<int>())
174*35238bceSAndroid Build Coastguard Worker                 continue;
175*35238bceSAndroid Build Coastguard Worker 
176*35238bceSAndroid Build Coastguard Worker             const int minIntVal   = -16;
177*35238bceSAndroid Build Coastguard Worker             const int maxIntVal   = 16;
178*35238bceSAndroid Build Coastguard Worker             const int maxRangeLen = maxIntVal - minIntVal;
179*35238bceSAndroid Build Coastguard Worker 
180*35238bceSAndroid Build Coastguard Worker             int rangeLen = rnd.getInt(0, maxRangeLen);
181*35238bceSAndroid Build Coastguard Worker             int minVal   = minIntVal + rnd.getInt(0, maxRangeLen - rangeLen);
182*35238bceSAndroid Build Coastguard Worker             int maxVal   = minVal + rangeLen;
183*35238bceSAndroid Build Coastguard Worker 
184*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx).asInt() = minVal;
185*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx).asInt() = maxVal;
186*35238bceSAndroid Build Coastguard Worker         }
187*35238bceSAndroid Build Coastguard Worker         break;
188*35238bceSAndroid Build Coastguard Worker 
189*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
190*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
191*35238bceSAndroid Build Coastguard Worker         {
192*35238bceSAndroid Build Coastguard Worker             if (valueRange.getMin().component(ndx).asScalar() != Scalar::min<float>() ||
193*35238bceSAndroid Build Coastguard Worker                 valueRange.getMax().component(ndx).asScalar() != Scalar::max<float>())
194*35238bceSAndroid Build Coastguard Worker                 continue;
195*35238bceSAndroid Build Coastguard Worker 
196*35238bceSAndroid Build Coastguard Worker             const float step        = 0.1f;
197*35238bceSAndroid Build Coastguard Worker             const int maxSteps      = 320;
198*35238bceSAndroid Build Coastguard Worker             const float minFloatVal = -16.0f;
199*35238bceSAndroid Build Coastguard Worker 
200*35238bceSAndroid Build Coastguard Worker             int rangeLen = rnd.getInt(0, maxSteps);
201*35238bceSAndroid Build Coastguard Worker             int minStep  = rnd.getInt(0, maxSteps - rangeLen);
202*35238bceSAndroid Build Coastguard Worker 
203*35238bceSAndroid Build Coastguard Worker             float minVal = minFloatVal + step * (float)minStep;
204*35238bceSAndroid Build Coastguard Worker             float maxVal = minVal + step * (float)rangeLen;
205*35238bceSAndroid Build Coastguard Worker 
206*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx).asFloat() = minVal;
207*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx).asFloat() = maxVal;
208*35238bceSAndroid Build Coastguard Worker         }
209*35238bceSAndroid Build Coastguard Worker         break;
210*35238bceSAndroid Build Coastguard Worker 
211*35238bceSAndroid Build Coastguard Worker     default:
212*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
213*35238bceSAndroid Build Coastguard Worker         throw Exception("computeRandomValueRangeForInfElements(): unsupported type");
214*35238bceSAndroid Build Coastguard Worker     }
215*35238bceSAndroid Build Coastguard Worker }
216*35238bceSAndroid Build Coastguard Worker 
setInfiniteRange(ValueRangeAccess valueRange)217*35238bceSAndroid Build Coastguard Worker void setInfiniteRange(ValueRangeAccess valueRange)
218*35238bceSAndroid Build Coastguard Worker {
219*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
220*35238bceSAndroid Build Coastguard Worker 
221*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
222*35238bceSAndroid Build Coastguard Worker     {
223*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
224*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
225*35238bceSAndroid Build Coastguard Worker         {
226*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx) = Scalar::min<bool>();
227*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx) = Scalar::max<bool>();
228*35238bceSAndroid Build Coastguard Worker         }
229*35238bceSAndroid Build Coastguard Worker         break;
230*35238bceSAndroid Build Coastguard Worker 
231*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
232*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
233*35238bceSAndroid Build Coastguard Worker         {
234*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx) = Scalar::min<int>();
235*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx) = Scalar::max<int>();
236*35238bceSAndroid Build Coastguard Worker         }
237*35238bceSAndroid Build Coastguard Worker         break;
238*35238bceSAndroid Build Coastguard Worker 
239*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
240*35238bceSAndroid Build Coastguard Worker         for (int ndx = 0; ndx < type.getNumElements(); ndx++)
241*35238bceSAndroid Build Coastguard Worker         {
242*35238bceSAndroid Build Coastguard Worker             valueRange.getMin().component(ndx) = Scalar::min<float>();
243*35238bceSAndroid Build Coastguard Worker             valueRange.getMax().component(ndx) = Scalar::max<float>();
244*35238bceSAndroid Build Coastguard Worker         }
245*35238bceSAndroid Build Coastguard Worker         break;
246*35238bceSAndroid Build Coastguard Worker 
247*35238bceSAndroid Build Coastguard Worker     default:
248*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
249*35238bceSAndroid Build Coastguard Worker         throw Exception("setInfiniteRange(): unsupported type");
250*35238bceSAndroid Build Coastguard Worker     }
251*35238bceSAndroid Build Coastguard Worker }
252*35238bceSAndroid Build Coastguard Worker 
canAllocateVariable(const GeneratorState & state,const VariableType & type)253*35238bceSAndroid Build Coastguard Worker bool canAllocateVariable(const GeneratorState &state, const VariableType &type)
254*35238bceSAndroid Build Coastguard Worker {
255*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(!type.isVoid());
256*35238bceSAndroid Build Coastguard Worker 
257*35238bceSAndroid Build Coastguard Worker     if (state.getExpressionFlags() & NO_VAR_ALLOCATION)
258*35238bceSAndroid Build Coastguard Worker         return false;
259*35238bceSAndroid Build Coastguard Worker 
260*35238bceSAndroid Build Coastguard Worker     if (state.getVariableManager().getNumAllocatedScalars() + type.getScalarSize() >
261*35238bceSAndroid Build Coastguard Worker         state.getShaderParameters().maxCombinedVariableScalars)
262*35238bceSAndroid Build Coastguard Worker         return false;
263*35238bceSAndroid Build Coastguard Worker 
264*35238bceSAndroid Build Coastguard Worker     return true;
265*35238bceSAndroid Build Coastguard Worker }
266*35238bceSAndroid Build Coastguard Worker 
267*35238bceSAndroid Build Coastguard Worker template <class T>
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)268*35238bceSAndroid Build Coastguard Worker float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
269*35238bceSAndroid Build Coastguard Worker {
270*35238bceSAndroid Build Coastguard Worker     return T::getWeight(state, valueRange);
271*35238bceSAndroid Build Coastguard Worker }
272*35238bceSAndroid Build Coastguard Worker template <class T>
create(GeneratorState & state,ConstValueRangeAccess valueRange)273*35238bceSAndroid Build Coastguard Worker Expression *create(GeneratorState &state, ConstValueRangeAccess valueRange)
274*35238bceSAndroid Build Coastguard Worker {
275*35238bceSAndroid Build Coastguard Worker     return new T(state, valueRange);
276*35238bceSAndroid Build Coastguard Worker }
277*35238bceSAndroid Build Coastguard Worker 
278*35238bceSAndroid Build Coastguard Worker struct ExpressionSpec
279*35238bceSAndroid Build Coastguard Worker {
280*35238bceSAndroid Build Coastguard Worker     float (*getWeight)(const GeneratorState &state, ConstValueRangeAccess valueRange);
281*35238bceSAndroid Build Coastguard Worker     Expression *(*create)(GeneratorState &state, ConstValueRangeAccess valueRange);
282*35238bceSAndroid Build Coastguard Worker };
283*35238bceSAndroid Build Coastguard Worker 
284*35238bceSAndroid Build Coastguard Worker static const ExpressionSpec s_expressionSpecs[] = {{getWeight<FloatLiteral>, create<FloatLiteral>},
285*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<IntLiteral>, create<IntLiteral>},
286*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<BoolLiteral>, create<BoolLiteral>},
287*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<ConstructorOp>, create<ConstructorOp>},
288*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<AssignOp>, create<AssignOp>},
289*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<VariableRead>, create<VariableRead>},
290*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<MulOp>, create<MulOp>},
291*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<AddOp>, create<AddOp>},
292*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<SubOp>, create<SubOp>},
293*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<LessThanOp>, create<LessThanOp>},
294*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<LessOrEqualOp>, create<LessOrEqualOp>},
295*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<GreaterThanOp>, create<GreaterThanOp>},
296*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<GreaterOrEqualOp>, create<GreaterOrEqualOp>},
297*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<EqualOp>, create<EqualOp>},
298*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<NotEqualOp>, create<NotEqualOp>},
299*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<SwizzleOp>, create<SwizzleOp>},
300*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<SinOp>, create<SinOp>},
301*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<CosOp>, create<CosOp>},
302*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<TanOp>, create<TanOp>},
303*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<AsinOp>, create<AsinOp>},
304*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<AcosOp>, create<AcosOp>},
305*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<AtanOp>, create<AtanOp>},
306*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<ExpOp>, create<ExpOp>},
307*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<LogOp>, create<LogOp>},
308*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<Exp2Op>, create<Exp2Op>},
309*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<Log2Op>, create<Log2Op>},
310*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<SqrtOp>, create<SqrtOp>},
311*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<InvSqrtOp>, create<InvSqrtOp>},
312*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<ParenOp>, create<ParenOp>},
313*35238bceSAndroid Build Coastguard Worker                                                    {getWeight<TexLookup>, create<TexLookup>}};
314*35238bceSAndroid Build Coastguard Worker 
315*35238bceSAndroid Build Coastguard Worker static const ExpressionSpec s_lvalueSpecs[] = {{getWeight<VariableWrite>, create<VariableWrite>}};
316*35238bceSAndroid Build Coastguard Worker 
317*35238bceSAndroid Build Coastguard Worker #if !defined(DE_MAX)
318*35238bceSAndroid Build Coastguard Worker #define DE_MAX(a, b) ((b) > (a) ? (b) : (a))
319*35238bceSAndroid Build Coastguard Worker #endif
320*35238bceSAndroid Build Coastguard Worker 
321*35238bceSAndroid Build Coastguard Worker enum
322*35238bceSAndroid Build Coastguard Worker {
323*35238bceSAndroid Build Coastguard Worker     MAX_EXPRESSION_SPECS = (int)DE_MAX(DE_LENGTH_OF_ARRAY(s_expressionSpecs), DE_LENGTH_OF_ARRAY(s_lvalueSpecs))
324*35238bceSAndroid Build Coastguard Worker };
325*35238bceSAndroid Build Coastguard Worker 
chooseExpression(GeneratorState & state,const ExpressionSpec * specs,int numSpecs,ConstValueRangeAccess valueRange)326*35238bceSAndroid Build Coastguard Worker const ExpressionSpec *chooseExpression(GeneratorState &state, const ExpressionSpec *specs, int numSpecs,
327*35238bceSAndroid Build Coastguard Worker                                        ConstValueRangeAccess valueRange)
328*35238bceSAndroid Build Coastguard Worker {
329*35238bceSAndroid Build Coastguard Worker     float weights[MAX_EXPRESSION_SPECS];
330*35238bceSAndroid Build Coastguard Worker 
331*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(numSpecs <= (int)DE_LENGTH_OF_ARRAY(weights));
332*35238bceSAndroid Build Coastguard Worker 
333*35238bceSAndroid Build Coastguard Worker     // Compute weights
334*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < numSpecs; ndx++)
335*35238bceSAndroid Build Coastguard Worker         weights[ndx] = specs[ndx].getWeight(state, valueRange);
336*35238bceSAndroid Build Coastguard Worker 
337*35238bceSAndroid Build Coastguard Worker     // Choose
338*35238bceSAndroid Build Coastguard Worker     return &state.getRandom().chooseWeighted<const ExpressionSpec &>(specs, specs + numSpecs, weights);
339*35238bceSAndroid Build Coastguard Worker }
340*35238bceSAndroid Build Coastguard Worker 
341*35238bceSAndroid Build Coastguard Worker } // namespace
342*35238bceSAndroid Build Coastguard Worker 
~Expression(void)343*35238bceSAndroid Build Coastguard Worker Expression::~Expression(void)
344*35238bceSAndroid Build Coastguard Worker {
345*35238bceSAndroid Build Coastguard Worker }
346*35238bceSAndroid Build Coastguard Worker 
createRandom(GeneratorState & state,ConstValueRangeAccess valueRange)347*35238bceSAndroid Build Coastguard Worker Expression *Expression::createRandom(GeneratorState &state, ConstValueRangeAccess valueRange)
348*35238bceSAndroid Build Coastguard Worker {
349*35238bceSAndroid Build Coastguard Worker     return chooseExpression(state, s_expressionSpecs, (int)DE_LENGTH_OF_ARRAY(s_expressionSpecs), valueRange)
350*35238bceSAndroid Build Coastguard Worker         ->create(state, valueRange);
351*35238bceSAndroid Build Coastguard Worker }
352*35238bceSAndroid Build Coastguard Worker 
createRandomLValue(GeneratorState & state,ConstValueRangeAccess valueRange)353*35238bceSAndroid Build Coastguard Worker Expression *Expression::createRandomLValue(GeneratorState &state, ConstValueRangeAccess valueRange)
354*35238bceSAndroid Build Coastguard Worker {
355*35238bceSAndroid Build Coastguard Worker     return chooseExpression(state, s_lvalueSpecs, (int)DE_LENGTH_OF_ARRAY(s_lvalueSpecs), valueRange)
356*35238bceSAndroid Build Coastguard Worker         ->create(state, valueRange);
357*35238bceSAndroid Build Coastguard Worker }
358*35238bceSAndroid Build Coastguard Worker 
FloatLiteral(GeneratorState & state,ConstValueRangeAccess valueRange)359*35238bceSAndroid Build Coastguard Worker FloatLiteral::FloatLiteral(GeneratorState &state, ConstValueRangeAccess valueRange)
360*35238bceSAndroid Build Coastguard Worker     : m_value(VariableType::getScalarType(VariableType::TYPE_FLOAT))
361*35238bceSAndroid Build Coastguard Worker {
362*35238bceSAndroid Build Coastguard Worker     float minVal = -10.0f;
363*35238bceSAndroid Build Coastguard Worker     float maxVal = +10.0f;
364*35238bceSAndroid Build Coastguard Worker     float step   = 0.25f;
365*35238bceSAndroid Build Coastguard Worker 
366*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType() == VariableType(VariableType::TYPE_FLOAT, 1))
367*35238bceSAndroid Build Coastguard Worker     {
368*35238bceSAndroid Build Coastguard Worker         minVal = valueRange.getMin().component(0).asFloat();
369*35238bceSAndroid Build Coastguard Worker         maxVal = valueRange.getMax().component(0).asFloat();
370*35238bceSAndroid Build Coastguard Worker 
371*35238bceSAndroid Build Coastguard Worker         if (Scalar::min<float>() == minVal)
372*35238bceSAndroid Build Coastguard Worker             minVal = -10.0f;
373*35238bceSAndroid Build Coastguard Worker 
374*35238bceSAndroid Build Coastguard Worker         if (Scalar::max<float>() == maxVal)
375*35238bceSAndroid Build Coastguard Worker             maxVal = +10.0f;
376*35238bceSAndroid Build Coastguard Worker     }
377*35238bceSAndroid Build Coastguard Worker 
378*35238bceSAndroid Build Coastguard Worker     int numSteps = (int)((maxVal - minVal) / step) + 1;
379*35238bceSAndroid Build Coastguard Worker 
380*35238bceSAndroid Build Coastguard Worker     const float value      = deFloatClamp(minVal + step * (float)state.getRandom().getInt(0, numSteps), minVal, maxVal);
381*35238bceSAndroid Build Coastguard Worker     ExecValueAccess access = m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT));
382*35238bceSAndroid Build Coastguard Worker 
383*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
384*35238bceSAndroid Build Coastguard Worker         access.asFloat(ndx) = value;
385*35238bceSAndroid Build Coastguard Worker }
386*35238bceSAndroid Build Coastguard Worker 
FloatLiteral(float customValue)387*35238bceSAndroid Build Coastguard Worker FloatLiteral::FloatLiteral(float customValue) : m_value(VariableType::getScalarType(VariableType::TYPE_FLOAT))
388*35238bceSAndroid Build Coastguard Worker {
389*35238bceSAndroid Build Coastguard Worker     // This constructor is required to handle corner case in which comparision
390*35238bceSAndroid Build Coastguard Worker     // of two same floats produced different results - this was resolved by
391*35238bceSAndroid Build Coastguard Worker     // adding FloatLiteral containing epsilon to one of values
392*35238bceSAndroid Build Coastguard Worker     ExecValueAccess access = m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT));
393*35238bceSAndroid Build Coastguard Worker 
394*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
395*35238bceSAndroid Build Coastguard Worker         access.asFloat(ndx) = customValue;
396*35238bceSAndroid Build Coastguard Worker }
397*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)398*35238bceSAndroid Build Coastguard Worker float FloatLiteral::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
399*35238bceSAndroid Build Coastguard Worker {
400*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
401*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
402*35238bceSAndroid Build Coastguard Worker     if (type == VariableType(VariableType::TYPE_FLOAT, 1))
403*35238bceSAndroid Build Coastguard Worker     {
404*35238bceSAndroid Build Coastguard Worker         float minVal = valueRange.getMin().asFloat();
405*35238bceSAndroid Build Coastguard Worker         float maxVal = valueRange.getMax().asFloat();
406*35238bceSAndroid Build Coastguard Worker 
407*35238bceSAndroid Build Coastguard Worker         if (Scalar::min<float>() == minVal && Scalar::max<float>() == maxVal)
408*35238bceSAndroid Build Coastguard Worker             return 0.1f;
409*35238bceSAndroid Build Coastguard Worker 
410*35238bceSAndroid Build Coastguard Worker         // Weight based on value range length
411*35238bceSAndroid Build Coastguard Worker         float rangeLength = maxVal - minVal;
412*35238bceSAndroid Build Coastguard Worker 
413*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(rangeLength >= 0.0f);
414*35238bceSAndroid Build Coastguard Worker         return deFloatMax(0.1f, 1.0f - rangeLength);
415*35238bceSAndroid Build Coastguard Worker     }
416*35238bceSAndroid Build Coastguard Worker     else if (type.isVoid())
417*35238bceSAndroid Build Coastguard Worker         return unusedValueWeight;
418*35238bceSAndroid Build Coastguard Worker     else
419*35238bceSAndroid Build Coastguard Worker         return 0.0f;
420*35238bceSAndroid Build Coastguard Worker }
421*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const422*35238bceSAndroid Build Coastguard Worker void FloatLiteral::tokenize(GeneratorState &state, TokenStream &str) const
423*35238bceSAndroid Build Coastguard Worker {
424*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
425*35238bceSAndroid Build Coastguard Worker     str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT)).asFloat(0));
426*35238bceSAndroid Build Coastguard Worker }
427*35238bceSAndroid Build Coastguard Worker 
IntLiteral(GeneratorState & state,ConstValueRangeAccess valueRange)428*35238bceSAndroid Build Coastguard Worker IntLiteral::IntLiteral(GeneratorState &state, ConstValueRangeAccess valueRange)
429*35238bceSAndroid Build Coastguard Worker     : m_value(VariableType::getScalarType(VariableType::TYPE_INT))
430*35238bceSAndroid Build Coastguard Worker {
431*35238bceSAndroid Build Coastguard Worker     int minVal = -16;
432*35238bceSAndroid Build Coastguard Worker     int maxVal = +16;
433*35238bceSAndroid Build Coastguard Worker 
434*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType() == VariableType(VariableType::TYPE_INT, 1))
435*35238bceSAndroid Build Coastguard Worker     {
436*35238bceSAndroid Build Coastguard Worker         minVal = valueRange.getMin().component(0).asInt();
437*35238bceSAndroid Build Coastguard Worker         maxVal = valueRange.getMax().component(0).asInt();
438*35238bceSAndroid Build Coastguard Worker 
439*35238bceSAndroid Build Coastguard Worker         if (Scalar::min<int>() == minVal)
440*35238bceSAndroid Build Coastguard Worker             minVal = -16;
441*35238bceSAndroid Build Coastguard Worker 
442*35238bceSAndroid Build Coastguard Worker         if (Scalar::max<int>() == maxVal)
443*35238bceSAndroid Build Coastguard Worker             maxVal = 16;
444*35238bceSAndroid Build Coastguard Worker     }
445*35238bceSAndroid Build Coastguard Worker 
446*35238bceSAndroid Build Coastguard Worker     int value              = state.getRandom().getInt(minVal, maxVal);
447*35238bceSAndroid Build Coastguard Worker     ExecValueAccess access = m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT));
448*35238bceSAndroid Build Coastguard Worker 
449*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
450*35238bceSAndroid Build Coastguard Worker         access.asInt(ndx) = value;
451*35238bceSAndroid Build Coastguard Worker }
452*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)453*35238bceSAndroid Build Coastguard Worker float IntLiteral::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
454*35238bceSAndroid Build Coastguard Worker {
455*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
456*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
457*35238bceSAndroid Build Coastguard Worker     if (type == VariableType(VariableType::TYPE_INT, 1))
458*35238bceSAndroid Build Coastguard Worker     {
459*35238bceSAndroid Build Coastguard Worker         int minVal = valueRange.getMin().asInt();
460*35238bceSAndroid Build Coastguard Worker         int maxVal = valueRange.getMax().asInt();
461*35238bceSAndroid Build Coastguard Worker 
462*35238bceSAndroid Build Coastguard Worker         if (Scalar::min<int>() == minVal && Scalar::max<int>() == maxVal)
463*35238bceSAndroid Build Coastguard Worker             return 0.1f;
464*35238bceSAndroid Build Coastguard Worker 
465*35238bceSAndroid Build Coastguard Worker         int rangeLength = maxVal - minVal;
466*35238bceSAndroid Build Coastguard Worker 
467*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(rangeLength >= 0);
468*35238bceSAndroid Build Coastguard Worker         return deFloatMax(0.1f, 1.0f - (float)rangeLength / 4.0f);
469*35238bceSAndroid Build Coastguard Worker     }
470*35238bceSAndroid Build Coastguard Worker     else if (type.isVoid())
471*35238bceSAndroid Build Coastguard Worker         return unusedValueWeight;
472*35238bceSAndroid Build Coastguard Worker     else
473*35238bceSAndroid Build Coastguard Worker         return 0.0f;
474*35238bceSAndroid Build Coastguard Worker }
475*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const476*35238bceSAndroid Build Coastguard Worker void IntLiteral::tokenize(GeneratorState &state, TokenStream &str) const
477*35238bceSAndroid Build Coastguard Worker {
478*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
479*35238bceSAndroid Build Coastguard Worker     str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT)).asInt(0));
480*35238bceSAndroid Build Coastguard Worker }
481*35238bceSAndroid Build Coastguard Worker 
BoolLiteral(GeneratorState & state,ConstValueRangeAccess valueRange)482*35238bceSAndroid Build Coastguard Worker BoolLiteral::BoolLiteral(GeneratorState &state, ConstValueRangeAccess valueRange)
483*35238bceSAndroid Build Coastguard Worker     : m_value(VariableType::getScalarType(VariableType::TYPE_BOOL))
484*35238bceSAndroid Build Coastguard Worker {
485*35238bceSAndroid Build Coastguard Worker     int minVal = 0;
486*35238bceSAndroid Build Coastguard Worker     int maxVal = 1;
487*35238bceSAndroid Build Coastguard Worker 
488*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType() == VariableType(VariableType::TYPE_BOOL, 1))
489*35238bceSAndroid Build Coastguard Worker     {
490*35238bceSAndroid Build Coastguard Worker         minVal = valueRange.getMin().component(0).asBool() ? 1 : 0;
491*35238bceSAndroid Build Coastguard Worker         maxVal = valueRange.getMax().component(0).asBool() ? 1 : 0;
492*35238bceSAndroid Build Coastguard Worker     }
493*35238bceSAndroid Build Coastguard Worker 
494*35238bceSAndroid Build Coastguard Worker     bool value             = state.getRandom().getInt(minVal, maxVal) == 1;
495*35238bceSAndroid Build Coastguard Worker     ExecValueAccess access = m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL));
496*35238bceSAndroid Build Coastguard Worker 
497*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
498*35238bceSAndroid Build Coastguard Worker         access.asBool(ndx) = value;
499*35238bceSAndroid Build Coastguard Worker }
500*35238bceSAndroid Build Coastguard Worker 
BoolLiteral(bool customValue)501*35238bceSAndroid Build Coastguard Worker BoolLiteral::BoolLiteral(bool customValue) : m_value(VariableType::getScalarType(VariableType::TYPE_BOOL))
502*35238bceSAndroid Build Coastguard Worker {
503*35238bceSAndroid Build Coastguard Worker     // This constructor is required to handle corner case in which comparision
504*35238bceSAndroid Build Coastguard Worker     // of two same floats produced different results - this was resolved by
505*35238bceSAndroid Build Coastguard Worker     // adding FloatLiteral containing epsilon to one of values
506*35238bceSAndroid Build Coastguard Worker     ExecValueAccess access = m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL));
507*35238bceSAndroid Build Coastguard Worker 
508*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
509*35238bceSAndroid Build Coastguard Worker         access.asBool(ndx) = customValue;
510*35238bceSAndroid Build Coastguard Worker }
511*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)512*35238bceSAndroid Build Coastguard Worker float BoolLiteral::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
513*35238bceSAndroid Build Coastguard Worker {
514*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
515*35238bceSAndroid Build Coastguard Worker     const VariableType &type = valueRange.getType();
516*35238bceSAndroid Build Coastguard Worker     if (type == VariableType(VariableType::TYPE_BOOL, 1))
517*35238bceSAndroid Build Coastguard Worker         return 0.5f;
518*35238bceSAndroid Build Coastguard Worker     else if (type.isVoid())
519*35238bceSAndroid Build Coastguard Worker         return unusedValueWeight;
520*35238bceSAndroid Build Coastguard Worker     else
521*35238bceSAndroid Build Coastguard Worker         return 0.0f;
522*35238bceSAndroid Build Coastguard Worker }
523*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const524*35238bceSAndroid Build Coastguard Worker void BoolLiteral::tokenize(GeneratorState &state, TokenStream &str) const
525*35238bceSAndroid Build Coastguard Worker {
526*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
527*35238bceSAndroid Build Coastguard Worker     str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL)).asBool(0));
528*35238bceSAndroid Build Coastguard Worker }
529*35238bceSAndroid Build Coastguard Worker 
530*35238bceSAndroid Build Coastguard Worker namespace
531*35238bceSAndroid Build Coastguard Worker {
532*35238bceSAndroid Build Coastguard Worker 
533*35238bceSAndroid Build Coastguard Worker // \note int-bool and float-bool conversions handled in a special way.
534*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
convert(SrcType src)535*35238bceSAndroid Build Coastguard Worker inline DstType convert(SrcType src)
536*35238bceSAndroid Build Coastguard Worker {
537*35238bceSAndroid Build Coastguard Worker     if (Scalar::min<SrcType>() == src)
538*35238bceSAndroid Build Coastguard Worker         return Scalar::min<DstType>().template as<DstType>();
539*35238bceSAndroid Build Coastguard Worker     else if (Scalar::max<SrcType>() == src)
540*35238bceSAndroid Build Coastguard Worker         return Scalar::max<DstType>().template as<DstType>();
541*35238bceSAndroid Build Coastguard Worker     else
542*35238bceSAndroid Build Coastguard Worker         return DstType(src);
543*35238bceSAndroid Build Coastguard Worker }
544*35238bceSAndroid Build Coastguard Worker 
545*35238bceSAndroid Build Coastguard Worker // According to GLSL ES spec.
546*35238bceSAndroid Build Coastguard Worker template <>
convert(float src)547*35238bceSAndroid Build Coastguard Worker inline bool convert<float, bool>(float src)
548*35238bceSAndroid Build Coastguard Worker {
549*35238bceSAndroid Build Coastguard Worker     return src != 0.0f;
550*35238bceSAndroid Build Coastguard Worker }
551*35238bceSAndroid Build Coastguard Worker template <>
convert(int src)552*35238bceSAndroid Build Coastguard Worker inline bool convert<int, bool>(int src)
553*35238bceSAndroid Build Coastguard Worker {
554*35238bceSAndroid Build Coastguard Worker     return src != 0;
555*35238bceSAndroid Build Coastguard Worker }
556*35238bceSAndroid Build Coastguard Worker template <>
convert(bool src)557*35238bceSAndroid Build Coastguard Worker inline bool convert<bool, bool>(bool src)
558*35238bceSAndroid Build Coastguard Worker {
559*35238bceSAndroid Build Coastguard Worker     return src;
560*35238bceSAndroid Build Coastguard Worker }
561*35238bceSAndroid Build Coastguard Worker template <>
convert(bool src)562*35238bceSAndroid Build Coastguard Worker inline float convert<bool, float>(bool src)
563*35238bceSAndroid Build Coastguard Worker {
564*35238bceSAndroid Build Coastguard Worker     return src ? 1.0f : 0.0f;
565*35238bceSAndroid Build Coastguard Worker }
566*35238bceSAndroid Build Coastguard Worker template <>
convert(bool src)567*35238bceSAndroid Build Coastguard Worker inline int convert<bool, int>(bool src)
568*35238bceSAndroid Build Coastguard Worker {
569*35238bceSAndroid Build Coastguard Worker     return src ? 1 : 0;
570*35238bceSAndroid Build Coastguard Worker }
571*35238bceSAndroid Build Coastguard Worker 
572*35238bceSAndroid Build Coastguard Worker template <>
convert(float src)573*35238bceSAndroid Build Coastguard Worker inline int convert<float, int>(float src)
574*35238bceSAndroid Build Coastguard Worker {
575*35238bceSAndroid Build Coastguard Worker     if (Scalar::min<float>() == src)
576*35238bceSAndroid Build Coastguard Worker         return Scalar::min<int>().as<int>();
577*35238bceSAndroid Build Coastguard Worker     else if (Scalar::max<float>() == src)
578*35238bceSAndroid Build Coastguard Worker         return Scalar::max<int>().as<int>();
579*35238bceSAndroid Build Coastguard Worker     else if (src > 0.0f)
580*35238bceSAndroid Build Coastguard Worker         return (int)deFloatFloor(src);
581*35238bceSAndroid Build Coastguard Worker     else
582*35238bceSAndroid Build Coastguard Worker         return (int)deFloatCeil(src);
583*35238bceSAndroid Build Coastguard Worker }
584*35238bceSAndroid Build Coastguard Worker 
585*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
convertValueRange(SrcType srcMin,SrcType srcMax,DstType & dstMin,DstType & dstMax)586*35238bceSAndroid Build Coastguard Worker inline void convertValueRange(SrcType srcMin, SrcType srcMax, DstType &dstMin, DstType &dstMax)
587*35238bceSAndroid Build Coastguard Worker {
588*35238bceSAndroid Build Coastguard Worker     dstMin = convert<SrcType, DstType>(srcMin);
589*35238bceSAndroid Build Coastguard Worker     dstMax = convert<SrcType, DstType>(srcMax);
590*35238bceSAndroid Build Coastguard Worker }
591*35238bceSAndroid Build Coastguard Worker 
592*35238bceSAndroid Build Coastguard Worker template <>
convertValueRange(float srcMin,float srcMax,int & dstMin,int & dstMax)593*35238bceSAndroid Build Coastguard Worker inline void convertValueRange<float, int>(float srcMin, float srcMax, int &dstMin, int &dstMax)
594*35238bceSAndroid Build Coastguard Worker {
595*35238bceSAndroid Build Coastguard Worker     if (Scalar::min<float>() == srcMin)
596*35238bceSAndroid Build Coastguard Worker         dstMin = Scalar::min<int>().as<int>();
597*35238bceSAndroid Build Coastguard Worker     else
598*35238bceSAndroid Build Coastguard Worker         dstMin = (int)deFloatCeil(srcMin);
599*35238bceSAndroid Build Coastguard Worker 
600*35238bceSAndroid Build Coastguard Worker     if (Scalar::max<float>() == srcMax)
601*35238bceSAndroid Build Coastguard Worker         dstMax = Scalar::max<int>().as<int>();
602*35238bceSAndroid Build Coastguard Worker     else
603*35238bceSAndroid Build Coastguard Worker         dstMax = (int)deFloatFloor(srcMax);
604*35238bceSAndroid Build Coastguard Worker }
605*35238bceSAndroid Build Coastguard Worker 
606*35238bceSAndroid Build Coastguard Worker template <>
convertValueRange(float srcMin,float srcMax,bool & dstMin,bool & dstMax)607*35238bceSAndroid Build Coastguard Worker inline void convertValueRange<float, bool>(float srcMin, float srcMax, bool &dstMin, bool &dstMax)
608*35238bceSAndroid Build Coastguard Worker {
609*35238bceSAndroid Build Coastguard Worker     dstMin = srcMin > 0.0f;
610*35238bceSAndroid Build Coastguard Worker     dstMax = srcMax > 0.0f;
611*35238bceSAndroid Build Coastguard Worker }
612*35238bceSAndroid Build Coastguard Worker 
613*35238bceSAndroid Build Coastguard Worker // \todo [pyry] More special cases?
614*35238bceSAndroid Build Coastguard Worker 
615*35238bceSAndroid Build Coastguard Worker // Returns whether it is possible to convert some SrcType value range to given DstType valueRange
616*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
isConversionOk(DstType min,DstType max)617*35238bceSAndroid Build Coastguard Worker bool isConversionOk(DstType min, DstType max)
618*35238bceSAndroid Build Coastguard Worker {
619*35238bceSAndroid Build Coastguard Worker     SrcType sMin, sMax;
620*35238bceSAndroid Build Coastguard Worker     convertValueRange(min, max, sMin, sMax);
621*35238bceSAndroid Build Coastguard Worker     return sMin <= sMax && de::inRange(convert<SrcType, DstType>(sMin), min, max) &&
622*35238bceSAndroid Build Coastguard Worker            de::inRange(convert<SrcType, DstType>(sMax), min, max);
623*35238bceSAndroid Build Coastguard Worker }
624*35238bceSAndroid Build Coastguard Worker 
625*35238bceSAndroid Build Coastguard Worker // Work-around for non-deterministic float behavior
626*35238bceSAndroid Build Coastguard Worker template <>
isConversionOk(float,float)627*35238bceSAndroid Build Coastguard Worker bool isConversionOk<float, float>(float, float)
628*35238bceSAndroid Build Coastguard Worker {
629*35238bceSAndroid Build Coastguard Worker     return true;
630*35238bceSAndroid Build Coastguard Worker }
631*35238bceSAndroid Build Coastguard Worker 
632*35238bceSAndroid Build Coastguard Worker // \todo [2011-03-26 pyry] Provide this in ValueAccess?
633*35238bceSAndroid Build Coastguard Worker template <typename T>
634*35238bceSAndroid Build Coastguard Worker T getValueAccessValue(ConstValueAccess access);
635*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ConstValueAccess access)636*35238bceSAndroid Build Coastguard Worker inline float getValueAccessValue<float>(ConstValueAccess access)
637*35238bceSAndroid Build Coastguard Worker {
638*35238bceSAndroid Build Coastguard Worker     return access.asFloat();
639*35238bceSAndroid Build Coastguard Worker }
640*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ConstValueAccess access)641*35238bceSAndroid Build Coastguard Worker inline int getValueAccessValue<int>(ConstValueAccess access)
642*35238bceSAndroid Build Coastguard Worker {
643*35238bceSAndroid Build Coastguard Worker     return access.asInt();
644*35238bceSAndroid Build Coastguard Worker }
645*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ConstValueAccess access)646*35238bceSAndroid Build Coastguard Worker inline bool getValueAccessValue<bool>(ConstValueAccess access)
647*35238bceSAndroid Build Coastguard Worker {
648*35238bceSAndroid Build Coastguard Worker     return access.asBool();
649*35238bceSAndroid Build Coastguard Worker }
650*35238bceSAndroid Build Coastguard Worker 
651*35238bceSAndroid Build Coastguard Worker template <typename T>
652*35238bceSAndroid Build Coastguard Worker T &getValueAccessValue(ValueAccess access);
653*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ValueAccess access)654*35238bceSAndroid Build Coastguard Worker inline float &getValueAccessValue<float>(ValueAccess access)
655*35238bceSAndroid Build Coastguard Worker {
656*35238bceSAndroid Build Coastguard Worker     return access.asFloat();
657*35238bceSAndroid Build Coastguard Worker }
658*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ValueAccess access)659*35238bceSAndroid Build Coastguard Worker inline int &getValueAccessValue<int>(ValueAccess access)
660*35238bceSAndroid Build Coastguard Worker {
661*35238bceSAndroid Build Coastguard Worker     return access.asInt();
662*35238bceSAndroid Build Coastguard Worker }
663*35238bceSAndroid Build Coastguard Worker template <>
getValueAccessValue(ValueAccess access)664*35238bceSAndroid Build Coastguard Worker inline bool &getValueAccessValue<bool>(ValueAccess access)
665*35238bceSAndroid Build Coastguard Worker {
666*35238bceSAndroid Build Coastguard Worker     return access.asBool();
667*35238bceSAndroid Build Coastguard Worker }
668*35238bceSAndroid Build Coastguard Worker 
669*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
isConversionOk(ConstValueRangeAccess valueRange)670*35238bceSAndroid Build Coastguard Worker bool isConversionOk(ConstValueRangeAccess valueRange)
671*35238bceSAndroid Build Coastguard Worker {
672*35238bceSAndroid Build Coastguard Worker     return isConversionOk<SrcType>(getValueAccessValue<DstType>(valueRange.getMin()),
673*35238bceSAndroid Build Coastguard Worker                                    getValueAccessValue<DstType>(valueRange.getMax()));
674*35238bceSAndroid Build Coastguard Worker }
675*35238bceSAndroid Build Coastguard Worker 
676*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
convertValueRangeTempl(ConstValueRangeAccess src,ValueRangeAccess dst)677*35238bceSAndroid Build Coastguard Worker void convertValueRangeTempl(ConstValueRangeAccess src, ValueRangeAccess dst)
678*35238bceSAndroid Build Coastguard Worker {
679*35238bceSAndroid Build Coastguard Worker     DstType dMin, dMax;
680*35238bceSAndroid Build Coastguard Worker     convertValueRange(getValueAccessValue<SrcType>(src.getMin()), getValueAccessValue<SrcType>(src.getMax()), dMin,
681*35238bceSAndroid Build Coastguard Worker                       dMax);
682*35238bceSAndroid Build Coastguard Worker     getValueAccessValue<DstType>(dst.getMin()) = dMin;
683*35238bceSAndroid Build Coastguard Worker     getValueAccessValue<DstType>(dst.getMax()) = dMax;
684*35238bceSAndroid Build Coastguard Worker }
685*35238bceSAndroid Build Coastguard Worker 
686*35238bceSAndroid Build Coastguard Worker template <typename SrcType, typename DstType>
convertExecValueTempl(ExecConstValueAccess src,ExecValueAccess dst)687*35238bceSAndroid Build Coastguard Worker void convertExecValueTempl(ExecConstValueAccess src, ExecValueAccess dst)
688*35238bceSAndroid Build Coastguard Worker {
689*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
690*35238bceSAndroid Build Coastguard Worker         dst.as<DstType>(ndx) = convert<SrcType, DstType>(src.as<SrcType>(ndx));
691*35238bceSAndroid Build Coastguard Worker }
692*35238bceSAndroid Build Coastguard Worker 
693*35238bceSAndroid Build Coastguard Worker typedef bool (*IsConversionOkFunc)(ConstValueRangeAccess);
694*35238bceSAndroid Build Coastguard Worker typedef void (*ConvertValueRangeFunc)(ConstValueRangeAccess, ValueRangeAccess);
695*35238bceSAndroid Build Coastguard Worker typedef void (*ConvertExecValueFunc)(ExecConstValueAccess, ExecValueAccess);
696*35238bceSAndroid Build Coastguard Worker 
getBaseTypeConvNdx(VariableType::Type type)697*35238bceSAndroid Build Coastguard Worker inline int getBaseTypeConvNdx(VariableType::Type type)
698*35238bceSAndroid Build Coastguard Worker {
699*35238bceSAndroid Build Coastguard Worker     switch (type)
700*35238bceSAndroid Build Coastguard Worker     {
701*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
702*35238bceSAndroid Build Coastguard Worker         return 0;
703*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
704*35238bceSAndroid Build Coastguard Worker         return 1;
705*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
706*35238bceSAndroid Build Coastguard Worker         return 2;
707*35238bceSAndroid Build Coastguard Worker     default:
708*35238bceSAndroid Build Coastguard Worker         return -1;
709*35238bceSAndroid Build Coastguard Worker     }
710*35238bceSAndroid Build Coastguard Worker }
711*35238bceSAndroid Build Coastguard Worker 
isConversionOk(VariableType::Type srcType,VariableType::Type dstType,ConstValueRangeAccess valueRange)712*35238bceSAndroid Build Coastguard Worker bool isConversionOk(VariableType::Type srcType, VariableType::Type dstType, ConstValueRangeAccess valueRange)
713*35238bceSAndroid Build Coastguard Worker {
714*35238bceSAndroid Build Coastguard Worker     // [src][dst]
715*35238bceSAndroid Build Coastguard Worker     static const IsConversionOkFunc convTable[3][3] = {
716*35238bceSAndroid Build Coastguard Worker         {isConversionOk<float, float>, isConversionOk<float, int>, isConversionOk<float, bool>},
717*35238bceSAndroid Build Coastguard Worker         {isConversionOk<int, float>, isConversionOk<int, int>, isConversionOk<int, bool>},
718*35238bceSAndroid Build Coastguard Worker         {isConversionOk<bool, float>, isConversionOk<bool, int>, isConversionOk<bool, bool>}};
719*35238bceSAndroid Build Coastguard Worker     return convTable[getBaseTypeConvNdx(srcType)][getBaseTypeConvNdx(dstType)](valueRange);
720*35238bceSAndroid Build Coastguard Worker }
721*35238bceSAndroid Build Coastguard Worker 
convertValueRange(ConstValueRangeAccess src,ValueRangeAccess dst)722*35238bceSAndroid Build Coastguard Worker void convertValueRange(ConstValueRangeAccess src, ValueRangeAccess dst)
723*35238bceSAndroid Build Coastguard Worker {
724*35238bceSAndroid Build Coastguard Worker     // [src][dst]
725*35238bceSAndroid Build Coastguard Worker     static const ConvertValueRangeFunc convTable[3][3] = {
726*35238bceSAndroid Build Coastguard Worker         {convertValueRangeTempl<float, float>, convertValueRangeTempl<float, int>, convertValueRangeTempl<float, bool>},
727*35238bceSAndroid Build Coastguard Worker         {convertValueRangeTempl<int, float>, convertValueRangeTempl<int, int>, convertValueRangeTempl<int, bool>},
728*35238bceSAndroid Build Coastguard Worker         {convertValueRangeTempl<bool, float>, convertValueRangeTempl<bool, int>, convertValueRangeTempl<bool, bool>}};
729*35238bceSAndroid Build Coastguard Worker 
730*35238bceSAndroid Build Coastguard Worker     convTable[getBaseTypeConvNdx(src.getType().getBaseType())][getBaseTypeConvNdx(dst.getType().getBaseType())](src,
731*35238bceSAndroid Build Coastguard Worker                                                                                                                 dst);
732*35238bceSAndroid Build Coastguard Worker }
733*35238bceSAndroid Build Coastguard Worker 
convertExecValue(ExecConstValueAccess src,ExecValueAccess dst)734*35238bceSAndroid Build Coastguard Worker void convertExecValue(ExecConstValueAccess src, ExecValueAccess dst)
735*35238bceSAndroid Build Coastguard Worker {
736*35238bceSAndroid Build Coastguard Worker     // [src][dst]
737*35238bceSAndroid Build Coastguard Worker     static const ConvertExecValueFunc convTable[3][3] = {
738*35238bceSAndroid Build Coastguard Worker         {convertExecValueTempl<float, float>, convertExecValueTempl<float, int>, convertExecValueTempl<float, bool>},
739*35238bceSAndroid Build Coastguard Worker         {convertExecValueTempl<int, float>, convertExecValueTempl<int, int>, convertExecValueTempl<int, bool>},
740*35238bceSAndroid Build Coastguard Worker         {convertExecValueTempl<bool, float>, convertExecValueTempl<bool, int>, convertExecValueTempl<bool, bool>}};
741*35238bceSAndroid Build Coastguard Worker 
742*35238bceSAndroid Build Coastguard Worker     convTable[getBaseTypeConvNdx(src.getType().getBaseType())][getBaseTypeConvNdx(dst.getType().getBaseType())](src,
743*35238bceSAndroid Build Coastguard Worker                                                                                                                 dst);
744*35238bceSAndroid Build Coastguard Worker }
745*35238bceSAndroid Build Coastguard Worker 
746*35238bceSAndroid Build Coastguard Worker } // namespace
747*35238bceSAndroid Build Coastguard Worker 
ConstructorOp(GeneratorState & state,ConstValueRangeAccess valueRange)748*35238bceSAndroid Build Coastguard Worker ConstructorOp::ConstructorOp(GeneratorState &state, ConstValueRangeAccess valueRange) : m_valueRange(valueRange)
749*35238bceSAndroid Build Coastguard Worker {
750*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid())
751*35238bceSAndroid Build Coastguard Worker     {
752*35238bceSAndroid Build Coastguard Worker         // Use random range
753*35238bceSAndroid Build Coastguard Worker         const int maxScalars = 4; // We don't have to be able to assign this value to anywhere
754*35238bceSAndroid Build Coastguard Worker         m_valueRange         = ValueRange(computeRandomType(state, maxScalars));
755*35238bceSAndroid Build Coastguard Worker         computeRandomValueRange(state, m_valueRange.asAccess());
756*35238bceSAndroid Build Coastguard Worker     }
757*35238bceSAndroid Build Coastguard Worker 
758*35238bceSAndroid Build Coastguard Worker     // \todo [2011-03-26 pyry] Vector conversions
759*35238bceSAndroid Build Coastguard Worker     // int remainingDepth = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
760*35238bceSAndroid Build Coastguard Worker 
761*35238bceSAndroid Build Coastguard Worker     const VariableType &type    = m_valueRange.getType();
762*35238bceSAndroid Build Coastguard Worker     VariableType::Type baseType = type.getBaseType();
763*35238bceSAndroid Build Coastguard Worker     int numScalars              = type.getNumElements();
764*35238bceSAndroid Build Coastguard Worker     int curScalarNdx            = 0;
765*35238bceSAndroid Build Coastguard Worker 
766*35238bceSAndroid Build Coastguard Worker     // \todo [2011-03-26 pyry] Separate op for struct constructors!
767*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(type.isFloatOrVec() || type.isIntOrVec() || type.isBoolOrVec());
768*35238bceSAndroid Build Coastguard Worker 
769*35238bceSAndroid Build Coastguard Worker     bool scalarConversions = state.getProgramParameters().useScalarConversions;
770*35238bceSAndroid Build Coastguard Worker 
771*35238bceSAndroid Build Coastguard Worker     while (curScalarNdx < numScalars)
772*35238bceSAndroid Build Coastguard Worker     {
773*35238bceSAndroid Build Coastguard Worker         ConstValueRangeAccess comp = m_valueRange.asAccess().component(curScalarNdx);
774*35238bceSAndroid Build Coastguard Worker 
775*35238bceSAndroid Build Coastguard Worker         if (scalarConversions)
776*35238bceSAndroid Build Coastguard Worker         {
777*35238bceSAndroid Build Coastguard Worker             int numInTypes = 0;
778*35238bceSAndroid Build Coastguard Worker             VariableType::Type inTypes[3];
779*35238bceSAndroid Build Coastguard Worker 
780*35238bceSAndroid Build Coastguard Worker             if (isConversionOk(VariableType::TYPE_FLOAT, baseType, comp))
781*35238bceSAndroid Build Coastguard Worker                 inTypes[numInTypes++] = VariableType::TYPE_FLOAT;
782*35238bceSAndroid Build Coastguard Worker             if (isConversionOk(VariableType::TYPE_INT, baseType, comp))
783*35238bceSAndroid Build Coastguard Worker                 inTypes[numInTypes++] = VariableType::TYPE_INT;
784*35238bceSAndroid Build Coastguard Worker             if (isConversionOk(VariableType::TYPE_BOOL, baseType, comp))
785*35238bceSAndroid Build Coastguard Worker                 inTypes[numInTypes++] = VariableType::TYPE_BOOL;
786*35238bceSAndroid Build Coastguard Worker 
787*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(numInTypes > 0); // At least nop conversion should be ok
788*35238bceSAndroid Build Coastguard Worker 
789*35238bceSAndroid Build Coastguard Worker             // Choose random
790*35238bceSAndroid Build Coastguard Worker             VariableType::Type inType =
791*35238bceSAndroid Build Coastguard Worker                 state.getRandom().choose<VariableType::Type>(&inTypes[0], &inTypes[0] + numInTypes);
792*35238bceSAndroid Build Coastguard Worker 
793*35238bceSAndroid Build Coastguard Worker             // Compute converted value range
794*35238bceSAndroid Build Coastguard Worker             ValueRange inValueRange(VariableType(inType, 1));
795*35238bceSAndroid Build Coastguard Worker             convertValueRange(comp, inValueRange);
796*35238bceSAndroid Build Coastguard Worker             m_inputValueRanges.push_back(inValueRange);
797*35238bceSAndroid Build Coastguard Worker 
798*35238bceSAndroid Build Coastguard Worker             curScalarNdx += 1;
799*35238bceSAndroid Build Coastguard Worker         }
800*35238bceSAndroid Build Coastguard Worker         else
801*35238bceSAndroid Build Coastguard Worker         {
802*35238bceSAndroid Build Coastguard Worker             m_inputValueRanges.push_back(ValueRange(comp));
803*35238bceSAndroid Build Coastguard Worker             curScalarNdx += 1;
804*35238bceSAndroid Build Coastguard Worker         }
805*35238bceSAndroid Build Coastguard Worker     }
806*35238bceSAndroid Build Coastguard Worker }
807*35238bceSAndroid Build Coastguard Worker 
~ConstructorOp(void)808*35238bceSAndroid Build Coastguard Worker ConstructorOp::~ConstructorOp(void)
809*35238bceSAndroid Build Coastguard Worker {
810*35238bceSAndroid Build Coastguard Worker     for (vector<Expression *>::iterator i = m_inputExpressions.begin(); i != m_inputExpressions.end(); i++)
811*35238bceSAndroid Build Coastguard Worker         delete *i;
812*35238bceSAndroid Build Coastguard Worker }
813*35238bceSAndroid Build Coastguard Worker 
createNextChild(GeneratorState & state)814*35238bceSAndroid Build Coastguard Worker Expression *ConstructorOp::createNextChild(GeneratorState &state)
815*35238bceSAndroid Build Coastguard Worker {
816*35238bceSAndroid Build Coastguard Worker     int numChildren   = (int)m_inputExpressions.size();
817*35238bceSAndroid Build Coastguard Worker     Expression *child = DE_NULL;
818*35238bceSAndroid Build Coastguard Worker 
819*35238bceSAndroid Build Coastguard Worker     // \note Created in reverse order!
820*35238bceSAndroid Build Coastguard Worker     if (numChildren < (int)m_inputValueRanges.size())
821*35238bceSAndroid Build Coastguard Worker     {
822*35238bceSAndroid Build Coastguard Worker         const ValueRange &inValueRange = m_inputValueRanges[m_inputValueRanges.size() - 1 - numChildren];
823*35238bceSAndroid Build Coastguard Worker         child                          = Expression::createRandom(state, inValueRange);
824*35238bceSAndroid Build Coastguard Worker         try
825*35238bceSAndroid Build Coastguard Worker         {
826*35238bceSAndroid Build Coastguard Worker             m_inputExpressions.push_back(child);
827*35238bceSAndroid Build Coastguard Worker         }
828*35238bceSAndroid Build Coastguard Worker         catch (const std::exception &)
829*35238bceSAndroid Build Coastguard Worker         {
830*35238bceSAndroid Build Coastguard Worker             delete child;
831*35238bceSAndroid Build Coastguard Worker             throw;
832*35238bceSAndroid Build Coastguard Worker         }
833*35238bceSAndroid Build Coastguard Worker     }
834*35238bceSAndroid Build Coastguard Worker 
835*35238bceSAndroid Build Coastguard Worker     return child;
836*35238bceSAndroid Build Coastguard Worker }
837*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)838*35238bceSAndroid Build Coastguard Worker float ConstructorOp::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
839*35238bceSAndroid Build Coastguard Worker {
840*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid())
841*35238bceSAndroid Build Coastguard Worker         return unusedValueWeight;
842*35238bceSAndroid Build Coastguard Worker 
843*35238bceSAndroid Build Coastguard Worker     if (!valueRange.getType().isFloatOrVec() && !valueRange.getType().isIntOrVec() &&
844*35238bceSAndroid Build Coastguard Worker         !valueRange.getType().isBoolOrVec())
845*35238bceSAndroid Build Coastguard Worker         return 0.0f;
846*35238bceSAndroid Build Coastguard Worker 
847*35238bceSAndroid Build Coastguard Worker     if (state.getExpressionDepth() + getTypeConstructorDepth(valueRange.getType()) >
848*35238bceSAndroid Build Coastguard Worker         state.getShaderParameters().maxExpressionDepth)
849*35238bceSAndroid Build Coastguard Worker         return 0.0f;
850*35238bceSAndroid Build Coastguard Worker 
851*35238bceSAndroid Build Coastguard Worker     return 1.0f;
852*35238bceSAndroid Build Coastguard Worker }
853*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const854*35238bceSAndroid Build Coastguard Worker void ConstructorOp::tokenize(GeneratorState &state, TokenStream &str) const
855*35238bceSAndroid Build Coastguard Worker {
856*35238bceSAndroid Build Coastguard Worker     const VariableType &type = m_valueRange.getType();
857*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(type.getPrecision() == VariableType::PRECISION_NONE);
858*35238bceSAndroid Build Coastguard Worker     type.tokenizeShortType(str);
859*35238bceSAndroid Build Coastguard Worker 
860*35238bceSAndroid Build Coastguard Worker     str << Token::LEFT_PAREN;
861*35238bceSAndroid Build Coastguard Worker 
862*35238bceSAndroid Build Coastguard Worker     for (vector<Expression *>::const_reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend();
863*35238bceSAndroid Build Coastguard Worker          i++)
864*35238bceSAndroid Build Coastguard Worker     {
865*35238bceSAndroid Build Coastguard Worker         if (i != m_inputExpressions.rbegin())
866*35238bceSAndroid Build Coastguard Worker             str << Token::COMMA;
867*35238bceSAndroid Build Coastguard Worker         (*i)->tokenize(state, str);
868*35238bceSAndroid Build Coastguard Worker     }
869*35238bceSAndroid Build Coastguard Worker 
870*35238bceSAndroid Build Coastguard Worker     str << Token::RIGHT_PAREN;
871*35238bceSAndroid Build Coastguard Worker }
872*35238bceSAndroid Build Coastguard Worker 
evaluate(ExecutionContext & evalCtx)873*35238bceSAndroid Build Coastguard Worker void ConstructorOp::evaluate(ExecutionContext &evalCtx)
874*35238bceSAndroid Build Coastguard Worker {
875*35238bceSAndroid Build Coastguard Worker     // Evaluate children
876*35238bceSAndroid Build Coastguard Worker     for (vector<Expression *>::reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend(); i++)
877*35238bceSAndroid Build Coastguard Worker         (*i)->evaluate(evalCtx);
878*35238bceSAndroid Build Coastguard Worker 
879*35238bceSAndroid Build Coastguard Worker     // Compute value
880*35238bceSAndroid Build Coastguard Worker     const VariableType &type = m_valueRange.getType();
881*35238bceSAndroid Build Coastguard Worker     m_value.setStorage(type);
882*35238bceSAndroid Build Coastguard Worker 
883*35238bceSAndroid Build Coastguard Worker     ExecValueAccess dst = m_value.getValue(type);
884*35238bceSAndroid Build Coastguard Worker     int curScalarNdx    = 0;
885*35238bceSAndroid Build Coastguard Worker 
886*35238bceSAndroid Build Coastguard Worker     for (vector<Expression *>::reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend(); i++)
887*35238bceSAndroid Build Coastguard Worker     {
888*35238bceSAndroid Build Coastguard Worker         ExecConstValueAccess src = (*i)->getValue();
889*35238bceSAndroid Build Coastguard Worker 
890*35238bceSAndroid Build Coastguard Worker         for (int elemNdx = 0; elemNdx < src.getType().getNumElements(); elemNdx++)
891*35238bceSAndroid Build Coastguard Worker             convertExecValue(src.component(elemNdx), dst.component(curScalarNdx++));
892*35238bceSAndroid Build Coastguard Worker     }
893*35238bceSAndroid Build Coastguard Worker }
894*35238bceSAndroid Build Coastguard Worker 
AssignOp(GeneratorState & state,ConstValueRangeAccess valueRange)895*35238bceSAndroid Build Coastguard Worker AssignOp::AssignOp(GeneratorState &state, ConstValueRangeAccess valueRange)
896*35238bceSAndroid Build Coastguard Worker     : m_valueRange(valueRange)
897*35238bceSAndroid Build Coastguard Worker     , m_lvalueExpr(DE_NULL)
898*35238bceSAndroid Build Coastguard Worker     , m_rvalueExpr(DE_NULL)
899*35238bceSAndroid Build Coastguard Worker {
900*35238bceSAndroid Build Coastguard Worker     if (m_valueRange.getType().isVoid())
901*35238bceSAndroid Build Coastguard Worker     {
902*35238bceSAndroid Build Coastguard Worker         // Compute random value range
903*35238bceSAndroid Build Coastguard Worker         int maxScalars = state.getShaderParameters().maxCombinedVariableScalars -
904*35238bceSAndroid Build Coastguard Worker                          state.getVariableManager().getNumAllocatedScalars();
905*35238bceSAndroid Build Coastguard Worker         bool useRandomRange = !state.getVariableManager().hasEntry<IsWritableEntry>() ||
906*35238bceSAndroid Build Coastguard Worker                               ((maxScalars > 0) && getWeightedBool(state.getRandom(), 0.1f));
907*35238bceSAndroid Build Coastguard Worker 
908*35238bceSAndroid Build Coastguard Worker         if (useRandomRange)
909*35238bceSAndroid Build Coastguard Worker         {
910*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(maxScalars > 0);
911*35238bceSAndroid Build Coastguard Worker             m_valueRange = ValueRange(computeRandomType(state, maxScalars));
912*35238bceSAndroid Build Coastguard Worker             computeRandomValueRange(state, m_valueRange.asAccess());
913*35238bceSAndroid Build Coastguard Worker         }
914*35238bceSAndroid Build Coastguard Worker         else
915*35238bceSAndroid Build Coastguard Worker         {
916*35238bceSAndroid Build Coastguard Worker             // Use value range from random entry
917*35238bceSAndroid Build Coastguard Worker             // \todo [2011-02-28 pyry] Give lower weight to entries without range? Choose subtype range?
918*35238bceSAndroid Build Coastguard Worker             const ValueEntry *entry =
919*35238bceSAndroid Build Coastguard Worker                 state.getRandom().choose<const ValueEntry *>(state.getVariableManager().getBegin<IsWritableEntry>(),
920*35238bceSAndroid Build Coastguard Worker                                                              state.getVariableManager().getEnd<IsWritableEntry>());
921*35238bceSAndroid Build Coastguard Worker             m_valueRange = ValueRange(entry->getValueRange());
922*35238bceSAndroid Build Coastguard Worker 
923*35238bceSAndroid Build Coastguard Worker             computeRandomValueRangeForInfElements(state, m_valueRange.asAccess());
924*35238bceSAndroid Build Coastguard Worker 
925*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(state.getVariableManager().hasEntry(IsWritableIntersectingEntry(m_valueRange.asAccess())));
926*35238bceSAndroid Build Coastguard Worker         }
927*35238bceSAndroid Build Coastguard Worker     }
928*35238bceSAndroid Build Coastguard Worker 
929*35238bceSAndroid Build Coastguard Worker     IsWritableIntersectingEntry::Iterator first =
930*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().getBegin(IsWritableIntersectingEntry(m_valueRange.asAccess()));
931*35238bceSAndroid Build Coastguard Worker     IsWritableIntersectingEntry::Iterator end =
932*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().getEnd(IsWritableIntersectingEntry(m_valueRange.asAccess()));
933*35238bceSAndroid Build Coastguard Worker 
934*35238bceSAndroid Build Coastguard Worker     bool possiblyCreateVar = canAllocateVariable(state, m_valueRange.getType()) &&
935*35238bceSAndroid Build Coastguard Worker                              (first == end || getWeightedBool(state.getRandom(), 0.5f));
936*35238bceSAndroid Build Coastguard Worker 
937*35238bceSAndroid Build Coastguard Worker     if (!possiblyCreateVar)
938*35238bceSAndroid Build Coastguard Worker     {
939*35238bceSAndroid Build Coastguard Worker         // Find all possible valueranges matching given type and intersecting with valuerange
940*35238bceSAndroid Build Coastguard Worker         // \todo [pyry] Actually collect all ValueRanges, currently operates only on whole variables
941*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(first != end);
942*35238bceSAndroid Build Coastguard Worker 
943*35238bceSAndroid Build Coastguard Worker         // Try to select one closest to given range but bigger (eg. superset)
944*35238bceSAndroid Build Coastguard Worker         bool supersetExists = false;
945*35238bceSAndroid Build Coastguard Worker         for (IsWritableIntersectingEntry::Iterator i = first; i != end; i++)
946*35238bceSAndroid Build Coastguard Worker         {
947*35238bceSAndroid Build Coastguard Worker             if ((*i)->getValueRange().isSupersetOf(m_valueRange.asAccess()))
948*35238bceSAndroid Build Coastguard Worker             {
949*35238bceSAndroid Build Coastguard Worker                 supersetExists = true;
950*35238bceSAndroid Build Coastguard Worker                 break;
951*35238bceSAndroid Build Coastguard Worker             }
952*35238bceSAndroid Build Coastguard Worker         }
953*35238bceSAndroid Build Coastguard Worker 
954*35238bceSAndroid Build Coastguard Worker         if (!supersetExists)
955*35238bceSAndroid Build Coastguard Worker         {
956*35238bceSAndroid Build Coastguard Worker             // Select some other range and compute intersection
957*35238bceSAndroid Build Coastguard Worker             // \todo [2011-02-03 pyry] Use some heuristics to select the range?
958*35238bceSAndroid Build Coastguard Worker             ConstValueRangeAccess selectedRange =
959*35238bceSAndroid Build Coastguard Worker                 state.getRandom().choose<const ValueEntry *>(first, end)->getValueRange();
960*35238bceSAndroid Build Coastguard Worker 
961*35238bceSAndroid Build Coastguard Worker             ValueRange::computeIntersection(m_valueRange.asAccess(), m_valueRange.asAccess(), selectedRange);
962*35238bceSAndroid Build Coastguard Worker         }
963*35238bceSAndroid Build Coastguard Worker     }
964*35238bceSAndroid Build Coastguard Worker }
965*35238bceSAndroid Build Coastguard Worker 
~AssignOp(void)966*35238bceSAndroid Build Coastguard Worker AssignOp::~AssignOp(void)
967*35238bceSAndroid Build Coastguard Worker {
968*35238bceSAndroid Build Coastguard Worker     delete m_lvalueExpr;
969*35238bceSAndroid Build Coastguard Worker     delete m_rvalueExpr;
970*35238bceSAndroid Build Coastguard Worker }
971*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)972*35238bceSAndroid Build Coastguard Worker float AssignOp::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
973*35238bceSAndroid Build Coastguard Worker {
974*35238bceSAndroid Build Coastguard Worker     if (!valueRange.getType().isVoid() && !canAllocateVariable(state, valueRange.getType()) &&
975*35238bceSAndroid Build Coastguard Worker         !state.getVariableManager().hasEntry(IsWritableIntersectingEntry(valueRange)))
976*35238bceSAndroid Build Coastguard Worker         return 0.0f; // Would require creating a new variable
977*35238bceSAndroid Build Coastguard Worker 
978*35238bceSAndroid Build Coastguard Worker     if (!valueRange.getType().isVoid() &&
979*35238bceSAndroid Build Coastguard Worker         state.getExpressionDepth() + getTypeConstructorDepth(valueRange.getType()) + 1 >=
980*35238bceSAndroid Build Coastguard Worker             state.getShaderParameters().maxExpressionDepth)
981*35238bceSAndroid Build Coastguard Worker         return 0.0f;
982*35238bceSAndroid Build Coastguard Worker 
983*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid() && !state.getVariableManager().hasEntry<IsWritableEntry>() &&
984*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().getNumAllocatedScalars() >= state.getShaderParameters().maxCombinedVariableScalars)
985*35238bceSAndroid Build Coastguard Worker         return 0.0f; // Can not allocate a new entry
986*35238bceSAndroid Build Coastguard Worker 
987*35238bceSAndroid Build Coastguard Worker     if (state.getExpressionDepth() == 0)
988*35238bceSAndroid Build Coastguard Worker         return 4.0f;
989*35238bceSAndroid Build Coastguard Worker     else
990*35238bceSAndroid Build Coastguard Worker         return 0.0f; // \todo [pyry] Fix assign ops
991*35238bceSAndroid Build Coastguard Worker }
992*35238bceSAndroid Build Coastguard Worker 
createNextChild(GeneratorState & state)993*35238bceSAndroid Build Coastguard Worker Expression *AssignOp::createNextChild(GeneratorState &state)
994*35238bceSAndroid Build Coastguard Worker {
995*35238bceSAndroid Build Coastguard Worker     if (m_lvalueExpr == DE_NULL)
996*35238bceSAndroid Build Coastguard Worker     {
997*35238bceSAndroid Build Coastguard Worker         // Construct lvalue
998*35238bceSAndroid Build Coastguard Worker         // \todo [2011-03-14 pyry] Proper l-value generation:
999*35238bceSAndroid Build Coastguard Worker         //  - pure L-value part is generated first
1000*35238bceSAndroid Build Coastguard Worker         //  - variable valuerange is made unbound
1001*35238bceSAndroid Build Coastguard Worker         //  - R-value is generated
1002*35238bceSAndroid Build Coastguard Worker         //  - R-values in L-value are generated
1003*35238bceSAndroid Build Coastguard Worker         m_lvalueExpr = Expression::createRandomLValue(state, m_valueRange.asAccess());
1004*35238bceSAndroid Build Coastguard Worker         return m_lvalueExpr;
1005*35238bceSAndroid Build Coastguard Worker     }
1006*35238bceSAndroid Build Coastguard Worker     else if (m_rvalueExpr == DE_NULL)
1007*35238bceSAndroid Build Coastguard Worker     {
1008*35238bceSAndroid Build Coastguard Worker         // Construct value expr
1009*35238bceSAndroid Build Coastguard Worker         m_rvalueExpr = Expression::createRandom(state, m_valueRange.asAccess());
1010*35238bceSAndroid Build Coastguard Worker         return m_rvalueExpr;
1011*35238bceSAndroid Build Coastguard Worker     }
1012*35238bceSAndroid Build Coastguard Worker     else
1013*35238bceSAndroid Build Coastguard Worker         return DE_NULL;
1014*35238bceSAndroid Build Coastguard Worker }
1015*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const1016*35238bceSAndroid Build Coastguard Worker void AssignOp::tokenize(GeneratorState &state, TokenStream &str) const
1017*35238bceSAndroid Build Coastguard Worker {
1018*35238bceSAndroid Build Coastguard Worker     m_lvalueExpr->tokenize(state, str);
1019*35238bceSAndroid Build Coastguard Worker     str << Token::EQUAL;
1020*35238bceSAndroid Build Coastguard Worker     m_rvalueExpr->tokenize(state, str);
1021*35238bceSAndroid Build Coastguard Worker }
1022*35238bceSAndroid Build Coastguard Worker 
evaluate(ExecutionContext & evalCtx)1023*35238bceSAndroid Build Coastguard Worker void AssignOp::evaluate(ExecutionContext &evalCtx)
1024*35238bceSAndroid Build Coastguard Worker {
1025*35238bceSAndroid Build Coastguard Worker     // Evaluate l-value
1026*35238bceSAndroid Build Coastguard Worker     m_lvalueExpr->evaluate(evalCtx);
1027*35238bceSAndroid Build Coastguard Worker 
1028*35238bceSAndroid Build Coastguard Worker     // Evaluate value
1029*35238bceSAndroid Build Coastguard Worker     m_rvalueExpr->evaluate(evalCtx);
1030*35238bceSAndroid Build Coastguard Worker     m_value.setStorage(m_valueRange.getType());
1031*35238bceSAndroid Build Coastguard Worker     m_value.getValue(m_valueRange.getType()) = m_rvalueExpr->getValue().value();
1032*35238bceSAndroid Build Coastguard Worker 
1033*35238bceSAndroid Build Coastguard Worker     // Assign
1034*35238bceSAndroid Build Coastguard Worker     assignMasked(m_lvalueExpr->getLValue(), m_value.getValue(m_valueRange.getType()), evalCtx.getExecutionMask());
1035*35238bceSAndroid Build Coastguard Worker }
1036*35238bceSAndroid Build Coastguard Worker 
1037*35238bceSAndroid Build Coastguard Worker namespace
1038*35238bceSAndroid Build Coastguard Worker {
1039*35238bceSAndroid Build Coastguard Worker 
isShaderInOutSupportedType(const VariableType & type)1040*35238bceSAndroid Build Coastguard Worker inline bool isShaderInOutSupportedType(const VariableType &type)
1041*35238bceSAndroid Build Coastguard Worker {
1042*35238bceSAndroid Build Coastguard Worker     // \todo [2011-03-11 pyry] Float arrays, structs?
1043*35238bceSAndroid Build Coastguard Worker     return type.getBaseType() == VariableType::TYPE_FLOAT;
1044*35238bceSAndroid Build Coastguard Worker }
1045*35238bceSAndroid Build Coastguard Worker 
allocateNewVariable(GeneratorState & state,ConstValueRangeAccess valueRange)1046*35238bceSAndroid Build Coastguard Worker Variable *allocateNewVariable(GeneratorState &state, ConstValueRangeAccess valueRange)
1047*35238bceSAndroid Build Coastguard Worker {
1048*35238bceSAndroid Build Coastguard Worker     Variable *variable = state.getVariableManager().allocate(valueRange.getType());
1049*35238bceSAndroid Build Coastguard Worker 
1050*35238bceSAndroid Build Coastguard Worker     // Update value range
1051*35238bceSAndroid Build Coastguard Worker     state.getVariableManager().setValue(variable, valueRange);
1052*35238bceSAndroid Build Coastguard Worker 
1053*35238bceSAndroid Build Coastguard Worker     // Random storage \todo [pyry] Check that scalar count in uniform/input classes is not exceeded
1054*35238bceSAndroid Build Coastguard Worker     static const Variable::Storage storages[] = {Variable::STORAGE_CONST, Variable::STORAGE_UNIFORM,
1055*35238bceSAndroid Build Coastguard Worker                                                  Variable::STORAGE_LOCAL, Variable::STORAGE_SHADER_IN};
1056*35238bceSAndroid Build Coastguard Worker     float weights[DE_LENGTH_OF_ARRAY(storages)];
1057*35238bceSAndroid Build Coastguard Worker 
1058*35238bceSAndroid Build Coastguard Worker     // Dynamic vs. constant weight.
1059*35238bceSAndroid Build Coastguard Worker     float dynWeight = computeDynamicRangeWeight(valueRange);
1060*35238bceSAndroid Build Coastguard Worker     int numScalars  = valueRange.getType().getScalarSize();
1061*35238bceSAndroid Build Coastguard Worker     bool uniformOk  = state.getVariableManager().getNumAllocatedUniformScalars() + numScalars <=
1062*35238bceSAndroid Build Coastguard Worker                      state.getShaderParameters().maxUniformScalars;
1063*35238bceSAndroid Build Coastguard Worker     bool shaderInOk = isShaderInOutSupportedType(valueRange.getType()) &&
1064*35238bceSAndroid Build Coastguard Worker                       (state.getVariableManager().getNumAllocatedShaderInVariables() + NUM_RESERVED_SHADER_INPUTS <
1065*35238bceSAndroid Build Coastguard Worker                        state.getShaderParameters().maxInputVariables);
1066*35238bceSAndroid Build Coastguard Worker 
1067*35238bceSAndroid Build Coastguard Worker     weights[0] = de::max(1.0f - dynWeight, 0.1f);
1068*35238bceSAndroid Build Coastguard Worker     weights[1] = uniformOk ? dynWeight * 0.5f : 0.0f;
1069*35238bceSAndroid Build Coastguard Worker     weights[2] = dynWeight;
1070*35238bceSAndroid Build Coastguard Worker     weights[3] = shaderInOk ? dynWeight * 2.0f : 0.0f;
1071*35238bceSAndroid Build Coastguard Worker 
1072*35238bceSAndroid Build Coastguard Worker     state.getVariableManager().setStorage(variable,
1073*35238bceSAndroid Build Coastguard Worker                                           state.getRandom().chooseWeighted<Variable::Storage>(
1074*35238bceSAndroid Build Coastguard Worker                                               &storages[0], &storages[DE_LENGTH_OF_ARRAY(storages)], &weights[0]));
1075*35238bceSAndroid Build Coastguard Worker 
1076*35238bceSAndroid Build Coastguard Worker     return variable;
1077*35238bceSAndroid Build Coastguard Worker }
1078*35238bceSAndroid Build Coastguard Worker 
combineWeight(float curCombinedWeight,float partialWeight)1079*35238bceSAndroid Build Coastguard Worker inline float combineWeight(float curCombinedWeight, float partialWeight)
1080*35238bceSAndroid Build Coastguard Worker {
1081*35238bceSAndroid Build Coastguard Worker     return curCombinedWeight * partialWeight;
1082*35238bceSAndroid Build Coastguard Worker }
1083*35238bceSAndroid Build Coastguard Worker 
computeEntryReadWeight(ConstValueRangeAccess entryValueRange,ConstValueRangeAccess readValueRange)1084*35238bceSAndroid Build Coastguard Worker float computeEntryReadWeight(ConstValueRangeAccess entryValueRange, ConstValueRangeAccess readValueRange)
1085*35238bceSAndroid Build Coastguard Worker {
1086*35238bceSAndroid Build Coastguard Worker     const VariableType &type = entryValueRange.getType();
1087*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(type == readValueRange.getType());
1088*35238bceSAndroid Build Coastguard Worker 
1089*35238bceSAndroid Build Coastguard Worker     float weight = 1.0f;
1090*35238bceSAndroid Build Coastguard Worker 
1091*35238bceSAndroid Build Coastguard Worker     switch (type.getBaseType())
1092*35238bceSAndroid Build Coastguard Worker     {
1093*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_FLOAT:
1094*35238bceSAndroid Build Coastguard Worker     {
1095*35238bceSAndroid Build Coastguard Worker         for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
1096*35238bceSAndroid Build Coastguard Worker         {
1097*35238bceSAndroid Build Coastguard Worker             float entryMin = entryValueRange.component(elemNdx).getMin().asFloat();
1098*35238bceSAndroid Build Coastguard Worker             float entryMax = entryValueRange.component(elemNdx).getMax().asFloat();
1099*35238bceSAndroid Build Coastguard Worker             float readMin  = readValueRange.component(elemNdx).getMin().asFloat();
1100*35238bceSAndroid Build Coastguard Worker             float readMax  = readValueRange.component(elemNdx).getMax().asFloat();
1101*35238bceSAndroid Build Coastguard Worker 
1102*35238bceSAndroid Build Coastguard Worker             // Check for -inf..inf ranges - they don't bring down the weight.
1103*35238bceSAndroid Build Coastguard Worker             if (Scalar::min<float>() == entryMin && Scalar::max<float>() == entryMax)
1104*35238bceSAndroid Build Coastguard Worker                 continue;
1105*35238bceSAndroid Build Coastguard Worker 
1106*35238bceSAndroid Build Coastguard Worker             // Intersection to entry value range length ratio.
1107*35238bceSAndroid Build Coastguard Worker             float intersectionMin = deFloatMax(entryMin, readMin);
1108*35238bceSAndroid Build Coastguard Worker             float intersectionMax = deFloatMin(entryMax, readMax);
1109*35238bceSAndroid Build Coastguard Worker             float entryRangeLen   = entryMax - entryMin;
1110*35238bceSAndroid Build Coastguard Worker             float readRangeLen    = readMax - readMin;
1111*35238bceSAndroid Build Coastguard Worker             float intersectionLen = intersectionMax - intersectionMin;
1112*35238bceSAndroid Build Coastguard Worker             float entryRatio      = (entryRangeLen > 0.0f) ? (intersectionLen / entryRangeLen) : 1.0f;
1113*35238bceSAndroid Build Coastguard Worker             float readRatio       = (readRangeLen > 0.0f) ? (intersectionLen / readRangeLen) : 1.0f;
1114*35238bceSAndroid Build Coastguard Worker             float elementWeight   = 0.5f * readRatio + 0.5f * entryRatio;
1115*35238bceSAndroid Build Coastguard Worker 
1116*35238bceSAndroid Build Coastguard Worker             weight = combineWeight(weight, elementWeight);
1117*35238bceSAndroid Build Coastguard Worker         }
1118*35238bceSAndroid Build Coastguard Worker         break;
1119*35238bceSAndroid Build Coastguard Worker     }
1120*35238bceSAndroid Build Coastguard Worker 
1121*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_INT:
1122*35238bceSAndroid Build Coastguard Worker     {
1123*35238bceSAndroid Build Coastguard Worker         for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
1124*35238bceSAndroid Build Coastguard Worker         {
1125*35238bceSAndroid Build Coastguard Worker             int entryMin = entryValueRange.component(elemNdx).getMin().asInt();
1126*35238bceSAndroid Build Coastguard Worker             int entryMax = entryValueRange.component(elemNdx).getMax().asInt();
1127*35238bceSAndroid Build Coastguard Worker             int readMin  = readValueRange.component(elemNdx).getMin().asInt();
1128*35238bceSAndroid Build Coastguard Worker             int readMax  = readValueRange.component(elemNdx).getMax().asInt();
1129*35238bceSAndroid Build Coastguard Worker 
1130*35238bceSAndroid Build Coastguard Worker             // Check for -inf..inf ranges - they don't bring down the weight.
1131*35238bceSAndroid Build Coastguard Worker             if (Scalar::min<int>() == entryMin && Scalar::max<int>() == entryMax)
1132*35238bceSAndroid Build Coastguard Worker                 continue;
1133*35238bceSAndroid Build Coastguard Worker 
1134*35238bceSAndroid Build Coastguard Worker             // Intersection to entry value range length ratio.
1135*35238bceSAndroid Build Coastguard Worker             int intersectionMin     = deMax32(entryMin, readMin);
1136*35238bceSAndroid Build Coastguard Worker             int intersectionMax     = deMin32(entryMax, readMax);
1137*35238bceSAndroid Build Coastguard Worker             int64_t entryRangeLen   = (int64_t)entryMax - (int64_t)entryMin;
1138*35238bceSAndroid Build Coastguard Worker             int64_t readRangeLen    = (int64_t)readMax - (int64_t)readMin;
1139*35238bceSAndroid Build Coastguard Worker             int64_t intersectionLen = (int64_t)intersectionMax - (int64_t)intersectionMin;
1140*35238bceSAndroid Build Coastguard Worker             float entryRatio        = (entryRangeLen > 0) ? ((float)intersectionLen / (float)entryRangeLen) : 1.0f;
1141*35238bceSAndroid Build Coastguard Worker             float readRatio         = (readRangeLen > 0) ? ((float)intersectionLen / (float)readRangeLen) : 1.0f;
1142*35238bceSAndroid Build Coastguard Worker             float elementWeight     = 0.5f * readRatio + 0.5f * entryRatio;
1143*35238bceSAndroid Build Coastguard Worker 
1144*35238bceSAndroid Build Coastguard Worker             weight = combineWeight(weight, elementWeight);
1145*35238bceSAndroid Build Coastguard Worker         }
1146*35238bceSAndroid Build Coastguard Worker         break;
1147*35238bceSAndroid Build Coastguard Worker     }
1148*35238bceSAndroid Build Coastguard Worker 
1149*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_BOOL:
1150*35238bceSAndroid Build Coastguard Worker     {
1151*35238bceSAndroid Build Coastguard Worker         // \todo
1152*35238bceSAndroid Build Coastguard Worker         break;
1153*35238bceSAndroid Build Coastguard Worker     }
1154*35238bceSAndroid Build Coastguard Worker 
1155*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_ARRAY:
1156*35238bceSAndroid Build Coastguard Worker     case VariableType::TYPE_STRUCT:
1157*35238bceSAndroid Build Coastguard Worker 
1158*35238bceSAndroid Build Coastguard Worker     default:
1159*35238bceSAndroid Build Coastguard Worker         TCU_FAIL("Unsupported type");
1160*35238bceSAndroid Build Coastguard Worker     }
1161*35238bceSAndroid Build Coastguard Worker 
1162*35238bceSAndroid Build Coastguard Worker     return deFloatMax(weight, 0.01f);
1163*35238bceSAndroid Build Coastguard Worker }
1164*35238bceSAndroid Build Coastguard Worker 
1165*35238bceSAndroid Build Coastguard Worker } // namespace
1166*35238bceSAndroid Build Coastguard Worker 
VariableRead(GeneratorState & state,ConstValueRangeAccess valueRange)1167*35238bceSAndroid Build Coastguard Worker VariableRead::VariableRead(GeneratorState &state, ConstValueRangeAccess valueRange)
1168*35238bceSAndroid Build Coastguard Worker {
1169*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid())
1170*35238bceSAndroid Build Coastguard Worker     {
1171*35238bceSAndroid Build Coastguard Worker         IsReadableEntry filter = IsReadableEntry(state.getExpressionFlags());
1172*35238bceSAndroid Build Coastguard Worker         int maxScalars         = state.getShaderParameters().maxCombinedVariableScalars -
1173*35238bceSAndroid Build Coastguard Worker                          state.getVariableManager().getNumAllocatedScalars();
1174*35238bceSAndroid Build Coastguard Worker         bool useRandomRange = !state.getVariableManager().hasEntry(filter) ||
1175*35238bceSAndroid Build Coastguard Worker                               ((maxScalars > 0) && getWeightedBool(state.getRandom(), 0.5f));
1176*35238bceSAndroid Build Coastguard Worker 
1177*35238bceSAndroid Build Coastguard Worker         if (useRandomRange)
1178*35238bceSAndroid Build Coastguard Worker         {
1179*35238bceSAndroid Build Coastguard Worker             // Allocate a new variable
1180*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(maxScalars > 0);
1181*35238bceSAndroid Build Coastguard Worker             ValueRange newVarRange(computeRandomType(state, maxScalars));
1182*35238bceSAndroid Build Coastguard Worker             computeRandomValueRange(state, newVarRange.asAccess());
1183*35238bceSAndroid Build Coastguard Worker 
1184*35238bceSAndroid Build Coastguard Worker             m_variable = allocateNewVariable(state, newVarRange.asAccess());
1185*35238bceSAndroid Build Coastguard Worker         }
1186*35238bceSAndroid Build Coastguard Worker         else
1187*35238bceSAndroid Build Coastguard Worker         {
1188*35238bceSAndroid Build Coastguard Worker             // Use random entry \todo [pyry] Handle -inf..inf ranges?
1189*35238bceSAndroid Build Coastguard Worker             m_variable = state.getRandom()
1190*35238bceSAndroid Build Coastguard Worker                              .choose<const ValueEntry *>(state.getVariableManager().getBegin(filter),
1191*35238bceSAndroid Build Coastguard Worker                                                          state.getVariableManager().getEnd(filter))
1192*35238bceSAndroid Build Coastguard Worker                              ->getVariable();
1193*35238bceSAndroid Build Coastguard Worker         }
1194*35238bceSAndroid Build Coastguard Worker     }
1195*35238bceSAndroid Build Coastguard Worker     else
1196*35238bceSAndroid Build Coastguard Worker     {
1197*35238bceSAndroid Build Coastguard Worker         // Find variable that has value range that intersects with given range
1198*35238bceSAndroid Build Coastguard Worker         IsReadableIntersectingEntry::Iterator first =
1199*35238bceSAndroid Build Coastguard Worker             state.getVariableManager().getBegin(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags()));
1200*35238bceSAndroid Build Coastguard Worker         IsReadableIntersectingEntry::Iterator end =
1201*35238bceSAndroid Build Coastguard Worker             state.getVariableManager().getEnd(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags()));
1202*35238bceSAndroid Build Coastguard Worker 
1203*35238bceSAndroid Build Coastguard Worker         const float createOnReadWeight = 0.5f;
1204*35238bceSAndroid Build Coastguard Worker         bool createVar                 = canAllocateVariable(state, valueRange.getType()) &&
1205*35238bceSAndroid Build Coastguard Worker                          (first == end || getWeightedBool(state.getRandom(), createOnReadWeight));
1206*35238bceSAndroid Build Coastguard Worker 
1207*35238bceSAndroid Build Coastguard Worker         if (createVar)
1208*35238bceSAndroid Build Coastguard Worker         {
1209*35238bceSAndroid Build Coastguard Worker             m_variable = allocateNewVariable(state, valueRange);
1210*35238bceSAndroid Build Coastguard Worker         }
1211*35238bceSAndroid Build Coastguard Worker         else
1212*35238bceSAndroid Build Coastguard Worker         {
1213*35238bceSAndroid Build Coastguard Worker             // Copy value entries for computing weights.
1214*35238bceSAndroid Build Coastguard Worker             std::vector<const ValueEntry *> availableVars;
1215*35238bceSAndroid Build Coastguard Worker             std::vector<float> weights;
1216*35238bceSAndroid Build Coastguard Worker 
1217*35238bceSAndroid Build Coastguard Worker             std::copy(first, end, std::inserter(availableVars, availableVars.begin()));
1218*35238bceSAndroid Build Coastguard Worker 
1219*35238bceSAndroid Build Coastguard Worker             // Compute weights.
1220*35238bceSAndroid Build Coastguard Worker             weights.resize(availableVars.size());
1221*35238bceSAndroid Build Coastguard Worker             for (int ndx = 0; ndx < (int)availableVars.size(); ndx++)
1222*35238bceSAndroid Build Coastguard Worker                 weights[ndx] = computeEntryReadWeight(availableVars[ndx]->getValueRange(), valueRange);
1223*35238bceSAndroid Build Coastguard Worker 
1224*35238bceSAndroid Build Coastguard Worker             // Select.
1225*35238bceSAndroid Build Coastguard Worker             const ValueEntry *entry = state.getRandom().chooseWeighted<const ValueEntry *>(
1226*35238bceSAndroid Build Coastguard Worker                 availableVars.begin(), availableVars.end(), weights.begin());
1227*35238bceSAndroid Build Coastguard Worker             m_variable = entry->getVariable();
1228*35238bceSAndroid Build Coastguard Worker 
1229*35238bceSAndroid Build Coastguard Worker             // Compute intersection
1230*35238bceSAndroid Build Coastguard Worker             ValueRange intersection(m_variable->getType());
1231*35238bceSAndroid Build Coastguard Worker             ValueRange::computeIntersection(intersection, entry->getValueRange(), valueRange);
1232*35238bceSAndroid Build Coastguard Worker             state.getVariableManager().setValue(m_variable, intersection.asAccess());
1233*35238bceSAndroid Build Coastguard Worker         }
1234*35238bceSAndroid Build Coastguard Worker     }
1235*35238bceSAndroid Build Coastguard Worker }
1236*35238bceSAndroid Build Coastguard Worker 
VariableRead(const Variable * variable)1237*35238bceSAndroid Build Coastguard Worker VariableRead::VariableRead(const Variable *variable)
1238*35238bceSAndroid Build Coastguard Worker {
1239*35238bceSAndroid Build Coastguard Worker     m_variable = variable;
1240*35238bceSAndroid Build Coastguard Worker }
1241*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)1242*35238bceSAndroid Build Coastguard Worker float VariableRead::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
1243*35238bceSAndroid Build Coastguard Worker {
1244*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid())
1245*35238bceSAndroid Build Coastguard Worker     {
1246*35238bceSAndroid Build Coastguard Worker         if (state.getVariableManager().hasEntry(IsReadableEntry(state.getExpressionFlags())) ||
1247*35238bceSAndroid Build Coastguard Worker             state.getVariableManager().getNumAllocatedScalars() <
1248*35238bceSAndroid Build Coastguard Worker                 state.getShaderParameters().maxCombinedVariableScalars)
1249*35238bceSAndroid Build Coastguard Worker             return unusedValueWeight;
1250*35238bceSAndroid Build Coastguard Worker         else
1251*35238bceSAndroid Build Coastguard Worker             return 0.0f;
1252*35238bceSAndroid Build Coastguard Worker     }
1253*35238bceSAndroid Build Coastguard Worker 
1254*35238bceSAndroid Build Coastguard Worker     if (!canAllocateVariable(state, valueRange.getType()) &&
1255*35238bceSAndroid Build Coastguard Worker         !state.getVariableManager().hasEntry(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags())))
1256*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1257*35238bceSAndroid Build Coastguard Worker     else
1258*35238bceSAndroid Build Coastguard Worker         return 1.0f;
1259*35238bceSAndroid Build Coastguard Worker }
1260*35238bceSAndroid Build Coastguard Worker 
VariableWrite(GeneratorState & state,ConstValueRangeAccess valueRange)1261*35238bceSAndroid Build Coastguard Worker VariableWrite::VariableWrite(GeneratorState &state, ConstValueRangeAccess valueRange)
1262*35238bceSAndroid Build Coastguard Worker {
1263*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(!valueRange.getType().isVoid());
1264*35238bceSAndroid Build Coastguard Worker 
1265*35238bceSAndroid Build Coastguard Worker     // Find variable with range that is superset of given range
1266*35238bceSAndroid Build Coastguard Worker     IsWritableSupersetEntry::Iterator first = state.getVariableManager().getBegin(IsWritableSupersetEntry(valueRange));
1267*35238bceSAndroid Build Coastguard Worker     IsWritableSupersetEntry::Iterator end   = state.getVariableManager().getEnd(IsWritableSupersetEntry(valueRange));
1268*35238bceSAndroid Build Coastguard Worker 
1269*35238bceSAndroid Build Coastguard Worker     const float createOnAssignWeight = 0.1f; // Will essentially create an unused variable
1270*35238bceSAndroid Build Coastguard Worker     bool createVar                   = canAllocateVariable(state, valueRange.getType()) &&
1271*35238bceSAndroid Build Coastguard Worker                      (first == end || getWeightedBool(state.getRandom(), createOnAssignWeight));
1272*35238bceSAndroid Build Coastguard Worker 
1273*35238bceSAndroid Build Coastguard Worker     if (createVar)
1274*35238bceSAndroid Build Coastguard Worker     {
1275*35238bceSAndroid Build Coastguard Worker         m_variable = state.getVariableManager().allocate(valueRange.getType());
1276*35238bceSAndroid Build Coastguard Worker         // \note Storage will be LOCAL
1277*35238bceSAndroid Build Coastguard Worker     }
1278*35238bceSAndroid Build Coastguard Worker     else
1279*35238bceSAndroid Build Coastguard Worker     {
1280*35238bceSAndroid Build Coastguard Worker         // Choose random
1281*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(first != end);
1282*35238bceSAndroid Build Coastguard Worker         const ValueEntry *entry = state.getRandom().choose<const ValueEntry *>(first, end);
1283*35238bceSAndroid Build Coastguard Worker         m_variable              = entry->getVariable();
1284*35238bceSAndroid Build Coastguard Worker     }
1285*35238bceSAndroid Build Coastguard Worker 
1286*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(m_variable);
1287*35238bceSAndroid Build Coastguard Worker 
1288*35238bceSAndroid Build Coastguard Worker     // Reset value range.
1289*35238bceSAndroid Build Coastguard Worker     const ValueEntry *parentEntry = state.getVariableManager().getParentValue(m_variable);
1290*35238bceSAndroid Build Coastguard Worker     if (parentEntry)
1291*35238bceSAndroid Build Coastguard Worker     {
1292*35238bceSAndroid Build Coastguard Worker         // Use parent value range.
1293*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().setValue(m_variable, parentEntry->getValueRange());
1294*35238bceSAndroid Build Coastguard Worker     }
1295*35238bceSAndroid Build Coastguard Worker     else
1296*35238bceSAndroid Build Coastguard Worker     {
1297*35238bceSAndroid Build Coastguard Worker         // Use infinite range.
1298*35238bceSAndroid Build Coastguard Worker         ValueRange infRange(m_variable->getType());
1299*35238bceSAndroid Build Coastguard Worker         setInfiniteRange(infRange);
1300*35238bceSAndroid Build Coastguard Worker 
1301*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().setValue(m_variable, infRange.asAccess());
1302*35238bceSAndroid Build Coastguard Worker     }
1303*35238bceSAndroid Build Coastguard Worker }
1304*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)1305*35238bceSAndroid Build Coastguard Worker float VariableWrite::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
1306*35238bceSAndroid Build Coastguard Worker {
1307*35238bceSAndroid Build Coastguard Worker     if (!canAllocateVariable(state, valueRange.getType()) &&
1308*35238bceSAndroid Build Coastguard Worker         !state.getVariableManager().hasEntry(IsWritableSupersetEntry(valueRange)))
1309*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1310*35238bceSAndroid Build Coastguard Worker     else
1311*35238bceSAndroid Build Coastguard Worker         return 1.0f;
1312*35238bceSAndroid Build Coastguard Worker }
1313*35238bceSAndroid Build Coastguard Worker 
evaluate(ExecutionContext & evalCtx)1314*35238bceSAndroid Build Coastguard Worker void VariableAccess::evaluate(ExecutionContext &evalCtx)
1315*35238bceSAndroid Build Coastguard Worker {
1316*35238bceSAndroid Build Coastguard Worker     m_valueAccess = evalCtx.getValue(m_variable);
1317*35238bceSAndroid Build Coastguard Worker }
1318*35238bceSAndroid Build Coastguard Worker 
ParenOp(GeneratorState & state,ConstValueRangeAccess valueRange)1319*35238bceSAndroid Build Coastguard Worker ParenOp::ParenOp(GeneratorState &state, ConstValueRangeAccess valueRange) : m_valueRange(valueRange), m_child(DE_NULL)
1320*35238bceSAndroid Build Coastguard Worker {
1321*35238bceSAndroid Build Coastguard Worker     DE_UNREF(state);
1322*35238bceSAndroid Build Coastguard Worker }
1323*35238bceSAndroid Build Coastguard Worker 
~ParenOp(void)1324*35238bceSAndroid Build Coastguard Worker ParenOp::~ParenOp(void)
1325*35238bceSAndroid Build Coastguard Worker {
1326*35238bceSAndroid Build Coastguard Worker     delete m_child;
1327*35238bceSAndroid Build Coastguard Worker }
1328*35238bceSAndroid Build Coastguard Worker 
createNextChild(GeneratorState & state)1329*35238bceSAndroid Build Coastguard Worker Expression *ParenOp::createNextChild(GeneratorState &state)
1330*35238bceSAndroid Build Coastguard Worker {
1331*35238bceSAndroid Build Coastguard Worker     if (m_child == DE_NULL)
1332*35238bceSAndroid Build Coastguard Worker     {
1333*35238bceSAndroid Build Coastguard Worker         m_child = Expression::createRandom(state, m_valueRange.asAccess());
1334*35238bceSAndroid Build Coastguard Worker         return m_child;
1335*35238bceSAndroid Build Coastguard Worker     }
1336*35238bceSAndroid Build Coastguard Worker     else
1337*35238bceSAndroid Build Coastguard Worker         return DE_NULL;
1338*35238bceSAndroid Build Coastguard Worker }
1339*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const1340*35238bceSAndroid Build Coastguard Worker void ParenOp::tokenize(GeneratorState &state, TokenStream &str) const
1341*35238bceSAndroid Build Coastguard Worker {
1342*35238bceSAndroid Build Coastguard Worker     str << Token::LEFT_PAREN;
1343*35238bceSAndroid Build Coastguard Worker     m_child->tokenize(state, str);
1344*35238bceSAndroid Build Coastguard Worker     str << Token::RIGHT_PAREN;
1345*35238bceSAndroid Build Coastguard Worker }
1346*35238bceSAndroid Build Coastguard Worker 
setChild(Expression * expression)1347*35238bceSAndroid Build Coastguard Worker void ParenOp::setChild(Expression *expression)
1348*35238bceSAndroid Build Coastguard Worker {
1349*35238bceSAndroid Build Coastguard Worker     m_child = expression;
1350*35238bceSAndroid Build Coastguard Worker }
1351*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)1352*35238bceSAndroid Build Coastguard Worker float ParenOp::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
1353*35238bceSAndroid Build Coastguard Worker {
1354*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType().isVoid())
1355*35238bceSAndroid Build Coastguard Worker         return state.getExpressionDepth() + 2 <= state.getShaderParameters().maxExpressionDepth ? unusedValueWeight :
1356*35238bceSAndroid Build Coastguard Worker                                                                                                   0.0f;
1357*35238bceSAndroid Build Coastguard Worker     else
1358*35238bceSAndroid Build Coastguard Worker     {
1359*35238bceSAndroid Build Coastguard Worker         int requiredDepth = 1 + getConservativeValueExprDepth(state, valueRange);
1360*35238bceSAndroid Build Coastguard Worker         return state.getExpressionDepth() + requiredDepth <= state.getShaderParameters().maxExpressionDepth ? 1.0f :
1361*35238bceSAndroid Build Coastguard Worker                                                                                                               0.0f;
1362*35238bceSAndroid Build Coastguard Worker     }
1363*35238bceSAndroid Build Coastguard Worker }
1364*35238bceSAndroid Build Coastguard Worker 
1365*35238bceSAndroid Build Coastguard Worker const int swizzlePrecedence = 2;
1366*35238bceSAndroid Build Coastguard Worker 
SwizzleOp(GeneratorState & state,ConstValueRangeAccess valueRange)1367*35238bceSAndroid Build Coastguard Worker SwizzleOp::SwizzleOp(GeneratorState &state, ConstValueRangeAccess valueRange)
1368*35238bceSAndroid Build Coastguard Worker     : m_outValueRange(valueRange)
1369*35238bceSAndroid Build Coastguard Worker     , m_numInputElements(0)
1370*35238bceSAndroid Build Coastguard Worker     , m_child(DE_NULL)
1371*35238bceSAndroid Build Coastguard Worker {
1372*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(!m_outValueRange.getType().isVoid()); // \todo [2011-06-13 pyry] Void support
1373*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(m_outValueRange.getType().isFloatOrVec() || m_outValueRange.getType().isIntOrVec() ||
1374*35238bceSAndroid Build Coastguard Worker               m_outValueRange.getType().isBoolOrVec());
1375*35238bceSAndroid Build Coastguard Worker 
1376*35238bceSAndroid Build Coastguard Worker     m_value.setStorage(m_outValueRange.getType());
1377*35238bceSAndroid Build Coastguard Worker 
1378*35238bceSAndroid Build Coastguard Worker     int numOutputElements = m_outValueRange.getType().getNumElements();
1379*35238bceSAndroid Build Coastguard Worker 
1380*35238bceSAndroid Build Coastguard Worker     // \note Swizzle works for vector types only.
1381*35238bceSAndroid Build Coastguard Worker     // \todo [2011-06-13 pyry] Use components multiple times.
1382*35238bceSAndroid Build Coastguard Worker     m_numInputElements = state.getRandom().getInt(deMax32(numOutputElements, 2), 4);
1383*35238bceSAndroid Build Coastguard Worker 
1384*35238bceSAndroid Build Coastguard Worker     std::set<int> availableElements;
1385*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < m_numInputElements; ndx++)
1386*35238bceSAndroid Build Coastguard Worker         availableElements.insert(ndx);
1387*35238bceSAndroid Build Coastguard Worker 
1388*35238bceSAndroid Build Coastguard Worker     // Randomize swizzle.
1389*35238bceSAndroid Build Coastguard Worker     for (int elemNdx = 0; elemNdx < (int)DE_LENGTH_OF_ARRAY(m_swizzle); elemNdx++)
1390*35238bceSAndroid Build Coastguard Worker     {
1391*35238bceSAndroid Build Coastguard Worker         if (elemNdx < numOutputElements)
1392*35238bceSAndroid Build Coastguard Worker         {
1393*35238bceSAndroid Build Coastguard Worker             int inElemNdx = state.getRandom().choose<int>(availableElements.begin(), availableElements.end());
1394*35238bceSAndroid Build Coastguard Worker             availableElements.erase(inElemNdx);
1395*35238bceSAndroid Build Coastguard Worker             m_swizzle[elemNdx] = (uint8_t)inElemNdx;
1396*35238bceSAndroid Build Coastguard Worker         }
1397*35238bceSAndroid Build Coastguard Worker         else
1398*35238bceSAndroid Build Coastguard Worker             m_swizzle[elemNdx] = 0;
1399*35238bceSAndroid Build Coastguard Worker     }
1400*35238bceSAndroid Build Coastguard Worker }
1401*35238bceSAndroid Build Coastguard Worker 
~SwizzleOp(void)1402*35238bceSAndroid Build Coastguard Worker SwizzleOp::~SwizzleOp(void)
1403*35238bceSAndroid Build Coastguard Worker {
1404*35238bceSAndroid Build Coastguard Worker     delete m_child;
1405*35238bceSAndroid Build Coastguard Worker }
1406*35238bceSAndroid Build Coastguard Worker 
createNextChild(GeneratorState & state)1407*35238bceSAndroid Build Coastguard Worker Expression *SwizzleOp::createNextChild(GeneratorState &state)
1408*35238bceSAndroid Build Coastguard Worker {
1409*35238bceSAndroid Build Coastguard Worker     if (m_child)
1410*35238bceSAndroid Build Coastguard Worker         return DE_NULL;
1411*35238bceSAndroid Build Coastguard Worker 
1412*35238bceSAndroid Build Coastguard Worker     // Compute input value range.
1413*35238bceSAndroid Build Coastguard Worker     VariableType inVarType  = VariableType(m_outValueRange.getType().getBaseType(), m_numInputElements);
1414*35238bceSAndroid Build Coastguard Worker     ValueRange inValueRange = ValueRange(inVarType);
1415*35238bceSAndroid Build Coastguard Worker 
1416*35238bceSAndroid Build Coastguard Worker     // Initialize all inputs to -inf..inf
1417*35238bceSAndroid Build Coastguard Worker     setInfiniteRange(inValueRange);
1418*35238bceSAndroid Build Coastguard Worker 
1419*35238bceSAndroid Build Coastguard Worker     // Compute intersections.
1420*35238bceSAndroid Build Coastguard Worker     int numOutputElements = m_outValueRange.getType().getNumElements();
1421*35238bceSAndroid Build Coastguard Worker     for (int outElemNdx = 0; outElemNdx < numOutputElements; outElemNdx++)
1422*35238bceSAndroid Build Coastguard Worker     {
1423*35238bceSAndroid Build Coastguard Worker         int inElemNdx = m_swizzle[outElemNdx];
1424*35238bceSAndroid Build Coastguard Worker         ValueRange::computeIntersection(inValueRange.asAccess().component(inElemNdx),
1425*35238bceSAndroid Build Coastguard Worker                                         inValueRange.asAccess().component(inElemNdx),
1426*35238bceSAndroid Build Coastguard Worker                                         m_outValueRange.asAccess().component(outElemNdx));
1427*35238bceSAndroid Build Coastguard Worker     }
1428*35238bceSAndroid Build Coastguard Worker 
1429*35238bceSAndroid Build Coastguard Worker     // Create child.
1430*35238bceSAndroid Build Coastguard Worker     state.pushPrecedence(swizzlePrecedence);
1431*35238bceSAndroid Build Coastguard Worker     m_child = Expression::createRandom(state, inValueRange.asAccess());
1432*35238bceSAndroid Build Coastguard Worker     state.popPrecedence();
1433*35238bceSAndroid Build Coastguard Worker 
1434*35238bceSAndroid Build Coastguard Worker     return m_child;
1435*35238bceSAndroid Build Coastguard Worker }
1436*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const1437*35238bceSAndroid Build Coastguard Worker void SwizzleOp::tokenize(GeneratorState &state, TokenStream &str) const
1438*35238bceSAndroid Build Coastguard Worker {
1439*35238bceSAndroid Build Coastguard Worker     const char *rgbaSet[]   = {"r", "g", "b", "a"};
1440*35238bceSAndroid Build Coastguard Worker     const char *xyzwSet[]   = {"x", "y", "z", "w"};
1441*35238bceSAndroid Build Coastguard Worker     const char *stpqSet[]   = {"s", "t", "p", "q"};
1442*35238bceSAndroid Build Coastguard Worker     const char **swizzleSet = DE_NULL;
1443*35238bceSAndroid Build Coastguard Worker 
1444*35238bceSAndroid Build Coastguard Worker     switch (state.getRandom().getInt(0, 2))
1445*35238bceSAndroid Build Coastguard Worker     {
1446*35238bceSAndroid Build Coastguard Worker     case 0:
1447*35238bceSAndroid Build Coastguard Worker         swizzleSet = rgbaSet;
1448*35238bceSAndroid Build Coastguard Worker         break;
1449*35238bceSAndroid Build Coastguard Worker     case 1:
1450*35238bceSAndroid Build Coastguard Worker         swizzleSet = xyzwSet;
1451*35238bceSAndroid Build Coastguard Worker         break;
1452*35238bceSAndroid Build Coastguard Worker     case 2:
1453*35238bceSAndroid Build Coastguard Worker         swizzleSet = stpqSet;
1454*35238bceSAndroid Build Coastguard Worker         break;
1455*35238bceSAndroid Build Coastguard Worker     default:
1456*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
1457*35238bceSAndroid Build Coastguard Worker     }
1458*35238bceSAndroid Build Coastguard Worker 
1459*35238bceSAndroid Build Coastguard Worker     std::string swizzleStr;
1460*35238bceSAndroid Build Coastguard Worker     for (int elemNdx = 0; elemNdx < m_outValueRange.getType().getNumElements(); elemNdx++)
1461*35238bceSAndroid Build Coastguard Worker         swizzleStr += swizzleSet[m_swizzle[elemNdx]];
1462*35238bceSAndroid Build Coastguard Worker 
1463*35238bceSAndroid Build Coastguard Worker     m_child->tokenize(state, str);
1464*35238bceSAndroid Build Coastguard Worker     str << Token::DOT << Token(swizzleStr.c_str());
1465*35238bceSAndroid Build Coastguard Worker }
1466*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)1467*35238bceSAndroid Build Coastguard Worker float SwizzleOp::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
1468*35238bceSAndroid Build Coastguard Worker {
1469*35238bceSAndroid Build Coastguard Worker     if (!state.getProgramParameters().useSwizzle)
1470*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1471*35238bceSAndroid Build Coastguard Worker 
1472*35238bceSAndroid Build Coastguard Worker     if (state.getPrecedence() < swizzlePrecedence)
1473*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1474*35238bceSAndroid Build Coastguard Worker 
1475*35238bceSAndroid Build Coastguard Worker     if (!valueRange.getType().isFloatOrVec() && !valueRange.getType().isIntOrVec() &&
1476*35238bceSAndroid Build Coastguard Worker         !valueRange.getType().isBoolOrVec())
1477*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1478*35238bceSAndroid Build Coastguard Worker 
1479*35238bceSAndroid Build Coastguard Worker     int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
1480*35238bceSAndroid Build Coastguard Worker 
1481*35238bceSAndroid Build Coastguard Worker     // Swizzle + Constructor + Values
1482*35238bceSAndroid Build Coastguard Worker     if (availableLevels < 3)
1483*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1484*35238bceSAndroid Build Coastguard Worker 
1485*35238bceSAndroid Build Coastguard Worker     return 1.0f;
1486*35238bceSAndroid Build Coastguard Worker }
1487*35238bceSAndroid Build Coastguard Worker 
evaluate(ExecutionContext & execCtx)1488*35238bceSAndroid Build Coastguard Worker void SwizzleOp::evaluate(ExecutionContext &execCtx)
1489*35238bceSAndroid Build Coastguard Worker {
1490*35238bceSAndroid Build Coastguard Worker     m_child->evaluate(execCtx);
1491*35238bceSAndroid Build Coastguard Worker 
1492*35238bceSAndroid Build Coastguard Worker     ExecConstValueAccess inValue = m_child->getValue();
1493*35238bceSAndroid Build Coastguard Worker     ExecValueAccess outValue     = m_value.getValue(m_outValueRange.getType());
1494*35238bceSAndroid Build Coastguard Worker 
1495*35238bceSAndroid Build Coastguard Worker     for (int outElemNdx = 0; outElemNdx < outValue.getType().getNumElements(); outElemNdx++)
1496*35238bceSAndroid Build Coastguard Worker     {
1497*35238bceSAndroid Build Coastguard Worker         int inElemNdx                  = m_swizzle[outElemNdx];
1498*35238bceSAndroid Build Coastguard Worker         outValue.component(outElemNdx) = inValue.component(inElemNdx).value();
1499*35238bceSAndroid Build Coastguard Worker     }
1500*35238bceSAndroid Build Coastguard Worker }
1501*35238bceSAndroid Build Coastguard Worker 
countSamplers(const VariableManager & varManager,VariableType::Type samplerType)1502*35238bceSAndroid Build Coastguard Worker static int countSamplers(const VariableManager &varManager, VariableType::Type samplerType)
1503*35238bceSAndroid Build Coastguard Worker {
1504*35238bceSAndroid Build Coastguard Worker     int numSamplers = 0;
1505*35238bceSAndroid Build Coastguard Worker 
1506*35238bceSAndroid Build Coastguard Worker     IsSamplerEntry::Iterator i   = varManager.getBegin(IsSamplerEntry(samplerType));
1507*35238bceSAndroid Build Coastguard Worker     IsSamplerEntry::Iterator end = varManager.getEnd(IsSamplerEntry(samplerType));
1508*35238bceSAndroid Build Coastguard Worker 
1509*35238bceSAndroid Build Coastguard Worker     for (; i != end; i++)
1510*35238bceSAndroid Build Coastguard Worker         numSamplers += 1;
1511*35238bceSAndroid Build Coastguard Worker 
1512*35238bceSAndroid Build Coastguard Worker     return numSamplers;
1513*35238bceSAndroid Build Coastguard Worker }
1514*35238bceSAndroid Build Coastguard Worker 
TexLookup(GeneratorState & state,ConstValueRangeAccess valueRange)1515*35238bceSAndroid Build Coastguard Worker TexLookup::TexLookup(GeneratorState &state, ConstValueRangeAccess valueRange)
1516*35238bceSAndroid Build Coastguard Worker     : m_type(TYPE_LAST)
1517*35238bceSAndroid Build Coastguard Worker     , m_coordExpr(DE_NULL)
1518*35238bceSAndroid Build Coastguard Worker     , m_lodBiasExpr(DE_NULL)
1519*35238bceSAndroid Build Coastguard Worker     , m_valueType(VariableType::TYPE_FLOAT, 4)
1520*35238bceSAndroid Build Coastguard Worker     , m_value(m_valueType)
1521*35238bceSAndroid Build Coastguard Worker {
1522*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(valueRange.getType() == VariableType(VariableType::TYPE_FLOAT, 4));
1523*35238bceSAndroid Build Coastguard Worker     DE_UNREF(valueRange); // Texture output value range is constant.
1524*35238bceSAndroid Build Coastguard Worker 
1525*35238bceSAndroid Build Coastguard Worker     // Select type.
1526*35238bceSAndroid Build Coastguard Worker     vector<Type> typeCandidates;
1527*35238bceSAndroid Build Coastguard Worker     if (state.getShaderParameters().useTexture2D)
1528*35238bceSAndroid Build Coastguard Worker     {
1529*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURE2D);
1530*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURE2D_LOD);
1531*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURE2D_PROJ);
1532*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURE2D_PROJ_LOD);
1533*35238bceSAndroid Build Coastguard Worker     }
1534*35238bceSAndroid Build Coastguard Worker 
1535*35238bceSAndroid Build Coastguard Worker     if (state.getShaderParameters().useTextureCube)
1536*35238bceSAndroid Build Coastguard Worker     {
1537*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURECUBE);
1538*35238bceSAndroid Build Coastguard Worker         typeCandidates.push_back(TYPE_TEXTURECUBE_LOD);
1539*35238bceSAndroid Build Coastguard Worker     }
1540*35238bceSAndroid Build Coastguard Worker 
1541*35238bceSAndroid Build Coastguard Worker     m_type = state.getRandom().choose<Type>(typeCandidates.begin(), typeCandidates.end());
1542*35238bceSAndroid Build Coastguard Worker 
1543*35238bceSAndroid Build Coastguard Worker     // Select or allocate sampler.
1544*35238bceSAndroid Build Coastguard Worker     VariableType::Type samplerType = VariableType::TYPE_LAST;
1545*35238bceSAndroid Build Coastguard Worker     switch (m_type)
1546*35238bceSAndroid Build Coastguard Worker     {
1547*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D:
1548*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_LOD:
1549*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_PROJ:
1550*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_PROJ_LOD:
1551*35238bceSAndroid Build Coastguard Worker         samplerType = VariableType::TYPE_SAMPLER_2D;
1552*35238bceSAndroid Build Coastguard Worker         break;
1553*35238bceSAndroid Build Coastguard Worker 
1554*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURECUBE:
1555*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURECUBE_LOD:
1556*35238bceSAndroid Build Coastguard Worker         samplerType = VariableType::TYPE_SAMPLER_CUBE;
1557*35238bceSAndroid Build Coastguard Worker         break;
1558*35238bceSAndroid Build Coastguard Worker 
1559*35238bceSAndroid Build Coastguard Worker     default:
1560*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
1561*35238bceSAndroid Build Coastguard Worker     }
1562*35238bceSAndroid Build Coastguard Worker 
1563*35238bceSAndroid Build Coastguard Worker     int sampler2DCount   = countSamplers(state.getVariableManager(), VariableType::TYPE_SAMPLER_2D);
1564*35238bceSAndroid Build Coastguard Worker     int samplerCubeCount = countSamplers(state.getVariableManager(), VariableType::TYPE_SAMPLER_CUBE);
1565*35238bceSAndroid Build Coastguard Worker     bool canAllocSampler = sampler2DCount + samplerCubeCount < state.getShaderParameters().maxSamplers;
1566*35238bceSAndroid Build Coastguard Worker     bool hasSampler      = samplerType == VariableType::TYPE_SAMPLER_2D ? (sampler2DCount > 0) : (samplerCubeCount > 0);
1567*35238bceSAndroid Build Coastguard Worker     bool allocSampler    = !hasSampler || (canAllocSampler && state.getRandom().getBool());
1568*35238bceSAndroid Build Coastguard Worker 
1569*35238bceSAndroid Build Coastguard Worker     if (allocSampler)
1570*35238bceSAndroid Build Coastguard Worker     {
1571*35238bceSAndroid Build Coastguard Worker         Variable *sampler = state.getVariableManager().allocate(VariableType(samplerType, 1));
1572*35238bceSAndroid Build Coastguard Worker         state.getVariableManager().setStorage(sampler, Variable::STORAGE_UNIFORM); // Samplers are always uniforms.
1573*35238bceSAndroid Build Coastguard Worker         m_sampler = sampler;
1574*35238bceSAndroid Build Coastguard Worker     }
1575*35238bceSAndroid Build Coastguard Worker     else
1576*35238bceSAndroid Build Coastguard Worker         m_sampler = state.getRandom()
1577*35238bceSAndroid Build Coastguard Worker                         .choose<const ValueEntry *>(state.getVariableManager().getBegin(IsSamplerEntry(samplerType)),
1578*35238bceSAndroid Build Coastguard Worker                                                     state.getVariableManager().getEnd(IsSamplerEntry(samplerType)))
1579*35238bceSAndroid Build Coastguard Worker                         ->getVariable();
1580*35238bceSAndroid Build Coastguard Worker }
1581*35238bceSAndroid Build Coastguard Worker 
~TexLookup(void)1582*35238bceSAndroid Build Coastguard Worker TexLookup::~TexLookup(void)
1583*35238bceSAndroid Build Coastguard Worker {
1584*35238bceSAndroid Build Coastguard Worker     delete m_coordExpr;
1585*35238bceSAndroid Build Coastguard Worker     delete m_lodBiasExpr;
1586*35238bceSAndroid Build Coastguard Worker }
1587*35238bceSAndroid Build Coastguard Worker 
createNextChild(GeneratorState & state)1588*35238bceSAndroid Build Coastguard Worker Expression *TexLookup::createNextChild(GeneratorState &state)
1589*35238bceSAndroid Build Coastguard Worker {
1590*35238bceSAndroid Build Coastguard Worker     bool hasLodBias =
1591*35238bceSAndroid Build Coastguard Worker         m_type == TYPE_TEXTURE2D_LOD || m_type == TYPE_TEXTURE2D_PROJ_LOD || m_type == TYPE_TEXTURECUBE_LOD;
1592*35238bceSAndroid Build Coastguard Worker 
1593*35238bceSAndroid Build Coastguard Worker     if (hasLodBias && !m_lodBiasExpr)
1594*35238bceSAndroid Build Coastguard Worker     {
1595*35238bceSAndroid Build Coastguard Worker         ValueRange lodRange(VariableType(VariableType::TYPE_FLOAT, 1));
1596*35238bceSAndroid Build Coastguard Worker         setInfiniteRange(lodRange); // Any value is valid.
1597*35238bceSAndroid Build Coastguard Worker 
1598*35238bceSAndroid Build Coastguard Worker         m_lodBiasExpr = Expression::createRandom(state, lodRange.asAccess());
1599*35238bceSAndroid Build Coastguard Worker         return m_lodBiasExpr;
1600*35238bceSAndroid Build Coastguard Worker     }
1601*35238bceSAndroid Build Coastguard Worker 
1602*35238bceSAndroid Build Coastguard Worker     if (!m_coordExpr)
1603*35238bceSAndroid Build Coastguard Worker     {
1604*35238bceSAndroid Build Coastguard Worker         if (m_type == TYPE_TEXTURECUBE || m_type == TYPE_TEXTURECUBE_LOD)
1605*35238bceSAndroid Build Coastguard Worker         {
1606*35238bceSAndroid Build Coastguard Worker             // Make sure major axis selection can be done.
1607*35238bceSAndroid Build Coastguard Worker             int majorAxisNdx = state.getRandom().getInt(0, 2);
1608*35238bceSAndroid Build Coastguard Worker 
1609*35238bceSAndroid Build Coastguard Worker             ValueRange coordRange(VariableType(VariableType::TYPE_FLOAT, 3));
1610*35238bceSAndroid Build Coastguard Worker 
1611*35238bceSAndroid Build Coastguard Worker             for (int ndx = 0; ndx < 3; ndx++)
1612*35238bceSAndroid Build Coastguard Worker             {
1613*35238bceSAndroid Build Coastguard Worker                 if (ndx == majorAxisNdx)
1614*35238bceSAndroid Build Coastguard Worker                 {
1615*35238bceSAndroid Build Coastguard Worker                     bool neg                           = state.getRandom().getBool();
1616*35238bceSAndroid Build Coastguard Worker                     coordRange.getMin().component(ndx) = neg ? -4.0f : 2.25f;
1617*35238bceSAndroid Build Coastguard Worker                     coordRange.getMax().component(ndx) = neg ? -2.25f : 4.0f;
1618*35238bceSAndroid Build Coastguard Worker                 }
1619*35238bceSAndroid Build Coastguard Worker                 else
1620*35238bceSAndroid Build Coastguard Worker                 {
1621*35238bceSAndroid Build Coastguard Worker                     coordRange.getMin().component(ndx) = -2.0f;
1622*35238bceSAndroid Build Coastguard Worker                     coordRange.getMax().component(ndx) = 2.0f;
1623*35238bceSAndroid Build Coastguard Worker                 }
1624*35238bceSAndroid Build Coastguard Worker             }
1625*35238bceSAndroid Build Coastguard Worker 
1626*35238bceSAndroid Build Coastguard Worker             m_coordExpr = Expression::createRandom(state, coordRange.asAccess());
1627*35238bceSAndroid Build Coastguard Worker         }
1628*35238bceSAndroid Build Coastguard Worker         else
1629*35238bceSAndroid Build Coastguard Worker         {
1630*35238bceSAndroid Build Coastguard Worker             bool isProj         = m_type == TYPE_TEXTURE2D_PROJ || m_type == TYPE_TEXTURE2D_PROJ_LOD;
1631*35238bceSAndroid Build Coastguard Worker             int coordScalarSize = isProj ? 3 : 2;
1632*35238bceSAndroid Build Coastguard Worker 
1633*35238bceSAndroid Build Coastguard Worker             ValueRange coordRange(VariableType(VariableType::TYPE_FLOAT, coordScalarSize));
1634*35238bceSAndroid Build Coastguard Worker             setInfiniteRange(coordRange); // Initialize base range with -inf..inf
1635*35238bceSAndroid Build Coastguard Worker 
1636*35238bceSAndroid Build Coastguard Worker             if (isProj)
1637*35238bceSAndroid Build Coastguard Worker             {
1638*35238bceSAndroid Build Coastguard Worker                 // w coordinate must be something sane, and not 0.
1639*35238bceSAndroid Build Coastguard Worker                 bool neg                         = state.getRandom().getBool();
1640*35238bceSAndroid Build Coastguard Worker                 coordRange.getMin().component(2) = neg ? -4.0f : 0.25f;
1641*35238bceSAndroid Build Coastguard Worker                 coordRange.getMax().component(2) = neg ? -0.25f : 4.0f;
1642*35238bceSAndroid Build Coastguard Worker             }
1643*35238bceSAndroid Build Coastguard Worker 
1644*35238bceSAndroid Build Coastguard Worker             m_coordExpr = Expression::createRandom(state, coordRange.asAccess());
1645*35238bceSAndroid Build Coastguard Worker         }
1646*35238bceSAndroid Build Coastguard Worker 
1647*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(m_coordExpr);
1648*35238bceSAndroid Build Coastguard Worker         return m_coordExpr;
1649*35238bceSAndroid Build Coastguard Worker     }
1650*35238bceSAndroid Build Coastguard Worker 
1651*35238bceSAndroid Build Coastguard Worker     return DE_NULL; // Done.
1652*35238bceSAndroid Build Coastguard Worker }
1653*35238bceSAndroid Build Coastguard Worker 
tokenize(GeneratorState & state,TokenStream & str) const1654*35238bceSAndroid Build Coastguard Worker void TexLookup::tokenize(GeneratorState &state, TokenStream &str) const
1655*35238bceSAndroid Build Coastguard Worker {
1656*35238bceSAndroid Build Coastguard Worker     bool isVertex = state.getShader().getType() == Shader::TYPE_VERTEX;
1657*35238bceSAndroid Build Coastguard Worker 
1658*35238bceSAndroid Build Coastguard Worker     if (state.getProgramParameters().version == VERSION_300)
1659*35238bceSAndroid Build Coastguard Worker     {
1660*35238bceSAndroid Build Coastguard Worker         switch (m_type)
1661*35238bceSAndroid Build Coastguard Worker         {
1662*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D:
1663*35238bceSAndroid Build Coastguard Worker             str << "texture";
1664*35238bceSAndroid Build Coastguard Worker             break;
1665*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_LOD:
1666*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "textureLod" : "texture");
1667*35238bceSAndroid Build Coastguard Worker             break;
1668*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_PROJ:
1669*35238bceSAndroid Build Coastguard Worker             str << "textureProj";
1670*35238bceSAndroid Build Coastguard Worker             break;
1671*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_PROJ_LOD:
1672*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "textureProjLod" : "textureProj");
1673*35238bceSAndroid Build Coastguard Worker             break;
1674*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURECUBE:
1675*35238bceSAndroid Build Coastguard Worker             str << "texture";
1676*35238bceSAndroid Build Coastguard Worker             break;
1677*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURECUBE_LOD:
1678*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "textureLod" : "texture");
1679*35238bceSAndroid Build Coastguard Worker             break;
1680*35238bceSAndroid Build Coastguard Worker         default:
1681*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(false);
1682*35238bceSAndroid Build Coastguard Worker         }
1683*35238bceSAndroid Build Coastguard Worker     }
1684*35238bceSAndroid Build Coastguard Worker     else
1685*35238bceSAndroid Build Coastguard Worker     {
1686*35238bceSAndroid Build Coastguard Worker         switch (m_type)
1687*35238bceSAndroid Build Coastguard Worker         {
1688*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D:
1689*35238bceSAndroid Build Coastguard Worker             str << "texture2D";
1690*35238bceSAndroid Build Coastguard Worker             break;
1691*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_LOD:
1692*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "texture2DLod" : "texture2D");
1693*35238bceSAndroid Build Coastguard Worker             break;
1694*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_PROJ:
1695*35238bceSAndroid Build Coastguard Worker             str << "texture2DProj";
1696*35238bceSAndroid Build Coastguard Worker             break;
1697*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURE2D_PROJ_LOD:
1698*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "texture2DProjLod" : "texture2DProj");
1699*35238bceSAndroid Build Coastguard Worker             break;
1700*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURECUBE:
1701*35238bceSAndroid Build Coastguard Worker             str << "textureCube";
1702*35238bceSAndroid Build Coastguard Worker             break;
1703*35238bceSAndroid Build Coastguard Worker         case TYPE_TEXTURECUBE_LOD:
1704*35238bceSAndroid Build Coastguard Worker             str << (isVertex ? "textureCubeLod" : "textureCube");
1705*35238bceSAndroid Build Coastguard Worker             break;
1706*35238bceSAndroid Build Coastguard Worker         default:
1707*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(false);
1708*35238bceSAndroid Build Coastguard Worker         }
1709*35238bceSAndroid Build Coastguard Worker     }
1710*35238bceSAndroid Build Coastguard Worker 
1711*35238bceSAndroid Build Coastguard Worker     str << Token::LEFT_PAREN;
1712*35238bceSAndroid Build Coastguard Worker     str << m_sampler->getName();
1713*35238bceSAndroid Build Coastguard Worker     str << Token::COMMA;
1714*35238bceSAndroid Build Coastguard Worker     m_coordExpr->tokenize(state, str);
1715*35238bceSAndroid Build Coastguard Worker 
1716*35238bceSAndroid Build Coastguard Worker     if (m_lodBiasExpr)
1717*35238bceSAndroid Build Coastguard Worker     {
1718*35238bceSAndroid Build Coastguard Worker         str << Token::COMMA;
1719*35238bceSAndroid Build Coastguard Worker         m_lodBiasExpr->tokenize(state, str);
1720*35238bceSAndroid Build Coastguard Worker     }
1721*35238bceSAndroid Build Coastguard Worker 
1722*35238bceSAndroid Build Coastguard Worker     str << Token::RIGHT_PAREN;
1723*35238bceSAndroid Build Coastguard Worker }
1724*35238bceSAndroid Build Coastguard Worker 
getWeight(const GeneratorState & state,ConstValueRangeAccess valueRange)1725*35238bceSAndroid Build Coastguard Worker float TexLookup::getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange)
1726*35238bceSAndroid Build Coastguard Worker {
1727*35238bceSAndroid Build Coastguard Worker     if (state.getShaderParameters().texLookupBaseWeight <= 0.0f)
1728*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1729*35238bceSAndroid Build Coastguard Worker 
1730*35238bceSAndroid Build Coastguard Worker     int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
1731*35238bceSAndroid Build Coastguard Worker 
1732*35238bceSAndroid Build Coastguard Worker     // Lookup + Constructor + Values
1733*35238bceSAndroid Build Coastguard Worker     if (availableLevels < 3)
1734*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1735*35238bceSAndroid Build Coastguard Worker 
1736*35238bceSAndroid Build Coastguard Worker     if (state.getExpressionFlags() & (CONST_EXPR | NO_VAR_ALLOCATION))
1737*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1738*35238bceSAndroid Build Coastguard Worker 
1739*35238bceSAndroid Build Coastguard Worker     if (valueRange.getType() != VariableType(VariableType::TYPE_FLOAT, 4))
1740*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1741*35238bceSAndroid Build Coastguard Worker 
1742*35238bceSAndroid Build Coastguard Worker     ValueRange texOutputRange(VariableType(VariableType::TYPE_FLOAT, 4));
1743*35238bceSAndroid Build Coastguard Worker     for (int ndx = 0; ndx < 4; ndx++)
1744*35238bceSAndroid Build Coastguard Worker     {
1745*35238bceSAndroid Build Coastguard Worker         texOutputRange.getMin().component(ndx) = 0.0f;
1746*35238bceSAndroid Build Coastguard Worker         texOutputRange.getMax().component(ndx) = 1.0f;
1747*35238bceSAndroid Build Coastguard Worker     }
1748*35238bceSAndroid Build Coastguard Worker 
1749*35238bceSAndroid Build Coastguard Worker     if (!valueRange.isSupersetOf(texOutputRange.asAccess()))
1750*35238bceSAndroid Build Coastguard Worker         return 0.0f;
1751*35238bceSAndroid Build Coastguard Worker 
1752*35238bceSAndroid Build Coastguard Worker     return state.getShaderParameters().texLookupBaseWeight;
1753*35238bceSAndroid Build Coastguard Worker }
1754*35238bceSAndroid Build Coastguard Worker 
evaluate(ExecutionContext & execCtx)1755*35238bceSAndroid Build Coastguard Worker void TexLookup::evaluate(ExecutionContext &execCtx)
1756*35238bceSAndroid Build Coastguard Worker {
1757*35238bceSAndroid Build Coastguard Worker     // Evaluate coord and bias.
1758*35238bceSAndroid Build Coastguard Worker     m_coordExpr->evaluate(execCtx);
1759*35238bceSAndroid Build Coastguard Worker     if (m_lodBiasExpr)
1760*35238bceSAndroid Build Coastguard Worker         m_lodBiasExpr->evaluate(execCtx);
1761*35238bceSAndroid Build Coastguard Worker 
1762*35238bceSAndroid Build Coastguard Worker     ExecConstValueAccess coords = m_coordExpr->getValue();
1763*35238bceSAndroid Build Coastguard Worker     ExecValueAccess dst         = m_value.getValue(m_valueType);
1764*35238bceSAndroid Build Coastguard Worker 
1765*35238bceSAndroid Build Coastguard Worker     switch (m_type)
1766*35238bceSAndroid Build Coastguard Worker     {
1767*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D:
1768*35238bceSAndroid Build Coastguard Worker     {
1769*35238bceSAndroid Build Coastguard Worker         const Sampler2D &tex = execCtx.getSampler2D(m_sampler);
1770*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1771*35238bceSAndroid Build Coastguard Worker         {
1772*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1773*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1774*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s, t, 0.0f);
1775*35238bceSAndroid Build Coastguard Worker 
1776*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1777*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1778*35238bceSAndroid Build Coastguard Worker         }
1779*35238bceSAndroid Build Coastguard Worker         break;
1780*35238bceSAndroid Build Coastguard Worker     }
1781*35238bceSAndroid Build Coastguard Worker 
1782*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_LOD:
1783*35238bceSAndroid Build Coastguard Worker     {
1784*35238bceSAndroid Build Coastguard Worker         ExecConstValueAccess lod = m_lodBiasExpr->getValue();
1785*35238bceSAndroid Build Coastguard Worker         const Sampler2D &tex     = execCtx.getSampler2D(m_sampler);
1786*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1787*35238bceSAndroid Build Coastguard Worker         {
1788*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1789*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1790*35238bceSAndroid Build Coastguard Worker             float l     = lod.component(0).asFloat(i);
1791*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s, t, l);
1792*35238bceSAndroid Build Coastguard Worker 
1793*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1794*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1795*35238bceSAndroid Build Coastguard Worker         }
1796*35238bceSAndroid Build Coastguard Worker         break;
1797*35238bceSAndroid Build Coastguard Worker     }
1798*35238bceSAndroid Build Coastguard Worker 
1799*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_PROJ:
1800*35238bceSAndroid Build Coastguard Worker     {
1801*35238bceSAndroid Build Coastguard Worker         const Sampler2D &tex = execCtx.getSampler2D(m_sampler);
1802*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1803*35238bceSAndroid Build Coastguard Worker         {
1804*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1805*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1806*35238bceSAndroid Build Coastguard Worker             float w     = coords.component(2).asFloat(i);
1807*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s / w, t / w, 0.0f);
1808*35238bceSAndroid Build Coastguard Worker 
1809*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1810*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1811*35238bceSAndroid Build Coastguard Worker         }
1812*35238bceSAndroid Build Coastguard Worker         break;
1813*35238bceSAndroid Build Coastguard Worker     }
1814*35238bceSAndroid Build Coastguard Worker 
1815*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURE2D_PROJ_LOD:
1816*35238bceSAndroid Build Coastguard Worker     {
1817*35238bceSAndroid Build Coastguard Worker         ExecConstValueAccess lod = m_lodBiasExpr->getValue();
1818*35238bceSAndroid Build Coastguard Worker         const Sampler2D &tex     = execCtx.getSampler2D(m_sampler);
1819*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1820*35238bceSAndroid Build Coastguard Worker         {
1821*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1822*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1823*35238bceSAndroid Build Coastguard Worker             float w     = coords.component(2).asFloat(i);
1824*35238bceSAndroid Build Coastguard Worker             float l     = lod.component(0).asFloat(i);
1825*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s / w, t / w, l);
1826*35238bceSAndroid Build Coastguard Worker 
1827*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1828*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1829*35238bceSAndroid Build Coastguard Worker         }
1830*35238bceSAndroid Build Coastguard Worker         break;
1831*35238bceSAndroid Build Coastguard Worker     }
1832*35238bceSAndroid Build Coastguard Worker 
1833*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURECUBE:
1834*35238bceSAndroid Build Coastguard Worker     {
1835*35238bceSAndroid Build Coastguard Worker         const SamplerCube &tex = execCtx.getSamplerCube(m_sampler);
1836*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1837*35238bceSAndroid Build Coastguard Worker         {
1838*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1839*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1840*35238bceSAndroid Build Coastguard Worker             float r     = coords.component(2).asFloat(i);
1841*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s, t, r, 0.0f);
1842*35238bceSAndroid Build Coastguard Worker 
1843*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1844*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1845*35238bceSAndroid Build Coastguard Worker         }
1846*35238bceSAndroid Build Coastguard Worker         break;
1847*35238bceSAndroid Build Coastguard Worker     }
1848*35238bceSAndroid Build Coastguard Worker 
1849*35238bceSAndroid Build Coastguard Worker     case TYPE_TEXTURECUBE_LOD:
1850*35238bceSAndroid Build Coastguard Worker     {
1851*35238bceSAndroid Build Coastguard Worker         ExecConstValueAccess lod = m_lodBiasExpr->getValue();
1852*35238bceSAndroid Build Coastguard Worker         const SamplerCube &tex   = execCtx.getSamplerCube(m_sampler);
1853*35238bceSAndroid Build Coastguard Worker         for (int i = 0; i < EXEC_VEC_WIDTH; i++)
1854*35238bceSAndroid Build Coastguard Worker         {
1855*35238bceSAndroid Build Coastguard Worker             float s     = coords.component(0).asFloat(i);
1856*35238bceSAndroid Build Coastguard Worker             float t     = coords.component(1).asFloat(i);
1857*35238bceSAndroid Build Coastguard Worker             float r     = coords.component(2).asFloat(i);
1858*35238bceSAndroid Build Coastguard Worker             float l     = lod.component(0).asFloat(i);
1859*35238bceSAndroid Build Coastguard Worker             tcu::Vec4 p = tex.sample(s, t, r, l);
1860*35238bceSAndroid Build Coastguard Worker 
1861*35238bceSAndroid Build Coastguard Worker             for (int comp = 0; comp < 4; comp++)
1862*35238bceSAndroid Build Coastguard Worker                 dst.component(comp).asFloat(i) = p[comp];
1863*35238bceSAndroid Build Coastguard Worker         }
1864*35238bceSAndroid Build Coastguard Worker         break;
1865*35238bceSAndroid Build Coastguard Worker     }
1866*35238bceSAndroid Build Coastguard Worker 
1867*35238bceSAndroid Build Coastguard Worker     default:
1868*35238bceSAndroid Build Coastguard Worker         DE_ASSERT(false);
1869*35238bceSAndroid Build Coastguard Worker     }
1870*35238bceSAndroid Build Coastguard Worker }
1871*35238bceSAndroid Build Coastguard Worker 
1872*35238bceSAndroid Build Coastguard Worker } // namespace rsg
1873