matInstance.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 _MATINSTANCE_H_
  23. #define _MATINSTANCE_H_
  24. #ifndef _MATERIALDEFINITION_H_
  25. #include "materials/materialDefinition.h"
  26. #endif
  27. #ifndef _BASEMATINSTANCE_H_
  28. #include "materials/baseMatInstance.h"
  29. #endif
  30. #ifndef _SCENEDATA_H_
  31. #include "materials/sceneData.h"
  32. #endif
  33. #ifndef _GFXSTATEBLOCK_H_
  34. #include "gfx/gfxStateBlock.h"
  35. #endif
  36. #ifndef _FEATURESET_H_
  37. #include "shaderGen/featureSet.h"
  38. #endif
  39. class GFXShader;
  40. class GFXCubemap;
  41. class ShaderFeature;
  42. class MatInstanceParameterHandle;
  43. class MatInstParameters;
  44. class ProcessedMaterial;
  45. class GuiTreeViewCtrl;
  46. ///
  47. class MatInstance : public BaseMatInstance
  48. {
  49. public:
  50. virtual ~MatInstance();
  51. // BaseMatInstance
  52. virtual bool init( const FeatureSet &features,
  53. const GFXVertexFormat *vertexFormat );
  54. virtual bool reInit();
  55. virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
  56. virtual void updateStateBlocks();
  57. virtual void addShaderMacro( const String &name, const String &value );
  58. virtual MaterialParameters* allocMaterialParameters();
  59. virtual void setMaterialParameters(MaterialParameters* param);
  60. virtual MaterialParameters* getMaterialParameters();
  61. virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name);
  62. virtual bool setupPass(SceneRenderState *, const SceneData &sgData );
  63. virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state);
  64. virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms);
  65. virtual void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData);
  66. virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData);
  67. virtual void setTextureStages(SceneRenderState * state, const SceneData &sgData );
  68. virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer);
  69. virtual bool isInstanced() const;
  70. virtual bool stepInstance();
  71. virtual bool isForwardLit() const { return mIsForwardLit; }
  72. virtual bool isHardwareSkinned() const { return mIsHardwareSkinned; }
  73. virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; }
  74. virtual SimObject* getUserObject() const { return mUserObject; }
  75. virtual Material *getMaterial() { return mMaterial; }
  76. virtual bool hasGlow();
  77. virtual bool hasAccumulation();
  78. virtual U32 getCurPass() { return getMax( mCurPass, 0 ); }
  79. virtual U32 getCurStageNum();
  80. virtual RenderPassData *getPass(U32 pass);
  81. virtual const MatStateHint& getStateHint() const;
  82. virtual const GFXVertexFormat* getVertexFormat() const { return mVertexFormat; }
  83. virtual const FeatureSet& getFeatures() const;
  84. virtual const FeatureSet& getRequestedFeatures() const { return mFeatureList; }
  85. virtual void dumpShaderInfo() const;
  86. virtual void getShaderInfo(GuiTreeViewCtrl* tree, U32 item) const;
  87. ProcessedMaterial *getProcessedMaterial() const { return mProcessedMaterial; }
  88. virtual const GFXStateBlockDesc &getUserStateBlock() const { return mUserDefinedState; }
  89. virtual bool isCustomMaterial() const { return mCreatedFromCustomMaterial; }
  90. protected:
  91. friend class Material;
  92. /// Create a material instance by reference to a Material.
  93. MatInstance( Material &mat );
  94. virtual bool processMaterial();
  95. virtual ProcessedMaterial* getShaderMaterial();
  96. Material* mMaterial;
  97. ProcessedMaterial* mProcessedMaterial;
  98. /// The features requested at material creation time.
  99. FeatureSet mFeatureList;
  100. /// The vertex format on which this material will render.
  101. const GFXVertexFormat *mVertexFormat;
  102. /// If the processed material requires forward lighting or not.
  103. bool mIsForwardLit;
  104. /// If the processed material requires bone transforms
  105. bool mIsHardwareSkinned;
  106. S32 mCurPass;
  107. U32 mMaxStages;
  108. GFXStateBlockDesc mUserDefinedState;
  109. Vector<GFXShaderMacro> mUserMacros;
  110. SimObject *mUserObject;
  111. Vector<MatInstanceParameterHandle*> mCurrentHandles;
  112. Vector<MatInstParameters*> mCurrentParameters;
  113. MatInstParameters* mActiveParameters;
  114. MatInstParameters* mDefaultParameters;
  115. bool mCreatedFromCustomMaterial;
  116. private:
  117. void construct();
  118. };
  119. //
  120. // MatInstParameters
  121. //
  122. class MatInstParameters : public MaterialParameters
  123. {
  124. public:
  125. MatInstParameters();
  126. MatInstParameters(MaterialParameters* matParams);
  127. virtual ~MatInstParameters();
  128. void loadParameters(ProcessedMaterial* pmat);
  129. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  130. virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
  131. /// @name Set shader constant values
  132. /// @{
  133. /// Actually set shader constant values
  134. /// @param name Name of the constant, this should be a name contained in the array returned in getShaderConstDesc,
  135. /// if an invalid name is used, it is ignored.
  136. virtual void set(MaterialParameterHandle* handle, const F32 f);
  137. virtual void set(MaterialParameterHandle* handle, const Point2F& fv);
  138. virtual void set(MaterialParameterHandle* handle, const Point3F& fv);
  139. virtual void set(MaterialParameterHandle* handle, const Point4F& fv);
  140. virtual void set(MaterialParameterHandle* handle, const LinearColorF& fv);
  141. virtual void set(MaterialParameterHandle* handle, const S32 f);
  142. virtual void set(MaterialParameterHandle* handle, const Point2I& fv);
  143. virtual void set(MaterialParameterHandle* handle, const Point3I& fv);
  144. virtual void set(MaterialParameterHandle* handle, const Point4I& fv);
  145. virtual void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv);
  146. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv);
  147. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv);
  148. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv);
  149. virtual void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv);
  150. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv);
  151. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv);
  152. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv);
  153. virtual void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
  154. virtual void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
  155. virtual U32 getAlignmentValue(const GFXShaderConstType constType);
  156. private:
  157. MaterialParameters* mParameters;
  158. bool mOwnParameters;
  159. };
  160. #endif // _MATINSTANCE_H_