processedMaterial.h 11 KB

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