1 #ifndef _RSGEXPRESSION_HPP
2 #define _RSGEXPRESSION_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.
25 * Creating expressions:
26 * + Children must be created in in reverse evaluation order.
27 * - Must be tokenized / evaluated taking that order in account.
30 * + Done recursively. (Do we have enough stack?)
31 * + R-values: Nodes must implement getValue() in some way. Value
32 * must be valid after evaluate().
33 * + L-values: Valid writable value access proxy must be returned after
35 *//*--------------------------------------------------------------------*/
37 #include "rsgDefs.hpp"
38 #include "rsgGeneratorState.hpp"
39 #include "rsgVariableValue.hpp"
40 #include "rsgVariable.hpp"
41 #include "rsgVariableManager.hpp"
42 #include "rsgExecutionContext.hpp"
47 // \todo [2011-06-10 pyry] Declare in ShaderParameters?
48 const float unusedValueWeight = 0.05f;
53 virtual ~Expression (void);
55 // Shader generation API
56 virtual Expression* createNextChild (GeneratorState& state) = DE_NULL;
57 virtual void tokenize (GeneratorState& state, TokenStream& str) const = DE_NULL;
60 virtual void evaluate (ExecutionContext& ctx) = DE_NULL;
61 virtual ExecConstValueAccess getValue (void) const = DE_NULL;
62 virtual ExecValueAccess getLValue (void) const { DE_ASSERT(DE_FALSE); throw Exception("Expression::getLValue(): not L-value node"); }
64 static Expression* createRandom (GeneratorState& state, ConstValueRangeAccess valueRange);
65 static Expression* createRandomLValue (GeneratorState& state, ConstValueRangeAccess valueRange);
68 class VariableAccess : public Expression
71 virtual ~VariableAccess (void) {}
73 Expression* createNextChild (GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
74 void tokenize (GeneratorState& state, TokenStream& str) const { DE_UNREF(state); str << Token(m_variable->getName()); }
76 void evaluate (ExecutionContext& ctx);
77 ExecConstValueAccess getValue (void) const { return m_valueAccess; }
78 ExecValueAccess getLValue (void) const { return m_valueAccess; }
81 VariableAccess (void) : m_variable(DE_NULL) {}
83 const Variable* m_variable;
84 ExecValueAccess m_valueAccess;
87 class VariableRead : public VariableAccess
90 VariableRead (GeneratorState& state, ConstValueRangeAccess valueRange);
91 VariableRead (const Variable* variable);
92 virtual ~VariableRead (void) {}
94 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
97 class VariableWrite : public VariableAccess
100 VariableWrite (GeneratorState& state, ConstValueRangeAccess valueRange);
101 virtual ~VariableWrite (void) {}
103 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
106 class FloatLiteral : public Expression
109 FloatLiteral (GeneratorState& state, ConstValueRangeAccess valueRange);
110 virtual ~FloatLiteral (void) {}
112 Expression* createNextChild (GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
113 void tokenize (GeneratorState& state, TokenStream& str) const;
115 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
117 void evaluate (ExecutionContext& ctx) { DE_UNREF(ctx); }
118 ExecConstValueAccess getValue (void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT)); }
121 ExecValueStorage m_value;
124 class IntLiteral : public Expression
127 IntLiteral (GeneratorState& state, ConstValueRangeAccess valueRange);
128 virtual ~IntLiteral (void) {}
130 Expression* createNextChild (GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
131 void tokenize (GeneratorState& state, TokenStream& str) const;
133 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
135 void evaluate (ExecutionContext& ctx) { DE_UNREF(ctx); }
136 ExecConstValueAccess getValue (void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT)); }
139 ExecValueStorage m_value;
142 class BoolLiteral : public Expression
145 BoolLiteral (GeneratorState& state, ConstValueRangeAccess valueRange);
146 virtual ~BoolLiteral (void) {}
148 Expression* createNextChild (GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
149 void tokenize (GeneratorState& state, TokenStream& str) const;
151 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
153 void evaluate (ExecutionContext& ctx) { DE_UNREF(ctx); }
154 ExecConstValueAccess getValue (void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL)); }
157 ExecValueStorage m_value;
160 class ConstructorOp : public Expression
163 ConstructorOp (GeneratorState& state, ConstValueRangeAccess valueRange);
164 virtual ~ConstructorOp (void);
166 Expression* createNextChild (GeneratorState& state);
167 void tokenize (GeneratorState& state, TokenStream& str) const;
169 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
171 void evaluate (ExecutionContext& ctx);
172 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_valueRange.getType()); }
175 ValueRange m_valueRange;
176 ExecValueStorage m_value;
178 std::vector<ValueRange> m_inputValueRanges;
179 std::vector<Expression*> m_inputExpressions;
182 class AssignOp : public Expression
185 AssignOp (GeneratorState& state, ConstValueRangeAccess valueRange);
186 virtual ~AssignOp (void);
188 Expression* createNextChild (GeneratorState& state);
189 void tokenize (GeneratorState& state, TokenStream& str) const;
191 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
193 // \todo [2011-02-28 pyry] LValue variant of AssignOp
194 // static float getLValueWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
196 void evaluate (ExecutionContext& ctx);
197 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_valueRange.getType()); }
200 ValueRange m_valueRange;
201 ExecValueStorage m_value;
203 Expression* m_lvalueExpr;
204 Expression* m_rvalueExpr;
207 class ParenOp : public Expression
210 ParenOp (GeneratorState& state, ConstValueRangeAccess valueRange);
211 virtual ~ParenOp (void);
213 Expression* createNextChild (GeneratorState& state);
214 void tokenize (GeneratorState& state, TokenStream& str) const;
216 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
218 void evaluate (ExecutionContext& execCtx) { m_child->evaluate(execCtx); }
219 ExecConstValueAccess getValue (void) const { return m_child->getValue(); }
222 ValueRange m_valueRange;
226 class SwizzleOp : public Expression
229 SwizzleOp (GeneratorState& state, ConstValueRangeAccess valueRange);
230 virtual ~SwizzleOp (void);
232 Expression* createNextChild (GeneratorState& state);
233 void tokenize (GeneratorState& state, TokenStream& str) const;
235 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
237 void evaluate (ExecutionContext& execCtx);
238 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_outValueRange.getType()); }
241 ValueRange m_outValueRange;
242 int m_numInputElements;
243 deUint8 m_swizzle[4];
245 ExecValueStorage m_value;
248 class TexLookup : public Expression
251 TexLookup (GeneratorState& state, ConstValueRangeAccess valueRange);
252 virtual ~TexLookup (void);
254 Expression* createNextChild (GeneratorState& state);
255 void tokenize (GeneratorState& state, TokenStream& str) const;
257 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
259 void evaluate (ExecutionContext& execCtx);
260 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_valueType); }
268 TYPE_TEXTURE2D_PROJ_LOD,
271 TYPE_TEXTURECUBE_LOD,
277 const Variable* m_sampler;
278 Expression* m_coordExpr;
279 Expression* m_lodBiasExpr;
280 VariableType m_valueType;
281 ExecValueStorage m_value;
286 #endif // _RSGEXPRESSION_HPP