Torque3D Documentation / _generateds / processedMaterial.h

processedMaterial.h

Engine/source/materials/processedMaterial.h

More...

Classes:

class

This is an abstract base class which provides the external interface all subclasses must implement.

class

This contains the common data needed to render a pass.

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
 24#ifndef _MATERIALS_PROCESSEDMATERIAL_H_
 25#define _MATERIALS_PROCESSEDMATERIAL_H_
 26
 27#ifndef _MATERIALDEFINITION_H_
 28#include "materials/materialDefinition.h"
 29#endif
 30#ifndef _MATERIALFEATUREDATA_H_
 31#include "materials/materialFeatureData.h"
 32#endif
 33#ifndef _GFXSTATEBLOCK_H_
 34#include "gfx/gfxStateBlock.h"
 35#endif
 36#ifndef _MATTEXTURETARGET_H_
 37#include "materials/matTextureTarget.h"
 38#endif
 39#ifndef _MATSTATEHINT_H_
 40#include "materials/matStateHint.h"
 41#endif
 42
 43#ifndef CUSTOMSHADERBINDINGDATA_H
 44#include "materials/customShaderBindingData.h"
 45#endif
 46
 47class ShaderFeature;
 48class MaterialParameters;
 49class MaterialParameterHandle;
 50class SceneRenderState;
 51class GFXVertexBufferHandleBase;
 52class GFXPrimitiveBufferHandle;
 53class MatrixSet;
 54class GuiTreeViewCtrl;
 55
 56/// This contains the common data needed to render a pass.
 57struct RenderPassData
 58{
 59public:
 60
 61   struct TexSlotT
 62   {
 63      /// This is the default type of texture which 
 64      /// is valid with most texture types.
 65      /// @see mTexType
 66      GFXTexHandle texObject;
 67
 68      /// Only valid when the texture type is set 
 69      /// to Material::TexTarget.
 70      /// @see mTexType
 71     NamedTexTargetRef texTarget;
 72
 73   } mTexSlot[Material::MAX_TEX_PER_PASS];
 74
 75   U32 mTexType[Material::MAX_TEX_PER_PASS];
 76   String mSamplerNames[Material::MAX_TEX_PER_PASS];
 77
 78   /// The cubemap to use when the texture type is
 79   /// set to Material::Cube.
 80   /// @see mTexType
 81   GFXCubemapHandle mCubeMap;
 82
 83   U32 mNumTex;
 84
 85   U32 mNumTexReg;
 86
 87   MaterialFeatureData mFeatureData;
 88
 89   bool mGlow;
 90
 91   Material::BlendOp mBlendOp;
 92
 93   U32 mStageNum;
 94
 95   /// State permutations, used to index into 
 96   /// the render states array.
 97   /// @see mRenderStates
 98   enum 
 99   {
100      STATE_REFLECT = 1,
101      STATE_TRANSLUCENT = 2,
102      STATE_GLOW = 4,      
103      STATE_WIREFRAME = 8,
104      STATE_MAX = 16
105   };
106
107   ///
108   GFXStateBlockRef mRenderStates[STATE_MAX];
109
110   RenderPassData();
111
112   virtual ~RenderPassData() { reset(); }
113
114   virtual void reset();
115
116   /// Creates and returns a unique description string.
117   virtual String describeSelf() const;
118};
119
120/// This is an abstract base class which provides the external
121/// interface all subclasses must implement. This interface
122/// primarily consists of setting state.  Pass creation
123/// is implementation specific, and internal, thus it is
124/// not in this base class.
125class ProcessedMaterial
126{
127public:
128   ProcessedMaterial();
129   virtual ~ProcessedMaterial();
130
131   /// @name State setting functions
132   ///
133   /// @{
134
135   ///
136   virtual void addStateBlockDesc(const GFXStateBlockDesc& sb);
137
138   ///
139   virtual void updateStateBlocks() { _initRenderPassDataStateBlocks(); }
140
141   /// Set the user defined shader macros.
142   virtual void setShaderMacros( const Vector<GFXShaderMacro> &macros ) { mUserMacros = macros; }
143
144   /// Sets the textures needed for rendering the current pass
145   virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ) = 0;
146
147   /// Sets the transformation matrix, i.e. Model * View * Projection
148   virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass) = 0;
149   
150   /// Sets the node transforms for HW Skinning
151   virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) = 0;
152
153   /// Sets any custom shader data
154   virtual void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData, const U32 pass) = 0;
155   
156   /// Sets the scene info like lights for the given pass.
157   virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass) = 0;
158
159   /// Sets the given vertex and primitive buffers so we can render geometry
160   virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); 
161   
162   /// @see BaseMatInstance::setUserObject
163   virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; }
164
165   /// 
166   virtual bool stepInstance();
167
168   /// @}
169
170   /// Initializes us (eg. loads textures, creates passes, generates shaders)
171   virtual bool init(   const FeatureSet& features, 
172                        const GFXVertexFormat *vertexFormat,
173                        const MatFeaturesDelegate &featuresDelegate ) = 0;
174
175   /// Returns the state hint which can be used for 
176   /// sorting and fast comparisions of the equality 
177   /// of a material instance.
178   virtual const MatStateHint& getStateHint() const { return mStateHint; }
179
180   /// Sets up the given pass.  Returns true if the pass was set up, false if there was an error or if
181   /// the specified pass is out of bounds.
182   virtual bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass) = 0;
183
184   // Material parameter methods
185   virtual MaterialParameters* allocMaterialParameters() = 0;
186   virtual MaterialParameters* getDefaultMaterialParameters() = 0;
187   virtual void setMaterialParameters(MaterialParameters* param, S32 pass) { mCurrentParams = param; }; 
188   virtual MaterialParameters* getMaterialParameters() { return mCurrentParams; }
189   virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0;
190
191   /// Returns the pass data for the given pass.
192   RenderPassData* getPass(U32 pass)
193   {
194      if(pass >= mPasses.size())
195         return NULL;
196      return mPasses[pass];
197   }
198
199   /// Returns the pass data for the given pass.
200   const RenderPassData* getPass( U32 pass ) const { return mPasses[pass]; }
201
202   /// Returns the number of stages we're rendering (not to be confused with the number of passes).
203   virtual U32 getNumStages() = 0;
204
205   /// Returns the number of passes we are rendering (not to be confused with the number of stages).
206   U32 getNumPasses() const { return mPasses.size(); }
207
208   /// Returns true if any pass glows
209   bool hasGlow() const { return mHasGlow; }
210
211   /// Returns true if any pass accumulates
212   bool hasAccumulation() const { return mHasAccumulation; }
213
214   /// Gets the stage number for a pass
215   U32 getStageFromPass(U32 pass) const
216   {
217      if(pass >= mPasses.size())
218         return 0;
219      return mPasses[pass]->mStageNum;
220   }
221
222   /// Returns the active features in use by this material.
223   /// @see BaseMatInstance::getFeatures
224   const FeatureSet& getFeatures() const { return mFeatures; }
225
226   /// Dump shader info, or FF texture info?
227   virtual void dumpMaterialInfo() { }
228
229   virtual void getMaterialInfo(GuiTreeViewCtrl* tree, U32 item) {}
230
231   /// Returns the source material.
232   Material* getMaterial() const { return mMaterial; }
233
234   /// Returns the texture used by a stage
235   GFXTexHandle getStageTexture(U32 stage, const FeatureType &type)
236   {
237      return (stage < Material::MAX_STAGES) ? mStages[stage].getTex(type) : NULL;
238   }
239
240protected:
241
242   /// Our passes.
243   Vector<RenderPassData*> mPasses;
244
245   /// The active features in use by this material.
246   FeatureSet mFeatures;
247
248   /// The material which we are processing.
249   Material* mMaterial;
250
251   MaterialParameters* mCurrentParams;
252
253   /// Material::StageData is used here because the shader 
254   /// generator throws a fit if it's passed anything else.
255   Material::StageData mStages[Material::MAX_STAGES];
256
257   /// If we've already loaded the stage data
258   bool mHasSetStageData;
259
260   /// If we glow
261   bool mHasGlow;
262
263   /// If we have accumulation.
264   bool mHasAccumulation;
265
266   /// Number of stages (not to be confused with number of passes)
267   U32 mMaxStages;
268
269   /// The vertex format on which this material will render.
270   const GFXVertexFormat *mVertexFormat;
271
272   ///  Set by addStateBlockDesc, should be considered 
273   /// when initPassStateBlock is called.
274   GFXStateBlockDesc mUserDefined;   
275
276   /// The user defined macros to pass to the 
277   /// shader initialization.
278   Vector<GFXShaderMacro> mUserMacros;
279
280   /// The user defined object to pass to ShaderFeature::createConstHandles.
281   SimObject *mUserObject;
282
283   /// The state hint used for material sorting 
284   /// and quick equality comparision.
285   MatStateHint mStateHint;
286
287   /// Loads all the textures for all of the stages in the Material
288   virtual void _setStageData();
289
290   /// Sets the blend state for rendering   
291   void _setBlendState(Material::BlendOp blendOp, GFXStateBlockDesc& desc );
292
293   /// Returns the path the material will attempt to load for a given texture filename.
294   String _getTexturePath(const String& filename);
295
296   /// Loads the texture located at _getTexturePath(filename) and gives it the specified profile
297   GFXTexHandle _createTexture( const char *filename, GFXTextureProfile *profile );
298   GFXTexHandle _createCompositeTexture(const char *filenameR, const char *filenameG, const char *filenameB, const char *filenameA, U32 inputKey[4], GFXTextureProfile *profile);
299
300   /// @name State blocks
301   ///
302   /// @{
303
304   /// Creates the default state block templates, used by initStateBlocks.
305   virtual void _initStateBlockTemplates(GFXStateBlockDesc& stateTranslucent, GFXStateBlockDesc& stateGlow, GFXStateBlockDesc& stateReflect);
306
307   /// Does the base render state block setting, normally per pass.
308   virtual void _initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc& result);
309
310   /// Creates the default state blocks for a list of render states.
311   virtual void _initRenderStateStateBlocks( RenderPassData *rpd );
312
313   /// Creates the default state blocks for each RenderPassData item.
314   virtual void _initRenderPassDataStateBlocks();
315
316   /// This returns the index into the renderState array based on the sgData passed in.
317   virtual U32 _getRenderStateIndex(   const SceneRenderState *state, 
318                                       const SceneData &sgData );
319
320   /// Activates the correct mPasses[currPass].renderState based on scene graph info
321   virtual void _setRenderState( const SceneRenderState *state, 
322                                 const SceneData &sgData, 
323                                 U32 pass );
324   /// @
325};
326
327#endif // _MATERIALS_PROCESSEDMATERIAL_H_
328