processedMaterial.h 10 KB

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