Path: blob/master/servers/rendering/renderer_rd/effects/fsr2.h
10279 views
/**************************************************************************/1/* fsr2.h */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#pragma once3132#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_accumulate_pass.glsl.gen.h"33#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_autogen_reactive_pass.glsl.gen.h"34#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_compute_luminance_pyramid_pass.glsl.gen.h"35#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_depth_clip_pass.glsl.gen.h"36#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_lock_pass.glsl.gen.h"37#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_rcas_pass.glsl.gen.h"38#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_reconstruct_previous_depth_pass.glsl.gen.h"39#include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_tcr_autogen_pass.glsl.gen.h"4041// This flag doesn't actually control anything GCC specific in FSR2. It determines42// if symbols should be exported, which is not required for Godot.43#ifndef FFX_GCC44#define FFX_GCC45#endif4647#include "thirdparty/amd-fsr2/ffx_fsr2.h"4849#define FSR2_MAX_QUEUED_FRAMES (4)50#define FSR2_MAX_UNIFORM_BUFFERS (4)51#define FSR2_MAX_BUFFERED_DESCRIPTORS (FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES)52#define FSR2_UBO_RING_BUFFER_SIZE (FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS)5354namespace RendererRD {55class FSR2Context {56public:57enum ResourceID : uint32_t {58RESOURCE_ID_DYNAMIC = 0xFFFFFFFF59};6061struct Resources {62LocalVector<RID> rids;63LocalVector<LocalVector<RID>> mip_slice_rids;64LocalVector<uint32_t> ids;65LocalVector<FfxResourceDescription> descriptions;66LocalVector<uint32_t> dynamic_list;67LocalVector<uint32_t> free_list;6869uint32_t add(RID p_rid, bool p_dynamic, uint32_t p_id, FfxResourceDescription p_description) {70uint32_t ret_index;71if (free_list.is_empty()) {72ret_index = rids.size();73uint32_t new_size = ret_index + 1;74rids.resize(new_size);75mip_slice_rids.resize(new_size);76ids.resize(new_size);77descriptions.resize(new_size);78} else {79uint32_t end_index = free_list.size() - 1;80ret_index = free_list[end_index];81free_list.resize(end_index);82}8384rids[ret_index] = p_rid;85mip_slice_rids[ret_index].clear();86ids[ret_index] = p_id;87descriptions[ret_index] = p_description;8889if (p_dynamic) {90dynamic_list.push_back(ret_index);91}9293return ret_index;94}9596void remove(uint32_t p_index) {97DEV_ASSERT(p_index < rids.size());98free_list.push_back(p_index);99rids[p_index] = RID();100mip_slice_rids[p_index].clear();101ids[p_index] = 0;102descriptions[p_index] = {};103dynamic_list.erase(p_index);104}105106uint32_t size() const {107return rids.size();108}109};110111struct Scratch {112Resources resources;113LocalVector<FfxGpuJobDescription> gpu_jobs;114RID ubo_ring_buffer[FSR2_UBO_RING_BUFFER_SIZE];115uint32_t ubo_ring_buffer_index = 0;116FfxDevice device = nullptr;117};118119Scratch scratch;120FfxFsr2Context fsr_context;121FfxFsr2ContextDescription fsr_desc;122123~FSR2Context();124};125126class FSR2Effect {127public:128struct RootSignature {129// Proxy structure to store the shader required by RD that uses the terminology used by the FSR2 API.130RID shader_rid;131};132133struct Pipeline {134RID pipeline_rid;135};136137struct Pass {138ShaderRD *shader;139RID shader_version;140RootSignature root_signature;141uint32_t shader_variant = 0;142Pipeline pipeline;143Vector<FfxResourceBinding> sampled_bindings;144Vector<FfxResourceBinding> storage_bindings;145Vector<FfxResourceBinding> uniform_bindings;146};147148struct Device {149Pass passes[FFX_FSR2_PASS_COUNT];150FfxDeviceCapabilities capabilities;151RID point_clamp_sampler;152RID linear_clamp_sampler;153};154155struct Parameters {156FSR2Context *context;157Size2i internal_size;158RID color;159RID depth;160RID velocity;161RID reactive;162RID exposure;163RID output;164float z_near = 0.0f;165float z_far = 0.0f;166float fovy = 0.0f;167Vector2 jitter;168float delta_time = 0.0f;169float sharpness = 0.0f;170bool reset_accumulation = false;171Projection reprojection;172};173174FSR2Effect();175~FSR2Effect();176FSR2Context *create_context(Size2i p_internal_size, Size2i p_target_size);177void upscale(const Parameters &p_params);178179private:180struct {181Fsr2DepthClipPassShaderRD depth_clip;182Fsr2ReconstructPreviousDepthPassShaderRD reconstruct_previous_depth;183Fsr2LockPassShaderRD lock;184Fsr2AccumulatePassShaderRD accumulate;185Fsr2AccumulatePassShaderRD accumulate_sharpen;186Fsr2RcasPassShaderRD rcas;187Fsr2ComputeLuminancePyramidPassShaderRD compute_luminance_pyramid;188Fsr2AutogenReactivePassShaderRD autogen_reactive;189Fsr2TcrAutogenPassShaderRD tcr_autogen;190} shaders;191192Device device;193};194195} // namespace RendererRD196197198