shaderOp.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 _SHADEROP_H_
  23. #define _SHADEROP_H_
  24. #ifndef _LANG_ELEMENT_H_
  25. #include "shaderGen/langElement.h"
  26. #endif
  27. //**************************************************************************
  28. /*!
  29. This file contains "shader operation" classes. Originally they were
  30. to represent basic language operations like adding, assignment, etc.
  31. That proved to be far too verbose when implementing shader features,
  32. so they became more generalized helper classes. Along with LangElement
  33. classes, they are the building blocks for the procedurally generated
  34. shaders.
  35. Each shader is a linked list of LangElements. The list is generated
  36. when the features of the shader are processed. When all the features
  37. are processed, then ShaderGen prints them out by traversing the linked
  38. list of LangElement and calling their print() function.
  39. The ShaderOp classes are just extensions of LangElement.
  40. */
  41. //**************************************************************************
  42. ///**************************************************************************
  43. /// Shader operation base class
  44. ///**************************************************************************
  45. class ShaderOp : public LangElement
  46. {
  47. protected:
  48. LangElement * mInput[2];
  49. public:
  50. ShaderOp( LangElement *in1, LangElement *in2 );
  51. };
  52. /*!
  53. DecOp - Declaration Operation - Used when declaring a variable in a shader
  54. feature. It will automatically print the type of the variable and then
  55. the variable name. If a shader feature set up code like:
  56. @code
  57. Var *foo = new Var;
  58. foo->setType( "float" );
  59. foo->setName( "foo" );
  60. LangElement *fooDecl = new DecOp( foo );
  61. LangElement *statement = new GenOp( " @ = 8.0 * 5.0;", fooDecl );
  62. @endcode
  63. The output in the shader file would be:
  64. @code
  65. float foo = 8.0 * 5.0;
  66. @endcode
  67. */
  68. class DecOp : public ShaderOp
  69. {
  70. typedef ShaderOp Parent;
  71. public:
  72. DecOp( Var *in1 );
  73. void print( Stream &stream ) override;
  74. };
  75. //----------------------------------------------------------------------------
  76. /*!
  77. Like the name suggests, EchoOp merely echoes back whatever string it is
  78. assigned.
  79. */
  80. //----------------------------------------------------------------------------
  81. class EchoOp : public ShaderOp
  82. {
  83. typedef ShaderOp Parent;
  84. const char * mStatement;
  85. public:
  86. EchoOp( const char * statement );
  87. ~EchoOp();
  88. void print( Stream &stream ) override;
  89. };
  90. //----------------------------------------------------------------------------
  91. /*!
  92. Accesses the given index on the variable
  93. */
  94. //----------------------------------------------------------------------------
  95. class IndexOp : public ShaderOp
  96. {
  97. typedef ShaderOp Parent;
  98. U32 mIndex;
  99. public:
  100. IndexOp( Var* var, U32 index );
  101. void print( Stream &stream ) override;
  102. };
  103. //----------------------------------------------------------------------------
  104. /*!
  105. GenOp - General Operation - Very useful for combining several variables
  106. into one LangElement statement. It uses an elipses as a parameter, so it can
  107. take as many variables as you can throw at it. It takes a string and parses
  108. it for the '@' symbol which it replaces with passed in parameters. Similar
  109. to the C statement printf(). Here's an example:
  110. @code
  111. ( assuming three variables var1, var2, var3 exist and their assigned names
  112. are var1Name, var2Name, and var3Name )
  113. LangElement *statement = new GenOp( " @ = @ * @.x + @.y;", var1, var1, var2, var3 );
  114. @endcode
  115. The output in the shader file would be:
  116. @code
  117. var1Name = var1Name * var2Name.x + var3Name.y;
  118. @endcode
  119. */
  120. //----------------------------------------------------------------------------
  121. class GenOp : public ShaderOp
  122. {
  123. typedef ShaderOp Parent;
  124. Vector<LangElement *> mElemList;
  125. public:
  126. GenOp( const char * statement, ... );
  127. void print( Stream &stream ) override;
  128. };
  129. class CastOp : public ShaderOp
  130. {
  131. typedef ShaderOp Parent;
  132. const char* mConstType;
  133. public:
  134. CastOp(Var* in1, GFXShaderConstType type);
  135. void print(Stream& stream) override;
  136. };
  137. #endif // _SHADEROP_H_