2
0

RenderQueue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/Renderer/Common.h>
  7. #include <AnKi/Resource/RenderingKey.h>
  8. #include <AnKi/Ui/Canvas.h>
  9. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  10. #include <AnKi/Shaders/Include/ModelTypes.h>
  11. namespace anki {
  12. /// @addtogroup renderer
  13. /// @{
  14. class RenderingMatrices
  15. {
  16. public:
  17. Mat4 m_cameraTransform;
  18. Mat4 m_viewMatrix;
  19. Mat4 m_projectionMatrix;
  20. Mat4 m_viewProjectionMatrix;
  21. Mat4 m_previousViewProjectionMatrix;
  22. };
  23. /// Some options that can be used as hints in debug drawcalls.
  24. enum class RenderQueueDebugDrawFlag : U32
  25. {
  26. DEPTH_TEST_ON,
  27. DITHERED_DEPTH_TEST_ON,
  28. COUNT
  29. };
  30. /// Context that contains variables for drawing and will be passed to RenderQueueDrawCallback.
  31. class RenderQueueDrawContext final : public RenderingMatrices
  32. {
  33. public:
  34. RenderingKey m_key;
  35. CommandBufferPtr m_commandBuffer;
  36. SamplerPtr m_sampler; ///< A trilinear sampler with anisotropy.
  37. StagingGpuMemoryManager* m_stagingGpuAllocator ANKI_DEBUG_CODE(= nullptr);
  38. StackAllocator<U8> m_frameAllocator;
  39. Bool m_debugDraw; ///< If true the drawcall should be drawing some kind of debug mesh.
  40. BitSet<U(RenderQueueDebugDrawFlag::COUNT), U32> m_debugDrawFlags = {false};
  41. };
  42. /// Draw callback for drawing.
  43. using RenderQueueDrawCallback = void (*)(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData);
  44. /// Render queue element that contains info on items that populate the G-buffer or the forward shading buffer etc.
  45. class RenderableQueueElement final
  46. {
  47. public:
  48. RenderQueueDrawCallback m_callback;
  49. const void* m_userData;
  50. /// Elements with the same m_mergeKey and same m_callback may be merged and the m_callback will be called once.
  51. /// Unless m_mergeKey is zero.
  52. U64 m_mergeKey;
  53. F32 m_distanceFromCamera; ///< Don't set this. Visibility will.
  54. U8 m_lod; ///< Don't set this. Visibility will.
  55. RenderableQueueElement()
  56. {
  57. }
  58. };
  59. static_assert(std::is_trivially_destructible<RenderableQueueElement>::value == true,
  60. "Should be trivially destructible");
  61. /// Context that contains variables for the GenericGpuComputeJobQueueElement.
  62. class GenericGpuComputeJobQueueElementContext final : public RenderingMatrices
  63. {
  64. public:
  65. CommandBufferPtr m_commandBuffer;
  66. StagingGpuMemoryManager* m_stagingGpuAllocator ANKI_DEBUG_CODE(= nullptr);
  67. };
  68. /// Callback for GenericGpuComputeJobQueueElement.
  69. using GenericGpuComputeJobQueueElementCallback = void (*)(GenericGpuComputeJobQueueElementContext& ctx,
  70. const void* userData);
  71. /// It has enough info to execute generic compute on the GPU.
  72. class GenericGpuComputeJobQueueElement final
  73. {
  74. public:
  75. GenericGpuComputeJobQueueElementCallback m_callback;
  76. const void* m_userData;
  77. GenericGpuComputeJobQueueElement()
  78. {
  79. }
  80. };
  81. static_assert(std::is_trivially_destructible<GenericGpuComputeJobQueueElement>::value == true,
  82. "Should be trivially destructible");
  83. /// Point light render queue element.
  84. class PointLightQueueElement final
  85. {
  86. public:
  87. U64 m_uuid;
  88. Vec3 m_worldPosition;
  89. F32 m_radius;
  90. Vec3 m_diffuseColor;
  91. Array<RenderQueue*, 6> m_shadowRenderQueues;
  92. RenderQueueDrawCallback m_debugDrawCallback;
  93. const void* m_debugDrawCallbackUserData;
  94. Array<Vec2, 6> m_shadowAtlasTileOffsets; ///< Renderer internal.
  95. F32 m_shadowAtlasTileSize; ///< Renderer internal.
  96. U8 m_shadowLayer; ///< Renderer internal.
  97. PointLightQueueElement()
  98. {
  99. }
  100. Bool hasShadow() const
  101. {
  102. return m_shadowRenderQueues[0] != nullptr;
  103. }
  104. };
  105. static_assert(std::is_trivially_destructible<PointLightQueueElement>::value == true,
  106. "Should be trivially destructible");
  107. /// Spot light render queue element.
  108. class SpotLightQueueElement final
  109. {
  110. public:
  111. U64 m_uuid;
  112. Mat4 m_worldTransform;
  113. Mat4 m_textureMatrix;
  114. F32 m_distance;
  115. F32 m_outerAngle;
  116. F32 m_innerAngle;
  117. Vec3 m_diffuseColor;
  118. Array<Vec3, 4> m_edgePoints;
  119. RenderQueue* m_shadowRenderQueue;
  120. RenderQueueDrawCallback m_debugDrawCallback;
  121. const void* m_debugDrawCallbackUserData;
  122. U8 m_shadowLayer; ///< Renderer internal.
  123. SpotLightQueueElement()
  124. {
  125. }
  126. Bool hasShadow() const
  127. {
  128. return m_shadowRenderQueue != nullptr;
  129. }
  130. };
  131. static_assert(std::is_trivially_destructible<SpotLightQueueElement>::value == true, "Should be trivially destructible");
  132. /// Directional light render queue element.
  133. class DirectionalLightQueueElement final
  134. {
  135. public:
  136. Array<Mat4, MAX_SHADOW_CASCADES2> m_textureMatrices;
  137. Array<RenderQueue*, MAX_SHADOW_CASCADES2> m_shadowRenderQueues;
  138. RenderQueueDrawCallback m_drawCallback;
  139. const void* m_drawCallbackUserData;
  140. U64 m_uuid; ///< Zero means that there is no dir light
  141. Vec3 m_diffuseColor;
  142. Vec3 m_direction;
  143. F32 m_effectiveShadowDistance;
  144. F32 m_shadowCascadesDistancePower;
  145. U8 m_shadowCascadeCount; ///< Zero means that it doesn't case any shadows
  146. U8 m_shadowLayer; ///< Renderer internal.
  147. DirectionalLightQueueElement()
  148. {
  149. }
  150. ANKI_USE_RESULT Bool isEnabled() const
  151. {
  152. return m_uuid != 0;
  153. }
  154. ANKI_USE_RESULT Bool hasShadow() const
  155. {
  156. return isEnabled() && m_shadowCascadeCount > 0;
  157. }
  158. };
  159. static_assert(std::is_trivially_destructible<DirectionalLightQueueElement>::value == true,
  160. "Should be trivially destructible");
  161. /// Normally the visibility tests don't perform tests on the reflection probes because probes dont change that often.
  162. /// This callback will be used by the renderer to inform a reflection probe that on the next frame it will be rendererd.
  163. /// In that case the visibility tests should fill the render queues of the probe.
  164. using ReflectionProbeQueueElementFeedbackCallback = void (*)(Bool fillRenderQueuesOnNextFrame, void* userData);
  165. /// Reflection probe render queue element.
  166. class ReflectionProbeQueueElement final
  167. {
  168. public:
  169. U64 m_uuid;
  170. ReflectionProbeQueueElementFeedbackCallback m_feedbackCallback;
  171. void* m_feedbackCallbackUserData;
  172. RenderQueueDrawCallback m_debugDrawCallback;
  173. const void* m_debugDrawCallbackUserData;
  174. Array<RenderQueue*, 6> m_renderQueues;
  175. Vec3 m_worldPosition;
  176. Vec3 m_aabbMin;
  177. Vec3 m_aabbMax;
  178. U32 m_textureArrayIndex; ///< Renderer internal.
  179. ReflectionProbeQueueElement()
  180. {
  181. }
  182. };
  183. static_assert(std::is_trivially_destructible<ReflectionProbeQueueElement>::value == true,
  184. "Should be trivially destructible");
  185. /// See ReflectionProbeQueueElementFeedbackCallback for its purpose.
  186. using GlobalIlluminationProbeQueueElementFeedbackCallback = void (*)(Bool fillRenderQueuesOnNextFrame, void* userData,
  187. const Vec4& eyeWorldPosition);
  188. // Probe for global illumination.
  189. class GlobalIlluminationProbeQueueElement final
  190. {
  191. public:
  192. U64 m_uuid;
  193. GlobalIlluminationProbeQueueElementFeedbackCallback m_feedbackCallback;
  194. void* m_feedbackCallbackUserData;
  195. RenderQueueDrawCallback m_debugDrawCallback;
  196. const void* m_debugDrawCallbackUserData;
  197. Array<RenderQueue*, 6> m_renderQueues;
  198. Vec3 m_aabbMin;
  199. Vec3 m_aabbMax;
  200. UVec3 m_cellCounts;
  201. U32 m_totalCellCount;
  202. Vec3 m_cellSizes; ///< The cells might not be cubes so have different sizes per dimension.
  203. F32 m_fadeDistance;
  204. GlobalIlluminationProbeQueueElement()
  205. {
  206. }
  207. Bool operator<(const GlobalIlluminationProbeQueueElement& b) const
  208. {
  209. if(m_cellSizes.x() != b.m_cellSizes.x())
  210. {
  211. return m_cellSizes.x() < b.m_cellSizes.x();
  212. }
  213. else
  214. {
  215. return m_totalCellCount < b.m_totalCellCount;
  216. }
  217. }
  218. };
  219. static_assert(std::is_trivially_destructible<GlobalIlluminationProbeQueueElement>::value == true,
  220. "Should be trivially destructible");
  221. /// Lens flare render queue element.
  222. class LensFlareQueueElement final
  223. {
  224. public:
  225. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  226. const TextureView* m_textureView;
  227. const void* m_userData;
  228. RenderQueueDrawCallback m_drawCallback;
  229. Vec3 m_worldPosition;
  230. Vec2 m_firstFlareSize;
  231. Vec4 m_colorMultiplier;
  232. LensFlareQueueElement()
  233. {
  234. }
  235. };
  236. static_assert(std::is_trivially_destructible<LensFlareQueueElement>::value == true, "Should be trivially destructible");
  237. /// Decal render queue element.
  238. class DecalQueueElement final
  239. {
  240. public:
  241. RenderQueueDrawCallback m_debugDrawCallback;
  242. const void* m_debugDrawCallbackUserData;
  243. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  244. TextureView* m_diffuseAtlas;
  245. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  246. TextureView* m_specularRoughnessAtlas;
  247. Vec4 m_diffuseAtlasUv;
  248. Vec4 m_specularRoughnessAtlasUv;
  249. F32 m_diffuseAtlasBlendFactor;
  250. F32 m_specularRoughnessAtlasBlendFactor;
  251. Mat4 m_textureMatrix;
  252. Vec3 m_obbCenter;
  253. Vec3 m_obbExtend;
  254. Mat3 m_obbRotation;
  255. DecalQueueElement()
  256. {
  257. }
  258. };
  259. static_assert(std::is_trivially_destructible<DecalQueueElement>::value == true, "Should be trivially destructible");
  260. /// Draw callback for drawing.
  261. using UiQueueElementDrawCallback = void (*)(CanvasPtr& canvas, void* userData);
  262. /// UI element render queue element.
  263. class UiQueueElement final
  264. {
  265. public:
  266. void* m_userData;
  267. UiQueueElementDrawCallback m_drawCallback;
  268. UiQueueElement()
  269. {
  270. }
  271. };
  272. static_assert(std::is_trivially_destructible<UiQueueElement>::value == true, "Should be trivially destructible");
  273. /// Fog density queue element.
  274. class FogDensityQueueElement final
  275. {
  276. public:
  277. union
  278. {
  279. Vec3 m_aabbMin;
  280. Vec3 m_sphereCenter;
  281. };
  282. union
  283. {
  284. Vec3 m_aabbMax;
  285. F32 m_sphereRadius;
  286. };
  287. F32 m_density;
  288. Bool m_isBox;
  289. FogDensityQueueElement()
  290. {
  291. }
  292. };
  293. static_assert(std::is_trivially_destructible<FogDensityQueueElement>::value == true,
  294. "Should be trivially destructible");
  295. /// A callback to fill a coverage buffer.
  296. using FillCoverageBufferCallback = void (*)(void* userData, F32* depthValues, U32 width, U32 height);
  297. /// Ray tracing queue element.
  298. class RayTracingInstanceQueueElement final
  299. {
  300. public:
  301. AccelerationStructure* m_bottomLevelAccelerationStructure;
  302. ModelGpuDescriptor m_modelDescriptor;
  303. Array<U32, U(RayType::COUNT)> m_shaderGroupHandleIndices;
  304. /// This points to the GR objects that are m_modelDescriptor is referencing. Use this to add a refcount to avoid
  305. /// accidential deletions.
  306. Array<GrObject*, 8> m_grObjects;
  307. U32 m_grObjectCount;
  308. };
  309. static_assert(std::is_trivially_destructible<RayTracingInstanceQueueElement>::value == true,
  310. "Should be trivially destructible");
  311. /// The render queue. This is what the renderer is fed to render.
  312. class RenderQueue : public RenderingMatrices
  313. {
  314. public:
  315. WeakArray<RenderableQueueElement> m_renderables; ///< Deferred shading or shadow renderables.
  316. WeakArray<RenderableQueueElement> m_earlyZRenderables; ///< Some renderables that will be used for Early Z pass.
  317. WeakArray<RenderableQueueElement> m_forwardShadingRenderables;
  318. WeakArray<PointLightQueueElement> m_pointLights; ///< Those who cast shadows are first.
  319. WeakArray<SpotLightQueueElement> m_spotLights; ///< Those who cast shadows are first.
  320. DirectionalLightQueueElement m_directionalLight;
  321. WeakArray<ReflectionProbeQueueElement> m_reflectionProbes;
  322. WeakArray<GlobalIlluminationProbeQueueElement> m_giProbes;
  323. WeakArray<LensFlareQueueElement> m_lensFlares;
  324. WeakArray<DecalQueueElement> m_decals;
  325. WeakArray<FogDensityQueueElement> m_fogDensityVolumes;
  326. WeakArray<UiQueueElement> m_uis;
  327. WeakArray<GenericGpuComputeJobQueueElement> m_genericGpuComputeJobs;
  328. WeakArray<RayTracingInstanceQueueElement> m_rayTracingInstances;
  329. /// Contains the ray tracing elements. The rest of the members are unused. It's separate to avoid multithreading
  330. /// bugs.
  331. RenderQueue* m_rayTracingQueue = nullptr;
  332. /// Applies only if the RenderQueue holds shadow casters. It's the max timesamp of all shadow casters
  333. Timestamp m_shadowRenderablesLastUpdateTimestamp = 0;
  334. F32 m_cameraNear;
  335. F32 m_cameraFar;
  336. F32 m_cameraFovX;
  337. F32 m_cameraFovY;
  338. F32 m_effectiveShadowDistance;
  339. FillCoverageBufferCallback m_fillCoverageBufferCallback = nullptr;
  340. void* m_fillCoverageBufferCallbackUserData = nullptr;
  341. RenderQueue()
  342. {
  343. zeroMemory(m_directionalLight);
  344. }
  345. PtrSize countAllRenderables() const;
  346. };
  347. static_assert(std::is_trivially_destructible<RenderQueue>::value == true, "Should be trivially destructible");
  348. /// @}
  349. } // end namespace anki