Home | History | Annotate | Download | only in randomshaders
      1 #ifndef _RSGVARIABLETYPE_HPP
      2 #define _RSGVARIABLETYPE_HPP
      3 /*-------------------------------------------------------------------------
      4  * drawElements Quality Program Random Shader Generator
      5  * ----------------------------------------------------
      6  *
      7  * Copyright 2014 The Android Open Source Project
      8  *
      9  * Licensed under the Apache License, Version 2.0 (the "License");
     10  * you may not use this file except in compliance with the License.
     11  * You may obtain a copy of the License at
     12  *
     13  *      http://www.apache.org/licenses/LICENSE-2.0
     14  *
     15  * Unless required by applicable law or agreed to in writing, software
     16  * distributed under the License is distributed on an "AS IS" BASIS,
     17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18  * See the License for the specific language governing permissions and
     19  * limitations under the License.
     20  *
     21  *//*!
     22  * \file
     23  * \brief Variable Type class.
     24  *//*--------------------------------------------------------------------*/
     25 
     26 #include "rsgDefs.hpp"
     27 
     28 #include <vector>
     29 #include <string>
     30 
     31 namespace rsg
     32 {
     33 
     34 class TokenStream;
     35 
     36 class VariableType
     37 {
     38 public:
     39 	enum Type
     40 	{
     41 		TYPE_VOID = 0,
     42 		TYPE_FLOAT,
     43 		TYPE_INT,
     44 		TYPE_BOOL,
     45 		TYPE_STRUCT,
     46 		TYPE_ARRAY,
     47 		TYPE_SAMPLER_2D,
     48 		TYPE_SAMPLER_CUBE,
     49 
     50 		TYPE_LAST
     51 	};
     52 
     53 	enum Precision
     54 	{
     55 		PRECISION_NONE = 0,
     56 		PRECISION_LOW,
     57 		PRECISION_MEDIUM,
     58 		PRECISION_HIGH,
     59 
     60 		PRECISION_LAST
     61 	};
     62 
     63 	class Member
     64 	{
     65 	public:
     66 		Member (void)
     67 			: m_type(DE_NULL)
     68 			, m_name()
     69 		{
     70 		}
     71 
     72 		Member (const VariableType& type, const char* name)
     73 			: m_type(new VariableType(type))
     74 			, m_name(name)
     75 		{
     76 		}
     77 
     78 		~Member (void)
     79 		{
     80 			delete m_type;
     81 		}
     82 
     83 		Member (const Member& other)
     84 			: m_type(DE_NULL)
     85 			, m_name(other.m_name)
     86 		{
     87 			if (other.m_type)
     88 				m_type = new VariableType(*other.m_type);
     89 		}
     90 
     91 		Member& operator= (const Member& other)
     92 		{
     93 			if (this == &other)
     94 				return *this;
     95 
     96 			delete m_type;
     97 
     98 			m_type = DE_NULL;
     99 			m_name = other.m_name;
    100 
    101 			if (other.m_type)
    102 				m_type = new VariableType(*other.m_type);
    103 
    104 			return *this;
    105 		}
    106 
    107 		bool operator!= (const Member& other) const
    108 		{
    109 			if (m_name != other.m_name)
    110 				return true;
    111 			if (!!m_type != !!other.m_type)
    112 				return true;
    113 			if (m_type && *m_type != *other.m_type)
    114 				return true;
    115 			return false;
    116 		}
    117 
    118 		bool operator== (const Member& other) const
    119 		{
    120 			return !(*this != other);
    121 		}
    122 
    123 		const VariableType&		getType		(void) const	{ return *m_type;			}
    124 		const char*				getName		(void) const	{ return m_name.c_str();	}
    125 
    126 	private:
    127 		VariableType*			m_type;
    128 		std::string				m_name;
    129 	};
    130 
    131 										VariableType		(void);
    132 										VariableType		(Type baseType, int numElements = 0);
    133 										VariableType		(Type baseType, const VariableType& elementType, int numElements);
    134 										VariableType		(Type baseType, const char* typeName);
    135 										~VariableType		(void);
    136 
    137 	Type								getBaseType			(void) const	{ return m_baseType;			}
    138 	Precision							getPrecision		(void) const	{ return m_precision;			}
    139 	const char*							getTypeName			(void) const	{ return m_typeName.c_str();	}
    140 	int									getNumElements		(void) const	{ return m_numElements;			}
    141 	const VariableType&					getElementType		(void) const;
    142 
    143 	const std::vector<Member>&			getMembers			(void) const	{ return m_members;				}
    144 	std::vector<Member>&				getMembers			(void)			{ return m_members;				}
    145 
    146 	int									getScalarSize			(void) const;
    147 	int									getElementScalarOffset	(int elementNdx) const;
    148 	int									getMemberScalarOffset	(int memberNdx) const;
    149 
    150 	bool								operator!=			(const VariableType& other) const;
    151 	bool								operator==			(const VariableType& other) const;
    152 
    153 	VariableType&						operator=			(const VariableType& other);
    154 										VariableType		(const VariableType& other);
    155 
    156 	void								tokenizeShortType	(TokenStream& str) const;
    157 
    158 	bool								isStruct			(void) const	{ return m_baseType == TYPE_STRUCT; }
    159 	bool								isArray				(void) const	{ return m_baseType == TYPE_ARRAY;	}
    160 	bool								isFloatOrVec		(void) const	{ return m_baseType == TYPE_FLOAT;	}
    161 	bool								isIntOrVec			(void) const	{ return m_baseType == TYPE_INT;	}
    162 	bool								isBoolOrVec			(void) const	{ return m_baseType == TYPE_BOOL;	}
    163 	bool								isSampler			(void) const	{ return m_baseType == TYPE_SAMPLER_2D || m_baseType == TYPE_SAMPLER_CUBE; }
    164 	bool								isVoid				(void) const	{ return m_baseType == TYPE_VOID;	}
    165 
    166 	static const VariableType&			getScalarType		(Type baseType);
    167 
    168 private:
    169 	Type								m_baseType;
    170 	Precision							m_precision;
    171 	std::string							m_typeName;
    172 	int									m_numElements;
    173 	VariableType*						m_elementType;
    174 	std::vector<Member>					m_members;
    175 };
    176 
    177 inline VariableType::VariableType (void)
    178 	: m_baseType	(TYPE_VOID)
    179 	, m_precision	(PRECISION_NONE)
    180 	, m_typeName	()
    181 	, m_numElements	(0)
    182 	, m_elementType	(DE_NULL)
    183 {
    184 }
    185 
    186 inline VariableType::VariableType (Type baseType, int numElements)
    187 	: m_baseType	(baseType)
    188 	, m_precision	(PRECISION_NONE)
    189 	, m_typeName	()
    190 	, m_numElements	(numElements)
    191 	, m_elementType	(DE_NULL)
    192 {
    193 	DE_ASSERT(baseType != TYPE_ARRAY && baseType != TYPE_STRUCT);
    194 }
    195 
    196 inline VariableType::VariableType (Type baseType, const VariableType& elementType, int numElements)
    197 	: m_baseType	(baseType)
    198 	, m_precision	(PRECISION_NONE)
    199 	, m_typeName	()
    200 	, m_numElements	(numElements)
    201 	, m_elementType	(new VariableType(elementType))
    202 {
    203 	DE_ASSERT(baseType == TYPE_ARRAY);
    204 }
    205 
    206 inline VariableType::VariableType (Type baseType, const char* typeName)
    207 	: m_baseType	(baseType)
    208 	, m_precision	(PRECISION_NONE)
    209 	, m_typeName	(typeName)
    210 	, m_numElements	(0)
    211 	, m_elementType	(DE_NULL)
    212 {
    213 	DE_ASSERT(baseType == TYPE_STRUCT);
    214 }
    215 
    216 inline VariableType::~VariableType (void)
    217 {
    218 	delete m_elementType;
    219 }
    220 
    221 } // rsg
    222 
    223 #endif // _RSGVARIABLETYPE_HPP
    224