Path: blob/master/servers/rendering/renderer_rd/effects/fsr.cpp
10279 views
/**************************************************************************/1/* fsr.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "fsr.h"31#include "../storage_rd/material_storage.h"32#include "../uniform_set_cache_rd.h"3334using namespace RendererRD;3536FSR::FSR() {37Vector<String> fsr_upscale_modes;38fsr_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_NORMAL\n");39fsr_upscale_modes.push_back("\n#define MODE_FSR_UPSCALE_FALLBACK\n");40fsr_shader.initialize(fsr_upscale_modes);4142FSRShaderVariant variant;43if (RD::get_singleton()->has_feature(RD::SUPPORTS_HALF_FLOAT)) {44variant = FSR_SHADER_VARIANT_NORMAL;45} else {46variant = FSR_SHADER_VARIANT_FALLBACK;47}4849shader_version = fsr_shader.version_create();50pipeline = RD::get_singleton()->compute_pipeline_create(fsr_shader.version_get_shader(shader_version, variant));51}5253FSR::~FSR() {54fsr_shader.version_free(shader_version);55}5657void FSR::process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_source_rd_texture, RID p_destination_texture) {58UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();59ERR_FAIL_NULL(uniform_set_cache);60MaterialStorage *material_storage = MaterialStorage::get_singleton();61ERR_FAIL_NULL(material_storage);6263Size2i internal_size = p_render_buffers->get_internal_size();64Size2i target_size = p_render_buffers->get_target_size();65float fsr_upscale_sharpness = p_render_buffers->get_fsr_sharpness();6667if (!p_render_buffers->has_texture(SNAME("FSR"), SNAME("upscale_texture"))) {68RD::DataFormat format = p_render_buffers->get_base_data_format();69uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;70uint32_t layers = 1; // we only need one layer, in multiview we're processing one layer at a time.7172p_render_buffers->create_texture(SNAME("FSR"), SNAME("upscale_texture"), format, usage_bits, RD::TEXTURE_SAMPLES_1, target_size, layers);73}7475RID upscale_texture = p_render_buffers->get_texture(SNAME("FSR"), SNAME("upscale_texture"));7677FSRUpscalePushConstant push_constant;78memset(&push_constant, 0, sizeof(FSRUpscalePushConstant));7980int dispatch_x = (target_size.x + 15) / 16;81int dispatch_y = (target_size.y + 15) / 16;8283RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();84RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline);8586push_constant.resolution_width = internal_size.width;87push_constant.resolution_height = internal_size.height;88push_constant.upscaled_width = target_size.width;89push_constant.upscaled_height = target_size.height;90push_constant.sharpness = fsr_upscale_sharpness;9192RID shader = fsr_shader.version_get_shader(shader_version, 0);93ERR_FAIL_COND(shader.is_null());9495RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);9697//FSR Easc98RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, { default_sampler, p_source_rd_texture });99RD::Uniform u_upscale_texture(RD::UNIFORM_TYPE_IMAGE, 0, { upscale_texture });100101push_constant.pass = FSR_UPSCALE_PASS_EASU;102RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);103RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_upscale_texture), 1);104105RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(FSRUpscalePushConstant));106107RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);108RD::get_singleton()->compute_list_add_barrier(compute_list);109110//FSR Rcas111RD::Uniform u_upscale_texture_with_sampler(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, { default_sampler, upscale_texture });112RD::Uniform u_destination_texture(RD::UNIFORM_TYPE_IMAGE, 0, { p_destination_texture });113114push_constant.pass = FSR_UPSCALE_PASS_RCAS;115RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_upscale_texture_with_sampler), 0);116RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_destination_texture), 1);117118RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(FSRUpscalePushConstant));119120RD::get_singleton()->compute_list_dispatch(compute_list, dispatch_x, dispatch_y, 1);121122RD::get_singleton()->compute_list_end();123}124125126