Common.h 6.2 KB

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