lightmapper_rd.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 MeshInstance {
  40. MeshData data;
  41. int slice = 0;
  42. Vector2i offset;
  43. };
  44. struct Light {
  45. float position[3] = {};
  46. uint32_t type = LIGHT_TYPE_DIRECTIONAL;
  47. float direction[3] = {};
  48. float energy = 0.0;
  49. float color[3] = {};
  50. float size = 0.0;
  51. float range = 0.0;
  52. float attenuation = 0.0;
  53. float cos_spot_angle = 0.0;
  54. float inv_spot_attenuation = 0.0;
  55. float shadow_blur = 0.0;
  56. uint32_t static_bake = 0;
  57. uint32_t pad[2] = {};
  58. bool operator<(const Light &p_light) const {
  59. return type < p_light.type;
  60. }
  61. };
  62. struct Vertex {
  63. float position[3] = {};
  64. float normal_z = 0.0;
  65. float uv[2] = {};
  66. float normal_xy[2] = {};
  67. bool operator==(const Vertex &p_vtx) const {
  68. return (position[0] == p_vtx.position[0]) &&
  69. (position[1] == p_vtx.position[1]) &&
  70. (position[2] == p_vtx.position[2]) &&
  71. (uv[0] == p_vtx.uv[0]) &&
  72. (uv[1] == p_vtx.uv[1]) &&
  73. (normal_xy[0] == p_vtx.normal_xy[0]) &&
  74. (normal_xy[1] == p_vtx.normal_xy[1]) &&
  75. (normal_z == p_vtx.normal_z);
  76. }
  77. };
  78. struct Edge {
  79. Vector3 a;
  80. Vector3 b;
  81. Vector3 na;
  82. Vector3 nb;
  83. bool operator==(const Edge &p_seam) const {
  84. return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;
  85. }
  86. Edge() {
  87. }
  88. Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {
  89. a = p_a;
  90. b = p_b;
  91. na = p_na;
  92. nb = p_nb;
  93. }
  94. };
  95. struct Probe {
  96. float position[4] = {};
  97. };
  98. Vector<Probe> probe_positions;
  99. struct EdgeHash {
  100. _FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {
  101. uint32_t h = hash_murmur3_one_float(p_edge.a.x);
  102. h = hash_murmur3_one_float(p_edge.a.y, h);
  103. h = hash_murmur3_one_float(p_edge.a.z, h);
  104. h = hash_murmur3_one_float(p_edge.b.x, h);
  105. h = hash_murmur3_one_float(p_edge.b.y, h);
  106. h = hash_murmur3_one_float(p_edge.b.z, h);
  107. return h;
  108. }
  109. };
  110. struct EdgeUV2 {
  111. Vector2 a;
  112. Vector2 b;
  113. Vector2i indices;
  114. bool operator==(const EdgeUV2 &p_uv2) const {
  115. return a == p_uv2.a && b == p_uv2.b;
  116. }
  117. bool seam_found = false;
  118. EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {
  119. a = p_a;
  120. b = p_b;
  121. indices = p_indices;
  122. }
  123. EdgeUV2() {}
  124. };
  125. struct Seam {
  126. Vector2i a;
  127. Vector2i b;
  128. uint32_t slice;
  129. bool operator<(const Seam &p_seam) const {
  130. return slice < p_seam.slice;
  131. }
  132. };
  133. struct VertexHash {
  134. _FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {
  135. uint32_t h = hash_murmur3_one_float(p_vtx.position[0]);
  136. h = hash_murmur3_one_float(p_vtx.position[1], h);
  137. h = hash_murmur3_one_float(p_vtx.position[2], h);
  138. h = hash_murmur3_one_float(p_vtx.uv[0], h);
  139. h = hash_murmur3_one_float(p_vtx.uv[1], h);
  140. h = hash_murmur3_one_float(p_vtx.normal_xy[0], h);
  141. h = hash_murmur3_one_float(p_vtx.normal_xy[1], h);
  142. h = hash_murmur3_one_float(p_vtx.normal_z, h);
  143. return hash_fmix32(h);
  144. }
  145. };
  146. struct Triangle {
  147. uint32_t indices[3] = {};
  148. uint32_t slice = 0;
  149. float min_bounds[3] = {};
  150. float pad0 = 0.0;
  151. float max_bounds[3] = {};
  152. float pad1 = 0.0;
  153. bool operator<(const Triangle &p_triangle) const {
  154. return slice < p_triangle.slice;
  155. }
  156. };
  157. Vector<MeshInstance> mesh_instances;
  158. Vector<Light> lights;
  159. struct TriangleSort {
  160. uint32_t cell_index = 0;
  161. uint32_t triangle_index = 0;
  162. bool operator<(const TriangleSort &p_triangle_sort) const {
  163. return cell_index < p_triangle_sort.cell_index; //sorting by triangle index in this case makes no sense
  164. }
  165. };
  166. 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);
  167. struct RasterPushConstant {
  168. float atlas_size[2] = {};
  169. float uv_offset[2] = {};
  170. float to_cell_size[3] = {};
  171. uint32_t base_triangle = 0;
  172. float to_cell_offset[3] = {};
  173. float bias = 0.0;
  174. int32_t grid_size[3] = {};
  175. uint32_t pad2 = 0;
  176. };
  177. struct RasterSeamsPushConstant {
  178. uint32_t base_index = 0;
  179. uint32_t slice = 0;
  180. float uv_offset[2] = {};
  181. uint32_t debug = 0;
  182. float blend = 0.0;
  183. uint32_t pad[2] = {};
  184. };
  185. struct PushConstant {
  186. int32_t atlas_size[2] = {};
  187. uint32_t ray_count = 0;
  188. uint32_t ray_to = 0;
  189. float world_size[3] = {};
  190. float bias = 0.0;
  191. float to_cell_offset[3] = {};
  192. uint32_t ray_from = 0;
  193. float to_cell_size[3] = {};
  194. uint32_t light_count = 0;
  195. int32_t grid_size = 0;
  196. int32_t atlas_slice = 0;
  197. int32_t region_ofs[2] = {};
  198. float environment_xform[12] = {};
  199. };
  200. Vector<Ref<Image>> bake_textures;
  201. Vector<Color> probe_values;
  202. 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);
  203. void _create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_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 &triangle_cell_indices_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata);
  204. 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);
  205. 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);
  206. public:
  207. virtual void add_mesh(const MeshData &p_mesh) override;
  208. virtual void add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_angular_distance, float p_shadow_blur) override;
  209. virtual void add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) override;
  210. 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_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) override;
  211. virtual void add_probe(const Vector3 &p_position) override;
  212. virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, int p_bounces, float p_bias, int p_max_texture_size, bool p_bake_sh, 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;
  213. int get_bake_texture_count() const override;
  214. Ref<Image> get_bake_texture(int p_index) const override;
  215. int get_bake_mesh_count() const override;
  216. Variant get_bake_mesh_userdata(int p_index) const override;
  217. Rect2 get_bake_mesh_uv_scale(int p_index) const override;
  218. int get_bake_mesh_texture_slice(int p_index) const override;
  219. int get_bake_probe_count() const override;
  220. Vector3 get_bake_probe_point(int p_probe) const override;
  221. Vector<Color> get_bake_probe_sh(int p_probe) const override;
  222. LightmapperRD();
  223. };
  224. #endif // LIGHTMAPPER_RD_H