RenderQueue.h 12 KB

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