processedMaterial.h 10 KB

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