cluster_builder_rd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**************************************************************************/
  2. /* cluster_builder_rd.h */
  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. #ifndef CLUSTER_BUILDER_RD_H
  31. #define CLUSTER_BUILDER_RD_H
  32. #include "servers/rendering/renderer_rd/shaders/cluster_debug.glsl.gen.h"
  33. #include "servers/rendering/renderer_rd/shaders/cluster_render.glsl.gen.h"
  34. #include "servers/rendering/renderer_rd/shaders/cluster_store.glsl.gen.h"
  35. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  36. class ClusterBuilderSharedDataRD {
  37. friend class ClusterBuilderRD;
  38. RID sphere_vertex_buffer;
  39. RID sphere_vertex_array;
  40. RID sphere_index_buffer;
  41. RID sphere_index_array;
  42. float sphere_overfit = 0.0; // Because an icosphere is not a perfect sphere, we need to enlarge it to cover the sphere area.
  43. RID cone_vertex_buffer;
  44. RID cone_vertex_array;
  45. RID cone_index_buffer;
  46. RID cone_index_array;
  47. float cone_overfit = 0.0; // Because an cone mesh is not a perfect cone, we need to enlarge it to cover the actual cone area.
  48. RID box_vertex_buffer;
  49. RID box_vertex_array;
  50. RID box_index_buffer;
  51. RID box_index_array;
  52. enum Divisor {
  53. DIVISOR_1,
  54. DIVISOR_2,
  55. DIVISOR_4,
  56. };
  57. struct ClusterRender {
  58. struct PushConstant {
  59. uint32_t base_index;
  60. uint32_t pad0;
  61. uint32_t pad1;
  62. uint32_t pad2;
  63. };
  64. ClusterRenderShaderRD cluster_render_shader;
  65. RID shader_version;
  66. RID shader;
  67. enum PipelineVersion {
  68. PIPELINE_NORMAL,
  69. PIPELINE_MSAA,
  70. PIPELINE_MAX
  71. };
  72. RID shader_pipelines[PIPELINE_MAX];
  73. } cluster_render;
  74. struct ClusterStore {
  75. struct PushConstant {
  76. uint32_t cluster_render_data_size; // how much data for a single cluster takes
  77. uint32_t max_render_element_count_div_32; // divided by 32
  78. uint32_t cluster_screen_size[2];
  79. uint32_t render_element_count_div_32; // divided by 32
  80. uint32_t max_cluster_element_count_div_32; // divided by 32
  81. uint32_t pad1;
  82. uint32_t pad2;
  83. };
  84. ClusterStoreShaderRD cluster_store_shader;
  85. RID shader_version;
  86. RID shader;
  87. RID shader_pipeline;
  88. } cluster_store;
  89. struct ClusterDebug {
  90. struct PushConstant {
  91. uint32_t screen_size[2];
  92. uint32_t cluster_screen_size[2];
  93. uint32_t cluster_shift;
  94. uint32_t cluster_type;
  95. float z_near;
  96. float z_far;
  97. uint32_t orthogonal;
  98. uint32_t max_cluster_element_count_div_32;
  99. uint32_t pad1;
  100. uint32_t pad2;
  101. };
  102. ClusterDebugShaderRD cluster_debug_shader;
  103. RID shader_version;
  104. RID shader;
  105. RID shader_pipeline;
  106. } cluster_debug;
  107. public:
  108. ClusterBuilderSharedDataRD();
  109. ~ClusterBuilderSharedDataRD();
  110. };
  111. class ClusterBuilderRD {
  112. public:
  113. static constexpr float WIDE_SPOT_ANGLE_THRESHOLD_DEG = 60.0f;
  114. enum LightType {
  115. LIGHT_TYPE_OMNI,
  116. LIGHT_TYPE_SPOT
  117. };
  118. enum BoxType {
  119. BOX_TYPE_REFLECTION_PROBE,
  120. BOX_TYPE_DECAL,
  121. };
  122. enum ElementType {
  123. ELEMENT_TYPE_OMNI_LIGHT,
  124. ELEMENT_TYPE_SPOT_LIGHT,
  125. ELEMENT_TYPE_DECAL,
  126. ELEMENT_TYPE_REFLECTION_PROBE,
  127. ELEMENT_TYPE_MAX,
  128. };
  129. private:
  130. ClusterBuilderSharedDataRD *shared = nullptr;
  131. struct RenderElementData {
  132. uint32_t type; // 0-4
  133. uint32_t touches_near;
  134. uint32_t touches_far;
  135. uint32_t original_index;
  136. float transform_inv[12]; // Transposed transform for less space.
  137. float scale[3];
  138. uint32_t has_wide_spot_angle;
  139. }; // Keep aligned to 32 bytes.
  140. uint32_t cluster_count_by_type[ELEMENT_TYPE_MAX] = {};
  141. uint32_t max_elements_by_type = 0;
  142. RenderElementData *render_elements = nullptr;
  143. uint32_t render_element_count = 0;
  144. uint32_t render_element_max = 0;
  145. Transform3D view_xform;
  146. Projection adjusted_projection;
  147. Projection projection;
  148. float z_far = 0;
  149. float z_near = 0;
  150. bool camera_orthogonal = false;
  151. enum Divisor {
  152. DIVISOR_1,
  153. DIVISOR_2,
  154. DIVISOR_4,
  155. };
  156. uint32_t cluster_size = 32;
  157. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  158. // Results in visual artifacts on macOS and iOS when using MSAA and subgroups.
  159. // Using subgroups and disabling MSAA is the optimal solution for now and also works
  160. // with MoltenVK.
  161. bool use_msaa = false;
  162. #else
  163. bool use_msaa = true;
  164. #endif
  165. Divisor divisor = DIVISOR_4;
  166. Size2i screen_size;
  167. Size2i cluster_screen_size;
  168. RID framebuffer;
  169. RID cluster_render_buffer; // Used for creating.
  170. RID cluster_buffer; // Used for rendering.
  171. RID element_buffer; // Used for storing, to hint element touches far plane or near plane.
  172. uint32_t cluster_render_buffer_size = 0;
  173. uint32_t cluster_buffer_size = 0;
  174. RID cluster_render_uniform_set;
  175. RID cluster_store_uniform_set;
  176. // Persistent data.
  177. void _clear();
  178. struct StateUniform {
  179. float projection[16];
  180. float inv_z_far;
  181. uint32_t screen_to_clusters_shift; // Shift to obtain coordinates in block indices.
  182. uint32_t cluster_screen_width;
  183. uint32_t cluster_data_size; // How much data is needed for a single cluster.
  184. uint32_t cluster_depth_offset;
  185. uint32_t pad0;
  186. uint32_t pad1;
  187. uint32_t pad2;
  188. };
  189. RID state_uniform;
  190. RID debug_uniform_set;
  191. public:
  192. void setup(Size2i p_screen_size, uint32_t p_max_elements, RID p_depth_buffer, RID p_depth_buffer_sampler, RID p_color_buffer);
  193. void begin(const Transform3D &p_view_transform, const Projection &p_cam_projection, bool p_flip_y);
  194. _FORCE_INLINE_ void add_light(LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture) {
  195. if (p_type == LIGHT_TYPE_OMNI && cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT] == max_elements_by_type) {
  196. return; // Max number elements reached.
  197. }
  198. if (p_type == LIGHT_TYPE_SPOT && cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT] == max_elements_by_type) {
  199. return; // Max number elements reached.
  200. }
  201. RenderElementData &e = render_elements[render_element_count];
  202. Transform3D xform = view_xform * p_transform;
  203. float radius = xform.basis.get_uniform_scale();
  204. if (radius < 0.98 || radius > 1.02) {
  205. xform.basis.orthonormalize();
  206. }
  207. radius *= p_radius;
  208. if (p_type == LIGHT_TYPE_OMNI) {
  209. radius *= shared->sphere_overfit; // Overfit icosphere.
  210. float depth = -xform.origin.z;
  211. if (camera_orthogonal) {
  212. e.touches_near = (depth - radius) < z_near;
  213. } else {
  214. // Contains camera inside light.
  215. float radius2 = radius * shared->sphere_overfit; // Overfit again for outer size (camera may be outside actual sphere but behind an icosphere vertex)
  216. e.touches_near = xform.origin.length_squared() < radius2 * radius2;
  217. }
  218. e.touches_far = (depth + radius) > z_far;
  219. e.scale[0] = radius;
  220. e.scale[1] = radius;
  221. e.scale[2] = radius;
  222. e.type = ELEMENT_TYPE_OMNI_LIGHT;
  223. e.original_index = cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT];
  224. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  225. cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT]++;
  226. } else /*LIGHT_TYPE_SPOT */ {
  227. radius *= shared->cone_overfit; // Overfit icosphere
  228. real_t len = Math::tan(Math::deg_to_rad(p_spot_aperture)) * radius;
  229. // Approximate, probably better to use a cone support function.
  230. float max_d = -1e20;
  231. float min_d = 1e20;
  232. #define CONE_MINMAX(m_x, m_y) \
  233. { \
  234. float d = -xform.xform(Vector3(len * m_x, len * m_y, -radius)).z; \
  235. min_d = MIN(d, min_d); \
  236. max_d = MAX(d, max_d); \
  237. }
  238. CONE_MINMAX(1, 1);
  239. CONE_MINMAX(-1, 1);
  240. CONE_MINMAX(-1, -1);
  241. CONE_MINMAX(1, -1);
  242. if (camera_orthogonal) {
  243. e.touches_near = min_d < z_near;
  244. } else {
  245. Plane base_plane(-xform.basis.get_column(Vector3::AXIS_Z), xform.origin);
  246. float dist = base_plane.distance_to(Vector3());
  247. if (dist >= 0 && dist < radius) {
  248. // Contains camera inside light, check angle.
  249. float angle = Math::rad_to_deg(Math::acos((-xform.origin.normalized()).dot(-xform.basis.get_column(Vector3::AXIS_Z))));
  250. e.touches_near = angle < p_spot_aperture * 1.05; //overfit aperture a little due to cone overfit
  251. } else {
  252. e.touches_near = false;
  253. }
  254. }
  255. e.touches_far = max_d > z_far;
  256. // If the spot angle is above the threshold, use a sphere instead of a cone for building the clusters
  257. // since the cone gets too flat/large (spot angle close to 90 degrees) or
  258. // can't even cover the affected area of the light (spot angle above 90 degrees).
  259. if (p_spot_aperture > WIDE_SPOT_ANGLE_THRESHOLD_DEG) {
  260. e.scale[0] = radius;
  261. e.scale[1] = radius;
  262. e.scale[2] = radius;
  263. e.has_wide_spot_angle = true;
  264. } else {
  265. e.scale[0] = len * shared->cone_overfit;
  266. e.scale[1] = len * shared->cone_overfit;
  267. e.scale[2] = radius;
  268. e.has_wide_spot_angle = false;
  269. }
  270. e.type = ELEMENT_TYPE_SPOT_LIGHT;
  271. e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]; // Use omni light since they share index.
  272. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  273. cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]++;
  274. }
  275. render_element_count++;
  276. }
  277. _FORCE_INLINE_ void add_box(BoxType p_box_type, const Transform3D &p_transform, const Vector3 &p_half_size) {
  278. if (p_box_type == BOX_TYPE_DECAL && cluster_count_by_type[ELEMENT_TYPE_DECAL] == max_elements_by_type) {
  279. return; // Max number elements reached.
  280. }
  281. if (p_box_type == BOX_TYPE_REFLECTION_PROBE && cluster_count_by_type[ELEMENT_TYPE_REFLECTION_PROBE] == max_elements_by_type) {
  282. return; // Max number elements reached.
  283. }
  284. RenderElementData &e = render_elements[render_element_count];
  285. Transform3D xform = view_xform * p_transform;
  286. // Extract scale and scale the matrix by it, makes things simpler.
  287. Vector3 scale = p_half_size;
  288. for (uint32_t i = 0; i < 3; i++) {
  289. float s = xform.basis.rows[i].length();
  290. scale[i] *= s;
  291. xform.basis.rows[i] /= s;
  292. };
  293. float box_depth = Math::abs(xform.basis.xform_inv(Vector3(0, 0, -1)).dot(scale));
  294. float depth = -xform.origin.z;
  295. if (camera_orthogonal) {
  296. e.touches_near = depth - box_depth < z_near;
  297. } else {
  298. // Contains camera inside box.
  299. Vector3 inside = xform.xform_inv(Vector3(0, 0, 0)).abs();
  300. e.touches_near = inside.x < scale.x && inside.y < scale.y && inside.z < scale.z;
  301. }
  302. e.touches_far = depth + box_depth > z_far;
  303. e.scale[0] = scale.x;
  304. e.scale[1] = scale.y;
  305. e.scale[2] = scale.z;
  306. e.type = (p_box_type == BOX_TYPE_DECAL) ? ELEMENT_TYPE_DECAL : ELEMENT_TYPE_REFLECTION_PROBE;
  307. e.original_index = cluster_count_by_type[e.type];
  308. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  309. cluster_count_by_type[e.type]++;
  310. render_element_count++;
  311. }
  312. void bake_cluster();
  313. void debug(ElementType p_element);
  314. RID get_cluster_buffer() const;
  315. uint32_t get_cluster_size() const;
  316. uint32_t get_max_cluster_elements() const;
  317. void set_shared(ClusterBuilderSharedDataRD *p_shared);
  318. ClusterBuilderRD();
  319. ~ClusterBuilderRD();
  320. };
  321. #endif // CLUSTER_BUILDER_RD_H