13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _RSGVARIABLEVALUE_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _RSGVARIABLEVALUE_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Random Shader Generator
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Variable Value class.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgVariableType.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgVariable.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuVector.hpp"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <algorithm>
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace rsg
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunion Scalar
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int		intVal;
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float	floatVal;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool	boolVal;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar (void)		: intVal	(0) {}
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar (float v)	: floatVal	(v) {}
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar (int v)		: intVal	(v) {}
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar (bool v)		: boolVal	(v) {}
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Bit-exact compare
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool operator== (Scalar other) const { return intVal == other.intVal; }
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool operator!= (Scalar other) const { return intVal != other.intVal; }
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T> static Scalar min	(void);
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T> static Scalar max	(void);
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T> T		as (void) const;
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T> T&	as (void);
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
573c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_STATIC_ASSERT(sizeof(Scalar) == sizeof(deUint32));
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::min<float>	(void)	{ return Scalar((int)0xff800000);	}
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::max<float>	(void)	{ return Scalar((int)0x7f800000);	}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::min<int>		(void)	{ return Scalar((int)0x80000000);	}
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::max<int>		(void)	{ return Scalar((int)0x7fffffff);	}
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::min<bool>		(void)	{ return Scalar(false);				}
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline Scalar Scalar::max<bool>		(void)	{ return Scalar(true);				}
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline float	Scalar::as<float>	(void) const	{ return floatVal;	}
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline float&	Scalar::as<float>	(void)			{ return floatVal;	}
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline int		Scalar::as<int>		(void) const	{ return intVal;	}
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline int&		Scalar::as<int>		(void)			{ return intVal;	}
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline bool		Scalar::as<bool>	(void) const	{ return boolVal;	}
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <> inline bool&	Scalar::as<bool>	(void)			{ return boolVal;	}
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass StridedValueRead
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								StridedValueRead		(const VariableType& type, const Scalar* value) : m_type(type), m_value(value) {}
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&			getType					(void) const			{ return m_type;	}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Scalar*				getValuePtr				(void) const			{ return m_value;	}
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&			m_type;
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Scalar*				m_value;
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ConstStridedValueAccess
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ConstStridedValueAccess	(void) : m_type(DE_NULL), m_value(DE_NULL) {}
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ConstStridedValueAccess	(const VariableType& type, const Scalar* valuePtr) : m_type(&type), m_value(const_cast<Scalar*>(valuePtr)) {}
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&			getType					(void) const			{ return *m_type;			}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-only access
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstStridedValueAccess		component				(int compNdx) const		{ return ConstStridedValueAccess(getType().getElementType(), m_value + Stride*compNdx);													}
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstStridedValueAccess		arrayElement			(int elementNdx) const	{ return ConstStridedValueAccess(getType().getElementType(), m_value + Stride*getType().getElementScalarOffset(elementNdx));				}
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstStridedValueAccess		member					(int memberNdx) const	{ return ConstStridedValueAccess(getType().getMembers()[memberNdx].getType(), m_value + Stride*getType().getMemberScalarOffset(memberNdx));	}
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float						asFloat					(void) const			{ DE_STATIC_ASSERT(Stride == 1); return m_value->floatVal;	}
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							asInt					(void) const			{ DE_STATIC_ASSERT(Stride == 1); return m_value->intVal;	}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool						asBool					(void) const			{ DE_STATIC_ASSERT(Stride == 1); return m_value->boolVal;	}
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar						asScalar				(void) const			{ DE_STATIC_ASSERT(Stride == 1); return *m_value;			}
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float						asFloat					(int ndx) const			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].floatVal;	}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int							asInt					(int ndx) const			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].intVal;	}
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool						asBool					(int ndx) const			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx].boolVal;	}
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar						asScalar				(int ndx) const			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return m_value[ndx];			}
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T>
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T							as						(int ndx) const			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].template as<T>();	}
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// For assignment: b = a.value()
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueRead<Stride>	value					(void) const			{ return StridedValueRead<Stride>(getType(), m_value);		}
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprotected:
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType*			m_type;
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar*						m_value;	// \note Non-const internal pointer is used so that ValueAccess can extend this class with RW access
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass StridedValueAccess : public ConstStridedValueAccess<Stride>
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								StridedValueAccess	(void) {}
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								StridedValueAccess	(const VariableType& type, Scalar* valuePtr) : ConstStridedValueAccess<Stride>(type, valuePtr) {}
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-write access
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess			component			(int compNdx)		{ return StridedValueAccess(this->getType().getElementType(), this->m_value + Stride*compNdx);													}
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess			arrayElement		(int elementNdx)	{ return StridedValueAccess(this->getType().getElementType(), this->m_value + Stride*this->getType().getElementScalarOffset(elementNdx));					}
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess			member				(int memberNdx)		{ return StridedValueAccess(this->getType().getMembers()[memberNdx].getType(), this->m_value + Stride*this->getType().getMemberScalarOffset(memberNdx));	}
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float&						asFloat				(void)				{ DE_STATIC_ASSERT(Stride == 1); return this->m_value->floatVal;	}
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int&						asInt				(void)				{ DE_STATIC_ASSERT(Stride == 1); return this->m_value->intVal;		}
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool&						asBool				(void)				{ DE_STATIC_ASSERT(Stride == 1); return this->m_value->boolVal;		}
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar&						asScalar			(void)				{ DE_STATIC_ASSERT(Stride == 1); return *this->m_value;				}
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	float&						asFloat				(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].floatVal;	}
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int&						asInt				(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].intVal;		}
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool&						asBool				(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].boolVal;		}
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar&						asScalar			(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx];				}
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <typename T>
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&							as					(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Stride)); return this->m_value[ndx].template as<T>();		}
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <int SrcStride>
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(const StridedValueRead<SrcStride>& value);
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Helpers, work only in Stride == 1 case
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <int Size>
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(const tcu::Vector<float, Size>& vec);
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(float floatVal)	{ asFloat()		= floatVal;	return *this;	}
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(int intVal)		{ asInt()		= intVal;	return *this;	}
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(bool boolVal)		{ asBool()		= boolVal;	return *this;	}
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess&			operator=			(Scalar val)		{ asScalar()	= val;		return *this;	}
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int SrcStride>
1613c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStridedValueAccess<Stride>& StridedValueAccess<Stride>::operator= (const StridedValueRead<SrcStride>& valueRead)
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(SrcStride == Stride || SrcStride == 1);
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(this->getType() == valueRead.getType());
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int scalarSize = this->getType().getScalarSize();
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (scalarSize == 0)
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return *this; // Happens when void value range is copied
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (Stride == SrcStride)
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		std::copy(valueRead.getValuePtr(), valueRead.getValuePtr() + scalarSize*Stride, this->m_value);
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			std::fill(this->m_value + scalarNdx*Stride, this->m_value + (scalarNdx+1)*Stride, valueRead.getValuePtr()[scalarNdx]);
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Size>
1843c827367444ee418f129b2c238299f49d3264554Jarkko PoyryStridedValueAccess<Stride>& StridedValueAccess<Stride>::operator= (const tcu::Vector<float, Size>& vec)
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(this->getType() == VariableType(VariableType::TYPE_FLOAT, Size));
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int comp = 0; comp < 4; comp++)
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		component(comp).asFloat() = vec.getPtr()[comp];
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Typedefs for stride == 1 case
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef ConstStridedValueAccess<1>	ConstValueAccess;
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef StridedValueAccess<1>		ValueAccess;
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ConstValueRangeAccess
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ConstValueRangeAccess	(void) : m_type(DE_NULL), m_min(DE_NULL), m_max(DE_NULL) {}
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ConstValueRangeAccess	(const VariableType& type, const Scalar* minVal, const Scalar* maxVal) : m_type(&type), m_min(const_cast<Scalar*>(minVal)), m_max(const_cast<Scalar*>(maxVal)) {}
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&			getType					(void) const	{ return *m_type;							}
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueAccess			getMin					(void) const	{ return ConstValueAccess(*m_type, m_min);	}
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueAccess			getMax					(void) const	{ return ConstValueAccess(*m_type, m_max);	}
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-only access
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueRangeAccess		component				(int compNdx) const;
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueRangeAccess		arrayElement			(int elementNdx) const;
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueRangeAccess		member					(int memberNdx) const;
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Set operations - tests condition for all elements
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool						intersects				(const ConstValueRangeAccess& other) const;
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool						isSupersetOf			(const ConstValueRangeAccess& other) const;
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool						isSubsetOf				(const ConstValueRangeAccess& other) const;
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprotected:
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType*			m_type;
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar*						m_min;	// \note See note in ConstValueAccess
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar*						m_max;
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ConstValueRangeAccess ConstValueRangeAccess::component (int compNdx) const
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ConstValueRangeAccess(m_type->getElementType(), m_min + compNdx, m_max + compNdx);
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ConstValueRangeAccess ConstValueRangeAccess::arrayElement (int elementNdx) const
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int offset = m_type->getElementScalarOffset(elementNdx);
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ConstValueRangeAccess(m_type->getElementType(), m_min + offset, m_max + offset);
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ConstValueRangeAccess ConstValueRangeAccess::member (int memberNdx) const
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int offset = m_type->getMemberScalarOffset(memberNdx);
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ConstValueRangeAccess(m_type->getMembers()[memberNdx].getType(), m_min + offset, m_max + offset);
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ValueRangeAccess : public ConstValueRangeAccess
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ValueRangeAccess		(const VariableType& type, Scalar* minVal, Scalar* maxVal) : ConstValueRangeAccess(type, minVal, maxVal) {}
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-write access
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess					getMin					(void) { return ValueAccess(*m_type, m_min);	}
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess					getMax					(void) { return ValueAccess(*m_type, m_max);	}
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueRangeAccess			component				(int compNdx);
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueRangeAccess			arrayElement			(int elementNdx);
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueRangeAccess			member					(int memberNdx);
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ValueRangeAccess ValueRangeAccess::component (int compNdx)
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ValueRangeAccess(m_type->getElementType(), m_min + compNdx, m_max + compNdx);
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ValueRangeAccess ValueRangeAccess::arrayElement (int elementNdx)
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int offset = m_type->getElementScalarOffset(elementNdx);
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ValueRangeAccess(m_type->getElementType(), m_min + offset, m_max + offset);
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline ValueRangeAccess ValueRangeAccess::member (int memberNdx)
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int offset = m_type->getMemberScalarOffset(memberNdx);
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ValueRangeAccess(m_type->getMembers()[memberNdx].getType(), m_min + offset, m_max + offset);
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ValueRange
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ValueRange			(const VariableType& type);
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ValueRange			(const VariableType& type, const ConstValueAccess& minVal, const ConstValueAccess& maxVal);
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ValueRange			(const VariableType& type, const Scalar* minVal, const Scalar* maxVal);
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								ValueRange			(ConstValueRangeAccess other);
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry								~ValueRange			(void);
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&			getType				(void) const	{ return m_type;								}
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess					getMin				(void)			{ return ValueAccess(m_type, getMinPtr());		}
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess					getMax				(void)			{ return ValueAccess(m_type, getMaxPtr());		}
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueAccess			getMin				(void) const	{ return ConstValueAccess(m_type, getMinPtr());	}
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueAccess			getMax				(void) const	{ return ConstValueAccess(m_type, getMaxPtr());	}
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueRangeAccess			asAccess			(void)			{ return ValueRangeAccess(m_type, getMinPtr(), getMaxPtr());		}
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueRangeAccess		asAccess			(void) const	{ return ConstValueRangeAccess(m_type, getMinPtr(), getMaxPtr());	}
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	operator ConstValueRangeAccess					(void) const	{ return asAccess();							}
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	operator ValueRangeAccess						(void)			{ return asAccess();							}
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static void					computeIntersection	(ValueRangeAccess dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b);
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	static void					computeIntersection	(ValueRange& dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b);
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Scalar*				getMinPtr			(void) const	{ return m_min.empty() ? DE_NULL : &m_min[0];	}
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Scalar*				getMaxPtr			(void) const	{ return m_max.empty() ? DE_NULL : &m_max[0];	}
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar*						getMinPtr			(void)			{ return m_min.empty() ? DE_NULL : &m_min[0];	}
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Scalar*						getMaxPtr			(void)			{ return m_max.empty() ? DE_NULL : &m_max[0];	}
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VariableType				m_type;
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::vector<Scalar>			m_min;
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::vector<Scalar>			m_max;
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ValueStorage
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										ValueStorage		(void);
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										ValueStorage		(const VariableType& type);
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void								setStorage			(const VariableType& type);
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	StridedValueAccess<Stride>			getValue			(const VariableType& type)			{ return StridedValueAccess<Stride>(type, &m_value[0]);			}
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstStridedValueAccess<Stride>		getValue			(const VariableType& type) const	{ return ConstStridedValueAccess<Stride>(type, &m_value[0]);	}
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										ValueStorage		(const ValueStorage& other);
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueStorage						operator=			(const ValueStorage& other);
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::vector<Scalar>					m_value;
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
3293c827367444ee418f129b2c238299f49d3264554Jarkko PoyryValueStorage<Stride>::ValueStorage (void)
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
3343c827367444ee418f129b2c238299f49d3264554Jarkko PoyryValueStorage<Stride>::ValueStorage (const VariableType& type)
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	setStorage(type);
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Stride>
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ValueStorage<Stride>::setStorage (const VariableType& type)
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_value.resize(type.getScalarSize() * Stride);
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass VariableValue
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							VariableValue		(const Variable* variable) : m_variable(variable), m_storage(m_variable->getType()) {}
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							~VariableValue		(void) {}
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Variable*			getVariable			(void) const	{ return m_variable;								}
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess				getValue			(void)			{ return m_storage.getValue(m_variable->getType());	}
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ConstValueAccess		getValue			(void) const	{ return m_storage.getValue(m_variable->getType());	}
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							VariableValue		(const VariableValue& other);
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VariableValue&			operator=			(const VariableValue& other);
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType&		getType				(void) const	{ return m_variable->getType();						}
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const Variable*			m_variable;
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueStorage<1>			m_storage;
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // rsg
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _RSGVARIABLEVALUE_HPP
368