uiacceltk/hitchcock/coretoolkit/inc/Variable.h
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef VARIABLE_H
       
    19 #define VARIABLE_H
       
    20 
       
    21 #include <string>
       
    22 #include "Util.h"
       
    23 #include "GLState.h"
       
    24 
       
    25 using namespace std;
       
    26 
       
    27 namespace shadergen
       
    28 {
       
    29 
       
    30 
       
    31 
       
    32 
       
    33 class Variable
       
    34 {
       
    35 public:
       
    36 
       
    37 	/* Variable class */
       
    38 	typedef enum Class
       
    39 	    {
       
    40 		CLASS_UNIFORM = 0,
       
    41 		CLASS_ATTRIBUTE,
       
    42 		CLASS_VARYING,
       
    43 
       
    44 		CLASS_MAX
       
    45 	    } Class;
       
    46 
       
    47 	/* Variable precision */
       
    48 	typedef enum Precision
       
    49 	    {
       
    50 		PRECISION_LOW = 0,
       
    51 		PRECISION_MEDIUM,
       
    52 		PRECISION_HIGH,
       
    53 
       
    54 		PRECISION_MAX
       
    55 	    } Precision;
       
    56 
       
    57 	/* Variable type */
       
    58 	typedef enum Type
       
    59 	    {
       
    60 		TYPE_FLOAT = 0,
       
    61 		TYPE_VEC2,
       
    62 		TYPE_VEC3,
       
    63 		TYPE_VEC4,
       
    64 
       
    65 		TYPE_INT,
       
    66 		TYPE_IVEC2,
       
    67 		TYPE_IVEC3,
       
    68 		TYPE_IVEC4,
       
    69 
       
    70 		TYPE_MAT2,
       
    71 		TYPE_MAT3,
       
    72 		TYPE_MAT4,
       
    73 
       
    74 		TYPE_SAMPLER,
       
    75 
       
    76 		TYPE_MAX
       
    77 	    } Type;
       
    78 
       
    79 	/* variable data's type tag --- an ugly hack for now */
       
    80 	typedef enum Tag
       
    81 	    {
       
    82 		DATAPTR = 0,
       
    83 		DATAPTRPTR,
       
    84 		TEXTUREUNIT
       
    85 	    } Tag;
       
    86 
       
    87 	union variableData 
       
    88 	    {
       
    89 		const void *		dataPtr;
       
    90 		const void * const *	dataPtrPtr;
       
    91 		int			textureUnit;
       
    92 
       
    93 		variableData(const void *dataPointer) 
       
    94 		    {
       
    95 			dataPtr = dataPointer;
       
    96 		    }
       
    97 
       
    98 		variableData(const void * const *dataPointerPointer) 
       
    99 		    {
       
   100 			dataPtrPtr = dataPointerPointer;
       
   101 		    }
       
   102 
       
   103 		variableData(int unit) 
       
   104 		    { 
       
   105 		    textureUnit = unit; 
       
   106 		    }
       
   107 	};
       
   108 
       
   109 
       
   110 
       
   111 	Variable (const Class vclass,
       
   112 		const char* name,
       
   113 		const Type type,
       
   114 		const GLenum format,
       
   115 		int dim,
       
   116 		const Precision precision,
       
   117 		const void* dataPtr)
       
   118 		: m_class(vclass),
       
   119 		m_name(name),
       
   120 		m_type(type),
       
   121 		m_format(format),
       
   122 		m_dimension(dim),
       
   123 		m_precision(precision),
       
   124 		m_tag(DATAPTR),
       
   125 		m_variableData(dataPtr),
       
   126 		m_location(0)
       
   127 	{
       
   128 		SG_ASSERT(type != TYPE_SAMPLER);
       
   129 		SG_ASSERT(format == GL_BYTE			||
       
   130 			      format == GL_UNSIGNED_BYTE	||
       
   131 			      format == GL_SHORT			||
       
   132 			      format == GL_UNSIGNED_SHORT	||
       
   133 			      format == GL_INT		||
       
   134 			      format == GL_FLOAT			||
       
   135 			      format == GL_FIXED);
       
   136 //		SG_ASSERT((vclass != CLASS_VARYING) == (funcPtr != NULL));
       
   137 	}
       
   138 
       
   139 
       
   140 	Variable (const Class vclass,
       
   141 		const char* name,
       
   142 		const Type type,
       
   143 		const GLenum format,
       
   144 		int dim,
       
   145 		const Precision precision,
       
   146 		const void* const* dataPtrPtr)
       
   147 		: m_class(vclass),
       
   148 		m_name(name),
       
   149 		m_type(type),
       
   150 		m_format(format),
       
   151 		m_dimension(dim),
       
   152 		m_precision(precision),
       
   153 		m_tag(DATAPTRPTR),
       
   154 		m_variableData(dataPtrPtr),
       
   155 		m_location(0)
       
   156 	{
       
   157 		SG_ASSERT(type != TYPE_SAMPLER);
       
   158 		SG_ASSERT(format == GL_BYTE			||
       
   159 			   format == GL_UNSIGNED_BYTE	||
       
   160 			   format == GL_SHORT			||
       
   161 			   format == GL_UNSIGNED_SHORT	||
       
   162 			   format == GL_INT		||
       
   163 			   format == GL_FLOAT			||
       
   164 			   format == GL_FIXED);
       
   165 //		SG_ASSERT((vclass != CLASS_VARYING) == (funcPtr != NULL));
       
   166 	}
       
   167 
       
   168 
       
   169 
       
   170 	Variable (const Class vclass,
       
   171 		const char* name,
       
   172 		const Type type,
       
   173 		const GLenum format,
       
   174 		int dim,
       
   175 		const Precision precision,
       
   176 		int textureUnit)
       
   177 		: m_class(vclass),
       
   178 		m_name(name),
       
   179 		m_type(type),
       
   180 		m_format(format),
       
   181 		m_dimension(dim),
       
   182 		m_precision(precision),
       
   183 		m_tag(TEXTUREUNIT),
       
   184 		m_variableData(textureUnit),
       
   185 		m_location(0)
       
   186 	{
       
   187 		SG_ASSERT((type == TYPE_SAMPLER) && (format == GL_INT) ||
       
   188 		          (vclass == CLASS_VARYING && textureUnit == 0));
       
   189 	}
       
   190 
       
   191 
       
   192 
       
   193 	string getDeclaration (void) const
       
   194 	{
       
   195 		string decl = classToStr() + " " + precisionToStr() + " " + typeToStr() + " " + m_name;
       
   196 
       
   197 		if (m_dimension != 1)
       
   198 		{
       
   199 			SG_ASSERT(SG_IN_RANGE<int>(m_dimension, 1, 9));
       
   200 			decl += "[";
       
   201 			decl += (char)('0' + m_dimension);
       
   202 			decl += "]";
       
   203 		}
       
   204 
       
   205 		decl += ";\n";
       
   206 		return decl;
       
   207 	}
       
   208 
       
   209 	const string& getName (void) const
       
   210 	{
       
   211 		return m_name;
       
   212 	}
       
   213 
       
   214 	Class getClass (void) const
       
   215 	{
       
   216 		return m_class;
       
   217 	}
       
   218 
       
   219 	Type getType (void) const
       
   220 	{
       
   221 		return m_type;
       
   222 	}
       
   223 
       
   224 	GLenum getFormat (void) const
       
   225 	{
       
   226 		return m_format;
       
   227 	}
       
   228 
       
   229 	int getDimension (void) const
       
   230 	{
       
   231 		return m_dimension;
       
   232 	}
       
   233 
       
   234 	Precision getPrecision (void) const
       
   235 	{
       
   236 		return m_precision;
       
   237 	}
       
   238 
       
   239 	const void* getPtr(void) const
       
   240 	{
       
   241 		switch(m_tag) {
       
   242 			case DATAPTR : 
       
   243 				return m_variableData.dataPtr;
       
   244 
       
   245 			case DATAPTRPTR :
       
   246 				return *m_variableData.dataPtrPtr;
       
   247 
       
   248 			case TEXTUREUNIT : 
       
   249 				return (void *)(&m_variableData.textureUnit);
       
   250 
       
   251 			default : 
       
   252 				SG_ASSERT(0);
       
   253 		}
       
   254 		return NULL;
       
   255 	}
       
   256 
       
   257     void setLocation(int location)
       
   258         {
       
   259         m_location = location;
       
   260         }
       
   261 
       
   262     int location(void) const
       
   263         {
       
   264         return m_location;
       
   265         }
       
   266 
       
   267 private:
       
   268 
       
   269 	string typeToStr (void) const
       
   270 	{
       
   271 		const char* s_typeStr[] =
       
   272 		{
       
   273 			"float",	// TYPE_FLOAT = 0,
       
   274 			"vec2",		// TYPE_VEC2,
       
   275 			"vec3",		// TYPE_VEC3,
       
   276 			"vec4",		// TYPE_VEC4,
       
   277 
       
   278 			"int",		// TYPE_INT,
       
   279 			"ivec2",	// TYPE_IVEC2,
       
   280 			"ivec3",	// TYPE_IVEC3,
       
   281 			"ivec4",	// TYPE_IVEC4,
       
   282 
       
   283 			"mat2",		// TYPE_MAT2,
       
   284 			"mat3",		// TYPE_MAT3,
       
   285 			"mat4",		// TYPE_MAT4,
       
   286 
       
   287 			"sampler2D"	// TYPE_SAMPLER
       
   288 		};
       
   289 
       
   290 		SG_CT_ASSERT(SG_LENGTH_OF_ARRAY(s_typeStr) == TYPE_MAX);
       
   291 		return s_typeStr[m_type];
       
   292 	}
       
   293 
       
   294 	string classToStr (void) const
       
   295 	{
       
   296 		const char* s_classStr[] =
       
   297 		{
       
   298 			"uniform",		// CLASS_UNIFORM = 0
       
   299 			"attribute",		// CLASS_ATTRIBUTE,
       
   300 			"varying"		// CLASS_VARYING,
       
   301 		};
       
   302 
       
   303 		SG_CT_ASSERT(SG_LENGTH_OF_ARRAY(s_classStr) == CLASS_MAX);
       
   304 		return s_classStr[m_class];
       
   305 	}
       
   306 
       
   307 	string precisionToStr (void) const
       
   308 	{
       
   309 		const char* s_precStr[] =
       
   310 		{
       
   311 			"lowp",		// PRECISION_LOW = 0
       
   312 			"mediump",	// PRECISION_MEDIUM,
       
   313 			"highp"		// PRECISION_HIGH,
       
   314 		};
       
   315 
       
   316 		SG_CT_ASSERT(SG_LENGTH_OF_ARRAY(s_precStr) == PRECISION_MAX);
       
   317 		return s_precStr[m_precision];
       
   318 	}
       
   319 
       
   320    
       
   321 
       
   322 	Class		m_class;
       
   323 	string		m_name;
       
   324 	Type		m_type;
       
   325 	GLenum		m_format;
       
   326 	int		m_dimension;
       
   327 	Precision	m_precision;
       
   328 	Tag		m_tag;
       
   329 	variableData	m_variableData;
       
   330     int m_location;
       
   331 };
       
   332 
       
   333 }
       
   334 
       
   335 #endif /* VARIABLE_H */