matInstance.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 U32 getCurPass() { return getMax( mCurPass, 0 ); }
  74. virtual U32 getCurStageNum();
  75. virtual RenderPassData *getPass(U32 pass);
  76. virtual const MatStateHint& getStateHint() const;
  77. virtual const GFXVertexFormat* getVertexFormat() const { return mVertexFormat; }
  78. virtual const FeatureSet& getFeatures() const;
  79. virtual const FeatureSet& getRequestedFeatures() const { return mFeatureList; }
  80. virtual void dumpShaderInfo() const;
  81. ProcessedMaterial *getProcessedMaterial() const { return mProcessedMaterial; }
  82. virtual const GFXStateBlockDesc &getUserStateBlock() const { return mUserDefinedState; }
  83. virtual bool isCustomMaterial() const { return mCreatedFromCustomMaterial; }
  84. protected:
  85. friend class Material;
  86. /// Create a material instance by reference to a Material.
  87. MatInstance( Material &mat );
  88. virtual bool processMaterial();
  89. virtual ProcessedMaterial* getShaderMaterial();
  90. Material* mMaterial;
  91. ProcessedMaterial* mProcessedMaterial;
  92. /// The features requested at material creation time.
  93. FeatureSet mFeatureList;
  94. /// The vertex format on which this material will render.
  95. const GFXVertexFormat *mVertexFormat;
  96. /// If the processed material requires forward lighting or not.
  97. bool mIsForwardLit;
  98. S32 mCurPass;
  99. U32 mMaxStages;
  100. GFXStateBlockDesc mUserDefinedState;
  101. Vector<GFXShaderMacro> mUserMacros;
  102. SimObject *mUserObject;
  103. Vector<MatInstanceParameterHandle*> mCurrentHandles;
  104. Vector<MatInstParameters*> mCurrentParameters;
  105. MatInstParameters* mActiveParameters;
  106. MatInstParameters* mDefaultParameters;
  107. bool mCreatedFromCustomMaterial;
  108. private:
  109. void construct();
  110. };
  111. //
  112. // MatInstParameters
  113. //
  114. class MatInstParameters : public MaterialParameters
  115. {
  116. public:
  117. MatInstParameters();
  118. MatInstParameters(MaterialParameters* matParams);
  119. virtual ~MatInstParameters();
  120. void loadParameters(ProcessedMaterial* pmat);
  121. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  122. virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
  123. /// @name Set shader constant values
  124. /// @{
  125. /// Actually set shader constant values
  126. /// @param name Name of the constant, this should be a name contained in the array returned in getShaderConstDesc,
  127. /// if an invalid name is used, it is ignored.
  128. virtual void set(MaterialParameterHandle* handle, const F32 f);
  129. virtual void set(MaterialParameterHandle* handle, const Point2F& fv);
  130. virtual void set(MaterialParameterHandle* handle, const Point3F& fv);
  131. virtual void set(MaterialParameterHandle* handle, const Point4F& fv);
  132. virtual void set(MaterialParameterHandle* handle, const ColorF& fv);
  133. virtual void set(MaterialParameterHandle* handle, const S32 f);
  134. virtual void set(MaterialParameterHandle* handle, const Point2I& fv);
  135. virtual void set(MaterialParameterHandle* handle, const Point3I& fv);
  136. virtual void set(MaterialParameterHandle* handle, const Point4I& fv);
  137. virtual void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv);
  138. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv);
  139. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv);
  140. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv);
  141. virtual void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv);
  142. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv);
  143. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv);
  144. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv);
  145. virtual void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
  146. virtual void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
  147. virtual U32 getAlignmentValue(const GFXShaderConstType constType);
  148. private:
  149. MaterialParameters* mParameters;
  150. bool mOwnParameters;
  151. };
  152. #endif // _MATINSTANCE_H_