Debian Codegraph @

servers/rendering/renderer_rd/effects/fsr2.h

raw symbols SCIP
1 /**************************************************************************/
2 /* fsr2.h */
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 #pragma once
32
33 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_accumulate_pass.glsl.gen.h"
34 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_autogen_reactive_pass.glsl.gen.h"
35 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_compute_luminance_pyramid_pass.glsl.gen.h"
36 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_depth_clip_pass.glsl.gen.h"
37 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_lock_pass.glsl.gen.h"
38 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_rcas_pass.glsl.gen.h"
39 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_reconstruct_previous_depth_pass.glsl.gen.h"
40 #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_tcr_autogen_pass.glsl.gen.h"
41
42 // This flag doesn't actually control anything GCC specific in FSR2. It determines
43 // if symbols should be exported, which is not required for Godot.
44 #ifndef FFX_GCC
45 #define FFX_GCC
46 #endif
47
48 #include "thirdparty/amd-fsr2/ffx_fsr2.h"
49
50 #define FSR2_MAX_QUEUED_FRAMES (4)
51 #define FSR2_MAX_UNIFORM_BUFFERS (4)
52 #define FSR2_MAX_BUFFERED_DESCRIPTORS (FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES)
53 #define FSR2_UBO_RING_BUFFER_SIZE (FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS)
54
55 namespace RendererRD {
56 class FSR2Context {
57 public:
58 enum ResourceID : uint32_t {
59 RESOURCE_ID_DYNAMIC = 0xFFFFFFFF
60 };
61
62 struct Resources {
63 LocalVector<RID> rids;
64 LocalVector<LocalVector<RID>> mip_slice_rids;
65 LocalVector<uint32_t> ids;
66 LocalVector<FfxResourceDescription> descriptions;
67 LocalVector<uint32_t> dynamic_list;
68 LocalVector<uint32_t> free_list;
69
70 uint32_t add(RID p_rid, bool p_dynamic, uint32_t p_id, FfxResourceDescription p_description) {
71 uint32_t ret_index;
72 if (free_list.is_empty()) {
73 ret_index = rids.size();
74 uint32_t new_size = ret_index + 1;
75 rids.resize(new_size);
76 mip_slice_rids.resize(new_size);
77 ids.resize(new_size);
78 descriptions.resize(new_size);
79 } else {
80 uint32_t end_index = free_list.size() - 1;
81 ret_index = free_list[end_index];
82 free_list.resize(end_index);
83 }
84
85 rids[ret_index] = p_rid;
86 mip_slice_rids[ret_index].clear();
87 ids[ret_index] = p_id;
88 descriptions[ret_index] = p_description;
89
90 if (p_dynamic) {
91 dynamic_list.push_back(ret_index);
92 }
93
94 return ret_index;
95 }
96
97 void remove(uint32_t p_index) {
98 DEV_ASSERT(p_index < rids.size());
99 free_list.push_back(p_index);
100 rids[p_index] = RID();
101 mip_slice_rids[p_index].clear();
102 ids[p_index] = 0;
103 descriptions[p_index] = {};
104 dynamic_list.erase(p_index);
105 }
106
107 uint32_t size() const {
108 return rids.size();
109 }
110 };
111
112 struct Scratch {
113 Resources resources;
114 LocalVector<FfxGpuJobDescription> gpu_jobs;
115 RID ubo_ring_buffer[FSR2_UBO_RING_BUFFER_SIZE];
116 uint32_t ubo_ring_buffer_index = 0;
117 FfxDevice device = nullptr;
118 };
119
120 Scratch scratch;
121 FfxFsr2Context fsr_context;
122 FfxFsr2ContextDescription fsr_desc;
123
124 ~FSR2Context();
125 };
126
127 class FSR2Effect {
128 public:
129 struct RootSignature {
130 // Proxy structure to store the shader required by RD that uses the terminology used by the FSR2 API.
131 RID shader_rid;
132 };
133
134 struct Pipeline {
135 RID pipeline_rid;
136 };
137
138 struct Pass {
139 ShaderRD *shader;
140 RID shader_version;
141 RootSignature root_signature;
142 uint32_t shader_variant = 0;
143 Pipeline pipeline;
144 Vector<FfxResourceBinding> sampled_bindings;
145 Vector<FfxResourceBinding> storage_bindings;
146 Vector<FfxResourceBinding> uniform_bindings;
147 };
148
149 struct Device {
150 Pass passes[FFX_FSR2_PASS_COUNT];
151 FfxDeviceCapabilities capabilities;
152 RID point_clamp_sampler;
153 RID linear_clamp_sampler;
154 };
155
156 struct Parameters {
157 FSR2Context *context;
158 Size2i internal_size;
159 RID color;
160 RID depth;
161 RID velocity;
162 RID reactive;
163 RID exposure;
164 RID output;
165 float z_near = 0.0f;
166 float z_far = 0.0f;
167 float fovy = 0.0f;
168 Vector2 jitter;
169 float delta_time = 0.0f;
170 float sharpness = 0.0f;
171 bool reset_accumulation = false;
172 Projection reprojection;
173 };
174
175 FSR2Effect();
176 ~FSR2Effect();
177 FSR2Context *create_context(Size2i p_internal_size, Size2i p_target_size);
178 void upscale(const Parameters &p_params);
179
180 private:
181 struct {
182 Fsr2DepthClipPassShaderRD depth_clip;
183 Fsr2ReconstructPreviousDepthPassShaderRD reconstruct_previous_depth;
184 Fsr2LockPassShaderRD lock;
185 Fsr2AccumulatePassShaderRD accumulate;
186 Fsr2AccumulatePassShaderRD accumulate_sharpen;
187 Fsr2RcasPassShaderRD rcas;
188 Fsr2ComputeLuminancePyramidPassShaderRD compute_luminance_pyramid;
189 Fsr2AutogenReactivePassShaderRD autogen_reactive;
190 Fsr2TcrAutogenPassShaderRD tcr_autogen;
191 } shaders;
192
193 Device device;
194 };
195
196 } // namespace RendererRD