lightmapper_rd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 shadowmask_light_idx = 0;
  52. uint32_t transparency_rays = 0;
  53. float supersampling_factor = 0.0f;
  54. };
  55. struct MeshInstance {
  56. MeshData data;
  57. int slice = 0;
  58. Vector2i offset;
  59. };
  60. struct Light {
  61. float position[3] = {};
  62. uint32_t type = LIGHT_TYPE_DIRECTIONAL;
  63. float direction[3] = {};
  64. float energy = 0.0;
  65. float color[3] = {};
  66. float size = 0.0;
  67. float range = 0.0;
  68. float attenuation = 0.0;
  69. float cos_spot_angle = 0.0;
  70. float inv_spot_attenuation = 0.0;
  71. float indirect_energy = 0.0;
  72. float shadow_blur = 0.0;
  73. uint32_t static_bake = 0;
  74. uint32_t pad = 0;
  75. bool operator<(const Light &p_light) const {
  76. return type < p_light.type;
  77. }
  78. };
  79. struct Vertex {
  80. float position[3] = {};
  81. float normal_z = 0.0;
  82. float uv[2] = {};
  83. float normal_xy[2] = {};
  84. bool operator==(const Vertex &p_vtx) const {
  85. return (position[0] == p_vtx.position[0]) &&
  86. (position[1] == p_vtx.position[1]) &&
  87. (position[2] == p_vtx.position[2]) &&
  88. (uv[0] == p_vtx.uv[0]) &&
  89. (uv[1] == p_vtx.uv[1]) &&
  90. (normal_xy[0] == p_vtx.normal_xy[0]) &&
  91. (normal_xy[1] == p_vtx.normal_xy[1]) &&
  92. (normal_z == p_vtx.normal_z);
  93. }
  94. };
  95. struct Edge {
  96. Vector3 a;
  97. Vector3 b;
  98. Vector3 na;
  99. Vector3 nb;
  100. bool operator==(const Edge &p_seam) const {
  101. return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;
  102. }
  103. Edge() {
  104. }
  105. Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {
  106. a = p_a;
  107. b = p_b;
  108. na = p_na;
  109. nb = p_nb;
  110. }
  111. };
  112. struct Probe {
  113. float position[4] = {};
  114. };
  115. Vector<Probe> probe_positions;
  116. struct EdgeHash {
  117. _FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {
  118. uint32_t h = hash_murmur3_one_float(p_edge.a.x);
  119. h = hash_murmur3_one_float(p_edge.a.y, h);
  120. h = hash_murmur3_one_float(p_edge.a.z, h);
  121. h = hash_murmur3_one_float(p_edge.b.x, h);
  122. h = hash_murmur3_one_float(p_edge.b.y, h);
  123. h = hash_murmur3_one_float(p_edge.b.z, h);
  124. return h;
  125. }
  126. };
  127. struct EdgeUV2 {
  128. Vector2 a;
  129. Vector2 b;
  130. Vector2i indices;
  131. bool operator==(const EdgeUV2 &p_uv2) const {
  132. return a == p_uv2.a && b == p_uv2.b;
  133. }
  134. bool seam_found = false;
  135. EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {
  136. a = p_a;
  137. b = p_b;
  138. indices = p_indices;
  139. }
  140. EdgeUV2() {}
  141. };
  142. struct Seam {
  143. Vector2i a;
  144. Vector2i b;
  145. uint32_t slice;
  146. bool operator<(const Seam &p_seam) const {
  147. return slice < p_seam.slice;
  148. }
  149. };
  150. struct VertexHash {
  151. _FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {
  152. uint32_t h = hash_murmur3_one_float(p_vtx.position[0]);
  153. h = hash_murmur3_one_float(p_vtx.position[1], h);
  154. h = hash_murmur3_one_float(p_vtx.position[2], h);
  155. h = hash_murmur3_one_float(p_vtx.uv[0], h);
  156. h = hash_murmur3_one_float(p_vtx.uv[1], h);
  157. h = hash_murmur3_one_float(p_vtx.normal_xy[0], h);
  158. h = hash_murmur3_one_float(p_vtx.normal_xy[1], h);
  159. h = hash_murmur3_one_float(p_vtx.normal_z, h);
  160. return hash_fmix32(h);
  161. }
  162. };
  163. struct Triangle {
  164. uint32_t indices[3] = {};
  165. uint32_t slice = 0;
  166. float min_bounds[3] = {};
  167. uint32_t cull_mode = 0;
  168. float max_bounds[3] = {};
  169. float pad1 = 0.0;
  170. bool operator<(const Triangle &p_triangle) const {
  171. return slice < p_triangle.slice;
  172. }
  173. };
  174. struct ClusterAABB {
  175. float min_bounds[3];
  176. float pad0 = 0.0f;
  177. float max_bounds[3];
  178. float pad1 = 0.0f;
  179. };
  180. Vector<MeshInstance> mesh_instances;
  181. Vector<Light> lights;
  182. Vector<String> light_names;
  183. struct TriangleSort {
  184. uint32_t cell_index = 0;
  185. uint32_t triangle_index = 0;
  186. AABB triangle_aabb;
  187. bool operator<(const TriangleSort &p_triangle_sort) const {
  188. return cell_index < p_triangle_sort.cell_index; //sorting by triangle index in this case makes no sense
  189. }
  190. };
  191. template <int T>
  192. struct TriangleSortAxis {
  193. bool operator()(const TriangleSort &p_a, const TriangleSort &p_b) const {
  194. return p_a.triangle_aabb.get_center()[T] < p_b.triangle_aabb.get_center()[T];
  195. }
  196. };
  197. 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);
  198. 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);
  199. struct RasterPushConstant {
  200. float atlas_size[2] = {};
  201. float uv_offset[2] = {};
  202. float to_cell_size[3] = {};
  203. uint32_t base_triangle = 0;
  204. float to_cell_offset[3] = {};
  205. float bias = 0.0;
  206. int32_t grid_size[3] = {};
  207. uint32_t pad2 = 0;
  208. };
  209. struct RasterSeamsPushConstant {
  210. uint32_t base_index = 0;
  211. uint32_t slice = 0;
  212. float uv_offset[2] = {};
  213. uint32_t debug = 0;
  214. float blend = 0.0;
  215. uint32_t pad[2] = {};
  216. };
  217. struct PushConstant {
  218. uint32_t atlas_slice = 0;
  219. uint32_t ray_count = 0;
  220. uint32_t ray_from = 0;
  221. uint32_t ray_to = 0;
  222. uint32_t region_ofs[2] = {};
  223. uint32_t probe_count = 0;
  224. uint32_t pad = 0;
  225. };
  226. Vector<Ref<Image>> lightmap_textures;
  227. Vector<Ref<Image>> shadowmask_textures;
  228. Vector<Color> probe_values;
  229. struct DilateParams {
  230. uint32_t radius;
  231. uint32_t pad[3];
  232. };
  233. struct DenoiseParams {
  234. float spatial_bandwidth;
  235. float light_bandwidth;
  236. float albedo_bandwidth;
  237. float normal_bandwidth;
  238. int half_search_window;
  239. float filter_strength;
  240. float pad[2];
  241. };
  242. BakeError _blit_meshes_into_atlas(int p_max_texture_size, int p_denoiser_range, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, float p_supersampling_factor, BakeStepFunc p_step_function, void *p_bake_userdata);
  243. 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);
  244. 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);
  245. 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);
  246. 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, int p_denoiser_range, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function, void *p_bake_userdata);
  247. BakeError _pack_l1(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);
  248. Error _store_pfm(RenderingDevice *p_rd, RID p_atlas_tex, int p_index, const Size2i &p_atlas_size, const String &p_name, bool p_shadowmask);
  249. Ref<Image> _read_pfm(const String &p_name, bool p_shadowmask);
  250. 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, bool p_shadowmask, const String &p_exe);
  251. public:
  252. virtual void add_mesh(const MeshData &p_mesh) override;
  253. virtual void add_directional_light(const String &p_name, 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;
  254. virtual void add_omni_light(const String &p_name, 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;
  255. virtual void add_spot_light(const String &p_name, 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;
  256. virtual void add_probe(const Vector3 &p_position) override;
  257. virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_bake_shadowmask, 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, float p_supersampling_factor = 1.0f) override;
  258. int get_bake_texture_count() const override;
  259. Ref<Image> get_bake_texture(int p_index) const override;
  260. int get_shadowmask_texture_count() const override;
  261. Ref<Image> get_shadowmask_texture(int p_index) const override;
  262. int get_bake_mesh_count() const override;
  263. Variant get_bake_mesh_userdata(int p_index) const override;
  264. Rect2 get_bake_mesh_uv_scale(int p_index) const override;
  265. int get_bake_mesh_texture_slice(int p_index) const override;
  266. int get_bake_probe_count() const override;
  267. Vector3 get_bake_probe_point(int p_probe) const override;
  268. Vector<Color> get_bake_probe_sh(int p_probe) const override;
  269. LightmapperRD();
  270. };
  271. #endif // LIGHTMAPPER_RD_H