2
0

shaderOp.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*!
  54. DecOp - Declaration Operation - Used when declaring a variable in a shader
  55. feature. It will automatically print the type of the variable and then
  56. the variable name. If a shader feature set up code like:
  57. @code
  58. Var *foo = new Var;
  59. foo->setType( "float" );
  60. foo->setName( "foo" );
  61. LangElement *fooDecl = new DecOp( foo );
  62. LangElement *statement = new GenOp( " @ = 8.0 * 5.0;", fooDecl );
  63. @endcode
  64. The output in the shader file would be:
  65. @code
  66. float foo = 8.0 * 5.0;
  67. @endcode
  68. */
  69. //----------------------------------------------------------------------------
  70. class DecOp : public ShaderOp
  71. {
  72. typedef ShaderOp Parent;
  73. public:
  74. DecOp( Var *in1 );
  75. virtual void print( Stream &stream );
  76. };
  77. //----------------------------------------------------------------------------
  78. /*!
  79. Like the name suggests, EchoOp merely echoes back whatever string it is
  80. assigned.
  81. */
  82. //----------------------------------------------------------------------------
  83. class EchoOp : public ShaderOp
  84. {
  85. typedef ShaderOp Parent;
  86. const char * mStatement;
  87. public:
  88. EchoOp( const char * statement );
  89. ~EchoOp();
  90. virtual void print( Stream &stream );
  91. };
  92. //----------------------------------------------------------------------------
  93. /*!
  94. Accesses the given index on the variable
  95. */
  96. //----------------------------------------------------------------------------
  97. class IndexOp : public ShaderOp
  98. {
  99. typedef ShaderOp Parent;
  100. U32 mIndex;
  101. public:
  102. IndexOp( Var* var, U32 index );
  103. virtual void print( Stream &stream );
  104. };
  105. //----------------------------------------------------------------------------
  106. /*!
  107. GenOp - General Operation - Very useful for combining several variables
  108. into one LangElement statement. It uses an elipses as a parameter, so it can
  109. take as many variables as you can throw at it. It takes a string and parses
  110. it for the '@' symbol which it replaces with passed in parameters. Similar
  111. to the C statement printf(). Here's an example:
  112. @code
  113. ( assuming three variables var1, var2, var3 exist and their assigned names
  114. are var1Name, var2Name, and var3Name )
  115. LangElement *statement = new GenOp( " @ = @ * @.x + @.y;", var1, var1, var2, var3 );
  116. @endcode
  117. The output in the shader file would be:
  118. @code
  119. var1Name = var1Name * var2Name.x + var3Name.y;
  120. @endcode
  121. */
  122. //----------------------------------------------------------------------------
  123. class GenOp : public ShaderOp
  124. {
  125. typedef ShaderOp Parent;
  126. Vector<LangElement *> mElemList;
  127. public:
  128. GenOp( const char * statement, ... );
  129. virtual void print( Stream &stream );
  130. };
  131. #endif // _SHADEROP_H_