processedMaterial.h 10 KB

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