| 1 | /**************************************************************************/ |
| 2 | /* fsr.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 "fsr.h" |
| 32 | #include "../storage_rd/material_storage.h" |
| 33 | #include "../uniform_set_cache_rd.h" |
| 34 | |
| 35 | using namespace RendererRD; |
| 36 | |
| 37 | FSR::FSR() { |
| 38 | Vector<String> FSR_upscale_modes; |
| 39 | |
| 40 | #if defined(MACOS_ENABLED) || defined(IOS_ENABLED) |
| 41 | // MoltenVK does not support some of the operations used by the normal mode of FSR. Fallback works just fine though. |
| 42 | FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n" ); |
| 43 | #else |
| 44 | // Everyone else can use normal mode when available. |
| 45 | if (RD::get_singleton()->has_feature(RD::SUPPORTS_FSR_HALF_FLOAT)) { |
| 46 | FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_NORMAL\n" ); |
| 47 | } else { |
| 48 | FSR_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n" ); |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | fsr_shader.initialize(FSR_upscale_modes); |
| 53 | |
| 54 | shader_version = fsr_shader.version_create(); |
| 55 | pipeline = RD::get_singleton()->compute_pipeline_create(fsr_shader.version_get_shader(shader_version, 0)); |
| 56 | } |
| 57 | |
| 58 | FSR::~FSR() { |
| 59 | fsr_shader.version_free(shader_version); |
| 60 | } |
| 61 | |
| 62 | void FSR::fsr_upscale(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_source_rd_texture, RID p_destination_texture) { |
| 63 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 64 | ERR_FAIL_NULL(uniform_set_cache); |
| 65 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 66 | ERR_FAIL_NULL(material_storage); |
| 67 | |
| 68 | Size2i internal_size = p_render_buffers->get_internal_size(); |
| 69 | Size2i target_size = p_render_buffers->get_target_size(); |
| 70 | float fsr_upscale_sharpness = p_render_buffers->get_fsr_sharpness(); |
| 71 | |
| 72 | if (!p_render_buffers->has_texture(SNAME("FSR" ), SNAME("upscale_texture" ))) { |
| 73 | RD::DataFormat format = p_render_buffers->get_base_data_format(); |
| 74 | uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 75 | uint32_t layers = 1; // we only need one layer, in multiview we're processing one layer at a time. |
| 76 | |
| 77 | p_render_buffers->create_texture(SNAME("FSR" ), SNAME("upscale_texture" ), format, usage_bits, RD::TEXTURE_SAMPLES_1, target_size, layers); |
| 78 | } |
| 79 | |
| 80 | RID upscale_texture = p_render_buffers->get_texture(SNAME("FSR" ), SNAME("upscale_texture" )); |
| 81 | |
| 82 | FSRUpscalePushConstant push_constant; |
| 83 | memset(&push_constant, 0, sizeof(FSRUpscalePushConstant)); |
| 84 | |
| 85 | int dispatch_x = (target_size.x + 15) / 16; |
| 86 | int dispatch_y = (target_size.y + 15) / 16; |
| 87 | |
| 88 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 89 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline); |
| 90 | |
| 91 | push_constant.resolution_width = internal_size.width; |
| 92 | push_constant.resolution_height = internal_size.height; |
| 93 | push_constant.upscaled_width = target_size.width; |
| 94 | push_constant.upscaled_height = target_size.height; |
| 95 | push_constant.sharpness = fsr_upscale_sharpness; |
| 96 | |
| 97 | RID shader = fsr_shader.version_get_shader(shader_version, 0); |
| 98 | ERR_FAIL_COND(shader.is_null()); |
| 99 | |
| 100 | RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 101 | |
| 102 | //FSR Easc |
| 103 | RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, { default_sampler, p_source_rd_texture }); |
| 104 | RD::Uniform u_upscale_texture(RD::UNIFORM_TYPE_IMAGE, 0, { upscale_texture }); |
| 105 | |
| 106 | push_constant.pass = FSR_UPSCALE_PASS_EASU; |
| 107 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0); |
| 108 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_upscale_texture), 1); |
| 109 | |
| 110 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(FSRUpscalePushConstant)); |
| 111 | |
| 112 | RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1); |
| 113 | RD::get_singleton()->compute_list_add_barrier(compute_list); |
| 114 | |
| 115 | //FSR Rcas |
| 116 | RD::Uniform u_upscale_texture_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, { default_sampler, upscale_texture }); |
| 117 | RD::Uniform u_destination_texture(RD::UNIFORM_TYPE_IMAGE, 0, { p_destination_texture }); |
| 118 | |
| 119 | push_constant.pass = FSR_UPSCALE_PASS_RCAS; |
| 120 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_upscale_texture_with_sampler), 0); |
| 121 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_destination_texture), 1); |
| 122 | |
| 123 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(FSRUpscalePushConstant)); |
| 124 | |
| 125 | RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1); |
| 126 | |
| 127 | RD::get_singleton()->compute_list_end(compute_list); |
| 128 | } |
| 129 | |