advancedLightBinManager.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. StaticLightNonLMGeometry,
  57. StaticLightLMGeometry,
  58. NUM_LIT_STATES
  59. };
  60. GFXStateBlockRef mLitState[NUM_LIT_STATES];
  61. public:
  62. LightMatInstance(Material &mat) : Parent(mat), mLightMapParamsSC(NULL), mInternalPass(false) {}
  63. virtual bool init( const FeatureSet &features, const GFXVertexFormat *vertexFormat );
  64. virtual bool setupPass( SceneRenderState *state, const SceneData &sgData );
  65. };
  66. class AdvancedLightBinManager : public RenderTexTargetBinManager
  67. {
  68. typedef RenderTexTargetBinManager Parent;
  69. public:
  70. // Light info Render Inst Type
  71. static const RenderInstType RIT_LightInfo;
  72. // registered buffer name
  73. static const String smBufferName;
  74. /// The shadow filter mode to use on shadowed light materials.
  75. static ShadowFilterMode smShadowFilterMode;
  76. /// Used to toggle the PSSM debug rendering mode.
  77. static bool smPSSMDebugRender;
  78. /// Set by the SSAO post effect to tell the vector
  79. /// light to compile in the SSAO mask.
  80. static bool smUseSSAOMask;
  81. // Used for console init
  82. AdvancedLightBinManager( AdvancedLightManager *lm = NULL,
  83. ShadowMapManager *sm = NULL,
  84. GFXFormat lightBufferFormat = GFXFormatR8G8B8A8 );
  85. virtual ~AdvancedLightBinManager();
  86. // ConsoleObject
  87. static void consoleInit();
  88. // RenderBinManager
  89. virtual void render(SceneRenderState *);
  90. virtual void clear() {}
  91. virtual void sort() {}
  92. // Add a light to the bins
  93. void addLight( LightInfo *light );
  94. // Clear all lights from the bins
  95. void clearAllLights();
  96. virtual bool setTargetSize(const Point2I &newTargetSize);
  97. // ConsoleObject interface
  98. DECLARE_CONOBJECT(AdvancedLightBinManager);
  99. bool MRTLightmapsDuringPrePass() const { return mMRTLightmapsDuringPrePass; }
  100. void MRTLightmapsDuringPrePass(bool val);
  101. typedef Signal<void(SceneRenderState *, AdvancedLightBinManager *)> RenderSignal;
  102. static RenderSignal &getRenderSignal();
  103. AdvancedLightManager *getManager() { return mLightManager; }
  104. protected:
  105. /// Frees all the currently allocated light materials.
  106. void _deleteLightMaterials();
  107. // Track a light material and associated data
  108. struct LightMaterialInfo
  109. {
  110. LightMatInstance *matInstance;
  111. // { zNear, zFar, 1/zNear, 1/zFar }
  112. MaterialParameterHandle *zNearFarInvNearFar;
  113. // Far frustum plane (World Space)
  114. MaterialParameterHandle *farPlane;
  115. // Far frustum plane (View Space)
  116. MaterialParameterHandle *vsFarPlane;
  117. // -dot( farPlane, eyePos )
  118. MaterialParameterHandle *negFarPlaneDotEye;
  119. // Light Parameters
  120. MaterialParameterHandle *lightPosition;
  121. MaterialParameterHandle *lightDirection;
  122. MaterialParameterHandle *lightColor;
  123. MaterialParameterHandle *lightBrightness;
  124. MaterialParameterHandle *lightAttenuation;
  125. MaterialParameterHandle *lightRange;
  126. MaterialParameterHandle *lightAmbient;
  127. MaterialParameterHandle *lightTrilight;
  128. MaterialParameterHandle *lightSpotParams;
  129. LightMaterialInfo( const String &matName,
  130. const GFXVertexFormat *vertexFormat,
  131. const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>() );
  132. virtual ~LightMaterialInfo();
  133. void setViewParameters( const F32 zNear,
  134. const F32 zFar,
  135. const Point3F &eyePos,
  136. const PlaneF &farPlane,
  137. const PlaneF &_vsFarPlane );
  138. void setLightParameters( const LightInfo *light, const SceneRenderState* renderState, const MatrixF &worldViewOnly );
  139. };
  140. protected:
  141. struct LightBinEntry
  142. {
  143. LightInfo* lightInfo;
  144. LightShadowMap* shadowMap;
  145. LightShadowMap* dynamicShadowMap;
  146. LightMaterialInfo* lightMaterial;
  147. GFXPrimitiveBuffer* primBuffer;
  148. GFXVertexBuffer* vertBuffer;
  149. U32 numPrims;
  150. };
  151. Vector<LightBinEntry> mLightBin;
  152. typedef Vector<LightBinEntry>::iterator LightBinIterator;
  153. bool mMRTLightmapsDuringPrePass;
  154. /// Used in setupSGData to set the object transform.
  155. MatrixF mLightMat;
  156. U32 mNumLightsCulled;
  157. AdvancedLightManager *mLightManager;
  158. ShadowMapManager *mShadowManager;
  159. static const String smLightMatNames[LightInfo::Count];
  160. static const String smShadowTypeMacro[ShadowType_Count];
  161. static const GFXVertexFormat* smLightMatVertex[LightInfo::Count];
  162. typedef CompoundKey3<LightInfo::Type,ShadowType,bool> LightMatKey;
  163. typedef HashTable<LightMatKey,LightMaterialInfo*> LightMatTable;
  164. /// The fixed table of light material info.
  165. LightMatTable mLightMaterials;
  166. LightMaterialInfo* _getLightMaterial( LightInfo::Type lightType, ShadowType shadowType, bool useCookieTex );
  167. ///
  168. void _onShadowFilterChanged();
  169. AdvancedLightBufferConditioner *mConditioner;
  170. typedef GFXVertexPNTT FarFrustumQuadVert;
  171. GFXVertexBufferHandle<FarFrustumQuadVert> mFarFrustumQuadVerts;
  172. //void _createMaterials();
  173. void _setupPerFrameParameters( const SceneRenderState *state );
  174. void setupSGData( SceneData &data, const SceneRenderState* state, LightInfo *light );
  175. };
  176. #endif // _ADVANCEDLIGHTBINMANAGER_H_