advancedLightBinManager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ADVANCEDLIGHTBINMANAGER_H_
  23. #define _ADVANCEDLIGHTBINMANAGER_H_
  24. #ifndef _TEXTARGETBIN_MGR_H_
  25. #include "renderInstance/renderTexTargetBinManager.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _LIGHTINFO_H_
  31. #include "lighting/lightInfo.h"
  32. #endif
  33. #ifndef _MATHUTIL_FRUSTUM_H_
  34. #include "math/util/frustum.h"
  35. #endif
  36. #ifndef _MATINSTANCE_H_
  37. #include "materials/matInstance.h"
  38. #endif
  39. #ifndef _SHADOW_COMMON_H_
  40. #include "lighting/shadowMap/shadowCommon.h"
  41. #endif
  42. class AdvancedLightManager;
  43. class ShadowMapManager;
  44. class LightShadowMap;
  45. class AdvancedLightBufferConditioner;
  46. class LightMapParams;
  47. class LightMatInstance : public MatInstance
  48. {
  49. typedef MatInstance Parent;
  50. protected:
  51. MaterialParameterHandle *mLightMapParamsSC;
  52. bool mInternalPass;
  53. enum
  54. {
  55. DynamicLight = 0,
  56. SunLight,
  57. StaticLightNonLMGeometry,
  58. StaticLightLMGeometry,
  59. NUM_LIT_STATES,
  60. Base = 0,
  61. Reflecting = 1
  62. };
  63. GFXStateBlockRef mLitState[NUM_LIT_STATES][2];
  64. public:
  65. LightMatInstance(Material &mat) : Parent(mat), mLightMapParamsSC(NULL), mInternalPass(false), mSpecialLight(false) {}
  66. virtual bool init( const FeatureSet &features, const GFXVertexFormat *vertexFormat );
  67. virtual bool setupPass( SceneRenderState *state, const SceneData &sgData );
  68. bool mSpecialLight;
  69. };
  70. class AdvancedLightBinManager : public RenderBinManager
  71. {
  72. typedef RenderBinManager Parent;
  73. public:
  74. // Light info Render Inst Type
  75. static const RenderInstType RIT_LightInfo;
  76. // registered buffer name
  77. static const String smBufferName;
  78. NamedTexTargetRef mDiffuseLightingTarget;
  79. GFXTexHandle mDiffuseLightingTex;
  80. GFXTextureTargetRef mLightingTargetRef;
  81. /// The shadow filter mode to use on shadowed light materials.
  82. static ShadowFilterMode smShadowFilterMode;
  83. /// Used to toggle the PSSM debug rendering mode.
  84. static bool smPSSMDebugRender;
  85. /// Set by the SSAO post effect to tell the vector
  86. /// light to compile in the SSAO mask.
  87. static bool smUseSSAOMask;
  88. //Used to toggle the visualization of diffuse and specular light contribution
  89. static bool smDiffuseLightViz;
  90. static bool smSpecularLightViz;
  91. static bool smDetailLightingViz;
  92. static S32 smMaximumNumOfLights;
  93. static bool smUseLightFade;
  94. static F32 smLightFadeEnd;
  95. static F32 smLightFadeStart;
  96. static bool smAllowLocalLightShadows;
  97. // Used for console init
  98. AdvancedLightBinManager( AdvancedLightManager *lm = NULL,
  99. ShadowMapManager *sm = NULL,
  100. GFXFormat lightBufferFormat = GFXFormatR8G8B8A8 );
  101. virtual ~AdvancedLightBinManager();
  102. // ConsoleObject
  103. static void consoleInit();
  104. // RenderBinManager
  105. virtual void render(SceneRenderState *);
  106. virtual void clear() {}
  107. virtual void sort() {}
  108. // Add a light to the bins
  109. void addLight( LightInfo *light );
  110. // Clear all lights from the bins
  111. void clearAllLights();
  112. virtual bool setTargetSize(const Point2I &newTargetSize);
  113. /// Scores the registered lights for sorting/ordering purposes
  114. void _scoreLights(const MatrixF& cameraTrans);
  115. // ConsoleObject interface
  116. DECLARE_CONOBJECT(AdvancedLightBinManager);
  117. bool MRTLightmapsDuringDeferred() const { return mMRTLightmapsDuringDeferred; }
  118. void MRTLightmapsDuringDeferred(bool val);
  119. bool _updateTargets();
  120. typedef Signal<void(SceneRenderState *, AdvancedLightBinManager *)> RenderSignal;
  121. static RenderSignal &getRenderSignal();
  122. AdvancedLightManager *getManager() { return mLightManager; }
  123. protected:
  124. /// Frees all the currently allocated light materials.
  125. void _deleteLightMaterials();
  126. // Track a light material and associated data
  127. struct LightMaterialInfo
  128. {
  129. LightMatInstance *matInstance;
  130. // { zNear, zFar, 1/zNear, 1/zFar }
  131. MaterialParameterHandle *zNearFarInvNearFar;
  132. // Far frustum plane (World Space)
  133. MaterialParameterHandle *farPlane;
  134. // Far frustum plane (View Space)
  135. MaterialParameterHandle *vsFarPlane;
  136. // -dot( farPlane, eyePos )
  137. MaterialParameterHandle *negFarPlaneDotEye;
  138. // Light Parameters
  139. MaterialParameterHandle *lightPosition;
  140. MaterialParameterHandle *lightDirection;
  141. MaterialParameterHandle *lightColor;
  142. MaterialParameterHandle *lightBrightness;
  143. MaterialParameterHandle *lightRange;
  144. MaterialParameterHandle *lightInvSqrRange;
  145. MaterialParameterHandle *lightAmbient;
  146. MaterialParameterHandle *lightSpotParams;
  147. LightMaterialInfo( const String &matName,
  148. const GFXVertexFormat *vertexFormat,
  149. const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>() );
  150. virtual ~LightMaterialInfo();
  151. void setViewParameters( const F32 zNear,
  152. const F32 zFar,
  153. const Point3F &eyePos,
  154. const PlaneF &farPlane,
  155. const PlaneF &_vsFarPlane );
  156. void setLightParameters( const LightInfo *light, const SceneRenderState* renderState );
  157. };
  158. protected:
  159. struct LightBinEntry
  160. {
  161. LightInfo* lightInfo;
  162. LightShadowMap* shadowMap;
  163. LightMaterialInfo* lightMaterial;
  164. GFXPrimitiveBuffer* primBuffer;
  165. GFXVertexBuffer* vertBuffer;
  166. U32 numPrims;
  167. };
  168. Vector<LightBinEntry> mLightBin;
  169. typedef Vector<LightBinEntry>::iterator LightBinIterator;
  170. bool mMRTLightmapsDuringDeferred;
  171. /// Used in setupSGData to set the object transform.
  172. MatrixF mLightMat;
  173. U32 mNumLightsCulled;
  174. AdvancedLightManager *mLightManager;
  175. ShadowMapManager *mShadowManager;
  176. static const String smLightMatNames[LightInfo::Count];
  177. static const String smShadowTypeMacro[ShadowType_Count];
  178. static const GFXVertexFormat* smLightMatVertex[LightInfo::Count];
  179. typedef CompoundKey3<LightInfo::Type,ShadowType,bool> LightMatKey;
  180. typedef HashTable<LightMatKey,LightMaterialInfo*> LightMatTable;
  181. /// The fixed table of light material info.
  182. LightMatTable mLightMaterials;
  183. LightMaterialInfo* _getLightMaterial( LightInfo::Type lightType, ShadowType shadowType, bool useCookieTex );
  184. ///
  185. void _onShadowFilterChanged();
  186. typedef GFXVertexPNTT FarFrustumQuadVert;
  187. GFXVertexBufferHandle<FarFrustumQuadVert> mFarFrustumQuadVerts;
  188. //void _createMaterials();
  189. void _setupPerFrameParameters( const SceneRenderState *state );
  190. void setupSGData( SceneData &data, const SceneRenderState* state, LightInfo *light );
  191. static S32 QSORT_CALLBACK _lightScoreCmp(const LightBinEntry* a, const LightBinEntry* b);
  192. };
  193. #endif // _ADVANCEDLIGHTBINMANAGER_H_