light_cluster_builder.cpp 6.8 KB

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