Home | History | Annotate | Download | only in opengl
      1 #ifndef _GLUVARTYPE_HPP
      2 #define _GLUVARTYPE_HPP
      3 /*-------------------------------------------------------------------------
      4  * drawElements Quality Program OpenGL ES Utilities
      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 Shader variable type.
     24  *//*--------------------------------------------------------------------*/
     25 
     26 #include "tcuDefs.hpp"
     27 #include "gluShaderUtil.hpp"
     28 
     29 #include <vector>
     30 #include <string>
     31 #include <ostream>
     32 
     33 namespace glu
     34 {
     35 
     36 class StructType;
     37 
     38 /*--------------------------------------------------------------------*//*!
     39  * \brief Shader variable type.
     40  *
     41  * Variable type represents data type. No storage qualifiers are supported
     42  * since they are associated to a declaration, not to the variable type.
     43  *
     44  * \note Structs are handled using struct pointers since it is often desirable
     45  *		 to maintain unique list of struct declarations.
     46  *//*--------------------------------------------------------------------*/
     47 class VarType
     48 {
     49 public:
     50 						VarType			(void);
     51 						VarType			(const VarType& other);
     52 
     53 						VarType			(DataType basicType, Precision precision);		//!< Basic type constructor.
     54 						VarType			(const VarType& elementType, int arraySize);	//!< Array type constructor.
     55 	explicit			VarType			(const StructType* structPtr);					//!< Struct type constructor.
     56 						~VarType		(void);
     57 
     58 	bool				isBasicType		(void) const	{ return m_type == TYPE_BASIC;	}
     59 	bool				isArrayType		(void) const	{ return m_type == TYPE_ARRAY;	}
     60 	bool				isStructType	(void) const	{ return m_type == TYPE_STRUCT;	}
     61 
     62 	DataType			getBasicType	(void) const	{ DE_ASSERT(isBasicType()); return m_data.basic.type;			}
     63 	Precision			getPrecision	(void) const	{ DE_ASSERT(isBasicType()); return m_data.basic.precision;		}
     64 
     65 	const VarType&		getElementType	(void) const	{ DE_ASSERT(isArrayType()); return *m_data.array.elementType;	}
     66 	int					getArraySize	(void) const	{ DE_ASSERT(isArrayType()); return m_data.array.size;			}
     67 
     68 	const StructType*	getStructPtr	(void) const	{ DE_ASSERT(isStructType()); return m_data.structPtr;			}
     69 
     70 	int					getScalarSize	(void) const;
     71 
     72 	VarType&			operator=		(const VarType& other);
     73 
     74 	bool				operator==		(const VarType& other) const;
     75 	bool				operator!=		(const VarType& other) const;
     76 
     77 	enum
     78 	{
     79 		UNSIZED_ARRAY = -1 //!< Array length for unsized arrays.
     80 	};
     81 
     82 private:
     83 	enum Type
     84 	{
     85 		TYPE_BASIC,
     86 		TYPE_ARRAY,
     87 		TYPE_STRUCT,
     88 
     89 		TYPE_LAST
     90 	};
     91 
     92 	Type				m_type;
     93 	union Data
     94 	{
     95 		// TYPE_BASIC
     96 		struct
     97 		{
     98 			DataType		type;
     99 			Precision		precision;
    100 		} basic;
    101 
    102 		// TYPE_ARRAY
    103 		struct
    104 		{
    105 			VarType*		elementType;
    106 			int				size;
    107 		} array;
    108 
    109 		// TYPE_STRUCT
    110 		const StructType*	structPtr;
    111 
    112 		Data (void)
    113 		{
    114 			array.elementType	= DE_NULL;
    115 			array.size			= 0;
    116 		};
    117 	} m_data;
    118 };
    119 
    120 template <typename T>
    121 inline VarType varTypeOf (Precision prec = PRECISION_LAST) { return VarType(dataTypeOf<T>(), prec); }
    122 
    123 class StructMember
    124 {
    125 public:
    126 						StructMember	(const char* name, const VarType& type) : m_name(name), m_type(type) {}
    127 						StructMember	(void) {}
    128 
    129 	const char*			getName			(void) const { return m_name.c_str();	}
    130 	const VarType&		getType			(void) const { return m_type;			}
    131 
    132 	bool				operator==		(const StructMember& other) const;
    133 	bool				operator!=		(const StructMember& other) const;
    134 
    135 private:
    136 	std::string			m_name;
    137 	VarType				m_type;
    138 };
    139 
    140 class StructType
    141 {
    142 public:
    143 	typedef std::vector<StructMember>::iterator			Iterator;
    144 	typedef std::vector<StructMember>::const_iterator	ConstIterator;
    145 
    146 								StructType		(const char* typeName) : m_typeName(typeName) {}
    147 								~StructType		(void) {}
    148 
    149 	bool						hasTypeName		(void) const	{ return !m_typeName.empty();	}
    150 	const char*					getTypeName		(void) const	{ return hasTypeName() ? m_typeName.c_str() : DE_NULL; }
    151 
    152 	void						addMember		(const char* name, const VarType& type);
    153 
    154 	int							getNumMembers	(void) const	{ return (int)m_members.size();	}
    155 	const StructMember&			getMember		(int ndx) const	{ return m_members[ndx];		}
    156 
    157 	inline Iterator				begin			(void)			{ return m_members.begin();		}
    158 	inline ConstIterator		begin			(void) const	{ return m_members.begin();		}
    159 	inline Iterator				end				(void)			{ return m_members.end();		}
    160 	inline ConstIterator		end				(void) const	{ return m_members.end();		}
    161 
    162 	bool						operator==		(const StructType& other) const;
    163 	bool						operator!=		(const StructType& other) const;
    164 
    165 private:
    166 	std::string					m_typeName;
    167 	std::vector<StructMember>	m_members;
    168 };
    169 
    170 enum Storage
    171 {
    172 	STORAGE_IN,
    173 	STORAGE_OUT,
    174 	STORAGE_CONST,
    175 	STORAGE_UNIFORM,
    176 	STORAGE_BUFFER,
    177 	STORAGE_LAST
    178 };
    179 
    180 const char* getStorageName (Storage storage);
    181 
    182 enum Interpolation
    183 {
    184 	INTERPOLATION_SMOOTH,
    185 	INTERPOLATION_FLAT,
    186 	INTERPOLATION_CENTROID,
    187 	INTERPOLATION_LAST
    188 };
    189 
    190 const char* getInterpolationName (Interpolation interpolation);
    191 
    192 enum FormatLayout
    193 {
    194 	FORMATLAYOUT_RGBA32F = 0,
    195 	FORMATLAYOUT_RGBA16F,
    196 	FORMATLAYOUT_R32F,
    197 	FORMATLAYOUT_RGBA8,
    198 	FORMATLAYOUT_RGBA8_SNORM,
    199 
    200 	FORMATLAYOUT_RGBA32I,
    201 	FORMATLAYOUT_RGBA16I,
    202 	FORMATLAYOUT_RGBA8I,
    203 	FORMATLAYOUT_R32I,
    204 
    205 	FORMATLAYOUT_RGBA32UI,
    206 	FORMATLAYOUT_RGBA16UI,
    207 	FORMATLAYOUT_RGBA8UI,
    208 	FORMATLAYOUT_R32UI,
    209 
    210 	FORMATLAYOUT_LAST
    211 };
    212 
    213 const char* getFormatLayoutName (FormatLayout layout);
    214 
    215 enum MemoryAccessQualifier
    216 {
    217 	MEMORYACCESSQUALIFIER_COHERENT_BIT	= 0x01,
    218 	MEMORYACCESSQUALIFIER_VOLATILE_BIT	= 0x02,
    219 	MEMORYACCESSQUALIFIER_RESTRICT_BIT	= 0x04,
    220 	MEMORYACCESSQUALIFIER_READONLY_BIT	= 0x08,
    221 	MEMORYACCESSQUALIFIER_WRITEONLY_BIT	= 0x10,
    222 
    223 	MEMORYACCESSQUALIFIER_MASK = (MEMORYACCESSQUALIFIER_WRITEONLY_BIT << 1) - 1
    224 };
    225 
    226 const char* getMemoryAccessQualifierName (MemoryAccessQualifier qualifier);
    227 
    228 enum MatrixOrder
    229 {
    230 	MATRIXORDER_COLUMN_MAJOR = 0,
    231 	MATRIXORDER_ROW_MAJOR,
    232 
    233 	MATRIXORDER_LAST
    234 };
    235 
    236 const char* getMatrixOrderName (MatrixOrder qualifier);
    237 
    238 // Declaration utilities.
    239 
    240 struct Layout
    241 {
    242 					Layout			(int location_ = -1, int binding_ = -1, int offset_ = -1, FormatLayout format_ = FORMATLAYOUT_LAST, MatrixOrder matrixOrder_ = MATRIXORDER_LAST);
    243 
    244 	bool			operator==		(const Layout& other) const;
    245 	bool			operator!=		(const Layout& other) const;
    246 
    247 	int				location;
    248 	int				binding;
    249 	int				offset;
    250 	FormatLayout	format;
    251 	MatrixOrder		matrixOrder;
    252 };
    253 
    254 struct VariableDeclaration
    255 {
    256 						VariableDeclaration	(const VarType& varType_, const std::string& name_, Storage storage_ = STORAGE_LAST, Interpolation interpolation_ = INTERPOLATION_LAST, const Layout& layout_ = Layout(), deUint32 memoryAccessQualifierBits_ = 0);
    257 
    258 	bool				operator==			(const VariableDeclaration& other) const;
    259 	bool				operator!=			(const VariableDeclaration& other) const;
    260 
    261 	Layout				layout;
    262 	Interpolation		interpolation;
    263 	Storage				storage;
    264 	VarType				varType;
    265 	deUint32			memoryAccessQualifierBits;
    266 	std::string			name;
    267 };
    268 
    269 struct InterfaceBlock
    270 {
    271 											InterfaceBlock	(void);
    272 
    273 	glu::Layout								layout;
    274 	Storage									storage;
    275 	int										memoryAccessQualifierFlags;
    276 	std::string								interfaceName;
    277 	std::string								instanceName;
    278 	std::vector<glu::VariableDeclaration>	variables;
    279 	std::vector<int>						dimensions;
    280 };
    281 
    282 //! Internals for declare() utilities.
    283 namespace decl
    284 {
    285 
    286 struct Indent
    287 {
    288 	int level;
    289 	Indent (int level_) : level(level_) {}
    290 };
    291 
    292 struct DeclareStructTypePtr
    293 {
    294 	DeclareStructTypePtr (const StructType* structPtr_, int indentLevel_) : structPtr(structPtr_), indentLevel(indentLevel_) {}
    295 
    296 	const StructType*	structPtr;
    297 	int					indentLevel;
    298 };
    299 
    300 struct DeclareStructType
    301 {
    302 	DeclareStructType (const StructType& structType_, int indentLevel_) : structType(structType_), indentLevel(indentLevel_) {}
    303 
    304 	StructType			structType;
    305 	int					indentLevel;
    306 };
    307 
    308 struct DeclareVariable
    309 {
    310 	DeclareVariable (const VarType& varType_, const std::string& name_, int indentLevel_) : varType(varType_), name(name_), indentLevel(indentLevel_) {}
    311 
    312 	VarType				varType;
    313 	std::string			name;
    314 	int					indentLevel;
    315 };
    316 
    317 std::ostream&		operator<<		(std::ostream& str, const Indent&				indent);
    318 std::ostream&		operator<<		(std::ostream& str, const DeclareStructTypePtr&	decl);
    319 std::ostream&		operator<<		(std::ostream& str, const DeclareStructType&	decl);
    320 std::ostream&		operator<<		(std::ostream& str, const DeclareVariable&		decl);
    321 
    322 } // decl
    323 
    324 inline decl::Indent					indent			(int indentLevel)														{ return decl::Indent(indentLevel);									}
    325 inline decl::DeclareStructTypePtr	declare			(const StructType*	structPtr,	int indentLevel = 0)					{ return decl::DeclareStructTypePtr	(structPtr,		indentLevel);	}
    326 inline decl::DeclareStructType		declare			(const StructType&	structType,	int indentLevel = 0)					{ return decl::DeclareStructType	(structType,	indentLevel);	}
    327 inline decl::DeclareVariable		declare			(const VarType& varType, const std::string& name, int indentLevel = 0)	{ return decl::DeclareVariable		(varType, name, indentLevel);	}
    328 
    329 std::ostream&						operator<<		(std::ostream& str, const Layout& decl);
    330 std::ostream&						operator<<		(std::ostream& str, const VariableDeclaration& decl);
    331 
    332 } // glu
    333 
    334 #endif // _GLUVARTYPE_HPP
    335