cluster_builder_rd.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**************************************************************************/
  2. /* cluster_builder_rd.cpp */
  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. #include "cluster_builder_rd.h"
  31. #include "servers/rendering/rendering_device.h"
  32. #include "servers/rendering/rendering_server_globals.h"
  33. ClusterBuilderSharedDataRD::ClusterBuilderSharedDataRD() {
  34. RD::VertexFormatID vertex_format;
  35. {
  36. Vector<RD::VertexAttribute> attributes;
  37. {
  38. RD::VertexAttribute va;
  39. va.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  40. va.stride = sizeof(float) * 3;
  41. attributes.push_back(va);
  42. }
  43. vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  44. }
  45. {
  46. RD::FramebufferFormatID fb_format;
  47. RD::PipelineColorBlendState blend_state;
  48. String defines;
  49. if (RD::get_singleton()->has_feature(RD::SUPPORTS_FRAGMENT_SHADER_WITH_ONLY_SIDE_EFFECTS)) {
  50. fb_format = RD::get_singleton()->framebuffer_format_create_empty();
  51. blend_state = RD::PipelineColorBlendState::create_disabled();
  52. } else {
  53. Vector<RD::AttachmentFormat> afs;
  54. afs.push_back(RD::AttachmentFormat());
  55. afs.write[0].usage_flags = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  56. fb_format = RD::get_singleton()->framebuffer_format_create(afs);
  57. blend_state = RD::PipelineColorBlendState::create_blend();
  58. defines = "\n#define USE_ATTACHMENT\n";
  59. }
  60. RD::PipelineRasterizationState rasterization_state;
  61. rasterization_state.enable_depth_clamp = true;
  62. Vector<String> versions;
  63. versions.push_back("");
  64. cluster_render.cluster_render_shader.initialize(versions, defines);
  65. cluster_render.shader_version = cluster_render.cluster_render_shader.version_create();
  66. cluster_render.shader = cluster_render.cluster_render_shader.version_get_shader(cluster_render.shader_version, 0);
  67. cluster_render.shader_pipelines[ClusterRender::PIPELINE_NORMAL] = RD::get_singleton()->render_pipeline_create(cluster_render.shader, fb_format, vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rasterization_state, RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  68. RD::PipelineMultisampleState ms;
  69. ms.sample_count = RD::TEXTURE_SAMPLES_4;
  70. cluster_render.shader_pipelines[ClusterRender::PIPELINE_MSAA] = RD::get_singleton()->render_pipeline_create(cluster_render.shader, fb_format, vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rasterization_state, ms, RD::PipelineDepthStencilState(), blend_state, 0);
  71. }
  72. {
  73. Vector<String> versions;
  74. versions.push_back("");
  75. cluster_store.cluster_store_shader.initialize(versions);
  76. cluster_store.shader_version = cluster_store.cluster_store_shader.version_create();
  77. cluster_store.shader = cluster_store.cluster_store_shader.version_get_shader(cluster_store.shader_version, 0);
  78. cluster_store.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_store.shader);
  79. }
  80. {
  81. Vector<String> versions;
  82. versions.push_back("");
  83. cluster_debug.cluster_debug_shader.initialize(versions);
  84. cluster_debug.shader_version = cluster_debug.cluster_debug_shader.version_create();
  85. cluster_debug.shader = cluster_debug.cluster_debug_shader.version_get_shader(cluster_debug.shader_version, 0);
  86. cluster_debug.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_debug.shader);
  87. }
  88. { // Sphere mesh data.
  89. static const uint32_t icosphere_vertex_count = 42;
  90. static const float icosphere_vertices[icosphere_vertex_count * 3] = {
  91. 0, 0, -1, 0.7236073, -0.5257253, -0.4472195, -0.276388, -0.8506492, -0.4472199, -0.8944262, 0, -0.4472156, -0.276388, 0.8506492, -0.4472199, 0.7236073, 0.5257253, -0.4472195, 0.276388, -0.8506492, 0.4472199, -0.7236073, -0.5257253, 0.4472195, -0.7236073, 0.5257253, 0.4472195, 0.276388, 0.8506492, 0.4472199, 0.8944262, 0, 0.4472156, 0, 0, 1, -0.1624555, -0.4999952, -0.8506544, 0.4253227, -0.3090114, -0.8506542, 0.2628688, -0.8090116, -0.5257377, 0.8506479, 0, -0.5257359, 0.4253227, 0.3090114, -0.8506542, -0.5257298, 0, -0.8506517, -0.6881894, -0.4999969, -0.5257362, -0.1624555, 0.4999952, -0.8506544, -0.6881894, 0.4999969, -0.5257362, 0.2628688, 0.8090116, -0.5257377, 0.9510579, -0.3090126, 0, 0.9510579, 0.3090126, 0, 0, -1, 0, 0.5877856, -0.8090167, 0, -0.9510579, -0.3090126, 0, -0.5877856, -0.8090167, 0, -0.5877856, 0.8090167, 0, -0.9510579, 0.3090126, 0, 0.5877856, 0.8090167, 0, 0, 1, 0, 0.6881894, -0.4999969, 0.5257362, -0.2628688, -0.8090116, 0.5257377, -0.8506479, 0, 0.5257359, -0.2628688, 0.8090116, 0.5257377, 0.6881894, 0.4999969, 0.5257362, 0.1624555, -0.4999952, 0.8506544, 0.5257298, 0, 0.8506517, -0.4253227, -0.3090114, 0.8506542, -0.4253227, 0.3090114, 0.8506542, 0.1624555, 0.4999952, 0.8506544
  92. };
  93. static const uint32_t icosphere_triangle_count = 80;
  94. static const uint16_t icosphere_triangle_indices[icosphere_triangle_count * 3] = {
  95. 0, 13, 12, 1, 13, 15, 0, 12, 17, 0, 17, 19, 0, 19, 16, 1, 15, 22, 2, 14, 24, 3, 18, 26, 4, 20, 28, 5, 21, 30, 1, 22, 25, 2, 24, 27, 3, 26, 29, 4, 28, 31, 5, 30, 23, 6, 32, 37, 7, 33, 39, 8, 34, 40, 9, 35, 41, 10, 36, 38, 38, 41, 11, 38, 36, 41, 36, 9, 41, 41, 40, 11, 41, 35, 40, 35, 8, 40, 40, 39, 11, 40, 34, 39, 34, 7, 39, 39, 37, 11, 39, 33, 37, 33, 6, 37, 37, 38, 11, 37, 32, 38, 32, 10, 38, 23, 36, 10, 23, 30, 36, 30, 9, 36, 31, 35, 9, 31, 28, 35, 28, 8, 35, 29, 34, 8, 29, 26, 34, 26, 7, 34, 27, 33, 7, 27, 24, 33, 24, 6, 33, 25, 32, 6, 25, 22, 32, 22, 10, 32, 30, 31, 9, 30, 21, 31, 21, 4, 31, 28, 29, 8, 28, 20, 29, 20, 3, 29, 26, 27, 7, 26, 18, 27, 18, 2, 27, 24, 25, 6, 24, 14, 25, 14, 1, 25, 22, 23, 10, 22, 15, 23, 15, 5, 23, 16, 21, 5, 16, 19, 21, 19, 4, 21, 19, 20, 4, 19, 17, 20, 17, 3, 20, 17, 18, 3, 17, 12, 18, 12, 2, 18, 15, 16, 5, 15, 13, 16, 13, 0, 16, 12, 14, 2, 12, 13, 14, 13, 1, 14
  96. };
  97. Vector<uint8_t> vertex_data;
  98. vertex_data.resize(sizeof(float) * icosphere_vertex_count * 3);
  99. memcpy(vertex_data.ptrw(), icosphere_vertices, vertex_data.size());
  100. sphere_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  101. Vector<uint8_t> index_data;
  102. index_data.resize(sizeof(uint16_t) * icosphere_triangle_count * 3);
  103. memcpy(index_data.ptrw(), icosphere_triangle_indices, index_data.size());
  104. sphere_index_buffer = RD::get_singleton()->index_buffer_create(icosphere_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  105. Vector<RID> buffers;
  106. buffers.push_back(sphere_vertex_buffer);
  107. sphere_vertex_array = RD::get_singleton()->vertex_array_create(icosphere_vertex_count, vertex_format, buffers);
  108. sphere_index_array = RD::get_singleton()->index_array_create(sphere_index_buffer, 0, icosphere_triangle_count * 3);
  109. float min_d = 1e20;
  110. for (uint32_t i = 0; i < icosphere_triangle_count; i++) {
  111. Vector3 vertices[3];
  112. for (uint32_t j = 0; j < 3; j++) {
  113. uint32_t index = icosphere_triangle_indices[i * 3 + j];
  114. for (uint32_t k = 0; k < 3; k++) {
  115. vertices[j][k] = icosphere_vertices[index * 3 + k];
  116. }
  117. }
  118. Plane p(vertices[0], vertices[1], vertices[2]);
  119. min_d = MIN(Math::abs(p.d), min_d);
  120. }
  121. sphere_overfit = 1.0 / min_d;
  122. }
  123. { // Cone mesh data.
  124. static const uint32_t cone_vertex_count = 99;
  125. static const float cone_vertices[cone_vertex_count * 3] = {
  126. 0, 1, -1, 0.1950903, 0.9807853, -1, 0.3826835, 0.9238795, -1, 0.5555703, 0.8314696, -1, 0.7071068, 0.7071068, -1, 0.8314697, 0.5555702, -1, 0.9238795, 0.3826834, -1, 0.9807853, 0.1950903, -1, 1, 0, -1, 0.9807853, -0.1950902, -1, 0.9238796, -0.3826833, -1, 0.8314697, -0.5555702, -1, 0.7071068, -0.7071068, -1, 0.5555702, -0.8314697, -1, 0.3826833, -0.9238796, -1, 0.1950901, -0.9807853, -1, -3.25841e-7, -1, -1, -0.1950907, -0.9807852, -1, -0.3826839, -0.9238793, -1, -0.5555707, -0.8314693, -1, -0.7071073, -0.7071063, -1, -0.83147, -0.5555697, -1, -0.9238799, -0.3826827, -1, 0, 0, 0, -0.9807854, -0.1950894, -1, -1, 9.65599e-7, -1, -0.9807851, 0.1950913, -1, -0.9238791, 0.3826845, -1, -0.8314689, 0.5555713, -1, -0.7071059, 0.7071077, -1, -0.5555691, 0.8314704, -1, -0.3826821, 0.9238801, -1, -0.1950888, 0.9807856, -1
  127. };
  128. static const uint32_t cone_triangle_count = 62;
  129. static const uint16_t cone_triangle_indices[cone_triangle_count * 3] = {
  130. 0, 23, 1, 1, 23, 2, 2, 23, 3, 3, 23, 4, 4, 23, 5, 5, 23, 6, 6, 23, 7, 7, 23, 8, 8, 23, 9, 9, 23, 10, 10, 23, 11, 11, 23, 12, 12, 23, 13, 13, 23, 14, 14, 23, 15, 15, 23, 16, 16, 23, 17, 17, 23, 18, 18, 23, 19, 19, 23, 20, 20, 23, 21, 21, 23, 22, 22, 23, 24, 24, 23, 25, 25, 23, 26, 26, 23, 27, 27, 23, 28, 28, 23, 29, 29, 23, 30, 30, 23, 31, 31, 23, 32, 32, 23, 0, 7, 15, 24, 32, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 3, 6, 7, 3, 7, 8, 9, 9, 10, 7, 10, 11, 7, 11, 12, 15, 12, 13, 15, 13, 14, 15, 15, 16, 17, 17, 18, 19, 19, 20, 24, 20, 21, 24, 21, 22, 24, 24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 1, 3, 15, 17, 24, 17, 19, 24, 24, 26, 32, 26, 28, 32, 28, 30, 32, 32, 3, 7, 7, 11, 15, 32, 7, 24
  131. };
  132. Vector<uint8_t> vertex_data;
  133. vertex_data.resize(sizeof(float) * cone_vertex_count * 3);
  134. memcpy(vertex_data.ptrw(), cone_vertices, vertex_data.size());
  135. cone_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  136. Vector<uint8_t> index_data;
  137. index_data.resize(sizeof(uint16_t) * cone_triangle_count * 3);
  138. memcpy(index_data.ptrw(), cone_triangle_indices, index_data.size());
  139. cone_index_buffer = RD::get_singleton()->index_buffer_create(cone_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  140. Vector<RID> buffers;
  141. buffers.push_back(cone_vertex_buffer);
  142. cone_vertex_array = RD::get_singleton()->vertex_array_create(cone_vertex_count, vertex_format, buffers);
  143. cone_index_array = RD::get_singleton()->index_array_create(cone_index_buffer, 0, cone_triangle_count * 3);
  144. float min_d = 1e20;
  145. for (uint32_t i = 0; i < cone_triangle_count; i++) {
  146. Vector3 vertices[3];
  147. int32_t zero_index = -1;
  148. for (uint32_t j = 0; j < 3; j++) {
  149. uint32_t index = cone_triangle_indices[i * 3 + j];
  150. for (uint32_t k = 0; k < 3; k++) {
  151. vertices[j][k] = cone_vertices[index * 3 + k];
  152. }
  153. if (vertices[j] == Vector3()) {
  154. zero_index = j;
  155. }
  156. }
  157. if (zero_index != -1) {
  158. Vector3 a = vertices[(zero_index + 1) % 3];
  159. Vector3 b = vertices[(zero_index + 2) % 3];
  160. Vector3 c = a + Vector3(0, 0, 1);
  161. Plane p(a, b, c);
  162. min_d = MIN(Math::abs(p.d), min_d);
  163. }
  164. }
  165. cone_overfit = 1.0 / min_d;
  166. }
  167. { // Box mesh data.
  168. static const uint32_t box_vertex_count = 8;
  169. static const float box_vertices[box_vertex_count * 3] = {
  170. -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1
  171. };
  172. static const uint32_t box_triangle_count = 12;
  173. static const uint16_t box_triangle_indices[box_triangle_count * 3] = {
  174. 1, 2, 0, 3, 6, 2, 7, 4, 6, 5, 0, 4, 6, 0, 2, 3, 5, 7, 1, 3, 2, 3, 7, 6, 7, 5, 4, 5, 1, 0, 6, 4, 0, 3, 1, 5
  175. };
  176. Vector<uint8_t> vertex_data;
  177. vertex_data.resize(sizeof(float) * box_vertex_count * 3);
  178. memcpy(vertex_data.ptrw(), box_vertices, vertex_data.size());
  179. box_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  180. Vector<uint8_t> index_data;
  181. index_data.resize(sizeof(uint16_t) * box_triangle_count * 3);
  182. memcpy(index_data.ptrw(), box_triangle_indices, index_data.size());
  183. box_index_buffer = RD::get_singleton()->index_buffer_create(box_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  184. Vector<RID> buffers;
  185. buffers.push_back(box_vertex_buffer);
  186. box_vertex_array = RD::get_singleton()->vertex_array_create(box_vertex_count, vertex_format, buffers);
  187. box_index_array = RD::get_singleton()->index_array_create(box_index_buffer, 0, box_triangle_count * 3);
  188. }
  189. }
  190. ClusterBuilderSharedDataRD::~ClusterBuilderSharedDataRD() {
  191. RD::get_singleton()->free(sphere_vertex_buffer);
  192. RD::get_singleton()->free(sphere_index_buffer);
  193. RD::get_singleton()->free(cone_vertex_buffer);
  194. RD::get_singleton()->free(cone_index_buffer);
  195. RD::get_singleton()->free(box_vertex_buffer);
  196. RD::get_singleton()->free(box_index_buffer);
  197. cluster_render.cluster_render_shader.version_free(cluster_render.shader_version);
  198. cluster_store.cluster_store_shader.version_free(cluster_store.shader_version);
  199. cluster_debug.cluster_debug_shader.version_free(cluster_debug.shader_version);
  200. }
  201. /////////////////////////////
  202. void ClusterBuilderRD::_clear() {
  203. if (cluster_buffer.is_null()) {
  204. return;
  205. }
  206. RD::get_singleton()->free(cluster_buffer);
  207. RD::get_singleton()->free(cluster_render_buffer);
  208. RD::get_singleton()->free(element_buffer);
  209. cluster_buffer = RID();
  210. cluster_render_buffer = RID();
  211. element_buffer = RID();
  212. memfree(render_elements);
  213. render_elements = nullptr;
  214. render_element_max = 0;
  215. render_element_count = 0;
  216. RD::get_singleton()->free(framebuffer);
  217. framebuffer = RID();
  218. cluster_render_uniform_set = RID();
  219. cluster_store_uniform_set = RID();
  220. }
  221. void ClusterBuilderRD::setup(Size2i p_screen_size, uint32_t p_max_elements, RID p_depth_buffer, RID p_depth_buffer_sampler, RID p_color_buffer) {
  222. ERR_FAIL_COND(p_max_elements == 0);
  223. ERR_FAIL_COND(p_screen_size.x < 1);
  224. ERR_FAIL_COND(p_screen_size.y < 1);
  225. _clear();
  226. screen_size = p_screen_size;
  227. cluster_screen_size.width = (p_screen_size.width - 1) / cluster_size + 1;
  228. cluster_screen_size.height = (p_screen_size.height - 1) / cluster_size + 1;
  229. max_elements_by_type = p_max_elements;
  230. if (max_elements_by_type % 32) { // Needs to be aligned to 32.
  231. max_elements_by_type += 32 - (max_elements_by_type % 32);
  232. }
  233. cluster_buffer_size = cluster_screen_size.x * cluster_screen_size.y * (max_elements_by_type / 32 + 32) * ELEMENT_TYPE_MAX * 4;
  234. render_element_max = max_elements_by_type * ELEMENT_TYPE_MAX;
  235. uint32_t element_tag_bits_size = render_element_max / 32;
  236. uint32_t element_tag_depth_bits_size = render_element_max;
  237. cluster_render_buffer_size = cluster_screen_size.x * cluster_screen_size.y * (element_tag_bits_size + element_tag_depth_bits_size) * 4; // Tag bits (element was used) and tag depth (depth range in which it was used).
  238. cluster_render_buffer = RD::get_singleton()->storage_buffer_create(cluster_render_buffer_size);
  239. cluster_buffer = RD::get_singleton()->storage_buffer_create(cluster_buffer_size);
  240. render_elements = static_cast<RenderElementData *>(memalloc(sizeof(RenderElementData) * render_element_max));
  241. render_element_count = 0;
  242. element_buffer = RD::get_singleton()->storage_buffer_create(sizeof(RenderElementData) * render_element_max);
  243. uint32_t div_value = 1 << divisor;
  244. if (use_msaa) {
  245. framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value, RD::TEXTURE_SAMPLES_4);
  246. } else {
  247. framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value);
  248. }
  249. {
  250. Vector<RD::Uniform> uniforms;
  251. {
  252. RD::Uniform u;
  253. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  254. u.binding = 1;
  255. u.append_id(state_uniform);
  256. uniforms.push_back(u);
  257. }
  258. {
  259. RD::Uniform u;
  260. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  261. u.binding = 2;
  262. u.append_id(element_buffer);
  263. uniforms.push_back(u);
  264. }
  265. {
  266. RD::Uniform u;
  267. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  268. u.binding = 3;
  269. u.append_id(cluster_render_buffer);
  270. uniforms.push_back(u);
  271. }
  272. cluster_render_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_render.shader, 0);
  273. }
  274. {
  275. Vector<RD::Uniform> uniforms;
  276. {
  277. RD::Uniform u;
  278. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  279. u.binding = 1;
  280. u.append_id(cluster_render_buffer);
  281. uniforms.push_back(u);
  282. }
  283. {
  284. RD::Uniform u;
  285. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  286. u.binding = 2;
  287. u.append_id(cluster_buffer);
  288. uniforms.push_back(u);
  289. }
  290. {
  291. RD::Uniform u;
  292. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  293. u.binding = 3;
  294. u.append_id(element_buffer);
  295. uniforms.push_back(u);
  296. }
  297. cluster_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_store.shader, 0);
  298. }
  299. if (p_color_buffer.is_valid()) {
  300. Vector<RD::Uniform> uniforms;
  301. {
  302. RD::Uniform u;
  303. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  304. u.binding = 1;
  305. u.append_id(cluster_buffer);
  306. uniforms.push_back(u);
  307. }
  308. {
  309. RD::Uniform u;
  310. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  311. u.binding = 2;
  312. u.append_id(p_color_buffer);
  313. uniforms.push_back(u);
  314. }
  315. {
  316. RD::Uniform u;
  317. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  318. u.binding = 3;
  319. u.append_id(p_depth_buffer);
  320. uniforms.push_back(u);
  321. }
  322. {
  323. RD::Uniform u;
  324. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  325. u.binding = 4;
  326. u.append_id(p_depth_buffer_sampler);
  327. uniforms.push_back(u);
  328. }
  329. debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_debug.shader, 0);
  330. } else {
  331. debug_uniform_set = RID();
  332. }
  333. }
  334. void ClusterBuilderRD::begin(const Transform3D &p_view_transform, const Projection &p_cam_projection, bool p_flip_y) {
  335. view_xform = p_view_transform.affine_inverse();
  336. projection = p_cam_projection;
  337. z_near = projection.get_z_near();
  338. z_far = projection.get_z_far();
  339. camera_orthogonal = p_cam_projection.is_orthogonal();
  340. adjusted_projection = projection;
  341. if (!camera_orthogonal) {
  342. adjusted_projection.adjust_perspective_znear(0.0001);
  343. }
  344. Projection correction;
  345. correction.set_depth_correction(p_flip_y);
  346. projection = correction * projection;
  347. adjusted_projection = correction * adjusted_projection;
  348. // Reset counts.
  349. render_element_count = 0;
  350. for (uint32_t i = 0; i < ELEMENT_TYPE_MAX; i++) {
  351. cluster_count_by_type[i] = 0;
  352. }
  353. }
  354. void ClusterBuilderRD::bake_cluster() {
  355. RENDER_TIMESTAMP("> Bake 3D Cluster");
  356. RD::get_singleton()->draw_command_begin_label("Bake Light Cluster");
  357. // Clear cluster buffer.
  358. RD::get_singleton()->buffer_clear(cluster_buffer, 0, cluster_buffer_size, RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  359. if (render_element_count > 0) {
  360. // Clear render buffer.
  361. RD::get_singleton()->buffer_clear(cluster_render_buffer, 0, cluster_render_buffer_size, RD::BARRIER_MASK_RASTER);
  362. { // Fill state uniform.
  363. StateUniform state;
  364. RendererRD::MaterialStorage::store_camera(adjusted_projection, state.projection);
  365. state.inv_z_far = 1.0 / z_far;
  366. state.screen_to_clusters_shift = get_shift_from_power_of_2(cluster_size);
  367. state.screen_to_clusters_shift -= divisor; //screen is smaller, shift one less
  368. state.cluster_screen_width = cluster_screen_size.x;
  369. state.cluster_depth_offset = (render_element_max / 32);
  370. state.cluster_data_size = state.cluster_depth_offset + render_element_max;
  371. RD::get_singleton()->buffer_update(state_uniform, 0, sizeof(StateUniform), &state, RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  372. }
  373. // Update instances.
  374. RD::get_singleton()->buffer_update(element_buffer, 0, sizeof(RenderElementData) * render_element_count, render_elements, RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  375. RENDER_TIMESTAMP("Render 3D Cluster Elements");
  376. // Render elements.
  377. {
  378. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD);
  379. ClusterBuilderSharedDataRD::ClusterRender::PushConstant push_constant = {};
  380. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shared->cluster_render.shader_pipelines[use_msaa ? ClusterBuilderSharedDataRD::ClusterRender::PIPELINE_MSAA : ClusterBuilderSharedDataRD::ClusterRender::PIPELINE_NORMAL]);
  381. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, cluster_render_uniform_set, 0);
  382. for (uint32_t i = 0; i < render_element_count;) {
  383. push_constant.base_index = i;
  384. switch (render_elements[i].type) {
  385. case ELEMENT_TYPE_OMNI_LIGHT: {
  386. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
  387. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
  388. } break;
  389. case ELEMENT_TYPE_SPOT_LIGHT: {
  390. // If the spot angle is above a certain threshold, use a sphere instead of a cone for building the clusters
  391. // since the cone gets too flat/large (spot angle close to 90 degrees) or
  392. // can't even cover the affected area of the light (spot angle above 90 degrees).
  393. if (render_elements[i].has_wide_spot_angle) {
  394. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
  395. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
  396. } else {
  397. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->cone_vertex_array);
  398. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->cone_index_array);
  399. }
  400. } break;
  401. case ELEMENT_TYPE_DECAL:
  402. case ELEMENT_TYPE_REFLECTION_PROBE: {
  403. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->box_vertex_array);
  404. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->box_index_array);
  405. } break;
  406. }
  407. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterRender::PushConstant));
  408. uint32_t instances = 1;
  409. RD::get_singleton()->draw_list_draw(draw_list, true, instances);
  410. i += instances;
  411. }
  412. RD::get_singleton()->draw_list_end(RD::BARRIER_MASK_COMPUTE);
  413. }
  414. // Store elements.
  415. RENDER_TIMESTAMP("Pack 3D Cluster Elements");
  416. {
  417. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  418. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_store.shader_pipeline);
  419. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cluster_store_uniform_set, 0);
  420. ClusterBuilderSharedDataRD::ClusterStore::PushConstant push_constant;
  421. push_constant.cluster_render_data_size = render_element_max / 32 + render_element_max;
  422. push_constant.max_render_element_count_div_32 = render_element_max / 32;
  423. push_constant.cluster_screen_size[0] = cluster_screen_size.x;
  424. push_constant.cluster_screen_size[1] = cluster_screen_size.y;
  425. push_constant.render_element_count_div_32 = render_element_count > 0 ? (render_element_count - 1) / 32 + 1 : 0;
  426. push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
  427. push_constant.pad1 = 0;
  428. push_constant.pad2 = 0;
  429. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterStore::PushConstant));
  430. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cluster_screen_size.x, cluster_screen_size.y, 1);
  431. RD::get_singleton()->compute_list_end(RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  432. }
  433. } else {
  434. RD::get_singleton()->barrier(RD::BARRIER_MASK_TRANSFER, RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  435. }
  436. RENDER_TIMESTAMP("< Bake 3D Cluster");
  437. RD::get_singleton()->draw_command_end_label();
  438. }
  439. void ClusterBuilderRD::debug(ElementType p_element) {
  440. ERR_FAIL_COND(debug_uniform_set.is_null());
  441. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  442. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_debug.shader_pipeline);
  443. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set, 0);
  444. ClusterBuilderSharedDataRD::ClusterDebug::PushConstant push_constant;
  445. push_constant.screen_size[0] = screen_size.x;
  446. push_constant.screen_size[1] = screen_size.y;
  447. push_constant.cluster_screen_size[0] = cluster_screen_size.x;
  448. push_constant.cluster_screen_size[1] = cluster_screen_size.y;
  449. push_constant.cluster_shift = get_shift_from_power_of_2(cluster_size);
  450. push_constant.cluster_type = p_element;
  451. push_constant.orthogonal = camera_orthogonal;
  452. push_constant.z_far = z_far;
  453. push_constant.z_near = z_near;
  454. push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
  455. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterDebug::PushConstant));
  456. RD::get_singleton()->compute_list_dispatch_threads(compute_list, screen_size.x, screen_size.y, 1);
  457. RD::get_singleton()->compute_list_end();
  458. }
  459. RID ClusterBuilderRD::get_cluster_buffer() const {
  460. return cluster_buffer;
  461. }
  462. uint32_t ClusterBuilderRD::get_cluster_size() const {
  463. return cluster_size;
  464. }
  465. uint32_t ClusterBuilderRD::get_max_cluster_elements() const {
  466. return max_elements_by_type;
  467. }
  468. void ClusterBuilderRD::set_shared(ClusterBuilderSharedDataRD *p_shared) {
  469. shared = p_shared;
  470. }
  471. ClusterBuilderRD::ClusterBuilderRD() {
  472. state_uniform = RD::get_singleton()->uniform_buffer_create(sizeof(StateUniform));
  473. }
  474. ClusterBuilderRD::~ClusterBuilderRD() {
  475. _clear();
  476. RD::get_singleton()->free(state_uniform);
  477. }