langElement.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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( !dStrcmp( (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" );
  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. mapsToSampler = false;
  89. arraySize = 1;
  90. }
  91. Var::Var( const char *inName, const char *inType )
  92. {
  93. structName[0] = '\0';
  94. connectName[0] = '\0';
  95. uniform = false;
  96. vertData = false;
  97. connector = false;
  98. sampler = false;
  99. mapsToSampler = false;
  100. texCoordNum = 0;
  101. constSortPos = cspUninit;
  102. arraySize = 1;
  103. setName( inName );
  104. setType( inType );
  105. }
  106. void Var::setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos)
  107. {
  108. uniform = true;
  109. setType(constType.c_str());
  110. setName(constName.c_str());
  111. constSortPos = cspPass;
  112. }
  113. //--------------------------------------------------------------------------
  114. // Set struct name
  115. //--------------------------------------------------------------------------
  116. void Var::setStructName(const char* newName )
  117. {
  118. dStrncpy( ( char* ) structName, newName, sizeof( structName ) );
  119. structName[ sizeof( structName ) - 1 ] = '\0';
  120. }
  121. //--------------------------------------------------------------------------
  122. // Set connect name
  123. //--------------------------------------------------------------------------
  124. void Var::setConnectName(const char* newName )
  125. {
  126. dStrncpy( ( char* ) connectName, newName, sizeof( connectName ) );
  127. connectName[ sizeof( connectName ) - 1 ] = '\0';
  128. }
  129. //--------------------------------------------------------------------------
  130. // Set type
  131. //--------------------------------------------------------------------------
  132. void Var::setType(const char *newType )
  133. {
  134. dStrncpy( ( char* ) type, newType, sizeof( type ) );
  135. type[ sizeof( type ) - 1 ] = '\0';
  136. }
  137. //--------------------------------------------------------------------------
  138. // print
  139. //--------------------------------------------------------------------------
  140. void Var::print( Stream &stream )
  141. {
  142. if( structName[0] != '\0' )
  143. {
  144. stream.write( dStrlen((char*)structName), structName );
  145. if(GFX->getAdapterType() == OpenGL)
  146. stream.write( 1, "_" );
  147. else
  148. stream.write( 1, "." );
  149. }
  150. stream.write( dStrlen((char*)name), name );
  151. }
  152. //--------------------------------------------------------------------------
  153. // Get next available texture unit number
  154. //--------------------------------------------------------------------------
  155. U32 Var::getTexUnitNum(U32 numElements)
  156. {
  157. U32 ret = texUnitCount;
  158. texUnitCount += numElements;
  159. return ret;
  160. }
  161. //--------------------------------------------------------------------------
  162. // Reset
  163. //--------------------------------------------------------------------------
  164. void Var::reset()
  165. {
  166. texUnitCount = 0;
  167. }
  168. //**************************************************************************
  169. // Multi line statement
  170. //**************************************************************************
  171. void MultiLine::addStatement( LangElement *elem )
  172. {
  173. AssertFatal( elem, "Attempting to add empty statement" );
  174. mStatementList.push_back( elem );
  175. }
  176. //--------------------------------------------------------------------------
  177. // Print
  178. //--------------------------------------------------------------------------
  179. void MultiLine::print( Stream &stream )
  180. {
  181. for( U32 i=0; i<mStatementList.size(); i++ )
  182. {
  183. mStatementList[i]->print( stream );
  184. }
  185. }