Browse Source

Add lightmap bake cancelling

Bad Sector 8 months ago
parent
commit
5bca028e2b

+ 1 - 1
editor/plugins/lightmap_gi_editor_plugin.cpp

@@ -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) {
 bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
 	if (!tmp_progress) {
 	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);
 		ERR_FAIL_NULL_V(tmp_progress, false);
 	}
 	}
 	return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
 	return tmp_progress->step(p_description, p_progress * 1000, p_refresh);

+ 104 - 11
modules/lightmapper_rd/lightmapper_rd.cpp

@@ -247,7 +247,9 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
 	}
 	}
 
 
 	if (p_step_function) {
 	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);
 	atlas_size = Size2i(max, max);
@@ -324,7 +326,9 @@ Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_
 	emission_images.resize(atlas_slices);
 	emission_images.resize(atlas_slices);
 
 
 	if (p_step_function) {
 	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++) {
 	for (int i = 0; i < atlas_slices; i++) {
@@ -1013,7 +1017,9 @@ LightmapperRD::BakeError LightmapperRD::_denoise(RenderingDevice *p_rd, Ref<RDSh
 		if (p_step_function) {
 		if (p_step_function) {
 			int percent = (s + 1) * 100 / p_atlas_slices;
 			int percent = (s + 1) * 100 / p_atlas_slices;
 			float p = float(s) / p_atlas_slices * 0.1;
 			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;
+			}
 		}
 		}
 	}
 	}
 
 
@@ -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);
 	rd->buffer_update(bake_parameters_buffer, 0, sizeof(BakeParameters), &bake_parameters);
 
 
 	if (p_step_function) {
 	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
 	//shaders
@@ -1497,7 +1511,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
 	rd->sync();
 	rd->sync();
 
 
 	if (p_step_function) {
 	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;
 	PushConstant push_constant;
@@ -1539,7 +1563,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
 	}
 	}
 
 
 	if (p_step_function) {
 	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.
 	// Set ray count to the quality used for direct light and bounces.
@@ -1699,7 +1733,17 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
 		rd->sync();
 		rd->sync();
 
 
 		if (p_step_function) {
 		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;
 		int count = 0;
@@ -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 total = (atlas_slices * x_regions * y_regions * ray_iterations);
 							int percent = count * 100 / total;
 							int percent = count * 100 / total;
 							float p = float(count) / total * 0.1;
 							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;
+							}
 						}
 						}
 					}
 					}
 				}
 				}
@@ -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());
 		light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size());
 
 
 		if (p_step_function) {
 		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;
 		Vector<RD::Uniform> uniforms;
@@ -1822,7 +1889,20 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
 			if (p_step_function) {
 			if (p_step_function) {
 				int percent = i * 100 / ray_iterations;
 				int percent = i * 100 / ray_iterations;
 				float p = float(i) / ray_iterations * 0.1;
 				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;
+				}
 			}
 			}
 		}
 		}
 	}
 	}
@@ -1844,7 +1924,20 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
 
 
 	if (p_use_denoiser) {
 	if (p_use_denoiser) {
 		if (p_step_function) {
 		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;
+			}
 		}
 		}
 
 
 		{
 		{

+ 2 - 0
scene/3d/lightmap_gi.cpp

@@ -1189,6 +1189,8 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa
 		return BAKE_ERROR_MESHES_INVALID;
 		return BAKE_ERROR_MESHES_INVALID;
 	} else if (bake_err == Lightmapper::BAKE_ERROR_ATLAS_TOO_SMALL) {
 	} else if (bake_err == Lightmapper::BAKE_ERROR_ATLAS_TOO_SMALL) {
 		return 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.
 	// POSTBAKE: Save Textures.

+ 1 - 0
scene/3d/lightmapper.h

@@ -147,6 +147,7 @@ public:
 		BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE,
 		BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE,
 		BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,
 		BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,
 		BAKE_ERROR_ATLAS_TOO_SMALL,
 		BAKE_ERROR_ATLAS_TOO_SMALL,
+		BAKE_ERROR_USER_ABORTED,
 	};
 	};
 
 
 	enum BakeQuality {
 	enum BakeQuality {