shaderFeature.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 _SHADERFEATURE_H_
  23. #define _SHADERFEATURE_H_
  24. #ifndef _MATERIALDEFINITION_H_
  25. #include "materials/materialDefinition.h"
  26. #endif
  27. #ifndef _SHADERCOMP_H_
  28. #include "shaderGen/shaderComp.h"
  29. #endif
  30. #ifndef _SHADER_DEPENDENCY_H_
  31. #include "shaderGen/shaderDependency.h"
  32. #endif
  33. class MultiLine;
  34. struct LangElement;
  35. struct MaterialFeatureData;
  36. class GFXShaderConstBuffer;
  37. struct RenderPassData;
  38. struct SceneData;
  39. class SceneRenderState;
  40. class GFXShader;
  41. class GFXVertexFormat;
  42. ///
  43. class ShaderFeatureConstHandles
  44. {
  45. public:
  46. virtual ~ShaderFeatureConstHandles() { }
  47. virtual void init( GFXShader *shader ) = 0;
  48. virtual void setConsts( SceneRenderState *state,
  49. const SceneData &sgData,
  50. GFXShaderConstBuffer *buffer ) = 0;
  51. };
  52. //**************************************************************************
  53. /*!
  54. The ShaderFeature class is the base class for every procedurally generated
  55. feature. Each feature the engine recognizes is part of the MaterialFeatureType
  56. enum. That structure is used to indicate which features are present in a shader
  57. to be generated. This is useful as many ShaderFeatures will output different
  58. code depending on what other features are going to be in the shader.
  59. Shaders are generated using the ShaderFeature interface, so all of the
  60. descendants interact pretty much the same way.
  61. */
  62. //**************************************************************************
  63. //**************************************************************************
  64. // Shader Feature
  65. //**************************************************************************
  66. class ShaderFeature
  67. {
  68. public:
  69. // Bitfield which allows a shader feature to say which render targets it outputs
  70. // data to (could be more than one).
  71. enum OutputTarget
  72. {
  73. DefaultTarget = 1 << 0,
  74. RenderTarget1 = 1 << 1,
  75. RenderTarget2 = 1 << 2,
  76. RenderTarget3 = 1 << 3,
  77. RenderTarget4 = 1 << 4,
  78. RenderTarget5 = 1 << 5,
  79. };
  80. protected:
  81. LangElement *output;
  82. /// The list of unique shader dependencies.
  83. Vector<const ShaderDependency *> mDependencies;
  84. ///
  85. S32 mProcessIndex;
  86. public:
  87. // TODO: Make this protected and give it a proper API.
  88. const GFXVertexFormat *mVertexFormat;
  89. // TODO: Make this protected and give it a proper API.
  90. GFXVertexFormat *mInstancingFormat;
  91. public:
  92. //**************************************************************************
  93. /*!
  94. The Resources structure is used by ShaderFeature to indicate how many
  95. hardware "resources" it needs. Resources are things such as how
  96. many textures it uses and how many texture registers it needs to pass
  97. information from the vertex to the pixel shader.
  98. The Resources data can change depending what hardware is available. For
  99. instance, pixel 1.x level hardware may need more texture registers than
  100. pixel 2.0+ hardware because each texture register can only be used with
  101. its respective texture sampler.
  102. The data in Resources is used to determine how many features can be
  103. squeezed into a singe shader. If a feature requires too many resources
  104. to fit into the current shader, it will be put into another pass.
  105. */
  106. //**************************************************************************
  107. struct Resources
  108. {
  109. U32 numTex;
  110. U32 numTexReg;
  111. Resources()
  112. {
  113. dMemset( this, 0, sizeof( Resources ) );
  114. }
  115. };
  116. //-----------------------------------------------------------------------
  117. // Base functions
  118. //-----------------------------------------------------------------------
  119. ShaderFeature()
  120. : output( NULL ),
  121. mProcessIndex( 0 ),
  122. mVertexFormat( NULL ),
  123. mInstancingFormat( NULL )
  124. {
  125. }
  126. virtual ~ShaderFeature() {}
  127. /// returns output from a processed vertex or pixel shader
  128. LangElement* getOutput() const { return output; }
  129. ///
  130. void setProcessIndex( S32 index ) { mProcessIndex = index; }
  131. ///
  132. S32 getProcessIndex() const { return mProcessIndex; }
  133. //-----------------------------------------------------------------------
  134. // Virtual Functions
  135. //-----------------------------------------------------------------------
  136. /// Get the incoming base texture coords - useful for bumpmap and detail maps
  137. virtual Var* getVertTexCoord( const String &name ) = 0;
  138. /// Set up a texture space matrix - to pass into pixel shader
  139. virtual LangElement * setupTexSpaceMat( Vector<ShaderComponent*> &componentList,
  140. Var **texSpaceMat ) = 0;
  141. /// Expand and assign a normal map. This takes care of compressed normal maps as well.
  142. virtual LangElement * expandNormalMap( LangElement *sampleNormalOp,
  143. LangElement *normalDecl, LangElement *normalVar, const MaterialFeatureData &fd ) = 0;
  144. /// Helper function for applying the color to shader output.
  145. ///
  146. /// @param elem The rbg or rgba color to assign.
  147. ///
  148. /// @param blend The type of blending to perform.
  149. ///
  150. /// @param lerpElem The optional lerp parameter when doing a LerpAlpha blend,
  151. /// if not set then the elem is used.
  152. ///
  153. virtual LangElement* assignColor( LangElement *elem,
  154. Material::BlendOp blend,
  155. LangElement *lerpElem = NULL,
  156. ShaderFeature::OutputTarget outputTarget = ShaderFeature::DefaultTarget ) = 0;
  157. //-----------------------------------------------------------------------
  158. /*!
  159. Process vertex shader - This function is used by each feature to
  160. generate a list of LangElements that can be traversed and "printed"
  161. to generate the actual shader code. The 'output' member is the head
  162. of that list.
  163. The componentList is used mostly for access to the "Connector"
  164. structure which is used to pass data from the vertex to the pixel
  165. shader.
  166. The MaterialFeatureData parameter is used to determine what other
  167. features are present for the shader being generated.
  168. */
  169. //-----------------------------------------------------------------------
  170. virtual void processVert( Vector<ShaderComponent*> &componentList,
  171. const MaterialFeatureData &fd )
  172. { output = NULL; }
  173. //-----------------------------------------------------------------------
  174. /*!
  175. Process pixel shader - This function is used by each feature to
  176. generate a list of LangElements that can be traversed and "printed"
  177. to generate the actual shader code. The 'output' member is the head
  178. of that list.
  179. The componentList is used mostly for access to the "Connector"
  180. structure which is used to pass data from the vertex to the pixel
  181. shader.
  182. The MaterialFeatureData parameter is used to determine what other
  183. features are present for the shader being generated.
  184. */
  185. //-----------------------------------------------------------------------
  186. virtual void processPix( Vector<ShaderComponent*> &componentList,
  187. const MaterialFeatureData &fd )
  188. { output = NULL; }
  189. /// Allows the feature to add macros to pixel shader compiles.
  190. virtual void processPixMacros( Vector<GFXShaderMacro> &macros, const MaterialFeatureData &fd ) {};
  191. /// Allows the feature to add macros to vertex shader compiles.
  192. virtual void processVertMacros( Vector<GFXShaderMacro> &macros, const MaterialFeatureData &fd ) {};
  193. /// Identifies what type of blending a feature uses. This is used to
  194. /// group features with the same blend operation together in a multipass
  195. /// situation.
  196. virtual Material::BlendOp getBlendOp() { return Material::Add; }
  197. /// Returns the resource requirements of this feature based on what
  198. /// other features are present. The "resources" are things such as
  199. /// texture units, and texture registers of which there can be
  200. /// very limited numbers. The resources can vary depending on hardware
  201. /// and what other features are present.
  202. virtual Resources getResources( const MaterialFeatureData &fd );
  203. /// Fills texture related info in RenderPassData for this feature. It
  204. /// takes into account the current pass (passData) as well as what other
  205. /// data is available to the material stage (stageDat).
  206. ///
  207. /// For instance, ReflectCubeFeatHLSL would like to modulate its output
  208. /// by the alpha channel of another texture. If the current pass does
  209. /// not contain a diffuse or bump texture, but the Material does, then
  210. /// this function allows it to use one of those textures in the current
  211. /// pass.
  212. virtual void setTexData( Material::StageData &stageDat,
  213. const MaterialFeatureData &fd,
  214. RenderPassData &passData,
  215. U32 &texIndex ){};
  216. /// Returns the name of this feature.
  217. virtual String getName() = 0;
  218. /// Adds a dependency to this shader feature.
  219. virtual void addDependency( const ShaderDependency *depends );
  220. /// Gets the dependency list for this shader feature.
  221. virtual const Vector<const ShaderDependency *> &getDependencies() const { return mDependencies; }
  222. /// Returns the output variable name for this feature if it applies.
  223. virtual const char* getOutputVarName() const { return NULL; }
  224. /// Gets the render target this shader feature is assigning data to.
  225. virtual U32 getOutputTargets( const MaterialFeatureData &fd ) const { return DefaultTarget; }
  226. /// Returns the name of output targer var.
  227. const char* getOutputTargetVarName( OutputTarget target = DefaultTarget ) const;
  228. // Called from ProcessedShaderMaterial::determineFeatures to enable/disable features.
  229. virtual void determineFeature( Material *material,
  230. const GFXVertexFormat *vertexFormat,
  231. U32 stageNum,
  232. const FeatureType &type,
  233. const FeatureSet &features,
  234. MaterialFeatureData *outFeatureData ) { }
  235. //
  236. virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject ) { return NULL; }
  237. /// Called after processing the vertex and processing the pixel
  238. /// to cleanup any temporary structures stored in the feature.
  239. virtual void reset() { output = NULL; mProcessIndex = 0; mInstancingFormat = NULL; mVertexFormat = NULL; }
  240. /// A simpler helper function which either finds
  241. /// the existing local var or creates one.
  242. static Var* findOrCreateLocal( const char *name,
  243. const char *type,
  244. MultiLine *multi );
  245. // Set the instancing format
  246. void setInstancingFormat(GFXVertexFormat *format);
  247. };
  248. #endif // _SHADERFEATURE_H_