Skip to content

Commit

Permalink
Add lightmap bake cancelling
Browse files Browse the repository at this point in the history
  • Loading branch information
badsectoracula committed Nov 21, 2024
1 parent 9e60984 commit 5bca028
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 12 deletions.
2 changes: 1 addition & 1 deletion editor/plugins/lightmap_gi_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;

bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
if (!tmp_progress) {
tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, false));
tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, true));
ERR_FAIL_NULL_V(tmp_progress, false);
}
return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
Expand Down
115 changes: 104 additions & 11 deletions modules/lightmapper_rd/lightmapper_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
}

if (p_step_function) {
p_step_function(0.1, RTR("Determining optimal atlas size"), p_bake_userdata, true);
if (p_step_function(0.1, RTR("Determining optimal atlas size"), p_bake_userdata, true)) {
return BAKE_ERROR_USER_ABORTED;
}
}

atlas_size = Size2i(max, max);
Expand Down Expand Up @@ -324,7 +326,9 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
emission_images.resize(atlas_slices);

if (p_step_function) {
p_step_function(0.2, RTR("Blitting albedo and emission"), p_bake_userdata, true);
if (p_step_function(0.2, RTR("Blitting albedo and emission"), p_bake_userdata, true)) {
return BAKE_ERROR_USER_ABORTED;
}
}

for (int i = 0; i < atlas_slices; i++) {
Expand Down Expand Up @@ -1013,7 +1017,9 @@ LightmapperRD::BakeError LightmapperRD::_denoise(RenderingDevice *p_rd, Ref<RDSh
if (p_step_function) {
int percent = (s + 1) * 100 / p_atlas_slices;
float p = float(s) / p_atlas_slices * 0.1;
p_step_function(0.8 + p, vformat(RTR("Denoising %d%%"), percent), p_bake_userdata, false);
if (p_step_function(0.8 + p, vformat(RTR("Denoising %d%%"), percent), p_bake_userdata, false)) {
return BAKE_ERROR_USER_ABORTED;
}
}
}

Expand Down Expand Up @@ -1265,7 +1271,15 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
rd->buffer_update(bake_parameters_buffer, 0, sizeof(BakeParameters), &bake_parameters);

if (p_step_function) {
p_step_function(0.47, RTR("Preparing shaders"), p_bake_userdata, true);
if (p_step_function(0.47, RTR("Preparing shaders"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

//shaders
Expand Down Expand Up @@ -1497,7 +1511,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
rd->sync();

if (p_step_function) {
p_step_function(0.49, RTR("Un-occluding geometry"), p_bake_userdata, true);
if (p_step_function(0.49, RTR("Un-occluding geometry"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

PushConstant push_constant;
Expand Down Expand Up @@ -1539,7 +1563,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
}

if (p_step_function) {
p_step_function(0.5, RTR("Plot direct lighting"), p_bake_userdata, true);
if (p_step_function(0.5, RTR("Plot direct lighting"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

// Set ray count to the quality used for direct light and bounces.
Expand Down Expand Up @@ -1699,7 +1733,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
rd->sync();

if (p_step_function) {
p_step_function(0.6, RTR("Integrate indirect lighting"), p_bake_userdata, true);
if (p_step_function(0.6, RTR("Integrate indirect lighting"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

int count = 0;
Expand Down Expand Up @@ -1738,7 +1782,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
int total = (atlas_slices * x_regions * y_regions * ray_iterations);
int percent = count * 100 / total;
float p = float(count) / total * 0.1;
p_step_function(0.6 + p, vformat(RTR("Integrate indirect lighting %d%%"), percent), p_bake_userdata, false);
if (p_step_function(0.6 + p, vformat(RTR("Integrate indirect lighting %d%%"), percent), p_bake_userdata, false)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}
}
}
Expand All @@ -1754,7 +1808,20 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size());

if (p_step_function) {
p_step_function(0.7, RTR("Baking light probes"), p_bake_userdata, true);
if (p_step_function(0.7, RTR("Baking light probes"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
if (probe_positions.size() > 0) {
rd->free(light_probe_buffer);
}
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

Vector<RD::Uniform> uniforms;
Expand Down Expand Up @@ -1822,7 +1889,20 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
if (p_step_function) {
int percent = i * 100 / ray_iterations;
float p = float(i) / ray_iterations * 0.1;
p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%"), percent), p_bake_userdata, false);
if (p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%"), percent), p_bake_userdata, false)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
if (probe_positions.size() > 0) {
rd->free(light_probe_buffer);
}
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}
}
}
Expand All @@ -1844,7 +1924,20 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d

if (p_use_denoiser) {
if (p_step_function) {
p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true);
if (p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true)) {
FREE_TEXTURES
FREE_BUFFERS
FREE_RASTER_RESOURCES
FREE_COMPUTE_RESOURCES
if (probe_positions.size() > 0) {
rd->free(light_probe_buffer);
}
memdelete(rd);
if (rcd != nullptr) {
memdelete(rcd);
}
return BAKE_ERROR_USER_ABORTED;
}
}

{
Expand Down
2 changes: 2 additions & 0 deletions scene/3d/lightmap_gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa
return BAKE_ERROR_MESHES_INVALID;
} else if (bake_err == Lightmapper::BAKE_ERROR_ATLAS_TOO_SMALL) {
return BAKE_ERROR_ATLAS_TOO_SMALL;
} else if (bake_err == Lightmapper::BAKE_ERROR_USER_ABORTED) {
return BAKE_ERROR_USER_ABORTED;
}

// POSTBAKE: Save Textures.
Expand Down
1 change: 1 addition & 0 deletions scene/3d/lightmapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Lightmapper : public RefCounted {
BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE,
BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,
BAKE_ERROR_ATLAS_TOO_SMALL,
BAKE_ERROR_USER_ABORTED,
};

enum BakeQuality {
Expand Down

0 comments on commit 5bca028

Please sign in to comment.