matInstance.h 7.4 KB

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