Common.h 5.8 KB

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