Torque3D Documentation / _generateds / processedShaderMaterial.h

processedShaderMaterial.h

Engine/source/materials/processedShaderMaterial.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
 24#ifndef _MATERIALS_PROCESSEDSHADERMATERIAL_H_
 25#define _MATERIALS_PROCESSEDSHADERMATERIAL_H_
 26
 27#ifndef _MATERIALS_PROCESSEDMATERIAL_H_
 28#include "processedMaterial.h"
 29#endif
 30#ifndef _GFXSHADER_H_
 31#include "gfx/gfxShader.h"
 32#endif
 33#ifndef CUSTOMSHADERBINDINGDATA_H
 34#include "materials/customShaderBindingData.h"
 35#endif
 36
 37class GenericConstBufferLayout;
 38class ShaderData;
 39class LightInfo;
 40class ShaderMaterialParameterHandle;
 41class ShaderFeatureConstHandles;
 42class CustomMaterial;
 43
 44class ShaderConstHandles
 45{
 46public:
 47   GFXShaderConstHandle* mDiffuseColorSC;
 48   GFXShaderConstHandle* mToneMapTexSC;
 49   GFXShaderConstHandle* mTexMatSC;
 50   GFXShaderConstHandle* mORMConfigSC;
 51   GFXShaderConstHandle* mRoughnessSC;
 52   GFXShaderConstHandle* mMetalnessSC;
 53   GFXShaderConstHandle* mGlowMulSC;
 54   GFXShaderConstHandle* mParallaxInfoSC;
 55   GFXShaderConstHandle* mAccuScaleSC;
 56   GFXShaderConstHandle* mAccuDirectionSC;
 57   GFXShaderConstHandle* mAccuStrengthSC;
 58   GFXShaderConstHandle* mAccuCoverageSC;
 59   GFXShaderConstHandle* mAccuSpecularSC;
 60   GFXShaderConstHandle* mFogDataSC;
 61   GFXShaderConstHandle* mFogColorSC;   
 62   GFXShaderConstHandle* mDetailScaleSC;
 63   GFXShaderConstHandle* mVisiblitySC;
 64   GFXShaderConstHandle* mColorMultiplySC;
 65   GFXShaderConstHandle* mAlphaTestValueSC;
 66   GFXShaderConstHandle* mModelViewProjSC;
 67   GFXShaderConstHandle* mWorldViewOnlySC;     
 68   GFXShaderConstHandle* mWorldToCameraSC;
 69   GFXShaderConstHandle* mCameraToWorldSC;
 70   GFXShaderConstHandle* mWorldToObjSC;         
 71   GFXShaderConstHandle* mViewToObjSC;
 72   GFXShaderConstHandle* mInvCameraTransSC;
 73   GFXShaderConstHandle* mCameraToScreenSC;
 74   GFXShaderConstHandle* mScreenToCameraSC;
 75   GFXShaderConstHandle* mCubeTransSC;
 76   GFXShaderConstHandle* mCubeMipsSC;
 77   GFXShaderConstHandle* mObjTransSC;
 78   GFXShaderConstHandle* mCubeEyePosSC;
 79   GFXShaderConstHandle* mEyePosSC;
 80   GFXShaderConstHandle* mEyePosWorldSC;
 81   GFXShaderConstHandle* m_vEyeSC;
 82   GFXShaderConstHandle* mEyeMatSC;
 83   GFXShaderConstHandle* mOneOverFarplane;
 84   GFXShaderConstHandle* mAccumTimeSC;
 85   GFXShaderConstHandle* mMinnaertConstantSC;
 86   GFXShaderConstHandle* mSubSurfaceParamsSC;
 87   GFXShaderConstHandle* mDiffuseAtlasParamsSC;
 88   GFXShaderConstHandle* mBumpAtlasParamsSC;
 89   GFXShaderConstHandle* mDiffuseAtlasTileSC;
 90   GFXShaderConstHandle* mBumpAtlasTileSC;
 91   GFXShaderConstHandle *mRTSizeSC;
 92   GFXShaderConstHandle *mOneOverRTSizeSC;
 93   GFXShaderConstHandle* mDetailBumpStrength;
 94   GFXShaderConstHandle* mViewProjSC;
 95
 96   GFXShaderConstHandle *mImposterUVs;
 97   GFXShaderConstHandle *mImposterLimits;
 98
 99   // Deferred Shading : Material Info Flags
100   GFXShaderConstHandle* mMatInfoFlagsSC;
101
102   GFXShaderConstHandle* mTexHandlesSC[Material::MAX_TEX_PER_PASS];
103   GFXShaderConstHandle* mRTParamsSC[GFX_TEXTURE_STAGE_COUNT];
104
105   GFXShaderConstHandle* mNodeTransforms;
106
107   struct customHandleData
108   {
109      StringTableEntry handleName;
110      GFXShaderConstHandle* handle;
111   };
112   Vector<customHandleData> mCustomHandles;
113
114   void init( GFXShader* shader, CustomMaterial* mat = NULL);
115   
116};
117
118class ShaderRenderPassData : public RenderPassData
119{
120   typedef RenderPassData Parent;
121
122public:
123
124   virtual ~ShaderRenderPassData() { reset(); }
125
126   GFXShaderRef         shader;
127   ShaderConstHandles   shaderHandles;
128   Vector<ShaderFeatureConstHandles*> featureShaderHandles;
129
130   virtual void reset();
131   virtual String describeSelf() const;
132};
133
134class ProcessedShaderMaterial : public ProcessedMaterial
135{
136   typedef ProcessedMaterial Parent;
137public:
138
139   ProcessedShaderMaterial();
140   ProcessedShaderMaterial(Material &mat);
141   ~ProcessedShaderMaterial();
142
143   // ProcessedMaterial
144   virtual bool init(   const FeatureSet &features, 
145                        const GFXVertexFormat *vertexFormat,
146                        const MatFeaturesDelegate &featuresDelegate );
147   virtual bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass);
148   virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass );
149   virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass);
150   virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass);
151   virtual void setCustomShaderData(Vector<CustomShaderBindingData> &shaderData, const U32 pass);
152   virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass);
153   virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); 
154   virtual bool stepInstance();
155   virtual void dumpMaterialInfo();
156   virtual void getMaterialInfo(GuiTreeViewCtrl* tree, U32 item);
157   virtual MaterialParameters* allocMaterialParameters();    
158   virtual MaterialParameters* getDefaultMaterialParameters() { return mDefaultParameters; }   
159   virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name);
160   virtual U32 getNumStages();
161
162protected:
163
164   Vector<GFXShaderConstDesc> mShaderConstDesc;
165   MaterialParameters* mDefaultParameters;
166   Vector<ShaderMaterialParameterHandle*> mParameterHandles;
167
168   /// Hold the instancing state data for the material.   
169   class InstancingState
170   {
171      const static U32 COUNT = 200;
172
173   public:
174
175      InstancingState()
176         :  mInstFormat( NULL ),
177            mBuffer( NULL ),
178            mCount( -1 )
179      {
180      }
181
182      ~InstancingState()
183      {
184         delete [] mBuffer;
185      }
186
187      void setFormat( const GFXVertexFormat *instFormat, const GFXVertexFormat *vertexFormat )
188      {
189         mInstFormat = instFormat;
190         mDeclFormat.copy( *vertexFormat );
191         mDeclFormat.append( *mInstFormat, 1 );
192         // Let the declaration know we have instancing.
193         mDeclFormat.enableInstancing();
194         mDeclFormat.getDecl();
195
196         delete [] mBuffer;
197         mBuffer = new U8[ mInstFormat->getSizeInBytes() * COUNT ];
198         mCount = -1;
199      }
200
201      bool step( U8 **outPtr )
202      {
203         // Are we starting a new draw call?
204         if ( mCount < 0 )
205         {
206            *outPtr = mBuffer;
207            mCount = 0;
208         }
209         else
210         {
211            // Increment to the next instance.
212            *outPtr += mInstFormat->getSizeInBytes();
213            mCount++;
214         }
215
216         return mCount < COUNT;
217      }
218
219      void resetStep() { mCount = -1; }
220
221      U8* getBuffer() const { return mBuffer; }
222
223      S32 getCount() const { return mCount; }
224
225      const GFXVertexFormat* getFormat() const { return mInstFormat; }
226
227      const GFXVertexFormat* getDeclFormat() const { return &mDeclFormat; }
228
229   protected:
230
231      GFXVertexFormat mDeclFormat;
232      const GFXVertexFormat *mInstFormat;  
233      U8 *mBuffer;
234      S32 mCount;
235
236   };
237
238   /// The instancing state if this material
239   /// supports instancing.
240   InstancingState *mInstancingState;
241   
242   /// @name Internal functions
243   ///
244   /// @{
245
246   /// Adds a pass for the given stage.
247   virtual bool _addPass( ShaderRenderPassData &rpd, 
248      U32 &texIndex, 
249      MaterialFeatureData &fd,
250      U32 stageNum,
251      const FeatureSet &features);
252
253   /// Chooses a blend op for the given pass
254   virtual void _setPassBlendOp( ShaderFeature *sf,
255      ShaderRenderPassData &passData,
256      U32 &texIndex,
257      MaterialFeatureData &stageFeatures,
258      U32 stageNum,
259      const FeatureSet &features);
260
261   /// Creates passes for the given stage
262   virtual bool _createPasses( MaterialFeatureData &fd, U32 stageNum, const FeatureSet &features );
263
264   /// Fills in the MaterialFeatureData for the given stage
265   virtual void _determineFeatures( U32 stageNum, 
266                                    MaterialFeatureData &fd, 
267                                    const FeatureSet &features );
268
269   /// Do we have a cubemap on pass?
270   virtual bool _hasCubemap(U32 pass);
271
272   /// Used by setTextureTransforms
273   F32 _getWaveOffset( U32 stage );
274
275   /// Sets texture transformation matrices for texture animations such as scale and wave
276   virtual void _setTextureTransforms(const U32 pass);
277
278   /// Sets all of the necessary shader constants for the given pass
279   virtual void _setShaderConstants(SceneRenderState *, const SceneData &sgData, U32 pass);
280
281   /// @}
282
283   void _setPrimaryLightConst(const LightInfo* light, const MatrixF& objTrans, const U32 stageNum);
284
285   /// This is here to deal with the differences between ProcessedCustomMaterials and ProcessedShaderMaterials.
286public:
287   virtual GFXShaderConstBuffer* _getShaderConstBuffer(const U32 pass);
288   virtual ShaderConstHandles* _getShaderConstHandles(const U32 pass);
289
290protected:
291   ///
292   virtual void _initMaterialParameters();
293
294   ShaderRenderPassData* _getRPD(const U32 pass) { return static_cast<ShaderRenderPassData*>(mPasses[pass]); }
295};
296
297#endif // _MATERIALS_PROCESSEDSHADERMATERIAL_H_
298