| 1 | /**************************************************************************/ |
| 2 | /* tone_mapper.cpp */ |
| 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 | |
| 31 | #include "tone_mapper.h" |
| 32 | #include "servers/rendering/renderer_rd/renderer_compositor_rd.h" |
| 33 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 34 | #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h" |
| 35 | |
| 36 | using namespace RendererRD; |
| 37 | |
| 38 | ToneMapper::ToneMapper(bool p_use_mobile_version) { |
| 39 | using_mobile_version = p_use_mobile_version; |
| 40 | if (using_mobile_version) { |
| 41 | // Initialize tonemapper |
| 42 | Vector<String> tonemap_modes; |
| 43 | tonemap_modes.push_back("\n"); |
| 44 | tonemap_modes.push_back("\n#define USE_1D_LUT\n"); |
| 45 | tonemap_modes.push_back("\n#define SUBPASS\n"); |
| 46 | tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n"); |
| 47 | |
| 48 | // multiview versions of our shaders |
| 49 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n"); |
| 50 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n"); |
| 51 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n"); |
| 52 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n"); |
| 53 | |
| 54 | tonemap_mobile.shader.initialize(tonemap_modes); |
| 55 | |
| 56 | if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) { |
| 57 | tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_NORMAL_MULTIVIEW, false); |
| 58 | tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_1D_LUT_MULTIVIEW, false); |
| 59 | tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_MULTIVIEW, false); |
| 60 | tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT_MULTIVIEW, false); |
| 61 | } |
| 62 | |
| 63 | tonemap_mobile.shader_version = tonemap_mobile.shader.version_create(); |
| 64 | |
| 65 | for (int i = 0; i < TONEMAP_MODE_MAX; i++) { |
| 66 | if (tonemap_mobile.shader.is_variant_enabled(i)) { |
| 67 | tonemap_mobile.pipelines[i].setup(tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0); |
| 68 | } else { |
| 69 | tonemap_mobile.pipelines[i].clear(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | } else { |
| 74 | // Initialize tonemapper |
| 75 | Vector<String> tonemap_modes; |
| 76 | tonemap_modes.push_back("\n"); |
| 77 | tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n"); |
| 78 | tonemap_modes.push_back("\n#define USE_1D_LUT\n"); |
| 79 | tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n"); |
| 80 | |
| 81 | // multiview versions of our shaders |
| 82 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n"); |
| 83 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n"); |
| 84 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n"); |
| 85 | tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n"); |
| 86 | |
| 87 | tonemap.shader.initialize(tonemap_modes); |
| 88 | |
| 89 | if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) { |
| 90 | tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false); |
| 91 | tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false); |
| 92 | tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false); |
| 93 | tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false); |
| 94 | } |
| 95 | |
| 96 | tonemap.shader_version = tonemap.shader.version_create(); |
| 97 | |
| 98 | for (int i = 0; i < TONEMAP_MODE_MAX; i++) { |
| 99 | if (tonemap.shader.is_variant_enabled(i)) { |
| 100 | tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0); |
| 101 | } else { |
| 102 | tonemap.pipelines[i].clear(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ToneMapper::~ToneMapper() { |
| 109 | if (using_mobile_version) { |
| 110 | tonemap_mobile.shader.version_free(tonemap_mobile.shader_version); |
| 111 | } else { |
| 112 | tonemap.shader.version_free(tonemap.shader_version); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void ToneMapper::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) { |
| 117 | ERR_FAIL_COND_MSG(using_mobile_version, "Can't use the non mobile version of the tonemapper with the Mobile renderer."); |
| 118 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 119 | ERR_FAIL_NULL(uniform_set_cache); |
| 120 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 121 | ERR_FAIL_NULL(material_storage); |
| 122 | |
| 123 | memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant)); |
| 124 | |
| 125 | tonemap.push_constant.flags |= p_settings.use_bcs ? TONEMAP_FLAG_USE_BCS : 0; |
| 126 | tonemap.push_constant.bcs[0] = p_settings.brightness; |
| 127 | tonemap.push_constant.bcs[1] = p_settings.contrast; |
| 128 | tonemap.push_constant.bcs[2] = p_settings.saturation; |
| 129 | |
| 130 | tonemap.push_constant.flags |= p_settings.use_glow ? TONEMAP_FLAG_USE_GLOW : 0; |
| 131 | tonemap.push_constant.glow_intensity = p_settings.glow_intensity; |
| 132 | tonemap.push_constant.glow_map_strength = p_settings.glow_map_strength; |
| 133 | tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something |
| 134 | tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1]; |
| 135 | tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2]; |
| 136 | tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3]; |
| 137 | tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4]; |
| 138 | tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5]; |
| 139 | tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6]; |
| 140 | tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x; |
| 141 | tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y; |
| 142 | tonemap.push_constant.glow_mode = p_settings.glow_mode; |
| 143 | |
| 144 | int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL; |
| 145 | if (p_settings.use_1d_color_correction) { |
| 146 | mode += 2; |
| 147 | } |
| 148 | |
| 149 | tonemap.push_constant.tonemapper = p_settings.tonemap_mode; |
| 150 | tonemap.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0]; |
| 151 | tonemap.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1]; |
| 152 | tonemap.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2]; |
| 153 | tonemap.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3]; |
| 154 | tonemap.push_constant.flags |= p_settings.use_auto_exposure ? TONEMAP_FLAG_USE_AUTO_EXPOSURE : 0; |
| 155 | tonemap.push_constant.exposure = p_settings.exposure; |
| 156 | tonemap.push_constant.white = p_settings.white; |
| 157 | tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale; |
| 158 | tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier; |
| 159 | |
| 160 | tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0; |
| 161 | |
| 162 | tonemap.push_constant.flags |= p_settings.use_fxaa ? TONEMAP_FLAG_USE_FXAA : 0; |
| 163 | if (p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT) { |
| 164 | tonemap.push_constant.flags |= TONEMAP_FLAG_USE_8_BIT_DEBANDING; |
| 165 | } |
| 166 | tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x; |
| 167 | tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y; |
| 168 | |
| 169 | tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0; |
| 170 | |
| 171 | if (p_settings.view_count > 1) { |
| 172 | // Use USE_MULTIVIEW versions |
| 173 | mode += 4; |
| 174 | } |
| 175 | |
| 176 | RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 177 | RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 178 | |
| 179 | RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color })); |
| 180 | |
| 181 | RD::Uniform u_exposure_texture; |
| 182 | u_exposure_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 183 | u_exposure_texture.binding = 0; |
| 184 | u_exposure_texture.append_id(default_sampler); |
| 185 | u_exposure_texture.append_id(p_settings.exposure_texture); |
| 186 | |
| 187 | RD::Uniform u_glow_texture; |
| 188 | u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 189 | u_glow_texture.binding = 0; |
| 190 | u_glow_texture.append_id(default_mipmap_sampler); |
| 191 | u_glow_texture.append_id(p_settings.glow_texture); |
| 192 | |
| 193 | RD::Uniform u_glow_map; |
| 194 | u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 195 | u_glow_map.binding = 1; |
| 196 | u_glow_map.append_id(default_mipmap_sampler); |
| 197 | u_glow_map.append_id(p_settings.glow_map); |
| 198 | |
| 199 | RD::Uniform u_color_correction_texture; |
| 200 | u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 201 | u_color_correction_texture.binding = 0; |
| 202 | u_color_correction_texture.append_id(default_sampler); |
| 203 | u_color_correction_texture.append_id(p_settings.color_correction_texture); |
| 204 | |
| 205 | RID shader = tonemap.shader.version_get_shader(tonemap.shader_version, mode); |
| 206 | ERR_FAIL_COND(shader.is_null()); |
| 207 | |
| 208 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer); |
| 209 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass())); |
| 210 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color), 0); |
| 211 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_exposure_texture), 1); |
| 212 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_glow_texture, u_glow_map), 2); |
| 213 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 3, u_color_correction_texture), 3); |
| 214 | |
| 215 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant)); |
| 216 | RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u); |
| 217 | RD::get_singleton()->draw_list_end(); |
| 218 | } |
| 219 | |
| 220 | void ToneMapper::tonemapper_mobile(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) { |
| 221 | ERR_FAIL_COND_MSG(!using_mobile_version, "Can't use the mobile version of the tonemapper with the clustered renderer."); |
| 222 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 223 | ERR_FAIL_NULL(uniform_set_cache); |
| 224 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 225 | ERR_FAIL_NULL(material_storage); |
| 226 | |
| 227 | memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile)); |
| 228 | |
| 229 | tonemap_mobile.push_constant.bcs[0] = p_settings.brightness; |
| 230 | tonemap_mobile.push_constant.bcs[1] = p_settings.contrast; |
| 231 | tonemap_mobile.push_constant.bcs[2] = p_settings.saturation; |
| 232 | |
| 233 | tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x; |
| 234 | tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y; |
| 235 | tonemap_mobile.push_constant.dest_pixel_size[0] = 1.0 / p_settings.dest_texture_size.x; |
| 236 | tonemap_mobile.push_constant.dest_pixel_size[1] = 1.0 / p_settings.dest_texture_size.y; |
| 237 | tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity; |
| 238 | tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength; |
| 239 | |
| 240 | tonemap_mobile.push_constant.exposure = p_settings.exposure; |
| 241 | tonemap_mobile.push_constant.white = p_settings.white; |
| 242 | tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier; |
| 243 | |
| 244 | tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0]; |
| 245 | tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1]; |
| 246 | tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2]; |
| 247 | tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3]; |
| 248 | |
| 249 | uint32_t spec_constant = 0; |
| 250 | spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0; |
| 251 | spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0; |
| 252 | spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0; |
| 253 | spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0; |
| 254 | spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0; |
| 255 | spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0; |
| 256 | spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_10_BIT ? TONEMAP_MOBILE_FLAG_USE_10_BIT_DEBANDING : 0; |
| 257 | spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0; |
| 258 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0; |
| 259 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0; |
| 260 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0; |
| 261 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0; |
| 262 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0; |
| 263 | spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0; |
| 264 | spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0; |
| 265 | spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0; |
| 266 | spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0; |
| 267 | spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0; |
| 268 | |
| 269 | int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_1D_LUT : TONEMAP_MOBILE_MODE_NORMAL; |
| 270 | |
| 271 | if (p_settings.view_count > 1) { |
| 272 | // Use USE_MULTIVIEW versions |
| 273 | mode += 4; |
| 274 | } |
| 275 | |
| 276 | RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 277 | RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 278 | |
| 279 | RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color })); |
| 280 | |
| 281 | RD::Uniform u_glow_texture; |
| 282 | u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 283 | u_glow_texture.binding = 1; |
| 284 | u_glow_texture.append_id(default_mipmap_sampler); |
| 285 | u_glow_texture.append_id(p_settings.glow_texture); |
| 286 | |
| 287 | RD::Uniform u_glow_map; |
| 288 | u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 289 | u_glow_map.binding = 2; |
| 290 | u_glow_map.append_id(default_mipmap_sampler); |
| 291 | u_glow_map.append_id(p_settings.glow_map); |
| 292 | |
| 293 | RD::Uniform u_color_correction_texture; |
| 294 | u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 295 | u_color_correction_texture.binding = 3; |
| 296 | u_color_correction_texture.append_id(default_sampler); |
| 297 | u_color_correction_texture.append_id(p_settings.color_correction_texture); |
| 298 | |
| 299 | RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode); |
| 300 | ERR_FAIL_COND(shader.is_null()); |
| 301 | |
| 302 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer); |
| 303 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap_mobile.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass(), spec_constant)); |
| 304 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0); |
| 305 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile)); |
| 306 | RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u); |
| 307 | RD::get_singleton()->draw_list_end(); |
| 308 | } |
| 309 | |
| 310 | void ToneMapper::tonemapper_subpass(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) { |
| 311 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 312 | ERR_FAIL_NULL(uniform_set_cache); |
| 313 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 314 | ERR_FAIL_NULL(material_storage); |
| 315 | |
| 316 | ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses."); |
| 317 | |
| 318 | memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile)); |
| 319 | |
| 320 | tonemap_mobile.push_constant.bcs[0] = p_settings.brightness; |
| 321 | tonemap_mobile.push_constant.bcs[1] = p_settings.contrast; |
| 322 | tonemap_mobile.push_constant.bcs[2] = p_settings.saturation; |
| 323 | |
| 324 | tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x; |
| 325 | tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y; |
| 326 | tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity; |
| 327 | tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength; |
| 328 | |
| 329 | tonemap_mobile.push_constant.exposure = p_settings.exposure; |
| 330 | tonemap_mobile.push_constant.white = p_settings.white; |
| 331 | tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier; |
| 332 | |
| 333 | tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0]; |
| 334 | tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1]; |
| 335 | tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2]; |
| 336 | tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3]; |
| 337 | |
| 338 | uint32_t spec_constant = TONEMAP_MOBILE_ADRENO_BUG; |
| 339 | spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0; |
| 340 | //spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0; |
| 341 | //spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0; |
| 342 | spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0; |
| 343 | //spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0; |
| 344 | spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0; |
| 345 | spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0; |
| 346 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0; |
| 347 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0; |
| 348 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0; |
| 349 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0; |
| 350 | spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0; |
| 351 | //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0; |
| 352 | //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0; |
| 353 | //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0; |
| 354 | //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0; |
| 355 | //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0; |
| 356 | |
| 357 | int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT : TONEMAP_MOBILE_MODE_SUBPASS; |
| 358 | if (p_settings.view_count > 1) { |
| 359 | // Use USE_MULTIVIEW versions |
| 360 | mode += 4; |
| 361 | } |
| 362 | |
| 363 | RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 364 | RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 365 | |
| 366 | RD::Uniform u_source_color; |
| 367 | u_source_color.uniform_type = RD::UNIFORM_TYPE_INPUT_ATTACHMENT; |
| 368 | u_source_color.binding = 0; |
| 369 | u_source_color.append_id(p_source_color); |
| 370 | |
| 371 | RD::Uniform u_glow_texture; |
| 372 | u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 373 | u_glow_texture.binding = 1; |
| 374 | u_glow_texture.append_id(default_mipmap_sampler); |
| 375 | u_glow_texture.append_id(p_settings.glow_texture); |
| 376 | |
| 377 | RD::Uniform u_glow_map; |
| 378 | u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 379 | u_glow_map.binding = 2; |
| 380 | u_glow_map.append_id(default_mipmap_sampler); |
| 381 | u_glow_map.append_id(p_settings.glow_map); |
| 382 | |
| 383 | RD::Uniform u_color_correction_texture; |
| 384 | u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
| 385 | u_color_correction_texture.binding = 3; |
| 386 | u_color_correction_texture.append_id(default_sampler); |
| 387 | u_color_correction_texture.append_id(p_settings.color_correction_texture); |
| 388 | |
| 389 | RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode); |
| 390 | ERR_FAIL_COND(shader.is_null()); |
| 391 | |
| 392 | RD::get_singleton()->draw_list_bind_render_pipeline(p_subpass_draw_list, tonemap_mobile.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_dst_format_id, false, RD::get_singleton()->draw_list_get_current_pass(), spec_constant)); |
| 393 | RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0); |
| 394 | RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile)); |
| 395 | RD::get_singleton()->draw_list_draw(p_subpass_draw_list, false, 1u, 3u); |
| 396 | } |