Godot Engine – Multi-platform 2D and 3D game engine
Apply max texture size to the final lightmap instead of the supersampled textures
Hendrik Brucker755405c9b544
diff --git a/doc/classes/LightmapGI.xml b/doc/classes/LightmapGI.xmlindex 406b032a93c..a694baf362e 100644--- a/doc/classes/LightmapGI.xml+++ b/doc/classes/LightmapGI.xml@@ -64,6 +64,7 @@</member><member name="max_texture_size" type="int" setter="set_max_texture_size" getter="get_max_texture_size" default="16384">The maximum texture size for the generated texture atlas. Higher values will result in fewer slices being generated, but may not work on all hardware as a result of hardware limitations on texture sizes. Leave [member max_texture_size] at its default value of [code]16384[/code] if unsure.+ [b]Note:[/b] If [member supersampling] is enabled, the texture atlas used while baking may be up to [member supersampling_factor] times larger than [member max_texture_size], since it's downsampled before saving.</member><member name="quality" type="int" setter="set_bake_quality" getter="get_bake_quality" enum="LightmapGI.BakeQuality" default="1">The quality preset to use when baking lightmaps. This affects bake times, but output file sizes remain mostly identical across quality levels.@@ -164,6 +165,9 @@<constant name="BAKE_ERROR_ATLAS_TOO_SMALL" value="11" enum="BakeError">Lightmap baking failed as the lightmap was unable to fit into an atlas.</constant>+ <constant name="BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT" value="12" enum="BakeError">+ Lightmap baking failed as the baking process requires a texture larger than the maximum texture size supported by the rendering device. This can happen when [member supersampling] is enabled with a high [member supersampling_factor].+ </constant><constant name="ENVIRONMENT_MODE_DISABLED" value="0" enum="EnvironmentMode">Ignore environment lighting when baking lightmaps.</constant>diff --git a/editor/scene/3d/lightmap_gi_editor_plugin.cpp b/editor/scene/3d/lightmap_gi_editor_plugin.cppindex 5ff451538ae..fcad3ddaf50 100644--- a/editor/scene/3d/lightmap_gi_editor_plugin.cpp+++ b/editor/scene/3d/lightmap_gi_editor_plugin.cpp@@ -127,6 +127,9 @@ void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {case LightmapGI::BAKE_ERROR_ATLAS_TOO_SMALL: {EditorNode::get_singleton()->show_warning(TTR("Failed fitting a lightmap image into an atlas. This should never happen and should be reported."));} break;+ case LightmapGI::BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT: {+ EditorNode::get_singleton()->show_warning(TTR("The lightmap images exceed the maximum texture size supported by the rendering device used for baking.\nDecrease the supersampling factor or the texel scale, or split the scene into more objects."));+ } break;default: {} break;}diff --git a/modules/lightmapper_rd/lightmapper_rd.cpp b/modules/lightmapper_rd/lightmapper_rd.cppindex 9ed2cc62524..b09a1750ec6 100644--- a/modules/lightmapper_rd/lightmapper_rd.cpp+++ b/modules/lightmapper_rd/lightmapper_rd.cpp@@ -303,21 +303,24 @@ void LightmapperRD::_sort_triangle_clusters(uint32_t p_cluster_size, uint32_t p_}}-Lightmapper::BakeError LightmapperRD::_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) {+Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_size, int p_device_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) {Vector<Size2i> sizes;+ Size2i largest_mesh_lightmap_size;for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {MeshInstance &mi = mesh_instances.write[m_i];Size2i s = Size2i(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height());sizes.push_back(s);- atlas_size = atlas_size.max(s + Size2i(2, 2).maxi(p_denoiser_range) * p_supersampling_factor);+ largest_mesh_lightmap_size = largest_mesh_lightmap_size.max(s + Size2i(2, 2).maxi(p_denoiser_range) * p_supersampling_factor);}- int max = Math::nearest_power_of_2_templated(atlas_size.width);- max = MAX(max, Math::nearest_power_of_2_templated(atlas_size.height));+ const int min_atlas_size = MAX(+ Math::nearest_power_of_2_templated(largest_mesh_lightmap_size.width),+ Math::nearest_power_of_2_templated(largest_mesh_lightmap_size.height));- if (max > p_max_texture_size) {- return BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE;+ const int atlas_size_limit = MIN(int(p_max_texture_size * p_supersampling_factor), p_device_max_texture_size);+ if (min_atlas_size > atlas_size_limit) {+ return min_atlas_size > p_device_max_texture_size ? BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT : BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE;}if (p_step_function) {@@ -326,7 +329,7 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_}}- atlas_size = Size2i(max, max);+ atlas_size = Size2i(min_atlas_size, min_atlas_size);Size2i best_atlas_size;int best_atlas_slices = 0;@@ -334,7 +337,7 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_Vector<Vector3i> best_atlas_offsets;// Determine best texture array atlas size by bruteforce fitting.- while (atlas_size.x <= p_max_texture_size && atlas_size.y <= p_max_texture_size) {+ while (atlas_size.x <= atlas_size_limit && atlas_size.y <= atlas_size_limit) {Vector<Vector2i> source_sizes;Vector<int> source_indices;source_sizes.resize(sizes.size());@@ -1126,6 +1129,40 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_dshadowmask_textures.clear();int grid_size = 128;+ // Attempt to create a local device by requesting it from rendering server first.+ // If that fails because the current renderer is not implemented on top of RD, we fall back to creating+ // a local rendering device manually depending on the current platform.+ Error err;+ RenderingContextDriver *rcd = nullptr;+ RenderingDevice *rd = RenderingServer::get_singleton()->create_local_rendering_device();+ if (rd == nullptr) {+#if defined(METAL_ENABLED)+ rcd = memnew(RenderingContextDriverMetal);+ rd = memnew(RenderingDevice);+#endif+#if defined(VULKAN_ENABLED)+ if (rcd == nullptr) {+ rcd = memnew(RenderingContextDriverVulkan);+ rd = memnew(RenderingDevice);+ }+#endif+ if (rcd != nullptr && rd != nullptr) {+ err = rcd->initialize();+ if (err == OK) {+ err = rd->initialize(rcd);+ }++ if (err != OK) {+ memdelete(rd);+ memdelete(rcd);+ rd = nullptr;+ rcd = nullptr;+ }+ }+ }++ ERR_FAIL_NULL_V(rd, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);+/* STEP 1: Fetch material textures and compute the bounds */AABB bounds;@@ -1134,8 +1171,11 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_dVector<Ref<Image>> albedo_images;Vector<Ref<Image>> emission_images;- BakeError bake_error = _blit_meshes_into_atlas(p_max_texture_size, p_denoiser_range, albedo_images, emission_images, bounds, atlas_size, atlas_slices, p_supersampling_factor, p_step_function, p_bake_userdata);+ const int device_max_texture_size = rd->limit_get(RD::LIMIT_MAX_TEXTURE_SIZE_2D);+ BakeError bake_error = _blit_meshes_into_atlas(p_max_texture_size, device_max_texture_size, p_denoiser_range, albedo_images, emission_images, bounds, atlas_size, atlas_slices, p_supersampling_factor, p_step_function, p_bake_userdata);if (bake_error != BAKE_OK) {+ memdelete(rd);+ memdelete(rcd);return bake_error;}@@ -1173,40 +1213,6 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d}#endif- // Attempt to create a local device by requesting it from rendering server first.- // If that fails because the current renderer is not implemented on top of RD, we fall back to creating- // a local rendering device manually depending on the current platform.- Error err;- RenderingContextDriver *rcd = nullptr;- RenderingDevice *rd = RenderingServer::get_singleton()->create_local_rendering_device();- if (rd == nullptr) {-#if defined(METAL_ENABLED)- rcd = memnew(RenderingContextDriverMetal);- rd = memnew(RenderingDevice);-#endif-#if defined(VULKAN_ENABLED)- if (rcd == nullptr) {- rcd = memnew(RenderingContextDriverVulkan);- rd = memnew(RenderingDevice);- }-#endif- if (rcd != nullptr && rd != nullptr) {- err = rcd->initialize();- if (err == OK) {- err = rd->initialize(rcd);- }-- if (err != OK) {- memdelete(rd);- memdelete(rcd);- rd = nullptr;- rcd = nullptr;- }- }- }-- ERR_FAIL_NULL_V(rd, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);-RID albedo_array_tex;RID emission_array_tex;RID normal_tex;diff --git a/modules/lightmapper_rd/lightmapper_rd.h b/modules/lightmapper_rd/lightmapper_rd.hindex 4c0292d9623..d0a5773fba0 100644--- a/modules/lightmapper_rd/lightmapper_rd.h+++ b/modules/lightmapper_rd/lightmapper_rd.h@@ -294,7 +294,7 @@ class LightmapperRD : public Lightmapper {uint32_t pad;};- 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);+ BakeError _blit_meshes_into_atlas(int p_max_texture_size, int p_device_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);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);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);diff --git a/scene/3d/lightmap_gi.cpp b/scene/3d/lightmap_gi.cppindex 1fd7b197c50..90aeac7f44a 100644--- a/scene/3d/lightmap_gi.cpp+++ b/scene/3d/lightmap_gi.cpp@@ -1492,6 +1492,8 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pareturn BAKE_ERROR_ATLAS_TOO_SMALL;} else if (bake_err == Lightmapper::BAKE_ERROR_USER_ABORTED) {return BAKE_ERROR_USER_ABORTED;+ } else if (bake_err == Lightmapper::BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT) {+ return BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT;}// POSTBAKE: Save Textures.@@ -2202,6 +2204,7 @@ void LightmapGI::_bind_methods() {BIND_ENUM_CONSTANT(BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL);BIND_ENUM_CONSTANT(BAKE_ERROR_LIGHTMAP_TOO_SMALL);BIND_ENUM_CONSTANT(BAKE_ERROR_ATLAS_TOO_SMALL);+ BIND_ENUM_CONSTANT(BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT);BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_DISABLED);BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_SCENE);diff --git a/scene/3d/lightmap_gi.h b/scene/3d/lightmap_gi.hindex 3028a3243e3..ef9f2dc5514 100644--- a/scene/3d/lightmap_gi.h+++ b/scene/3d/lightmap_gi.h@@ -179,6 +179,7 @@ public:BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL,BAKE_ERROR_LIGHTMAP_TOO_SMALL,BAKE_ERROR_ATLAS_TOO_SMALL,+ BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT,};enum EnvironmentMode {diff --git a/scene/3d/lightmapper.h b/scene/3d/lightmapper.hindex 498661b573b..350aa59bd09 100644--- a/scene/3d/lightmapper.h+++ b/scene/3d/lightmapper.h@@ -137,6 +137,7 @@ public:BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,BAKE_ERROR_ATLAS_TOO_SMALL,BAKE_ERROR_USER_ABORTED,+ BAKE_ERROR_TEXTURE_EXCEEDS_DEVICE_LIMIT,};enum BakeQuality {