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 prepass lighting constants which are 
 87   // used from AdvancedLightBinManager.
 88   GFXShaderConstHandle *mLightPositionSC;
 89   GFXShaderConstHandle *mLightDiffuseSC;
 90   GFXShaderConstHandle *mLightAmbientSC;
 91   GFXShaderConstHandle *mLightInvRadiusSqSC;
 92   GFXShaderConstHandle *mLightSpotDirSC;
 93   GFXShaderConstHandle *mLightSpotAngleSC;
 94   GFXShaderConstHandle *mLightSpotFalloffSC;
 95
 96   GFXShaderConstHandle* mShadowMapSC;
 97   GFXShaderConstHandle* mDynamicShadowMapSC;
 98   GFXShaderConstHandle* mShadowMapSizeSC;
 99
100   GFXShaderConstHandle* mCookieMapSC;
101
102   GFXShaderConstHandle* mRandomDirsConst;
103   GFXShaderConstHandle* mShadowSoftnessConst;
104   GFXShaderConstHandle* mAtlasXOffsetSC;
105   GFXShaderConstHandle* mAtlasYOffsetSC;
106   GFXShaderConstHandle* mAtlasScaleSC;
107
108   // fadeStartLength.x = Distance in eye space to start fading shadows
109   // fadeStartLength.y = 1 / Length of fade
110   GFXShaderConstHandle* mFadeStartLength;
111   GFXShaderConstHandle* mOverDarkFactorPSSM;
112
113   GFXShaderConstHandle* mTapRotationTexSC;
114
115   // Static Specific:   
116   GFXShaderConstHandle* mWorldToLightProjSC;
117   GFXShaderConstHandle* mViewToLightProjSC;
118   GFXShaderConstHandle* mScaleXSC;
119   GFXShaderConstHandle* mScaleYSC;
120   GFXShaderConstHandle* mOffsetXSC;
121   GFXShaderConstHandle* mOffsetYSC;
122   GFXShaderConstHandle* mFarPlaneScalePSSM;
123
124   // Dynamic Specific:   
125   GFXShaderConstHandle* mDynamicWorldToLightProjSC;
126   GFXShaderConstHandle* mDynamicViewToLightProjSC;
127   GFXShaderConstHandle* mDynamicScaleXSC;
128   GFXShaderConstHandle* mDynamicScaleYSC;
129   GFXShaderConstHandle* mDynamicOffsetXSC;
130   GFXShaderConstHandle* mDynamicOffsetYSC;
131   GFXShaderConstHandle* mDynamicFarPlaneScalePSSM;
132
133   LightingShaderConstants();
134   ~LightingShaderConstants();
135
136   void init(GFXShader* buffer);
137
138   void _onShaderReload();
139};
140
141typedef Map<GFXShader*, LightingShaderConstants*> LightConstantMap;
142
143
144/// This represents everything we need to render
145/// the shadowmap for one light.
146class LightShadowMap
147{
148public:
149
150   const static GFXFormat ShadowMapFormat;
151
152   /// Used to scale the shadow texture size for performance tweaking.
153   static F32 smShadowTexScalar;
154
155   /// Whether to render shadow frustums for lights that have debug
156   /// rendering enabled.
157   static bool smDebugRenderFrustums;
158
159public:
160
161   LightShadowMap( LightInfo *light );
162
163   virtual ~LightShadowMap();
164
165   void render(   RenderPassManager* renderPass,
166                  const SceneRenderState *diffuseState,
167                  bool _dynamic, bool _forceUpdate);
168
169   U32 getLastUpdate() const { return mLastUpdate; }
170
171   //U32 getLastVisible() const { return mLastVisible; }
172
173   bool isViewDependent() const { return mIsViewDependent; }
174
175   void updatePriority( const SceneRenderState *state, U32 currTimeMs );
176
177   F32 getLastScreenSize() const { return mLastScreenSize; }
178
179   F32 getLastPriority() const { return mLastPriority; }
180
181   virtual bool hasShadowTex() const { return mShadowMapTex.isValid(); }
182
183   virtual bool setTextureStage( U32 currTexFlag, LightingShaderConstants* lsc );
184
185   LightInfo* getLightInfo() { return mLight; }
186
187   virtual void setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc) = 0;
188
189   U32 getTexSize() const { return mTexSize; }
190
191   /// Returns the best texture size based on the user
192   /// texture size, the last light screen size, and 
193   /// global shadow tweak parameters.
194   U32 getBestTexSize( U32 scale = 1 ) const;
195
196   const MatrixF& getWorldToLightProj() const { return mWorldToLightProj; }
197
198   static GFXTextureObject* _getDepthTarget( U32 width, U32 height );
199
200   virtual ShadowType getShadowType() const = 0;
201
202   // Cleanup texture resources
203   virtual void releaseTextures();
204
205   ///
206   GFXTextureObject* getTexture() const { return mShadowMapTex; }
207
208   ///
209   void setDebugTarget( const String &name );
210
211   static void releaseAllTextures();
212
213   /// Releases any shadow maps that have not been culled
214   /// in a while and returns the count of the remaing 
215   /// shadow maps in use.
216   static U32 releaseUnusedTextures();
217
218   ///
219   static S32 QSORT_CALLBACK cmpPriority( LightShadowMap *const *lsm1, LightShadowMap *const *lsm2 );
220
221   /// Returns the correct shadow material this type of light
222   /// or NULL if no shadow material is possible.
223   BaseMatInstance* getShadowMaterial( BaseMatInstance *inMat ) const;
224
225protected:
226
227   /// All the shadow maps in the system.
228   static Vector<LightShadowMap*> smShadowMaps;
229
230   /// All the shadow maps that have been recently rendered to.
231   static Vector<LightShadowMap*> smUsedShadowMaps;
232   
233   virtual void _render(   RenderPassManager* renderPass,
234                           const SceneRenderState *diffuseState ) = 0;
235
236   /// If there is a LightDebugInfo attached to the light that owns this map,
237   /// then update its information from the given render state.
238   ///
239   /// @note This method only does something in debug builds.
240   void _debugRender( SceneRenderState* shadowRenderState );
241
242   /// Helper for rendering shadow map for debugging.
243   NamedTexTarget mDebugTarget;
244
245   /// If true the shadow is view dependent and cannot
246   /// be skipped if visible and within active range.
247   bool mIsViewDependent;
248
249   /// The time this shadow was last updated.
250   U32 mLastUpdate;
251   PlatformTimer *mStaticRefreshTimer;
252   PlatformTimer *mDynamicRefreshTimer;
253
254   /// The time this shadow was last culled and prioritized.
255   U32 mLastCull;
256
257   F32 mLastScreenSize;
258
259   F32 mLastPriority;
260
261   MatrixF mWorldToLightProj;
262
263   GFXTextureTargetRef mTarget;
264   U32 mTexSize;
265   GFXTexHandle mShadowMapTex;
266   GFXTexHandle mShadowMapDepth;
267
268   // The light we are rendering.
269   LightInfo *mLight;   
270
271   // Used for blur
272   GFXShader* mLastShader;
273   GFXShaderConstHandle* mBlurBoundaries;
274
275   // Calculate view matrices and set proper projection with GFX
276   void calcLightMatrices( MatrixF& outLightMatrix, const Frustum &viewFrustum );
277
278   /// The callback used to get texture events.
279   /// @see GFXTextureManager::addEventDelegate
280   void _onTextureEvent( GFXTexCallbackCode code );  
281
282   bool mIsDynamic;
283public:
284
285   bool isDynamic() { return mIsDynamic; }
286   void setDynamic(bool value) { mIsDynamic = value; }
287
288};
289
290GFX_DeclareTextureProfile( ShadowMapProfile );
291GFX_DeclareTextureProfile( ShadowMapZProfile );
292
293
294class ShadowMapParams : public LightInfoEx
295{
296public:
297
298   ShadowMapParams( LightInfo *light );
299   virtual ~ShadowMapParams();
300
301   /// The LightInfoEx hook type.
302   static LightInfoExType Type;
303
304   // LightInfoEx
305   virtual void set( const LightInfoEx *ex );
306   virtual const LightInfoExType& getType() const { return Type; }
307   virtual void packUpdate( BitStream *stream ) const;
308   virtual void unpackUpdate( BitStream *stream );
309
310   LightShadowMap* getShadowMap(bool _isDynamic = false) const { return _isDynamic ? mDynamicShadowMap : mShadowMap; }
311
312   LightShadowMap* getOrCreateShadowMap(bool _isDynamic = false);
313
314   bool hasCookieTex() const { return cookie.isNotEmpty(); }
315
316   GFXOcclusionQuery* getOcclusionQuery() const { return mQuery; }
317
318   GFXTextureObject* getCookieTex();
319
320   GFXCubemap* getCookieCubeTex();
321
322   // Validates the parameters after a field is changed.
323   void _validate();
324
325protected:
326
327   void _initShadowMap();
328
329   ///
330   LightShadowMap *mShadowMap;
331   LightShadowMap *mDynamicShadowMap;
332   GFXOcclusionQuery* mQuery;
333
334   LightInfo *mLight;
335
336   GFXTexHandle mCookieTex;
337
338   GFXCubemapHandle mCookieCubeTex;
339
340public:
341
342   // We're leaving these public for easy access 
343   // for console protected fields.
344
345   /// @name Shadow Map
346   /// @{
347   
348   ///
349   U32 texSize;
350
351   /// 
352   FileName cookie;
353
354   /// @}
355
356   Point3F attenuationRatio;
357
358   /// @name Point Lights
359   /// @{
360
361   ///
362   ShadowType shadowType;
363
364   /// @}
365
366   /// @name Exponential Shadow Map Parameters
367   /// @{
368   Point4F overDarkFactor;
369   /// @}   
370
371   /// @name Parallel Split Shadow Map
372   /// @{
373
374   ///
375   F32 shadowDistance;  
376
377   ///
378   F32 shadowSoftness;
379
380   /// The number of splits in the shadow map.
381   U32 numSplits;
382
383   /// 
384   F32 logWeight;
385
386   /// At what distance do we start fading the shadows out completely.
387   F32 fadeStartDist;
388
389   /// This toggles only terrain being visible in the last
390   /// split of a PSSM shadow map.
391   bool lastSplitTerrainOnly;
392
393   /// @}
394   bool isDynamic;
395};
396
397#endif // _LIGHTSHADOWMAP_H_
398