langElement.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef _LANG_ELEMENT_H_
  23. #define _LANG_ELEMENT_H_
  24. #ifndef _TVECTOR_H_
  25. #include "core/util/tVector.h"
  26. #endif
  27. #ifndef _STREAM_H_
  28. #include "core/stream/stream.h"
  29. #endif
  30. #ifndef _GFXENUMS_H_
  31. #include "gfx/gfxEnums.h"
  32. #endif
  33. #define WRITESTR( a ){ stream.write( dStrlen(a), a ); }
  34. //**************************************************************************
  35. /*!
  36. The LangElement class is the base building block for procedurally
  37. generated shader code. LangElement and its subclasses are strung
  38. together using the static Vector 'elementList'.
  39. When a shader needs to be written to disk, the elementList is
  40. traversed and print() is called on each LangElement and the shader
  41. is output. elementList is cleared after each shader is printed out.
  42. */
  43. //**************************************************************************
  44. //**************************************************************************
  45. // Language element
  46. //**************************************************************************
  47. struct LangElement
  48. {
  49. static Vector<LangElement*> elementList;
  50. static LangElement * find( const char *name );
  51. static void deleteElements();
  52. U8 name[32];
  53. static const char* constTypeToString(GFXShaderConstType constType);
  54. LangElement();
  55. virtual ~LangElement() {};
  56. virtual void print( Stream &stream ){};
  57. void setName(const char *newName );
  58. };
  59. enum ConstantSortPosition
  60. {
  61. /// Default / unset
  62. cspUninit = 0,
  63. /// Updated before every draw primitive call.
  64. cspPrimitive,
  65. /// Potentially updated every draw primitive call, but not necessarily (lights for example)
  66. cspPotentialPrimitive,
  67. /// Updated one per pass
  68. cspPass,
  69. /// Count var, do not use
  70. csp_Count
  71. };
  72. //----------------------------------------------------------------------------
  73. /*!
  74. Var - Variable - used to specify a variable to be used in a shader.
  75. Var stores information such that when it is printed out, its context
  76. can be identified and the proper information will automatically be printed.
  77. For instance, if a variable is created with 'uniform' set to true, when the
  78. shader function definition is printed, it will automatically add that
  79. variable to the incoming parameters of the shader. There are several
  80. similar cases such as when a new variable is declared within a shader.
  81. example:
  82. @code
  83. Var *modelview = new Var;
  84. modelview->setType( "float4x4" );
  85. modelview->setName( "modelview" );
  86. modelview->uniform = true;
  87. modelview->constSortPos = cspPass;
  88. @endcode
  89. it prints out in the shader declaration as:
  90. @code
  91. ConnectData main( VertData IN,
  92. uniform float4x4 modelview : register(C0) )
  93. @endcode
  94. */
  95. //----------------------------------------------------------------------------
  96. struct Var : public LangElement
  97. {
  98. U8 type[32];
  99. U8 structName[32];
  100. char connectName[32];
  101. ConstantSortPosition constSortPos; // used to calculate constant number
  102. U32 constNum;
  103. U32 texCoordNum;
  104. bool uniform; // argument passed in through constant registers
  105. bool vertData; // argument coming from vertex data
  106. bool connector; // variable that will be passed to pixel shader
  107. bool sampler; // texture
  108. bool texture; //for D3D11 texture variables
  109. U32 arraySize; // 1 = no array, > 1 array of "type"
  110. U32 rank; // optional rank system to assist in sorting vars if needed
  111. static U32 texUnitCount;
  112. static U32 getTexUnitNum(U32 numElements = 1);
  113. static void reset();
  114. // Default
  115. Var();
  116. Var( const char *name, const char *type );
  117. Var( const char *name, GFXShaderConstType type );
  118. void setStructName(const char *newName );
  119. void setConnectName(const char *newName );
  120. void setType(const char *newType );
  121. void setType(GFXShaderConstType constType);
  122. void print( Stream &stream ) override;
  123. // Construct a uniform / shader const var
  124. void setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos);
  125. };
  126. //----------------------------------------------------------------------------
  127. /*!
  128. MultiLine - Multi Line Statement - This class simply ties multiple
  129. example:
  130. @code
  131. MultiLine *meta = new MultiLine;
  132. meta->addStatement( new GenOp( "foo = true;\r\n" ) );
  133. meta->addStatement( new GenOp( "bar = false;\r\n ) );
  134. @endcode
  135. it prints out in the shader declaration as:
  136. @code
  137. foo = true;
  138. bar = false;
  139. @endcode
  140. */
  141. //----------------------------------------------------------------------------
  142. class MultiLine : public LangElement
  143. {
  144. Vector <LangElement*> mStatementList;
  145. public:
  146. MultiLine()
  147. {
  148. VECTOR_SET_ASSOCIATION( mStatementList );
  149. }
  150. void addStatement( LangElement *elem );
  151. void print( Stream &stream ) override;
  152. };
  153. #endif // _LANG_ELEMENT_H_