FrustumComponent.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Util/BitMask.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. #include <AnKi/Collision/Obb.h>
  10. #include <AnKi/Collision/ConvexHullShape.h>
  11. #include <AnKi/Collision/Plane.h>
  12. #include <AnKi/Resource/Common.h>
  13. #include <AnKi/Shaders/Include/ClusteredShadingFunctions.h>
  14. namespace anki
  15. {
  16. /// @addtogroup scene
  17. /// @{
  18. /// Flags that affect visibility tests.
  19. enum class FrustumComponentVisibilityTestFlag : U32
  20. {
  21. NONE = 0,
  22. RENDER_COMPONENTS = 1 << 0,
  23. LIGHT_COMPONENTS = 1 << 1,
  24. LENS_FLARE_COMPONENTS = 1 << 2,
  25. SHADOW_CASTERS = 1 << 3, ///< Render components that cast shadow
  26. POINT_LIGHT_SHADOWS_ENABLED = 1 << 4,
  27. SPOT_LIGHT_SHADOWS_ENABLED = 1 << 5,
  28. DIRECTIONAL_LIGHT_SHADOWS_ALL_CASCADES = 1 << 6,
  29. DIRECTIONAL_LIGHT_SHADOWS_1_CASCADE = 1 << 7,
  30. REFLECTION_PROBES = 1 << 8,
  31. REFLECTION_PROXIES = 1 << 9,
  32. OCCLUDERS = 1 << 10,
  33. DECALS = 1 << 11,
  34. FOG_DENSITY_COMPONENTS = 1 << 12,
  35. GLOBAL_ILLUMINATION_PROBES = 1 << 13,
  36. EARLY_Z = 1 << 14,
  37. GENERIC_COMPUTE_JOB_COMPONENTS = 1 << 15,
  38. RAY_TRACING_SHADOWS = 1 << 16,
  39. RAY_TRACING_GI = 1 << 17,
  40. RAY_TRACING_REFLECTIONS = 1 << 18,
  41. RAY_TRACING_PATH_TRACING = 1 << 19,
  42. UI_COMPONENTS = 1 << 20,
  43. ALL = RENDER_COMPONENTS | LIGHT_COMPONENTS | LENS_FLARE_COMPONENTS | SHADOW_CASTERS | POINT_LIGHT_SHADOWS_ENABLED
  44. | SPOT_LIGHT_SHADOWS_ENABLED | DIRECTIONAL_LIGHT_SHADOWS_ALL_CASCADES | DIRECTIONAL_LIGHT_SHADOWS_1_CASCADE
  45. | REFLECTION_PROBES | REFLECTION_PROXIES | OCCLUDERS | DECALS | FOG_DENSITY_COMPONENTS
  46. | GLOBAL_ILLUMINATION_PROBES | EARLY_Z | GENERIC_COMPUTE_JOB_COMPONENTS | RAY_TRACING_SHADOWS | RAY_TRACING_GI
  47. | RAY_TRACING_REFLECTIONS | RAY_TRACING_PATH_TRACING | UI_COMPONENTS,
  48. ALL_SHADOWS_ENABLED =
  49. POINT_LIGHT_SHADOWS_ENABLED | SPOT_LIGHT_SHADOWS_ENABLED | DIRECTIONAL_LIGHT_SHADOWS_ALL_CASCADES,
  50. ALL_RAY_TRACING = RAY_TRACING_SHADOWS | RAY_TRACING_GI | RAY_TRACING_REFLECTIONS | RAY_TRACING_PATH_TRACING
  51. };
  52. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(FrustumComponentVisibilityTestFlag)
  53. /// Frustum component. Useful for nodes that take part in visibility tests like cameras and lights.
  54. class FrustumComponent : public SceneComponent
  55. {
  56. ANKI_SCENE_COMPONENT(FrustumComponent)
  57. public:
  58. FrustumComponent(SceneNode* node);
  59. ~FrustumComponent();
  60. SceneNode& getSceneNode()
  61. {
  62. return *m_node;
  63. }
  64. const SceneNode& getSceneNode() const
  65. {
  66. return *m_node;
  67. }
  68. void setFrustumType(FrustumType type)
  69. {
  70. ANKI_ASSERT(type >= FrustumType::FIRST && type < FrustumType::COUNT);
  71. m_frustumType = type;
  72. if(m_frustumType == FrustumType::PERSPECTIVE)
  73. {
  74. setPerspective(0.1f, 100.0f, toRad(45.0f), toRad(45.0f));
  75. }
  76. else
  77. {
  78. setOrthographic(0.1f, 100.0f, 5.0f, -5.0f, 5.0f, -5.0f);
  79. }
  80. }
  81. FrustumType getFrustumType() const
  82. {
  83. ANKI_ASSERT(m_frustumType != FrustumType::COUNT);
  84. return m_frustumType;
  85. }
  86. void setPerspective(F32 near, F32 far, F32 fovX, F32 fovY)
  87. {
  88. ANKI_ASSERT(near > 0.0f && far > 0.0f && near < far);
  89. ANKI_ASSERT(fovX > 0.0f && fovY > 0.0f && fovX < PI && fovY < PI);
  90. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  91. m_perspective.m_near = near;
  92. m_perspective.m_far = far;
  93. m_perspective.m_fovX = fovX;
  94. m_perspective.m_fovY = fovY;
  95. m_shapeMarkedForUpdate = true;
  96. }
  97. void setOrthographic(F32 near, F32 far, F32 right, F32 left, F32 top, F32 bottom)
  98. {
  99. ANKI_ASSERT(near > 0.0f && far > 0.0f && near < far);
  100. ANKI_ASSERT(right > left && top > bottom);
  101. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  102. m_ortho.m_near = near;
  103. m_ortho.m_far = far;
  104. m_ortho.m_right = right;
  105. m_ortho.m_left = left;
  106. m_ortho.m_top = top;
  107. m_ortho.m_bottom = bottom;
  108. m_shapeMarkedForUpdate = true;
  109. }
  110. void setNear(F32 near)
  111. {
  112. m_common.m_near = near;
  113. m_shapeMarkedForUpdate = true;
  114. }
  115. F32 getNear() const
  116. {
  117. return m_common.m_near;
  118. }
  119. void setFar(F32 far)
  120. {
  121. m_common.m_far = far;
  122. m_shapeMarkedForUpdate = true;
  123. }
  124. F32 getFar() const
  125. {
  126. return m_common.m_far;
  127. }
  128. void setFovX(F32 fovx)
  129. {
  130. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  131. m_shapeMarkedForUpdate = true;
  132. m_perspective.m_fovX = fovx;
  133. }
  134. F32 getFovX() const
  135. {
  136. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  137. return m_perspective.m_fovX;
  138. }
  139. void setFovY(F32 fovy)
  140. {
  141. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  142. m_shapeMarkedForUpdate = true;
  143. m_perspective.m_fovY = fovy;
  144. }
  145. F32 getFovY() const
  146. {
  147. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  148. return m_perspective.m_fovY;
  149. }
  150. F32 getLeft() const
  151. {
  152. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  153. return m_ortho.m_left;
  154. }
  155. void setLeft(F32 value)
  156. {
  157. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  158. m_ortho.m_left = value;
  159. }
  160. F32 getRight() const
  161. {
  162. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  163. return m_ortho.m_right;
  164. }
  165. void setRight(F32 value)
  166. {
  167. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  168. m_ortho.m_right = value;
  169. }
  170. F32 getTop() const
  171. {
  172. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  173. return m_ortho.m_top;
  174. }
  175. void setTop(F32 value)
  176. {
  177. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  178. m_ortho.m_top = value;
  179. }
  180. F32 getBottom() const
  181. {
  182. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  183. return m_ortho.m_bottom;
  184. }
  185. void setBottom(F32 value)
  186. {
  187. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  188. m_ortho.m_bottom = value;
  189. }
  190. const Transform& getWorldTransform() const
  191. {
  192. return m_trf;
  193. }
  194. void setWorldTransform(const Transform& trf)
  195. {
  196. m_trf = trf;
  197. m_trfMarkedForUpdate = true;
  198. }
  199. const Mat4& getProjectionMatrix() const
  200. {
  201. return m_projMat;
  202. }
  203. const Mat4& getViewMatrix() const
  204. {
  205. return m_viewMat;
  206. }
  207. const Mat4& getViewProjectionMatrix() const
  208. {
  209. return m_viewProjMat;
  210. }
  211. const Mat4& getPreviousViewProjectionMatrix() const
  212. {
  213. return m_prevViewProjMat;
  214. }
  215. /// Check if a shape is inside the frustum.
  216. template<typename T>
  217. Bool insideFrustum(const T& t) const
  218. {
  219. for(const Plane& plane : m_viewPlanesW)
  220. {
  221. if(testPlane(plane, t) < 0.0f)
  222. {
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  229. {
  230. ANKI_ASSERT(&node == m_node);
  231. updated = updateInternal();
  232. return Error::NONE;
  233. }
  234. void setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag bits);
  235. FrustumComponentVisibilityTestFlag getEnabledVisibilityTests() const
  236. {
  237. return m_flags;
  238. }
  239. /// The type is FillCoverageBufferCallback.
  240. static void fillCoverageBufferCallback(void* userData, F32* depthValues, U32 width, U32 height);
  241. Bool hasCoverageBuffer() const
  242. {
  243. return m_coverageBuff.m_depthMap.getSize() > 0;
  244. }
  245. void getCoverageBufferInfo(ConstWeakArray<F32>& depthBuff, U32& width, U32& height) const
  246. {
  247. if(m_coverageBuff.m_depthMap.getSize() > 0)
  248. {
  249. depthBuff = ConstWeakArray<F32>(&m_coverageBuff.m_depthMap[0], m_coverageBuff.m_depthMap.getSize());
  250. width = m_coverageBuff.m_depthMapWidth;
  251. height = m_coverageBuff.m_depthMapHeight;
  252. }
  253. else
  254. {
  255. depthBuff = ConstWeakArray<F32>();
  256. width = height = 0;
  257. }
  258. }
  259. /// Set how far to render shadows for this frustum or set to negative if you want to use the m_frustun's far.
  260. void setEffectiveShadowDistance(F32 distance)
  261. {
  262. m_effectiveShadowDistance = distance;
  263. }
  264. /// How far to render shadows for this frustum.
  265. F32 getEffectiveShadowDistance() const
  266. {
  267. const F32 distance = (m_effectiveShadowDistance <= 0.0f) ? m_common.m_far : m_effectiveShadowDistance;
  268. ANKI_ASSERT(distance > m_common.m_near && distance <= m_common.m_far);
  269. return distance;
  270. }
  271. /// See computeShadowCascadeDistance()
  272. void setShadowCascadesDistancePower(F32 power)
  273. {
  274. ANKI_ASSERT(power >= 1.0f);
  275. m_shadowCascadesDistancePower = power;
  276. }
  277. /// See computeShadowCascadeDistance()
  278. F32 getShadowCascadesDistancePower() const
  279. {
  280. return m_shadowCascadesDistancePower;
  281. }
  282. F32 computeShadowCascadeDistance(U32 cascadeIdx) const
  283. {
  284. return anki::computeShadowCascadeDistance(cascadeIdx, m_shadowCascadesDistancePower,
  285. getEffectiveShadowDistance(), getCascadeCount());
  286. }
  287. const ConvexHullShape& getPerspectiveBoundingShapeWorldSpace() const
  288. {
  289. ANKI_ASSERT(m_frustumType == FrustumType::PERSPECTIVE);
  290. return m_perspective.m_hull;
  291. }
  292. const Obb& getOrthographicBoundingShapeWorldSpace() const
  293. {
  294. ANKI_ASSERT(m_frustumType == FrustumType::ORTHOGRAPHIC);
  295. return m_ortho.m_obbW;
  296. }
  297. const Array<Plane, U32(FrustumPlaneType::COUNT)>& getViewPlanes() const
  298. {
  299. return m_viewPlanesW;
  300. }
  301. /// Set the lod distance. It doesn't interact with the far plane.
  302. void setLodDistance(U32 lod, F32 maxDistance)
  303. {
  304. ANKI_ASSERT(maxDistance > 0.0f);
  305. m_maxLodDistances[lod] = maxDistance;
  306. }
  307. /// See setLodDistance.
  308. F32 getLodDistance(U32 lod) const
  309. {
  310. ANKI_ASSERT(m_maxLodDistances[lod] > 0.0f);
  311. return m_maxLodDistances[lod];
  312. }
  313. private:
  314. class Common
  315. {
  316. public:
  317. F32 m_near;
  318. F32 m_far;
  319. };
  320. class Perspective : public Common
  321. {
  322. public:
  323. F32 m_fovX;
  324. F32 m_fovY;
  325. Array<Vec4, 5> m_edgesW;
  326. Array<Vec4, 4> m_edgesL; ///< Don't need the eye point.
  327. ConvexHullShape m_hull;
  328. };
  329. class Ortho : public Common
  330. {
  331. public:
  332. F32 m_left;
  333. F32 m_right;
  334. F32 m_top;
  335. F32 m_bottom;
  336. Obb m_obbL;
  337. Obb m_obbW; ///< Including shape
  338. };
  339. SceneNode* m_node;
  340. FrustumType m_frustumType = FrustumType::COUNT;
  341. union
  342. {
  343. Perspective m_perspective;
  344. Ortho m_ortho;
  345. Common m_common;
  346. };
  347. // View planes
  348. Array<Plane, U32(FrustumPlaneType::COUNT)> m_viewPlanesL;
  349. Array<Plane, U32(FrustumPlaneType::COUNT)> m_viewPlanesW;
  350. Transform m_trf = Transform::getIdentity();
  351. Mat4 m_projMat = Mat4::getIdentity(); ///< Projection matrix
  352. Mat4 m_viewMat = Mat4::getIdentity(); ///< View matrix
  353. Mat4 m_viewProjMat = Mat4::getIdentity(); ///< View projection matrix
  354. Mat4 m_prevViewProjMat = Mat4::getIdentity();
  355. /// How far to render shadows for this frustum. If negative it's the m_frustum's far.
  356. F32 m_effectiveShadowDistance = -1.0f;
  357. /// Defines the the rate of the cascade distances
  358. F32 m_shadowCascadesDistancePower = 1.0f;
  359. Array<F32, MAX_LOD_COUNT - 1> m_maxLodDistances = {};
  360. class
  361. {
  362. public:
  363. DynamicArray<F32> m_depthMap;
  364. U32 m_depthMapWidth = 0;
  365. U32 m_depthMapHeight = 0;
  366. } m_coverageBuff; ///< Coverage buffer for extra visibility tests.
  367. FrustumComponentVisibilityTestFlag m_flags = FrustumComponentVisibilityTestFlag::NONE;
  368. Bool m_shapeMarkedForUpdate : 1;
  369. Bool m_trfMarkedForUpdate : 1;
  370. Bool updateInternal();
  371. U32 getCascadeCount() const
  372. {
  373. return !!(m_flags & FrustumComponentVisibilityTestFlag::DIRECTIONAL_LIGHT_SHADOWS_ALL_CASCADES)
  374. ? MAX_SHADOW_CASCADES2
  375. : 1;
  376. }
  377. };
  378. /// @}
  379. } // end namespace anki