1 #ifndef _RSGBUILTINFUNCTIONS_HPP
2 #define _RSGBUILTINFUNCTIONS_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Random Shader Generator
5 * ----------------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Built-in Functions.
24 *//*--------------------------------------------------------------------*/
26 #include "rsgDefs.hpp"
27 #include "rsgExpression.hpp"
28 #include "rsgUtils.hpp"
34 // Template for built-in functions with form "GenType func(GenType val)".
35 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
36 class UnaryBuiltinVecFunc : public Expression
39 UnaryBuiltinVecFunc (GeneratorState& state, const char* function, ConstValueRangeAccess valueRange);
40 virtual ~UnaryBuiltinVecFunc (void);
42 Expression* createNextChild (GeneratorState& state);
43 void tokenize (GeneratorState& state, TokenStream& str) const;
45 void evaluate (ExecutionContext& execCtx);
46 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_inValueRange.getType()); }
48 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
51 std::string m_function;
52 ValueRange m_inValueRange;
53 ExecValueStorage m_value;
57 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
58 UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::UnaryBuiltinVecFunc (GeneratorState& state, const char* function, ConstValueRangeAccess valueRange)
59 : m_function (function)
60 , m_inValueRange (valueRange.getType())
64 DE_ASSERT(valueRange.getType().isFloatOrVec());
66 m_value.setStorage(valueRange.getType());
68 // Compute input value range
69 for (int ndx = 0; ndx < m_inValueRange.getType().getNumElements(); ndx++)
71 ConstValueRangeAccess outRange = valueRange.component(ndx);
72 ValueRangeAccess inRange = m_inValueRange.asAccess().component(ndx);
74 ComputeValueRange()(outRange.getMin().asFloat(), outRange.getMax().asFloat(), inRange.getMin().asFloat(), inRange.getMax().asFloat());
78 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
79 UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::~UnaryBuiltinVecFunc (void)
84 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
85 Expression* UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::createNextChild (GeneratorState& state)
90 m_child = Expression::createRandom(state, m_inValueRange.asAccess());
94 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
95 void UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::tokenize (GeneratorState& state, TokenStream& str) const
97 str << Token(m_function.c_str()) << Token::LEFT_PAREN;
98 m_child->tokenize(state, str);
99 str << Token::RIGHT_PAREN;
102 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
103 void UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::evaluate (ExecutionContext& execCtx)
105 m_child->evaluate(execCtx);
107 ExecConstValueAccess srcValue = m_child->getValue();
108 ExecValueAccess dstValue = m_value.getValue(m_inValueRange.getType());
110 for (int elemNdx = 0; elemNdx < m_inValueRange.getType().getNumElements(); elemNdx++)
112 ExecConstValueAccess srcComp = srcValue.component(elemNdx);
113 ExecValueAccess dstComp = dstValue.component(elemNdx);
115 for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
116 dstComp.asFloat(compNdx) = Evaluate()(srcComp.asFloat(compNdx));
120 template <class GetValueRangeWeight, class ComputeValueRange, class Evaluate>
121 float UnaryBuiltinVecFunc<GetValueRangeWeight, ComputeValueRange, Evaluate>::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
123 // \todo [2011-06-14 pyry] Void support?
124 if (!valueRange.getType().isFloatOrVec())
127 int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
129 if (availableLevels < getConservativeValueExprDepth(state, valueRange) + 1)
132 // Compute value range weight
133 float combinedWeight = 1.0f;
134 for (int elemNdx = 0; elemNdx < valueRange.getType().getNumElements(); elemNdx++)
136 float elemWeight = GetValueRangeWeight()(valueRange.component(elemNdx).getMin().asFloat(), valueRange.component(elemNdx).getMax().asFloat());
137 combinedWeight *= elemWeight;
140 return combinedWeight;
145 struct GetUnaryBuiltinVecWeight
147 inline float operator() (float outMin, float outMax) const { return C::getCompWeight(outMin, outMax); }
151 struct ComputeUnaryBuiltinVecRange
153 inline void operator() (float outMin, float outMax, float& inMin, float& inMax) const { C::computeValueRange(outMin, outMax, inMin, inMax); }
157 struct EvaluateUnaryBuiltinVec
159 inline float operator() (float inVal) const { return C::evaluateComp(inVal); }
163 class UnaryBuiltinVecTemplateProxy : public UnaryBuiltinVecFunc<GetUnaryBuiltinVecWeight<C>, ComputeUnaryBuiltinVecRange<C>, EvaluateUnaryBuiltinVec<C> >
166 UnaryBuiltinVecTemplateProxy (GeneratorState& state, const char* function, ConstValueRangeAccess valueRange)
167 : UnaryBuiltinVecFunc<GetUnaryBuiltinVecWeight<C>, ComputeUnaryBuiltinVecRange<C>, EvaluateUnaryBuiltinVec<C> >(state, function, valueRange)
172 // Template for trigonometric function group.
174 class UnaryTrigonometricFunc : public UnaryBuiltinVecTemplateProxy<C>
177 UnaryTrigonometricFunc (GeneratorState& state, const char* function, ConstValueRangeAccess valueRange)
178 : UnaryBuiltinVecTemplateProxy<C>(state, function, valueRange)
182 static inline float getCompWeight (float outMin, float outMax)
184 if (Scalar::min<float>() == outMin || Scalar::max<float>() == outMax)
185 return 1.0f; // Infinite value range, anything goes
189 if (!C::transformValueRange(outMin, outMax, inMin, inMax))
190 return 0.0f; // Not possible to transform value range (out of range perhaps)
193 if (!quantizeFloatRange(inMin, inMax))
194 return 0.0f; // Not possible to quantize - would cause accuracy issues
196 if (outMin == outMax)
197 return 1.0f; // Constant value and passed quantization
199 // Evaluate new intersection
200 float intersectionLen = C::evaluateComp(inMax) - C::evaluateComp(inMin);
201 float valRangeLen = outMax - outMin;
203 return deFloatMax(0.1f, intersectionLen/valRangeLen);
206 static inline void computeValueRange (float outMin, float outMax, float& inMin, float& inMax)
208 DE_VERIFY(C::transformValueRange(outMin, outMax, inMin, inMax));
209 DE_VERIFY(quantizeFloatRange(inMin, inMax));
210 DE_ASSERT(inMin <= inMax);
213 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
215 if (state.getProgramParameters().trigonometricBaseWeight <= 0.0f)
218 return UnaryBuiltinVecTemplateProxy<C>::getWeight(state, valueRange) * state.getProgramParameters().trigonometricBaseWeight;
222 class SinOp : public UnaryTrigonometricFunc<SinOp>
225 SinOp (GeneratorState& state, ConstValueRangeAccess valueRange)
226 : UnaryTrigonometricFunc<SinOp>(state, "sin", valueRange)
230 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
232 if (outMax < -1.0f || outMin > 1.0f)
235 inMin = (outMin >= -1.0f) ? deFloatAsin(outMin) : -0.5f*DE_PI;
236 inMax = (outMax <= +1.0f) ? deFloatAsin(outMax) : +0.5f*DE_PI;
241 static inline float evaluateComp (float inVal)
243 return deFloatSin(inVal);
247 class CosOp : public UnaryTrigonometricFunc<CosOp>
250 CosOp (GeneratorState& state, ConstValueRangeAccess valueRange)
251 : UnaryTrigonometricFunc<CosOp>(state, "cos", valueRange)
255 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
257 if (outMax < -1.0f || outMin > 1.0f)
260 inMax = (outMin >= -1.0f) ? deFloatAcos(outMin) : +DE_PI;
261 inMin = (outMax <= +1.0f) ? deFloatAcos(outMax) : -DE_PI;
266 static inline float evaluateComp (float inVal)
268 return deFloatCos(inVal);
272 class TanOp : public UnaryTrigonometricFunc<TanOp>
275 TanOp (GeneratorState& state, ConstValueRangeAccess valueRange)
276 : UnaryTrigonometricFunc<TanOp>(state, "tan", valueRange)
280 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
282 // \note Currently tan() is limited to -4..4 range. Otherwise we will run into accuracy issues
283 const float rangeMin = -4.0f;
284 const float rangeMax = +4.0f;
286 if (outMax < rangeMin || outMin > rangeMax)
289 inMin = deFloatAtanOver(deFloatMax(outMin, rangeMin));
290 inMax = deFloatAtanOver(deFloatMin(outMax, rangeMax));
295 static inline float evaluateComp (float inVal)
297 return deFloatTan(inVal);
301 class AsinOp : public UnaryTrigonometricFunc<AsinOp>
304 AsinOp (GeneratorState& state, ConstValueRangeAccess valueRange)
305 : UnaryTrigonometricFunc<AsinOp>(state, "asin", valueRange)
309 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
311 const float rangeMin = -DE_PI/2.0f;
312 const float rangeMax = +DE_PI/2.0f;
314 if (outMax < rangeMin || outMin > rangeMax)
315 return false; // Out of range
317 inMin = deFloatSin(deFloatMax(outMin, rangeMin));
318 inMax = deFloatSin(deFloatMin(outMax, rangeMax));
323 static inline float evaluateComp (float inVal)
325 return deFloatAsin(inVal);
329 class AcosOp : public UnaryTrigonometricFunc<AcosOp>
332 AcosOp (GeneratorState& state, ConstValueRangeAccess valueRange)
333 : UnaryTrigonometricFunc<AcosOp>(state, "acos", valueRange)
337 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
339 const float rangeMin = 0.0f;
340 const float rangeMax = DE_PI;
342 if (outMax < rangeMin || outMin > rangeMax)
343 return false; // Out of range
345 inMax = deFloatCos(deFloatMax(outMin, rangeMin));
346 inMin = deFloatCos(deFloatMin(outMax, rangeMax));
351 static inline float evaluateComp (float inVal)
353 return deFloatAcos(inVal);
357 class AtanOp : public UnaryTrigonometricFunc<AtanOp>
360 AtanOp (GeneratorState& state, ConstValueRangeAccess valueRange)
361 : UnaryTrigonometricFunc<AtanOp>(state, "atan", valueRange)
365 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
367 // \note For accuracy reasons output range is limited to -1..1
368 const float rangeMin = -1.0f;
369 const float rangeMax = +1.0f;
371 if (outMax < rangeMin || outMin > rangeMax)
372 return false; // Out of range
374 inMin = deFloatTan(deFloatMax(outMin, rangeMin));
375 inMax = deFloatTan(deFloatMin(outMax, rangeMax));
380 static inline float evaluateComp (float inVal)
382 return deFloatAtanOver(inVal);
386 // Template for exponential function group.
387 // \todo [2011-07-07 pyry] Shares most of the code with Trigonometric variant..
389 class UnaryExponentialFunc : public UnaryBuiltinVecTemplateProxy<C>
392 UnaryExponentialFunc (GeneratorState& state, const char* function, ConstValueRangeAccess valueRange)
393 : UnaryBuiltinVecTemplateProxy<C>(state, function, valueRange)
397 static inline float getCompWeight (float outMin, float outMax)
399 if (Scalar::min<float>() == outMin || Scalar::max<float>() == outMax)
400 return 1.0f; // Infinite value range, anything goes
404 if (!C::transformValueRange(outMin, outMax, inMin, inMax))
405 return 0.0f; // Not possible to transform value range (out of range perhaps)
408 if (!quantizeFloatRange(inMin, inMax))
409 return 0.0f; // Not possible to quantize - would cause accuracy issues
411 if (outMin == outMax)
412 return 1.0f; // Constant value and passed quantization
414 // Evaluate new intersection
415 float intersectionLen = C::evaluateComp(inMax) - C::evaluateComp(inMin);
416 float valRangeLen = outMax - outMin;
418 return deFloatMax(0.1f, intersectionLen/valRangeLen);
421 static inline void computeValueRange (float outMin, float outMax, float& inMin, float& inMax)
423 DE_VERIFY(C::transformValueRange(outMin, outMax, inMin, inMax));
424 DE_VERIFY(quantizeFloatRange(inMin, inMax));
425 DE_ASSERT(inMin <= inMax);
428 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
430 if (state.getProgramParameters().exponentialBaseWeight <= 0.0f)
433 return UnaryBuiltinVecTemplateProxy<C>::getWeight(state, valueRange) * state.getProgramParameters().exponentialBaseWeight;
437 class ExpOp : public UnaryExponentialFunc<ExpOp>
440 ExpOp (GeneratorState& state, ConstValueRangeAccess valueRange)
441 : UnaryExponentialFunc<ExpOp>(state, "exp", valueRange)
445 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
447 // Limited due to accuracy reasons, should be 0..+inf
448 const float rangeMin = 0.1f;
449 const float rangeMax = 10.0f;
451 if (outMax < rangeMin || outMin > rangeMax)
452 return false; // Out of range
454 inMin = deFloatLog(deFloatMax(outMin, rangeMin));
455 inMax = deFloatLog(deFloatMin(outMax, rangeMax));
460 static inline float evaluateComp (float inVal)
462 return deFloatExp(inVal);
466 class LogOp : public UnaryExponentialFunc<LogOp>
469 LogOp (GeneratorState& state, ConstValueRangeAccess valueRange)
470 : UnaryExponentialFunc<LogOp>(state, "log", valueRange)
474 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
476 // Limited due to accuracy reasons, should be -inf..+inf
477 const float rangeMin = 0.1f;
478 const float rangeMax = 6.0f;
480 if (outMax < rangeMin || outMin > rangeMax)
481 return false; // Out of range
483 inMin = deFloatExp(deFloatMax(outMin, rangeMin));
484 inMax = deFloatExp(deFloatMin(outMax, rangeMax));
489 static inline float evaluateComp (float inVal)
491 return deFloatLog(inVal);
495 class Exp2Op : public UnaryExponentialFunc<Exp2Op>
498 Exp2Op (GeneratorState& state, ConstValueRangeAccess valueRange)
499 : UnaryExponentialFunc<Exp2Op>(state, "exp2", valueRange)
503 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
505 // Limited due to accuracy reasons, should be 0..+inf
506 const float rangeMin = 0.1f;
507 const float rangeMax = 10.0f;
509 if (outMax < rangeMin || outMin > rangeMax)
510 return false; // Out of range
512 inMin = deFloatLog2(deFloatMax(outMin, rangeMin));
513 inMax = deFloatLog2(deFloatMin(outMax, rangeMax));
518 static inline float evaluateComp (float inVal)
520 return deFloatExp2(inVal);
524 class Log2Op : public UnaryExponentialFunc<Log2Op>
527 Log2Op (GeneratorState& state, ConstValueRangeAccess valueRange)
528 : UnaryExponentialFunc<Log2Op>(state, "log2", valueRange)
532 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
534 // Limited due to accuracy reasons, should be -inf..+inf
535 const float rangeMin = 0.1f;
536 const float rangeMax = 6.0f;
538 if (outMax < rangeMin || outMin > rangeMax)
539 return false; // Out of range
541 inMin = deFloatExp2(deFloatMax(outMin, rangeMin));
542 inMax = deFloatExp2(deFloatMin(outMax, rangeMax));
547 static inline float evaluateComp (float inVal)
549 return deFloatLog2(inVal);
553 class SqrtOp : public UnaryExponentialFunc<SqrtOp>
556 SqrtOp (GeneratorState& state, ConstValueRangeAccess valueRange)
557 : UnaryExponentialFunc<SqrtOp>(state, "sqrt", valueRange)
561 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
563 // Limited due to accuracy reasons, should be 0..+inf
564 const float rangeMin = 0.0f;
565 const float rangeMax = 4.0f;
567 if (outMax < rangeMin || outMin > rangeMax)
568 return false; // Out of range
570 inMin = deFloatMax(outMin, rangeMin);
571 inMax = deFloatMin(outMax, rangeMax);
579 static inline float evaluateComp (float inVal)
581 return deFloatSqrt(inVal);
585 class InvSqrtOp : public UnaryExponentialFunc<InvSqrtOp>
588 InvSqrtOp (GeneratorState& state, ConstValueRangeAccess valueRange)
589 : UnaryExponentialFunc<InvSqrtOp>(state, "inversesqrt", valueRange)
593 static inline bool transformValueRange (float outMin, float outMax, float& inMin, float& inMax)
595 // Limited due to accuracy reasons
596 const float rangeMin = 0.4f;
597 const float rangeMax = 3.0f;
599 if (outMax < rangeMin || outMin > rangeMax)
600 return false; // Out of range
602 inMax = 1.0f/deFloatMax(outMin, rangeMin);
603 inMin = 1.0f/deFloatMin(outMax, rangeMax);
611 static inline float evaluateComp (float inVal)
613 return 1.0f/deFloatSqrt(inVal);
619 #endif // _RSGBUILTINFUNCTIONS_HPP