langElement.cpp 6.5 KB

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