ShadowMapping.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (C) 2009-2020, 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/TextureResource.h>
  9. #include <anki/renderer/TileAllocator.h>
  10. namespace anki
  11. {
  12. /// @addtogroup renderer
  13. /// @{
  14. /// Shadowmapping pass
  15. class ShadowMapping : public RendererObject
  16. {
  17. public:
  18. ShadowMapping(Renderer* r)
  19. : RendererObject(r)
  20. {
  21. }
  22. ~ShadowMapping();
  23. ANKI_USE_RESULT Error init(const ConfigSet& initializer);
  24. /// Populate the rendergraph.
  25. void populateRenderGraph(RenderingContext& ctx);
  26. RenderTargetHandle getShadowmapRt() const
  27. {
  28. return m_atlas.m_rt;
  29. }
  30. private:
  31. using Viewport = Array<U32, 4>;
  32. /// @name Atlas stuff
  33. /// @{
  34. class Atlas
  35. {
  36. public:
  37. class ResolveWorkItem;
  38. TileAllocator m_tileAlloc;
  39. TexturePtr m_tex; ///< Size (m_tileResolution*m_tileCountBothAxis)^2
  40. RenderTargetHandle m_rt;
  41. U32 m_tileResolution = 0; ///< Tile resolution.
  42. U32 m_tileCountBothAxis = 0;
  43. ShaderProgramResourcePtr m_resolveProg;
  44. ShaderProgramPtr m_resolveGrProg;
  45. WeakArray<ResolveWorkItem> m_resolveWorkItems;
  46. } m_atlas;
  47. ANKI_USE_RESULT Error initAtlas(const ConfigSet& cfg);
  48. inline Mat4 createSpotLightTextureMatrix(const Viewport& 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. ANKI_USE_RESULT Error initScratch(const ConfigSet& cfg);
  70. void runShadowMapping(RenderPassWorkContext& rgraphCtx);
  71. /// @}
  72. /// @name Misc & common
  73. /// @{
  74. static const U32 m_lodCount = 3;
  75. static const U32 m_pointLightsMaxLod = 1;
  76. Array<F32, m_lodCount - 1> m_lodDistances;
  77. /// Find the lod of the light
  78. U32 choseLod(const Vec4& cameraOrigin, const PointLightQueueElement& light, Bool& blurAtlas) const;
  79. /// Find the lod of the light
  80. U32 choseLod(const Vec4& cameraOrigin, const SpotLightQueueElement& light, Bool& blurAtlas) const;
  81. /// Try to allocate a number of scratch tiles and regular tiles.
  82. TileAllocatorResult allocateTilesAndScratchTiles(U64 lightUuid,
  83. U32 faceCount,
  84. const U64* faceTimestamps,
  85. const U32* faceIndices,
  86. const U32* drawcallsCount,
  87. const U32* lods,
  88. Viewport* atlasTileViewports,
  89. Viewport* scratchTileViewports,
  90. TileAllocatorResult* subResults);
  91. /// Add new work to render to scratch buffer and atlas buffer.
  92. void newScratchAndAtlasResloveRenderWorkItems(const Viewport& atlasViewport,
  93. const Viewport& scratchVewport,
  94. Bool blurAtlas,
  95. RenderQueue* lightRenderQueue,
  96. DynamicArrayAuto<Scratch::LightToRenderToScratchInfo>& scratchWorkItem,
  97. DynamicArrayAuto<Atlas::ResolveWorkItem>& atlasResolveWorkItem,
  98. U32& drawcallCount) const;
  99. /// Iterate lights and create work items.
  100. void processLights(RenderingContext& ctx, U32& threadCountForScratchPass);
  101. ANKI_USE_RESULT Error initInternal(const ConfigSet& config);
  102. /// @}
  103. };
  104. /// @}
  105. } // end namespace anki