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. bool init( const FeatureSet &features,
  53. const GFXVertexFormat *vertexFormat ) override;
  54. bool reInit() override;
  55. void addStateBlockDesc(const GFXStateBlockDesc& desc) override;
  56. void updateStateBlocks() override;
  57. void addShaderMacro( const String &name, const String &value ) override;
  58. MaterialParameters* allocMaterialParameters() override;
  59. void setMaterialParameters(MaterialParameters* param) override;
  60. MaterialParameters* getMaterialParameters() override;
  61. MaterialParameterHandle* getMaterialParameterHandle(const String& name) override;
  62. bool setupPass(SceneRenderState *, const SceneData &sgData ) override;
  63. void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state) override;
  64. void setNodeTransforms(const MatrixF *address, const U32 numTransforms) override;
  65. void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData) override;
  66. void setSceneInfo(SceneRenderState *, const SceneData& sgData) override;
  67. void setTextureStages(SceneRenderState * state, const SceneData &sgData ) override;
  68. void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer) override;
  69. bool isInstanced() const override;
  70. bool stepInstance() override;
  71. bool isForwardLit() const override { return mIsForwardLit; }
  72. virtual bool isHardwareSkinned() const { return mIsHardwareSkinned; }
  73. void setUserObject( SimObject *userObject ) override { mUserObject = userObject; }
  74. SimObject* getUserObject() const override { return mUserObject; }
  75. Material *getMaterial() override { return mMaterial; }
  76. bool hasGlow() override;
  77. bool hasAccumulation() override;
  78. U32 getCurPass() override { return getMax( mCurPass, 0 ); }
  79. U32 getCurStageNum() override;
  80. RenderPassData *getPass(U32 pass) override;
  81. const MatStateHint& getStateHint() const override;
  82. const GFXVertexFormat* getVertexFormat() const override { return mVertexFormat; }
  83. const FeatureSet& getFeatures() const override;
  84. const FeatureSet& getRequestedFeatures() const override { return mFeatureList; }
  85. void dumpShaderInfo() const override;
  86. void getShaderInfo(GuiTreeViewCtrl* tree, U32 item) const override;
  87. ProcessedMaterial *getProcessedMaterial() const { return mProcessedMaterial; }
  88. const GFXStateBlockDesc &getUserStateBlock() const override { return mUserDefinedState; }
  89. bool isCustomMaterial() const override { 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. const Vector<GFXShaderConstDesc>& getShaderConstDesc() const override;
  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. void set(MaterialParameterHandle* handle, const F32 f) override;
  137. void set(MaterialParameterHandle* handle, const Point2F& fv) override;
  138. void set(MaterialParameterHandle* handle, const Point3F& fv) override;
  139. void set(MaterialParameterHandle* handle, const Point4F& fv) override;
  140. void set(MaterialParameterHandle* handle, const LinearColorF& fv) override;
  141. void set(MaterialParameterHandle* handle, const S32 f) override;
  142. void set(MaterialParameterHandle* handle, const Point2I& fv) override;
  143. void set(MaterialParameterHandle* handle, const Point3I& fv) override;
  144. void set(MaterialParameterHandle* handle, const Point4I& fv) override;
  145. void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv) override;
  146. void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv) override;
  147. void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv) override;
  148. void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv) override;
  149. void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv) override;
  150. void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv) override;
  151. void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv) override;
  152. void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv) override;
  153. void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4) override;
  154. void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4) override;
  155. U32 getAlignmentValue(const GFXShaderConstType constType) override;
  156. private:
  157. MaterialParameters* mParameters;
  158. bool mOwnParameters;
  159. };
  160. #endif // _MATINSTANCE_H_