Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.2 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. #ifndef CSM_PARALLEL_SPLIT_H
  3. #define CSM_PARALLEL_SPLIT_H
  4. #ifdef _WIN32
  5. #pragma once
  6. #endif
  7. #include "mathlib/volumeculler.h"
  8. #include "mathlib/camera.h"
  9. namespace CCSMFrustumDefinition
  10. {
  11. enum { NUM_FRUSTUM_VERTS = 8 };
  12. enum { NUM_FRUSTUM_QUADS = 6 };
  13. enum { NUM_FRUSTUM_PLANES = NUM_FRUSTUM_QUADS };
  14. enum { NUM_FRUSTUM_EDGES = 12 };
  15. enum { NUM_FRUSTUM_LINES = 12 };
  16. extern Vector4D g_vProjFrustumVerts[NUM_FRUSTUM_VERTS];
  17. extern const uint8 g_nFrustumLineVertIndices[24];
  18. // The vertices of each frustum quad
  19. extern const uint8 g_nQuadVertIndices[NUM_FRUSTUM_QUADS][4];
  20. // The vertices of each frustum edge
  21. extern const uint8 g_nEdgeVertIndices[NUM_FRUSTUM_EDGES][2];
  22. // The quads sharing each frustum edge
  23. extern const uint8 g_nEdgeQuadIndices[NUM_FRUSTUM_EDGES][2];
  24. } // namespace CCSMFrustumDefinition
  25. enum
  26. {
  27. // Use caution changing this constant. There are a number of assumptions made about the max # of cascades being 4 (such as the manually laid out shader constants).
  28. MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE = 4
  29. };
  30. struct ShadowFrustaDebugInfo_t
  31. {
  32. uint m_nNumWorldFocusVerts;
  33. enum { MAX_WORLD_FOCUS_POINTS = 9 };
  34. Vector m_WorldFocusVerts[MAX_WORLD_FOCUS_POINTS];
  35. float m_flSplitPlaneDistance;
  36. float m_flLeft, m_flRight;
  37. float m_flTop, m_flBottom;
  38. float m_flZNear, m_flZFar;
  39. };
  40. struct SunLightState_t
  41. {
  42. uint m_nShadowCascadeSize;
  43. CFrustum m_CascadeFrustums[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ];
  44. CVolumeCuller m_CascadeVolumeCullers[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ];
  45. VMatrix m_CascadeProjToTexMatrices[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ];
  46. Rect_t m_CascadeViewports[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ];
  47. VPlane m_CascadeFrustumPlanes[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ][6];
  48. CascadedShadowMappingState_t m_SunLightShaderParams;
  49. ShadowFrustaDebugInfo_t m_DebugInfo[ MAX_SUN_LIGHT_SHADOW_CASCADE_SIZE ];
  50. float m_flActualMaxShadowDist;
  51. float m_flZLerpStartDist;
  52. float m_flZLerpEndDist;
  53. };
  54. struct SunLightViewState_t
  55. {
  56. Vector m_Direction;
  57. color32 m_LightColor;
  58. int m_LightColorScale;
  59. CFrustum m_frustum;
  60. int m_nMaxCascadeSize;
  61. int m_nAtlasFirstCascadeIndex;
  62. bool m_bSetAllCascadesToFirst;
  63. float m_flMaxVisibleDist;
  64. float m_flMaxShadowDist;
  65. int m_nCSMQualityLevel;
  66. };
  67. class CCSMParallelSplit : public CAlignedNewDelete<>
  68. {
  69. public:
  70. CCSMParallelSplit();
  71. ~CCSMParallelSplit();
  72. CCSMParallelSplit( const CCSMParallelSplit &other );
  73. const CCSMParallelSplit &operator==( const CCSMParallelSplit &rhs );
  74. void Init( uint nMaxShadowBufferSize, uint nMaxCascadeSize );
  75. void Clear();
  76. bool Update( const SunLightViewState_t &viewState );
  77. bool IsValid() const { return m_bValid; }
  78. inline void Reset() { m_bValid = false; }
  79. const SunLightViewState_t& GetViewState() const { return m_viewState; }
  80. SunLightViewState_t& GetViewState() { return m_viewState; }
  81. const SunLightState_t &GetLightState() const { return m_lightState; }
  82. SunLightState_t &GetLightState() { return m_lightState; }
  83. private:
  84. void CalculateShadowFrustaParallelSplits( const CFrustum &sceneFrustum, uint &nOutCascadeSize, CFrustum *pOutFrustums, CVolumeCuller *pOutVolumeCullers, float &flOutActualMaxShadowDist, int nMaxCascadeSize, Vector vLightDir, float flMaxShadowDistance, float flMaxVisibleDistance, int nCSMQualityLevel, ShadowFrustaDebugInfo_t* pDebugInfo );
  85. uint ComputeCullingVolumePlanes( VPlane *pOutPlanes, Vector vDirToLight, const CFrustum &sceneFrustum, float flMaxDistBetweenAnyCasterAndReceiver );
  86. void ComputeCascadeProjToTexMatrices( SunLightState_t &lightState );
  87. static float CalculateSplitPlaneDistance( int nSplit, int nShadowSplits, float flMaxShadowDistance, float flZNear, float flZFar );
  88. float ComputeFarPlaneCameraRelativePoints( Vector *pPoints, float flZNear, float flZFar, const CFrustum &frustum );
  89. bool m_bValid;
  90. uint m_nShadowBufferSize; // resolution of a single shadow map
  91. uint m_nShadowCascadeMaxSize; // cascade size (number of shadow maps)
  92. uint m_nShadowAtlasWidth; // actual shadow texture resolution
  93. uint m_nShadowAtlasHeight;
  94. SunLightViewState_t m_viewState;
  95. SunLightState_t m_lightState;
  96. };
  97. #endif //CSM_PARALLEL_SPLIT_H