Common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/Gr.h>
  7. #include <AnKi/Core/StagingGpuMemoryManager.h>
  8. #include <AnKi/Util/Ptr.h>
  9. #include <AnKi/Shaders/Include/Evsm.h>
  10. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  11. namespace anki
  12. {
  13. #define ANKI_R_LOGI(...) ANKI_LOG("R ", NORMAL, __VA_ARGS__)
  14. #define ANKI_R_LOGE(...) ANKI_LOG("R ", ERROR, __VA_ARGS__)
  15. #define ANKI_R_LOGW(...) ANKI_LOG("R ", WARNING, __VA_ARGS__)
  16. #define ANKI_R_LOGF(...) ANKI_LOG("R ", FATAL, __VA_ARGS__)
  17. // Forward
  18. class Renderer;
  19. class RendererObject;
  20. class GBuffer;
  21. class GBufferPost;
  22. class ShadowMapping;
  23. class LightShading;
  24. class ForwardShading;
  25. class LensFlare;
  26. class Ssao;
  27. class Tonemapping;
  28. class Bloom;
  29. class FinalComposite;
  30. class Dbg;
  31. class ProbeReflections;
  32. class DownscaleBlur;
  33. class VolumetricFog;
  34. class DepthDownscale;
  35. class TemporalAA;
  36. class UiStage;
  37. class Ssr;
  38. class Ssgi;
  39. class VolumetricLightingAccumulation;
  40. class GlobalIllumination;
  41. class GenericCompute;
  42. class ShadowmapsResolve;
  43. class RtShadows;
  44. class AccelerationStructureBuilder;
  45. class MotionVectors;
  46. class ClusterBinning;
  47. class DebugDrawer;
  48. class RenderQueue;
  49. class RenderableQueueElement;
  50. class PointLightQueueElement;
  51. class DirectionalLightQueueElement;
  52. class SpotLightQueueElement;
  53. class ReflectionProbeQueueElement;
  54. class DecalQueueElement;
  55. class ShaderProgramResourceVariant;
  56. /// @addtogroup renderer
  57. /// @{
  58. /// Don't create second level command buffers if they contain more drawcalls than this constant.
  59. constexpr U32 MIN_DRAWCALLS_PER_2ND_LEVEL_COMMAND_BUFFER = 16;
  60. /// SSAO size is rendererSize/SSAO_FRACTION.
  61. constexpr U32 SSAO_FRACTION = 2;
  62. /// Bloom size is rendererSize/BLOOM_FRACTION.
  63. constexpr U32 BLOOM_FRACTION = 4;
  64. /// Volumetric size is rendererSize/VOLUMETRIC_FRACTION.
  65. constexpr U32 VOLUMETRIC_FRACTION = 4;
  66. /// Used to calculate the mipmap count of the HiZ map.
  67. constexpr U32 HIERARCHICAL_Z_MIN_HEIGHT = 80;
  68. const TextureSubresourceInfo HIZ_HALF_DEPTH(TextureSurfaceInfo(0, 0, 0, 0));
  69. const TextureSubresourceInfo HIZ_QUARTER_DEPTH(TextureSurfaceInfo(1, 0, 0, 0));
  70. /// Computes the 'a' and 'b' numbers for linearizeDepthOptimal (see shaders)
  71. inline void computeLinearizeDepthOptimal(F32 near, F32 far, F32& a, F32& b)
  72. {
  73. a = (near - far) / near;
  74. b = far / near;
  75. }
  76. constexpr U32 GBUFFER_COLOR_ATTACHMENT_COUNT = 4;
  77. /// Downsample and blur down to a texture with size DOWNSCALE_BLUR_DOWN_TO
  78. constexpr U32 DOWNSCALE_BLUR_DOWN_TO = 32;
  79. /// Use this size of render target for the avg lum calculation.
  80. constexpr U32 AVERAGE_LUMINANCE_RENDER_TARGET_SIZE = 128;
  81. extern const Array<Format, GBUFFER_COLOR_ATTACHMENT_COUNT> GBUFFER_COLOR_ATTACHMENT_PIXEL_FORMATS;
  82. constexpr Format GBUFFER_DEPTH_ATTACHMENT_PIXEL_FORMAT = Format::D32_SFLOAT;
  83. constexpr Format LIGHT_SHADING_COLOR_ATTACHMENT_PIXEL_FORMAT = Format::B10G11R11_UFLOAT_PACK32;
  84. constexpr Format FORWARD_SHADING_COLOR_ATTACHMENT_PIXEL_FORMAT = Format::R16G16B16A16_SFLOAT;
  85. constexpr Format DBG_COLOR_ATTACHMENT_PIXEL_FORMAT = Format::R8G8B8A8_UNORM;
  86. constexpr Format SHADOW_DEPTH_PIXEL_FORMAT = Format::D32_SFLOAT;
  87. #if ANKI_EVSM4
  88. constexpr Format SHADOW_COLOR_PIXEL_FORMAT = Format::R32G32B32A32_SFLOAT;
  89. #else
  90. constexpr Format SHADOW_COLOR_PIXEL_FORMAT = Format::R32G32_SFLOAT;
  91. #endif
  92. /// GPU buffers and textures that the clusterer refers to.
  93. class ClusteredShadingContext
  94. {
  95. public:
  96. StagingGpuMemoryToken m_pointLightsToken;
  97. void* m_pointLightsAddress = nullptr;
  98. StagingGpuMemoryToken m_spotLightsToken;
  99. void* m_spotLightsAddress = nullptr;
  100. StagingGpuMemoryToken m_reflectionProbesToken;
  101. void* m_reflectionProbesAddress = nullptr;
  102. StagingGpuMemoryToken m_decalsToken;
  103. void* m_decalsAddress = nullptr;
  104. StagingGpuMemoryToken m_fogDensityVolumesToken;
  105. void* m_fogDensityVolumesAddress = nullptr;
  106. StagingGpuMemoryToken m_globalIlluminationProbesToken;
  107. void* m_globalIlluminationProbesAddress = nullptr;
  108. StagingGpuMemoryToken m_clusteredShadingUniformsToken;
  109. void* m_clusteredShadingUniformsAddress = nullptr;
  110. StagingGpuMemoryToken m_clustersToken;
  111. void* m_clustersAddress = nullptr;
  112. BufferHandle m_clustersBufferHandle; ///< To track dependencies. Don't track all tokens, not worth it.
  113. TextureViewPtr m_diffuseDecalTextureView;
  114. TextureViewPtr m_specularRoughnessDecalTextureView;
  115. };
  116. /// Rendering context.
  117. class RenderingContext
  118. {
  119. public:
  120. StackAllocator<U8> m_tempAllocator;
  121. RenderQueue* m_renderQueue = nullptr;
  122. RenderGraphDescription m_renderGraphDescr;
  123. CommonMatrices m_matrices;
  124. CommonMatrices m_prevMatrices;
  125. /// The render target that the Renderer will populate.
  126. RenderTargetHandle m_outRenderTarget;
  127. U32 m_outRenderTargetWidth = 0;
  128. U32 m_outRenderTargetHeight = 0;
  129. ClusteredShadingContext m_clusteredShading;
  130. RenderingContext(const StackAllocator<U8>& alloc)
  131. : m_tempAllocator(alloc)
  132. , m_renderGraphDescr(alloc)
  133. {
  134. }
  135. };
  136. /// A convenience function to find empty cache entries. Used for various probes.
  137. template<typename THashMap, typename TCacheEntryArray, typename TAlloc>
  138. U32 findBestCacheEntry(U64 uuid, Timestamp crntTimestamp, const TCacheEntryArray& entries, THashMap& map, TAlloc alloc)
  139. {
  140. ANKI_ASSERT(uuid > 0);
  141. // First, try to see if the UUID is in the cache
  142. auto it = map.find(uuid);
  143. if(ANKI_LIKELY(it != map.getEnd()))
  144. {
  145. const U32 cacheEntryIdx = *it;
  146. if(ANKI_LIKELY(entries[cacheEntryIdx].m_uuid == uuid))
  147. {
  148. // Found it
  149. return cacheEntryIdx;
  150. }
  151. else
  152. {
  153. // Cache entry is wrong, remove it
  154. map.erase(alloc, it);
  155. }
  156. }
  157. // 2nd and 3rd choice, find an empty entry or some entry to re-use
  158. U32 emptyCacheEntryIdx = MAX_U32;
  159. U32 cacheEntryIdxToKick = MAX_U32;
  160. Timestamp cacheEntryIdxToKickMinTimestamp = MAX_TIMESTAMP;
  161. for(U32 cacheEntryIdx = 0; cacheEntryIdx < entries.getSize(); ++cacheEntryIdx)
  162. {
  163. if(entries[cacheEntryIdx].m_uuid == 0)
  164. {
  165. // Found an empty
  166. emptyCacheEntryIdx = cacheEntryIdx;
  167. break;
  168. }
  169. else if(entries[cacheEntryIdx].m_lastUsedTimestamp != crntTimestamp
  170. && entries[cacheEntryIdx].m_lastUsedTimestamp < cacheEntryIdxToKickMinTimestamp)
  171. {
  172. // Found some with low timestamp
  173. cacheEntryIdxToKick = cacheEntryIdx;
  174. cacheEntryIdxToKickMinTimestamp = entries[cacheEntryIdx].m_lastUsedTimestamp;
  175. }
  176. }
  177. U32 outCacheEntryIdx;
  178. if(emptyCacheEntryIdx != MAX_U32)
  179. {
  180. outCacheEntryIdx = emptyCacheEntryIdx;
  181. }
  182. else if(cacheEntryIdxToKick != MAX_U32)
  183. {
  184. outCacheEntryIdx = cacheEntryIdxToKick;
  185. }
  186. else
  187. {
  188. // We are out of cache entries. Return OOM
  189. outCacheEntryIdx = MAX_U32;
  190. }
  191. return outCacheEntryIdx;
  192. }
  193. /// @}
  194. } // end namespace anki