Home | History | Annotate | Download | only in randomshaders
      1 /*-------------------------------------------------------------------------
      2  * drawElements Quality Program Random Shader Generator
      3  * ----------------------------------------------------
      4  *
      5  * Copyright 2014 The Android Open Source Project
      6  *
      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
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     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.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Expressions.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "rsgExpression.hpp"
     25 #include "rsgVariableManager.hpp"
     26 #include "rsgBinaryOps.hpp"
     27 #include "rsgBuiltinFunctions.hpp"
     28 #include "rsgUtils.hpp"
     29 #include "deMath.h"
     30 
     31 using std::vector;
     32 
     33 namespace rsg
     34 {
     35 
     36 namespace
     37 {
     38 
     39 class IsReadableEntry
     40 {
     41 public:
     42 	typedef ValueEntryIterator<IsReadableEntry> Iterator;
     43 
     44 	IsReadableEntry (deUint32 exprFlags)
     45 		: m_exprFlags(exprFlags)
     46 	{
     47 	}
     48 
     49 	bool operator() (const ValueEntry* entry) const
     50 	{
     51 		if ((m_exprFlags & CONST_EXPR) && (entry->getVariable()->getStorage() != Variable::STORAGE_CONST))
     52 			return false;
     53 
     54 		return true;
     55 	}
     56 
     57 private:
     58 	deUint32 m_exprFlags;
     59 };
     60 
     61 class IsReadableIntersectingEntry : public IsReadableEntry
     62 {
     63 public:
     64 	typedef ValueEntryIterator<IsReadableIntersectingEntry> Iterator;
     65 
     66 	IsReadableIntersectingEntry (ConstValueRangeAccess valueRange, deUint32 exprFlags)
     67 		: IsReadableEntry	(exprFlags)
     68 		, m_valueRange		(valueRange)
     69 	{
     70 	}
     71 
     72 	bool operator() (const ValueEntry* entry) const
     73 	{
     74 		if (!IsReadableEntry::operator()(entry))
     75 			return false;
     76 
     77 		if (entry->getValueRange().getType() != m_valueRange.getType())
     78 			return false;
     79 
     80 		if (!entry->getValueRange().intersects(m_valueRange))
     81 			return false;
     82 
     83 		return true;
     84 	}
     85 
     86 private:
     87 	ConstValueRangeAccess m_valueRange;
     88 };
     89 
     90 class IsWritableIntersectingEntry : public IsWritableEntry
     91 {
     92 public:
     93 	typedef ValueEntryIterator<IsWritableIntersectingEntry> Iterator;
     94 
     95 	IsWritableIntersectingEntry (ConstValueRangeAccess valueRange)
     96 		: m_valueRange(valueRange)
     97 	{
     98 	}
     99 
    100 	bool operator() (const ValueEntry* entry) const
    101 	{
    102 		return IsWritableEntry::operator()(entry) &&
    103 			   entry->getVariable()->getType() == m_valueRange.getType() &&
    104 			   entry->getValueRange().intersects(m_valueRange);
    105 	}
    106 
    107 private:
    108 	ConstValueRangeAccess m_valueRange;
    109 };
    110 
    111 class IsWritableSupersetEntry : public IsWritableEntry
    112 {
    113 public:
    114 	typedef ValueEntryIterator<IsWritableSupersetEntry> Iterator;
    115 
    116 	IsWritableSupersetEntry (ConstValueRangeAccess valueRange)
    117 		: m_valueRange(valueRange)
    118 	{
    119 	}
    120 
    121 	bool operator() (const ValueEntry* entry) const
    122 	{
    123 		return IsWritableEntry()(entry) &&
    124 			   entry->getVariable()->getType() == m_valueRange.getType() &&
    125 			   entry->getValueRange().isSupersetOf(m_valueRange);
    126 	}
    127 
    128 private:
    129 	ConstValueRangeAccess m_valueRange;
    130 };
    131 
    132 class IsSamplerEntry
    133 {
    134 public:
    135 	typedef ValueEntryIterator<IsSamplerEntry> Iterator;
    136 
    137 	IsSamplerEntry (VariableType::Type type)
    138 		: m_type(type)
    139 	{
    140 		DE_ASSERT(m_type == VariableType::TYPE_SAMPLER_2D || m_type == VariableType::TYPE_SAMPLER_CUBE);
    141 	}
    142 
    143 	bool operator() (const ValueEntry* entry) const
    144 	{
    145 		if (entry->getVariable()->getType() == VariableType(m_type, 1))
    146 		{
    147 			DE_ASSERT(entry->getVariable()->getStorage() == Variable::STORAGE_UNIFORM);
    148 			return true;
    149 		}
    150 		else
    151 			return false;
    152 	}
    153 
    154 private:
    155 	VariableType::Type m_type;
    156 };
    157 
    158 inline bool getWeightedBool (de::Random& random, float trueWeight)
    159 {
    160 	DE_ASSERT(de::inRange<float>(trueWeight, 0.0f, 1.0f));
    161 	return (random.getFloat() < trueWeight);
    162 }
    163 
    164 void computeRandomValueRangeForInfElements (GeneratorState& state, ValueRangeAccess valueRange)
    165 {
    166 	const VariableType&	type	= valueRange.getType();
    167 	de::Random&		rnd		= state.getRandom();
    168 
    169 	switch (type.getBaseType())
    170 	{
    171 		case VariableType::TYPE_BOOL:
    172 			// No need to handle bool as it will be false, true
    173 			break;
    174 
    175 		case VariableType::TYPE_INT:
    176 			for (int ndx = 0; ndx < type.getNumElements(); ndx++)
    177 			{
    178 				if (valueRange.getMin().component(ndx).asScalar() != Scalar::min<int>() ||
    179 					valueRange.getMax().component(ndx).asScalar() != Scalar::max<int>())
    180 					continue;
    181 
    182 				const int minIntVal		= -16;
    183 				const int maxIntVal		=  16;
    184 				const int maxRangeLen	= maxIntVal - minIntVal;
    185 
    186 				int rangeLen	= rnd.getInt(0, maxRangeLen);
    187 				int minVal		= minIntVal + rnd.getInt(0, maxRangeLen-rangeLen);
    188 				int maxVal		= minVal + rangeLen;
    189 
    190 				valueRange.getMin().component(ndx).asInt() = minVal;
    191 				valueRange.getMax().component(ndx).asInt() = maxVal;
    192 			}
    193 			break;
    194 
    195 		case VariableType::TYPE_FLOAT:
    196 			for (int ndx = 0; ndx < type.getNumElements(); ndx++)
    197 			{
    198 				if (valueRange.getMin().component(ndx).asScalar() != Scalar::min<float>() ||
    199 					valueRange.getMax().component(ndx).asScalar() != Scalar::max<float>())
    200 					continue;
    201 
    202 				const float step			= 0.1f;
    203 				const int	maxSteps		= 320;
    204 				const float minFloatVal		= -16.0f;
    205 
    206 				int rangeLen	= rnd.getInt(0, maxSteps);
    207 				int minStep		= rnd.getInt(0, maxSteps-rangeLen);
    208 
    209 				float minVal	= minFloatVal + step*(float)minStep;
    210 				float maxVal	= minVal + step*(float)rangeLen;
    211 
    212 				valueRange.getMin().component(ndx).asFloat() = minVal;
    213 				valueRange.getMax().component(ndx).asFloat() = maxVal;
    214 			}
    215 			break;
    216 
    217 		default:
    218 			DE_ASSERT(DE_FALSE);
    219 			throw Exception("computeRandomValueRangeForInfElements(): unsupported type");
    220 	}
    221 }
    222 
    223 void setInfiniteRange (ValueRangeAccess valueRange)
    224 {
    225 	const VariableType& type = valueRange.getType();
    226 
    227 	switch (type.getBaseType())
    228 	{
    229 		case VariableType::TYPE_BOOL:
    230 			for (int ndx = 0; ndx < type.getNumElements(); ndx++)
    231 			{
    232 				valueRange.getMin().component(ndx) = Scalar::min<bool>();
    233 				valueRange.getMax().component(ndx) = Scalar::max<bool>();
    234 			}
    235 			break;
    236 
    237 		case VariableType::TYPE_INT:
    238 			for (int ndx = 0; ndx < type.getNumElements(); ndx++)
    239 			{
    240 				valueRange.getMin().component(ndx) = Scalar::min<int>();
    241 				valueRange.getMax().component(ndx) = Scalar::max<int>();
    242 			}
    243 			break;
    244 
    245 		case VariableType::TYPE_FLOAT:
    246 			for (int ndx = 0; ndx < type.getNumElements(); ndx++)
    247 			{
    248 				valueRange.getMin().component(ndx) = Scalar::min<float>();
    249 				valueRange.getMax().component(ndx) = Scalar::max<float>();
    250 			}
    251 			break;
    252 
    253 		default:
    254 			DE_ASSERT(DE_FALSE);
    255 			throw Exception("setInfiniteRange(): unsupported type");
    256 	}
    257 }
    258 
    259 bool canAllocateVariable (const GeneratorState& state, const VariableType& type)
    260 {
    261 	DE_ASSERT(!type.isVoid());
    262 
    263 	if (state.getExpressionFlags() & NO_VAR_ALLOCATION)
    264 		return false;
    265 
    266 	if (state.getVariableManager().getNumAllocatedScalars() + type.getScalarSize() > state.getShaderParameters().maxCombinedVariableScalars)
    267 		return false;
    268 
    269 	return true;
    270 }
    271 
    272 template <class T> float		getWeight	(const GeneratorState& state, ConstValueRangeAccess valueRange)	{ return T::getWeight(state, valueRange);	}
    273 template <class T> Expression*	create		(GeneratorState& state, ConstValueRangeAccess valueRange)		{ return new T(state, valueRange);			}
    274 
    275 struct ExpressionSpec
    276 {
    277 	float			(*getWeight)		(const GeneratorState& state, ConstValueRangeAccess valueRange);
    278 	Expression*		(*create)			(GeneratorState& state, ConstValueRangeAccess valueRange);
    279 };
    280 
    281 static const ExpressionSpec s_expressionSpecs[] =
    282 {
    283 	{ getWeight<FloatLiteral>,		create<FloatLiteral>		},
    284 	{ getWeight<IntLiteral>,		create<IntLiteral>			},
    285 	{ getWeight<BoolLiteral>,		create<BoolLiteral>			},
    286 	{ getWeight<ConstructorOp>,		create<ConstructorOp>		},
    287 	{ getWeight<AssignOp>,			create<AssignOp>			},
    288 	{ getWeight<VariableRead>,		create<VariableRead>		},
    289 	{ getWeight<MulOp>,				create<MulOp>				},
    290 	{ getWeight<AddOp>,				create<AddOp>				},
    291 	{ getWeight<SubOp>,				create<SubOp>				},
    292 	{ getWeight<LessThanOp>,		create<LessThanOp>			},
    293 	{ getWeight<LessOrEqualOp>,		create<LessOrEqualOp>		},
    294 	{ getWeight<GreaterThanOp>,		create<GreaterThanOp>		},
    295 	{ getWeight<GreaterOrEqualOp>,	create<GreaterOrEqualOp>	},
    296 	{ getWeight<EqualOp>,			create<EqualOp>				},
    297 	{ getWeight<NotEqualOp>,		create<NotEqualOp>			},
    298 	{ getWeight<SwizzleOp>,			create<SwizzleOp>			},
    299 	{ getWeight<SinOp>,				create<SinOp>				},
    300 	{ getWeight<CosOp>,				create<CosOp>				},
    301 	{ getWeight<TanOp>,				create<TanOp>				},
    302 	{ getWeight<AsinOp>,			create<AsinOp>				},
    303 	{ getWeight<AcosOp>,			create<AcosOp>				},
    304 	{ getWeight<AtanOp>,			create<AtanOp>				},
    305 	{ getWeight<ExpOp>,				create<ExpOp>				},
    306 	{ getWeight<LogOp>,				create<LogOp>				},
    307 	{ getWeight<Exp2Op>,			create<Exp2Op>				},
    308 	{ getWeight<Log2Op>,			create<Log2Op>				},
    309 	{ getWeight<SqrtOp>,			create<SqrtOp>				},
    310 	{ getWeight<InvSqrtOp>,			create<InvSqrtOp>			},
    311 	{ getWeight<ParenOp>,			create<ParenOp>				},
    312 	{ getWeight<TexLookup>,			create<TexLookup>			}
    313 };
    314 
    315 static const ExpressionSpec s_lvalueSpecs[] =
    316 {
    317 	{ getWeight<VariableWrite>,		create<VariableWrite>	}
    318 };
    319 
    320 #if !defined(DE_MAX)
    321 #	define DE_MAX(a, b) ((b) > (a) ? (b) : (a))
    322 #endif
    323 
    324 enum
    325 {
    326 	MAX_EXPRESSION_SPECS = (int)DE_MAX(DE_LENGTH_OF_ARRAY(s_expressionSpecs), DE_LENGTH_OF_ARRAY(s_lvalueSpecs))
    327 };
    328 
    329 const ExpressionSpec* chooseExpression (GeneratorState& state, const ExpressionSpec* specs, int numSpecs, ConstValueRangeAccess valueRange)
    330 {
    331 	float weights[MAX_EXPRESSION_SPECS];
    332 
    333 	DE_ASSERT(numSpecs <= (int)DE_LENGTH_OF_ARRAY(weights));
    334 
    335 	// Compute weights
    336 	for (int ndx = 0; ndx < numSpecs; ndx++)
    337 		weights[ndx] = specs[ndx].getWeight(state, valueRange);
    338 
    339 	// Choose
    340 	return &state.getRandom().chooseWeighted<const ExpressionSpec&>(specs, specs+numSpecs, weights);
    341 }
    342 
    343 } // anonymous
    344 
    345 Expression::~Expression (void)
    346 {
    347 }
    348 
    349 Expression* Expression::createRandom (GeneratorState& state, ConstValueRangeAccess valueRange)
    350 {
    351 	return chooseExpression(state, s_expressionSpecs, (int)DE_LENGTH_OF_ARRAY(s_expressionSpecs), valueRange)->create(state, valueRange);
    352 }
    353 
    354 Expression* Expression::createRandomLValue (GeneratorState& state, ConstValueRangeAccess valueRange)
    355 {
    356 	return chooseExpression(state, s_lvalueSpecs, (int)DE_LENGTH_OF_ARRAY(s_lvalueSpecs), valueRange)->create(state, valueRange);
    357 }
    358 
    359 FloatLiteral::FloatLiteral (GeneratorState& state, ConstValueRangeAccess valueRange)
    360 	: m_value(VariableType::getScalarType(VariableType::TYPE_FLOAT))
    361 {
    362 	float minVal	= -10.0f;
    363 	float maxVal	= +10.0f;
    364 	float step		= 0.25f;
    365 
    366 	if (valueRange.getType() == VariableType(VariableType::TYPE_FLOAT, 1))
    367 	{
    368 		minVal = valueRange.getMin().component(0).asFloat();
    369 		maxVal = valueRange.getMax().component(0).asFloat();
    370 
    371 		if (Scalar::min<float>() == minVal)
    372 			minVal = -10.0f;
    373 
    374 		if (Scalar::max<float>() == maxVal)
    375 			maxVal = +10.0f;
    376 	}
    377 
    378 	int numSteps = (int)((maxVal-minVal)/step) + 1;
    379 
    380 	const float		value	= deFloatClamp(minVal + step*(float)state.getRandom().getInt(0, numSteps), minVal, maxVal);
    381 	ExecValueAccess	access	= m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT));
    382 
    383 	for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
    384 		access.asFloat(ndx) = value;
    385 }
    386 
    387 float FloatLiteral::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
    388 {
    389 	DE_UNREF(state);
    390 	const VariableType& type = valueRange.getType();
    391 	if (type == VariableType(VariableType::TYPE_FLOAT, 1))
    392 	{
    393 		float minVal = valueRange.getMin().asFloat();
    394 		float maxVal = valueRange.getMax().asFloat();
    395 
    396 		if (Scalar::min<float>() == minVal && Scalar::max<float>() == maxVal)
    397 			return 0.1f;
    398 
    399 		// Weight based on value range length
    400 		float rangeLength = maxVal - minVal;
    401 
    402 		DE_ASSERT(rangeLength >= 0.0f);
    403 		return deFloatMax(0.1f, 1.0f - rangeLength);
    404 	}
    405 	else if (type.isVoid())
    406 		return unusedValueWeight;
    407 	else
    408 		return 0.0f;
    409 }
    410 
    411 void FloatLiteral::tokenize (GeneratorState& state, TokenStream& str) const
    412 {
    413 	DE_UNREF(state);
    414 	str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_FLOAT)).asFloat(0));
    415 }
    416 
    417 IntLiteral::IntLiteral (GeneratorState& state, ConstValueRangeAccess valueRange)
    418 	: m_value(VariableType::getScalarType(VariableType::TYPE_INT))
    419 {
    420 	int minVal = -16;
    421 	int maxVal = +16;
    422 
    423 	if (valueRange.getType() == VariableType(VariableType::TYPE_INT, 1))
    424 	{
    425 		minVal = valueRange.getMin().component(0).asInt();
    426 		maxVal = valueRange.getMax().component(0).asInt();
    427 
    428 		if (Scalar::min<int>() == minVal)
    429 			minVal = -16;
    430 
    431 		if (Scalar::max<int>() == maxVal)
    432 			maxVal = 16;
    433 	}
    434 
    435 	int				value	= state.getRandom().getInt(minVal, maxVal);
    436 	ExecValueAccess	access	= m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT));
    437 
    438 	for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
    439 		access.asInt(ndx) = value;
    440 }
    441 
    442 float IntLiteral::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
    443 {
    444 	DE_UNREF(state);
    445 	const VariableType& type = valueRange.getType();
    446 	if (type == VariableType(VariableType::TYPE_INT, 1))
    447 	{
    448 		int minVal = valueRange.getMin().asInt();
    449 		int maxVal = valueRange.getMax().asInt();
    450 
    451 		if (Scalar::min<int>() == minVal && Scalar::max<int>() == maxVal)
    452 			return 0.1f;
    453 
    454 		int rangeLength = maxVal - minVal;
    455 
    456 		DE_ASSERT(rangeLength >= 0);
    457 		return deFloatMax(0.1f, 1.0f - (float)rangeLength/4.0f);
    458 	}
    459 	else if (type.isVoid())
    460 		return unusedValueWeight;
    461 	else
    462 		return 0.0f;
    463 }
    464 
    465 void IntLiteral::tokenize (GeneratorState& state, TokenStream& str) const
    466 {
    467 	DE_UNREF(state);
    468 	str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_INT)).asInt(0));
    469 }
    470 
    471 BoolLiteral::BoolLiteral (GeneratorState& state, ConstValueRangeAccess valueRange)
    472 	: m_value(VariableType::getScalarType(VariableType::TYPE_BOOL))
    473 {
    474 	int minVal = 0;
    475 	int maxVal = 1;
    476 
    477 	if (valueRange.getType() == VariableType(VariableType::TYPE_BOOL, 1))
    478 	{
    479 		minVal = valueRange.getMin().component(0).asBool() ? 1 : 0;
    480 		maxVal = valueRange.getMax().component(0).asBool() ? 1 : 0;
    481 	}
    482 
    483 	bool			value	= state.getRandom().getInt(minVal, maxVal) == 1;
    484 	ExecValueAccess	access	= m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL));
    485 
    486 	for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
    487 		access.asBool(ndx) = value;
    488 }
    489 
    490 float BoolLiteral::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
    491 {
    492 	DE_UNREF(state);
    493 	const VariableType& type = valueRange.getType();
    494 	if (type == VariableType(VariableType::TYPE_BOOL, 1))
    495 		return 0.5f;
    496 	else if (type.isVoid())
    497 		return unusedValueWeight;
    498 	else
    499 		return 0.0f;
    500 }
    501 
    502 void BoolLiteral::tokenize (GeneratorState& state, TokenStream& str) const
    503 {
    504 	DE_UNREF(state);
    505 	str << Token(m_value.getValue(VariableType::getScalarType(VariableType::TYPE_BOOL)).asBool(0));
    506 }
    507 
    508 namespace
    509 {
    510 
    511 // \note int-bool and float-bool conversions handled in a special way.
    512 template <typename SrcType, typename DstType>
    513 inline DstType convert (SrcType src)
    514 {
    515 	if (Scalar::min<SrcType>() == src)
    516 		return Scalar::min<DstType>().template as<DstType>();
    517 	else if (Scalar::max<SrcType>() == src)
    518 		return Scalar::max<DstType>().template as<DstType>();
    519 	else
    520 		return DstType(src);
    521 }
    522 
    523 // According to GLSL ES spec.
    524 template <> inline bool		convert<float, bool>	(float src)	{ return src != 0.0f;					}
    525 template <> inline bool		convert<int, bool>		(int src)	{ return src != 0;						}
    526 template <> inline bool		convert<bool, bool>		(bool src)	{ return src;							}
    527 template <> inline float	convert<bool, float>	(bool src)	{ return src ? 1.0f : 0.0f;				}
    528 template <> inline int		convert<bool, int>		(bool src)	{ return src ? 1 : 0;					}
    529 
    530 template <> inline int convert<float, int> (float src)
    531 {
    532 	if (Scalar::min<float>() == src)
    533 		return Scalar::min<int>().as<int>();
    534 	else if (Scalar::max<float>() == src)
    535 		return Scalar::max<int>().as<int>();
    536 	else if (src > 0.0f)
    537 		return (int)deFloatFloor(src);
    538 	else
    539 		return (int)deFloatCeil(src);
    540 }
    541 
    542 template <typename SrcType, typename DstType>
    543 inline void convertValueRange (SrcType srcMin, SrcType srcMax, DstType& dstMin, DstType& dstMax)
    544 {
    545 	dstMin = convert<SrcType, DstType>(srcMin);
    546 	dstMax = convert<SrcType, DstType>(srcMax);
    547 }
    548 
    549 template <>
    550 inline void convertValueRange<float, int> (float srcMin, float srcMax, int& dstMin, int& dstMax)
    551 {
    552 	if (Scalar::min<float>() == srcMin)
    553 		dstMin = Scalar::min<int>().as<int>();
    554 	else
    555 		dstMin = (int)deFloatCeil(srcMin);
    556 
    557 	if (Scalar::max<float>() == srcMax)
    558 		dstMax = Scalar::max<int>().as<int>();
    559 	else
    560 		dstMax = (int)deFloatFloor(srcMax);
    561 }
    562 
    563 template <>
    564 inline void convertValueRange<float, bool> (float srcMin, float srcMax, bool& dstMin, bool& dstMax)
    565 {
    566 	dstMin = srcMin > 0.0f;
    567 	dstMax = srcMax > 0.0f;
    568 }
    569 
    570 // \todo [pyry] More special cases?
    571 
    572 // Returns whether it is possible to convert some SrcType value range to given DstType valueRange
    573 template <typename SrcType, typename DstType>
    574 bool isConversionOk (DstType min, DstType max)
    575 {
    576 	SrcType sMin, sMax;
    577 	convertValueRange(min, max, sMin, sMax);
    578 	return sMin <= sMax &&
    579 		   de::inRange(convert<SrcType, DstType>(sMin), min, max) &&
    580 		   de::inRange(convert<SrcType, DstType>(sMax), min, max);
    581 }
    582 
    583 // Work-around for non-deterministic float behavior
    584 template <> bool isConversionOk<float, float> (float, float) { return true; }
    585 
    586 // \todo [2011-03-26 pyry] Provide this in ValueAccess?
    587 template <typename T>	T				getValueAccessValue			(ConstValueAccess access);
    588 template<>				inline float	getValueAccessValue<float>	(ConstValueAccess access) { return access.asFloat();	}
    589 template<>				inline int		getValueAccessValue<int>	(ConstValueAccess access) { return access.asInt();		}
    590 template<>				inline bool		getValueAccessValue<bool>	(ConstValueAccess access) { return access.asBool();		}
    591 
    592 template <typename T>	T&				getValueAccessValue			(ValueAccess access);
    593 template<>				inline float&	getValueAccessValue<float>	(ValueAccess access) { return access.asFloat();		}
    594 template<>				inline int&		getValueAccessValue<int>	(ValueAccess access) { return access.asInt();		}
    595 template<>				inline bool&	getValueAccessValue<bool>	(ValueAccess access) { return access.asBool();		}
    596 
    597 template <typename SrcType, typename DstType>
    598 bool isConversionOk (ConstValueRangeAccess valueRange)
    599 {
    600 	return isConversionOk<SrcType>(getValueAccessValue<DstType>(valueRange.getMin()), getValueAccessValue<DstType>(valueRange.getMax()));
    601 }
    602 
    603 template <typename SrcType, typename DstType>
    604 void convertValueRangeTempl (ConstValueRangeAccess src, ValueRangeAccess dst)
    605 {
    606 	DstType dMin, dMax;
    607 	convertValueRange(getValueAccessValue<SrcType>(src.getMin()), getValueAccessValue<SrcType>(src.getMax()), dMin, dMax);
    608 	getValueAccessValue<DstType>(dst.getMin()) = dMin;
    609 	getValueAccessValue<DstType>(dst.getMax()) = dMax;
    610 }
    611 
    612 template <typename SrcType, typename DstType>
    613 void convertExecValueTempl (ExecConstValueAccess src, ExecValueAccess dst)
    614 {
    615 	for (int ndx = 0; ndx < EXEC_VEC_WIDTH; ndx++)
    616 		dst.as<DstType>(ndx) = convert<SrcType, DstType>(src.as<SrcType>(ndx));
    617 }
    618 
    619 typedef bool (*IsConversionOkFunc)		(ConstValueRangeAccess);
    620 typedef void (*ConvertValueRangeFunc)	(ConstValueRangeAccess, ValueRangeAccess);
    621 typedef void (*ConvertExecValueFunc)	(ExecConstValueAccess, ExecValueAccess);
    622 
    623 inline int getBaseTypeConvNdx (VariableType::Type type)
    624 {
    625 	switch (type)
    626 	{
    627 		case VariableType::TYPE_FLOAT:	return 0;
    628 		case VariableType::TYPE_INT:	return 1;
    629 		case VariableType::TYPE_BOOL:	return 2;
    630 		default:						return -1;
    631 	}
    632 }
    633 
    634 bool isConversionOk (VariableType::Type srcType, VariableType::Type dstType, ConstValueRangeAccess valueRange)
    635 {
    636 	// [src][dst]
    637 	static const IsConversionOkFunc convTable[3][3] =
    638 	{
    639 		{ isConversionOk<float, float>, isConversionOk<float,	int>,	isConversionOk<float,	bool>	},
    640 		{ isConversionOk<int,	float>,	isConversionOk<int,		int>,	isConversionOk<int,		bool>	},
    641 		{ isConversionOk<bool,	float>,	isConversionOk<bool,	int>,	isConversionOk<bool,	bool>	}
    642 	};
    643 	return convTable[getBaseTypeConvNdx(srcType)][getBaseTypeConvNdx(dstType)](valueRange);
    644 }
    645 
    646 void convertValueRange (ConstValueRangeAccess src, ValueRangeAccess dst)
    647 {
    648 	// [src][dst]
    649 	static const ConvertValueRangeFunc convTable[3][3] =
    650 	{
    651 		{ convertValueRangeTempl<float, float>, convertValueRangeTempl<float,	int>,	convertValueRangeTempl<float,	bool>	},
    652 		{ convertValueRangeTempl<int,	float>,	convertValueRangeTempl<int,		int>,	convertValueRangeTempl<int,		bool>	},
    653 		{ convertValueRangeTempl<bool,	float>,	convertValueRangeTempl<bool,	int>,	convertValueRangeTempl<bool,	bool>	}
    654 	};
    655 
    656 	convTable[getBaseTypeConvNdx(src.getType().getBaseType())][getBaseTypeConvNdx(dst.getType().getBaseType())](src, dst);
    657 }
    658 
    659 void convertExecValue (ExecConstValueAccess src, ExecValueAccess dst)
    660 {
    661 	// [src][dst]
    662 	static const ConvertExecValueFunc convTable[3][3] =
    663 	{
    664 		{ convertExecValueTempl<float,	float>,	convertExecValueTempl<float,	int>,	convertExecValueTempl<float,	bool>	},
    665 		{ convertExecValueTempl<int,	float>,	convertExecValueTempl<int,		int>,	convertExecValueTempl<int,		bool>	},
    666 		{ convertExecValueTempl<bool,	float>,	convertExecValueTempl<bool,		int>,	convertExecValueTempl<bool,		bool>	}
    667 	};
    668 
    669 	convTable[getBaseTypeConvNdx(src.getType().getBaseType())][getBaseTypeConvNdx(dst.getType().getBaseType())](src, dst);
    670 }
    671 
    672 } // anonymous
    673 
    674 ConstructorOp::ConstructorOp (GeneratorState& state, ConstValueRangeAccess valueRange)
    675 	: m_valueRange(valueRange)
    676 {
    677 	if (valueRange.getType().isVoid())
    678 	{
    679 		// Use random range
    680 		const int maxScalars = 4; // We don't have to be able to assign this value to anywhere
    681 		m_valueRange = ValueRange(computeRandomType(state, maxScalars));
    682 		computeRandomValueRange(state, m_valueRange.asAccess());
    683 	}
    684 
    685 	// \todo [2011-03-26 pyry] Vector conversions
    686 //	int						remainingDepth	= state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
    687 
    688 	const VariableType&		type			= m_valueRange.getType();
    689 	VariableType::Type		baseType		= type.getBaseType();
    690 	int						numScalars		= type.getNumElements();
    691 	int						curScalarNdx	= 0;
    692 
    693 	// \todo [2011-03-26 pyry] Separate op for struct constructors!
    694 	DE_ASSERT(type.isFloatOrVec() || type.isIntOrVec() || type.isBoolOrVec());
    695 
    696 	bool scalarConversions = state.getProgramParameters().useScalarConversions;
    697 
    698 	while (curScalarNdx < numScalars)
    699 	{
    700 		ConstValueRangeAccess comp = m_valueRange.asAccess().component(curScalarNdx);
    701 
    702 		if (scalarConversions)
    703 		{
    704 			int					numInTypes = 0;
    705 			VariableType::Type	inTypes[3];
    706 
    707 			if (isConversionOk(VariableType::TYPE_FLOAT, baseType, comp))	inTypes[numInTypes++] = VariableType::TYPE_FLOAT;
    708 			if (isConversionOk(VariableType::TYPE_INT, baseType, comp))		inTypes[numInTypes++] = VariableType::TYPE_INT;
    709 			if (isConversionOk(VariableType::TYPE_BOOL, baseType, comp))	inTypes[numInTypes++] = VariableType::TYPE_BOOL;
    710 
    711 			DE_ASSERT(numInTypes > 0); // At least nop conversion should be ok
    712 
    713 			// Choose random
    714 			VariableType::Type inType = state.getRandom().choose<VariableType::Type>(&inTypes[0], &inTypes[0] + numInTypes);
    715 
    716 			// Compute converted value range
    717 			ValueRange inValueRange(VariableType(inType, 1));
    718 			convertValueRange(comp, inValueRange);
    719 			m_inputValueRanges.push_back(inValueRange);
    720 
    721 			curScalarNdx += 1;
    722 		}
    723 		else
    724 		{
    725 			m_inputValueRanges.push_back(ValueRange(comp));
    726 			curScalarNdx += 1;
    727 		}
    728 	}
    729 }
    730 
    731 ConstructorOp::~ConstructorOp (void)
    732 {
    733 	for (vector<Expression*>::iterator i = m_inputExpressions.begin(); i != m_inputExpressions.end(); i++)
    734 		delete *i;
    735 }
    736 
    737 Expression* ConstructorOp::createNextChild (GeneratorState& state)
    738 {
    739 	int					numChildren	= (int)m_inputExpressions.size();
    740 	Expression*			child		= DE_NULL;
    741 
    742 	// \note Created in reverse order!
    743 	if (numChildren < (int)m_inputValueRanges.size())
    744 	{
    745 		const ValueRange& inValueRange = m_inputValueRanges[m_inputValueRanges.size()-1-numChildren];
    746 		child = Expression::createRandom(state, inValueRange);
    747 		try
    748 		{
    749 			m_inputExpressions.push_back(child);
    750 		}
    751 		catch (const std::exception&)
    752 		{
    753 			delete child;
    754 			throw;
    755 		}
    756 	}
    757 
    758 	return child;
    759 }
    760 
    761 float ConstructorOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
    762 {
    763 	if (valueRange.getType().isVoid())
    764 		return unusedValueWeight;
    765 
    766 	if (!valueRange.getType().isFloatOrVec() && !valueRange.getType().isIntOrVec() && !valueRange.getType().isBoolOrVec())
    767 		return 0.0f;
    768 
    769 	if (state.getExpressionDepth() + getTypeConstructorDepth(valueRange.getType()) > state.getShaderParameters().maxExpressionDepth)
    770 		return 0.0f;
    771 
    772 	return 1.0f;
    773 }
    774 
    775 void ConstructorOp::tokenize (GeneratorState& state, TokenStream& str) const
    776 {
    777 	const VariableType& type = m_valueRange.getType();
    778 	DE_ASSERT(type.getPrecision() == VariableType::PRECISION_NONE);
    779 	type.tokenizeShortType(str);
    780 
    781 	str << Token::LEFT_PAREN;
    782 
    783 	for (vector<Expression*>::const_reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend(); i++)
    784 	{
    785 		if (i != m_inputExpressions.rbegin())
    786 			str << Token::COMMA;
    787 		(*i)->tokenize(state, str);
    788 	}
    789 
    790 	str << Token::RIGHT_PAREN;
    791 }
    792 
    793 void ConstructorOp::evaluate (ExecutionContext& evalCtx)
    794 {
    795 	// Evaluate children
    796 	for (vector<Expression*>::reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend(); i++)
    797 		(*i)->evaluate(evalCtx);
    798 
    799 	// Compute value
    800 	const VariableType& type = m_valueRange.getType();
    801 	m_value.setStorage(type);
    802 
    803 	ExecValueAccess	dst				= m_value.getValue(type);
    804 	int				curScalarNdx	= 0;
    805 
    806 	for (vector<Expression*>::reverse_iterator i = m_inputExpressions.rbegin(); i != m_inputExpressions.rend(); i++)
    807 	{
    808 		ExecConstValueAccess src = (*i)->getValue();
    809 
    810 		for (int elemNdx = 0; elemNdx < src.getType().getNumElements(); elemNdx++)
    811 			convertExecValue(src.component(elemNdx), dst.component(curScalarNdx++));
    812 	}
    813 }
    814 
    815 AssignOp::AssignOp (GeneratorState& state, ConstValueRangeAccess valueRange)
    816 	: m_valueRange	(valueRange)
    817 	, m_lvalueExpr	(DE_NULL)
    818 	, m_rvalueExpr	(DE_NULL)
    819 {
    820 	if (m_valueRange.getType().isVoid())
    821 	{
    822 		// Compute random value range
    823 		int		maxScalars		= state.getShaderParameters().maxCombinedVariableScalars - state.getVariableManager().getNumAllocatedScalars();
    824 		bool	useRandomRange	= !state.getVariableManager().hasEntry<IsWritableEntry>() || ((maxScalars > 0) && getWeightedBool(state.getRandom(), 0.1f));
    825 
    826 		if (useRandomRange)
    827 		{
    828 			DE_ASSERT(maxScalars > 0);
    829 			m_valueRange = ValueRange(computeRandomType(state, maxScalars));
    830 			computeRandomValueRange(state, m_valueRange.asAccess());
    831 		}
    832 		else
    833 		{
    834 			// Use value range from random entry
    835 			// \todo [2011-02-28 pyry] Give lower weight to entries without range? Choose subtype range?
    836 			const ValueEntry* entry = state.getRandom().choose<const ValueEntry*>(state.getVariableManager().getBegin<IsWritableEntry>(), state.getVariableManager().getEnd<IsWritableEntry>());
    837 			m_valueRange = ValueRange(entry->getValueRange());
    838 
    839 			computeRandomValueRangeForInfElements(state, m_valueRange.asAccess());
    840 
    841 			DE_ASSERT(state.getVariableManager().hasEntry(IsWritableIntersectingEntry(m_valueRange.asAccess())));
    842 		}
    843 	}
    844 
    845 	IsWritableIntersectingEntry::Iterator first	= state.getVariableManager().getBegin(IsWritableIntersectingEntry(m_valueRange.asAccess()));
    846 	IsWritableIntersectingEntry::Iterator end	= state.getVariableManager().getEnd(IsWritableIntersectingEntry(m_valueRange.asAccess()));
    847 
    848 	bool possiblyCreateVar = canAllocateVariable(state, m_valueRange.getType()) &&
    849 							 (first == end || getWeightedBool(state.getRandom(), 0.5f));
    850 
    851 	if (!possiblyCreateVar)
    852 	{
    853 		// Find all possible valueranges matching given type and intersecting with valuerange
    854 		// \todo [pyry] Actually collect all ValueRanges, currently operates only on whole variables
    855 		DE_ASSERT(first != end);
    856 
    857 		// Try to select one closest to given range but bigger (eg. superset)
    858 		bool supersetExists = false;
    859 		for (IsWritableIntersectingEntry::Iterator i = first; i != end; i++)
    860 		{
    861 			if ((*i)->getValueRange().isSupersetOf(m_valueRange.asAccess()))
    862 			{
    863 				supersetExists = true;
    864 				break;
    865 			}
    866 		}
    867 
    868 		if (!supersetExists)
    869 		{
    870 			// Select some other range and compute intersection
    871 			// \todo [2011-02-03 pyry] Use some heuristics to select the range?
    872 			ConstValueRangeAccess selectedRange = state.getRandom().choose<const ValueEntry*>(first, end)->getValueRange();
    873 
    874 			ValueRange::computeIntersection(m_valueRange.asAccess(), m_valueRange.asAccess(), selectedRange);
    875 		}
    876 	}
    877 }
    878 
    879 AssignOp::~AssignOp (void)
    880 {
    881 	delete m_lvalueExpr;
    882 	delete m_rvalueExpr;
    883 }
    884 
    885 float AssignOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
    886 {
    887 	if (!valueRange.getType().isVoid() &&
    888 		!canAllocateVariable(state, valueRange.getType()) &&
    889 		!state.getVariableManager().hasEntry(IsWritableIntersectingEntry(valueRange)))
    890 		return 0.0f; // Would require creating a new variable
    891 
    892 	if (!valueRange.getType().isVoid() && state.getExpressionDepth() + getTypeConstructorDepth(valueRange.getType()) + 1 >= state.getShaderParameters().maxExpressionDepth)
    893 		return 0.0f;
    894 
    895 	if (valueRange.getType().isVoid() &&
    896 		!state.getVariableManager().hasEntry<IsWritableEntry>() &&
    897 		state.getVariableManager().getNumAllocatedScalars() >= state.getShaderParameters().maxCombinedVariableScalars)
    898 		return 0.0f; // Can not allocate a new entry
    899 
    900 	if (state.getExpressionDepth() == 0)
    901 		return 4.0f;
    902 	else
    903 		return 0.0f; // \todo [pyry] Fix assign ops
    904 }
    905 
    906 Expression* AssignOp::createNextChild (GeneratorState& state)
    907 {
    908 	if (m_lvalueExpr == DE_NULL)
    909 	{
    910 		// Construct lvalue
    911 		// \todo [2011-03-14 pyry] Proper l-value generation:
    912 		//  - pure L-value part is generated first
    913 		//  - variable valuerange is made unbound
    914 		//  - R-value is generated
    915 		//  - R-values in L-value are generated
    916 		m_lvalueExpr = Expression::createRandomLValue(state, m_valueRange.asAccess());
    917 		return m_lvalueExpr;
    918 	}
    919 	else if (m_rvalueExpr == DE_NULL)
    920 	{
    921 		// Construct value expr
    922 		m_rvalueExpr = Expression::createRandom(state, m_valueRange.asAccess());
    923 		return m_rvalueExpr;
    924 	}
    925 	else
    926 		return DE_NULL;
    927 }
    928 
    929 void AssignOp::tokenize (GeneratorState& state, TokenStream& str) const
    930 {
    931 	m_lvalueExpr->tokenize(state, str);
    932 	str << Token::EQUAL;
    933 	m_rvalueExpr->tokenize(state, str);
    934 }
    935 
    936 void AssignOp::evaluate (ExecutionContext& evalCtx)
    937 {
    938 	// Evaluate l-value
    939 	m_lvalueExpr->evaluate(evalCtx);
    940 
    941 	// Evaluate value
    942 	m_rvalueExpr->evaluate(evalCtx);
    943 	m_value.setStorage(m_valueRange.getType());
    944 	m_value.getValue(m_valueRange.getType()) = m_rvalueExpr->getValue().value();
    945 
    946 	// Assign
    947 	assignMasked(m_lvalueExpr->getLValue(), m_value.getValue(m_valueRange.getType()), evalCtx.getExecutionMask());
    948 }
    949 
    950 namespace
    951 {
    952 
    953 inline bool isShaderInOutSupportedType (const VariableType& type)
    954 {
    955 	// \todo [2011-03-11 pyry] Float arrays, structs?
    956 	return type.getBaseType() == VariableType::TYPE_FLOAT;
    957 }
    958 
    959 Variable* allocateNewVariable (GeneratorState& state, ConstValueRangeAccess valueRange)
    960 {
    961 	Variable* variable = state.getVariableManager().allocate(valueRange.getType());
    962 
    963 	// Update value range
    964 	state.getVariableManager().setValue(variable, valueRange);
    965 
    966 	// Random storage \todo [pyry] Check that scalar count in uniform/input classes is not exceeded
    967 	static const Variable::Storage storages[] =
    968 	{
    969 		Variable::STORAGE_CONST,
    970 		Variable::STORAGE_UNIFORM,
    971 		Variable::STORAGE_LOCAL,
    972 		Variable::STORAGE_SHADER_IN
    973 	};
    974 	float weights[DE_LENGTH_OF_ARRAY(storages)];
    975 
    976 	// Dynamic vs. constant weight.
    977 	float	dynWeight	= computeDynamicRangeWeight(valueRange);
    978 	int		numScalars	= valueRange.getType().getScalarSize();
    979 	bool	uniformOk	= state.getVariableManager().getNumAllocatedUniformScalars() + numScalars <= state.getShaderParameters().maxUniformScalars;
    980 	bool	shaderInOk	= isShaderInOutSupportedType(valueRange.getType()) &&
    981 						  (state.getVariableManager().getNumAllocatedShaderInVariables() + NUM_RESERVED_SHADER_INPUTS < state.getShaderParameters().maxInputVariables);
    982 
    983 	weights[0] = de::max(1.0f-dynWeight, 0.1f);
    984 	weights[1] = uniformOk ? dynWeight*0.5f : 0.0f;
    985 	weights[2] = dynWeight;
    986 	weights[3] = shaderInOk ? dynWeight*2.0f : 0.0f;
    987 
    988 	state.getVariableManager().setStorage(variable, state.getRandom().chooseWeighted<Variable::Storage>(&storages[0], &storages[DE_LENGTH_OF_ARRAY(storages)], &weights[0]));
    989 
    990 	return variable;
    991 }
    992 
    993 inline float combineWeight (float curCombinedWeight, float partialWeight)
    994 {
    995 	return curCombinedWeight * partialWeight;
    996 }
    997 
    998 float computeEntryReadWeight (ConstValueRangeAccess entryValueRange, ConstValueRangeAccess readValueRange)
    999 {
   1000 	const VariableType& type = entryValueRange.getType();
   1001 	DE_ASSERT(type == readValueRange.getType());
   1002 
   1003 	float weight = 1.0f;
   1004 
   1005 	switch (type.getBaseType())
   1006 	{
   1007 		case VariableType::TYPE_FLOAT:
   1008 		{
   1009 			for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
   1010 			{
   1011 				float entryMin	= entryValueRange.component(elemNdx).getMin().asFloat();
   1012 				float entryMax	= entryValueRange.component(elemNdx).getMax().asFloat();
   1013 				float readMin	= readValueRange.component(elemNdx).getMin().asFloat();
   1014 				float readMax	= readValueRange.component(elemNdx).getMax().asFloat();
   1015 
   1016 				// Check for -inf..inf ranges - they don't bring down the weight.
   1017 				if (Scalar::min<float>() == entryMin && Scalar::max<float>() == entryMax)
   1018 					continue;
   1019 
   1020 				// Intersection to entry value range length ratio.
   1021 				float intersectionMin		= deFloatMax(entryMin, readMin);
   1022 				float intersectionMax		= deFloatMin(entryMax, readMax);
   1023 				float entryRangeLen			= entryMax - entryMin;
   1024 				float readRangeLen			= readMax - readMin;
   1025 				float intersectionLen		= intersectionMax - intersectionMin;
   1026 				float entryRatio			= (entryRangeLen	> 0.0f) ? (intersectionLen / entryRangeLen)	: 1.0f;
   1027 				float readRatio				= (readRangeLen		> 0.0f) ? (intersectionLen / readRangeLen)	: 1.0f;
   1028 				float elementWeight			= 0.5f*readRatio + 0.5f*entryRatio;
   1029 
   1030 				weight = combineWeight(weight, elementWeight);
   1031 			}
   1032 			break;
   1033 		}
   1034 
   1035 		case VariableType::TYPE_INT:
   1036 		{
   1037 			for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
   1038 			{
   1039 				int entryMin	= entryValueRange.component(elemNdx).getMin().asInt();
   1040 				int entryMax	= entryValueRange.component(elemNdx).getMax().asInt();
   1041 				int readMin		= readValueRange.component(elemNdx).getMin().asInt();
   1042 				int readMax		= readValueRange.component(elemNdx).getMax().asInt();
   1043 
   1044 				// Check for -inf..inf ranges - they don't bring down the weight.
   1045 				if (Scalar::min<int>() == entryMin && Scalar::max<int>() == entryMax)
   1046 					continue;
   1047 
   1048 				// Intersection to entry value range length ratio.
   1049 				int		intersectionMin			= deMax32(entryMin, readMin);
   1050 				int		intersectionMax			= deMin32(entryMax, readMax);
   1051 				deInt64 entryRangeLen			= (deInt64)entryMax - (deInt64)entryMin;
   1052 				deInt64	readRangeLen			= (deInt64)readMax - (deInt64)readMin;
   1053 				deInt64	intersectionLen			= (deInt64)intersectionMax - (deInt64)intersectionMin;
   1054 				float	entryRatio				= (entryRangeLen	> 0) ? ((float)intersectionLen / (float)entryRangeLen)	: 1.0f;
   1055 				float	readRatio				= (readRangeLen		> 0) ? ((float)intersectionLen / (float)readRangeLen)	: 1.0f;
   1056 				float 	elementWeight			= 0.5f*readRatio + 0.5f*entryRatio;
   1057 
   1058 				weight = combineWeight(weight, elementWeight);
   1059 			}
   1060 			break;
   1061 		}
   1062 
   1063 		case VariableType::TYPE_BOOL:
   1064 		{
   1065 			// \todo
   1066 			break;
   1067 		}
   1068 
   1069 
   1070 		case VariableType::TYPE_ARRAY:
   1071 		case VariableType::TYPE_STRUCT:
   1072 
   1073 		default:
   1074 			TCU_FAIL("Unsupported type");
   1075 	}
   1076 
   1077 	return deFloatMax(weight, 0.01f);
   1078 }
   1079 
   1080 } // anonymous
   1081 
   1082 VariableRead::VariableRead (GeneratorState& state, ConstValueRangeAccess valueRange)
   1083 {
   1084 	if (valueRange.getType().isVoid())
   1085 	{
   1086 		IsReadableEntry	filter			= IsReadableEntry(state.getExpressionFlags());
   1087 		int				maxScalars		= state.getShaderParameters().maxCombinedVariableScalars - state.getVariableManager().getNumAllocatedScalars();
   1088 		bool			useRandomRange	= !state.getVariableManager().hasEntry(filter) || ((maxScalars > 0) && getWeightedBool(state.getRandom(), 0.5f));
   1089 
   1090 		if (useRandomRange)
   1091 		{
   1092 			// Allocate a new variable
   1093 			DE_ASSERT(maxScalars > 0);
   1094 			ValueRange newVarRange(computeRandomType(state, maxScalars));
   1095 			computeRandomValueRange(state, newVarRange.asAccess());
   1096 
   1097 			m_variable = allocateNewVariable(state, newVarRange.asAccess());
   1098 		}
   1099 		else
   1100 		{
   1101 			// Use random entry \todo [pyry] Handle -inf..inf ranges?
   1102 			m_variable = state.getRandom().choose<const ValueEntry*>(state.getVariableManager().getBegin(filter), state.getVariableManager().getEnd(filter))->getVariable();
   1103 		}
   1104 	}
   1105 	else
   1106 	{
   1107 		// Find variable that has value range that intersects with given range
   1108 		IsReadableIntersectingEntry::Iterator	first	= state.getVariableManager().getBegin(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags()));
   1109 		IsReadableIntersectingEntry::Iterator	end		= state.getVariableManager().getEnd(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags()));
   1110 
   1111 		const float	createOnReadWeight		= 0.5f;
   1112 		bool		createVar				= canAllocateVariable(state, valueRange.getType()) && (first == end || getWeightedBool(state.getRandom(), createOnReadWeight));
   1113 
   1114 		if (createVar)
   1115 		{
   1116 			m_variable = allocateNewVariable(state, valueRange);
   1117 		}
   1118 		else
   1119 		{
   1120 			// Copy value entries for computing weights.
   1121 			std::vector<const ValueEntry*>	availableVars;
   1122 			std::vector<float>				weights;
   1123 
   1124 			std::copy(first, end, std::inserter(availableVars, availableVars.begin()));
   1125 
   1126 			// Compute weights.
   1127 			weights.resize(availableVars.size());
   1128 			for (int ndx = 0; ndx < (int)availableVars.size(); ndx++)
   1129 				weights[ndx] = computeEntryReadWeight(availableVars[ndx]->getValueRange(), valueRange);
   1130 
   1131 			// Select.
   1132 			const ValueEntry* entry = state.getRandom().chooseWeighted<const ValueEntry*>(availableVars.begin(), availableVars.end(), weights.begin());
   1133 			m_variable = entry->getVariable();
   1134 
   1135 			// Compute intersection
   1136 			ValueRange intersection(m_variable->getType());
   1137 			ValueRange::computeIntersection(intersection, entry->getValueRange(), valueRange);
   1138 			state.getVariableManager().setValue(m_variable, intersection.asAccess());
   1139 		}
   1140 	}
   1141 }
   1142 
   1143 VariableRead::VariableRead (const Variable* variable)
   1144 {
   1145 	m_variable = variable;
   1146 }
   1147 
   1148 float VariableRead::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
   1149 {
   1150 	if (valueRange.getType().isVoid())
   1151 	{
   1152 		if (state.getVariableManager().hasEntry(IsReadableEntry(state.getExpressionFlags())) ||
   1153 			state.getVariableManager().getNumAllocatedScalars() < state.getShaderParameters().maxCombinedVariableScalars)
   1154 			return unusedValueWeight;
   1155 		else
   1156 			return 0.0f;
   1157 	}
   1158 
   1159 	if (!canAllocateVariable(state, valueRange.getType()) &&
   1160 		!state.getVariableManager().hasEntry(IsReadableIntersectingEntry(valueRange, state.getExpressionFlags())))
   1161 		return 0.0f;
   1162 	else
   1163 		return 1.0f;
   1164 }
   1165 
   1166 VariableWrite::VariableWrite (GeneratorState& state, ConstValueRangeAccess valueRange)
   1167 {
   1168 	DE_ASSERT(!valueRange.getType().isVoid());
   1169 
   1170 	// Find variable with range that is superset of given range
   1171 	IsWritableSupersetEntry::Iterator	first	= state.getVariableManager().getBegin(IsWritableSupersetEntry(valueRange));
   1172 	IsWritableSupersetEntry::Iterator	end		= state.getVariableManager().getEnd(IsWritableSupersetEntry(valueRange));
   1173 
   1174 	const float	createOnAssignWeight	= 0.1f; // Will essentially create an unused variable
   1175 	bool		createVar				= canAllocateVariable(state, valueRange.getType()) && (first == end || getWeightedBool(state.getRandom(), createOnAssignWeight));
   1176 
   1177 	if (createVar)
   1178 	{
   1179 		m_variable = state.getVariableManager().allocate(valueRange.getType());
   1180 		// \note Storage will be LOCAL
   1181 	}
   1182 	else
   1183 	{
   1184 		// Choose random
   1185 		DE_ASSERT(first != end);
   1186 		const ValueEntry* entry = state.getRandom().choose<const ValueEntry*>(first, end);
   1187 		m_variable = entry->getVariable();
   1188 	}
   1189 
   1190 	DE_ASSERT(m_variable);
   1191 
   1192 	// Reset value range.
   1193 	const ValueEntry* parentEntry = state.getVariableManager().getParentValue(m_variable);
   1194 	if (parentEntry)
   1195 	{
   1196 		// Use parent value range.
   1197 		state.getVariableManager().setValue(m_variable, parentEntry->getValueRange());
   1198 	}
   1199 	else
   1200 	{
   1201 		// Use infinite range.
   1202 		ValueRange infRange(m_variable->getType());
   1203 		setInfiniteRange(infRange);
   1204 
   1205 		state.getVariableManager().setValue(m_variable, infRange.asAccess());
   1206 	}
   1207 }
   1208 
   1209 float VariableWrite::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
   1210 {
   1211 	if (!canAllocateVariable(state, valueRange.getType()) &&
   1212 		!state.getVariableManager().hasEntry(IsWritableSupersetEntry(valueRange)))
   1213 		return 0.0f;
   1214 	else
   1215 		return 1.0f;
   1216 }
   1217 
   1218 void VariableAccess::evaluate (ExecutionContext& evalCtx)
   1219 {
   1220 	m_valueAccess = evalCtx.getValue(m_variable);
   1221 }
   1222 
   1223 ParenOp::ParenOp (GeneratorState& state, ConstValueRangeAccess valueRange)
   1224 	: m_valueRange	(valueRange)
   1225 	, m_child		(DE_NULL)
   1226 {
   1227 	DE_UNREF(state);
   1228 }
   1229 
   1230 ParenOp::~ParenOp (void)
   1231 {
   1232 	delete m_child;
   1233 }
   1234 
   1235 Expression* ParenOp::createNextChild (GeneratorState& state)
   1236 {
   1237 	if (m_child == DE_NULL)
   1238 	{
   1239 		m_child = Expression::createRandom(state, m_valueRange.asAccess());
   1240 		return m_child;
   1241 	}
   1242 	else
   1243 		return DE_NULL;
   1244 }
   1245 
   1246 void ParenOp::tokenize (GeneratorState& state, TokenStream& str) const
   1247 {
   1248 	str << Token::LEFT_PAREN;
   1249 	m_child->tokenize(state, str);
   1250 	str << Token::RIGHT_PAREN;
   1251 }
   1252 
   1253 float ParenOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
   1254 {
   1255 	if (valueRange.getType().isVoid())
   1256 		return state.getExpressionDepth() + 2 <= state.getShaderParameters().maxExpressionDepth ? unusedValueWeight : 0.0f;
   1257 	else
   1258 	{
   1259 		int requiredDepth = 1 + getConservativeValueExprDepth(state, valueRange);
   1260 		return state.getExpressionDepth() + requiredDepth <= state.getShaderParameters().maxExpressionDepth ? 1.0f : 0.0f;
   1261 	}
   1262 }
   1263 
   1264 const int swizzlePrecedence = 2;
   1265 
   1266 SwizzleOp::SwizzleOp (GeneratorState& state, ConstValueRangeAccess valueRange)
   1267 	: m_outValueRange		(valueRange)
   1268 	, m_numInputElements	(0)
   1269 	, m_child				(DE_NULL)
   1270 {
   1271 	DE_ASSERT(!m_outValueRange.getType().isVoid()); // \todo [2011-06-13 pyry] Void support
   1272 	DE_ASSERT(m_outValueRange.getType().isFloatOrVec()	||
   1273 			  m_outValueRange.getType().isIntOrVec()	||
   1274 			  m_outValueRange.getType().isBoolOrVec());
   1275 
   1276 	m_value.setStorage(m_outValueRange.getType());
   1277 
   1278 	int numOutputElements	= m_outValueRange.getType().getNumElements();
   1279 
   1280 	// \note Swizzle works for vector types only.
   1281 	// \todo [2011-06-13 pyry] Use components multiple times.
   1282 	m_numInputElements		= state.getRandom().getInt(deMax32(numOutputElements, 2), 4);
   1283 
   1284 	std::set<int> availableElements;
   1285 	for (int ndx = 0; ndx < m_numInputElements; ndx++)
   1286 		availableElements.insert(ndx);
   1287 
   1288 	// Randomize swizzle.
   1289 	for (int elemNdx = 0; elemNdx < (int)DE_LENGTH_OF_ARRAY(m_swizzle); elemNdx++)
   1290 	{
   1291 		if (elemNdx < numOutputElements)
   1292 		{
   1293 			int inElemNdx = state.getRandom().choose<int>(availableElements.begin(), availableElements.end());
   1294 			availableElements.erase(inElemNdx);
   1295 			m_swizzle[elemNdx] = (deUint8)inElemNdx;
   1296 		}
   1297 		else
   1298 			m_swizzle[elemNdx] = 0;
   1299 	}
   1300 }
   1301 
   1302 SwizzleOp::~SwizzleOp (void)
   1303 {
   1304 	delete m_child;
   1305 }
   1306 
   1307 Expression* SwizzleOp::createNextChild (GeneratorState& state)
   1308 {
   1309 	if (m_child)
   1310 		return DE_NULL;
   1311 
   1312 	// Compute input value range.
   1313 	VariableType	inVarType		= VariableType(m_outValueRange.getType().getBaseType(), m_numInputElements);
   1314 	ValueRange		inValueRange	= ValueRange(inVarType);
   1315 
   1316 	// Initialize all inputs to -inf..inf
   1317 	setInfiniteRange(inValueRange);
   1318 
   1319 	// Compute intersections.
   1320 	int numOutputElements = m_outValueRange.getType().getNumElements();
   1321 	for (int outElemNdx = 0; outElemNdx < numOutputElements; outElemNdx++)
   1322 	{
   1323 		int inElemNdx = m_swizzle[outElemNdx];
   1324 		ValueRange::computeIntersection(inValueRange.asAccess().component(inElemNdx), inValueRange.asAccess().component(inElemNdx), m_outValueRange.asAccess().component(outElemNdx));
   1325 	}
   1326 
   1327 	// Create child.
   1328 	state.pushPrecedence(swizzlePrecedence);
   1329 	m_child = Expression::createRandom(state, inValueRange.asAccess());
   1330 	state.popPrecedence();
   1331 
   1332 	return m_child;
   1333 }
   1334 
   1335 void SwizzleOp::tokenize (GeneratorState& state, TokenStream& str) const
   1336 {
   1337 	const char*		rgbaSet[]	= { "r", "g", "b", "a" };
   1338 	const char*		xyzwSet[]	= { "x", "y", "z", "w" };
   1339 	const char*		stpqSet[]	= { "s", "t", "p", "q" };
   1340 	const char**	swizzleSet	= DE_NULL;
   1341 
   1342 	switch (state.getRandom().getInt(0, 2))
   1343 	{
   1344 		case 0: swizzleSet = rgbaSet; break;
   1345 		case 1: swizzleSet = xyzwSet; break;
   1346 		case 2: swizzleSet = stpqSet; break;
   1347 		default: DE_ASSERT(DE_FALSE);
   1348 	}
   1349 
   1350 	std::string swizzleStr;
   1351 	for (int elemNdx = 0; elemNdx < m_outValueRange.getType().getNumElements(); elemNdx++)
   1352 		swizzleStr += swizzleSet[m_swizzle[elemNdx]];
   1353 
   1354 	m_child->tokenize(state, str);
   1355 	str << Token::DOT << Token(swizzleStr.c_str());
   1356 }
   1357 
   1358 float SwizzleOp::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
   1359 {
   1360 	if (!state.getProgramParameters().useSwizzle)
   1361 		return 0.0f;
   1362 
   1363 	if (state.getPrecedence() < swizzlePrecedence)
   1364 		return 0.0f;
   1365 
   1366 	if (!valueRange.getType().isFloatOrVec()	&&
   1367 		!valueRange.getType().isIntOrVec()		&&
   1368 		!valueRange.getType().isBoolOrVec())
   1369 		return 0.0f;
   1370 
   1371 	int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
   1372 
   1373 	// Swizzle + Constructor + Values
   1374 	if (availableLevels < 3)
   1375 		return 0.0f;
   1376 
   1377 	return 1.0f;
   1378 }
   1379 
   1380 void SwizzleOp::evaluate (ExecutionContext& execCtx)
   1381 {
   1382 	m_child->evaluate(execCtx);
   1383 
   1384 	ExecConstValueAccess	inValue		= m_child->getValue();
   1385 	ExecValueAccess			outValue	= m_value.getValue(m_outValueRange.getType());
   1386 
   1387 	for (int outElemNdx = 0; outElemNdx < outValue.getType().getNumElements(); outElemNdx++)
   1388 	{
   1389 		int inElemNdx = m_swizzle[outElemNdx];
   1390 		outValue.component(outElemNdx) = inValue.component(inElemNdx).value();
   1391 	}
   1392 }
   1393 
   1394 static int countSamplers (const VariableManager& varManager, VariableType::Type samplerType)
   1395 {
   1396 	int numSamplers = 0;
   1397 
   1398 	IsSamplerEntry::Iterator	i		= varManager.getBegin(IsSamplerEntry(samplerType));
   1399 	IsSamplerEntry::Iterator	end		= varManager.getEnd(IsSamplerEntry(samplerType));
   1400 
   1401 	for (; i != end; i++)
   1402 		numSamplers += 1;
   1403 
   1404 	return numSamplers;
   1405 }
   1406 
   1407 TexLookup::TexLookup (GeneratorState& state, ConstValueRangeAccess valueRange)
   1408 	: m_type			(TYPE_LAST)
   1409 	, m_coordExpr		(DE_NULL)
   1410 	, m_lodBiasExpr		(DE_NULL)
   1411 	, m_valueType		(VariableType::TYPE_FLOAT, 4)
   1412 	, m_value			(m_valueType)
   1413 {
   1414 	DE_ASSERT(valueRange.getType() == VariableType(VariableType::TYPE_FLOAT, 4));
   1415 	DE_UNREF(valueRange); // Texture output value range is constant.
   1416 
   1417 	// Select type.
   1418 	vector<Type> typeCandidates;
   1419 	if (state.getShaderParameters().useTexture2D)
   1420 	{
   1421 		typeCandidates.push_back(TYPE_TEXTURE2D);
   1422 		typeCandidates.push_back(TYPE_TEXTURE2D_LOD);
   1423 		typeCandidates.push_back(TYPE_TEXTURE2D_PROJ);
   1424 		typeCandidates.push_back(TYPE_TEXTURE2D_PROJ_LOD);
   1425 	}
   1426 
   1427 	if (state.getShaderParameters().useTextureCube)
   1428 	{
   1429 		typeCandidates.push_back(TYPE_TEXTURECUBE);
   1430 		typeCandidates.push_back(TYPE_TEXTURECUBE_LOD);
   1431 	}
   1432 
   1433 	m_type = state.getRandom().choose<Type>(typeCandidates.begin(), typeCandidates.end());
   1434 
   1435 	// Select or allocate sampler.
   1436 	VariableType::Type samplerType = VariableType::TYPE_LAST;
   1437 	switch (m_type)
   1438 	{
   1439 		case TYPE_TEXTURE2D:
   1440 		case TYPE_TEXTURE2D_LOD:
   1441 		case TYPE_TEXTURE2D_PROJ:
   1442 		case TYPE_TEXTURE2D_PROJ_LOD:
   1443 			samplerType = VariableType::TYPE_SAMPLER_2D;
   1444 			break;
   1445 
   1446 		case TYPE_TEXTURECUBE:
   1447 		case TYPE_TEXTURECUBE_LOD:
   1448 			samplerType = VariableType::TYPE_SAMPLER_CUBE;
   1449 			break;
   1450 
   1451 		default:
   1452 			DE_ASSERT(DE_FALSE);
   1453 	}
   1454 
   1455 	int		sampler2DCount		= countSamplers(state.getVariableManager(), VariableType::TYPE_SAMPLER_2D);
   1456 	int		samplerCubeCount	= countSamplers(state.getVariableManager(), VariableType::TYPE_SAMPLER_CUBE);
   1457 	bool	canAllocSampler		= sampler2DCount + samplerCubeCount < state.getShaderParameters().maxSamplers;
   1458 	bool	hasSampler			= samplerType == VariableType::TYPE_SAMPLER_2D ? (sampler2DCount > 0) : (samplerCubeCount > 0);
   1459 	bool	allocSampler		= !hasSampler || (canAllocSampler && state.getRandom().getBool());
   1460 
   1461 	if (allocSampler)
   1462 	{
   1463 		Variable* sampler = state.getVariableManager().allocate(VariableType(samplerType, 1));
   1464 		state.getVariableManager().setStorage(sampler, Variable::STORAGE_UNIFORM); // Samplers are always uniforms.
   1465 		m_sampler = sampler;
   1466 	}
   1467 	else
   1468 		m_sampler = state.getRandom().choose<const ValueEntry*>(state.getVariableManager().getBegin(IsSamplerEntry(samplerType)),
   1469 															    state.getVariableManager().getEnd(IsSamplerEntry(samplerType)))->getVariable();
   1470 }
   1471 
   1472 TexLookup::~TexLookup (void)
   1473 {
   1474 	delete m_coordExpr;
   1475 	delete m_lodBiasExpr;
   1476 }
   1477 
   1478 Expression* TexLookup::createNextChild (GeneratorState& state)
   1479 {
   1480 	bool hasLodBias		= m_type == TYPE_TEXTURE2D_LOD ||
   1481 						  m_type == TYPE_TEXTURE2D_PROJ_LOD ||
   1482 						  m_type == TYPE_TEXTURECUBE_LOD;
   1483 
   1484 	if (hasLodBias && !m_lodBiasExpr)
   1485 	{
   1486 		ValueRange lodRange(VariableType(VariableType::TYPE_FLOAT, 1));
   1487 		setInfiniteRange(lodRange); // Any value is valid.
   1488 
   1489 		m_lodBiasExpr = Expression::createRandom(state, lodRange.asAccess());
   1490 		return m_lodBiasExpr;
   1491 	}
   1492 
   1493 	if (!m_coordExpr)
   1494 	{
   1495 		if (m_type == TYPE_TEXTURECUBE || m_type == TYPE_TEXTURECUBE_LOD)
   1496 		{
   1497 			// Make sure major axis selection can be done.
   1498 			int majorAxisNdx = state.getRandom().getInt(0, 2);
   1499 
   1500 			ValueRange coordRange(VariableType(VariableType::TYPE_FLOAT, 3));
   1501 
   1502 			for (int ndx = 0; ndx < 3; ndx++)
   1503 			{
   1504 				if (ndx == majorAxisNdx)
   1505 				{
   1506 					bool neg = state.getRandom().getBool();
   1507 					coordRange.getMin().component(ndx) = neg ? -4.0f	: 2.25f;
   1508 					coordRange.getMax().component(ndx) = neg ? -2.25f	: 4.0f;
   1509 				}
   1510 				else
   1511 				{
   1512 					coordRange.getMin().component(ndx) = -2.0f;
   1513 					coordRange.getMax().component(ndx) =  2.0f;
   1514 				}
   1515 			}
   1516 
   1517 			m_coordExpr = Expression::createRandom(state, coordRange.asAccess());
   1518 		}
   1519 		else
   1520 		{
   1521 			bool	isProj				= m_type == TYPE_TEXTURE2D_PROJ || m_type == TYPE_TEXTURE2D_PROJ_LOD;
   1522 			int		coordScalarSize		= isProj ? 3 : 2;
   1523 
   1524 			ValueRange coordRange(VariableType(VariableType::TYPE_FLOAT, coordScalarSize));
   1525 			setInfiniteRange(coordRange); // Initialize base range with -inf..inf
   1526 
   1527 			if (isProj)
   1528 			{
   1529 				// w coordinate must be something sane, and not 0.
   1530 				bool neg = state.getRandom().getBool();
   1531 				coordRange.getMin().component(2) = neg ? -4.0f  : 0.25f;
   1532 				coordRange.getMax().component(2) = neg ? -0.25f : 4.0f;
   1533 			}
   1534 
   1535 			m_coordExpr = Expression::createRandom(state, coordRange.asAccess());
   1536 		}
   1537 
   1538 		DE_ASSERT(m_coordExpr);
   1539 		return m_coordExpr;
   1540 	}
   1541 
   1542 	return DE_NULL; // Done.
   1543 }
   1544 
   1545 void TexLookup::tokenize (GeneratorState& state, TokenStream& str) const
   1546 {
   1547 	bool isVertex = state.getShader().getType() == Shader::TYPE_VERTEX;
   1548 
   1549 	if (state.getProgramParameters().version == VERSION_300)
   1550 	{
   1551 		switch (m_type)
   1552 		{
   1553 			case TYPE_TEXTURE2D:			str << "texture";										break;
   1554 			case TYPE_TEXTURE2D_LOD:		str << (isVertex ? "textureLod" : "texture");			break;
   1555 			case TYPE_TEXTURE2D_PROJ:		str << "textureProj";									break;
   1556 			case TYPE_TEXTURE2D_PROJ_LOD:	str << (isVertex ? "textureProjLod" : "textureProj");	break;
   1557 			case TYPE_TEXTURECUBE:			str << "texture";										break;
   1558 			case TYPE_TEXTURECUBE_LOD:		str << (isVertex ? "textureLod" : "texture");			break;
   1559 			default:
   1560 				DE_ASSERT(DE_FALSE);
   1561 		}
   1562 	}
   1563 	else
   1564 	{
   1565 		switch (m_type)
   1566 		{
   1567 			case TYPE_TEXTURE2D:			str << "texture2D";											break;
   1568 			case TYPE_TEXTURE2D_LOD:		str << (isVertex ? "texture2DLod" : "texture2D");			break;
   1569 			case TYPE_TEXTURE2D_PROJ:		str << "texture2DProj";										break;
   1570 			case TYPE_TEXTURE2D_PROJ_LOD:	str << (isVertex ? "texture2DProjLod" : "texture2DProj");	break;
   1571 			case TYPE_TEXTURECUBE:			str << "textureCube";										break;
   1572 			case TYPE_TEXTURECUBE_LOD:		str << (isVertex ? "textureCubeLod" : "textureCube");		break;
   1573 			default:
   1574 				DE_ASSERT(DE_FALSE);
   1575 		}
   1576 	}
   1577 
   1578 	str << Token::LEFT_PAREN;
   1579 	str << m_sampler->getName();
   1580 	str << Token::COMMA;
   1581 	m_coordExpr->tokenize(state, str);
   1582 
   1583 	if (m_lodBiasExpr)
   1584 	{
   1585 		str << Token::COMMA;
   1586 		m_lodBiasExpr->tokenize(state, str);
   1587 	}
   1588 
   1589 	str << Token::RIGHT_PAREN;
   1590 }
   1591 
   1592 float TexLookup::getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange)
   1593 {
   1594 	if (state.getShaderParameters().texLookupBaseWeight <= 0.0f)
   1595 		return 0.0f;
   1596 
   1597 	int availableLevels = state.getShaderParameters().maxExpressionDepth - state.getExpressionDepth();
   1598 
   1599 	// Lookup + Constructor + Values
   1600 	if (availableLevels < 3)
   1601 		return 0.0f;
   1602 
   1603 	if (state.getExpressionFlags() & (CONST_EXPR|NO_VAR_ALLOCATION))
   1604 		return 0.0f;
   1605 
   1606 	if (valueRange.getType() != VariableType(VariableType::TYPE_FLOAT, 4))
   1607 		return 0.0f;
   1608 
   1609 	ValueRange texOutputRange(VariableType(VariableType::TYPE_FLOAT, 4));
   1610 	for (int ndx = 0; ndx < 4; ndx++)
   1611 	{
   1612 		texOutputRange.getMin().component(ndx) = 0.0f;
   1613 		texOutputRange.getMax().component(ndx) = 1.0f;
   1614 	}
   1615 
   1616 	if (!valueRange.isSupersetOf(texOutputRange.asAccess()))
   1617 		return 0.0f;
   1618 
   1619 	return state.getShaderParameters().texLookupBaseWeight;
   1620 }
   1621 
   1622 void TexLookup::evaluate (ExecutionContext& execCtx)
   1623 {
   1624 	// Evaluate coord and bias.
   1625 	m_coordExpr->evaluate(execCtx);
   1626 	if (m_lodBiasExpr)
   1627 		m_lodBiasExpr->evaluate(execCtx);
   1628 
   1629 	ExecConstValueAccess	coords	= m_coordExpr->getValue();
   1630 	ExecValueAccess			dst		= m_value.getValue(m_valueType);
   1631 
   1632 	switch (m_type)
   1633 	{
   1634 		case TYPE_TEXTURE2D:
   1635 		{
   1636 			const Sampler2D& tex = execCtx.getSampler2D(m_sampler);
   1637 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1638 			{
   1639 				float		s	= coords.component(0).asFloat(i);
   1640 				float		t	= coords.component(1).asFloat(i);
   1641 				tcu::Vec4	p	= tex.sample(s, t, 0.0f);
   1642 
   1643 				for (int comp = 0; comp < 4; comp++)
   1644 					dst.component(comp).asFloat(i) = p[comp];
   1645 			}
   1646 			break;
   1647 		}
   1648 
   1649 		case TYPE_TEXTURE2D_LOD:
   1650 		{
   1651 			ExecConstValueAccess	lod		= m_lodBiasExpr->getValue();
   1652 			const Sampler2D&		tex		= execCtx.getSampler2D(m_sampler);
   1653 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1654 			{
   1655 				float		s	= coords.component(0).asFloat(i);
   1656 				float		t	= coords.component(1).asFloat(i);
   1657 				float		l	= lod.component(0).asFloat(i);
   1658 				tcu::Vec4	p	= tex.sample(s, t, l);
   1659 
   1660 				for (int comp = 0; comp < 4; comp++)
   1661 					dst.component(comp).asFloat(i) = p[comp];
   1662 			}
   1663 			break;
   1664 		}
   1665 
   1666 		case TYPE_TEXTURE2D_PROJ:
   1667 		{
   1668 			const Sampler2D& tex = execCtx.getSampler2D(m_sampler);
   1669 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1670 			{
   1671 				float		s	= coords.component(0).asFloat(i);
   1672 				float		t	= coords.component(1).asFloat(i);
   1673 				float		w	= coords.component(2).asFloat(i);
   1674 				tcu::Vec4	p	= tex.sample(s/w, t/w, 0.0f);
   1675 
   1676 				for (int comp = 0; comp < 4; comp++)
   1677 					dst.component(comp).asFloat(i) = p[comp];
   1678 			}
   1679 			break;
   1680 		}
   1681 
   1682 		case TYPE_TEXTURE2D_PROJ_LOD:
   1683 		{
   1684 			ExecConstValueAccess	lod		= m_lodBiasExpr->getValue();
   1685 			const Sampler2D&		tex		= execCtx.getSampler2D(m_sampler);
   1686 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1687 			{
   1688 				float		s	= coords.component(0).asFloat(i);
   1689 				float		t	= coords.component(1).asFloat(i);
   1690 				float		w	= coords.component(2).asFloat(i);
   1691 				float		l	= lod.component(0).asFloat(i);
   1692 				tcu::Vec4	p	= tex.sample(s/w, t/w, l);
   1693 
   1694 				for (int comp = 0; comp < 4; comp++)
   1695 					dst.component(comp).asFloat(i) = p[comp];
   1696 			}
   1697 			break;
   1698 		}
   1699 
   1700 		case TYPE_TEXTURECUBE:
   1701 		{
   1702 			const SamplerCube& tex = execCtx.getSamplerCube(m_sampler);
   1703 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1704 			{
   1705 				float		s	= coords.component(0).asFloat(i);
   1706 				float		t	= coords.component(1).asFloat(i);
   1707 				float		r	= coords.component(2).asFloat(i);
   1708 				tcu::Vec4	p	= tex.sample(s, t, r, 0.0f);
   1709 
   1710 				for (int comp = 0; comp < 4; comp++)
   1711 					dst.component(comp).asFloat(i) = p[comp];
   1712 			}
   1713 			break;
   1714 		}
   1715 
   1716 		case TYPE_TEXTURECUBE_LOD:
   1717 		{
   1718 			ExecConstValueAccess	lod		= m_lodBiasExpr->getValue();
   1719 			const SamplerCube&		tex		= execCtx.getSamplerCube(m_sampler);
   1720 			for (int i = 0; i < EXEC_VEC_WIDTH; i++)
   1721 			{
   1722 				float		s	= coords.component(0).asFloat(i);
   1723 				float		t	= coords.component(1).asFloat(i);
   1724 				float		r	= coords.component(2).asFloat(i);
   1725 				float		l	= lod.component(0).asFloat(i);
   1726 				tcu::Vec4	p	= tex.sample(s, t, r, l);
   1727 
   1728 				for (int comp = 0; comp < 4; comp++)
   1729 					dst.component(comp).asFloat(i) = p[comp];
   1730 			}
   1731 			break;
   1732 		}
   1733 
   1734 		default:
   1735 			DE_ASSERT(DE_FALSE);
   1736 	}
   1737 }
   1738 
   1739 } // rsg
   1740