visual_server_light_culler.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* visual_server_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 VISUAL_SERVER_LIGHT_CULLER_H
  31. #define VISUAL_SERVER_LIGHT_CULLER_H
  32. #include "core/math/plane.h"
  33. #include "core/math/vector3.h"
  34. #include "visual_server_scene.h"
  35. struct CameraMatrix;
  36. class Transform;
  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. // #define LIGHT_CULLER_DEBUG_LOGGING
  42. // #define LIGHT_CULLER_DEBUG_FLASH
  43. #define LIGHT_CULLER_DEBUG_FLASH_FREQUENCY 1024
  44. ////////////////////////////////////////////////////////////////////////////////////////////////
  45. // The code to generate the lookup table is included but commented out.
  46. // This may be useful for debugging / regenerating the LUT in the future,
  47. // especially if the order of planes changes.
  48. // When this define is set, the generated lookup table will be printed to debug output.
  49. // The generated lookup table can be copy pasted
  50. // straight to LUT_entry_sizes and LUT_entries.
  51. // See the referenced article for explanation.
  52. // #define VISUAL_SERVER_LIGHT_CULLER_CALCULATE_LUT
  53. ////////////////////////////////////////////////////////////////////////////////////////////////
  54. // This define will be set automatically depending on earlier defines, you can leave this as is.
  55. #if defined(LIGHT_CULLER_DEBUG_LOGGING) || defined(VISUAL_SERVER_LIGHT_CULLER_CALCULATE_LUT)
  56. #define VISUAL_SERVER_LIGHT_CULLER_DEBUG_STRINGS
  57. #endif
  58. // Culls shadow casters that can't cast shadows into the camera frustum.
  59. class VisualServerLightCuller {
  60. public:
  61. VisualServerLightCuller();
  62. private:
  63. class LightSource {
  64. public:
  65. enum SourceType {
  66. ST_UNKNOWN,
  67. ST_DIRECTIONAL,
  68. ST_SPOTLIGHT,
  69. ST_OMNI,
  70. };
  71. LightSource() {
  72. type = ST_UNKNOWN;
  73. angle = 0.0f;
  74. range = FLT_MAX;
  75. }
  76. // All in world space, culling done in world space.
  77. Vector3 pos;
  78. Vector3 dir;
  79. SourceType type;
  80. float angle; // For spotlight.
  81. float range;
  82. };
  83. // Same order as godot.
  84. enum PlaneOrder {
  85. PLANE_NEAR,
  86. PLANE_FAR,
  87. PLANE_LEFT,
  88. PLANE_TOP,
  89. PLANE_RIGHT,
  90. PLANE_BOTTOM,
  91. PLANE_TOTAL,
  92. };
  93. // Same order as godot.
  94. enum PointOrder {
  95. PT_FAR_LEFT_TOP,
  96. PT_FAR_LEFT_BOTTOM,
  97. PT_FAR_RIGHT_TOP,
  98. PT_FAR_RIGHT_BOTTOM,
  99. PT_NEAR_LEFT_TOP,
  100. PT_NEAR_LEFT_BOTTOM,
  101. PT_NEAR_RIGHT_TOP,
  102. PT_NEAR_RIGHT_BOTTOM,
  103. };
  104. // 6 bits, 6 planes.
  105. enum {
  106. NUM_CAM_PLANES = 6,
  107. NUM_CAM_POINTS = 8,
  108. MAX_CULL_PLANES = 17,
  109. LUT_SIZE = 64,
  110. };
  111. public:
  112. // Before each pass with a different camera, you must call this so the culler can pre-create
  113. // the camera frustum planes and corner points in world space which are used for the culling.
  114. bool prepare_camera(const Transform &p_cam_transform, const CameraMatrix &p_cam_matrix);
  115. // Returns false if the entire light is culled (i.e. there is no intersection between the light and the view frustum).
  116. bool prepare_light(const VisualServerScene::Instance &p_instance);
  117. // Cull according to the planes that were setup in the previous call to prepare_light.
  118. int cull(int p_count, VisualServerScene::Instance **p_result_array);
  119. // Can turn on and off from the engine if desired.
  120. void set_caster_culling_active(bool p_active) { data.caster_culling_active = p_active; }
  121. void set_light_culling_active(bool p_active) { data.light_culling_active = p_active; }
  122. private:
  123. // Internal version uses LightSource.
  124. bool _add_light_camera_planes(const LightSource &p_light_source);
  125. // Directional light gives parallel culling planes (as opposed to point lights).
  126. bool add_light_camera_planes_directional(const LightSource &p_light_source);
  127. // Avoid adding extra culling planes derived from near colinear triangles.
  128. // The normals derived from these will be inaccurate, and can lead to false
  129. // culling of objects that should be within the light volume.
  130. bool _is_colinear_tri(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c) const {
  131. // Lengths of sides a, b and c.
  132. float la = (p_b - p_a).length();
  133. float lb = (p_c - p_b).length();
  134. float lc = (p_c - p_a).length();
  135. // Get longest side into lc.
  136. if (lb < la) {
  137. SWAP(la, lb);
  138. }
  139. if (lc < lb) {
  140. SWAP(lb, lc);
  141. }
  142. // Prevent divide by zero.
  143. if (lc > 0.001f) {
  144. // If the summed length of the smaller two
  145. // sides is close to the length of the longest side,
  146. // the points are colinear, and the triangle is near degenerate.
  147. float ld = ((la + lb) - lc) / lc;
  148. // ld will be close to zero for colinear tris.
  149. return ld < 0.001f;
  150. }
  151. // Don't create planes from tiny triangles,
  152. // they won't be accurate.
  153. return true;
  154. }
  155. // Is the light culler active? maybe not in the editor...
  156. bool is_caster_culling_active() const { return data.caster_culling_active; }
  157. bool is_light_culling_active() const { return data.light_culling_active; }
  158. // Do we want to log some debug output?
  159. bool is_logging() const { return data.debug_count == 0; }
  160. // Culling planes.
  161. void add_cull_plane(const Plane &p);
  162. struct Data {
  163. // Camera frustum planes (world space) - order ePlane.
  164. Vector<Plane> frustum_planes;
  165. // Camera frustum corners (world space) - order ePoint.
  166. Vector3 frustum_points[NUM_CAM_POINTS];
  167. // We are storing cull planes in a ye olde style array to prevent needless allocations.
  168. Plane cull_planes[MAX_CULL_PLANES];
  169. int num_cull_planes = 0;
  170. // The whole light can be out of range of the view frustum, in which case all casters should be culled.
  171. bool out_of_range = false;
  172. #ifdef VISUAL_SERVER_LIGHT_CULLER_DEBUG_STRINGS
  173. static String plane_bitfield_to_string(unsigned int BF);
  174. // Names of the plane and point enums, useful for debugging.
  175. static const char *string_planes[];
  176. static const char *string_points[];
  177. #endif
  178. // Precalculated look up table.
  179. static uint8_t LUT_entry_sizes[LUT_SIZE];
  180. static uint8_t LUT_entries[LUT_SIZE][8];
  181. bool caster_culling_active = true;
  182. bool light_culling_active = true;
  183. // Light culling is a basic on / off switch.
  184. // Caster culling only works if light culling is also on.
  185. bool is_active() const { return light_culling_active; }
  186. // Ideally a frame counter, but for ease of implementation
  187. // this is just incremented on each prepare_camera.
  188. // used to turn on and off debugging features.
  189. int debug_count = -1;
  190. } data;
  191. // This functionality is not required in general use (and is compiled out),
  192. // as the lookup table can normally be hard coded
  193. // (provided order of planes etc does not change).
  194. // It is provided for debugging / future maintenance.
  195. #ifdef VISUAL_SERVER_LIGHT_CULLER_CALCULATE_LUT
  196. void get_neighbouring_planes(PlaneOrder p_plane, PlaneOrder r_neigh_planes[4]) const;
  197. void get_corners_of_planes(PlaneOrder p_plane_a, PlaneOrder p_plane_b, PointOrder r_points[2]) const;
  198. void create_LUT();
  199. void compact_LUT_entry(uint32_t p_entry_id);
  200. void debug_print_LUT();
  201. void debug_print_LUT_as_table();
  202. void add_LUT(int p_plane_0, int p_plane_1, PointOrder p_pts[2]);
  203. void add_LUT_entry(uint32_t p_entry_id, PointOrder p_pts[2]);
  204. String debug_string_LUT_entry(const LocalVector<uint8_t> &p_entry, bool p_pair = false);
  205. String string_LUT_entry(const LocalVector<uint8_t> &p_entry);
  206. // Contains a list of points for each combination of plane facing directions.
  207. LocalVector<uint8_t> _calculated_LUT[LUT_SIZE];
  208. #endif
  209. };
  210. #endif // VISUAL_SERVER_LIGHT_CULLER_H