rendering_light_culler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**************************************************************************/
  2. /* rendering_light_culler.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RENDERING_LIGHT_CULLER_H
  31. #define RENDERING_LIGHT_CULLER_H
  32. #include "core/math/plane.h"
  33. #include "core/math/vector3.h"
  34. #include "renderer_scene_cull.h"
  35. struct Projection;
  36. struct Transform3D;
  37. // For testing performance improvements from the LightCuller:
  38. // Uncomment LIGHT_CULLER_DEBUG_FLASH and it will turn the culler
  39. // on and off every LIGHT_CULLER_DEBUG_FLASH_FREQUENCY camera prepares.
  40. // Uncomment LIGHT_CULLER_DEBUG_LOGGING to get periodic print of the number of casters culled before / after.
  41. // Uncomment LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT to get periodic print of the number of casters culled for the directional light..
  42. // #define LIGHT_CULLER_DEBUG_LOGGING
  43. // #define LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT
  44. // #define LIGHT_CULLER_DEBUG_REGULAR_LIGHT
  45. // #define LIGHT_CULLER_DEBUG_FLASH
  46. #define LIGHT_CULLER_DEBUG_FLASH_FREQUENCY 1024
  47. ////////////////////////////////////////////////////////////////////////////////////////////////
  48. // The code to generate the lookup table is included but commented out.
  49. // This may be useful for debugging / regenerating the LUT in the future,
  50. // especially if the order of planes changes.
  51. // When this define is set, the generated lookup table will be printed to debug output.
  52. // The generated lookup table can be copy pasted
  53. // straight to LUT_entry_sizes and LUT_entries.
  54. // See the referenced article for explanation.
  55. // #define RENDERING_LIGHT_CULLER_CALCULATE_LUT
  56. ////////////////////////////////////////////////////////////////////////////////////////////////
  57. // This define will be set automatically depending on earlier defines, you can leave this as is.
  58. #if defined(LIGHT_CULLER_DEBUG_LOGGING) || defined(RENDERING_LIGHT_CULLER_CALCULATE_LUT)
  59. #define RENDERING_LIGHT_CULLER_DEBUG_STRINGS
  60. #endif
  61. // Culls shadow casters that can't cast shadows into the camera frustum.
  62. class RenderingLightCuller {
  63. public:
  64. RenderingLightCuller();
  65. private:
  66. class LightSource {
  67. public:
  68. enum SourceType {
  69. ST_UNKNOWN,
  70. ST_DIRECTIONAL,
  71. ST_SPOTLIGHT,
  72. ST_OMNI,
  73. };
  74. LightSource() {
  75. type = ST_UNKNOWN;
  76. angle = 0.0f;
  77. range = FLT_MAX;
  78. }
  79. // All in world space, culling done in world space.
  80. Vector3 pos;
  81. Vector3 dir;
  82. SourceType type;
  83. float angle; // For spotlight.
  84. float range;
  85. };
  86. // Same order as godot.
  87. enum PlaneOrder {
  88. PLANE_NEAR,
  89. PLANE_FAR,
  90. PLANE_LEFT,
  91. PLANE_TOP,
  92. PLANE_RIGHT,
  93. PLANE_BOTTOM,
  94. PLANE_TOTAL,
  95. };
  96. // Same order as godot.
  97. enum PointOrder {
  98. PT_FAR_LEFT_TOP,
  99. PT_FAR_LEFT_BOTTOM,
  100. PT_FAR_RIGHT_TOP,
  101. PT_FAR_RIGHT_BOTTOM,
  102. PT_NEAR_LEFT_TOP,
  103. PT_NEAR_LEFT_BOTTOM,
  104. PT_NEAR_RIGHT_TOP,
  105. PT_NEAR_RIGHT_BOTTOM,
  106. };
  107. // 6 bits, 6 planes.
  108. enum {
  109. NUM_CAM_PLANES = 6,
  110. NUM_CAM_POINTS = 8,
  111. MAX_CULL_PLANES = 17,
  112. LUT_SIZE = 64,
  113. };
  114. public:
  115. // Before each pass with a different camera, you must call this so the culler can pre-create
  116. // the camera frustum planes and corner points in world space which are used for the culling.
  117. bool prepare_camera(const Transform3D &p_cam_transform, const Projection &p_cam_matrix);
  118. // REGULAR LIGHTS (SPOT, OMNI).
  119. // These are prepared then used for culling one by one, single threaded.
  120. // prepare_regular_light() returns false if the entire light is culled (i.e. there is no intersection between the light and the view frustum).
  121. bool prepare_regular_light(const RendererSceneCull::Instance &p_instance) { return _prepare_light(p_instance, -1); }
  122. // Cull according to the regular light planes that were setup in the previous call to prepare_regular_light.
  123. void cull_regular_light(PagedArray<RendererSceneCull::Instance *> &r_instance_shadow_cull_result);
  124. // Directional lights are prepared in advance, and can be culled multithreaded chopping and changing between
  125. // different directional_light_id.
  126. void prepare_directional_light(const RendererSceneCull::Instance *p_instance, int32_t p_directional_light_id);
  127. // Return false if the instance is to be culled.
  128. bool cull_directional_light(const RendererSceneCull::InstanceBounds &p_bound, int32_t p_directional_light_id);
  129. // Can turn on and off from the engine if desired.
  130. void set_caster_culling_active(bool p_active) { data.caster_culling_active = p_active; }
  131. void set_light_culling_active(bool p_active) { data.light_culling_active = p_active; }
  132. private:
  133. struct LightCullPlanes {
  134. void add_cull_plane(const Plane &p);
  135. Plane cull_planes[MAX_CULL_PLANES];
  136. int num_cull_planes = 0;
  137. #ifdef LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT
  138. uint32_t rejected_count = 0;
  139. #endif
  140. };
  141. bool _prepare_light(const RendererSceneCull::Instance &p_instance, int32_t p_directional_light_id = -1);
  142. // Avoid adding extra culling planes derived from near colinear triangles.
  143. // The normals derived from these will be inaccurate, and can lead to false
  144. // culling of objects that should be within the light volume.
  145. bool _is_colinear_tri(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c) const {
  146. // Lengths of sides a, b and c.
  147. float la = (p_b - p_a).length();
  148. float lb = (p_c - p_b).length();
  149. float lc = (p_c - p_a).length();
  150. // Get longest side into lc.
  151. if (lb < la) {
  152. SWAP(la, lb);
  153. }
  154. if (lc < lb) {
  155. SWAP(lb, lc);
  156. }
  157. // Prevent divide by zero.
  158. if (lc > 0.001f) {
  159. // If the summed length of the smaller two
  160. // sides is close to the length of the longest side,
  161. // the points are colinear, and the triangle is near degenerate.
  162. float ld = ((la + lb) - lc) / lc;
  163. // ld will be close to zero for colinear tris.
  164. return ld < 0.001f;
  165. }
  166. // Don't create planes from tiny triangles,
  167. // they won't be accurate.
  168. return true;
  169. }
  170. // Internal version uses LightSource.
  171. bool _add_light_camera_planes(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);
  172. // Directional light gives parallel culling planes (as opposed to point lights).
  173. bool add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);
  174. // Is the light culler active? maybe not in the editor...
  175. bool is_caster_culling_active() const { return data.caster_culling_active; }
  176. bool is_light_culling_active() const { return data.light_culling_active; }
  177. // Do we want to log some debug output?
  178. bool is_logging() const { return data.debug_count == 0; }
  179. struct Data {
  180. // Camera frustum planes (world space) - order ePlane.
  181. Vector<Plane> frustum_planes;
  182. // Camera frustum corners (world space) - order ePoint.
  183. Vector3 frustum_points[NUM_CAM_POINTS];
  184. // Master can have multiple directional lights.
  185. // These need to store their own cull planes individually, as master
  186. // chops and changes between culling different lights
  187. // instead of doing one by one, and we don't want to prepare
  188. // lights multiple times per frame.
  189. LocalVector<LightCullPlanes> directional_cull_planes;
  190. // Single threaded cull planes for regular lights
  191. // (OMNI, SPOT). These lights reuse the same set of cull plane data.
  192. LightCullPlanes regular_cull_planes;
  193. #ifdef LIGHT_CULLER_DEBUG_REGULAR_LIGHT
  194. uint32_t regular_rejected_count = 0;
  195. #endif
  196. // The whole regular light can be out of range of the view frustum, in which case all casters should be culled.
  197. bool out_of_range = false;
  198. #ifdef RENDERING_LIGHT_CULLER_DEBUG_STRINGS
  199. static String plane_bitfield_to_string(unsigned int BF);
  200. // Names of the plane and point enums, useful for debugging.
  201. static const char *string_planes[];
  202. static const char *string_points[];
  203. #endif
  204. // Precalculated look up table.
  205. static uint8_t LUT_entry_sizes[LUT_SIZE];
  206. static uint8_t LUT_entries[LUT_SIZE][8];
  207. bool caster_culling_active = true;
  208. bool light_culling_active = true;
  209. // Light culling is a basic on / off switch.
  210. // Caster culling only works if light culling is also on.
  211. bool is_active() const { return light_culling_active; }
  212. // Ideally a frame counter, but for ease of implementation
  213. // this is just incremented on each prepare_camera.
  214. // used to turn on and off debugging features.
  215. int debug_count = -1;
  216. } data;
  217. // This functionality is not required in general use (and is compiled out),
  218. // as the lookup table can normally be hard coded
  219. // (provided order of planes etc does not change).
  220. // It is provided for debugging / future maintenance.
  221. #ifdef RENDERING_LIGHT_CULLER_CALCULATE_LUT
  222. void get_neighbouring_planes(PlaneOrder p_plane, PlaneOrder r_neigh_planes[4]) const;
  223. void get_corners_of_planes(PlaneOrder p_plane_a, PlaneOrder p_plane_b, PointOrder r_points[2]) const;
  224. void create_LUT();
  225. void compact_LUT_entry(uint32_t p_entry_id);
  226. void debug_print_LUT();
  227. void debug_print_LUT_as_table();
  228. void add_LUT(int p_plane_0, int p_plane_1, PointOrder p_pts[2]);
  229. void add_LUT_entry(uint32_t p_entry_id, PointOrder p_pts[2]);
  230. String debug_string_LUT_entry(const LocalVector<uint8_t> &p_entry, bool p_pair = false);
  231. String string_LUT_entry(const LocalVector<uint8_t> &p_entry);
  232. // Contains a list of points for each combination of plane facing directions.
  233. LocalVector<uint8_t> _calculated_LUT[LUT_SIZE];
  234. #endif
  235. };
  236. #endif // RENDERING_LIGHT_CULLER_H