cluster_builder_rd.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. RD::PipelineRasterizationState rasterization_state;
  49. RD::PipelineMultisampleState ms;
  50. rasterization_state.enable_depth_clamp = true;
  51. ms.sample_count = RD::TEXTURE_SAMPLES_4;
  52. Vector<String> variants;
  53. variants.push_back("");
  54. variants.push_back("\n#define USE_ATTACHMENT\n");
  55. variants.push_back("\n#define MOLTENVK_USED\n#define NO_IMAGE_ATOMICS\n");
  56. variants.push_back("\n#define USE_ATTACHMENT\n#define MOLTENVK_USED\n#define NO_IMAGE_ATOMICS\n");
  57. variants.push_back("\n#define NO_IMAGE_ATOMICS\n");
  58. variants.push_back("\n#define MOLTENVK_USED\n#define NO_IMAGE_ATOMICS\n");
  59. ClusterRender::ShaderVariant shader_variant;
  60. RenderingDevice *rd = RD::get_singleton();
  61. if (rd->has_feature(RD::SUPPORTS_FRAGMENT_SHADER_WITH_ONLY_SIDE_EFFECTS)) {
  62. fb_format = rd->framebuffer_format_create_empty();
  63. blend_state = RD::PipelineColorBlendState::create_disabled();
  64. #if (defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED))
  65. if (rd->get_device_capabilities().device_family == RDD::DEVICE_VULKAN) {
  66. shader_variant = ClusterRender::SHADER_NORMAL_MOLTENVK;
  67. } else if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  68. shader_variant = ClusterRender::SHADER_NORMAL;
  69. } else {
  70. shader_variant = ClusterRender::SHADER_NORMAL_NO_ATOMICS;
  71. }
  72. #else
  73. if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  74. shader_variant = ClusterRender::SHADER_NORMAL;
  75. } else {
  76. shader_variant = ClusterRender::SHADER_NORMAL_NO_ATOMICS;
  77. }
  78. #endif
  79. } else {
  80. Vector<RD::AttachmentFormat> afs;
  81. afs.push_back(RD::AttachmentFormat());
  82. afs.write[0].usage_flags = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  83. fb_format = rd->framebuffer_format_create(afs);
  84. blend_state = RD::PipelineColorBlendState::create_blend();
  85. #if (defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED))
  86. if (rd->get_device_capabilities().device_family == RDD::DEVICE_VULKAN) {
  87. shader_variant = ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK;
  88. } else if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  89. shader_variant = ClusterRender::SHADER_USE_ATTACHMENT;
  90. } else {
  91. shader_variant = ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS;
  92. }
  93. #else
  94. if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  95. shader_variant = ClusterRender::SHADER_USE_ATTACHMENT;
  96. } else {
  97. shader_variant = ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS;
  98. }
  99. #endif
  100. }
  101. cluster_render.cluster_render_shader.initialize(variants);
  102. #if (defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED))
  103. if (rd->get_device_capabilities().device_family == RDD::DEVICE_VULKAN) {
  104. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL, false);
  105. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT, false);
  106. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_NO_ATOMICS, false);
  107. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, false);
  108. } else if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  109. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_MOLTENVK, false);
  110. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, false);
  111. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_NO_ATOMICS, false);
  112. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, false);
  113. } else {
  114. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL, false);
  115. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT, false);
  116. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_MOLTENVK, false);
  117. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, false);
  118. }
  119. #else
  120. if (rd->has_feature(RD::SUPPORTS_IMAGE_ATOMIC_32_BIT)) {
  121. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_MOLTENVK, false);
  122. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, false);
  123. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_NO_ATOMICS, false);
  124. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, false);
  125. } else {
  126. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL, false);
  127. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT, false);
  128. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_NORMAL_MOLTENVK, false);
  129. cluster_render.cluster_render_shader.set_variant_enabled(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, false);
  130. }
  131. #endif
  132. // Do not bake default (with "gl_HelperInvocation" and image atomics) variants for macOS/iOS Vulkan, but bake it for the rest of configs (including Metal).
  133. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL, "macos_forward_clustered_vulkan", false, true);
  134. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL, "ios_forward_clustered_vulkan", false, true);
  135. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT, "macos_forward_clustered_vulkan", false, true);
  136. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT, "ios_forward_clustered_vulkan", false, true);
  137. // Bake no "gl_HelperInvocation" and no "image atomics" variants for macOS/iOS Vulkan only.
  138. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL_MOLTENVK, "macos_forward_clustered_vulkan", true, false);
  139. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL_MOLTENVK, "ios_forward_clustered_vulkan", true, false);
  140. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, "macos_forward_clustered_vulkan", true, false);
  141. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT_MOLTENVK, "ios_forward_clustered_vulkan", true, false);
  142. // Bake no "image atomics" variants for macOS/iOS/visionOS Metal only.
  143. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL_NO_ATOMICS, "macos_forward_clustered_metal", true, false);
  144. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL_NO_ATOMICS, "ios_forward_clustered_metal", true, false);
  145. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_NORMAL_NO_ATOMICS, "visionos_forward_clustered_metal", true, false);
  146. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, "macos_forward_clustered_metal", true, false);
  147. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, "ios_forward_clustered_metal", true, false);
  148. cluster_render.cluster_render_shader.set_variants_bake_for(ClusterRender::SHADER_USE_ATTACHMENT_NO_ATOMICS, "visionos_forward_clustered_metal", true, false);
  149. cluster_render.shader_version = cluster_render.cluster_render_shader.version_create();
  150. cluster_render.shader = cluster_render.cluster_render_shader.version_get_shader(cluster_render.shader_version, shader_variant);
  151. 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);
  152. 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);
  153. }
  154. {
  155. Vector<String> versions;
  156. versions.push_back("");
  157. cluster_store.cluster_store_shader.initialize(versions);
  158. cluster_store.shader_version = cluster_store.cluster_store_shader.version_create();
  159. cluster_store.shader = cluster_store.cluster_store_shader.version_get_shader(cluster_store.shader_version, 0);
  160. cluster_store.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_store.shader);
  161. }
  162. {
  163. Vector<String> versions;
  164. versions.push_back("");
  165. cluster_debug.cluster_debug_shader.initialize(versions);
  166. cluster_debug.shader_version = cluster_debug.cluster_debug_shader.version_create();
  167. cluster_debug.shader = cluster_debug.cluster_debug_shader.version_get_shader(cluster_debug.shader_version, 0);
  168. cluster_debug.shader_pipeline = RD::get_singleton()->compute_pipeline_create(cluster_debug.shader);
  169. }
  170. { // Sphere mesh data.
  171. static const uint32_t icosphere_vertex_count = 42;
  172. static const float icosphere_vertices[icosphere_vertex_count * 3] = {
  173. 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
  174. };
  175. static const uint32_t icosphere_triangle_count = 80;
  176. static const uint16_t icosphere_triangle_indices[icosphere_triangle_count * 3] = {
  177. 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
  178. };
  179. Vector<uint8_t> vertex_data;
  180. vertex_data.resize(sizeof(float) * icosphere_vertex_count * 3);
  181. memcpy(vertex_data.ptrw(), icosphere_vertices, vertex_data.size());
  182. sphere_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  183. Vector<uint8_t> index_data;
  184. index_data.resize(sizeof(uint16_t) * icosphere_triangle_count * 3);
  185. memcpy(index_data.ptrw(), icosphere_triangle_indices, index_data.size());
  186. sphere_index_buffer = RD::get_singleton()->index_buffer_create(icosphere_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  187. Vector<RID> buffers;
  188. buffers.push_back(sphere_vertex_buffer);
  189. sphere_vertex_array = RD::get_singleton()->vertex_array_create(icosphere_vertex_count, vertex_format, buffers);
  190. sphere_index_array = RD::get_singleton()->index_array_create(sphere_index_buffer, 0, icosphere_triangle_count * 3);
  191. float min_d = 1e20;
  192. for (uint32_t i = 0; i < icosphere_triangle_count; i++) {
  193. Vector3 vertices[3];
  194. for (uint32_t j = 0; j < 3; j++) {
  195. uint32_t index = icosphere_triangle_indices[i * 3 + j];
  196. for (uint32_t k = 0; k < 3; k++) {
  197. vertices[j][k] = icosphere_vertices[index * 3 + k];
  198. }
  199. }
  200. Plane p(vertices[0], vertices[1], vertices[2]);
  201. min_d = MIN(Math::abs(p.d), min_d);
  202. }
  203. sphere_overfit = 1.0 / min_d;
  204. }
  205. { // Cone mesh data.
  206. static const uint32_t cone_vertex_count = 99;
  207. static const float cone_vertices[cone_vertex_count * 3] = {
  208. 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
  209. };
  210. static const uint32_t cone_triangle_count = 62;
  211. static const uint16_t cone_triangle_indices[cone_triangle_count * 3] = {
  212. 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
  213. };
  214. Vector<uint8_t> vertex_data;
  215. vertex_data.resize(sizeof(float) * cone_vertex_count * 3);
  216. memcpy(vertex_data.ptrw(), cone_vertices, vertex_data.size());
  217. cone_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  218. Vector<uint8_t> index_data;
  219. index_data.resize(sizeof(uint16_t) * cone_triangle_count * 3);
  220. memcpy(index_data.ptrw(), cone_triangle_indices, index_data.size());
  221. cone_index_buffer = RD::get_singleton()->index_buffer_create(cone_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  222. Vector<RID> buffers;
  223. buffers.push_back(cone_vertex_buffer);
  224. cone_vertex_array = RD::get_singleton()->vertex_array_create(cone_vertex_count, vertex_format, buffers);
  225. cone_index_array = RD::get_singleton()->index_array_create(cone_index_buffer, 0, cone_triangle_count * 3);
  226. float min_d = 1e20;
  227. for (uint32_t i = 0; i < cone_triangle_count; i++) {
  228. Vector3 vertices[3];
  229. int32_t zero_index = -1;
  230. for (uint32_t j = 0; j < 3; j++) {
  231. uint32_t index = cone_triangle_indices[i * 3 + j];
  232. for (uint32_t k = 0; k < 3; k++) {
  233. vertices[j][k] = cone_vertices[index * 3 + k];
  234. }
  235. if (vertices[j] == Vector3()) {
  236. zero_index = j;
  237. }
  238. }
  239. if (zero_index != -1) {
  240. Vector3 a = vertices[(zero_index + 1) % 3];
  241. Vector3 b = vertices[(zero_index + 2) % 3];
  242. Vector3 c = a + Vector3(0, 0, 1);
  243. Plane p(a, b, c);
  244. min_d = MIN(Math::abs(p.d), min_d);
  245. }
  246. }
  247. cone_overfit = 1.0 / min_d;
  248. }
  249. { // Box mesh data.
  250. static const uint32_t box_vertex_count = 8;
  251. static const float box_vertices[box_vertex_count * 3] = {
  252. -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1
  253. };
  254. static const uint32_t box_triangle_count = 12;
  255. static const uint16_t box_triangle_indices[box_triangle_count * 3] = {
  256. 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
  257. };
  258. Vector<uint8_t> vertex_data;
  259. vertex_data.resize(sizeof(float) * box_vertex_count * 3);
  260. memcpy(vertex_data.ptrw(), box_vertices, vertex_data.size());
  261. box_vertex_buffer = RD::get_singleton()->vertex_buffer_create(vertex_data.size(), vertex_data);
  262. Vector<uint8_t> index_data;
  263. index_data.resize(sizeof(uint16_t) * box_triangle_count * 3);
  264. memcpy(index_data.ptrw(), box_triangle_indices, index_data.size());
  265. box_index_buffer = RD::get_singleton()->index_buffer_create(box_triangle_count * 3, RD::INDEX_BUFFER_FORMAT_UINT16, index_data);
  266. Vector<RID> buffers;
  267. buffers.push_back(box_vertex_buffer);
  268. box_vertex_array = RD::get_singleton()->vertex_array_create(box_vertex_count, vertex_format, buffers);
  269. box_index_array = RD::get_singleton()->index_array_create(box_index_buffer, 0, box_triangle_count * 3);
  270. }
  271. }
  272. ClusterBuilderSharedDataRD::~ClusterBuilderSharedDataRD() {
  273. RD::get_singleton()->free(sphere_vertex_buffer);
  274. RD::get_singleton()->free(sphere_index_buffer);
  275. RD::get_singleton()->free(cone_vertex_buffer);
  276. RD::get_singleton()->free(cone_index_buffer);
  277. RD::get_singleton()->free(box_vertex_buffer);
  278. RD::get_singleton()->free(box_index_buffer);
  279. cluster_render.cluster_render_shader.version_free(cluster_render.shader_version);
  280. cluster_store.cluster_store_shader.version_free(cluster_store.shader_version);
  281. cluster_debug.cluster_debug_shader.version_free(cluster_debug.shader_version);
  282. }
  283. /////////////////////////////
  284. void ClusterBuilderRD::_clear() {
  285. if (cluster_buffer.is_null()) {
  286. return;
  287. }
  288. RD::get_singleton()->free(cluster_buffer);
  289. RD::get_singleton()->free(cluster_render_buffer);
  290. RD::get_singleton()->free(element_buffer);
  291. cluster_buffer = RID();
  292. cluster_render_buffer = RID();
  293. element_buffer = RID();
  294. memfree(render_elements);
  295. render_elements = nullptr;
  296. render_element_max = 0;
  297. render_element_count = 0;
  298. RD::get_singleton()->free(framebuffer);
  299. framebuffer = RID();
  300. cluster_render_uniform_set = RID();
  301. cluster_store_uniform_set = RID();
  302. }
  303. 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) {
  304. ERR_FAIL_COND(p_max_elements == 0);
  305. ERR_FAIL_COND(p_screen_size.x < 1);
  306. ERR_FAIL_COND(p_screen_size.y < 1);
  307. _clear();
  308. screen_size = p_screen_size;
  309. cluster_screen_size.width = Math::division_round_up((uint32_t)p_screen_size.width, cluster_size);
  310. cluster_screen_size.height = Math::division_round_up((uint32_t)p_screen_size.height, cluster_size);
  311. max_elements_by_type = p_max_elements;
  312. if (max_elements_by_type % 32) { // Needs to be aligned to 32.
  313. max_elements_by_type += 32 - (max_elements_by_type % 32);
  314. }
  315. cluster_buffer_size = cluster_screen_size.x * cluster_screen_size.y * (max_elements_by_type / 32 + 32) * ELEMENT_TYPE_MAX * 4;
  316. render_element_max = max_elements_by_type * ELEMENT_TYPE_MAX;
  317. uint32_t element_tag_bits_size = render_element_max / 32;
  318. uint32_t element_tag_depth_bits_size = render_element_max;
  319. 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).
  320. cluster_render_buffer = RD::get_singleton()->storage_buffer_create(cluster_render_buffer_size);
  321. cluster_buffer = RD::get_singleton()->storage_buffer_create(cluster_buffer_size);
  322. render_elements = static_cast<RenderElementData *>(memalloc(sizeof(RenderElementData) * render_element_max));
  323. render_element_count = 0;
  324. element_buffer = RD::get_singleton()->storage_buffer_create(sizeof(RenderElementData) * render_element_max);
  325. uint32_t div_value = 1 << divisor;
  326. if (use_msaa) {
  327. framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value, RD::TEXTURE_SAMPLES_4);
  328. } else {
  329. framebuffer = RD::get_singleton()->framebuffer_create_empty(p_screen_size / div_value);
  330. }
  331. {
  332. Vector<RD::Uniform> uniforms;
  333. {
  334. RD::Uniform u;
  335. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  336. u.binding = 1;
  337. u.append_id(state_uniform);
  338. uniforms.push_back(u);
  339. }
  340. {
  341. RD::Uniform u;
  342. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  343. u.binding = 2;
  344. u.append_id(element_buffer);
  345. uniforms.push_back(u);
  346. }
  347. {
  348. RD::Uniform u;
  349. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  350. u.binding = 3;
  351. u.append_id(cluster_render_buffer);
  352. uniforms.push_back(u);
  353. }
  354. cluster_render_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_render.shader, 0);
  355. }
  356. {
  357. Vector<RD::Uniform> uniforms;
  358. {
  359. RD::Uniform u;
  360. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  361. u.binding = 1;
  362. u.append_id(cluster_render_buffer);
  363. uniforms.push_back(u);
  364. }
  365. {
  366. RD::Uniform u;
  367. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  368. u.binding = 2;
  369. u.append_id(cluster_buffer);
  370. uniforms.push_back(u);
  371. }
  372. {
  373. RD::Uniform u;
  374. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  375. u.binding = 3;
  376. u.append_id(element_buffer);
  377. uniforms.push_back(u);
  378. }
  379. cluster_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_store.shader, 0);
  380. }
  381. if (p_color_buffer.is_valid()) {
  382. Vector<RD::Uniform> uniforms;
  383. {
  384. RD::Uniform u;
  385. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  386. u.binding = 1;
  387. u.append_id(cluster_buffer);
  388. uniforms.push_back(u);
  389. }
  390. {
  391. RD::Uniform u;
  392. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  393. u.binding = 2;
  394. u.append_id(p_color_buffer);
  395. uniforms.push_back(u);
  396. }
  397. {
  398. RD::Uniform u;
  399. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  400. u.binding = 3;
  401. u.append_id(p_depth_buffer);
  402. uniforms.push_back(u);
  403. }
  404. {
  405. RD::Uniform u;
  406. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  407. u.binding = 4;
  408. u.append_id(p_depth_buffer_sampler);
  409. uniforms.push_back(u);
  410. }
  411. debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shared->cluster_debug.shader, 0);
  412. } else {
  413. debug_uniform_set = RID();
  414. }
  415. }
  416. void ClusterBuilderRD::begin(const Transform3D &p_view_transform, const Projection &p_cam_projection, bool p_flip_y) {
  417. view_xform = p_view_transform.affine_inverse();
  418. projection = p_cam_projection;
  419. z_near = projection.get_z_near();
  420. z_far = projection.get_z_far();
  421. camera_orthogonal = p_cam_projection.is_orthogonal();
  422. adjusted_projection = projection;
  423. if (!camera_orthogonal) {
  424. adjusted_projection.adjust_perspective_znear(0.0001);
  425. }
  426. Projection correction;
  427. correction.set_depth_correction(p_flip_y);
  428. projection = correction * projection;
  429. adjusted_projection = correction * adjusted_projection;
  430. // Reset counts.
  431. render_element_count = 0;
  432. for (uint32_t i = 0; i < ELEMENT_TYPE_MAX; i++) {
  433. cluster_count_by_type[i] = 0;
  434. }
  435. }
  436. void ClusterBuilderRD::bake_cluster() {
  437. RENDER_TIMESTAMP("> Bake 3D Cluster");
  438. RD::get_singleton()->draw_command_begin_label("Bake Light Cluster");
  439. // Clear cluster buffer.
  440. RD::get_singleton()->buffer_clear(cluster_buffer, 0, cluster_buffer_size);
  441. if (render_element_count > 0) {
  442. // Clear render buffer.
  443. RD::get_singleton()->buffer_clear(cluster_render_buffer, 0, cluster_render_buffer_size);
  444. { // Fill state uniform.
  445. StateUniform state;
  446. RendererRD::MaterialStorage::store_camera(adjusted_projection, state.projection);
  447. state.inv_z_far = 1.0 / z_far;
  448. state.screen_to_clusters_shift = get_shift_from_power_of_2(cluster_size);
  449. state.screen_to_clusters_shift -= divisor; //screen is smaller, shift one less
  450. state.cluster_screen_width = cluster_screen_size.x;
  451. state.cluster_depth_offset = (render_element_max / 32);
  452. state.cluster_data_size = state.cluster_depth_offset + render_element_max;
  453. RD::get_singleton()->buffer_update(state_uniform, 0, sizeof(StateUniform), &state);
  454. }
  455. // Update instances.
  456. RD::get_singleton()->buffer_update(element_buffer, 0, sizeof(RenderElementData) * render_element_count, render_elements);
  457. RENDER_TIMESTAMP("Render 3D Cluster Elements");
  458. // Render elements.
  459. {
  460. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  461. ClusterBuilderSharedDataRD::ClusterRender::PushConstant push_constant = {};
  462. 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]);
  463. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, cluster_render_uniform_set, 0);
  464. for (uint32_t i = 0; i < render_element_count;) {
  465. push_constant.base_index = i;
  466. switch (render_elements[i].type) {
  467. case ELEMENT_TYPE_OMNI_LIGHT: {
  468. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
  469. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
  470. } break;
  471. case ELEMENT_TYPE_SPOT_LIGHT: {
  472. // If the spot angle is above a certain threshold, use a sphere instead of a cone for building the clusters
  473. // since the cone gets too flat/large (spot angle close to 90 degrees) or
  474. // can't even cover the affected area of the light (spot angle above 90 degrees).
  475. if (render_elements[i].has_wide_spot_angle) {
  476. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->sphere_vertex_array);
  477. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->sphere_index_array);
  478. } else {
  479. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->cone_vertex_array);
  480. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->cone_index_array);
  481. }
  482. } break;
  483. case ELEMENT_TYPE_DECAL:
  484. case ELEMENT_TYPE_REFLECTION_PROBE: {
  485. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, shared->box_vertex_array);
  486. RD::get_singleton()->draw_list_bind_index_array(draw_list, shared->box_index_array);
  487. } break;
  488. }
  489. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterRender::PushConstant));
  490. uint32_t instances = 1;
  491. RD::get_singleton()->draw_list_draw(draw_list, true, instances);
  492. i += instances;
  493. }
  494. RD::get_singleton()->draw_list_end();
  495. }
  496. // Store elements.
  497. RENDER_TIMESTAMP("Pack 3D Cluster Elements");
  498. {
  499. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  500. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_store.shader_pipeline);
  501. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cluster_store_uniform_set, 0);
  502. ClusterBuilderSharedDataRD::ClusterStore::PushConstant push_constant;
  503. push_constant.cluster_render_data_size = render_element_max / 32 + render_element_max;
  504. push_constant.max_render_element_count_div_32 = render_element_max / 32;
  505. push_constant.cluster_screen_size[0] = cluster_screen_size.x;
  506. push_constant.cluster_screen_size[1] = cluster_screen_size.y;
  507. push_constant.render_element_count_div_32 = Math::division_round_up(render_element_count, 32U);
  508. push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
  509. push_constant.pad1 = 0;
  510. push_constant.pad2 = 0;
  511. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterStore::PushConstant));
  512. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cluster_screen_size.x, cluster_screen_size.y, 1);
  513. RD::get_singleton()->compute_list_end();
  514. }
  515. }
  516. RENDER_TIMESTAMP("< Bake 3D Cluster");
  517. RD::get_singleton()->draw_command_end_label();
  518. }
  519. void ClusterBuilderRD::debug(ElementType p_element) {
  520. ERR_FAIL_COND(debug_uniform_set.is_null());
  521. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  522. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, shared->cluster_debug.shader_pipeline);
  523. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, debug_uniform_set, 0);
  524. ClusterBuilderSharedDataRD::ClusterDebug::PushConstant push_constant;
  525. push_constant.screen_size[0] = screen_size.x;
  526. push_constant.screen_size[1] = screen_size.y;
  527. push_constant.cluster_screen_size[0] = cluster_screen_size.x;
  528. push_constant.cluster_screen_size[1] = cluster_screen_size.y;
  529. push_constant.cluster_shift = get_shift_from_power_of_2(cluster_size);
  530. push_constant.cluster_type = p_element;
  531. push_constant.orthogonal = camera_orthogonal;
  532. push_constant.z_far = z_far;
  533. push_constant.z_near = z_near;
  534. push_constant.max_cluster_element_count_div_32 = max_elements_by_type / 32;
  535. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ClusterBuilderSharedDataRD::ClusterDebug::PushConstant));
  536. RD::get_singleton()->compute_list_dispatch_threads(compute_list, screen_size.x, screen_size.y, 1);
  537. RD::get_singleton()->compute_list_end();
  538. }
  539. RID ClusterBuilderRD::get_cluster_buffer() const {
  540. return cluster_buffer;
  541. }
  542. uint32_t ClusterBuilderRD::get_cluster_size() const {
  543. return cluster_size;
  544. }
  545. uint32_t ClusterBuilderRD::get_max_cluster_elements() const {
  546. return max_elements_by_type;
  547. }
  548. void ClusterBuilderRD::set_shared(ClusterBuilderSharedDataRD *p_shared) {
  549. shared = p_shared;
  550. }
  551. ClusterBuilderRD::ClusterBuilderRD() {
  552. state_uniform = RD::get_singleton()->uniform_buffer_create(sizeof(StateUniform));
  553. }
  554. ClusterBuilderRD::~ClusterBuilderRD() {
  555. _clear();
  556. RD::get_singleton()->free(state_uniform);
  557. }