shaderGen.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 _SHADERGEN_H_
  23. #define _SHADERGEN_H_
  24. #ifndef _LANG_ELEMENT_H_
  25. #include "shaderGen/langElement.h"
  26. #endif
  27. #ifndef _SHADERFEATURE_H_
  28. #include "shaderGen/shaderFeature.h"
  29. #endif
  30. #ifndef _SHADERCOMP_H_
  31. #include "shaderGen/shaderComp.h"
  32. #endif
  33. #ifndef _GFXDEVICE_H_
  34. #include "gfx/gfxDevice.h"
  35. #endif
  36. #ifndef _AUTOPTR_H_
  37. #include "core/util/autoPtr.h"
  38. #endif
  39. #ifndef _TSINGLETON_H_
  40. #include "core/util/tSingleton.h"
  41. #endif
  42. #ifndef _VOLUME_H_
  43. #include "core/volume.h"
  44. #endif
  45. #ifndef _MATERIALFEATUREDATA_H_
  46. #include "materials/materialFeatureData.h"
  47. #endif
  48. /// Base class used by shaderGen to be API agnostic. Subclasses implement the various methods
  49. /// in an API specific way.
  50. class ShaderGenPrinter
  51. {
  52. public:
  53. virtual ~ShaderGenPrinter() {}
  54. /// Prints a simple header, including the engine name, language type, and
  55. /// the fact that the shader was procedurally generated
  56. virtual void printShaderHeader(Stream& stream) = 0;
  57. /// Prints a comment block specifying the beginning of the main() function (or equivalent)
  58. virtual void printMainComment(Stream& stream) = 0;
  59. /// Prints the final line of the vertex shader, e.g. return OUT; }, }, END
  60. virtual void printVertexShaderCloser(Stream& stream) = 0;
  61. /// Prints the output struct for the pixel shader. Probably only used in HLSL/Cg.
  62. virtual void printPixelShaderOutputStruct(Stream& stream, const MaterialFeatureData &featureData) = 0;
  63. /// Prints the final line of the pixel shader.
  64. virtual void printPixelShaderCloser(Stream& stream) = 0;
  65. // Prints a line into the shader adding the proper terminator.
  66. virtual void printLine(Stream& stream, const String& line) = 0;
  67. };
  68. /// Abstract factory for created (and initializating, if necessary) shader components.
  69. class ShaderGenComponentFactory
  70. {
  71. public:
  72. virtual ~ShaderGenComponentFactory() {}
  73. /// Creates and initializes a vertex input connector with the specified flags
  74. virtual ShaderComponent* createVertexInputConnector( const GFXVertexFormat &vertexFormat ) = 0;
  75. /// Creates and names a vertex/pixel connector
  76. virtual ShaderComponent* createVertexPixelConnector() = 0;
  77. /// Creates an instance of VertexParamsDef
  78. virtual ShaderComponent* createVertexParamsDef() = 0;
  79. /// Creates an instance of PixelParamsDef
  80. virtual ShaderComponent* createPixelParamsDef() = 0;
  81. };
  82. //**************************************************************************
  83. /*!
  84. The ShaderGen class takes shader feature data (usually created by
  85. MatInstance) and creates a vertex/pixel shader pair in text files
  86. to be later compiled by a shader manager.
  87. It accomplishes this task by creating a group of shader "components" and
  88. "features" that output bits of high level shader code. Shader components
  89. translate to structures in HLSL that indicate incoming vertex data,
  90. data that is output from the vertex shader to the pixel shader, and data
  91. such as constants and textures that are passed directly to the shader
  92. from the app.
  93. Shader features are separable shader functions that can be turned on or
  94. off. Examples would be bumpmapping and specular highlights. See
  95. MaterialFeatureData for the current list of features supported.
  96. ShaderGen processes all of the features that are present for a desired
  97. shader, and then prints them out to the respective vertex or pixel
  98. shader file.
  99. For more information on shader features and components see the
  100. ShaderFeature and ShaderComponent classes.
  101. */
  102. //**************************************************************************
  103. //**************************************************************************
  104. // Shader generator
  105. //**************************************************************************
  106. class ShaderGen
  107. {
  108. public:
  109. virtual ~ShaderGen();
  110. /// Parameter 1 is the ShaderGen instance to initialize.
  111. typedef Delegate<void (ShaderGen*)> ShaderGenInitDelegate;
  112. /// Register an initialization delegate for adapterType. This should setPrinter/ComponentFactory/etc, and register
  113. /// shader features.
  114. void registerInitDelegate(GFXAdapterType adapterType, ShaderGenInitDelegate& initDelegate);
  115. /// Signal used to notify systems to register features.
  116. typedef Signal<void(GFXAdapterType type)> FeatureInitSignal;
  117. /// Returns the signal used to notify systems to register features.
  118. FeatureInitSignal& getFeatureInitSignal() { return mFeatureInitSignal; }
  119. /// vertFile and pixFile are filled in by this function. They point to
  120. /// the vertex and pixel shader files. pixVersion is also filled in by
  121. /// this function.
  122. /// @param assignNum used to assign a specific number as the filename
  123. void generateShader( const MaterialFeatureData &featureData,
  124. char *vertFile,
  125. char *pixFile,
  126. F32 *pixVersion,
  127. const GFXVertexFormat *vertexFormat,
  128. const char* cacheName,
  129. Vector<GFXShaderMacro> &macros);
  130. // Returns a shader that implements the features listed by dat.
  131. GFXShader* getShader( const MaterialFeatureData &dat, const GFXVertexFormat *vertexFormat, const Vector<GFXShaderMacro> *macros, const Vector<String> &samplers );
  132. // This will delete all of the procedural shaders that we have. Used to regenerate shaders when
  133. // the ShaderFeatures have changed (due to lighting system change, or new plugin)
  134. virtual void flushProceduralShaders();
  135. void setPrinter(ShaderGenPrinter* printer) { mPrinter = printer; }
  136. void setComponentFactory(ShaderGenComponentFactory* factory) { mComponentFactory = factory; }
  137. void setFileEnding(String ending) { mFileEnding = ending; }
  138. static String smCommonShaderPath;
  139. protected:
  140. friend class ManagedSingleton<ShaderGen>;
  141. // Shader generation
  142. MaterialFeatureData mFeatureData;
  143. const GFXVertexFormat *mVertexFormat;
  144. Vector< ShaderComponent *> mComponents;
  145. AutoPtr<ShaderGenPrinter> mPrinter;
  146. AutoPtr<ShaderGenComponentFactory> mComponentFactory;
  147. String mFileEnding;
  148. /// The currently processing output.
  149. MultiLine *mOutput;
  150. GFXVertexFormat mInstancingFormat;
  151. /// Init
  152. bool mInit;
  153. ShaderGenInitDelegate mInitDelegates[GFXAdapterType_Count];
  154. FeatureInitSignal mFeatureInitSignal;
  155. bool mRegisteredWithGFX;
  156. Torque::FS::FileSystemRef mMemFS;
  157. /// Map of cache string -> shaders
  158. typedef Map<String, GFXShaderRef> ShaderMap;
  159. ShaderMap mProcShaders;
  160. ShaderGen();
  161. bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
  162. /// Causes the init delegate to be called.
  163. void initShaderGen();
  164. void _init();
  165. void _uninit();
  166. /// Creates all the various shader components that will be filled in when
  167. /// the shader features are processed.
  168. void _createComponents();
  169. void _printFeatureList(Stream &stream);
  170. /// print out the processed features to the file stream
  171. void _printFeatures( Stream &stream );
  172. void _printDependencies( Stream &stream );
  173. void _processPixFeatures( Vector<GFXShaderMacro> &macros, bool macrosOnly = false );
  174. void _printPixShader( Stream &stream );
  175. void _processVertFeatures( Vector<GFXShaderMacro> &macros, bool macrosOnly = false );
  176. void _printVertShader( Stream &stream );
  177. // For ManagedSingleton.
  178. static const char* getSingletonName() { return "ShaderGen"; }
  179. };
  180. /// Returns the ShaderGen singleton.
  181. #define SHADERGEN ManagedSingleton<ShaderGen>::instance()
  182. #endif // _SHADERGEN_H_