| 980 | } |
| 981 | |
| 982 | LightmapperRD::BakeError LightmapperRD::_denoise(RenderingDevice *p_rd, Ref<RDShaderFile> &p_compute_shader, const RID &p_compute_base_uniform_set, PushConstant &p_push_constant, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, RID p_unocclude_tex, float p_denoiser_strength, int p_denoiser_range, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function, void *p_bake_userdata) { |
| 983 | RID denoise_params_buffer = p_rd->uniform_buffer_create(sizeof(DenoiseParams)); |
| 984 | DenoiseParams denoise_params; |
| 985 | denoise_params.spatial_bandwidth = 5.0f; |
| 986 | denoise_params.light_bandwidth = p_denoiser_strength; |
| 987 | denoise_params.albedo_bandwidth = 1.0f; |
| 988 | denoise_params.normal_bandwidth = 0.1f; |
| 989 | denoise_params.filter_strength = 10.0f; |
| 990 | denoise_params.half_search_window = p_denoiser_range; |
| 991 | p_rd->buffer_update(denoise_params_buffer, 0, sizeof(DenoiseParams), &denoise_params); |
| 992 | |
| 993 | Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(p_source_light_tex, p_dest_light_tex); |
| 994 | { |
| 995 | RD::Uniform u; |
| 996 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 997 | u.binding = 2; |
| 998 | u.append_id(p_source_normal_tex); |
| 999 | uniforms.push_back(u); |
| 1000 | } |
| 1001 | { |
| 1002 | RD::Uniform u; |
| 1003 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1004 | u.binding = 3; |
| 1005 | u.append_id(p_unocclude_tex); |
| 1006 | uniforms.push_back(u); |
| 1007 | } |
| 1008 | { |
| 1009 | RD::Uniform u; |
| 1010 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 1011 | u.binding = 4; |
| 1012 | u.append_id(denoise_params_buffer); |
| 1013 | uniforms.push_back(u); |
| 1014 | } |
| 1015 | |
| 1016 | RID compute_shader_denoise = p_rd->shader_create_from_spirv(p_compute_shader->get_spirv_stages("denoise")); |
| 1017 | ERR_FAIL_COND_V(compute_shader_denoise.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
| 1018 | |
| 1019 | RID compute_shader_denoise_pipeline = p_rd->compute_pipeline_create(compute_shader_denoise); |
| 1020 | RID denoise_uniform_set = p_rd->uniform_set_create(uniforms, compute_shader_denoise, 1); |
| 1021 | |
| 1022 | // We denoise in fixed size regions and synchronize execution to avoid GPU timeouts. |
| 1023 | // We use a region with 1/4 the amount of pixels if we're denoising SH lightmaps, as |
| 1024 | // all four of them are denoised in the shader in one dispatch. |
| 1025 | const int user_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size"))); |
| 1026 | const int max_region_size = p_bake_sh ? user_region_size / 2 : user_region_size; |
| 1027 | int x_regions = Math::division_round_up(p_atlas_size.width, max_region_size); |
| 1028 | int y_regions = Math::division_round_up(p_atlas_size.height, max_region_size); |
| 1029 | for (int s = 0; s < p_atlas_slices; s++) { |
| 1030 | p_push_constant.atlas_slice = s; |
| 1031 | |
| 1032 | for (int i = 0; i < x_regions; i++) { |
| 1033 | for (int j = 0; j < y_regions; j++) { |
| 1034 | int x = i * max_region_size; |
| 1035 | int y = j * max_region_size; |
| 1036 | int w = MIN((i + 1) * max_region_size, p_atlas_size.width) - x; |
| 1037 | int h = MIN((j + 1) * max_region_size, p_atlas_size.height) - y; |
| 1038 | p_push_constant.region_ofs[0] = x; |
| 1039 | p_push_constant.region_ofs[1] = y; |
nothing calls this directly
no test coverage detected