ShadowMapping.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009-2021, 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. ANKI_USE_RESULT Error init(const ConfigSet& initializer);
  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. U32 m_tileResolution = 0; ///< Tile resolution.
  40. U32 m_tileCountBothAxis = 0;
  41. ShaderProgramResourcePtr m_resolveProg;
  42. ShaderProgramPtr m_resolveGrProg;
  43. WeakArray<ResolveWorkItem> m_resolveWorkItems;
  44. } m_atlas;
  45. ANKI_USE_RESULT Error initAtlas(const ConfigSet& cfg);
  46. inline Mat4 createSpotLightTextureMatrix(const UVec4& viewport) const;
  47. void runAtlas(RenderPassWorkContext& rgraphCtx);
  48. /// @}
  49. /// @name Scratch buffer stuff
  50. /// @{
  51. class Scratch
  52. {
  53. public:
  54. class WorkItem;
  55. class LightToRenderToScratchInfo;
  56. TileAllocator m_tileAlloc;
  57. RenderTargetHandle m_rt; ///< Size of the RT is (m_tileSize * m_tileCount, m_tileSize).
  58. FramebufferDescription m_fbDescr; ///< FB info.
  59. RenderTargetDescription m_rtDescr; ///< Render target.
  60. U32 m_tileCountX = 0;
  61. U32 m_tileCountY = 0;
  62. U32 m_tileResolution = 0;
  63. WeakArray<WorkItem> m_workItems;
  64. U32 m_maxViewportWidth = 0;
  65. U32 m_maxViewportHeight = 0;
  66. } m_scratch;
  67. ANKI_USE_RESULT Error initScratch(const ConfigSet& cfg);
  68. void runShadowMapping(RenderPassWorkContext& rgraphCtx);
  69. /// @}
  70. /// @name Misc & common
  71. /// @{
  72. static constexpr U32 m_pointLightsMaxLod = 1;
  73. Array<F32, MAX_LOD_COUNT - 1> m_lodDistances;
  74. /// Find the lod of the light
  75. void chooseLod(const Vec4& cameraOrigin, const PointLightQueueElement& light, Bool& blurAtlas, U32& tileBufferLod,
  76. U32& renderQueueElementsLod) const;
  77. /// Find the lod of the light
  78. void chooseLod(const Vec4& cameraOrigin, const SpotLightQueueElement& light, Bool& blurAtlas, U32& tileBufferLod,
  79. U32& renderQueueElementsLod) const;
  80. /// Try to allocate a number of scratch tiles and regular tiles.
  81. TileAllocatorResult allocateTilesAndScratchTiles(U64 lightUuid, U32 faceCount, const U64* faceTimestamps,
  82. const U32* faceIndices, const U32* drawcallsCount, const U32* lods,
  83. UVec4* atlasTileViewports, UVec4* scratchTileViewports,
  84. TileAllocatorResult* subResults);
  85. /// Add new work to render to scratch buffer and atlas buffer.
  86. void newScratchAndAtlasResloveRenderWorkItems(
  87. const UVec4& atlasViewport, const UVec4& scratchVewport, Bool blurAtlas, RenderQueue* lightRenderQueue,
  88. U32 renderQueueElementsLod, DynamicArrayAuto<Scratch::LightToRenderToScratchInfo>& scratchWorkItem,
  89. DynamicArrayAuto<Atlas::ResolveWorkItem>& atlasResolveWorkItem, U32& drawcallCount) const;
  90. /// Iterate lights and create work items.
  91. void processLights(RenderingContext& ctx, U32& threadCountForScratchPass);
  92. ANKI_USE_RESULT Error initInternal(const ConfigSet& config);
  93. /// @}
  94. };
  95. /// @}
  96. } // end namespace anki