FrustumComponent.h 11 KB

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