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;
 48
 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 setSceneInfo(SceneRenderState *, const SceneData& sgData);
 71   virtual void setTextureStages(SceneRenderState * state, const SceneData &sgData );
 72   virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer);
 73   virtual bool isInstanced() const;
 74   virtual bool stepInstance();
 75   virtual bool isForwardLit() const { return mIsForwardLit; }
 76   virtual bool isHardwareSkinned() const { return mIsHardwareSkinned; }
 77   virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; }
 78   virtual SimObject* getUserObject() const { return mUserObject; }
 79   virtual Material *getMaterial() { return mMaterial; }
 80   virtual bool hasGlow();
 81   virtual bool hasAccumulation();
 82   virtual U32 getCurPass() { return getMax( mCurPass, 0 ); }
 83   virtual U32 getCurStageNum();
 84   virtual RenderPassData *getPass(U32 pass);   
 85   virtual const MatStateHint& getStateHint() const;
 86   virtual const GFXVertexFormat* getVertexFormat() const { return mVertexFormat; }
 87   virtual const FeatureSet& getFeatures() const;
 88   virtual const FeatureSet& getRequestedFeatures() const { return mFeatureList; }
 89   virtual void dumpShaderInfo() const;
 90   
 91
 92   ProcessedMaterial *getProcessedMaterial() const { return mProcessedMaterial; }
 93
 94   virtual const GFXStateBlockDesc &getUserStateBlock() const { return mUserDefinedState; }
 95
 96   virtual bool isCustomMaterial() const { return mCreatedFromCustomMaterial; }
 97protected:
 98
 99   friend class Material;
100
101   /// Create a material instance by reference to a Material.
102   MatInstance( Material &mat );
103
104   virtual bool processMaterial();
105   virtual ProcessedMaterial* getShaderMaterial();
106
107   Material* mMaterial;
108   ProcessedMaterial* mProcessedMaterial;
109
110   /// The features requested at material creation time.
111   FeatureSet mFeatureList;
112
113   /// The vertex format on which this material will render.
114   const GFXVertexFormat *mVertexFormat;
115
116   /// If the processed material requires forward lighting or not.
117   bool mIsForwardLit;
118
119   /// If the processed material requires bone transforms
120   bool mIsHardwareSkinned;
121
122   S32               mCurPass;
123   U32               mMaxStages;
124
125   GFXStateBlockDesc mUserDefinedState;
126
127   Vector<GFXShaderMacro> mUserMacros;
128
129   SimObject *mUserObject;
130   
131   Vector<MatInstanceParameterHandle*> mCurrentHandles;
132   Vector<MatInstParameters*> mCurrentParameters;
133   MatInstParameters* mActiveParameters;
134   MatInstParameters* mDefaultParameters;
135   
136   bool mCreatedFromCustomMaterial;
137private:
138   void construct();  
139};
140
141//
142// MatInstParameters 
143//
144class MatInstParameters : public MaterialParameters
145{
146public:
147   MatInstParameters();
148   MatInstParameters(MaterialParameters* matParams);
149   virtual ~MatInstParameters();
150   
151   void loadParameters(ProcessedMaterial* pmat);
152
153   /// Returns our list of shader constants, the material can get this and just set the constants it knows about
154   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
155
156   /// @name Set shader constant values
157   /// @{
158   /// Actually set shader constant values
159   /// @param name Name of the constant, this should be a name contained in the array returned in getShaderConstDesc,
160   /// if an invalid name is used, it is ignored.
161   virtual void set(MaterialParameterHandle* handle, const F32 f);
162   virtual void set(MaterialParameterHandle* handle, const Point2F& fv);
163   virtual void set(MaterialParameterHandle* handle, const Point3F& fv);
164   virtual void set(MaterialParameterHandle* handle, const Point4F& fv);
165   virtual void set(MaterialParameterHandle* handle, const ColorF& fv);
166   virtual void set(MaterialParameterHandle* handle, const S32 f);
167   virtual void set(MaterialParameterHandle* handle, const Point2I& fv);
168   virtual void set(MaterialParameterHandle* handle, const Point3I& fv);
169   virtual void set(MaterialParameterHandle* handle, const Point4I& fv);
170   virtual void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv);
171   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv);
172   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv);
173   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv);
174   virtual void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv);
175   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv);
176   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv);
177   virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv);
178   virtual void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
179   virtual void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4);
180   virtual U32 getAlignmentValue(const GFXShaderConstType constType);
181private:
182   MaterialParameters* mParameters;
183   bool mOwnParameters;
184};
185
186
187#endif // _MATINSTANCE_H_
188