processedMaterial.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 _MATERIALS_PROCESSEDMATERIAL_H_
  23. #define _MATERIALS_PROCESSEDMATERIAL_H_
  24. #ifndef _MATERIALDEFINITION_H_
  25. #include "materials/materialDefinition.h"
  26. #endif
  27. #ifndef _MATERIALFEATUREDATA_H_
  28. #include "materials/materialFeatureData.h"
  29. #endif
  30. #ifndef _GFXSTATEBLOCK_H_
  31. #include "gfx/gfxStateBlock.h"
  32. #endif
  33. #ifndef _MATTEXTURETARGET_H_
  34. #include "materials/matTextureTarget.h"
  35. #endif
  36. #ifndef _MATSTATEHINT_H_
  37. #include "materials/matStateHint.h"
  38. #endif
  39. #ifndef CUSTOMSHADERBINDINGDATA_H
  40. #include "materials/customShaderBindingData.h"
  41. #endif
  42. class ShaderFeature;
  43. class MaterialParameters;
  44. class MaterialParameterHandle;
  45. class SceneRenderState;
  46. class GFXVertexBufferHandleBase;
  47. class GFXPrimitiveBufferHandle;
  48. class MatrixSet;
  49. class GuiTreeViewCtrl;
  50. /// This contains the common data needed to render a pass.
  51. struct RenderPassData
  52. {
  53. public:
  54. struct TexSlotT
  55. {
  56. /// This is the default type of texture which
  57. /// is valid with most texture types.
  58. /// @see mTexType
  59. GFXTexHandle texObject;
  60. /// Only valid when the texture type is set
  61. /// to Material::TexTarget.
  62. /// @see mTexType
  63. NamedTexTargetRef texTarget;
  64. } mTexSlot[Material::MAX_TEX_PER_PASS];
  65. U32 mTexType[Material::MAX_TEX_PER_PASS];
  66. String mSamplerNames[Material::MAX_TEX_PER_PASS];
  67. /// The cubemap to use when the texture type is
  68. /// set to Material::Cube.
  69. /// @see mTexType
  70. GFXCubemapHandle mCubeMap;
  71. U32 mNumTex;
  72. U32 mNumTexReg;
  73. MaterialFeatureData mFeatureData;
  74. Vector<CustomShaderFeatureData*> mCustomShaderFeatureData;
  75. bool mGlow;
  76. Material::BlendOp mBlendOp;
  77. U32 mStageNum;
  78. /// State permutations, used to index into
  79. /// the render states array.
  80. /// @see mRenderStates
  81. enum
  82. {
  83. STATE_REFLECT = 1,
  84. STATE_TRANSLUCENT = 2,
  85. STATE_GLOW = 4,
  86. STATE_WIREFRAME = 8,
  87. STATE_MAX = 16
  88. };
  89. ///
  90. GFXStateBlockRef mRenderStates[STATE_MAX];
  91. RenderPassData();
  92. virtual ~RenderPassData() { reset(); }
  93. virtual void reset();
  94. /// Creates and returns a unique description string.
  95. virtual String describeSelf() const;
  96. };
  97. /// This is an abstract base class which provides the external
  98. /// interface all subclasses must implement. This interface
  99. /// primarily consists of setting state. Pass creation
  100. /// is implementation specific, and internal, thus it is
  101. /// not in this base class.
  102. class ProcessedMaterial
  103. {
  104. public:
  105. ProcessedMaterial();
  106. virtual ~ProcessedMaterial();
  107. /// @name State setting functions
  108. ///
  109. /// @{
  110. ///
  111. virtual void addStateBlockDesc(const GFXStateBlockDesc& sb);
  112. ///
  113. virtual void updateStateBlocks() { _initRenderPassDataStateBlocks(); }
  114. /// Set the user defined shader macros.
  115. virtual void setShaderMacros( const Vector<GFXShaderMacro> &macros ) { mUserMacros = macros; }
  116. /// Sets the textures needed for rendering the current pass
  117. virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ) = 0;
  118. /// Sets the transformation matrix, i.e. Model * View * Projection
  119. virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass) = 0;
  120. /// Sets the node transforms for HW Skinning
  121. virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) = 0;
  122. /// Sets any custom shader data
  123. virtual void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData, const U32 pass) = 0;
  124. /// Sets the scene info like lights for the given pass.
  125. virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass) = 0;
  126. /// Sets the given vertex and primitive buffers so we can render geometry
  127. virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer);
  128. /// @see BaseMatInstance::setUserObject
  129. virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; }
  130. ///
  131. virtual bool stepInstance();
  132. /// @}
  133. /// Initializes us (eg. loads textures, creates passes, generates shaders)
  134. virtual bool init( const FeatureSet& features,
  135. const GFXVertexFormat *vertexFormat,
  136. const MatFeaturesDelegate &featuresDelegate ) = 0;
  137. /// Returns the state hint which can be used for
  138. /// sorting and fast comparisions of the equality
  139. /// of a material instance.
  140. virtual const MatStateHint& getStateHint() const { return mStateHint; }
  141. /// Sets up the given pass. Returns true if the pass was set up, false if there was an error or if
  142. /// the specified pass is out of bounds.
  143. virtual bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass) = 0;
  144. // Material parameter methods
  145. virtual MaterialParameters* allocMaterialParameters() = 0;
  146. virtual MaterialParameters* getDefaultMaterialParameters() = 0;
  147. virtual void setMaterialParameters(MaterialParameters* param, S32 pass) { mCurrentParams = param; };
  148. virtual MaterialParameters* getMaterialParameters() { return mCurrentParams; }
  149. virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0;
  150. /// Returns the pass data for the given pass.
  151. RenderPassData* getPass(U32 pass)
  152. {
  153. if(pass >= mPasses.size())
  154. return NULL;
  155. return mPasses[pass];
  156. }
  157. /// Returns the pass data for the given pass.
  158. const RenderPassData* getPass( U32 pass ) const { return mPasses[pass]; }
  159. /// Returns the number of stages we're rendering (not to be confused with the number of passes).
  160. virtual U32 getNumStages() = 0;
  161. /// Returns the number of passes we are rendering (not to be confused with the number of stages).
  162. U32 getNumPasses() const { return mPasses.size(); }
  163. /// Returns true if any pass glows
  164. bool hasGlow() const { return mHasGlow; }
  165. /// Returns true if any pass accumulates
  166. bool hasAccumulation() const { return mHasAccumulation; }
  167. /// Gets the stage number for a pass
  168. U32 getStageFromPass(U32 pass) const
  169. {
  170. if(pass >= mPasses.size())
  171. return 0;
  172. return mPasses[pass]->mStageNum;
  173. }
  174. /// Returns the active features in use by this material.
  175. /// @see BaseMatInstance::getFeatures
  176. const FeatureSet& getFeatures() const { return mFeatures; }
  177. /// Dump shader info, or FF texture info?
  178. virtual void dumpMaterialInfo() { }
  179. virtual void getMaterialInfo(GuiTreeViewCtrl* tree, U32 item) {}
  180. /// Returns the source material.
  181. Material* getMaterial() const { return mMaterial; }
  182. /// Returns the texture used by a stage
  183. GFXTexHandle getStageTexture(U32 stage, const FeatureType &type)
  184. {
  185. return (stage < Material::MAX_STAGES) ? mStages[stage].getTex(type) : NULL;
  186. }
  187. protected:
  188. /// Our passes.
  189. Vector<RenderPassData*> mPasses;
  190. /// The active features in use by this material.
  191. FeatureSet mFeatures;
  192. /// The material which we are processing.
  193. Material* mMaterial;
  194. MaterialParameters* mCurrentParams;
  195. /// Material::StageData is used here because the shader
  196. /// generator throws a fit if it's passed anything else.
  197. Material::StageData mStages[Material::MAX_STAGES];
  198. /// If we've already loaded the stage data
  199. bool mHasSetStageData;
  200. /// If we glow
  201. bool mHasGlow;
  202. /// If we have accumulation.
  203. bool mHasAccumulation;
  204. /// Number of stages (not to be confused with number of passes)
  205. U32 mMaxStages;
  206. /// The vertex format on which this material will render.
  207. const GFXVertexFormat *mVertexFormat;
  208. /// Set by addStateBlockDesc, should be considered
  209. /// when initPassStateBlock is called.
  210. GFXStateBlockDesc mUserDefined;
  211. /// The user defined macros to pass to the
  212. /// shader initialization.
  213. Vector<GFXShaderMacro> mUserMacros;
  214. /// The user defined object to pass to ShaderFeature::createConstHandles.
  215. SimObject *mUserObject;
  216. /// The state hint used for material sorting
  217. /// and quick equality comparision.
  218. MatStateHint mStateHint;
  219. /// Loads all the textures for all of the stages in the Material
  220. virtual void _setStageData();
  221. /// Sets the blend state for rendering
  222. void _setBlendState(Material::BlendOp blendOp, GFXStateBlockDesc& desc );
  223. /// Returns the path the material will attempt to load for a given texture filename.
  224. String _getTexturePath(const String& filename);
  225. /// Loads the texture located at _getTexturePath(filename) and gives it the specified profile
  226. GFXTexHandle _createTexture( const char *filename, GFXTextureProfile *profile );
  227. GFXTexHandle _createCompositeTexture(const char *filenameR, const char *filenameG, const char *filenameB, const char *filenameA, U32 inputKey[4], GFXTextureProfile *profile);
  228. /// @name State blocks
  229. ///
  230. /// @{
  231. /// Creates the default state block templates, used by initStateBlocks.
  232. virtual void _initStateBlockTemplates(GFXStateBlockDesc& stateTranslucent, GFXStateBlockDesc& stateGlow, GFXStateBlockDesc& stateReflect);
  233. /// Does the base render state block setting, normally per pass.
  234. virtual void _initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc& result);
  235. /// Creates the default state blocks for a list of render states.
  236. virtual void _initRenderStateStateBlocks( RenderPassData *rpd );
  237. /// Creates the default state blocks for each RenderPassData item.
  238. virtual void _initRenderPassDataStateBlocks();
  239. /// This returns the index into the renderState array based on the sgData passed in.
  240. virtual U32 _getRenderStateIndex( const SceneRenderState *state,
  241. const SceneData &sgData );
  242. /// Activates the correct mPasses[currPass].renderState based on scene graph info
  243. virtual void _setRenderState( const SceneRenderState *state,
  244. const SceneData &sgData,
  245. U32 pass );
  246. /// @
  247. };
  248. #endif // _MATERIALS_PROCESSEDMATERIAL_H_