1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Random Shader Generator
3 * ----------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 *//*--------------------------------------------------------------------*/
24 #include "rsgBinaryOps.hpp"
25 #include "rsgVariableManager.hpp"
26 #include "rsgUtils.hpp"
34 // CustomAbsOp and CustomBinaryOp are used to resolve float comparision corner case.
35 // This error happened when two floats with the same value were compared
36 // without using epsilon. If result of this comparisment influenced the
37 // output color then result and reference images could differ.
38 class CustomAbsOp : public Expression
42 virtual ~CustomAbsOp (void);
44 void setChild (Expression* expression);
45 Expression* createNextChild (GeneratorState& state);
46 void tokenize (GeneratorState& state, TokenStream& str) const;
48 void evaluate (ExecutionContext& execCtx);
49 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_type); }
52 std::string m_function;
54 ExecValueStorage m_value;
58 CustomAbsOp::CustomAbsOp (void)
60 , m_type (VariableType::TYPE_FLOAT, 1)
63 m_value.setStorage(m_type);
66 CustomAbsOp::~CustomAbsOp (void)
71 void CustomAbsOp::setChild(Expression* expression)
76 Expression* CustomAbsOp::createNextChild (GeneratorState&)
82 void CustomAbsOp::tokenize (GeneratorState& state, TokenStream& str) const
84 str << Token(m_function.c_str()) << Token::LEFT_PAREN;
85 m_child->tokenize(state, str);
86 str << Token::RIGHT_PAREN;
89 void CustomAbsOp::evaluate (ExecutionContext& execCtx)
91 m_child->evaluate(execCtx);
93 ExecConstValueAccess srcValue = m_child->getValue();
94 ExecValueAccess dstValue = m_value.getValue(m_type);
96 for (int elemNdx = 0; elemNdx < m_type.getNumElements(); elemNdx++)
98 ExecConstValueAccess srcComp = srcValue.component(elemNdx);
99 ExecValueAccess dstComp = dstValue.component(elemNdx);
101 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
102 dstComp.asFloat(compNdx) = deFloatAbs(srcComp.asFloat(compNdx));
106 typedef BinaryOp<5, ASSOCIATIVITY_LEFT> CustomBinaryBase;
108 // CustomBinaryOp and CustomAbsOp are used to resolve float comparision corner case.
109 // CustomBinaryOp supports addition and substraction as only those functionalities
111 template <typename ComputeValue>
112 class CustomBinaryOp: public CustomBinaryBase
116 virtual ~CustomBinaryOp (void) {}
118 void setLeftValue (Expression* expression);
119 void setRightValue (Expression* expression);
121 void evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b);
124 template <typename ComputeValue>
125 CustomBinaryOp<ComputeValue>::CustomBinaryOp ()
126 : CustomBinaryBase(Token::PLUS)
128 // By default add operation is assumed, for every other operation
129 // separate constructor specialization should be implemented
130 m_type = VariableType(VariableType::TYPE_FLOAT, 1);
131 m_value.setStorage(m_type);
135 CustomBinaryOp<EvaluateSub>::CustomBinaryOp ()
136 : CustomBinaryBase(Token::MINUS)
138 // Specialization for substraction
139 m_type = VariableType(VariableType::TYPE_FLOAT, 1);
140 m_leftValueRange = ValueRange(m_type);
141 m_rightValueRange = ValueRange(m_type);
142 m_value.setStorage(m_type);
146 CustomBinaryOp<EvaluateLessThan>::CustomBinaryOp ()
147 : CustomBinaryBase(Token::CMP_LT)
149 // Specialization for less_then comparision
150 m_type = VariableType(VariableType::TYPE_BOOL, 1);
151 VariableType floatType = VariableType(VariableType::TYPE_FLOAT, 1);
152 m_leftValueRange = ValueRange(floatType);
153 m_rightValueRange = ValueRange(floatType);
154 m_value.setStorage(m_type);
157 template <typename ComputeValue>
158 void CustomBinaryOp<ComputeValue>::setLeftValue(Expression* expression)
160 m_leftValueExpr = expression;
163 template <typename ComputeValue>
164 void CustomBinaryOp<ComputeValue>::setRightValue(Expression* expression)
166 m_rightValueExpr = expression;
169 template <typename ComputeValue>
170 void CustomBinaryOp<ComputeValue>::evaluate(ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b)
172 DE_ASSERT(dst.getType() == a.getType());
173 DE_ASSERT(dst.getType() == b.getType());
174 DE_ASSERT(dst.getType().getBaseType() == VariableType::TYPE_FLOAT);
176 for (int elemNdx = 0; elemNdx < dst.getType().getNumElements(); elemNdx++)
178 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
179 dst.component(elemNdx).asFloat(compNdx) = ComputeValue()(a.component(elemNdx).asFloat(compNdx),b.component(elemNdx).asFloat(compNdx));
184 void CustomBinaryOp<EvaluateLessThan>::evaluate(ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b)
186 DE_ASSERT(a.getType() == b.getType());
187 DE_ASSERT(dst.getType().getBaseType() == VariableType::TYPE_BOOL);
189 for (int elemNdx = 0; elemNdx < dst.getType().getNumElements(); elemNdx++)
191 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
192 dst.component(elemNdx).asBool(compNdx) = EvaluateLessThan()(a.component(elemNdx).asFloat(compNdx),b.component(elemNdx).asFloat(compNdx));
196 template <int Precedence, Associativity Assoc>
197 BinaryOp<Precedence, Assoc>::BinaryOp (Token::Type operatorToken)
198 : m_operator (operatorToken)
199 , m_leftValueRange (m_type)
200 , m_rightValueRange (m_type)
201 , m_leftValueExpr (DE_NULL)
202 , m_rightValueExpr (DE_NULL)
206 template <int Precedence, Associativity Assoc>
207 BinaryOp<Precedence, Assoc>::~BinaryOp (void)
209 delete m_leftValueExpr;
210 delete m_rightValueExpr;
213 template <int Precedence, Associativity Assoc>
214 Expression* BinaryOp<Precedence, Assoc>::createNextChild (GeneratorState& state)
216 int leftPrec = Assoc == ASSOCIATIVITY_LEFT ? Precedence : Precedence-1;
217 int rightPrec = Assoc == ASSOCIATIVITY_LEFT ? Precedence-1 : Precedence;
219 if (m_rightValueExpr == DE_NULL)
221 state.pushPrecedence(rightPrec);
222 m_rightValueExpr = Expression::createRandom(state, m_rightValueRange.asAccess());
223 state.popPrecedence();
224 return m_rightValueExpr;
226 else if (m_leftValueExpr == DE_NULL)
228 state.pushPrecedence(leftPrec);
229 m_leftValueExpr = Expression::createRandom(state, m_leftValueRange.asAccess());
230 state.popPrecedence();
231 return m_leftValueExpr;
235 // Check for corrner cases
240 // When comparing two floats epsilon should be included
241 // to eliminate the risk that we get different results
242 // because of precission error
243 VariableType floatType(VariableType::TYPE_FLOAT, 1);
244 if (m_rightValueRange.getType() == floatType)
246 FloatLiteral* epsilonLiteral = new FloatLiteral(0.001f);
248 typedef CustomBinaryOp<EvaluateAdd> CustomAddOp;
249 CustomAddOp* addOperation = new CustomAddOp();
250 addOperation->setLeftValue(m_rightValueExpr);
251 addOperation->setRightValue(epsilonLiteral);
253 // add epsilon to right-hand side
254 m_rightValueExpr = addOperation;
260 // When comparing two floats epsilon should be included
261 // to eliminate the risk that we get different results
262 // because of precission error
263 VariableType floatType(VariableType::TYPE_FLOAT, 1);
264 if (m_leftValueRange.getType() == floatType)
266 FloatLiteral* epsilonLiteral = new FloatLiteral(0.001f);
268 typedef CustomBinaryOp<EvaluateAdd> CustomAddOp;
269 CustomAddOp* addOperation = new CustomAddOp();
270 addOperation->setLeftValue(m_leftValueExpr);
271 addOperation->setRightValue(epsilonLiteral);
273 // add epsilon to left-hand side
274 m_leftValueExpr = addOperation;
280 // When comparing two floats epsilon should be included
281 // to eliminate the risk that we get different results
282 // because of precission error
283 VariableType floatType(VariableType::TYPE_FLOAT, 1);
284 if (m_leftValueRange.getType() == floatType)
286 VariableType boolType(VariableType::TYPE_BOOL, 1);
287 const ValueRange boolRange(boolType);
289 ParenOp* parenRight = new ParenOp(state, boolRange);
290 parenRight->setChild(m_rightValueExpr);
292 typedef CustomBinaryOp<EvaluateSub> CustomSubOp;
293 CustomSubOp* subOperation = new CustomSubOp();
294 subOperation->setLeftValue(m_leftValueExpr);
295 subOperation->setRightValue(parenRight);
297 CustomAbsOp* absOperation = new CustomAbsOp();
298 absOperation->setChild(subOperation);
299 FloatLiteral* epsilonLiteral = new FloatLiteral(0.001f);
301 typedef CustomBinaryOp<EvaluateLessThan> CustomLessThanOp;
302 CustomLessThanOp* lessOperation = new CustomLessThanOp();
303 lessOperation->setLeftValue(absOperation);
304 lessOperation->setRightValue(epsilonLiteral);
306 ParenOp* parenOperation = new ParenOp(state, boolRange);
307 parenOperation->setChild(lessOperation);
308 BoolLiteral* trueLiteral = new BoolLiteral(true);
310 // EQ operation cant be removed so it is replaced with:
311 // ((abs(lhs-rhs) < epsilon) == true).
312 m_leftValueExpr = parenOperation;
313 m_rightValueExpr = trueLiteral;
325 template <int Precedence, Associativity Assoc>
326 float BinaryOp<Precedence, Assoc>::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
328 if (state.getPrecedence() < Precedence)
331 int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
333 if (valueRange.getType().isVoid())
334 return availableLevels >= 2 ? unusedValueWeight : 0.0f;
336 if (availableLevels < getConservativeValueExprDepth(state, valueRange) + 1)
342 template <int Precedence, Associativity Assoc>
343 void BinaryOp<Precedence, Assoc>::tokenize (GeneratorState& state, TokenStream& str) const
345 m_leftValueExpr->tokenize(state, str);
347 m_rightValueExpr->tokenize(state, str);
350 template <int Precedence, Associativity Assoc>
351 void BinaryOp<Precedence, Assoc>::evaluate (ExecutionContext& execCtx)
353 m_leftValueExpr->evaluate(execCtx);
354 m_rightValueExpr->evaluate(execCtx);
356 ExecConstValueAccess leftVal = m_leftValueExpr->getValue();
357 ExecConstValueAccess rightVal = m_rightValueExpr->getValue();
358 ExecValueAccess dst = m_value.getValue(m_type);
360 evaluate(dst, leftVal, rightVal);
363 template <int Precedence, bool Float, bool Int, bool Bool, class ComputeValueRange, class EvaluateComp>
364 BinaryVecOp<Precedence, Float, Int, Bool, ComputeValueRange, EvaluateComp>::BinaryVecOp (GeneratorState& state, Token::Type operatorToken, ConstValueRangeAccess inValueRange)
365 : BinaryOp<Precedence, ASSOCIATIVITY_LEFT>(operatorToken)
367 ValueRange valueRange = inValueRange;
369 if (valueRange.getType().isVoid())
371 int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
372 vector<VariableType::Type> baseTypes;
374 if (Float) baseTypes.push_back(VariableType::TYPE_FLOAT);
375 if (Int) baseTypes.push_back(VariableType::TYPE_INT);
376 if (Bool) baseTypes.push_back(VariableType::TYPE_BOOL);
378 VariableType::Type baseType = state.getRandom().choose<VariableType::Type>(baseTypes.begin(), baseTypes.end());
379 int numElements = state.getRandom().getInt(1, availableLevels >= 3 ? 4 : 1);
381 valueRange = ValueRange(VariableType(baseType, numElements));
382 computeRandomValueRange(state, valueRange.asAccess());
385 // Choose type, allocate storage for execution
386 this->m_type = valueRange.getType();
387 this->m_value.setStorage(this->m_type);
389 // Initialize storage for value ranges
390 this->m_rightValueRange = ValueRange(this->m_type);
391 this->m_leftValueRange = ValueRange(this->m_type);
393 VariableType::Type baseType = this->m_type.getBaseType();
395 // Compute range for b that satisfies requested value range
396 for (int elemNdx = 0; elemNdx < this->m_type.getNumElements(); elemNdx++)
398 ConstValueRangeAccess dst = valueRange.asAccess().component(elemNdx);
399 ValueRangeAccess a = this->m_leftValueRange.asAccess().component(elemNdx); // \todo [2011-03-25 pyry] Commutative: randomize inputs
400 ValueRangeAccess b = this->m_rightValueRange.asAccess().component(elemNdx);
402 // Just pass undefined ranges
403 if ((baseType == VariableType::TYPE_FLOAT || baseType == VariableType::TYPE_INT) && isUndefinedValueRange(dst))
405 a.getMin() = dst.getMin().value();
406 b.getMin() = dst.getMin().value();
407 a.getMax() = dst.getMax().value();
408 b.getMax() = dst.getMax().value();
412 if (baseType == VariableType::TYPE_FLOAT)
413 ComputeValueRange()(state.getRandom(), dst.getMin().asFloat(), dst.getMax().asFloat(),
414 a.getMin().asFloat(), a.getMax().asFloat(),
415 b.getMin().asFloat(), b.getMax().asFloat());
416 else if (baseType == VariableType::TYPE_INT)
417 ComputeValueRange()(state.getRandom(), dst.getMin().asInt(), dst.getMax().asInt(),
418 a.getMin().asInt(), a.getMax().asInt(),
419 b.getMin().asInt(), b.getMax().asInt());
422 DE_ASSERT(baseType == VariableType::TYPE_BOOL);
423 ComputeValueRange()(state.getRandom(), dst.getMin().asBool(), dst.getMax().asBool(),
424 a.getMin().asBool(), a.getMax().asBool(),
425 b.getMin().asBool(), b.getMax().asBool());
430 template <int Precedence, bool Float, bool Int, bool Bool, class ComputeValueRange, class EvaluateComp>
431 BinaryVecOp<Precedence, Float, Int, Bool, ComputeValueRange, EvaluateComp>::~BinaryVecOp (void)
435 template <int Precedence, bool Float, bool Int, bool Bool, class ComputeValueRange, class EvaluateComp>
436 void BinaryVecOp<Precedence, Float, Int, Bool, ComputeValueRange, EvaluateComp>::evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b)
438 DE_ASSERT(dst.getType() == a.getType());
439 DE_ASSERT(dst.getType() == b.getType());
440 switch (dst.getType().getBaseType())
442 case VariableType::TYPE_FLOAT:
443 for (int elemNdx = 0; elemNdx < dst.getType().getNumElements(); elemNdx++)
445 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
446 dst.component(elemNdx).asFloat(compNdx) = EvaluateComp()(a.component(elemNdx).asFloat(compNdx), b.component(elemNdx).asFloat(compNdx));
450 case VariableType::TYPE_INT:
451 for (int elemNdx = 0; elemNdx < dst.getType().getNumElements(); elemNdx++)
453 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
454 dst.component(elemNdx).asInt(compNdx) = EvaluateComp()(a.component(elemNdx).asInt(compNdx), b.component(elemNdx).asInt(compNdx));
459 DE_ASSERT(DE_FALSE); // Invalid type for multiplication
463 void ComputeMulRange::operator() (de::Random& rnd, float dstMin, float dstMax, float& aMin, float& aMax, float& bMin, float& bMax) const
465 const float minScale = 0.25f;
466 const float maxScale = 2.0f;
467 const float subRangeStep = 0.25f;
468 const float scaleStep = 0.25f;
470 float scale = getQuantizedFloat(rnd, minScale, maxScale, scaleStep);
471 float scaledMin = dstMin/scale;
472 float scaledMax = dstMax/scale;
474 // Quantize scaled value range if possible
475 if (!quantizeFloatRange(scaledMin, scaledMax))
477 // Fall back to 1.0 as a scale
483 float subRangeLen = getQuantizedFloat(rnd, 0.0f, scaledMax-scaledMin, subRangeStep);
484 aMin = scaledMin + getQuantizedFloat(rnd, 0.0f, (scaledMax-scaledMin)-subRangeLen, subRangeStep);
485 aMax = aMin + subRangeLen;
490 for (int i = 0; i < 5; i++)
492 if (de::inBounds(aMin*(scale-(float)i*scaleStep), dstMin, dstMax) &&
493 de::inBounds(aMax*(scale-(float)i*scaleStep), dstMin, dstMax))
494 bMin = scale-(float)i*scaleStep;
496 if (de::inBounds(aMin*(scale+(float)i*scaleStep), dstMin, dstMax) &&
497 de::inBounds(aMax*(scale+(float)i*scaleStep), dstMin, dstMax))
498 bMax = scale+(float)i*scaleStep;
504 std::swap(aMin, aMax);
505 std::swap(bMin, bMax);
512 #if defined(DE_DEBUG)
513 const float eps = 0.001f;
514 DE_ASSERT(aMin <= aMax && bMin <= bMax);
515 DE_ASSERT(de::inRange(aMin*bMin, dstMin-eps, dstMax+eps));
516 DE_ASSERT(de::inRange(aMin*bMax, dstMin-eps, dstMax+eps));
517 DE_ASSERT(de::inRange(aMax*bMin, dstMin-eps, dstMax+eps));
518 DE_ASSERT(de::inRange(aMax*bMax, dstMin-eps, dstMax+eps));
522 void ComputeMulRange::operator() (de::Random& rnd, int dstMin, int dstMax, int& aMin, int& aMax, int& bMin, int& bMax) const
531 MulOp::MulOp (GeneratorState& state, ConstValueRangeAccess valueRange)
532 : MulBase(state, Token::MUL, valueRange)
536 float MulOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
538 if (valueRange.getType().isVoid() ||
539 valueRange.getType().isFloatOrVec() ||
540 valueRange.getType().isIntOrVec())
541 return MulBase::getWeight(state, valueRange);
546 template <typename T>
547 void ComputeAddRange::operator() (de::Random& random, T dstMin, T dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
551 int operator() (de::Random& rnd, int min, int max) const { return rnd.getInt(min, max); }
552 float operator() (de::Random& rnd, float min, float max) const { return getQuantizedFloat(rnd, min, max, 0.5f); }
555 T rangeLen = dstMax-dstMin;
556 T subRangeLen = GetRandom()(random, T(0), rangeLen);
557 T aOffset = GetRandom()(random, T(-8), T(8));
559 aMin = dstMin+aOffset;
560 aMax = aMin+subRangeLen;
563 bMax = -aOffset+(rangeLen-subRangeLen);
565 #if defined(DE_DEBUG)
567 DE_ASSERT(aMin <= aMax && bMin <= bMax);
568 DE_ASSERT(de::inRange(aMin+bMin, dstMin-eps, dstMax+eps));
569 DE_ASSERT(de::inRange(aMin+bMax, dstMin-eps, dstMax+eps));
570 DE_ASSERT(de::inRange(aMax+bMin, dstMin-eps, dstMax+eps));
571 DE_ASSERT(de::inRange(aMax+bMax, dstMin-eps, dstMax+eps));
576 void ComputeAddRange::operator()<bool> (de::Random&, bool, bool, bool&, bool&, bool&, bool&) const
581 AddOp::AddOp (GeneratorState& state, ConstValueRangeAccess valueRange)
582 : AddBase(state, Token::PLUS, valueRange)
586 float AddOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
588 if (valueRange.getType().isVoid() ||
589 valueRange.getType().isFloatOrVec() ||
590 valueRange.getType().isIntOrVec())
591 return AddBase::getWeight(state, valueRange);
596 template <typename T>
597 void ComputeSubRange::operator() (de::Random& random, T dstMin, T dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
601 int operator() (de::Random& rnd, int min, int max) const { return rnd.getInt(min, max); }
602 float operator() (de::Random& rnd, float min, float max) const { return getQuantizedFloat(rnd, min, max, 0.5f); }
605 T rangeLen = dstMax-dstMin;
606 T subRangeLen = GetRandom()(random, T(0), rangeLen);
607 T aOffset = GetRandom()(random, T(-8), T(8));
609 aMin = dstMin+aOffset;
610 aMax = aMin+subRangeLen;
612 bMin = aOffset-(rangeLen-subRangeLen);
615 #if defined(DE_DEBUG)
617 DE_ASSERT(aMin <= aMax && bMin <= bMax);
618 DE_ASSERT(de::inRange(aMin-bMin, dstMin-eps, dstMax+eps));
619 DE_ASSERT(de::inRange(aMin-bMax, dstMin-eps, dstMax+eps));
620 DE_ASSERT(de::inRange(aMax-bMin, dstMin-eps, dstMax+eps));
621 DE_ASSERT(de::inRange(aMax-bMax, dstMin-eps, dstMax+eps));
626 void ComputeSubRange::operator()<bool> (de::Random&, bool, bool, bool&, bool&, bool&, bool&) const
631 SubOp::SubOp (GeneratorState& state, ConstValueRangeAccess valueRange)
632 : SubBase(state, Token::MINUS, valueRange)
636 float SubOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
638 if (valueRange.getType().isVoid() ||
639 valueRange.getType().isFloatOrVec() ||
640 valueRange.getType().isIntOrVec())
641 return SubBase::getWeight(state, valueRange);
646 template <class ComputeValueRange, class EvaluateComp>
647 RelationalOp<ComputeValueRange, EvaluateComp>::RelationalOp (GeneratorState& state, Token::Type operatorToken, ConstValueRangeAccess inValueRange)
648 : BinaryOp<7, ASSOCIATIVITY_LEFT>(operatorToken)
650 ValueRange valueRange = inValueRange;
652 if (valueRange.getType().isVoid())
654 valueRange = ValueRange(VariableType(VariableType::TYPE_BOOL, 1));
655 computeRandomValueRange(state, valueRange.asAccess());
658 // Choose type, allocate storage for execution
659 this->m_type = valueRange.getType();
660 this->m_value.setStorage(this->m_type);
662 // Choose random input type
663 VariableType::Type inBaseTypes[] = { VariableType::TYPE_FLOAT, VariableType::TYPE_INT };
664 VariableType::Type inBaseType = state.getRandom().choose<VariableType::Type>(&inBaseTypes[0], &inBaseTypes[DE_LENGTH_OF_ARRAY(inBaseTypes)]);
666 // Initialize storage for input value ranges
667 this->m_rightValueRange = ValueRange(VariableType(inBaseType, 1));
668 this->m_leftValueRange = ValueRange(VariableType(inBaseType, 1));
670 // Compute range for b that satisfies requested value range
672 bool dstMin = valueRange.getMin().asBool();
673 bool dstMax = valueRange.getMax().asBool();
674 ValueRangeAccess a = this->m_leftValueRange.asAccess();
675 ValueRangeAccess b = this->m_rightValueRange.asAccess();
677 if (inBaseType == VariableType::TYPE_FLOAT)
678 ComputeValueRange()(state.getRandom(), dstMin, dstMax,
679 a.getMin().asFloat(), a.getMax().asFloat(),
680 b.getMin().asFloat(), b.getMax().asFloat());
681 else if (inBaseType == VariableType::TYPE_INT)
682 ComputeValueRange()(state.getRandom(), dstMin, dstMax,
683 a.getMin().asInt(), a.getMax().asInt(),
684 b.getMin().asInt(), b.getMax().asInt());
688 template <class ComputeValueRange, class EvaluateComp>
689 RelationalOp<ComputeValueRange, EvaluateComp>::~RelationalOp (void)
693 template <class ComputeValueRange, class EvaluateComp>
694 void RelationalOp<ComputeValueRange, EvaluateComp>::evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b)
696 DE_ASSERT(a.getType() == b.getType());
697 switch (a.getType().getBaseType())
699 case VariableType::TYPE_FLOAT:
700 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
701 dst.asBool(compNdx) = EvaluateComp()(a.asFloat(compNdx), b.asFloat(compNdx));
704 case VariableType::TYPE_INT:
705 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
706 dst.asBool(compNdx) = EvaluateComp()(a.asInt(compNdx), b.asInt(compNdx));
714 template <class ComputeValueRange, class EvaluateComp>
715 float RelationalOp<ComputeValueRange, EvaluateComp>::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
717 if (!state.getProgramParameters().useComparisonOps)
720 if (valueRange.getType().isVoid() ||
721 (valueRange.getType().getBaseType() == VariableType::TYPE_BOOL && valueRange.getType().getNumElements() == 1))
722 return BinaryOp<7, ASSOCIATIVITY_LEFT>::getWeight(state, valueRange);
730 template <typename T> T getStep (void);
731 template <> inline float getStep (void) { return 0.25f; }
732 template <> inline int getStep (void) { return 1; }
736 template <typename T>
737 void ComputeLessThanRange::operator () (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
741 int operator() (de::Random& random, int min, int max) const { return random.getInt(min, max); }
742 float operator() (de::Random& random, float min, float max) const { return getQuantizedFloat(random, min, max, getStep<float>()); }
746 T rLen = GetRandom()(rnd, T(0), T(8));
747 T rMin = GetRandom()(rnd, T(-4), T(4));
750 if (dstMin == false && dstMax == true)
752 // Both values are possible, use same range for both inputs
758 else if (dstMin == true && dstMax == true)
760 // Compute range that is less than rMin..rMax
761 T aLen = GetRandom()(rnd, T(0), T(8)-rLen);
763 aMax = rMin - getStep<T>();
771 // Compute range that is greater than or equal to rMin..rMax
772 T aLen = GetRandom()(rnd, T(0), T(8)-rLen);
782 LessThanOp::LessThanOp (GeneratorState& state, ConstValueRangeAccess valueRange)
783 : LessThanBase(state, Token::CMP_LT, valueRange)
787 float LessThanOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
789 return LessThanBase::getWeight(state, valueRange);
792 template <typename T>
793 void ComputeLessOrEqualRange::operator () (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
797 int operator() (de::Random& random, int min, int max) const { return random.getInt(min, max); }
798 float operator() (de::Random& random, float min, float max) const { return getQuantizedFloat(random, min, max, getStep<float>()); }
802 T rLen = GetRandom()(rnd, T(0), T(8));
803 T rMin = GetRandom()(rnd, T(-4), T(4));
806 if (dstMin == false && dstMax == true)
808 // Both values are possible, use same range for both inputs
814 else if (dstMin == true && dstMax == true)
816 // Compute range that is less than or equal to rMin..rMax
817 T aLen = GetRandom()(rnd, T(0), T(8)-rLen);
827 // Compute range that is greater than rMin..rMax
828 T aLen = GetRandom()(rnd, T(0), T(8)-rLen);
830 aMin = rMax + getStep<T>();
838 LessOrEqualOp::LessOrEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange)
839 : LessOrEqualBase(state, Token::CMP_LE, valueRange)
843 float LessOrEqualOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
845 return LessOrEqualBase::getWeight(state, valueRange);
848 GreaterThanOp::GreaterThanOp (GeneratorState& state, ConstValueRangeAccess valueRange)
849 : GreaterThanBase(state, Token::CMP_GT, valueRange)
853 float GreaterThanOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
855 return GreaterThanBase::getWeight(state, valueRange);
858 GreaterOrEqualOp::GreaterOrEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange)
859 : GreaterOrEqualBase(state, Token::CMP_GE, valueRange)
863 float GreaterOrEqualOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
865 return GreaterOrEqualBase::getWeight(state, valueRange);
871 template <bool IsEqual, typename T>
872 void computeEqualityValueRange (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax)
874 if (dstMin == false && dstMax == true)
875 ComputeLessThanRange()(rnd, false, true, aMin, aMax, bMin, bMax);
876 else if (IsEqual && dstMin == false)
877 ComputeLessThanRange()(rnd, true, true, aMin, aMax, bMin, bMax);
878 else if (!IsEqual && dstMin == true)
879 ComputeLessThanRange()(rnd, true, true, aMin, aMax, bMin, bMax);
882 // Must have exactly same values.
885 int operator() (de::Random& random, int min, int max) const { return random.getInt(min, max); }
886 float operator() (de::Random& random, float min, float max) const { return getQuantizedFloat(random, min, max, 0.5f); }
889 T val = GetRandom()(rnd, T(-1), T(1));
899 void computeEqualityValueRange<true, bool> (de::Random& rnd, bool dstMin, bool dstMax, bool& aMin, bool& aMax, bool& bMin, bool& bMax)
901 if (dstMin == false && dstMax == true)
908 else if (dstMin == false)
910 DE_ASSERT(dstMax == false);
911 bool val = rnd.getBool();
920 DE_ASSERT(dstMin == true && dstMax == true);
921 bool val = rnd.getBool();
931 void computeEqualityValueRange<false, bool> (de::Random& rnd, bool dstMin, bool dstMax, bool& aMin, bool& aMax, bool& bMin, bool& bMax)
933 if (dstMin == false && dstMax == true)
934 computeEqualityValueRange<true>(rnd, dstMin, dstMax, aMin, aMax, bMin, bMax);
936 computeEqualityValueRange<true>(rnd, !dstMin, !dstMax, aMin, aMax, bMin, bMax);
941 template <bool IsEqual>
942 EqualityComparisonOp<IsEqual>::EqualityComparisonOp (GeneratorState& state, ConstValueRangeAccess inValueRange)
943 : BinaryOp<8, ASSOCIATIVITY_LEFT>(IsEqual ? Token::CMP_EQ : Token::CMP_NE)
945 ValueRange valueRange = inValueRange;
947 if (valueRange.getType().isVoid())
949 valueRange = ValueRange(VariableType(VariableType::TYPE_BOOL, 1));
950 computeRandomValueRange(state, valueRange.asAccess());
953 // Choose type, allocate storage for execution
954 this->m_type = valueRange.getType();
955 this->m_value.setStorage(this->m_type);
957 // Choose random input type
958 VariableType::Type inBaseTypes[] = { VariableType::TYPE_FLOAT, VariableType::TYPE_INT };
959 VariableType::Type inBaseType = state.getRandom().choose<VariableType::Type>(&inBaseTypes[0], &inBaseTypes[DE_LENGTH_OF_ARRAY(inBaseTypes)]);
960 int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
961 int numElements = state.getRandom().getInt(1, availableLevels >= 3 ? 4 : 1);
963 // Initialize storage for input value ranges
964 this->m_rightValueRange = ValueRange(VariableType(inBaseType, numElements));
965 this->m_leftValueRange = ValueRange(VariableType(inBaseType, numElements));
967 // Compute range for b that satisfies requested value range
968 for (int elementNdx = 0; elementNdx < numElements; elementNdx++)
970 bool dstMin = valueRange.getMin().asBool();
971 bool dstMax = valueRange.getMax().asBool();
973 ValueRangeAccess a = this->m_leftValueRange.asAccess().component(elementNdx);
974 ValueRangeAccess b = this->m_rightValueRange.asAccess().component(elementNdx);
976 if (inBaseType == VariableType::TYPE_FLOAT)
977 computeEqualityValueRange<IsEqual>(state.getRandom(), dstMin, dstMax,
978 a.getMin().asFloat(), a.getMax().asFloat(),
979 b.getMin().asFloat(), b.getMax().asFloat());
980 else if (inBaseType == VariableType::TYPE_INT)
981 computeEqualityValueRange<IsEqual>(state.getRandom(), dstMin, dstMax,
982 a.getMin().asInt(), a.getMax().asInt(),
983 b.getMin().asInt(), b.getMax().asInt());
986 DE_ASSERT(inBaseType == VariableType::TYPE_BOOL);
987 computeEqualityValueRange<IsEqual>(state.getRandom(), dstMin, dstMax,
988 a.getMin().asBool(), a.getMax().asBool(),
989 b.getMin().asBool(), b.getMax().asBool());
994 template <bool IsEqual>
995 float EqualityComparisonOp<IsEqual>::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
997 if (!state.getProgramParameters().useComparisonOps)
1000 // \todo [2011-06-13 pyry] Weight down cases that would force constant inputs.
1002 if (valueRange.getType().isVoid() ||
1003 (valueRange.getType().getBaseType() == VariableType::TYPE_BOOL && valueRange.getType().getNumElements() == 1))
1004 return BinaryOp<8, ASSOCIATIVITY_LEFT>::getWeight(state, valueRange);
1012 template <bool IsEqual>
1013 struct EqualityCompare
1015 template <typename T>
1016 static bool compare (T a, T b);
1017 static bool combine (bool a, bool b);
1021 template <typename T>
1022 inline bool EqualityCompare<true>::compare (T a, T b) { return a == b; }
1025 inline bool EqualityCompare<true>::combine (bool a, bool b) { return a && b; }
1028 template <typename T>
1029 inline bool EqualityCompare<false>::compare (T a, T b) { return a != b; }
1032 inline bool EqualityCompare<false>::combine (bool a, bool b) { return a || b; }
1036 template <bool IsEqual>
1037 void EqualityComparisonOp<IsEqual>::evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b)
1039 DE_ASSERT(a.getType() == b.getType());
1042 switch (a.getType().getBaseType())
1044 case VariableType::TYPE_FLOAT:
1045 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
1047 bool result = IsEqual ? true : false;
1049 for (int elemNdx = 0; elemNdx < a.getType().getNumElements(); elemNdx++)
1050 result = EqualityCompare<IsEqual>::combine(result, EqualityCompare<IsEqual>::compare(a.component(elemNdx).asFloat(compNdx), b.component(elemNdx).asFloat(compNdx)));
1052 dst.asBool(compNdx) = result;
1056 case VariableType::TYPE_INT:
1057 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
1059 bool result = IsEqual ? true : false;
1061 for (int elemNdx = 0; elemNdx < a.getType().getNumElements(); elemNdx++)
1062 result = EqualityCompare<IsEqual>::combine(result, EqualityCompare<IsEqual>::compare(a.component(elemNdx).asInt(compNdx), b.component(elemNdx).asInt(compNdx)));
1064 dst.asBool(compNdx) = result;
1068 case VariableType::TYPE_BOOL:
1069 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
1071 bool result = IsEqual ? true : false;
1073 for (int elemNdx = 0; elemNdx < a.getType().getNumElements(); elemNdx++)
1074 result = EqualityCompare<IsEqual>::combine(result, EqualityCompare<IsEqual>::compare(a.component(elemNdx).asBool(compNdx), b.component(elemNdx).asBool(compNdx)));
1076 dst.asBool(compNdx) = result;
1081 DE_ASSERT(DE_FALSE);
1085 EqualOp::EqualOp (GeneratorState& state, ConstValueRangeAccess valueRange)
1086 : EqualityComparisonOp<true>(state, valueRange)
1090 float EqualOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
1092 return EqualityComparisonOp<true>::getWeight(state, valueRange);
1095 NotEqualOp::NotEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange)
1096 : EqualityComparisonOp<false>(state, valueRange)
1100 float NotEqualOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
1102 return EqualityComparisonOp<false>::getWeight(state, valueRange);