baseMatInstance.h 9.2 KB

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