ShadowMapping.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Renderer/RendererObject.h>
  7. #include <AnKi/Gr.h>
  8. #include <AnKi/Resource/ImageResource.h>
  9. #include <AnKi/Renderer/TileAllocator.h>
  10. namespace anki {
  11. /// @addtogroup renderer
  12. /// @{
  13. /// Shadowmapping pass
  14. class ShadowMapping : public RendererObject
  15. {
  16. public:
  17. ShadowMapping(Renderer* r)
  18. : RendererObject(r)
  19. {
  20. }
  21. ~ShadowMapping();
  22. Error init();
  23. /// Populate the rendergraph.
  24. void populateRenderGraph(RenderingContext& ctx);
  25. RenderTargetHandle getShadowmapRt() const
  26. {
  27. return m_atlas.m_rt;
  28. }
  29. private:
  30. /// @name Atlas stuff
  31. /// @{
  32. class Atlas
  33. {
  34. public:
  35. class ResolveWorkItem;
  36. TileAllocator m_tileAlloc;
  37. TexturePtr m_tex; ///< Size (m_tileResolution*m_tileCountBothAxis)^2
  38. RenderTargetHandle m_rt;
  39. Bool m_rtImportedOnce = false;
  40. U32 m_tileResolution = 0; ///< Tile resolution.
  41. U32 m_tileCountBothAxis = 0;
  42. ShaderProgramResourcePtr m_resolveProg;
  43. ShaderProgramPtr m_resolveGrProg;
  44. FramebufferDescription m_fbDescr;
  45. WeakArray<ResolveWorkItem> m_resolveWorkItems;
  46. } m_atlas;
  47. Error initAtlas();
  48. inline Mat4 createSpotLightTextureMatrix(const UVec4& viewport) const;
  49. void runAtlas(RenderPassWorkContext& rgraphCtx);
  50. /// @}
  51. /// @name Scratch buffer stuff
  52. /// @{
  53. class Scratch
  54. {
  55. public:
  56. class WorkItem;
  57. class LightToRenderToScratchInfo;
  58. TileAllocator m_tileAlloc;
  59. RenderTargetHandle m_rt; ///< Size of the RT is (m_tileSize * m_tileCount, m_tileSize).
  60. FramebufferDescription m_fbDescr; ///< FB info.
  61. RenderTargetDescription m_rtDescr; ///< Render target.
  62. U32 m_tileCountX = 0;
  63. U32 m_tileCountY = 0;
  64. U32 m_tileResolution = 0;
  65. WeakArray<WorkItem> m_workItems;
  66. U32 m_maxViewportWidth = 0;
  67. U32 m_maxViewportHeight = 0;
  68. } m_scratch;
  69. Error initScratch();
  70. void runShadowMapping(RenderPassWorkContext& rgraphCtx);
  71. /// @}
  72. /// @name Misc & common
  73. /// @{
  74. static constexpr U32 m_pointLightsMaxLod = 1;
  75. /// Find the lod of the light
  76. void chooseLod(const Vec4& cameraOrigin, const PointLightQueueElement& light, Bool& blurAtlas, U32& tileBufferLod,
  77. U32& renderQueueElementsLod) const;
  78. /// Find the lod of the light
  79. void chooseLod(const Vec4& cameraOrigin, const SpotLightQueueElement& light, Bool& blurAtlas, U32& tileBufferLod,
  80. U32& renderQueueElementsLod) const;
  81. /// Try to allocate a number of scratch tiles and regular tiles.
  82. TileAllocatorResult allocateTilesAndScratchTiles(U64 lightUuid, U32 faceCount, const U64* faceTimestamps,
  83. const U32* faceIndices, const U32* drawcallsCount, const U32* lods,
  84. UVec4* atlasTileViewports, UVec4* scratchTileViewports,
  85. TileAllocatorResult* subResults);
  86. /// Add new work to render to scratch buffer and atlas buffer.
  87. void newScratchAndAtlasResloveRenderWorkItems(
  88. const UVec4& atlasViewport, const UVec4& scratchVewport, Bool blurAtlas, RenderQueue* lightRenderQueue,
  89. U32 renderQueueElementsLod, DynamicArrayAuto<Scratch::LightToRenderToScratchInfo>& scratchWorkItem,
  90. DynamicArrayAuto<Atlas::ResolveWorkItem>& atlasResolveWorkItem, U32& drawcallCount) const;
  91. /// Iterate lights and create work items.
  92. void processLights(RenderingContext& ctx, U32& threadCountForScratchPass);
  93. Error initInternal();
  94. /// @}
  95. };
  96. /// @}
  97. } // end namespace anki