processedMaterial.h 11 KB

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