baseMatInstance.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. public:
  75. virtual ~BaseMatInstance();
  76. /// @param features The features you want to allow for this material.
  77. ///
  78. /// @param vertexFormat The vertex format on which this material will be rendered.
  79. ///
  80. /// @see GFXVertexFormat
  81. /// @see FeatureSet
  82. virtual bool init( const FeatureSet &features,
  83. const GFXVertexFormat *vertexFormat ) = 0;
  84. /// Reinitializes the material using the previous
  85. /// initialization parameters.
  86. /// @see init
  87. virtual bool reInit() = 0;
  88. /// Returns true if init has been successfully called.
  89. /// It is up to the derived class to set this value properly.
  90. bool isValid() { return mIsValid; }
  91. /// Adds this stateblock to the base state block
  92. /// used during initialization.
  93. /// @see init
  94. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc) = 0;
  95. /// Updates the state blocks for this material.
  96. virtual void updateStateBlocks() = 0;
  97. /// Adds a shader macro which will be passed to the shader
  98. /// during initialization.
  99. /// @see init
  100. virtual void addShaderMacro( const String &name, const String &value ) = 0;
  101. /// Get a MaterialParameters block for this BaseMatInstance,
  102. /// caller is responsible for freeing it.
  103. virtual MaterialParameters* allocMaterialParameters() = 0;
  104. /// Set the current parameters for this BaseMatInstance
  105. virtual void setMaterialParameters(MaterialParameters* param) = 0;
  106. /// Get the current parameters for this BaseMatInstance (BaseMatInstances are created with a default active
  107. /// MaterialParameters which is managed by BaseMatInstance.
  108. virtual MaterialParameters* getMaterialParameters() = 0;
  109. /// Returns a MaterialParameterHandle for name.
  110. virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0;
  111. /// Sets up the next rendering pass for this material. It is
  112. /// typically called like so...
  113. ///
  114. ///@code
  115. /// while( mat->setupPass( state, sgData ) )
  116. /// {
  117. /// mat->setTransforms(...);
  118. /// mat->setSceneInfo(...);
  119. /// ...
  120. /// GFX->drawPrimitive();
  121. /// }
  122. ///@endcode
  123. ///
  124. virtual bool setupPass( SceneRenderState *state, const SceneData &sgData ) = 0;
  125. /// This initializes the material transforms and should be
  126. /// called after setupPass() within the pass loop.
  127. /// @see setupPass
  128. virtual void setTransforms( const MatrixSet &matrixSet, SceneRenderState *state ) = 0;
  129. /// This initializes various material scene state settings and
  130. /// should be called after setupPass() within the pass loop.
  131. /// @see setupPass
  132. virtual void setSceneInfo( SceneRenderState *state, const SceneData &sgData ) = 0;
  133. /// This is normally called from within setupPass() automatically, so its
  134. /// unnecessary to do so manually unless a texture stage has changed. If
  135. /// so it should be called after setupPass() within the pass loop.
  136. /// @see setupPass
  137. virtual void setTextureStages(SceneRenderState *, const SceneData &sgData ) = 0;
  138. /// Sets the vertex and primitive buffers as well as the instancing
  139. /// stream buffer for the current material if the material is instanced.
  140. virtual void setBuffers( GFXVertexBufferHandleBase *vertBuffer, GFXPrimitiveBufferHandle *primBuffer ) = 0;
  141. /// Returns true if this material is instanced.
  142. virtual bool isInstanced() const = 0;
  143. /// Used to increment the instance buffer for this material.
  144. virtual bool stepInstance() = 0;
  145. /// Returns true if the material is forward lit and requires
  146. /// a list of lights which affect it when rendering.
  147. virtual bool isForwardLit() const = 0;
  148. /// Sets a SimObject which will passed into ShaderFeature::createConstHandles.
  149. /// Normal features do not make use of this, it is for special class specific
  150. /// or user designed features.
  151. virtual void setUserObject( SimObject *userObject ) = 0;
  152. virtual SimObject* getUserObject() const = 0;
  153. /// Returns the material this instance is based on.
  154. virtual BaseMaterialDefinition* getMaterial() = 0;
  155. // BTRTODO: This stuff below should probably not be in BaseMatInstance
  156. virtual bool hasGlow() = 0;
  157. virtual bool hasAccumulation() = 0;
  158. virtual U32 getCurPass() = 0;
  159. virtual U32 getCurStageNum() = 0;
  160. virtual RenderPassData *getPass(U32 pass) = 0;
  161. /// Returns the state hint which can be used for
  162. /// sorting and fast comparisions of the equality
  163. /// of a material instance.
  164. virtual const MatStateHint& getStateHint() const = 0;
  165. /// Returns the active features in use by this material.
  166. /// @see getRequestedFeatures
  167. virtual const FeatureSet& getFeatures() const = 0;
  168. /// Returns the features that were requested at material
  169. /// creation time which may differ from the active features.
  170. /// @see getFeatures
  171. virtual const FeatureSet& getRequestedFeatures() const = 0;
  172. virtual const GFXVertexFormat* getVertexFormat() const = 0;
  173. virtual void dumpShaderInfo() const = 0;
  174. /// Fast test for use of normal maps in this material.
  175. bool hasNormalMap() const { return mHasNormalMaps; }
  176. ///
  177. MatFeaturesDelegate& getFeaturesDelegate() { return mFeaturesDelegate; }
  178. /// Returns true if this MatInstance is built from a CustomMaterial.
  179. virtual bool isCustomMaterial() const = 0;
  180. /// @name Material Hook functions
  181. /// @{
  182. ///
  183. void addHook( MatInstanceHook *hook );
  184. /// Helper function for getting a hook.
  185. /// @see getHook
  186. template <class HOOK>
  187. inline HOOK* getHook() { return (HOOK*)getHook( HOOK::Type ); }
  188. ///
  189. MatInstanceHook* getHook( const MatInstanceHookType &type ) const;
  190. ///
  191. void deleteHook( const MatInstanceHookType &type );
  192. ///
  193. U32 deleteAllHooks();
  194. /// @}
  195. virtual const GFXStateBlockDesc &getUserStateBlock() const = 0;
  196. };
  197. #endif /// _BASEMATINSTANCE_H_