Torque3D Documentation / _generateds / lightShadowMap.h

lightShadowMap.h

Engine/source/lighting/shadowMap/lightShadowMap.h

More...

Classes:

class

This represents everything we need to render the shadowmap for one light.

Public Typedefs

LightConstantMap 

Public Functions

GFX_DeclareTextureProfile(ShadowMapProfile )
GFX_DeclareTextureProfile(ShadowMapZProfile )

Detailed Description

Public Typedefs

typedef Map< GFXShader *, LightingShaderConstants * > LightConstantMap 

Public Functions

GFX_DeclareTextureProfile(ShadowMapProfile )

GFX_DeclareTextureProfile(ShadowMapZProfile )

  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 _LIGHTSHADOWMAP_H_
 25#define _LIGHTSHADOWMAP_H_
 26
 27#ifndef _GFXTEXTUREHANDLE_H_
 28#include "gfx/gfxTextureHandle.h"
 29#endif
 30#ifndef _GFXCUBEMAP_H_
 31#include "gfx/gfxCubemap.h"
 32#endif
 33#ifndef _GFXTARGET_H_
 34#include "gfx/gfxTarget.h"
 35#endif
 36#ifndef _LIGHTINFO_H_
 37#include "lighting/lightInfo.h"
 38#endif
 39#ifndef _MATHUTIL_FRUSTUM_H_
 40#include "math/util/frustum.h"
 41#endif
 42#ifndef _MATTEXTURETARGET_H_
 43#include "materials/matTextureTarget.h"
 44#endif
 45#ifndef _SHADOW_COMMON_H_
 46#include "lighting/shadowMap/shadowCommon.h"
 47#endif
 48#ifndef _GFXSHADER_H_
 49#include "gfx/gfxShader.h"
 50#endif
 51#ifndef _GFXOCCLUSIONQUERY_H_
 52#include "gfx/gfxOcclusionQuery.h"
 53#endif
 54#ifndef _PLATFORM_PLATFORMTIMER_H_
 55#include "platform/platformTimer.h"
 56#endif
 57
 58class ShadowMapManager;
 59class SceneManager;
 60class SceneRenderState;
 61class BaseMatInstance;
 62class MaterialParameters;
 63class SharedShadowMapObjects;
 64struct SceneData;
 65class GFXShaderConstBuffer;
 66class GFXShaderConstHandle;
 67class GFXShader;
 68class LightManager;
 69class RenderPassManager;
 70
 71
 72// Shader constant handle lookup
 73// This isn't broken up as much as it could be, we're mixing single light constants
 74// and pssm constants.
 75struct LightingShaderConstants
 76{
 77   bool mInit;
 78
 79   GFXShaderRef mShader;
 80
 81   GFXShaderConstHandle* mLightParamsSC;
 82   GFXShaderConstHandle* mLightSpotParamsSC;
 83   
 84   // NOTE: These are the shader constants used for doing 
 85   // lighting  during the forward pass.  Do not confuse 
 86   // these for the deferred lighting constants which are 
 87   // used from AdvancedLightBinManager.
 88   GFXShaderConstHandle *mLightPositionSC;
 89   GFXShaderConstHandle *mLightDiffuseSC;
 90   GFXShaderConstHandle *mLightAmbientSC;
 91   GFXShaderConstHandle *mLightConfigDataSC;
 92   GFXShaderConstHandle *mLightSpotDirSC;
 93
 94   GFXShaderConstHandle* mHasVectorLightSC;
 95   GFXShaderConstHandle* mVectorLightDirectionSC;
 96   GFXShaderConstHandle* mVectorLightColorSC;
 97   GFXShaderConstHandle* mVectorLightBrightnessSC;
 98
 99   GFXShaderConstHandle* mShadowMapSC;
100   GFXShaderConstHandle* mShadowMapSizeSC;
101
102   GFXShaderConstHandle* mCookieMapSC;
103
104   GFXShaderConstHandle* mRandomDirsConst;
105   GFXShaderConstHandle* mShadowSoftnessConst;
106   GFXShaderConstHandle* mAtlasXOffsetSC;
107   GFXShaderConstHandle* mAtlasYOffsetSC;
108   GFXShaderConstHandle* mAtlasScaleSC;
109
110   // fadeStartLength.x = Distance in eye space to start fading shadows
111   // fadeStartLength.y = 1 / Length of fade
112   GFXShaderConstHandle* mFadeStartLength;
113   GFXShaderConstHandle* mOverDarkFactorPSSM;
114
115   GFXShaderConstHandle* mTapRotationTexSC;
116
117   // Static Specific:   
118   GFXShaderConstHandle* mWorldToLightProjSC;
119   GFXShaderConstHandle* mViewToLightProjSC;
120   GFXShaderConstHandle* mScaleXSC;
121   GFXShaderConstHandle* mScaleYSC;
122   GFXShaderConstHandle* mOffsetXSC;
123   GFXShaderConstHandle* mOffsetYSC;
124   GFXShaderConstHandle* mFarPlaneScalePSSM;
125
126   LightingShaderConstants();
127   ~LightingShaderConstants();
128
129   void init(GFXShader* buffer);
130
131   void _onShaderReload();
132};
133
134typedef Map<GFXShader*, LightingShaderConstants*> LightConstantMap;
135
136
137/// This represents everything we need to render
138/// the shadowmap for one light.
139class LightShadowMap
140{
141public:
142
143   const static GFXFormat ShadowMapFormat;
144
145   /// Used to scale the shadow texture size for performance tweaking.
146   static F32 smShadowTexScalar;
147
148   /// Whether to render shadow frustums for lights that have debug
149   /// rendering enabled.
150   static bool smDebugRenderFrustums;
151
152public:
153
154   LightShadowMap( LightInfo *light );
155
156   virtual ~LightShadowMap();
157
158   void render(   RenderPassManager* renderPass,
159                  const SceneRenderState *diffuseState);
160
161   //U32 getLastVisible() const { return mLastVisible; }
162
163   bool isViewDependent() const { return mIsViewDependent; }
164
165   void updatePriority( const SceneRenderState *state, U32 currTimeMs );
166
167   F32 getLastScreenSize() const { return mLastScreenSize; }
168
169   F32 getLastPriority() const { return mLastPriority; }
170
171   virtual bool hasShadowTex() const { return mShadowMapTex.isValid(); }
172
173   virtual bool setTextureStage( U32 currTexFlag, LightingShaderConstants* lsc );
174
175   LightInfo* getLightInfo() { return mLight; }
176
177   virtual void setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc) = 0;
178
179   U32 getTexSize() const { return mTexSize; }
180
181   /// Returns the best texture size based on the user
182   /// texture size, the last light screen size, and 
183   /// global shadow tweak parameters.
184   U32 getBestTexSize( U32 scale = 1 ) const;
185
186   const MatrixF& getWorldToLightProj() const { return mWorldToLightProj; }
187
188   static GFXTextureObject* _getDepthTarget( U32 width, U32 height );
189
190   virtual ShadowType getShadowType() const = 0;
191
192   // Cleanup texture resources
193   virtual void releaseTextures();
194
195   ///
196   GFXTextureObject* getTexture() const { return mShadowMapTex; }
197
198   ///
199   void setDebugTarget( const String &name );
200
201   static void releaseAllTextures();
202
203   /// Releases any shadow maps that have not been culled
204   /// in a while and returns the count of the remaing 
205   /// shadow maps in use.
206   static U32 releaseUnusedTextures();
207
208   ///
209   static S32 QSORT_CALLBACK cmpPriority( LightShadowMap *const *lsm1, LightShadowMap *const *lsm2 );
210
211   /// Returns the correct shadow material this type of light
212   /// or NULL if no shadow material is possible.
213   BaseMatInstance* getShadowMaterial( BaseMatInstance *inMat ) const;
214
215protected:
216
217   /// All the shadow maps in the system.
218   static Vector<LightShadowMap*> smShadowMaps;
219
220   /// All the shadow maps that have been recently rendered to.
221   static Vector<LightShadowMap*> smUsedShadowMaps;
222   
223   virtual void _render(   RenderPassManager* renderPass,
224                           const SceneRenderState *diffuseState ) = 0;
225
226   /// If there is a LightDebugInfo attached to the light that owns this map,
227   /// then update its information from the given render state.
228   ///
229   /// @note This method only does something in debug builds.
230   void _debugRender( SceneRenderState* shadowRenderState );
231
232   /// Helper for rendering shadow map for debugging.
233   NamedTexTarget mDebugTarget;
234
235   /// If true the shadow is view dependent and cannot
236   /// be skipped if visible and within active range.
237   bool mIsViewDependent;
238
239   /// The time this shadow was last culled and prioritized.
240   U32 mLastCull;
241
242   F32 mLastScreenSize;
243
244   F32 mLastPriority;
245
246   MatrixF mWorldToLightProj;
247
248   GFXTextureTargetRef mTarget;
249   U32 mTexSize;
250   GFXTexHandle mShadowMapTex;
251   GFXTexHandle mShadowMapDepth;
252
253   // The light we are rendering.
254   LightInfo *mLight;   
255
256   // Used for blur
257   GFXShader* mLastShader;
258   GFXShaderConstHandle* mBlurBoundaries;
259
260   // Calculate view matrices and set proper projection with GFX
261   void calcLightMatrices( MatrixF& outLightMatrix, const Frustum &viewFrustum );
262
263   /// The callback used to get texture events.
264   /// @see GFXTextureManager::addEventDelegate
265   void _onTextureEvent( GFXTexCallbackCode code );  
266};
267
268GFX_DeclareTextureProfile( ShadowMapProfile );
269GFX_DeclareTextureProfile( ShadowMapZProfile );
270
271
272class ShadowMapParams : public LightInfoEx
273{
274public:
275
276   ShadowMapParams( LightInfo *light );
277   virtual ~ShadowMapParams();
278
279   /// The LightInfoEx hook type.
280   static LightInfoExType Type;
281
282   // LightInfoEx
283   virtual void set( const LightInfoEx *ex );
284   virtual const LightInfoExType& getType() const { return Type; }
285   virtual void packUpdate( BitStream *stream ) const;
286   virtual void unpackUpdate( BitStream *stream );
287
288   LightShadowMap* getShadowMap() const { return mShadowMap; }
289
290   LightShadowMap* getOrCreateShadowMap();
291
292   bool hasCookieTex() const { return cookie.isNotEmpty(); }
293
294   GFXOcclusionQuery* getOcclusionQuery() const { return mQuery; }
295
296   GFXTextureObject* getCookieTex();
297
298   GFXCubemap* getCookieCubeTex();
299
300   // Validates the parameters after a field is changed.
301   void _validate();
302
303protected:
304
305   void _initShadowMap();
306
307   ///
308   LightShadowMap *mShadowMap;
309   GFXOcclusionQuery* mQuery;
310
311   LightInfo *mLight;
312
313   GFXTexHandle mCookieTex;
314
315   GFXCubemapHandle mCookieCubeTex;
316
317public:
318
319   // We're leaving these public for easy access 
320   // for console protected fields.
321
322   /// @name Shadow Map
323   /// @{
324   
325   ///
326   U32 texSize;
327
328   /// 
329   FileName cookie;
330
331   /// @}
332
333   Point3F attenuationRatio;
334
335   /// @name Point Lights
336   /// @{
337
338   ///
339   ShadowType shadowType;
340
341   /// @}
342
343   /// @name Exponential Shadow Map Parameters
344   /// @{
345   Point4F overDarkFactor;
346   /// @}   
347
348   /// @name Parallel Split Shadow Map
349   /// @{
350
351   ///
352   F32 shadowDistance;  
353
354   ///
355   F32 shadowSoftness;
356
357   /// The number of splits in the shadow map.
358   U32 numSplits;
359
360   /// 
361   F32 logWeight;
362
363   /// At what distance do we start fading the shadows out completely.
364   F32 fadeStartDist;
365
366   /// This toggles only terrain being visible in the last
367   /// split of a PSSM shadow map.
368   bool lastSplitTerrainOnly;
369
370   /// @}
371};
372
373#endif // _LIGHTSHADOWMAP_H_
374