matInstance.h

Engine/source/materials/matInstance.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23#ifndef _MATINSTANCE_H_
 24#define _MATINSTANCE_H_
 25
 26#ifndef _MATERIALDEFINITION_H_
 27#include "materials/materialDefinition.h"
 28#endif
 29#ifndef _BASEMATINSTANCE_H_
 30#include "materials/baseMatInstance.h"
 31#endif
 32#ifndef _SCENEDATA_H_
 33#include "materials/sceneData.h"
 34#endif
 35#ifndef _GFXSTATEBLOCK_H_
 36#include "gfx/gfxStateBlock.h"
 37#endif
 38#ifndef _FEATURESET_H_
 39#include "shaderGen/featureSet.h"
 40#endif
 41
 42class GFXShader;
 43class GFXCubemap;
 44class ShaderFeature;
 45class MatInstanceParameterHandle;
 46class MatInstParameters;
 47class ProcessedMaterial;
 48class GuiTreeViewCtrl;
 49
 50///
 51class MatInstance : public BaseMatInstance
 52{   
 53public:
 54   virtual ~MatInstance();
 55
 56   // BaseMatInstance
 57   virtual bool init(   const FeatureSet &features, 
 58                        const GFXVertexFormat *vertexFormat );
 59   virtual bool reInit();
 60   virtual void addStateBlockDesc(const GFXStateBlockDesc& desc);
 61   virtual void updateStateBlocks();
 62   virtual void addShaderMacro( const String &name, const String &value );
 63   virtual MaterialParameters* allocMaterialParameters();
 64   virtual void setMaterialParameters(MaterialParameters* param); 
 65   virtual MaterialParameters* getMaterialParameters();
 66   virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name);
 67   virtual bool setupPass(SceneRenderState *, const SceneData &sgData );
 68   virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state);
 69   virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms);
 70   virtual void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData);
 71   virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData);
 72   virtual void setTextureStages(SceneRenderState * state, const SceneData &sgData );
 73   virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer);
 74   virtual bool isInstanced() const;
 75   virtual bool stepInstance();
 76   virtual bool isForwardLit() const { return mIsForwardLit; }
 77   virtual bool isHardwareSkinned() const { return mIsHardwareSkinned; }
 78   virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; }
 79   virtual SimObject* getUserObject() const { return mUserObject; }
 80   virtual Material *getMaterial() { return mMaterial; }
 81   virtual bool hasGlow();
 82   virtual bool hasAccumulation();
 83   virtual U32 getCurPass() { return getMax( mCurPass, 0 ); }
 84   virtual U32 getCurStageNum();
 85   virtual RenderPassData *getPass(U32 pass);   
 86   virtual const MatStateHint& getStateHint() const;
 87   virtual const GFXVertexFormat* getVertexFormat() const { return mVertexFormat; }
 88   virtual const FeatureSet& getFeatures() const;
 89   virtual const FeatureSet& getRequestedFeatures() const { return mFeatureList; }
 90   virtual void dumpShaderInfo() const;
 91   virtual void getShaderInfo(GuiTreeViewCtrl* tree, U32 item) const;
 92   
 93
 94   ProcessedMaterial *getProcessedMaterial() const { return mProcessedMaterial; }
 95
 96   virtual const GFXStateBlockDesc &getUserStateBlock() const { return mUserDefinedState; }
 97
 98   virtual bool isCustomMaterial() const { return mCreatedFromCustomMaterial; }
 99protected:
100
101   friend class Material;
102
103   /// Create a material instance by reference to a Material.
104   MatInstance( Material &mat );
105
106   virtual bool processMaterial();
107   virtual ProcessedMaterial* getShaderMaterial();
108
109   Material* mMaterial;
110   ProcessedMaterial* mProcessedMaterial;
111
112   /// The features requested at material creation time.
113   FeatureSet mFeatureList;
114
115   /// The vertex format on which this material will render.
116   const GFXVertexFormat *mVertexFormat;
117
118   /// If the processed material requires forward lighting or not.
119   bool mIsForwardLit;
120
121   /// If the processed material requires bone transforms
122   bool mIsHardwareSkinned;
123
124   S32               mCurPass;
125   U32               mMaxStages;
126
127   GFXStateBlockDesc mUserDefinedState;
128
129   Vector<GFXShaderMacro> mUserMacros;
130
131   SimObject *mUserObject;
132   
133   Vector<MatInstanceParameterHandle*> mCurrentHandles;
134   Vector<MatInstParameters*> mCurrentParameters;
135   MatInstParameters* mActiveParameters;
136   MatInstParameters* mDefaultParameters;
137   
138   bool mCreatedFromCustomMaterial;
139private:
140   void construct();  
141};
142
143//
144// MatInstParameters 
145//
146class MatInstParameters : public MaterialParameters
147{
148public:
149   MatInstParameters();
150   MatInstParameters(MaterialParameters* matParams);
151   virtual ~MatInstParameters();
152   
153   void loadParameters(ProcessedMaterial* pmat);
154
155   /// Returns our list of shader constants, the material can get this and just set the constants it knows about
156   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
157
158   /// @name Set shader constant values
159   /// @{
160   /// Actually set shader constant values
161   /// @param name Name of the constant, this should be a name contained in the array returned in getShaderConstDesc,
162   /// if an invalid name is used, it is ignored.
163   virtual void set(MaterialParameterHandle* handle, const F32 f);
164   virtual void set(MaterialParameterHandle* handle, const Point2F& fv);
165   virtual void set(MaterialParameterHandle* handle, const Point3F& fv);
166   virtual void set(MaterialParameterHandle* handle, const Point4F& fv);
167   virtual void set(MaterialParameterHandle* handle, const LinearColorF& fv);
168   virtual void set(MaterialParameterHandle* handle, const S32 f);
169   virtual void set(MaterialParameterHandle* handle, const Point2I& fv);
170   virtual void set(MaterialParameterHandle* handle, const Point3I& fv);
171   virtual void set(MaterialParameterHandle* handle, const Point4I& fv);
172   virtual void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv);
173   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv);
174   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv);
175   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv);
176   virtual void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv);
177   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv);
178   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv);
179   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv);
180   virtual void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
181   virtual void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
182   virtual U32 getAlignmentValue(const GFXShaderConstType constType);
183private:
184   MaterialParameters* mParameters;
185   bool mOwnParameters;
186};
187
188
189#endif // _MATINSTANCE_H_
190