langElement.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "core/util/str.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "langElement.h"
  26. //**************************************************************************
  27. // Language element
  28. //**************************************************************************
  29. Vector<LangElement*> LangElement::elementList( __FILE__, __LINE__ );
  30. const char* LangElement::constTypeToString(GFXShaderConstType constType)
  31. {
  32. // Determine shader language based on GFXAdapterAPI
  33. if (GFX->getAdapterType() == OpenGL)
  34. {
  35. switch (constType)
  36. {
  37. case GFXSCT_Float: return "float"; break;
  38. case GFXSCT_Float2: return "vec2"; break;
  39. case GFXSCT_Float3: return "vec3"; break;
  40. case GFXSCT_Float4: return "vec4"; break;
  41. case GFXSCT_Float2x2: return "mat2"; break;
  42. case GFXSCT_Float3x3: return "mat3"; break;
  43. case GFXSCT_Float3x4: return "mat3x4"; break;
  44. case GFXSCT_Float4x3: return "mat4x3"; break;
  45. case GFXSCT_Float4x4: return "mat4"; break;
  46. case GFXSCT_Int: return "int"; break;
  47. case GFXSCT_Int2: return "ivec2"; break;
  48. case GFXSCT_Int3: return "ivec3"; break;
  49. case GFXSCT_Int4: return "ivec4"; break;
  50. case GFXSCT_UInt: return "uint"; break;
  51. case GFXSCT_UInt2: return "uvec2"; break;
  52. case GFXSCT_UInt3: return "uvec3"; break;
  53. case GFXSCT_UInt4: return "uvec4"; break;
  54. case GFXSCT_Bool: return "bool"; break;
  55. case GFXSCT_Bool2: return "bvec2"; break;
  56. case GFXSCT_Bool3: return "bvec3"; break;
  57. case GFXSCT_Bool4: return "bvec4"; break;
  58. default: return "unknown"; break;
  59. }
  60. }
  61. else // Assume DirectX/HLSL
  62. {
  63. switch (constType)
  64. {
  65. case GFXSCT_Float: return "float"; break;
  66. case GFXSCT_Float2: return "float2"; break;
  67. case GFXSCT_Float3: return "float3"; break;
  68. case GFXSCT_Float4: return "float4"; break;
  69. case GFXSCT_Float2x2: return "float2x2"; break;
  70. case GFXSCT_Float3x3: return "float3x3"; break;
  71. case GFXSCT_Float3x4: return "float3x4"; break;
  72. case GFXSCT_Float4x3: return "float4x3"; break;
  73. case GFXSCT_Float4x4: return "float4x4"; break;
  74. case GFXSCT_Int: return "int"; break;
  75. case GFXSCT_Int2: return "int2"; break;
  76. case GFXSCT_Int3: return "int3"; break;
  77. case GFXSCT_Int4: return "int4"; break;
  78. case GFXSCT_UInt: return "uint"; break;
  79. case GFXSCT_UInt2: return "uint2"; break;
  80. case GFXSCT_UInt3: return "uint3"; break;
  81. case GFXSCT_UInt4: return "uint4"; break;
  82. case GFXSCT_Bool: return "bool"; break;
  83. case GFXSCT_Bool2: return "bool2"; break;
  84. case GFXSCT_Bool3: return "bool3"; break;
  85. case GFXSCT_Bool4: return "bool4"; break;
  86. default: return "unknown"; break;
  87. }
  88. }
  89. return "";
  90. }
  91. //--------------------------------------------------------------------------
  92. // Constructor
  93. //--------------------------------------------------------------------------
  94. LangElement::LangElement()
  95. {
  96. elementList.push_back( this );
  97. static U32 tempNum = 0;
  98. dSprintf( (char*)name, sizeof(name), "tempName%d", tempNum++ );
  99. }
  100. //--------------------------------------------------------------------------
  101. // Find element of specified name
  102. //--------------------------------------------------------------------------
  103. LangElement * LangElement::find( const char *name )
  104. {
  105. for( U32 i=0; i<elementList.size(); i++ )
  106. {
  107. if( !String::compare( (char*)elementList[i]->name, name ) )
  108. {
  109. return elementList[i];
  110. }
  111. }
  112. return NULL;
  113. }
  114. //--------------------------------------------------------------------------
  115. // Delete existing elements
  116. //--------------------------------------------------------------------------
  117. void LangElement::deleteElements()
  118. {
  119. for( U32 i=0; i<elementList.size(); i++ )
  120. {
  121. delete elementList[i];
  122. }
  123. elementList.setSize( 0 );
  124. }
  125. //--------------------------------------------------------------------------
  126. // Set name
  127. //--------------------------------------------------------------------------
  128. void LangElement::setName(const char* newName )
  129. {
  130. dStrncpy( ( char* ) name, newName, sizeof( name ) );
  131. name[ sizeof( name ) - 1 ] = '\0';
  132. }
  133. //**************************************************************************
  134. // Variable
  135. //**************************************************************************
  136. U32 Var::texUnitCount = 0;
  137. Var::Var()
  138. {
  139. dStrcpy( (char*)type, "float4", 32 );
  140. structName[0] = '\0';
  141. connectName[0] = '\0';
  142. constSortPos = cspUninit;
  143. constNum = 0;
  144. texCoordNum = 0;
  145. uniform = false;
  146. vertData = false;
  147. connector = false;
  148. sampler = false;
  149. arraySize = 1;
  150. texture = false;
  151. rank = 0;
  152. }
  153. Var::Var( const char *inName, const char *inType )
  154. {
  155. structName[0] = '\0';
  156. connectName[0] = '\0';
  157. uniform = false;
  158. vertData = false;
  159. connector = false;
  160. sampler = false;
  161. texCoordNum = 0;
  162. constSortPos = cspUninit;
  163. constNum = 0;
  164. arraySize = 1;
  165. texture = false;
  166. rank = 0;
  167. setName( inName );
  168. setType( inType );
  169. }
  170. Var::Var(const char* name, GFXShaderConstType type)
  171. {
  172. structName[0] = '\0';
  173. connectName[0] = '\0';
  174. uniform = false;
  175. vertData = false;
  176. connector = false;
  177. sampler = false;
  178. texCoordNum = 0;
  179. constSortPos = cspUninit;
  180. constNum = 0;
  181. arraySize = 1;
  182. texture = false;
  183. rank = 0;
  184. setName(name);
  185. setType(type);
  186. }
  187. void Var::setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos)
  188. {
  189. uniform = true;
  190. setType(constType.c_str());
  191. setName(constName.c_str());
  192. constSortPos = cspPass;
  193. }
  194. //--------------------------------------------------------------------------
  195. // Set struct name
  196. //--------------------------------------------------------------------------
  197. void Var::setStructName(const char* newName )
  198. {
  199. dStrncpy( ( char* ) structName, newName, sizeof( structName ) );
  200. structName[ sizeof( structName ) - 1 ] = '\0';
  201. }
  202. //--------------------------------------------------------------------------
  203. // Set connect name
  204. //--------------------------------------------------------------------------
  205. void Var::setConnectName(const char* newName )
  206. {
  207. dStrncpy( ( char* ) connectName, newName, sizeof( connectName ) );
  208. connectName[ sizeof( connectName ) - 1 ] = '\0';
  209. }
  210. //--------------------------------------------------------------------------
  211. // Set type
  212. //--------------------------------------------------------------------------
  213. void Var::setType(const char *newType )
  214. {
  215. dStrncpy( ( char* ) type, newType, sizeof( type ) );
  216. type[ sizeof( type ) - 1 ] = '\0';
  217. }
  218. void Var::setType(GFXShaderConstType constType)
  219. {
  220. const char* typeStr = "unknown"; // Default unknown type
  221. typeStr = constTypeToString(constType);
  222. // Copy the string into type[]
  223. dStrcpy((char*)type, typeStr, sizeof(type));
  224. type[sizeof(type) - 1] = '\0';
  225. }
  226. //--------------------------------------------------------------------------
  227. // print
  228. //--------------------------------------------------------------------------
  229. void Var::print( Stream &stream )
  230. {
  231. if( structName[0] != '\0' )
  232. {
  233. stream.write( dStrlen((char*)structName), structName );
  234. if(GFX->getAdapterType() == OpenGL)
  235. stream.write( 1, "_" );
  236. else
  237. stream.write( 1, "." );
  238. }
  239. stream.write( dStrlen((char*)name), name );
  240. }
  241. //--------------------------------------------------------------------------
  242. // Get next available texture unit number
  243. //--------------------------------------------------------------------------
  244. U32 Var::getTexUnitNum(U32 numElements)
  245. {
  246. U32 ret = texUnitCount;
  247. texUnitCount += numElements;
  248. return ret;
  249. }
  250. //--------------------------------------------------------------------------
  251. // Reset
  252. //--------------------------------------------------------------------------
  253. void Var::reset()
  254. {
  255. texUnitCount = 0;
  256. }
  257. //**************************************************************************
  258. // Multi line statement
  259. //**************************************************************************
  260. void MultiLine::addStatement( LangElement *elem )
  261. {
  262. AssertFatal( elem, "Attempting to add empty statement" );
  263. mStatementList.push_back( elem );
  264. }
  265. //--------------------------------------------------------------------------
  266. // Print
  267. //--------------------------------------------------------------------------
  268. void MultiLine::print( Stream &stream )
  269. {
  270. for( U32 i=0; i<mStatementList.size(); i++ )
  271. {
  272. mStatementList[i]->print( stream );
  273. }
  274. }