Home | History | Annotate | Download | only in randomshaders
      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;
     62 	virtual ExecValueAccess			getLValue			(void) const { DE_ASSERT(DE_FALSE); throw Exception("Expression::getLValue(): not L-value node"); }
     63 
     64 	static Expression*				createRandom		(GeneratorState& state, ConstValueRangeAccess valueRange);
     65 	static Expression*				createRandomLValue	(GeneratorState& state, ConstValueRangeAccess valueRange);
     66 };
     67 
     68 class VariableAccess : public Expression
     69 {
     70 public:
     71 	virtual						~VariableAccess		(void) {}
     72 
     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());	}
     75 
     76 	void						evaluate			(ExecutionContext& ctx);
     77 	ExecConstValueAccess		getValue			(void) const									{ return m_valueAccess;									}
     78 	ExecValueAccess				getLValue			(void) const									{ return m_valueAccess;									}
     79 
     80 protected:
     81 								VariableAccess		(void) : m_variable(DE_NULL) {}
     82 
     83 	const Variable*				m_variable;
     84 	ExecValueAccess				m_valueAccess;
     85 };
     86 
     87 class VariableRead : public VariableAccess
     88 {
     89 public:
     90 								VariableRead		(GeneratorState& state, ConstValueRangeAccess valueRange);
     91 								VariableRead		(const Variable* variable);
     92 	virtual						~VariableRead		(void) {}
     93 
     94 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
     95 };
     96 
     97 class VariableWrite : public VariableAccess
     98 {
     99 public:
    100 								VariableWrite		(GeneratorState& state, ConstValueRangeAccess valueRange);
    101 	virtual						~VariableWrite		(void) {}
    102 
    103 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    104 };
    105 
    106 class FloatLiteral : public Expression
    107 {
    108 public:
    109 								FloatLiteral		(GeneratorState& state, ConstValueRangeAccess valueRange);
    110 								FloatLiteral		(float customValue);
    111 	virtual						~FloatLiteral		(void) {}
    112 
    113 	Expression*					createNextChild		(GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
    114 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    115 
    116 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    117 
    118 	void						evaluate			(ExecutionContext& ctx) { DE_UNREF(ctx); }
    119 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT)); }
    120 
    121 private:
    122 	ExecValueStorage			m_value;
    123 };
    124 
    125 class IntLiteral : public Expression
    126 {
    127 public:
    128 								IntLiteral			(GeneratorState& state, ConstValueRangeAccess valueRange);
    129 	virtual						~IntLiteral			(void) {}
    130 
    131 	Expression*					createNextChild		(GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
    132 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    133 
    134 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    135 
    136 	void						evaluate			(ExecutionContext& ctx) { DE_UNREF(ctx); }
    137 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT)); }
    138 
    139 private:
    140 	ExecValueStorage			m_value;
    141 };
    142 
    143 class BoolLiteral : public Expression
    144 {
    145 public:
    146 								BoolLiteral			(GeneratorState& state, ConstValueRangeAccess valueRange);
    147 								BoolLiteral			(bool customValue);
    148 	virtual						~BoolLiteral		(void) {}
    149 
    150 	Expression*					createNextChild		(GeneratorState& state) { DE_UNREF(state); return DE_NULL; }
    151 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    152 
    153 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    154 
    155 	void						evaluate			(ExecutionContext& ctx) { DE_UNREF(ctx); }
    156 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL)); }
    157 
    158 private:
    159 	ExecValueStorage			m_value;
    160 };
    161 
    162 class ConstructorOp : public Expression
    163 {
    164 public:
    165 								ConstructorOp		(GeneratorState& state, ConstValueRangeAccess valueRange);
    166 	virtual						~ConstructorOp		(void);
    167 
    168 	Expression*					createNextChild		(GeneratorState& state);
    169 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    170 
    171 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    172 
    173 	void						evaluate			(ExecutionContext& ctx);
    174 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(m_valueRange.getType()); }
    175 
    176 private:
    177 	ValueRange					m_valueRange;
    178 	ExecValueStorage			m_value;
    179 
    180 	std::vector<ValueRange>		m_inputValueRanges;
    181 	std::vector<Expression*>	m_inputExpressions;
    182 };
    183 
    184 class AssignOp : public Expression
    185 {
    186 public:
    187 								AssignOp			(GeneratorState& state, ConstValueRangeAccess valueRange);
    188 	virtual						~AssignOp			(void);
    189 
    190 	Expression*					createNextChild		(GeneratorState& state);
    191 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    192 
    193 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    194 
    195 	// \todo [2011-02-28 pyry] LValue variant of AssignOp
    196 //	static float				getLValueWeight		(const GeneratorState& state, ConstValueRangeAccess valueRange);
    197 
    198 	void						evaluate			(ExecutionContext& ctx);
    199 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(m_valueRange.getType()); }
    200 
    201 private:
    202 	ValueRange					m_valueRange;
    203 	ExecValueStorage			m_value;
    204 
    205 	Expression*					m_lvalueExpr;
    206 	Expression*					m_rvalueExpr;
    207 };
    208 
    209 class ParenOp : public Expression
    210 {
    211 public:
    212 								ParenOp				(GeneratorState& state, ConstValueRangeAccess valueRange);
    213 	virtual						~ParenOp			(void);
    214 
    215 	Expression*					createNextChild		(GeneratorState& state);
    216 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    217 
    218 	void						setChild			(Expression* expression);
    219 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    220 
    221 	void						evaluate			(ExecutionContext& execCtx)		{ m_child->evaluate(execCtx);	}
    222 	ExecConstValueAccess		getValue			(void) const					{ return m_child->getValue();	}
    223 
    224 private:
    225 	ValueRange					m_valueRange;
    226 	Expression*					m_child;
    227 };
    228 
    229 class SwizzleOp : public Expression
    230 {
    231 public:
    232 								SwizzleOp			(GeneratorState& state, ConstValueRangeAccess valueRange);
    233 	virtual						~SwizzleOp			(void);
    234 
    235 	Expression*					createNextChild		(GeneratorState& state);
    236 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    237 
    238 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    239 
    240 	void						evaluate			(ExecutionContext& execCtx);
    241 	ExecConstValueAccess		getValue			(void) const					{ return m_value.getValue(m_outValueRange.getType()); }
    242 
    243 private:
    244 	ValueRange					m_outValueRange;
    245 	int							m_numInputElements;
    246 	deUint8						m_swizzle[4];
    247 	Expression*					m_child;
    248 	ExecValueStorage			m_value;
    249 };
    250 
    251 class TexLookup : public Expression
    252 {
    253 public:
    254 								TexLookup			(GeneratorState& state, ConstValueRangeAccess valueRange);
    255 	virtual						~TexLookup			(void);
    256 
    257 	Expression*					createNextChild		(GeneratorState& state);
    258 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    259 
    260 	static float				getWeight			(const GeneratorState& state, ConstValueRangeAccess valueRange);
    261 
    262 	void						evaluate			(ExecutionContext& execCtx);
    263 	ExecConstValueAccess		getValue			(void) const { return m_value.getValue(m_valueType); }
    264 
    265 private:
    266 	enum Type
    267 	{
    268 		TYPE_TEXTURE2D,
    269 		TYPE_TEXTURE2D_LOD,
    270 		TYPE_TEXTURE2D_PROJ,
    271 		TYPE_TEXTURE2D_PROJ_LOD,
    272 
    273 		TYPE_TEXTURECUBE,
    274 		TYPE_TEXTURECUBE_LOD,
    275 
    276 		TYPE_LAST
    277 	};
    278 
    279 	Type						m_type;
    280 	const Variable*				m_sampler;
    281 	Expression*					m_coordExpr;
    282 	Expression*					m_lodBiasExpr;
    283 	VariableType				m_valueType;
    284 	ExecValueStorage			m_value;
    285 };
    286 
    287 } // rsg
    288 
    289 #endif // _RSGEXPRESSION_HPP
    290