lightmapper_rd.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*************************************************************************/
  2. /* lightmapper_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 LightmapperRD : public Lightmapper {
  37. GDCLASS(LightmapperRD, Lightmapper)
  38. struct MeshInstance {
  39. MeshData data;
  40. int slice = 0;
  41. Vector2i offset;
  42. };
  43. struct Light {
  44. float position[3] = {};
  45. uint32_t type = LIGHT_TYPE_DIRECTIONAL;
  46. float direction[3] = {};
  47. float energy = 0.0;
  48. float color[3] = {};
  49. float size = 0.0;
  50. float range = 0.0;
  51. float attenuation = 0.0;
  52. float cos_spot_angle = 0.0;
  53. float inv_spot_attenuation = 0.0;
  54. uint32_t static_bake = 0;
  55. uint32_t pad[3] = {};
  56. bool operator<(const Light &p_light) const {
  57. return type < p_light.type;
  58. }
  59. };
  60. struct Vertex {
  61. float position[3] = {};
  62. float normal_z = 0.0;
  63. float uv[2] = {};
  64. float normal_xy[2] = {};
  65. bool operator==(const Vertex &p_vtx) const {
  66. return (position[0] == p_vtx.position[0]) &&
  67. (position[1] == p_vtx.position[1]) &&
  68. (position[2] == p_vtx.position[2]) &&
  69. (uv[0] == p_vtx.uv[0]) &&
  70. (uv[1] == p_vtx.uv[1]) &&
  71. (normal_xy[0] == p_vtx.normal_xy[0]) &&
  72. (normal_xy[1] == p_vtx.normal_xy[1]) &&
  73. (normal_z == p_vtx.normal_z);
  74. }
  75. };
  76. struct Edge {
  77. Vector3 a;
  78. Vector3 b;
  79. Vector3 na;
  80. Vector3 nb;
  81. bool operator==(const Edge &p_seam) const {
  82. return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;
  83. }
  84. Edge() {
  85. }
  86. Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {
  87. a = p_a;
  88. b = p_b;
  89. na = p_na;
  90. nb = p_nb;
  91. }
  92. };
  93. struct Probe {
  94. float position[4] = {};
  95. };
  96. Vector<Probe> probe_positions;
  97. struct EdgeHash {
  98. _FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {
  99. uint32_t h = hash_djb2_one_float(p_edge.a.x);
  100. h = hash_djb2_one_float(p_edge.a.y, h);
  101. h = hash_djb2_one_float(p_edge.a.z, h);
  102. h = hash_djb2_one_float(p_edge.b.x, h);
  103. h = hash_djb2_one_float(p_edge.b.y, h);
  104. h = hash_djb2_one_float(p_edge.b.z, h);
  105. return h;
  106. }
  107. };
  108. struct EdgeUV2 {
  109. Vector2 a;
  110. Vector2 b;
  111. Vector2i indices;
  112. bool operator==(const EdgeUV2 &p_uv2) const {
  113. return a == p_uv2.a && b == p_uv2.b;
  114. }
  115. bool seam_found = false;
  116. EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {
  117. a = p_a;
  118. b = p_b;
  119. indices = p_indices;
  120. }
  121. EdgeUV2() {}
  122. };
  123. struct Seam {
  124. Vector2i a;
  125. Vector2i b;
  126. uint32_t slice;
  127. bool operator<(const Seam &p_seam) const {
  128. return slice < p_seam.slice;
  129. }
  130. };
  131. struct VertexHash {
  132. _FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {
  133. uint32_t h = hash_djb2_one_float(p_vtx.position[0]);
  134. h = hash_djb2_one_float(p_vtx.position[1], h);
  135. h = hash_djb2_one_float(p_vtx.position[2], h);
  136. h = hash_djb2_one_float(p_vtx.uv[0], h);
  137. h = hash_djb2_one_float(p_vtx.uv[1], h);
  138. h = hash_djb2_one_float(p_vtx.normal_xy[0], h);
  139. h = hash_djb2_one_float(p_vtx.normal_xy[1], h);
  140. h = hash_djb2_one_float(p_vtx.normal_z, h);
  141. return h;
  142. }
  143. };
  144. struct Box {
  145. float min_bounds[3] = {};
  146. float pad0 = 0.0;
  147. float max_bounds[3] = {};
  148. float pad1 = 0.0;
  149. };
  150. struct Triangle {
  151. uint32_t indices[3] = {};
  152. uint32_t slice = 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[], 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 &box_buffer, RID &lights_buffer, RID &triangle_cell_indices_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &grid_texture_sdf, 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. public:
  206. virtual void add_mesh(const MeshData &p_mesh) override;
  207. virtual void add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_angular_distance) override;
  208. 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) override;
  209. 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) override;
  210. virtual void add_probe(const Vector3 &p_position) override;
  211. 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) override;
  212. int get_bake_texture_count() const override;
  213. Ref<Image> get_bake_texture(int p_index) const override;
  214. int get_bake_mesh_count() const override;
  215. Variant get_bake_mesh_userdata(int p_index) const override;
  216. Rect2 get_bake_mesh_uv_scale(int p_index) const override;
  217. int get_bake_mesh_texture_slice(int p_index) const override;
  218. int get_bake_probe_count() const override;
  219. Vector3 get_bake_probe_point(int p_probe) const override;
  220. Vector<Color> get_bake_probe_sh(int p_probe) const override;
  221. LightmapperRD();
  222. };
  223. #endif // LIGHTMAPPER_H