langElement.h 5.8 KB

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