lightmapper_rd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************/
  2. /* lightmapper_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 LIGHTMAPPER_RD_H
  31. #define LIGHTMAPPER_RD_H
  32. #include "core/templates/local_vector.h"
  33. #include "scene/3d/lightmapper.h"
  34. #include "scene/resources/mesh.h"
  35. #include "servers/rendering/rendering_device.h"
  36. class RDShaderFile;
  37. class LightmapperRD : public Lightmapper {
  38. GDCLASS(LightmapperRD, Lightmapper)
  39. struct BakeParameters {
  40. float world_size[3] = {};
  41. float bias = 0.0;
  42. float to_cell_offset[3] = {};
  43. int32_t grid_size = 0;
  44. float to_cell_size[3] = {};
  45. uint32_t light_count = 0;
  46. float env_transform[12] = {};
  47. int32_t atlas_size[2] = {};
  48. float exposure_normalization = 0.0f;
  49. uint32_t bounces = 0;
  50. float bounce_indirect_energy = 0.0f;
  51. uint32_t pad[3] = {};
  52. };
  53. struct MeshInstance {
  54. MeshData data;
  55. int slice = 0;
  56. Vector2i offset;
  57. };
  58. struct Light {
  59. float position[3] = {};
  60. uint32_t type = LIGHT_TYPE_DIRECTIONAL;
  61. float direction[3] = {};
  62. float energy = 0.0;
  63. float color[3] = {};
  64. float size = 0.0;
  65. float range = 0.0;
  66. float attenuation = 0.0;
  67. float cos_spot_angle = 0.0;
  68. float inv_spot_attenuation = 0.0;
  69. float indirect_energy = 0.0;
  70. float shadow_blur = 0.0;
  71. uint32_t static_bake = 0;
  72. uint32_t pad = 0;
  73. bool operator<(const Light &p_light) const {
  74. return type < p_light.type;
  75. }
  76. };
  77. struct Vertex {
  78. float position[3] = {};
  79. float normal_z = 0.0;
  80. float uv[2] = {};
  81. float normal_xy[2] = {};
  82. bool operator==(const Vertex &p_vtx) const {
  83. return (position[0] == p_vtx.position[0]) &&
  84. (position[1] == p_vtx.position[1]) &&
  85. (position[2] == p_vtx.position[2]) &&
  86. (uv[0] == p_vtx.uv[0]) &&
  87. (uv[1] == p_vtx.uv[1]) &&
  88. (normal_xy[0] == p_vtx.normal_xy[0]) &&
  89. (normal_xy[1] == p_vtx.normal_xy[1]) &&
  90. (normal_z == p_vtx.normal_z);
  91. }
  92. };
  93. struct Edge {
  94. Vector3 a;
  95. Vector3 b;
  96. Vector3 na;
  97. Vector3 nb;
  98. bool operator==(const Edge &p_seam) const {
  99. return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;
  100. }
  101. Edge() {
  102. }
  103. Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {
  104. a = p_a;
  105. b = p_b;
  106. na = p_na;
  107. nb = p_nb;
  108. }
  109. };
  110. struct Probe {
  111. float position[4] = {};
  112. };
  113. Vector<Probe> probe_positions;
  114. struct EdgeHash {
  115. _FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {
  116. uint32_t h = hash_murmur3_one_float(p_edge.a.x);
  117. h = hash_murmur3_one_float(p_edge.a.y, h);
  118. h = hash_murmur3_one_float(p_edge.a.z, h);
  119. h = hash_murmur3_one_float(p_edge.b.x, h);
  120. h = hash_murmur3_one_float(p_edge.b.y, h);
  121. h = hash_murmur3_one_float(p_edge.b.z, h);
  122. return h;
  123. }
  124. };
  125. struct EdgeUV2 {
  126. Vector2 a;
  127. Vector2 b;
  128. Vector2i indices;
  129. bool operator==(const EdgeUV2 &p_uv2) const {
  130. return a == p_uv2.a && b == p_uv2.b;
  131. }
  132. bool seam_found = false;
  133. EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {
  134. a = p_a;
  135. b = p_b;
  136. indices = p_indices;
  137. }
  138. EdgeUV2() {}
  139. };
  140. struct Seam {
  141. Vector2i a;
  142. Vector2i b;
  143. uint32_t slice;
  144. bool operator<(const Seam &p_seam) const {
  145. return slice < p_seam.slice;
  146. }
  147. };
  148. struct VertexHash {
  149. _FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {
  150. uint32_t h = hash_murmur3_one_float(p_vtx.position[0]);
  151. h = hash_murmur3_one_float(p_vtx.position[1], h);
  152. h = hash_murmur3_one_float(p_vtx.position[2], h);
  153. h = hash_murmur3_one_float(p_vtx.uv[0], h);
  154. h = hash_murmur3_one_float(p_vtx.uv[1], h);
  155. h = hash_murmur3_one_float(p_vtx.normal_xy[0], h);
  156. h = hash_murmur3_one_float(p_vtx.normal_xy[1], h);
  157. h = hash_murmur3_one_float(p_vtx.normal_z, h);
  158. return hash_fmix32(h);
  159. }
  160. };
  161. struct Triangle {
  162. uint32_t indices[3] = {};
  163. uint32_t slice = 0;
  164. float min_bounds[3] = {};
  165. float pad0 = 0.0;
  166. float max_bounds[3] = {};
  167. float pad1 = 0.0;
  168. bool operator<(const Triangle &p_triangle) const {
  169. return slice < p_triangle.slice;
  170. }
  171. };
  172. struct ClusterAABB {
  173. float min_bounds[3];
  174. float pad0 = 0.0f;
  175. float max_bounds[3];
  176. float pad1 = 0.0f;
  177. };
  178. Vector<MeshInstance> mesh_instances;
  179. Vector<Light> lights;
  180. struct TriangleSort {
  181. uint32_t cell_index = 0;
  182. uint32_t triangle_index = 0;
  183. AABB triangle_aabb;
  184. bool operator<(const TriangleSort &p_triangle_sort) const {
  185. return cell_index < p_triangle_sort.cell_index; //sorting by triangle index in this case makes no sense
  186. }
  187. };
  188. template <int T>
  189. struct TriangleSortAxis {
  190. bool operator()(const TriangleSort &p_a, const TriangleSort &p_b) const {
  191. return p_a.triangle_aabb.get_center()[T] < p_b.triangle_aabb.get_center()[T];
  192. }
  193. };
  194. void _plot_triangle_into_triangle_index_list(int p_size, const Vector3i &p_ofs, const AABB &p_bounds, const Vector3 p_points[3], uint32_t p_triangle_index, LocalVector<TriangleSort> &triangles, uint32_t p_grid_size);
  195. void _sort_triangle_clusters(uint32_t p_cluster_size, uint32_t p_cluster_index, uint32_t p_index_start, uint32_t p_count, LocalVector<TriangleSort> &p_triangle_sort, LocalVector<ClusterAABB> &p_cluster_aabb);
  196. struct RasterPushConstant {
  197. float atlas_size[2] = {};
  198. float uv_offset[2] = {};
  199. float to_cell_size[3] = {};
  200. uint32_t base_triangle = 0;
  201. float to_cell_offset[3] = {};
  202. float bias = 0.0;
  203. int32_t grid_size[3] = {};
  204. uint32_t pad2 = 0;
  205. };
  206. struct RasterSeamsPushConstant {
  207. uint32_t base_index = 0;
  208. uint32_t slice = 0;
  209. float uv_offset[2] = {};
  210. uint32_t debug = 0;
  211. float blend = 0.0;
  212. uint32_t pad[2] = {};
  213. };
  214. struct PushConstant {
  215. uint32_t atlas_slice = 0;
  216. uint32_t ray_count = 0;
  217. uint32_t ray_from = 0;
  218. uint32_t ray_to = 0;
  219. uint32_t region_ofs[2] = {};
  220. uint32_t probe_count = 0;
  221. uint32_t pad = 0;
  222. };
  223. Vector<Ref<Image>> bake_textures;
  224. Vector<Color> probe_values;
  225. struct DenoiseParams {
  226. float spatial_bandwidth;
  227. float light_bandwidth;
  228. float albedo_bandwidth;
  229. float normal_bandwidth;
  230. float filter_strength;
  231. float pad[3];
  232. };
  233. BakeError _blit_meshes_into_atlas(int p_max_texture_size, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, BakeStepFunc p_step_function, void *p_bake_userdata);
  234. void _create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_size, uint32_t p_cluster_size, Vector<Probe> &probe_positions, GenerateProbes p_generate_probes, Vector<int> &slice_triangle_count, Vector<int> &slice_seam_count, RID &vertex_buffer, RID &triangle_buffer, RID &lights_buffer, RID &r_triangle_indices_buffer, RID &r_cluster_indices_buffer, RID &r_cluster_aabbs_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata);
  235. void _raster_geometry(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, int grid_size, AABB bounds, float p_bias, Vector<int> slice_triangle_count, RID position_tex, RID unocclude_tex, RID normal_tex, RID raster_depth_buffer, RID rasterize_shader, RID raster_base_uniform);
  236. BakeError _dilate(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices);
  237. BakeError _denoise(RenderingDevice *p_rd, Ref<RDShaderFile> &p_compute_shader, const RID &p_compute_base_uniform_set, PushConstant &p_push_constant, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, float p_denoiser_strength, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function);
  238. Error _store_pfm(RenderingDevice *p_rd, RID p_atlas_tex, int p_index, const Size2i &p_atlas_size, const String &p_name);
  239. Ref<Image> _read_pfm(const String &p_name);
  240. BakeError _denoise_oidn(RenderingDevice *p_rd, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, const String &p_exe);
  241. public:
  242. virtual void add_mesh(const MeshData &p_mesh) override;
  243. virtual void add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) override;
  244. virtual void add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) override;
  245. virtual void add_spot_light(bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) override;
  246. virtual void add_probe(const Vector3 &p_position) override;
  247. virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_bake_userdata = nullptr, float p_exposure_normalization = 1.0) override;
  248. int get_bake_texture_count() const override;
  249. Ref<Image> get_bake_texture(int p_index) const override;
  250. int get_bake_mesh_count() const override;
  251. Variant get_bake_mesh_userdata(int p_index) const override;
  252. Rect2 get_bake_mesh_uv_scale(int p_index) const override;
  253. int get_bake_mesh_texture_slice(int p_index) const override;
  254. int get_bake_probe_count() const override;
  255. Vector3 get_bake_probe_point(int p_probe) const override;
  256. Vector<Color> get_bake_probe_sh(int p_probe) const override;
  257. LightmapperRD();
  258. };
  259. #endif // LIGHTMAPPER_RD_H