baseMatInstance.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 _BASEMATINSTANCE_H_
  23. #define _BASEMATINSTANCE_H_
  24. #ifndef _TSIGNAL_H_
  25. #include "core/util/tSignal.h"
  26. #endif
  27. #ifndef _BASEMATERIALDEFINITION_H_
  28. #include "materials/baseMaterialDefinition.h"
  29. #endif
  30. #ifndef _MATERIALPARAMETERS_H_
  31. #include "materials/materialParameters.h"
  32. #endif
  33. #ifndef _MMATRIX_H_
  34. #include "math/mMatrix.h"
  35. #endif
  36. #ifndef _GFXENUMS_H_
  37. #include "gfx/gfxEnums.h"
  38. #endif
  39. #ifndef _GFXSHADER_H_
  40. #include "gfx/gfxShader.h"
  41. #endif
  42. #ifndef _MATERIALFEATUREDATA_H_
  43. #include "materials/materialFeatureData.h"
  44. #endif
  45. #ifndef _MATINSTANCEHOOK_H_
  46. #include "materials/matInstanceHook.h"
  47. #endif
  48. #ifndef _MATSTATEHINT_H_
  49. #include "materials/matStateHint.h"
  50. #endif
  51. struct RenderPassData;
  52. class GFXVertexBufferHandleBase;
  53. class GFXPrimitiveBufferHandle;
  54. struct SceneData;
  55. class SceneRenderState;
  56. struct GFXStateBlockDesc;
  57. class GFXVertexFormat;
  58. class MatrixSet;
  59. class ProcessedMaterial;
  60. ///
  61. class BaseMatInstance
  62. {
  63. protected:
  64. /// The array of active material hooks indexed
  65. /// by a MatInstanceHookType.
  66. Vector<MatInstanceHook*> mHooks;
  67. ///
  68. MatFeaturesDelegate mFeaturesDelegate;
  69. /// Should be true if init has been called and it succeeded.
  70. /// It is up to the derived class to set this variable appropriately.
  71. bool mIsValid;
  72. /// This is set by initialization and used by the prepass.
  73. bool mHasNormalMaps;
  74. /// This material makes use of bone transforms
  75. bool mUsesHardwareSkinning;
  76. public:
  77. virtual ~BaseMatInstance();
  78. /// @param features The features you want to allow for this material.
  79. ///
  80. /// @param vertexFormat The vertex format on which this material will be rendered.
  81. ///
  82. /// @see GFXVertexFormat
  83. /// @see FeatureSet
  84. virtual bool init( const FeatureSet &features,
  85. const GFXVertexFormat *vertexFormat ) = 0;
  86. /// Reinitializes the material using the previous
  87. /// initialization parameters.
  88. /// @see init
  89. virtual bool reInit() = 0;
  90. /// Returns true if init has been successfully called.
  91. /// It is up to the derived class to set this value properly.
  92. bool isValid() { return mIsValid; }
  93. /// Adds this stateblock to the base state block
  94. /// used during initialization.
  95. /// @see init
  96. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc) = 0;
  97. /// Updates the state blocks for this material.
  98. virtual void updateStateBlocks() = 0;
  99. /// Adds a shader macro which will be passed to the shader
  100. /// during initialization.
  101. /// @see init
  102. virtual void addShaderMacro( const String &name, const String &value ) = 0;
  103. /// Get a MaterialParameters block for this BaseMatInstance,
  104. /// caller is responsible for freeing it.
  105. virtual MaterialParameters* allocMaterialParameters() = 0;
  106. /// Set the current parameters for this BaseMatInstance
  107. virtual void setMaterialParameters(MaterialParameters* param) = 0;
  108. /// Get the current parameters for this BaseMatInstance (BaseMatInstances are created with a default active
  109. /// MaterialParameters which is managed by BaseMatInstance.
  110. virtual MaterialParameters* getMaterialParameters() = 0;
  111. /// Returns a MaterialParameterHandle for name.
  112. virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0;
  113. /// Sets up the next rendering pass for this material. It is
  114. /// typically called like so...
  115. ///
  116. ///@code
  117. /// while( mat->setupPass( state, sgData ) )
  118. /// {
  119. /// mat->setTransforms(...);
  120. /// mat->setSceneInfo(...);
  121. /// ...
  122. /// GFX->drawPrimitive();
  123. /// }
  124. ///@endcode
  125. ///
  126. virtual bool setupPass( SceneRenderState *state, const SceneData &sgData ) = 0;
  127. /// This initializes the material transforms and should be
  128. /// called after setupPass() within the pass loop.
  129. /// @see setupPass
  130. virtual void setTransforms( const MatrixSet &matrixSet, SceneRenderState *state ) = 0;
  131. /// Sets node transforms for the current stage. Used for hardware skinning.
  132. virtual void setNodeTransforms( const MatrixF *address, const U32 numTransforms ) = 0;
  133. /// This initializes various material scene state settings and
  134. /// should be called after setupPass() within the pass loop.
  135. /// @see setupPass
  136. virtual void setSceneInfo( SceneRenderState *state, const SceneData &sgData ) = 0;
  137. /// This is normally called from within setupPass() automatically, so its
  138. /// unnecessary to do so manually unless a texture stage has changed. If
  139. /// so it should be called after setupPass() within the pass loop.
  140. /// @see setupPass
  141. virtual void setTextureStages(SceneRenderState *, const SceneData &sgData ) = 0;
  142. /// Sets the vertex and primitive buffers as well as the instancing
  143. /// stream buffer for the current material if the material is instanced.
  144. virtual void setBuffers( GFXVertexBufferHandleBase *vertBuffer, GFXPrimitiveBufferHandle *primBuffer ) = 0;
  145. /// Returns true if this material is instanced.
  146. virtual bool isInstanced() const = 0;
  147. /// Used to increment the instance buffer for this material.
  148. virtual bool stepInstance() = 0;
  149. /// Returns true if the material is forward lit and requires
  150. /// a list of lights which affect it when rendering.
  151. virtual bool isForwardLit() const = 0;
  152. /// Sets a SimObject which will passed into ShaderFeature::createConstHandles.
  153. /// Normal features do not make use of this, it is for special class specific
  154. /// or user designed features.
  155. virtual void setUserObject( SimObject *userObject ) = 0;
  156. virtual SimObject* getUserObject() const = 0;
  157. /// Returns the material this instance is based on.
  158. virtual BaseMaterialDefinition* getMaterial() = 0;
  159. // BTRTODO: This stuff below should probably not be in BaseMatInstance
  160. virtual bool hasGlow() = 0;
  161. virtual bool hasAccumulation() = 0;
  162. virtual U32 getCurPass() = 0;
  163. virtual U32 getCurStageNum() = 0;
  164. virtual RenderPassData *getPass(U32 pass) = 0;
  165. /// Returns the state hint which can be used for
  166. /// sorting and fast comparisions of the equality
  167. /// of a material instance.
  168. virtual const MatStateHint& getStateHint() const = 0;
  169. /// Returns the active features in use by this material.
  170. /// @see getRequestedFeatures
  171. virtual const FeatureSet& getFeatures() const = 0;
  172. /// Returns the features that were requested at material
  173. /// creation time which may differ from the active features.
  174. /// @see getFeatures
  175. virtual const FeatureSet& getRequestedFeatures() const = 0;
  176. virtual const GFXVertexFormat* getVertexFormat() const = 0;
  177. virtual void dumpShaderInfo() const = 0;
  178. /// Fast test for use of normal maps in this material.
  179. bool hasNormalMap() const { return mHasNormalMaps; }
  180. bool usesHardwareSkinning() const { return mUsesHardwareSkinning; }
  181. ///
  182. MatFeaturesDelegate& getFeaturesDelegate() { return mFeaturesDelegate; }
  183. /// Returns true if this MatInstance is built from a CustomMaterial.
  184. virtual bool isCustomMaterial() const = 0;
  185. /// @name Material Hook functions
  186. /// @{
  187. ///
  188. void addHook( MatInstanceHook *hook );
  189. /// Helper function for getting a hook.
  190. /// @see getHook
  191. template <class HOOK>
  192. inline HOOK* getHook() { return (HOOK*)getHook( HOOK::Type ); }
  193. ///
  194. MatInstanceHook* getHook( const MatInstanceHookType &type ) const;
  195. ///
  196. void deleteHook( const MatInstanceHookType &type );
  197. ///
  198. U32 deleteAllHooks();
  199. /// @}
  200. virtual const GFXStateBlockDesc &getUserStateBlock() const = 0;
  201. };
  202. #endif /// _BASEMATINSTANCE_H_