lightmapper_cpu.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**************************************************************************/
  2. /* lightmapper_cpu.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_CPU_H
  31. #define LIGHTMAPPER_CPU_H
  32. #include "core/local_vector.h"
  33. #include "scene/3d/lightmapper.h"
  34. #include "scene/resources/mesh.h"
  35. #include "scene/resources/surface_tool.h"
  36. #include <atomic>
  37. class LightmapperCPU : public Lightmapper {
  38. GDCLASS(LightmapperCPU, Lightmapper)
  39. struct MeshInstance {
  40. MeshData data;
  41. int slice = 0;
  42. Vector2i offset;
  43. Vector2i size;
  44. bool cast_shadows;
  45. bool generate_lightmap;
  46. String node_name;
  47. };
  48. struct Light {
  49. Vector3 position;
  50. uint32_t type = LIGHT_TYPE_DIRECTIONAL;
  51. Vector3 direction;
  52. float energy;
  53. float indirect_multiplier;
  54. Color color;
  55. float range;
  56. float attenuation;
  57. float spot_angle;
  58. float spot_attenuation;
  59. float size;
  60. bool bake_direct;
  61. };
  62. struct LightmapTexel {
  63. Vector3 albedo;
  64. float alpha;
  65. Vector3 emission;
  66. Vector3 pos;
  67. Vector3 normal;
  68. Vector3 direct_light;
  69. Vector3 output_light;
  70. float area_coverage;
  71. };
  72. struct BakeParams {
  73. float bias;
  74. int bounces;
  75. float bounce_indirect_energy;
  76. int samples;
  77. bool use_denoiser = true;
  78. bool use_physical_light_attenuation = false;
  79. Ref<Image> environment_panorama;
  80. Basis environment_transform;
  81. };
  82. struct UVSeam {
  83. Vector2 edge0[2];
  84. Vector2 edge1[2];
  85. };
  86. struct SeamEdge {
  87. Vector3 pos[2];
  88. Vector3 normal[2];
  89. Vector2 uv[2];
  90. _FORCE_INLINE_ bool operator<(const SeamEdge &p_edge) const {
  91. return pos[0].x < p_edge.pos[0].x;
  92. }
  93. };
  94. struct AtlasOffset {
  95. int slice;
  96. int x;
  97. int y;
  98. };
  99. struct ThreadData;
  100. typedef void (LightmapperCPU::*BakeThreadFunc)(uint32_t, void *);
  101. struct ThreadData {
  102. LightmapperCPU *instance;
  103. uint32_t count;
  104. BakeThreadFunc thread_func;
  105. void *userdata;
  106. };
  107. BakeParams parameters;
  108. LocalVector<Ref<Image>> bake_textures;
  109. Map<RID, Ref<Image>> albedo_textures;
  110. Map<RID, Ref<Image>> emission_textures;
  111. LocalVector<MeshInstance> mesh_instances;
  112. LocalVector<Light> lights;
  113. LocalVector<LocalVector<LightmapTexel>> scene_lightmaps;
  114. LocalVector<LocalVector<int>> scene_lightmap_indices;
  115. Set<int> no_shadow_meshes;
  116. std::atomic<uint32_t> thread_progress;
  117. std::atomic<bool> thread_cancelled;
  118. Ref<LightmapRaycaster> raycaster;
  119. Error _layout_atlas(int p_max_size, Vector2i *r_atlas_size, int *r_atlas_slices);
  120. static void _thread_func_callback(void *p_thread_data);
  121. void _thread_func_wrapper(uint32_t p_idx, ThreadData *p_thread_data);
  122. bool _parallel_run(int p_count, const String &p_description, BakeThreadFunc p_thread_func, void *p_userdata, BakeStepFunc p_substep_func = nullptr);
  123. void _generate_buffer(uint32_t p_idx, void *p_unused);
  124. Ref<Image> _init_bake_texture(const MeshData::TextureDef &p_texture_def, const Map<RID, Ref<Image>> &p_tex_cache, Image::Format p_default_format);
  125. Color _bilinear_sample(const Ref<Image> &p_img, const Vector2 &p_uv, bool p_clamp_x = false, bool p_clamp_y = false);
  126. Vector3 _fix_sample_position(const Vector3 &p_position, const Vector3 &p_texel_center, const Vector3 &p_normal, const Vector3 &p_tangent, const Vector3 &p_bitangent, const Vector2 &p_texel_size);
  127. void _plot_triangle(const Vector2 *p_vertices, const Vector3 *p_positions, const Vector3 *p_normals, const Vector2 *p_uvs, const Ref<Image> &p_albedo_texture, const Ref<Image> &p_emission_texture, Vector2i p_size, LocalVector<LightmapTexel> &r_texels, LocalVector<int> &r_lightmap_indices);
  128. float _get_omni_attenuation(float distance, float inv_range, float decay) const;
  129. void _compute_direct_light(uint32_t p_idx, void *r_lightmap);
  130. void _compute_indirect_light(uint32_t p_idx, void *r_lightmap);
  131. void _post_process(uint32_t p_idx, void *r_output);
  132. void _compute_seams(const MeshInstance &p_mesh, LocalVector<UVSeam> &r_seams);
  133. void _fix_seams(const LocalVector<UVSeam> &p_seams, Vector3 *r_lightmap, Vector2i p_size);
  134. void _fix_seam(const Vector2 &p_pos0, const Vector2 &p_pos1, const Vector2 &p_uv0, const Vector2 &p_uv1, const Vector3 *p_read_buffer, Vector3 *r_write_buffer, const Vector2i &p_size);
  135. void _dilate_lightmap(Vector3 *r_lightmap, const LocalVector<int> p_indices, Vector2i p_size, int margin);
  136. void _blit_lightmap(const Vector<Vector3> &p_src, const Vector2i &p_size, Ref<Image> &p_dst, int p_x, int p_y, bool p_with_padding);
  137. public:
  138. virtual void add_albedo_texture(Ref<Texture> p_texture);
  139. virtual void add_emission_texture(Ref<Texture> p_texture);
  140. virtual void add_mesh(const MeshData &p_mesh, Vector2i p_size);
  141. virtual void add_directional_light(bool p_bake_direct, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_multiplier, float p_size);
  142. virtual void add_omni_light(bool p_bake_direct, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_multiplier, float p_range, float p_attenuation, float p_size);
  143. virtual void add_spot_light(bool p_bake_direct, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_multiplier, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size);
  144. virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, int p_bounces, float p_bounce_energy, float p_bias, bool p_generate_atlas, int p_max_texture_size, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_bake_userdata = nullptr, BakeStepFunc p_substep_function = nullptr);
  145. int get_bake_texture_count() const;
  146. Ref<Image> get_bake_texture(int p_index) const;
  147. int get_bake_mesh_count() const;
  148. Variant get_bake_mesh_userdata(int p_index) const;
  149. Rect2 get_bake_mesh_uv_scale(int p_index) const;
  150. int get_bake_mesh_texture_slice(int p_index) const;
  151. LightmapperCPU();
  152. };
  153. #endif // LIGHTMAPPER_CPU_H