IndirectDiffuseClipmaps.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2009-present, 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/Renderer/Utils/TraditionalDeferredShading.h>
  8. #include <AnKi/Collision/Forward.h>
  9. namespace anki {
  10. /// @addtogroup renderer
  11. /// @{
  12. ANKI_CVAR(BoolCVar, Render, Idc, false, "Enable ray traced indirect diffuse clipmaps")
  13. constexpr U32 kDefaultClipmapProbeCountXZ = 32;
  14. constexpr U32 kDefaultClipmapProbeCountY = 12;
  15. constexpr F32 kDefaultClipmap0ProbeSize = 1.5f;
  16. constexpr F32 kDefaultClipmap1ProbeSize = 3.0f;
  17. constexpr F32 kDefaultClipmap2ProbeSize = 6.0f;
  18. constexpr U32 kDefaultRadianceOctMapSize = 10;
  19. constexpr U32 kDefaultRayCountPerTexelOfNewProbe = 4;
  20. /// As if you are updating 25% of the probes each frame.
  21. constexpr U32 kDefaultProbeRayBudget =
  22. (kIndirectDiffuseClipmapCount * square(kDefaultClipmapProbeCountXZ) * kDefaultClipmapProbeCountY * square(kDefaultRadianceOctMapSize)) * 25 / 100;
  23. ANKI_CVAR2(NumericCVar<U32>, Render, Idc, ProbesXZ, kDefaultClipmapProbeCountXZ, 10, 100, "The cell count of each dimension of 1st clipmap")
  24. ANKI_CVAR2(NumericCVar<U32>, Render, Idc, ProbesY, kDefaultClipmapProbeCountY, 4, 100, "The cell count of each dimension of 1st clipmap")
  25. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap0XZSize, F32(kDefaultClipmapProbeCountXZ) * kDefaultClipmap0ProbeSize, 10.0, 1000.0,
  26. "The clipmap size in meters")
  27. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap0YSize, F32(kDefaultClipmapProbeCountY) * kDefaultClipmap0ProbeSize, 10.0, 1000.0,
  28. "The clipmap size in meters")
  29. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap1XZSize, F32(kDefaultClipmapProbeCountXZ) * kDefaultClipmap1ProbeSize, 10.0, 1000.0,
  30. "The clipmap size in meters")
  31. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap1YSize, F32(kDefaultClipmapProbeCountY) * kDefaultClipmap1ProbeSize, 10.0, 1000.0,
  32. "The clipmap size in meters")
  33. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap2XZSize, F32(kDefaultClipmapProbeCountXZ) * kDefaultClipmap2ProbeSize, 10.0, 1000.0,
  34. "The clipmap size in meters")
  35. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, Clipmap2YSize, F32(kDefaultClipmapProbeCountY) * kDefaultClipmap2ProbeSize, 10.0, 1000.0,
  36. "The clipmap size in meters")
  37. ANKI_CVAR2(
  38. NumericCVar<U32>, Render, Idc, RadianceOctMapSize, kDefaultRadianceOctMapSize,
  39. [](U32 val) {
  40. return val >= 4 && val <= 30 && val % 2 == 0;
  41. },
  42. "Size of the octahedral for the light cache")
  43. ANKI_CVAR2(NumericCVar<U32>, Render, Idc, IrradianceOctMapSize, 5, 4, 20, "Size of the octahedral for the irradiance")
  44. ANKI_CVAR2(NumericCVar<F32>, Render, Idc, FirstBounceRayDistance, 0.0f, 0.0f, 10000.0f,
  45. "For the 1st bounce shoot rays instead of sampling the clipmaps")
  46. ANKI_CVAR2(BoolCVar, Render, Idc, ApplyHighQuality, false, "If true use 1/2 resolution else use 1/4")
  47. ANKI_CVAR2(NumericCVar<U8>, Render, Idc, RayCountPerTexelOfNewProbe, kDefaultRayCountPerTexelOfNewProbe, 1, 16,
  48. "The number of rays for a single texel of the oct map that will be cast for probes that are seen for the 1st time")
  49. ANKI_CVAR2(NumericCVar<U32>, Render, Idc, ProbeRayBudget, kDefaultProbeRayBudget, 1024, 100 * 1024 * 1024,
  50. "The number of rays for a single texel of the oct map that will be cast for probes that are seen for the 1st time")
  51. /// @memberof IndirectDiffuseClipmaps
  52. class IndirectDiffuseClipmapsRenderTargetHandles
  53. {
  54. public:
  55. RenderTargetHandle m_appliedIrradiance;
  56. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount> m_radianceVolumes;
  57. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount> m_irradianceVolumes;
  58. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount> m_distanceMomentsVolumes;
  59. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount> m_probeValidityVolumes;
  60. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount> m_avgIrradianceVolumes;
  61. };
  62. /// Indirect diffuse based on clipmaps of probes.
  63. class IndirectDiffuseClipmaps : public RtMaterialFetchRendererObject
  64. {
  65. public:
  66. IndirectDiffuseClipmaps()
  67. {
  68. registerDebugRenderTarget("IndirectDiffuseClipmapsTest");
  69. }
  70. Error init();
  71. void populateRenderGraph(RenderingContext& ctx);
  72. void getDebugRenderTarget([[maybe_unused]] CString rtName, Array<RenderTargetHandle, kMaxDebugRenderTargets>& handles,
  73. [[maybe_unused]] ShaderProgramPtr& optionalShaderProgram) const override
  74. {
  75. handles[0] = m_runCtx.m_handles.m_appliedIrradiance;
  76. }
  77. const IndirectDiffuseClipmapConstants& getClipmapConsts() const
  78. {
  79. return m_consts;
  80. }
  81. void drawDebugProbes(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx) const;
  82. const IndirectDiffuseClipmapsRenderTargetHandles& getRts() const
  83. {
  84. return m_runCtx.m_handles;
  85. }
  86. /// Output of IndirectDiffuseClipmaps is hidden and bindless so have this function to set dependencies
  87. void setDependencies(RenderPassBase& pass, TextureUsageBit usage) const
  88. {
  89. ANKI_ASSERT(!(usage & ~TextureUsageBit::kAllSrv) && "Only SRV allowed");
  90. // Cheat and only wait for the final RT. The rest will have been waited anyway
  91. pass.newTextureDependency(m_runCtx.m_handles.m_appliedIrradiance, usage);
  92. }
  93. private:
  94. Array<TexturePtr, kIndirectDiffuseClipmapCount> m_radianceVolumes;
  95. Array<TexturePtr, kIndirectDiffuseClipmapCount> m_irradianceVolumes;
  96. Array<TexturePtr, kIndirectDiffuseClipmapCount> m_distanceMomentsVolumes;
  97. Array<TexturePtr, kIndirectDiffuseClipmapCount> m_probeValidityVolumes;
  98. Array<TexturePtr, kIndirectDiffuseClipmapCount> m_avgIrradianceVolumes;
  99. RenderTargetDesc m_rtResultRtDesc;
  100. RenderTargetDesc m_lowRezRtDesc;
  101. RenderTargetDesc m_fullRtDesc;
  102. Array<TexturePtr, 2> m_irradianceRts;
  103. IndirectDiffuseClipmapConstants m_consts;
  104. ShaderProgramResourcePtr m_prog;
  105. ShaderProgramResourcePtr m_missProg;
  106. ShaderProgramPtr m_rtLibraryGrProg;
  107. ShaderProgramPtr m_populateCachesGrProg;
  108. ShaderProgramPtr m_computeIrradianceGrProg;
  109. ShaderProgramPtr m_applyGiGrProg;
  110. ShaderProgramPtr m_visProbesGrProg;
  111. ShaderProgramPtr m_temporalDenoiseGrProg;
  112. ShaderProgramPtr m_spatialReconstructGrProg;
  113. ShaderProgramPtr m_bilateralDenoiseGrProg;
  114. ImageResourcePtr m_blueNoiseImg;
  115. U32 m_sbtRecordSize = 0;
  116. Array<U32, 2> m_rayGenShaderGroupIndices = {kMaxU32, kMaxU32};
  117. U32 m_missShaderGroupIdx = kMaxU32;
  118. Bool m_texturesImportedOnce = false;
  119. class
  120. {
  121. public:
  122. IndirectDiffuseClipmapsRenderTargetHandles m_handles;
  123. } m_runCtx;
  124. };
  125. /// @}
  126. } // end namespace anki