light_cluster_builder.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*************************************************************************/
  2. /* light_cluster_builder.cpp */
  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. #include "light_cluster_builder.h"
  31. void LightClusterBuilder::begin(const Transform &p_view_transform, const CameraMatrix &p_cam_projection) {
  32. view_xform = p_view_transform;
  33. projection = p_cam_projection;
  34. z_near = -projection.get_z_near();
  35. z_far = -projection.get_z_far();
  36. //reset counts
  37. light_count = 0;
  38. refprobe_count = 0;
  39. decal_count = 0;
  40. item_count = 0;
  41. sort_id_count = 0;
  42. }
  43. void LightClusterBuilder::bake_cluster() {
  44. float slice_depth = (z_near - z_far) / depth;
  45. uint8_t *cluster_dataw = cluster_data.ptrw();
  46. Cell *cluster_data_ptr = (Cell *)cluster_dataw;
  47. //clear the cluster
  48. zeromem(cluster_data_ptr, (width * height * depth * sizeof(Cell)));
  49. /* Step 1, create cell positions and count them */
  50. for (uint32_t i = 0; i < item_count; i++) {
  51. const Item &item = items[i];
  52. int from_slice = Math::floor((z_near - (item.aabb.position.z + item.aabb.size.z)) / slice_depth);
  53. int to_slice = Math::floor((z_near - item.aabb.position.z) / slice_depth);
  54. if (from_slice >= (int)depth || to_slice < 0) {
  55. continue; //sorry no go
  56. }
  57. from_slice = MAX(0, from_slice);
  58. to_slice = MIN((int)depth - 1, to_slice);
  59. for (int j = from_slice; j <= to_slice; j++) {
  60. Vector3 min = item.aabb.position;
  61. Vector3 max = item.aabb.position + item.aabb.size;
  62. float limit_near = MIN((z_near - slice_depth * j), max.z);
  63. float limit_far = MAX((z_near - slice_depth * (j + 1)), min.z);
  64. max.z = limit_near;
  65. min.z = limit_near;
  66. Vector3 proj_min = projection.xform(min);
  67. Vector3 proj_max = projection.xform(max);
  68. int near_from_x = int(Math::floor((proj_min.x * 0.5 + 0.5) * width));
  69. int near_from_y = int(Math::floor((-proj_max.y * 0.5 + 0.5) * height));
  70. int near_to_x = int(Math::floor((proj_max.x * 0.5 + 0.5) * width));
  71. int near_to_y = int(Math::floor((-proj_min.y * 0.5 + 0.5) * height));
  72. max.z = limit_far;
  73. min.z = limit_far;
  74. proj_min = projection.xform(min);
  75. proj_max = projection.xform(max);
  76. int far_from_x = int(Math::floor((proj_min.x * 0.5 + 0.5) * width));
  77. int far_from_y = int(Math::floor((-proj_max.y * 0.5 + 0.5) * height));
  78. int far_to_x = int(Math::floor((proj_max.x * 0.5 + 0.5) * width));
  79. int far_to_y = int(Math::floor((-proj_min.y * 0.5 + 0.5) * height));
  80. //print_line(itos(j) + " near - " + Vector2i(near_from_x, near_from_y) + " -> " + Vector2i(near_to_x, near_to_y));
  81. //print_line(itos(j) + " far - " + Vector2i(far_from_x, far_from_y) + " -> " + Vector2i(far_to_x, far_to_y));
  82. int from_x = MIN(near_from_x, far_from_x);
  83. int from_y = MIN(near_from_y, far_from_y);
  84. int to_x = MAX(near_to_x, far_to_x);
  85. int to_y = MAX(near_to_y, far_to_y);
  86. if (from_x >= (int)width || to_x < 0 || from_y >= (int)height || to_y < 0) {
  87. continue;
  88. }
  89. int sx = MAX(0, from_x);
  90. int sy = MAX(0, from_y);
  91. int dx = MIN((int)width - 1, to_x);
  92. int dy = MIN((int)height - 1, to_y);
  93. //print_line(itos(j) + " - " + Vector2i(sx, sy) + " -> " + Vector2i(dx, dy));
  94. for (int x = sx; x <= dx; x++) {
  95. for (int y = sy; y <= dy; y++) {
  96. uint32_t offset = j * (width * height) + y * width + x;
  97. if (unlikely(sort_id_count == sort_id_max)) {
  98. sort_id_max = nearest_power_of_2_templated(sort_id_max + 1);
  99. sort_ids = (SortID *)memrealloc(sort_ids, sizeof(SortID) * sort_id_max);
  100. if (ids.size()) {
  101. ids.resize(sort_id_max);
  102. RD::get_singleton()->free(items_buffer);
  103. items_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * sort_id_max);
  104. }
  105. }
  106. sort_ids[sort_id_count].cell_index = offset;
  107. sort_ids[sort_id_count].item_index = item.index;
  108. sort_ids[sort_id_count].item_type = item.type;
  109. sort_id_count++;
  110. //for now, only count
  111. cluster_data_ptr[offset].item_pointers[item.type]++;
  112. //print_line("at offset " + itos(offset) + " value: " + itos(cluster_data_ptr[offset].item_pointers[item.type]));
  113. }
  114. }
  115. }
  116. }
  117. /* Step 2, Assign pointers (and reset counters) */
  118. uint32_t offset = 0;
  119. for (uint32_t i = 0; i < (width * height * depth); i++) {
  120. for (int j = 0; j < ITEM_TYPE_MAX; j++) {
  121. uint32_t count = cluster_data_ptr[i].item_pointers[j]; //save count
  122. cluster_data_ptr[i].item_pointers[j] = offset; //replace count by pointer
  123. offset += count; //increase offset by count;
  124. }
  125. }
  126. //print_line("offset: " + itos(offset));
  127. /* Step 3, Place item lists */
  128. uint32_t *ids_ptr = ids.ptrw();
  129. for (uint32_t i = 0; i < sort_id_count; i++) {
  130. const SortID &id = sort_ids[i];
  131. Cell &cell = cluster_data_ptr[id.cell_index];
  132. uint32_t pointer = cell.item_pointers[id.item_type] & POINTER_MASK;
  133. uint32_t counter = cell.item_pointers[id.item_type] >> COUNTER_SHIFT;
  134. ids_ptr[pointer + counter] = id.item_index;
  135. cell.item_pointers[id.item_type] = pointer | ((counter + 1) << COUNTER_SHIFT);
  136. }
  137. RD::get_singleton()->texture_update(cluster_texture, 0, cluster_data, true);
  138. RD::get_singleton()->buffer_update(items_buffer, 0, offset * sizeof(uint32_t), ids_ptr, true);
  139. }
  140. void LightClusterBuilder::setup(uint32_t p_width, uint32_t p_height, uint32_t p_depth) {
  141. if (width == p_width && height == p_height && depth == p_depth) {
  142. return;
  143. }
  144. if (cluster_texture.is_valid()) {
  145. RD::get_singleton()->free(cluster_texture);
  146. }
  147. width = p_width;
  148. height = p_height;
  149. depth = p_depth;
  150. cluster_data.resize(width * height * depth * sizeof(Cell));
  151. {
  152. RD::TextureFormat tf;
  153. tf.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  154. tf.type = RD::TEXTURE_TYPE_3D;
  155. tf.width = width;
  156. tf.height = height;
  157. tf.depth = depth;
  158. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  159. cluster_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  160. }
  161. }
  162. RID LightClusterBuilder::get_cluster_texture() const {
  163. return cluster_texture;
  164. }
  165. RID LightClusterBuilder::get_cluster_indices_buffer() const {
  166. return items_buffer;
  167. }
  168. LightClusterBuilder::LightClusterBuilder() {
  169. //initialize accumulators to something
  170. lights = (LightData *)memalloc(sizeof(LightData) * 1024);
  171. light_max = 1024;
  172. refprobes = (OrientedBoxData *)memalloc(sizeof(OrientedBoxData) * 1024);
  173. refprobe_max = 1024;
  174. decals = (OrientedBoxData *)memalloc(sizeof(OrientedBoxData) * 1024);
  175. decal_max = 1024;
  176. items = (Item *)memalloc(sizeof(Item) * 1024);
  177. item_max = 1024;
  178. sort_ids = (SortID *)memalloc(sizeof(SortID) * 1024);
  179. ids.resize(2014);
  180. items_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 1024);
  181. item_max = 1024;
  182. }
  183. LightClusterBuilder::~LightClusterBuilder() {
  184. if (cluster_data.size()) {
  185. RD::get_singleton()->free(cluster_texture);
  186. }
  187. if (lights) {
  188. memfree(lights);
  189. }
  190. if (refprobes) {
  191. memfree(refprobes);
  192. }
  193. if (decals) {
  194. memfree(decals);
  195. }
  196. if (items) {
  197. memfree(items);
  198. }
  199. if (sort_ids) {
  200. memfree(sort_ids);
  201. RD::get_singleton()->free(items_buffer);
  202. }
  203. }