1 #ifndef _RSGEXPRESSION_HPP 2 #define _RSGEXPRESSION_HPP 3 /*------------------------------------------------------------------------- 4 * drawElements Quality Program Random Shader Generator 5 * ---------------------------------------------------- 6 * 7 * Copyright 2014 The Android Open Source Project 8 * 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 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 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. 20 * 21 *//*! 22 * \file 23 * \brief Expressions. 24 * 25 * Creating expressions: 26 * + Children must be created in in reverse evaluation order. 27 * - Must be tokenized / evaluated taking that order in account. 28 * 29 * Evaluation: 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 34 * evaluate(). 35 *//*--------------------------------------------------------------------*/ 36 37 #include "rsgDefs.hpp" 38 #include "rsgGeneratorState.hpp" 39 #include "rsgVariableValue.hpp" 40 #include "rsgVariable.hpp" 41 #include "rsgVariableManager.hpp" 42 #include "rsgExecutionContext.hpp" 43 44 namespace rsg 45 { 46 47 // \todo [2011-06-10 pyry] Declare in ShaderParameters? 48 const float unusedValueWeight = 0.05f; 49 50 class Expression 51 { 52 public: 53 virtual ~Expression(void); 54 55 // Shader generation API 56 virtual Expression *createNextChild(GeneratorState &state) = DE_NULL; 57 virtual void tokenize(GeneratorState &state, TokenStream &str) const = DE_NULL; 58 59 // Execution API 60 virtual void evaluate(ExecutionContext &ctx) = DE_NULL; 61 virtual ExecConstValueAccess getValue(void) const = DE_NULL; getLValue(void) const62 virtual ExecValueAccess getLValue(void) const 63 { 64 DE_ASSERT(false); 65 throw Exception("Expression::getLValue(): not L-value node"); 66 } 67 68 static Expression *createRandom(GeneratorState &state, ConstValueRangeAccess valueRange); 69 static Expression *createRandomLValue(GeneratorState &state, ConstValueRangeAccess valueRange); 70 }; 71 72 class VariableAccess : public Expression 73 { 74 public: ~VariableAccess(void)75 virtual ~VariableAccess(void) 76 { 77 } 78 createNextChild(GeneratorState & state)79 Expression *createNextChild(GeneratorState &state) 80 { 81 DE_UNREF(state); 82 return DE_NULL; 83 } tokenize(GeneratorState & state,TokenStream & str) const84 void tokenize(GeneratorState &state, TokenStream &str) const 85 { 86 DE_UNREF(state); 87 str << Token(m_variable->getName()); 88 } 89 90 void evaluate(ExecutionContext &ctx); getValue(void) const91 ExecConstValueAccess getValue(void) const 92 { 93 return m_valueAccess; 94 } getLValue(void) const95 ExecValueAccess getLValue(void) const 96 { 97 return m_valueAccess; 98 } 99 100 protected: VariableAccess(void)101 VariableAccess(void) : m_variable(DE_NULL) 102 { 103 } 104 105 const Variable *m_variable; 106 ExecValueAccess m_valueAccess; 107 }; 108 109 class VariableRead : public VariableAccess 110 { 111 public: 112 VariableRead(GeneratorState &state, ConstValueRangeAccess valueRange); 113 VariableRead(const Variable *variable); ~VariableRead(void)114 virtual ~VariableRead(void) 115 { 116 } 117 118 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 119 }; 120 121 class VariableWrite : public VariableAccess 122 { 123 public: 124 VariableWrite(GeneratorState &state, ConstValueRangeAccess valueRange); ~VariableWrite(void)125 virtual ~VariableWrite(void) 126 { 127 } 128 129 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 130 }; 131 132 class FloatLiteral : public Expression 133 { 134 public: 135 FloatLiteral(GeneratorState &state, ConstValueRangeAccess valueRange); 136 FloatLiteral(float customValue); ~FloatLiteral(void)137 virtual ~FloatLiteral(void) 138 { 139 } 140 createNextChild(GeneratorState & state)141 Expression *createNextChild(GeneratorState &state) 142 { 143 DE_UNREF(state); 144 return DE_NULL; 145 } 146 void tokenize(GeneratorState &state, TokenStream &str) const; 147 148 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 149 evaluate(ExecutionContext & ctx)150 void evaluate(ExecutionContext &ctx) 151 { 152 DE_UNREF(ctx); 153 } getValue(void) const154 ExecConstValueAccess getValue(void) const 155 { 156 return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT)); 157 } 158 159 private: 160 ExecValueStorage m_value; 161 }; 162 163 class IntLiteral : public Expression 164 { 165 public: 166 IntLiteral(GeneratorState &state, ConstValueRangeAccess valueRange); ~IntLiteral(void)167 virtual ~IntLiteral(void) 168 { 169 } 170 createNextChild(GeneratorState & state)171 Expression *createNextChild(GeneratorState &state) 172 { 173 DE_UNREF(state); 174 return DE_NULL; 175 } 176 void tokenize(GeneratorState &state, TokenStream &str) const; 177 178 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 179 evaluate(ExecutionContext & ctx)180 void evaluate(ExecutionContext &ctx) 181 { 182 DE_UNREF(ctx); 183 } getValue(void) const184 ExecConstValueAccess getValue(void) const 185 { 186 return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT)); 187 } 188 189 private: 190 ExecValueStorage m_value; 191 }; 192 193 class BoolLiteral : public Expression 194 { 195 public: 196 BoolLiteral(GeneratorState &state, ConstValueRangeAccess valueRange); 197 BoolLiteral(bool customValue); ~BoolLiteral(void)198 virtual ~BoolLiteral(void) 199 { 200 } 201 createNextChild(GeneratorState & state)202 Expression *createNextChild(GeneratorState &state) 203 { 204 DE_UNREF(state); 205 return DE_NULL; 206 } 207 void tokenize(GeneratorState &state, TokenStream &str) const; 208 209 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 210 evaluate(ExecutionContext & ctx)211 void evaluate(ExecutionContext &ctx) 212 { 213 DE_UNREF(ctx); 214 } getValue(void) const215 ExecConstValueAccess getValue(void) const 216 { 217 return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL)); 218 } 219 220 private: 221 ExecValueStorage m_value; 222 }; 223 224 class ConstructorOp : public Expression 225 { 226 public: 227 ConstructorOp(GeneratorState &state, ConstValueRangeAccess valueRange); 228 virtual ~ConstructorOp(void); 229 230 Expression *createNextChild(GeneratorState &state); 231 void tokenize(GeneratorState &state, TokenStream &str) const; 232 233 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 234 235 void evaluate(ExecutionContext &ctx); getValue(void) const236 ExecConstValueAccess getValue(void) const 237 { 238 return m_value.getValue(m_valueRange.getType()); 239 } 240 241 private: 242 ValueRange m_valueRange; 243 ExecValueStorage m_value; 244 245 std::vector<ValueRange> m_inputValueRanges; 246 std::vector<Expression *> m_inputExpressions; 247 }; 248 249 class AssignOp : public Expression 250 { 251 public: 252 AssignOp(GeneratorState &state, ConstValueRangeAccess valueRange); 253 virtual ~AssignOp(void); 254 255 Expression *createNextChild(GeneratorState &state); 256 void tokenize(GeneratorState &state, TokenStream &str) const; 257 258 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 259 260 // \todo [2011-02-28 pyry] LValue variant of AssignOp 261 // static float getLValueWeight (const GeneratorState& state, ConstValueRangeAccess valueRange); 262 263 void evaluate(ExecutionContext &ctx); getValue(void) const264 ExecConstValueAccess getValue(void) const 265 { 266 return m_value.getValue(m_valueRange.getType()); 267 } 268 269 private: 270 ValueRange m_valueRange; 271 ExecValueStorage m_value; 272 273 Expression *m_lvalueExpr; 274 Expression *m_rvalueExpr; 275 }; 276 277 class ParenOp : public Expression 278 { 279 public: 280 ParenOp(GeneratorState &state, ConstValueRangeAccess valueRange); 281 virtual ~ParenOp(void); 282 283 Expression *createNextChild(GeneratorState &state); 284 void tokenize(GeneratorState &state, TokenStream &str) const; 285 286 void setChild(Expression *expression); 287 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 288 evaluate(ExecutionContext & execCtx)289 void evaluate(ExecutionContext &execCtx) 290 { 291 m_child->evaluate(execCtx); 292 } getValue(void) const293 ExecConstValueAccess getValue(void) const 294 { 295 return m_child->getValue(); 296 } 297 298 private: 299 ValueRange m_valueRange; 300 Expression *m_child; 301 }; 302 303 class SwizzleOp : public Expression 304 { 305 public: 306 SwizzleOp(GeneratorState &state, ConstValueRangeAccess valueRange); 307 virtual ~SwizzleOp(void); 308 309 Expression *createNextChild(GeneratorState &state); 310 void tokenize(GeneratorState &state, TokenStream &str) const; 311 312 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 313 314 void evaluate(ExecutionContext &execCtx); getValue(void) const315 ExecConstValueAccess getValue(void) const 316 { 317 return m_value.getValue(m_outValueRange.getType()); 318 } 319 320 private: 321 ValueRange m_outValueRange; 322 int m_numInputElements; 323 uint8_t m_swizzle[4]; 324 Expression *m_child; 325 ExecValueStorage m_value; 326 }; 327 328 class TexLookup : public Expression 329 { 330 public: 331 TexLookup(GeneratorState &state, ConstValueRangeAccess valueRange); 332 virtual ~TexLookup(void); 333 334 Expression *createNextChild(GeneratorState &state); 335 void tokenize(GeneratorState &state, TokenStream &str) const; 336 337 static float getWeight(const GeneratorState &state, ConstValueRangeAccess valueRange); 338 339 void evaluate(ExecutionContext &execCtx); getValue(void) const340 ExecConstValueAccess getValue(void) const 341 { 342 return m_value.getValue(m_valueType); 343 } 344 345 private: 346 enum Type 347 { 348 TYPE_TEXTURE2D, 349 TYPE_TEXTURE2D_LOD, 350 TYPE_TEXTURE2D_PROJ, 351 TYPE_TEXTURE2D_PROJ_LOD, 352 353 TYPE_TEXTURECUBE, 354 TYPE_TEXTURECUBE_LOD, 355 356 TYPE_LAST 357 }; 358 359 Type m_type; 360 const Variable *m_sampler; 361 Expression *m_coordExpr; 362 Expression *m_lodBiasExpr; 363 VariableType m_valueType; 364 ExecValueStorage m_value; 365 }; 366 367 } // namespace rsg 368 369 #endif // _RSGEXPRESSION_HPP 370