2
0

light_cluster_builder.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*************************************************************************/
  2. /* light_cluster_builder.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 LIGHT_CLUSTER_BUILDER_H
  31. #define LIGHT_CLUSTER_BUILDER_H
  32. #include "servers/rendering/rasterizer_rd/rasterizer_storage_rd.h"
  33. class LightClusterBuilder {
  34. public:
  35. enum LightType {
  36. LIGHT_TYPE_OMNI,
  37. LIGHT_TYPE_SPOT
  38. };
  39. enum ItemType {
  40. ITEM_TYPE_OMNI_LIGHT,
  41. ITEM_TYPE_SPOT_LIGHT,
  42. ITEM_TYPE_REFLECTION_PROBE,
  43. ITEM_TYPE_DECAL,
  44. ITEM_TYPE_MAX //should always be 4
  45. };
  46. enum {
  47. COUNTER_SHIFT = 20, //one million total ids
  48. POINTER_MASK = (1 << COUNTER_SHIFT) - 1,
  49. COUNTER_MASK = 0xfff // 4096 items per cell
  50. };
  51. private:
  52. struct LightData {
  53. float position[3];
  54. uint32_t type;
  55. float radius;
  56. float spot_aperture;
  57. uint32_t pad[2];
  58. };
  59. uint32_t light_count = 0;
  60. uint32_t light_max = 0;
  61. LightData *lights = nullptr;
  62. struct OrientedBoxData {
  63. float position[3];
  64. uint32_t pad;
  65. float x_axis[3];
  66. uint32_t pad2;
  67. float y_axis[3];
  68. uint32_t pad3;
  69. float z_axis[3];
  70. uint32_t pad4;
  71. };
  72. uint32_t refprobe_count = 0;
  73. uint32_t refprobe_max = 0;
  74. OrientedBoxData *refprobes = nullptr;
  75. uint32_t decal_count = 0;
  76. uint32_t decal_max = 0;
  77. OrientedBoxData *decals = nullptr;
  78. struct Item {
  79. AABB aabb;
  80. ItemType type;
  81. uint32_t index;
  82. };
  83. Item *items = nullptr;
  84. uint32_t item_count = 0;
  85. uint32_t item_max = 0;
  86. uint32_t width = 0;
  87. uint32_t height = 0;
  88. uint32_t depth = 0;
  89. struct Cell {
  90. uint32_t item_pointers[ITEM_TYPE_MAX];
  91. };
  92. Vector<uint8_t> cluster_data;
  93. RID cluster_texture;
  94. struct SortID {
  95. uint32_t cell_index;
  96. uint32_t item_index;
  97. ItemType item_type;
  98. };
  99. SortID *sort_ids = nullptr;
  100. Vector<uint32_t> ids;
  101. uint32_t sort_id_count = 0;
  102. uint32_t sort_id_max = 0;
  103. RID items_buffer;
  104. Transform view_xform;
  105. CameraMatrix projection;
  106. float z_far = 0;
  107. float z_near = 0;
  108. _FORCE_INLINE_ void _add_item(const AABB &p_aabb, ItemType p_type, uint32_t p_index) {
  109. if (unlikely(item_count == item_max)) {
  110. item_max = nearest_power_of_2_templated(item_max + 1);
  111. items = (Item *)memrealloc(items, sizeof(Item) * item_max);
  112. }
  113. Item &item = items[item_count];
  114. item.aabb = p_aabb;
  115. item.index = p_index;
  116. item.type = p_type;
  117. item_count++;
  118. }
  119. public:
  120. void begin(const Transform &p_view_transform, const CameraMatrix &p_cam_projection);
  121. _FORCE_INLINE_ void add_light(LightType p_type, const Transform &p_transform, float p_radius, float p_spot_aperture) {
  122. if (unlikely(light_count == light_max)) {
  123. light_max = nearest_power_of_2_templated(light_max + 1);
  124. lights = (LightData *)memrealloc(lights, sizeof(LightData) * light_max);
  125. }
  126. LightData &ld = lights[light_count];
  127. ld.type = p_type;
  128. ld.position[0] = p_transform.origin.x;
  129. ld.position[1] = p_transform.origin.y;
  130. ld.position[2] = p_transform.origin.z;
  131. ld.radius = p_radius;
  132. ld.spot_aperture = p_spot_aperture;
  133. Transform xform = view_xform * p_transform;
  134. ld.radius *= xform.basis.get_uniform_scale();
  135. AABB aabb;
  136. switch (p_type) {
  137. case LIGHT_TYPE_OMNI: {
  138. aabb.position = xform.origin;
  139. aabb.size = Vector3(ld.radius, ld.radius, ld.radius);
  140. aabb.position -= aabb.size;
  141. aabb.size *= 2.0;
  142. _add_item(aabb, ITEM_TYPE_OMNI_LIGHT, light_count);
  143. } break;
  144. case LIGHT_TYPE_SPOT: {
  145. float r = ld.radius;
  146. real_t len = Math::tan(Math::deg2rad(ld.spot_aperture)) * r;
  147. aabb.position = xform.origin;
  148. aabb.expand_to(xform.xform(Vector3(len, len, -r)));
  149. aabb.expand_to(xform.xform(Vector3(-len, len, -r)));
  150. aabb.expand_to(xform.xform(Vector3(-len, -len, -r)));
  151. aabb.expand_to(xform.xform(Vector3(len, -len, -r)));
  152. _add_item(aabb, ITEM_TYPE_SPOT_LIGHT, light_count);
  153. } break;
  154. }
  155. light_count++;
  156. }
  157. _FORCE_INLINE_ void add_reflection_probe(const Transform &p_transform, const Vector3 &p_half_extents) {
  158. if (unlikely(refprobe_count == refprobe_max)) {
  159. refprobe_max = nearest_power_of_2_templated(refprobe_max + 1);
  160. refprobes = (OrientedBoxData *)memrealloc(refprobes, sizeof(OrientedBoxData) * refprobe_max);
  161. }
  162. OrientedBoxData &rp = refprobes[refprobe_count];
  163. Vector3 origin = p_transform.origin;
  164. rp.position[0] = origin.x;
  165. rp.position[1] = origin.y;
  166. rp.position[2] = origin.z;
  167. Vector3 x_axis = p_transform.basis.get_axis(0) * p_half_extents.x;
  168. rp.x_axis[0] = x_axis.x;
  169. rp.x_axis[1] = x_axis.y;
  170. rp.x_axis[2] = x_axis.z;
  171. Vector3 y_axis = p_transform.basis.get_axis(1) * p_half_extents.y;
  172. rp.y_axis[0] = y_axis.x;
  173. rp.y_axis[1] = y_axis.y;
  174. rp.y_axis[2] = y_axis.z;
  175. Vector3 z_axis = p_transform.basis.get_axis(2) * p_half_extents.z;
  176. rp.z_axis[0] = z_axis.x;
  177. rp.z_axis[1] = z_axis.y;
  178. rp.z_axis[2] = z_axis.z;
  179. AABB aabb;
  180. aabb.position = origin + x_axis + y_axis + z_axis;
  181. aabb.expand_to(origin + x_axis + y_axis - z_axis);
  182. aabb.expand_to(origin + x_axis - y_axis + z_axis);
  183. aabb.expand_to(origin + x_axis - y_axis - z_axis);
  184. aabb.expand_to(origin - x_axis + y_axis + z_axis);
  185. aabb.expand_to(origin - x_axis + y_axis - z_axis);
  186. aabb.expand_to(origin - x_axis - y_axis + z_axis);
  187. aabb.expand_to(origin - x_axis - y_axis - z_axis);
  188. _add_item(aabb, ITEM_TYPE_REFLECTION_PROBE, refprobe_count);
  189. refprobe_count++;
  190. }
  191. _FORCE_INLINE_ void add_decal(const Transform &p_transform, const Vector2 &p_half_extents, float p_depth) {
  192. if (unlikely(decal_count == decal_max)) {
  193. decal_max = nearest_power_of_2_templated(decal_max + 1);
  194. decals = (OrientedBoxData *)memrealloc(decals, sizeof(OrientedBoxData) * decal_max);
  195. }
  196. OrientedBoxData &dc = decals[decal_count];
  197. Vector3 z_axis = -p_transform.basis.get_axis(2) * p_depth * 0.5;
  198. dc.z_axis[0] = z_axis.x;
  199. dc.z_axis[1] = z_axis.y;
  200. dc.z_axis[2] = z_axis.z;
  201. Vector3 origin = p_transform.origin - z_axis;
  202. dc.position[0] = origin.x;
  203. dc.position[1] = origin.y;
  204. dc.position[2] = origin.z;
  205. Vector3 x_axis = p_transform.basis.get_axis(0) * p_half_extents.x;
  206. dc.x_axis[0] = x_axis.x;
  207. dc.x_axis[1] = x_axis.y;
  208. dc.x_axis[2] = x_axis.z;
  209. Vector3 y_axis = p_transform.basis.get_axis(1) * p_half_extents.y;
  210. dc.y_axis[0] = y_axis.x;
  211. dc.y_axis[1] = y_axis.y;
  212. dc.y_axis[2] = y_axis.z;
  213. AABB aabb;
  214. aabb.position = origin + x_axis + y_axis + z_axis;
  215. aabb.expand_to(origin + x_axis + y_axis - z_axis);
  216. aabb.expand_to(origin + x_axis - y_axis + z_axis);
  217. aabb.expand_to(origin + x_axis - y_axis - z_axis);
  218. aabb.expand_to(origin - x_axis + y_axis + z_axis);
  219. aabb.expand_to(origin - x_axis + y_axis - z_axis);
  220. aabb.expand_to(origin - x_axis - y_axis + z_axis);
  221. aabb.expand_to(origin - x_axis - y_axis - z_axis);
  222. _add_item(aabb, ITEM_TYPE_DECAL, decal_count);
  223. decal_count++;
  224. }
  225. void bake_cluster();
  226. void setup(uint32_t p_width, uint32_t p_height, uint32_t p_depth);
  227. RID get_cluster_texture() const;
  228. RID get_cluster_indices_buffer() const;
  229. LightClusterBuilder();
  230. ~LightClusterBuilder();
  231. };
  232. #endif // LIGHT_CLUSTER_BUILDER_H