Torque3D Documentation / _generateds / advancedLightBinManager.h

advancedLightBinManager.h

Engine/source/lighting/advanced/advancedLightBinManager.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 _ADVANCEDLIGHTBINMANAGER_H_
 25#define _ADVANCEDLIGHTBINMANAGER_H_
 26
 27#ifndef _TEXTARGETBIN_MGR_H_
 28#include "renderInstance/renderTexTargetBinManager.h"
 29#endif
 30#ifndef _TVECTOR_H_
 31#include "core/util/tVector.h"
 32#endif
 33#ifndef _LIGHTINFO_H_
 34#include "lighting/lightInfo.h"
 35#endif
 36#ifndef _MATHUTIL_FRUSTUM_H_
 37#include "math/util/frustum.h"
 38#endif
 39#ifndef _MATINSTANCE_H_
 40#include "materials/matInstance.h"
 41#endif
 42#ifndef _SHADOW_COMMON_H_
 43#include "lighting/shadowMap/shadowCommon.h"
 44#endif
 45
 46
 47class AdvancedLightManager;
 48class ShadowMapManager;
 49class LightShadowMap;
 50class AdvancedLightBufferConditioner;
 51class LightMapParams;
 52
 53
 54class LightMatInstance : public MatInstance
 55{
 56   typedef MatInstance Parent;
 57protected:
 58   MaterialParameterHandle *mLightMapParamsSC;
 59   bool mInternalPass;
 60
 61   enum
 62   {
 63      DynamicLight = 0,
 64      SunLight,
 65      StaticLightNonLMGeometry,
 66      StaticLightLMGeometry,
 67      NUM_LIT_STATES,
 68      Base = 0,
 69      Reflecting = 1
 70   };
 71   GFXStateBlockRef mLitState[NUM_LIT_STATES][2];
 72
 73public:
 74   LightMatInstance(Material &mat) : Parent(mat), mLightMapParamsSC(NULL), mInternalPass(false),  mSpecialLight(NULL) {}
 75
 76   virtual bool init( const FeatureSet &features, const GFXVertexFormat *vertexFormat );
 77   virtual bool setupPass( SceneRenderState *state, const SceneData &sgData );
 78
 79   bool mSpecialLight;
 80};
 81
 82class AdvancedLightBinManager : public RenderBinManager
 83{
 84   typedef RenderBinManager Parent;
 85
 86public:
 87
 88   // Light info Render Inst Type
 89   static const RenderInstType RIT_LightInfo;
 90   
 91   // registered buffer name
 92   static const String smBufferName;
 93
 94   NamedTexTargetRef mDiffuseLightingTarget;
 95   GFXTexHandle      mDiffuseLightingTex;
 96
 97   GFXTextureTargetRef mLightingTargetRef;
 98
 99   /// The shadow filter mode to use on shadowed light materials.
100   static ShadowFilterMode smShadowFilterMode;
101
102   /// Used to toggle the PSSM debug rendering mode.
103   static bool smPSSMDebugRender;
104
105   /// Set by the SSAO post effect to tell the vector
106   /// light to compile in the SSAO mask.
107   static bool smUseSSAOMask;
108
109   //Used to toggle the visualization of diffuse and specular light contribution
110   static bool smDiffuseLightViz;
111   static bool smSpecularLightViz;
112   static bool smDetailLightingViz;
113
114   static S32 smMaximumNumOfLights;
115   static bool smUseLightFade;
116   static F32 smLightFadeEnd;
117   static F32 smLightFadeStart;
118
119   static bool smAllowLocalLightShadows;
120
121   // Used for console init
122   AdvancedLightBinManager( AdvancedLightManager *lm = NULL, 
123                            ShadowMapManager *sm = NULL,
124                            GFXFormat lightBufferFormat = GFXFormatR8G8B8A8 );
125   virtual ~AdvancedLightBinManager();
126
127   // ConsoleObject
128   static void consoleInit();
129
130   // RenderBinManager
131   virtual void render(SceneRenderState *);
132   virtual void clear() {}
133   virtual void sort() {}
134
135   // Add a light to the bins
136   void addLight( LightInfo *light );
137
138   // Clear all lights from the bins
139   void clearAllLights();
140
141   virtual bool setTargetSize(const Point2I &newTargetSize);
142
143   /// Scores the registered lights for sorting/ordering purposes
144   void _scoreLights(const MatrixF& cameraTrans);
145
146   // ConsoleObject interface
147   DECLARE_CONOBJECT(AdvancedLightBinManager);
148
149   bool MRTLightmapsDuringDeferred() const { return mMRTLightmapsDuringDeferred; }
150   void MRTLightmapsDuringDeferred(bool val);
151
152   bool _updateTargets();
153
154   typedef Signal<void(SceneRenderState *, AdvancedLightBinManager *)> RenderSignal;
155   static RenderSignal &getRenderSignal();
156
157   AdvancedLightManager *getManager() { return mLightManager; }
158
159protected:
160
161   /// Frees all the currently allocated light materials.
162   void _deleteLightMaterials();
163
164   // Track a light material and associated data
165   struct LightMaterialInfo
166   {
167      LightMatInstance *matInstance;
168
169      // { zNear, zFar, 1/zNear, 1/zFar }
170      MaterialParameterHandle *zNearFarInvNearFar;
171
172      // Far frustum plane (World Space)
173      MaterialParameterHandle *farPlane;
174
175      // Far frustum plane (View Space)
176      MaterialParameterHandle *vsFarPlane;
177
178      // -dot( farPlane, eyePos )
179      MaterialParameterHandle *negFarPlaneDotEye;
180
181      // Light Parameters
182      MaterialParameterHandle *lightPosition;
183      MaterialParameterHandle *lightDirection;
184      MaterialParameterHandle *lightColor;
185      MaterialParameterHandle *lightBrightness;
186      MaterialParameterHandle *lightRange;
187      MaterialParameterHandle *lightInvSqrRange;
188      MaterialParameterHandle *lightAmbient;
189      MaterialParameterHandle *lightSpotParams;
190
191      LightMaterialInfo(   const String &matName, 
192                           const GFXVertexFormat *vertexFormat,
193                           const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>() );
194
195      virtual ~LightMaterialInfo();
196
197
198      void setViewParameters( const F32 zNear, 
199                              const F32 zFar, 
200                              const Point3F &eyePos, 
201                              const PlaneF &farPlane,
202                              const PlaneF &_vsFarPlane );
203
204      void setLightParameters( const LightInfo *light, const SceneRenderState* renderState );
205   };
206
207protected:
208
209   struct LightBinEntry
210   {
211      LightInfo* lightInfo;
212      LightShadowMap* shadowMap;
213      LightMaterialInfo* lightMaterial;
214      GFXPrimitiveBuffer* primBuffer;
215      GFXVertexBuffer* vertBuffer;
216      U32 numPrims;
217   };
218
219   Vector<LightBinEntry> mLightBin;
220   typedef Vector<LightBinEntry>::iterator LightBinIterator;
221
222   bool mMRTLightmapsDuringDeferred;
223
224   /// Used in setupSGData to set the object transform.
225   MatrixF mLightMat;
226
227   U32 mNumLightsCulled;
228   AdvancedLightManager *mLightManager;
229   ShadowMapManager *mShadowManager;
230
231   static const String smLightMatNames[LightInfo::Count];
232
233   static const String smShadowTypeMacro[ShadowType_Count];
234
235   static const GFXVertexFormat* smLightMatVertex[LightInfo::Count];
236
237   typedef CompoundKey3<LightInfo::Type,ShadowType,bool> LightMatKey;
238
239   typedef HashTable<LightMatKey,LightMaterialInfo*> LightMatTable;
240
241   /// The fixed table of light material info.
242   LightMatTable mLightMaterials;
243
244   LightMaterialInfo* _getLightMaterial( LightInfo::Type lightType, ShadowType shadowType, bool useCookieTex );
245
246   ///
247   void _onShadowFilterChanged();
248
249   typedef GFXVertexPNTT FarFrustumQuadVert; 
250   GFXVertexBufferHandle<FarFrustumQuadVert> mFarFrustumQuadVerts;
251
252
253   //void _createMaterials();
254
255   void _setupPerFrameParameters( const SceneRenderState *state );
256
257   void setupSGData( SceneData &data, const SceneRenderState* state, LightInfo *light );
258
259   static S32 QSORT_CALLBACK _lightScoreCmp(const LightBinEntry* a, const  LightBinEntry* b);
260};
261
262#endif // _ADVANCEDLIGHTBINMANAGER_H_
263