langElement.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. //--------------------------------------------------------------------------
  31. // Constructor
  32. //--------------------------------------------------------------------------
  33. LangElement::LangElement()
  34. {
  35. elementList.push_back( this );
  36. static U32 tempNum = 0;
  37. dSprintf( (char*)name, sizeof(name), "tempName%d", tempNum++ );
  38. }
  39. //--------------------------------------------------------------------------
  40. // Find element of specified name
  41. //--------------------------------------------------------------------------
  42. LangElement * LangElement::find( const char *name )
  43. {
  44. for( U32 i=0; i<elementList.size(); i++ )
  45. {
  46. if( !String::compare( (char*)elementList[i]->name, name ) )
  47. {
  48. return elementList[i];
  49. }
  50. }
  51. return NULL;
  52. }
  53. //--------------------------------------------------------------------------
  54. // Delete existing elements
  55. //--------------------------------------------------------------------------
  56. void LangElement::deleteElements()
  57. {
  58. for( U32 i=0; i<elementList.size(); i++ )
  59. {
  60. delete elementList[i];
  61. }
  62. elementList.setSize( 0 );
  63. }
  64. //--------------------------------------------------------------------------
  65. // Set name
  66. //--------------------------------------------------------------------------
  67. void LangElement::setName(const char* newName )
  68. {
  69. dStrncpy( ( char* ) name, newName, sizeof( name ) );
  70. name[ sizeof( name ) - 1 ] = '\0';
  71. }
  72. //**************************************************************************
  73. // Variable
  74. //**************************************************************************
  75. U32 Var::texUnitCount = 0;
  76. Var::Var()
  77. {
  78. dStrcpy( (char*)type, "float4", 32 );
  79. structName[0] = '\0';
  80. connectName[0] = '\0';
  81. constSortPos = cspUninit;
  82. constNum = 0;
  83. texCoordNum = 0;
  84. uniform = false;
  85. vertData = false;
  86. connector = false;
  87. sampler = false;
  88. arraySize = 1;
  89. texture = false;
  90. rank = 0;
  91. }
  92. Var::Var( const char *inName, const char *inType )
  93. {
  94. structName[0] = '\0';
  95. connectName[0] = '\0';
  96. uniform = false;
  97. vertData = false;
  98. connector = false;
  99. sampler = false;
  100. texCoordNum = 0;
  101. constSortPos = cspUninit;
  102. constNum = 0;
  103. arraySize = 1;
  104. texture = false;
  105. rank = 0;
  106. setName( inName );
  107. setType( inType );
  108. }
  109. void Var::setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos)
  110. {
  111. uniform = true;
  112. setType(constType.c_str());
  113. setName(constName.c_str());
  114. constSortPos = cspPass;
  115. }
  116. //--------------------------------------------------------------------------
  117. // Set struct name
  118. //--------------------------------------------------------------------------
  119. void Var::setStructName(const char* newName )
  120. {
  121. dStrncpy( ( char* ) structName, newName, sizeof( structName ) );
  122. structName[ sizeof( structName ) - 1 ] = '\0';
  123. }
  124. //--------------------------------------------------------------------------
  125. // Set connect name
  126. //--------------------------------------------------------------------------
  127. void Var::setConnectName(const char* newName )
  128. {
  129. dStrncpy( ( char* ) connectName, newName, sizeof( connectName ) );
  130. connectName[ sizeof( connectName ) - 1 ] = '\0';
  131. }
  132. //--------------------------------------------------------------------------
  133. // Set type
  134. //--------------------------------------------------------------------------
  135. void Var::setType(const char *newType )
  136. {
  137. dStrncpy( ( char* ) type, newType, sizeof( type ) );
  138. type[ sizeof( type ) - 1 ] = '\0';
  139. }
  140. //--------------------------------------------------------------------------
  141. // print
  142. //--------------------------------------------------------------------------
  143. void Var::print( Stream &stream )
  144. {
  145. if( structName[0] != '\0' )
  146. {
  147. stream.write( dStrlen((char*)structName), structName );
  148. if(GFX->getAdapterType() == OpenGL)
  149. stream.write( 1, "_" );
  150. else
  151. stream.write( 1, "." );
  152. }
  153. stream.write( dStrlen((char*)name), name );
  154. }
  155. //--------------------------------------------------------------------------
  156. // Get next available texture unit number
  157. //--------------------------------------------------------------------------
  158. U32 Var::getTexUnitNum(U32 numElements)
  159. {
  160. U32 ret = texUnitCount;
  161. texUnitCount += numElements;
  162. return ret;
  163. }
  164. //--------------------------------------------------------------------------
  165. // Reset
  166. //--------------------------------------------------------------------------
  167. void Var::reset()
  168. {
  169. texUnitCount = 0;
  170. }
  171. //**************************************************************************
  172. // Multi line statement
  173. //**************************************************************************
  174. void MultiLine::addStatement( LangElement *elem )
  175. {
  176. AssertFatal( elem, "Attempting to add empty statement" );
  177. mStatementList.push_back( elem );
  178. }
  179. //--------------------------------------------------------------------------
  180. // Print
  181. //--------------------------------------------------------------------------
  182. void MultiLine::print( Stream &stream )
  183. {
  184. for( U32 i=0; i<mStatementList.size(); i++ )
  185. {
  186. mStatementList[i]->print( stream );
  187. }
  188. }