RenderQueue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright (C) 2009-2020, 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. namespace anki
  11. {
  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
  54. RenderableQueueElement()
  55. {
  56. }
  57. };
  58. static_assert(std::is_trivially_destructible<RenderableQueueElement>::value == true,
  59. "Should be trivially destructible");
  60. /// Context that contains variables for the GenericGpuComputeJobQueueElement.
  61. class GenericGpuComputeJobQueueElementContext final : public RenderingMatrices
  62. {
  63. public:
  64. CommandBufferPtr m_commandBuffer;
  65. StagingGpuMemoryManager* m_stagingGpuAllocator ANKI_DEBUG_CODE(= nullptr);
  66. };
  67. /// Callback for GenericGpuComputeJobQueueElement.
  68. using GenericGpuComputeJobQueueElementCallback = void (*)(GenericGpuComputeJobQueueElementContext& ctx,
  69. const void* userData);
  70. /// It has enough info to execute generic compute on the GPU.
  71. class GenericGpuComputeJobQueueElement final
  72. {
  73. public:
  74. GenericGpuComputeJobQueueElementCallback m_callback;
  75. const void* m_userData;
  76. GenericGpuComputeJobQueueElement()
  77. {
  78. }
  79. };
  80. static_assert(std::is_trivially_destructible<GenericGpuComputeJobQueueElement>::value == true,
  81. "Should be trivially destructible");
  82. /// Point light render queue element.
  83. class PointLightQueueElement final
  84. {
  85. public:
  86. U64 m_uuid;
  87. Vec3 m_worldPosition;
  88. F32 m_radius;
  89. Vec3 m_diffuseColor;
  90. Array<RenderQueue*, 6> m_shadowRenderQueues;
  91. RenderQueueDrawCallback m_debugDrawCallback;
  92. const void* m_debugDrawCallbackUserData;
  93. Array<Vec2, 6> m_shadowAtlasTileOffsets; ///< Renderer internal.
  94. F32 m_shadowAtlasTileSize; ///< Renderer internal.
  95. PointLightQueueElement()
  96. {
  97. }
  98. Bool hasShadow() const
  99. {
  100. return m_shadowRenderQueues[0] != nullptr;
  101. }
  102. };
  103. static_assert(std::is_trivially_destructible<PointLightQueueElement>::value == true,
  104. "Should be trivially destructible");
  105. /// Spot light render queue element.
  106. class SpotLightQueueElement final
  107. {
  108. public:
  109. U64 m_uuid;
  110. Mat4 m_worldTransform;
  111. Mat4 m_textureMatrix;
  112. F32 m_distance;
  113. F32 m_outerAngle;
  114. F32 m_innerAngle;
  115. Vec3 m_diffuseColor;
  116. RenderQueue* m_shadowRenderQueue;
  117. RenderQueueDrawCallback m_debugDrawCallback;
  118. const void* m_debugDrawCallbackUserData;
  119. SpotLightQueueElement()
  120. {
  121. }
  122. Bool hasShadow() const
  123. {
  124. return m_shadowRenderQueue != nullptr;
  125. }
  126. };
  127. static_assert(std::is_trivially_destructible<SpotLightQueueElement>::value == true, "Should be trivially destructible");
  128. /// Directional light render queue element.
  129. class DirectionalLightQueueElement final
  130. {
  131. public:
  132. Array<Mat4, MAX_SHADOW_CASCADES> m_textureMatrices;
  133. Array<RenderQueue*, MAX_SHADOW_CASCADES> m_shadowRenderQueues;
  134. RenderQueueDrawCallback m_drawCallback;
  135. const void* m_drawCallbackUserData;
  136. U64 m_uuid; ///< Zero means that there is no dir light
  137. Vec3 m_diffuseColor;
  138. Vec3 m_direction;
  139. F32 m_effectiveShadowDistance;
  140. F32 m_shadowCascadesDistancePower;
  141. U8 m_shadowCascadeCount; ///< Zero means that it doesn't case any shadows
  142. DirectionalLightQueueElement()
  143. {
  144. }
  145. ANKI_USE_RESULT Bool isEnabled() const
  146. {
  147. return m_uuid != 0;
  148. }
  149. ANKI_USE_RESULT Bool hasShadow() const
  150. {
  151. return isEnabled() && m_shadowCascadeCount > 0;
  152. }
  153. };
  154. static_assert(std::is_trivially_destructible<DirectionalLightQueueElement>::value == true,
  155. "Should be trivially destructible");
  156. /// Normally the visibility tests don't perform tests on the reflection probes because probes dont change that often.
  157. /// This callback will be used by the renderer to inform a reflection probe that on the next frame it will be rendererd.
  158. /// In that case the visibility tests should fill the render queues of the probe.
  159. using ReflectionProbeQueueElementFeedbackCallback = void (*)(Bool fillRenderQueuesOnNextFrame, void* userData);
  160. /// Reflection probe render queue element.
  161. class ReflectionProbeQueueElement final
  162. {
  163. public:
  164. U64 m_uuid;
  165. ReflectionProbeQueueElementFeedbackCallback m_feedbackCallback;
  166. void* m_feedbackCallbackUserData;
  167. RenderQueueDrawCallback m_debugDrawCallback;
  168. const void* m_debugDrawCallbackUserData;
  169. Array<RenderQueue*, 6> m_renderQueues;
  170. Vec3 m_worldPosition;
  171. Vec3 m_aabbMin;
  172. Vec3 m_aabbMax;
  173. U32 m_textureArrayIndex; ///< Renderer internal.
  174. ReflectionProbeQueueElement()
  175. {
  176. }
  177. };
  178. static_assert(std::is_trivially_destructible<ReflectionProbeQueueElement>::value == true,
  179. "Should be trivially destructible");
  180. /// See ReflectionProbeQueueElementFeedbackCallback for its purpose.
  181. using GlobalIlluminationProbeQueueElementFeedbackCallback = void (*)(Bool fillRenderQueuesOnNextFrame, void* userData,
  182. const Vec4& eyeWorldPosition);
  183. // Probe for global illumination.
  184. class GlobalIlluminationProbeQueueElement final
  185. {
  186. public:
  187. U64 m_uuid;
  188. GlobalIlluminationProbeQueueElementFeedbackCallback m_feedbackCallback;
  189. void* m_feedbackCallbackUserData;
  190. RenderQueueDrawCallback m_debugDrawCallback;
  191. const void* m_debugDrawCallbackUserData;
  192. Array<RenderQueue*, 6> m_renderQueues;
  193. Vec3 m_aabbMin;
  194. Vec3 m_aabbMax;
  195. UVec3 m_cellCounts;
  196. U32 m_totalCellCount;
  197. Vec3 m_cellSizes; ///< The cells might not be cubes so have different sizes per dimension.
  198. F32 m_fadeDistance;
  199. GlobalIlluminationProbeQueueElement()
  200. {
  201. }
  202. Bool operator<(const GlobalIlluminationProbeQueueElement& b) const
  203. {
  204. if(m_cellSizes.x() != b.m_cellSizes.x())
  205. {
  206. return m_cellSizes.x() < b.m_cellSizes.x();
  207. }
  208. else
  209. {
  210. return m_totalCellCount < b.m_totalCellCount;
  211. }
  212. }
  213. };
  214. static_assert(std::is_trivially_destructible<GlobalIlluminationProbeQueueElement>::value == true,
  215. "Should be trivially destructible");
  216. /// Lens flare render queue element.
  217. class LensFlareQueueElement final
  218. {
  219. public:
  220. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  221. const TextureView* m_textureView;
  222. const void* m_userData;
  223. RenderQueueDrawCallback m_drawCallback;
  224. Vec3 m_worldPosition;
  225. Vec2 m_firstFlareSize;
  226. Vec4 m_colorMultiplier;
  227. LensFlareQueueElement()
  228. {
  229. }
  230. };
  231. static_assert(std::is_trivially_destructible<LensFlareQueueElement>::value == true, "Should be trivially destructible");
  232. /// Decal render queue element.
  233. class DecalQueueElement final
  234. {
  235. public:
  236. RenderQueueDrawCallback m_debugDrawCallback;
  237. const void* m_debugDrawCallbackUserData;
  238. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  239. TextureView* m_diffuseAtlas;
  240. /// Totaly unsafe but we can't have a smart ptr in here since there will be no deletion.
  241. TextureView* m_specularRoughnessAtlas;
  242. Vec4 m_diffuseAtlasUv;
  243. Vec4 m_specularRoughnessAtlasUv;
  244. F32 m_diffuseAtlasBlendFactor;
  245. F32 m_specularRoughnessAtlasBlendFactor;
  246. Mat4 m_textureMatrix;
  247. Vec3 m_obbCenter;
  248. Vec3 m_obbExtend;
  249. Mat3 m_obbRotation;
  250. DecalQueueElement()
  251. {
  252. }
  253. };
  254. static_assert(std::is_trivially_destructible<DecalQueueElement>::value == true, "Should be trivially destructible");
  255. /// Draw callback for drawing.
  256. using UiQueueDrawCallback = void (*)(CanvasPtr& canvas, void* userData);
  257. /// UI element render queue element.
  258. class UiQueueElement final
  259. {
  260. public:
  261. void* m_userData;
  262. UiQueueDrawCallback m_drawCallback;
  263. UiQueueElement()
  264. {
  265. }
  266. };
  267. static_assert(std::is_trivially_destructible<UiQueueElement>::value == true, "Should be trivially destructible");
  268. /// Fog density queue element.
  269. class FogDensityQueueElement final
  270. {
  271. public:
  272. union
  273. {
  274. Vec3 m_aabbMin;
  275. Vec3 m_sphereCenter;
  276. };
  277. union
  278. {
  279. Vec3 m_aabbMax;
  280. F32 m_sphereRadius;
  281. };
  282. F32 m_density;
  283. Bool m_isBox;
  284. FogDensityQueueElement()
  285. {
  286. }
  287. };
  288. static_assert(std::is_trivially_destructible<FogDensityQueueElement>::value == true,
  289. "Should be trivially destructible");
  290. /// A callback to fill a coverage buffer.
  291. using FillCoverageBufferCallback = void (*)(void* userData, F32* depthValues, U32 width, U32 height);
  292. /// The render queue. This is what the renderer is fed to render.
  293. class RenderQueue : public RenderingMatrices
  294. {
  295. public:
  296. WeakArray<RenderableQueueElement> m_renderables; ///< Deferred shading or shadow renderables.
  297. WeakArray<RenderableQueueElement> m_earlyZRenderables; ///< Some renderables that will be used for Early Z pass.
  298. WeakArray<RenderableQueueElement> m_forwardShadingRenderables;
  299. WeakArray<PointLightQueueElement> m_pointLights;
  300. WeakArray<PointLightQueueElement*> m_shadowPointLights; ///< Points to elements in m_pointLights.
  301. WeakArray<SpotLightQueueElement> m_spotLights;
  302. DirectionalLightQueueElement m_directionalLight;
  303. WeakArray<SpotLightQueueElement*> m_shadowSpotLights; ///< Points to elements in m_spotLights.
  304. WeakArray<ReflectionProbeQueueElement> m_reflectionProbes;
  305. WeakArray<GlobalIlluminationProbeQueueElement> m_giProbes;
  306. WeakArray<LensFlareQueueElement> m_lensFlares;
  307. WeakArray<DecalQueueElement> m_decals;
  308. WeakArray<FogDensityQueueElement> m_fogDensityVolumes;
  309. WeakArray<UiQueueElement> m_uis;
  310. WeakArray<GenericGpuComputeJobQueueElement> m_genericGpuComputeJobs;
  311. /// Applies only if the RenderQueue holds shadow casters. It's the max timesamp of all shadow casters
  312. Timestamp m_shadowRenderablesLastUpdateTimestamp = 0;
  313. F32 m_cameraNear;
  314. F32 m_cameraFar;
  315. F32 m_cameraFovX;
  316. F32 m_cameraFovY;
  317. F32 m_effectiveShadowDistance;
  318. FillCoverageBufferCallback m_fillCoverageBufferCallback = nullptr;
  319. void* m_fillCoverageBufferCallbackUserData = nullptr;
  320. RenderQueue()
  321. {
  322. zeroMemory(m_directionalLight);
  323. }
  324. PtrSize countAllRenderables() const;
  325. };
  326. static_assert(std::is_trivially_destructible<RenderQueue>::value == true, "Should be trivially destructible");
  327. /// @}
  328. } // end namespace anki