cluster_builder_rd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. bool use_msaa = true;
  158. Divisor divisor = DIVISOR_4;
  159. Size2i screen_size;
  160. Size2i cluster_screen_size;
  161. RID framebuffer;
  162. RID cluster_render_buffer; // Used for creating.
  163. RID cluster_buffer; // Used for rendering.
  164. RID element_buffer; // Used for storing, to hint element touches far plane or near plane.
  165. uint32_t cluster_render_buffer_size = 0;
  166. uint32_t cluster_buffer_size = 0;
  167. RID cluster_render_uniform_set;
  168. RID cluster_store_uniform_set;
  169. // Persistent data.
  170. void _clear();
  171. struct StateUniform {
  172. float projection[16];
  173. float inv_z_far;
  174. uint32_t screen_to_clusters_shift; // Shift to obtain coordinates in block indices.
  175. uint32_t cluster_screen_width;
  176. uint32_t cluster_data_size; // How much data is needed for a single cluster.
  177. uint32_t cluster_depth_offset;
  178. uint32_t pad0;
  179. uint32_t pad1;
  180. uint32_t pad2;
  181. };
  182. RID state_uniform;
  183. RID debug_uniform_set;
  184. public:
  185. void setup(Size2i p_screen_size, uint32_t p_max_elements, RID p_depth_buffer, RID p_depth_buffer_sampler, RID p_color_buffer);
  186. void begin(const Transform3D &p_view_transform, const Projection &p_cam_projection, bool p_flip_y);
  187. _FORCE_INLINE_ void add_light(LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture) {
  188. if (p_type == LIGHT_TYPE_OMNI && cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT] == max_elements_by_type) {
  189. return; // Max number elements reached.
  190. }
  191. if (p_type == LIGHT_TYPE_SPOT && cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT] == max_elements_by_type) {
  192. return; // Max number elements reached.
  193. }
  194. RenderElementData &e = render_elements[render_element_count];
  195. Transform3D xform = view_xform * p_transform;
  196. float radius = xform.basis.get_uniform_scale();
  197. if (radius < 0.98 || radius > 1.02) {
  198. xform.basis.orthonormalize();
  199. }
  200. radius *= p_radius;
  201. if (p_type == LIGHT_TYPE_OMNI) {
  202. radius *= shared->sphere_overfit; // Overfit icosphere.
  203. float depth = -xform.origin.z;
  204. if (camera_orthogonal) {
  205. e.touches_near = (depth - radius) < z_near;
  206. } else {
  207. // Contains camera inside light.
  208. float radius2 = radius * shared->sphere_overfit; // Overfit again for outer size (camera may be outside actual sphere but behind an icosphere vertex)
  209. e.touches_near = xform.origin.length_squared() < radius2 * radius2;
  210. }
  211. e.touches_far = (depth + radius) > z_far;
  212. e.scale[0] = radius;
  213. e.scale[1] = radius;
  214. e.scale[2] = radius;
  215. e.type = ELEMENT_TYPE_OMNI_LIGHT;
  216. e.original_index = cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT];
  217. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  218. cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT]++;
  219. } else /*LIGHT_TYPE_SPOT */ {
  220. radius *= shared->cone_overfit; // Overfit icosphere
  221. real_t len = Math::tan(Math::deg_to_rad(p_spot_aperture)) * radius;
  222. // Approximate, probably better to use a cone support function.
  223. float max_d = -1e20;
  224. float min_d = 1e20;
  225. #define CONE_MINMAX(m_x, m_y) \
  226. { \
  227. float d = -xform.xform(Vector3(len * m_x, len * m_y, -radius)).z; \
  228. min_d = MIN(d, min_d); \
  229. max_d = MAX(d, max_d); \
  230. }
  231. CONE_MINMAX(1, 1);
  232. CONE_MINMAX(-1, 1);
  233. CONE_MINMAX(-1, -1);
  234. CONE_MINMAX(1, -1);
  235. if (camera_orthogonal) {
  236. e.touches_near = min_d < z_near;
  237. } else {
  238. Plane base_plane(-xform.basis.get_column(Vector3::AXIS_Z), xform.origin);
  239. float dist = base_plane.distance_to(Vector3());
  240. if (dist >= 0 && dist < radius) {
  241. // Contains camera inside light, check angle.
  242. float angle = Math::rad_to_deg(Math::acos((-xform.origin.normalized()).dot(-xform.basis.get_column(Vector3::AXIS_Z))));
  243. e.touches_near = angle < p_spot_aperture * 1.05; //overfit aperture a little due to cone overfit
  244. } else {
  245. e.touches_near = false;
  246. }
  247. }
  248. e.touches_far = max_d > z_far;
  249. // If the spot angle is above the threshold, use a sphere instead of a cone for building the clusters
  250. // since the cone gets too flat/large (spot angle close to 90 degrees) or
  251. // can't even cover the affected area of the light (spot angle above 90 degrees).
  252. if (p_spot_aperture > WIDE_SPOT_ANGLE_THRESHOLD_DEG) {
  253. e.scale[0] = radius;
  254. e.scale[1] = radius;
  255. e.scale[2] = radius;
  256. e.has_wide_spot_angle = true;
  257. } else {
  258. e.scale[0] = len * shared->cone_overfit;
  259. e.scale[1] = len * shared->cone_overfit;
  260. e.scale[2] = radius;
  261. e.has_wide_spot_angle = false;
  262. }
  263. e.type = ELEMENT_TYPE_SPOT_LIGHT;
  264. e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]; // Use omni light since they share index.
  265. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  266. cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]++;
  267. }
  268. render_element_count++;
  269. }
  270. _FORCE_INLINE_ void add_box(BoxType p_box_type, const Transform3D &p_transform, const Vector3 &p_half_size) {
  271. if (p_box_type == BOX_TYPE_DECAL && cluster_count_by_type[ELEMENT_TYPE_DECAL] == max_elements_by_type) {
  272. return; // Max number elements reached.
  273. }
  274. if (p_box_type == BOX_TYPE_REFLECTION_PROBE && cluster_count_by_type[ELEMENT_TYPE_REFLECTION_PROBE] == max_elements_by_type) {
  275. return; // Max number elements reached.
  276. }
  277. RenderElementData &e = render_elements[render_element_count];
  278. Transform3D xform = view_xform * p_transform;
  279. // Extract scale and scale the matrix by it, makes things simpler.
  280. Vector3 scale = p_half_size;
  281. for (uint32_t i = 0; i < 3; i++) {
  282. float s = xform.basis.rows[i].length();
  283. scale[i] *= s;
  284. xform.basis.rows[i] /= s;
  285. };
  286. float box_depth = Math::abs(xform.basis.xform_inv(Vector3(0, 0, -1)).dot(scale));
  287. float depth = -xform.origin.z;
  288. if (camera_orthogonal) {
  289. e.touches_near = depth - box_depth < z_near;
  290. } else {
  291. // Contains camera inside box.
  292. Vector3 inside = xform.xform_inv(Vector3(0, 0, 0)).abs();
  293. e.touches_near = inside.x < scale.x && inside.y < scale.y && inside.z < scale.z;
  294. }
  295. e.touches_far = depth + box_depth > z_far;
  296. e.scale[0] = scale.x;
  297. e.scale[1] = scale.y;
  298. e.scale[2] = scale.z;
  299. e.type = (p_box_type == BOX_TYPE_DECAL) ? ELEMENT_TYPE_DECAL : ELEMENT_TYPE_REFLECTION_PROBE;
  300. e.original_index = cluster_count_by_type[e.type];
  301. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv);
  302. cluster_count_by_type[e.type]++;
  303. render_element_count++;
  304. }
  305. void bake_cluster();
  306. void debug(ElementType p_element);
  307. RID get_cluster_buffer() const;
  308. uint32_t get_cluster_size() const;
  309. uint32_t get_max_cluster_elements() const;
  310. void set_shared(ClusterBuilderSharedDataRD *p_shared);
  311. ClusterBuilderRD();
  312. ~ClusterBuilderRD();
  313. };
  314. #endif // CLUSTER_BUILDER_RD_H