| 1 | /**************************************************************************/ |
| 2 | /* fsr2.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 "fsr2.h" |
| 32 | |
| 33 | #include "../storage_rd/material_storage.h" |
| 34 | #include "../uniform_set_cache_rd.h" |
| 35 | |
| 36 | using namespace RendererRD; |
| 37 | |
| 38 | #ifndef _MSC_VER |
| 39 | #include <cwchar> |
| 40 | #define wcscpy_s wcscpy |
| 41 | #endif |
| 42 | |
| 43 | static RD::TextureType ffx_resource_type_to_rd_texture_type(FfxResourceType p_type) { |
| 44 | switch (p_type) { |
| 45 | case FFX_RESOURCE_TYPE_TEXTURE1D: |
| 46 | return RD::TEXTURE_TYPE_1D; |
| 47 | case FFX_RESOURCE_TYPE_TEXTURE2D: |
| 48 | return RD::TEXTURE_TYPE_2D; |
| 49 | case FFX_RESOURCE_TYPE_TEXTURE3D: |
| 50 | return RD::TEXTURE_TYPE_3D; |
| 51 | default: |
| 52 | return RD::TEXTURE_TYPE_MAX; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static FfxResourceType rd_texture_type_to_ffx_resource_type(RD::TextureType p_type) { |
| 57 | switch (p_type) { |
| 58 | case RD::TEXTURE_TYPE_1D: |
| 59 | return FFX_RESOURCE_TYPE_TEXTURE1D; |
| 60 | case RD::TEXTURE_TYPE_2D: |
| 61 | return FFX_RESOURCE_TYPE_TEXTURE2D; |
| 62 | case RD::TEXTURE_TYPE_3D: |
| 63 | return FFX_RESOURCE_TYPE_TEXTURE3D; |
| 64 | default: |
| 65 | return FFX_RESOURCE_TYPE_BUFFER; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static RD::DataFormat ffx_surface_format_to_rd_format(FfxSurfaceFormat p_format) { |
| 70 | switch (p_format) { |
| 71 | case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: |
| 72 | return RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 73 | case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: |
| 74 | return RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 75 | case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: |
| 76 | return RD::DATA_FORMAT_R16G16B16A16_SFLOAT; |
| 77 | case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: |
| 78 | return RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 79 | case FFX_SURFACE_FORMAT_R32G32_FLOAT: |
| 80 | return RD::DATA_FORMAT_R32G32_SFLOAT; |
| 81 | case FFX_SURFACE_FORMAT_R32_UINT: |
| 82 | return RD::DATA_FORMAT_R32_UINT; |
| 83 | case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: |
| 84 | return RD::DATA_FORMAT_R8G8B8A8_UNORM; |
| 85 | case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: |
| 86 | return RD::DATA_FORMAT_R8G8B8A8_UNORM; |
| 87 | case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: |
| 88 | return RD::DATA_FORMAT_B10G11R11_UFLOAT_PACK32; |
| 89 | case FFX_SURFACE_FORMAT_R16G16_FLOAT: |
| 90 | return RD::DATA_FORMAT_R16G16_SFLOAT; |
| 91 | case FFX_SURFACE_FORMAT_R16G16_UINT: |
| 92 | return RD::DATA_FORMAT_R16G16_UINT; |
| 93 | case FFX_SURFACE_FORMAT_R16_FLOAT: |
| 94 | return RD::DATA_FORMAT_R16_SFLOAT; |
| 95 | case FFX_SURFACE_FORMAT_R16_UINT: |
| 96 | return RD::DATA_FORMAT_R16_UINT; |
| 97 | case FFX_SURFACE_FORMAT_R16_UNORM: |
| 98 | return RD::DATA_FORMAT_R16_UNORM; |
| 99 | case FFX_SURFACE_FORMAT_R16_SNORM: |
| 100 | return RD::DATA_FORMAT_R16_SNORM; |
| 101 | case FFX_SURFACE_FORMAT_R8_UNORM: |
| 102 | return RD::DATA_FORMAT_R8_UNORM; |
| 103 | case FFX_SURFACE_FORMAT_R8_UINT: |
| 104 | return RD::DATA_FORMAT_R8_UINT; |
| 105 | case FFX_SURFACE_FORMAT_R8G8_UNORM: |
| 106 | return RD::DATA_FORMAT_R8G8_UNORM; |
| 107 | case FFX_SURFACE_FORMAT_R32_FLOAT: |
| 108 | return RD::DATA_FORMAT_R32_SFLOAT; |
| 109 | default: |
| 110 | return RD::DATA_FORMAT_MAX; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static FfxSurfaceFormat rd_format_to_ffx_surface_format(RD::DataFormat p_format) { |
| 115 | switch (p_format) { |
| 116 | case RD::DATA_FORMAT_R32G32B32A32_SFLOAT: |
| 117 | return FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT; |
| 118 | case RD::DATA_FORMAT_R16G16B16A16_SFLOAT: |
| 119 | return FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT; |
| 120 | case RD::DATA_FORMAT_R16G16B16A16_UNORM: |
| 121 | return FFX_SURFACE_FORMAT_R16G16B16A16_UNORM; |
| 122 | case RD::DATA_FORMAT_R32G32_SFLOAT: |
| 123 | return FFX_SURFACE_FORMAT_R32G32_FLOAT; |
| 124 | case RD::DATA_FORMAT_R32_UINT: |
| 125 | return FFX_SURFACE_FORMAT_R32_UINT; |
| 126 | case RD::DATA_FORMAT_R8G8B8A8_UNORM: |
| 127 | return FFX_SURFACE_FORMAT_R8G8B8A8_UNORM; |
| 128 | case RD::DATA_FORMAT_B10G11R11_UFLOAT_PACK32: |
| 129 | return FFX_SURFACE_FORMAT_R11G11B10_FLOAT; |
| 130 | case RD::DATA_FORMAT_R16G16_SFLOAT: |
| 131 | return FFX_SURFACE_FORMAT_R16G16_FLOAT; |
| 132 | case RD::DATA_FORMAT_R16G16_UINT: |
| 133 | return FFX_SURFACE_FORMAT_R16G16_UINT; |
| 134 | case RD::DATA_FORMAT_R16_SFLOAT: |
| 135 | return FFX_SURFACE_FORMAT_R16_FLOAT; |
| 136 | case RD::DATA_FORMAT_R16_UINT: |
| 137 | return FFX_SURFACE_FORMAT_R16_UINT; |
| 138 | case RD::DATA_FORMAT_R16_UNORM: |
| 139 | return FFX_SURFACE_FORMAT_R16_UNORM; |
| 140 | case RD::DATA_FORMAT_R16_SNORM: |
| 141 | return FFX_SURFACE_FORMAT_R16_SNORM; |
| 142 | case RD::DATA_FORMAT_R8_UNORM: |
| 143 | return FFX_SURFACE_FORMAT_R8_UNORM; |
| 144 | case RD::DATA_FORMAT_R8_UINT: |
| 145 | return FFX_SURFACE_FORMAT_R8_UINT; |
| 146 | case RD::DATA_FORMAT_R8G8_UNORM: |
| 147 | return FFX_SURFACE_FORMAT_R8G8_UNORM; |
| 148 | case RD::DATA_FORMAT_R32_SFLOAT: |
| 149 | return FFX_SURFACE_FORMAT_R32_FLOAT; |
| 150 | default: |
| 151 | return FFX_SURFACE_FORMAT_UNKNOWN; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static uint32_t ffx_usage_to_rd_usage_flags(uint32_t p_flags) { |
| 156 | uint32_t ret = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT; |
| 157 | |
| 158 | if (p_flags & FFX_RESOURCE_USAGE_RENDERTARGET) { |
| 159 | ret |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 160 | } |
| 161 | |
| 162 | if (p_flags & FFX_RESOURCE_USAGE_UAV) { |
| 163 | ret |= RD::TEXTURE_USAGE_STORAGE_BIT; |
| 164 | ret |= RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT; |
| 165 | ret |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT; |
| 166 | } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static FfxErrorCode create_backend_context_rd(FfxFsr2Interface *p_backend_interface, FfxDevice p_device) { |
| 172 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 173 | |
| 174 | // Store pointer to the device common to all contexts. |
| 175 | scratch.device = p_device; |
| 176 | |
| 177 | // Create a ring buffer of uniform buffers. |
| 178 | // FIXME: This could be optimized to be a single memory block if it was possible for RD to create views into a particular memory range of a UBO. |
| 179 | for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) { |
| 180 | scratch.ubo_ring_buffer[i] = RD::get_singleton()->uniform_buffer_create(FFX_MAX_CONST_SIZE * sizeof(uint32_t)); |
| 181 | ERR_FAIL_COND_V(scratch.ubo_ring_buffer[i].is_null(), FFX_ERROR_BACKEND_API_ERROR); |
| 182 | } |
| 183 | |
| 184 | return FFX_OK; |
| 185 | } |
| 186 | |
| 187 | static FfxErrorCode get_device_capabilities_rd(FfxFsr2Interface *p_backend_interface, FfxDeviceCapabilities *p_out_device_capabilities, FfxDevice p_device) { |
| 188 | FSR2Effect::Device &effect_device = *reinterpret_cast<FSR2Effect::Device *>(p_device); |
| 189 | |
| 190 | *p_out_device_capabilities = effect_device.capabilities; |
| 191 | |
| 192 | return FFX_OK; |
| 193 | } |
| 194 | |
| 195 | static FfxErrorCode destroy_backend_context_rd(FfxFsr2Interface *p_backend_interface) { |
| 196 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 197 | |
| 198 | for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) { |
| 199 | RD::get_singleton()->free_rid(scratch.ubo_ring_buffer[i]); |
| 200 | } |
| 201 | |
| 202 | return FFX_OK; |
| 203 | } |
| 204 | |
| 205 | static FfxErrorCode create_resource_rd(FfxFsr2Interface *p_backend_interface, const FfxCreateResourceDescription *p_create_resource_description, FfxResourceInternal *p_out_resource) { |
| 206 | // FSR2's base implementation won't issue a call to create a heap type that isn't just default on its own, |
| 207 | // so we can safely ignore it as RD does not expose this concept. |
| 208 | ERR_FAIL_COND_V(p_create_resource_description->heapType != FFX_HEAP_TYPE_DEFAULT, FFX_ERROR_INVALID_ARGUMENT); |
| 209 | |
| 210 | RenderingDevice *rd = RD::get_singleton(); |
| 211 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 212 | FfxResourceDescription res_desc = p_create_resource_description->resourceDescription; |
| 213 | |
| 214 | // FSR2's base implementation never requests buffer creation. |
| 215 | ERR_FAIL_COND_V(res_desc.type != FFX_RESOURCE_TYPE_TEXTURE1D && res_desc.type != FFX_RESOURCE_TYPE_TEXTURE2D && res_desc.type != FFX_RESOURCE_TYPE_TEXTURE3D, FFX_ERROR_INVALID_ARGUMENT); |
| 216 | |
| 217 | if (res_desc.mipCount == 0) { |
| 218 | // Mipmap count must be derived from the resource's dimensions. |
| 219 | res_desc.mipCount = uint32_t(1 + std::floor(std::log2(MAX(MAX(res_desc.width, res_desc.height), res_desc.depth)))); |
| 220 | } |
| 221 | |
| 222 | Vector<PackedByteArray> initial_data; |
| 223 | if (p_create_resource_description->initDataSize) { |
| 224 | PackedByteArray byte_array; |
| 225 | byte_array.resize(p_create_resource_description->initDataSize); |
| 226 | memcpy(byte_array.ptrw(), p_create_resource_description->initData, p_create_resource_description->initDataSize); |
| 227 | initial_data.push_back(byte_array); |
| 228 | } |
| 229 | |
| 230 | RD::TextureFormat texture_format; |
| 231 | texture_format.texture_type = ffx_resource_type_to_rd_texture_type(res_desc.type); |
| 232 | texture_format.format = ffx_surface_format_to_rd_format(res_desc.format); |
| 233 | texture_format.usage_bits = ffx_usage_to_rd_usage_flags(p_create_resource_description->usage); |
| 234 | texture_format.width = res_desc.width; |
| 235 | texture_format.height = res_desc.height; |
| 236 | texture_format.depth = res_desc.depth; |
| 237 | texture_format.mipmaps = res_desc.mipCount; |
| 238 | texture_format.is_discardable = true; |
| 239 | |
| 240 | RID texture = rd->texture_create(texture_format, RD::TextureView(), initial_data); |
| 241 | ERR_FAIL_COND_V(texture.is_null(), FFX_ERROR_BACKEND_API_ERROR); |
| 242 | |
| 243 | rd->set_resource_name(texture, String(p_create_resource_description->name)); |
| 244 | |
| 245 | // Add the resource to the storage and use the internal index to reference it. |
| 246 | p_out_resource->internalIndex = scratch.resources.add(texture, false, p_create_resource_description->id, res_desc); |
| 247 | |
| 248 | return FFX_OK; |
| 249 | } |
| 250 | |
| 251 | static FfxErrorCode register_resource_rd(FfxFsr2Interface *p_backend_interface, const FfxResource *p_in_resource, FfxResourceInternal *p_out_resource) { |
| 252 | if (p_in_resource->resource == nullptr) { |
| 253 | // Null resource case. |
| 254 | p_out_resource->internalIndex = -1; |
| 255 | return FFX_OK; |
| 256 | } |
| 257 | |
| 258 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 259 | const RID &rid = *reinterpret_cast<const RID *>(p_in_resource->resource); |
| 260 | ERR_FAIL_COND_V(rid.is_null(), FFX_ERROR_INVALID_ARGUMENT); |
| 261 | |
| 262 | // Add the resource to the storage and use the internal index to reference it. |
| 263 | p_out_resource->internalIndex = scratch.resources.add(rid, true, FSR2Context::RESOURCE_ID_DYNAMIC, p_in_resource->description); |
| 264 | |
| 265 | return FFX_OK; |
| 266 | } |
| 267 | |
| 268 | static FfxErrorCode unregister_resources_rd(FfxFsr2Interface *p_backend_interface) { |
| 269 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 270 | LocalVector<uint32_t> dynamic_list_copy = scratch.resources.dynamic_list; |
| 271 | for (uint32_t i : dynamic_list_copy) { |
| 272 | scratch.resources.remove(i); |
| 273 | } |
| 274 | |
| 275 | return FFX_OK; |
| 276 | } |
| 277 | |
| 278 | static FfxResourceDescription get_resource_description_rd(FfxFsr2Interface *p_backend_interface, FfxResourceInternal p_resource) { |
| 279 | if (p_resource.internalIndex != -1) { |
| 280 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 281 | return scratch.resources.descriptions[p_resource.internalIndex]; |
| 282 | } else { |
| 283 | return {}; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static FfxErrorCode destroy_resource_rd(FfxFsr2Interface *p_backend_interface, FfxResourceInternal p_resource) { |
| 288 | if (p_resource.internalIndex != -1) { |
| 289 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 290 | if (scratch.resources.rids[p_resource.internalIndex].is_valid()) { |
| 291 | RD::get_singleton()->free_rid(scratch.resources.rids[p_resource.internalIndex]); |
| 292 | scratch.resources.remove(p_resource.internalIndex); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return FFX_OK; |
| 297 | } |
| 298 | |
| 299 | static FfxErrorCode create_pipeline_rd(FfxFsr2Interface *p_backend_interface, FfxFsr2Pass p_pass, const FfxPipelineDescription *p_pipeline_description, FfxPipelineState *p_out_pipeline) { |
| 300 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 301 | FSR2Effect::Device &device = *reinterpret_cast<FSR2Effect::Device *>(scratch.device); |
| 302 | FSR2Effect::Pass &effect_pass = device.passes[p_pass]; |
| 303 | |
| 304 | if (effect_pass.pipeline.pipeline_rid.is_null()) { |
| 305 | // Create pipeline for the device if it hasn't been created yet. |
| 306 | effect_pass.root_signature.shader_rid = effect_pass.shader->version_get_shader(effect_pass.shader_version, effect_pass.shader_variant); |
| 307 | ERR_FAIL_COND_V(effect_pass.root_signature.shader_rid.is_null(), FFX_ERROR_BACKEND_API_ERROR); |
| 308 | |
| 309 | effect_pass.pipeline.pipeline_rid = RD::get_singleton()->compute_pipeline_create(effect_pass.root_signature.shader_rid); |
| 310 | ERR_FAIL_COND_V(effect_pass.pipeline.pipeline_rid.is_null(), FFX_ERROR_BACKEND_API_ERROR); |
| 311 | } |
| 312 | |
| 313 | // While this is not their intended use, we use the pipeline and root signature pointers to store the |
| 314 | // RIDs to the pipeline and shader that RD needs for the compute pipeline. |
| 315 | p_out_pipeline->pipeline = reinterpret_cast<FfxPipeline>(&effect_pass.pipeline); |
| 316 | p_out_pipeline->rootSignature = reinterpret_cast<FfxRootSignature>(&effect_pass.root_signature); |
| 317 | |
| 318 | p_out_pipeline->srvCount = effect_pass.sampled_bindings.size(); |
| 319 | ERR_FAIL_COND_V(p_out_pipeline->srvCount > FFX_MAX_NUM_SRVS, FFX_ERROR_OUT_OF_RANGE); |
| 320 | memcpy(p_out_pipeline->srvResourceBindings, effect_pass.sampled_bindings.ptr(), sizeof(FfxResourceBinding) * p_out_pipeline->srvCount); |
| 321 | |
| 322 | p_out_pipeline->uavCount = effect_pass.storage_bindings.size(); |
| 323 | ERR_FAIL_COND_V(p_out_pipeline->uavCount > FFX_MAX_NUM_UAVS, FFX_ERROR_OUT_OF_RANGE); |
| 324 | memcpy(p_out_pipeline->uavResourceBindings, effect_pass.storage_bindings.ptr(), sizeof(FfxResourceBinding) * p_out_pipeline->uavCount); |
| 325 | |
| 326 | p_out_pipeline->constCount = effect_pass.uniform_bindings.size(); |
| 327 | ERR_FAIL_COND_V(p_out_pipeline->constCount > FFX_MAX_NUM_CONST_BUFFERS, FFX_ERROR_OUT_OF_RANGE); |
| 328 | memcpy(p_out_pipeline->cbResourceBindings, effect_pass.uniform_bindings.ptr(), sizeof(FfxResourceBinding) * p_out_pipeline->constCount); |
| 329 | |
| 330 | bool low_resolution_mvs = (p_pipeline_description->contextFlags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) == 0; |
| 331 | |
| 332 | if (p_pass == FFX_FSR2_PASS_ACCUMULATE || p_pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) { |
| 333 | // Change the binding for motion vectors in this particular pass if low resolution MVs are used. |
| 334 | if (low_resolution_mvs) { |
| 335 | FfxResourceBinding &binding = p_out_pipeline->srvResourceBindings[2]; |
| 336 | wcscpy_s(binding.name, L"r_dilated_motion_vectors"); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return FFX_OK; |
| 341 | } |
| 342 | |
| 343 | static FfxErrorCode destroy_pipeline_rd(FfxFsr2Interface *p_backend_interface, FfxPipelineState *p_pipeline) { |
| 344 | // We don't want to destroy pipelines when the FSR2 API deems it necessary as it'll do so whenever the context is destroyed. |
| 345 | |
| 346 | return FFX_OK; |
| 347 | } |
| 348 | |
| 349 | static FfxErrorCode schedule_gpu_job_rd(FfxFsr2Interface *p_backend_interface, const FfxGpuJobDescription *p_job) { |
| 350 | ERR_FAIL_NULL_V(p_backend_interface, FFX_ERROR_INVALID_ARGUMENT); |
| 351 | ERR_FAIL_NULL_V(p_job, FFX_ERROR_INVALID_ARGUMENT); |
| 352 | |
| 353 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 354 | scratch.gpu_jobs.push_back(*p_job); |
| 355 | |
| 356 | return FFX_OK; |
| 357 | } |
| 358 | |
| 359 | static FfxErrorCode execute_gpu_job_clear_float_rd(FSR2Context::Scratch &p_scratch, const FfxClearFloatJobDescription &p_job) { |
| 360 | RID resource = p_scratch.resources.rids[p_job.target.internalIndex]; |
| 361 | FfxResourceDescription &desc = p_scratch.resources.descriptions[p_job.target.internalIndex]; |
| 362 | |
| 363 | ERR_FAIL_COND_V(desc.type == FFX_RESOURCE_TYPE_BUFFER, FFX_ERROR_INVALID_ARGUMENT); |
| 364 | |
| 365 | Color color(p_job.color[0], p_job.color[1], p_job.color[2], p_job.color[3]); |
| 366 | RD::get_singleton()->texture_clear(resource, color, 0, desc.mipCount, 0, 1); |
| 367 | |
| 368 | return FFX_OK; |
| 369 | } |
| 370 | |
| 371 | static FfxErrorCode execute_gpu_job_copy_rd(FSR2Context::Scratch &p_scratch, const FfxCopyJobDescription &p_job) { |
| 372 | RID src = p_scratch.resources.rids[p_job.src.internalIndex]; |
| 373 | RID dst = p_scratch.resources.rids[p_job.dst.internalIndex]; |
| 374 | FfxResourceDescription &src_desc = p_scratch.resources.descriptions[p_job.src.internalIndex]; |
| 375 | FfxResourceDescription &dst_desc = p_scratch.resources.descriptions[p_job.dst.internalIndex]; |
| 376 | |
| 377 | ERR_FAIL_COND_V(src_desc.type == FFX_RESOURCE_TYPE_BUFFER, FFX_ERROR_INVALID_ARGUMENT); |
| 378 | ERR_FAIL_COND_V(dst_desc.type == FFX_RESOURCE_TYPE_BUFFER, FFX_ERROR_INVALID_ARGUMENT); |
| 379 | |
| 380 | for (uint32_t mip_level = 0; mip_level < src_desc.mipCount; mip_level++) { |
| 381 | RD::get_singleton()->texture_copy(src, dst, Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(src_desc.width, src_desc.height, src_desc.depth), mip_level, mip_level, 0, 0); |
| 382 | } |
| 383 | |
| 384 | return FFX_OK; |
| 385 | } |
| 386 | |
| 387 | static FfxErrorCode execute_gpu_job_compute_rd(FSR2Context::Scratch &p_scratch, const FfxComputeJobDescription &p_job) { |
| 388 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 389 | ERR_FAIL_NULL_V(uniform_set_cache, FFX_ERROR_BACKEND_API_ERROR); |
| 390 | |
| 391 | FSR2Effect::RootSignature &root_signature = *reinterpret_cast<FSR2Effect::RootSignature *>(p_job.pipeline.rootSignature); |
| 392 | ERR_FAIL_COND_V(root_signature.shader_rid.is_null(), FFX_ERROR_INVALID_ARGUMENT); |
| 393 | |
| 394 | FSR2Effect::Pipeline &backend_pipeline = *reinterpret_cast<FSR2Effect::Pipeline *>(p_job.pipeline.pipeline); |
| 395 | ERR_FAIL_COND_V(backend_pipeline.pipeline_rid.is_null(), FFX_ERROR_INVALID_ARGUMENT); |
| 396 | |
| 397 | thread_local LocalVector<RD::Uniform> compute_uniforms; |
| 398 | compute_uniforms.clear(); |
| 399 | |
| 400 | for (uint32_t i = 0; i < p_job.pipeline.srvCount; i++) { |
| 401 | RID texture_rid = p_scratch.resources.rids[p_job.srvs[i].internalIndex]; |
| 402 | RD::Uniform texture_uniform(RD::UNIFORM_TYPE_TEXTURE, p_job.pipeline.srvResourceBindings[i].slotIndex, texture_rid); |
| 403 | compute_uniforms.push_back(texture_uniform); |
| 404 | } |
| 405 | |
| 406 | for (uint32_t i = 0; i < p_job.pipeline.uavCount; i++) { |
| 407 | RID image_rid = p_scratch.resources.rids[p_job.uavs[i].internalIndex]; |
| 408 | RD::Uniform storage_uniform; |
| 409 | storage_uniform.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
| 410 | storage_uniform.binding = p_job.pipeline.uavResourceBindings[i].slotIndex; |
| 411 | |
| 412 | if (p_job.uavMip[i] > 0) { |
| 413 | LocalVector<RID> &mip_slice_rids = p_scratch.resources.mip_slice_rids[p_job.uavs[i].internalIndex]; |
| 414 | if (mip_slice_rids.is_empty()) { |
| 415 | mip_slice_rids.resize(p_scratch.resources.descriptions[p_job.uavs[i].internalIndex].mipCount); |
| 416 | } |
| 417 | |
| 418 | ERR_FAIL_COND_V(p_job.uavMip[i] >= mip_slice_rids.size(), FFX_ERROR_INVALID_ARGUMENT); |
| 419 | |
| 420 | if (mip_slice_rids[p_job.uavMip[i]].is_null()) { |
| 421 | mip_slice_rids[p_job.uavMip[i]] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), image_rid, 0, p_job.uavMip[i]); |
| 422 | } |
| 423 | |
| 424 | ERR_FAIL_COND_V(mip_slice_rids[p_job.uavMip[i]].is_null(), FFX_ERROR_BACKEND_API_ERROR); |
| 425 | |
| 426 | storage_uniform.append_id(mip_slice_rids[p_job.uavMip[i]]); |
| 427 | } else { |
| 428 | storage_uniform.append_id(image_rid); |
| 429 | } |
| 430 | |
| 431 | compute_uniforms.push_back(storage_uniform); |
| 432 | } |
| 433 | |
| 434 | for (uint32_t i = 0; i < p_job.pipeline.constCount; i++) { |
| 435 | RID buffer_rid = p_scratch.ubo_ring_buffer[p_scratch.ubo_ring_buffer_index]; |
| 436 | p_scratch.ubo_ring_buffer_index = (p_scratch.ubo_ring_buffer_index + 1) % FSR2_UBO_RING_BUFFER_SIZE; |
| 437 | |
| 438 | RD::get_singleton()->buffer_update(buffer_rid, 0, p_job.cbs[i].uint32Size * sizeof(uint32_t), p_job.cbs[i].data); |
| 439 | |
| 440 | RD::Uniform buffer_uniform(RD::UNIFORM_TYPE_UNIFORM_BUFFER, p_job.pipeline.cbResourceBindings[i].slotIndex, buffer_rid); |
| 441 | compute_uniforms.push_back(buffer_uniform); |
| 442 | } |
| 443 | |
| 444 | FSR2Effect::Device &device = *reinterpret_cast<FSR2Effect::Device *>(p_scratch.device); |
| 445 | RD::Uniform u_point_clamp_sampler(RD::UniformType::UNIFORM_TYPE_SAMPLER, 0, device.point_clamp_sampler); |
| 446 | RD::Uniform u_linear_clamp_sampler(RD::UniformType::UNIFORM_TYPE_SAMPLER, 1, device.linear_clamp_sampler); |
| 447 | |
| 448 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 449 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, backend_pipeline.pipeline_rid); |
| 450 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(root_signature.shader_rid, 0, u_point_clamp_sampler, u_linear_clamp_sampler), 0); |
| 451 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache_vec(root_signature.shader_rid, 1, compute_uniforms), 1); |
| 452 | RD::get_singleton()->compute_list_dispatch(compute_list, p_job.dimensions[0], p_job.dimensions[1], p_job.dimensions[2]); |
| 453 | RD::get_singleton()->compute_list_end(); |
| 454 | |
| 455 | return FFX_OK; |
| 456 | } |
| 457 | |
| 458 | static FfxErrorCode execute_gpu_jobs_rd(FfxFsr2Interface *p_backend_interface, FfxCommandList p_command_list) { |
| 459 | ERR_FAIL_NULL_V(p_backend_interface, FFX_ERROR_INVALID_ARGUMENT); |
| 460 | |
| 461 | FSR2Context::Scratch &scratch = *reinterpret_cast<FSR2Context::Scratch *>(p_backend_interface->scratchBuffer); |
| 462 | FfxErrorCode error_code = FFX_OK; |
| 463 | for (const FfxGpuJobDescription &job : scratch.gpu_jobs) { |
| 464 | switch (job.jobType) { |
| 465 | case FFX_GPU_JOB_CLEAR_FLOAT: { |
| 466 | error_code = execute_gpu_job_clear_float_rd(scratch, job.clearJobDescriptor); |
| 467 | } break; |
| 468 | case FFX_GPU_JOB_COPY: { |
| 469 | error_code = execute_gpu_job_copy_rd(scratch, job.copyJobDescriptor); |
| 470 | } break; |
| 471 | case FFX_GPU_JOB_COMPUTE: { |
| 472 | error_code = execute_gpu_job_compute_rd(scratch, job.computeJobDescriptor); |
| 473 | } break; |
| 474 | default: { |
| 475 | error_code = FFX_ERROR_INVALID_ARGUMENT; |
| 476 | } break; |
| 477 | } |
| 478 | |
| 479 | if (error_code != FFX_OK) { |
| 480 | scratch.gpu_jobs.clear(); |
| 481 | return error_code; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | scratch.gpu_jobs.clear(); |
| 486 | |
| 487 | return FFX_OK; |
| 488 | } |
| 489 | |
| 490 | static FfxResource get_resource_rd(RID *p_rid, const wchar_t *p_name) { |
| 491 | FfxResource res = {}; |
| 492 | if (p_rid->is_null()) { |
| 493 | return res; |
| 494 | } |
| 495 | |
| 496 | wcscpy_s(res.name, p_name); |
| 497 | |
| 498 | RD::TextureFormat texture_format = RD::get_singleton()->texture_get_format(*p_rid); |
| 499 | res.description.type = rd_texture_type_to_ffx_resource_type(texture_format.texture_type); |
| 500 | res.description.format = rd_format_to_ffx_surface_format(texture_format.format); |
| 501 | res.description.width = texture_format.width; |
| 502 | res.description.height = texture_format.height; |
| 503 | res.description.depth = texture_format.depth; |
| 504 | res.description.mipCount = texture_format.mipmaps; |
| 505 | res.description.flags = FFX_RESOURCE_FLAGS_NONE; |
| 506 | res.resource = reinterpret_cast<void *>(p_rid); |
| 507 | res.isDepth = texture_format.usage_bits & RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 508 | |
| 509 | return res; |
| 510 | } |
| 511 | |
| 512 | FSR2Context::~FSR2Context() { |
| 513 | ffxFsr2ContextDestroy(&fsr_context); |
| 514 | } |
| 515 | |
| 516 | FSR2Effect::FSR2Effect() { |
| 517 | FfxDeviceCapabilities &capabilities = device.capabilities; |
| 518 | capabilities.minimumSupportedShaderModel = FFX_SHADER_MODEL_5_1; |
| 519 | capabilities.waveLaneCountMin = 32; |
| 520 | capabilities.waveLaneCountMax = 32; |
| 521 | capabilities.fp16Supported = RD::get_singleton()->has_feature(RD::Features::SUPPORTS_HALF_FLOAT); |
| 522 | capabilities.raytracingSupported = false; |
| 523 | |
| 524 | String general_defines = |
| 525 | "\n#define FFX_GPU\n" |
| 526 | "\n#define FFX_GLSL 1\n" |
| 527 | "\n#define FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS 1\n" |
| 528 | "\n#define FFX_FSR2_OPTION_HDR_COLOR_INPUT 1\n" |
| 529 | "\n#define FFX_FSR2_OPTION_INVERTED_DEPTH 1\n" |
| 530 | "\n#define FFX_FSR2_OPTION_GODOT_REACTIVE_MASK_CLAMP 1\n" |
| 531 | "\n#define FFX_FSR2_OPTION_GODOT_DERIVE_INVALID_MOTION_VECTORS 1\n"; |
| 532 | |
| 533 | Vector<String> modes_single; |
| 534 | modes_single.push_back(""); |
| 535 | |
| 536 | Vector<String> modes_with_fp16; |
| 537 | modes_with_fp16.push_back(""); |
| 538 | modes_with_fp16.push_back("\n#define FFX_HALF 1\n"); |
| 539 | |
| 540 | // Since Godot currently lacks a shader reflection mechanism to persist the name of the bindings in the shader cache and |
| 541 | // there's also no mechanism to compile the shaders offline, the bindings are created manually by looking at the GLSL |
| 542 | // files included in FSR2 and mapping the macro bindings (#define FSR2_BIND_*) to their respective implementation names. |
| 543 | // |
| 544 | // It is not guaranteed these will remain consistent at all between versions of FSR2, so it'll be necessary to keep these |
| 545 | // bindings up to date whenever the library is updated. In such cases, it is very likely the validation layer will throw an |
| 546 | // error if the bindings do not match. |
| 547 | |
| 548 | { |
| 549 | Pass &pass = device.passes[FFX_FSR2_PASS_DEPTH_CLIP]; |
| 550 | pass.shader = &shaders.depth_clip; |
| 551 | pass.shader->initialize(modes_with_fp16, general_defines); |
| 552 | pass.shader_version = pass.shader->version_create(); |
| 553 | pass.shader_variant = capabilities.fp16Supported ? 1 : 0; |
| 554 | |
| 555 | pass.sampled_bindings = { |
| 556 | FfxResourceBinding{ 0, 0, L"r_reconstructed_previous_nearest_depth" }, |
| 557 | FfxResourceBinding{ 1, 0, L"r_dilated_motion_vectors" }, |
| 558 | FfxResourceBinding{ 2, 0, L"r_dilatedDepth" }, |
| 559 | FfxResourceBinding{ 3, 0, L"r_reactive_mask" }, |
| 560 | FfxResourceBinding{ 4, 0, L"r_transparency_and_composition_mask" }, |
| 561 | FfxResourceBinding{ 6, 0, L"r_previous_dilated_motion_vectors" }, |
| 562 | FfxResourceBinding{ 7, 0, L"r_input_motion_vectors" }, |
| 563 | FfxResourceBinding{ 8, 0, L"r_input_color_jittered" }, |
| 564 | FfxResourceBinding{ 9, 0, L"r_input_depth" }, |
| 565 | FfxResourceBinding{ 10, 0, L"r_input_exposure" } |
| 566 | }; |
| 567 | |
| 568 | pass.storage_bindings = { |
| 569 | // FSR2_BIND_UAV_DEPTH_CLIP (11) does not point to anything. |
| 570 | FfxResourceBinding{ 12, 0, L"rw_dilated_reactive_masks" }, |
| 571 | FfxResourceBinding{ 13, 0, L"rw_prepared_input_color" } |
| 572 | }; |
| 573 | |
| 574 | pass.uniform_bindings = { |
| 575 | FfxResourceBinding{ 14, 0, L"cbFSR2" } |
| 576 | }; |
| 577 | } |
| 578 | |
| 579 | { |
| 580 | Pass &pass = device.passes[FFX_FSR2_PASS_RECONSTRUCT_PREVIOUS_DEPTH]; |
| 581 | pass.shader = &shaders.reconstruct_previous_depth; |
| 582 | pass.shader->initialize(modes_with_fp16, general_defines); |
| 583 | pass.shader_version = pass.shader->version_create(); |
| 584 | pass.shader_variant = capabilities.fp16Supported ? 1 : 0; |
| 585 | |
| 586 | pass.sampled_bindings = { |
| 587 | FfxResourceBinding{ 0, 0, L"r_input_motion_vectors" }, |
| 588 | FfxResourceBinding{ 1, 0, L"r_input_depth" }, |
| 589 | FfxResourceBinding{ 2, 0, L"r_input_color_jittered" }, |
| 590 | FfxResourceBinding{ 3, 0, L"r_input_exposure" }, |
| 591 | FfxResourceBinding{ 4, 0, L"r_luma_history" } |
| 592 | }; |
| 593 | |
| 594 | pass.storage_bindings = { |
| 595 | FfxResourceBinding{ 5, 0, L"rw_reconstructed_previous_nearest_depth" }, |
| 596 | FfxResourceBinding{ 6, 0, L"rw_dilated_motion_vectors" }, |
| 597 | FfxResourceBinding{ 7, 0, L"rw_dilatedDepth" }, |
| 598 | FfxResourceBinding{ 8, 0, L"rw_prepared_input_color" }, |
| 599 | FfxResourceBinding{ 9, 0, L"rw_luma_history" }, |
| 600 | // FSR2_BIND_UAV_LUMA_INSTABILITY (10) does not point to anything. |
| 601 | FfxResourceBinding{ 11, 0, L"rw_lock_input_luma" } |
| 602 | }; |
| 603 | |
| 604 | pass.uniform_bindings = { |
| 605 | FfxResourceBinding{ 12, 0, L"cbFSR2" } |
| 606 | }; |
| 607 | } |
| 608 | |
| 609 | { |
| 610 | Pass &pass = device.passes[FFX_FSR2_PASS_LOCK]; |
| 611 | pass.shader = &shaders.lock; |
| 612 | pass.shader->initialize(modes_with_fp16, general_defines); |
| 613 | pass.shader_version = pass.shader->version_create(); |
| 614 | pass.shader_variant = capabilities.fp16Supported ? 1 : 0; |
| 615 | |
| 616 | pass.sampled_bindings = { |
| 617 | FfxResourceBinding{ 0, 0, L"r_lock_input_luma" } |
| 618 | }; |
| 619 | |
| 620 | pass.storage_bindings = { |
| 621 | FfxResourceBinding{ 1, 0, L"rw_new_locks" }, |
| 622 | FfxResourceBinding{ 2, 0, L"rw_reconstructed_previous_nearest_depth" } |
| 623 | }; |
| 624 | |
| 625 | pass.uniform_bindings = { |
| 626 | FfxResourceBinding{ 3, 0, L"cbFSR2" } |
| 627 | }; |
| 628 | } |
| 629 | |
| 630 | { |
| 631 | Vector<String> accumulate_modes_with_fp16; |
| 632 | accumulate_modes_with_fp16.push_back("\n"); |
| 633 | accumulate_modes_with_fp16.push_back("\n#define FFX_FSR2_OPTION_APPLY_SHARPENING 1\n"); |
| 634 | accumulate_modes_with_fp16.push_back("\n#define FFX_HALF 1\n"); |
| 635 | accumulate_modes_with_fp16.push_back("\n#define FFX_HALF 1\n#define FFX_FSR2_OPTION_APPLY_SHARPENING 1\n"); |
| 636 | |
| 637 | // Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput. |
| 638 | const bool fp16_path_supported = RD::get_singleton()->get_device_vendor_name() != "NVIDIA"; |
| 639 | Pass &pass = device.passes[FFX_FSR2_PASS_ACCUMULATE]; |
| 640 | pass.shader = &shaders.accumulate; |
| 641 | pass.shader->initialize(accumulate_modes_with_fp16, general_defines); |
| 642 | pass.shader_version = pass.shader->version_create(); |
| 643 | pass.shader_variant = capabilities.fp16Supported && fp16_path_supported ? 2 : 0; |
| 644 | |
| 645 | pass.sampled_bindings = { |
| 646 | FfxResourceBinding{ 0, 0, L"r_input_exposure" }, |
| 647 | FfxResourceBinding{ 1, 0, L"r_dilated_reactive_masks" }, |
| 648 | FfxResourceBinding{ 2, 0, L"r_input_motion_vectors" }, |
| 649 | FfxResourceBinding{ 3, 0, L"r_internal_upscaled_color" }, |
| 650 | FfxResourceBinding{ 4, 0, L"r_lock_status" }, |
| 651 | FfxResourceBinding{ 5, 0, L"r_input_depth" }, |
| 652 | FfxResourceBinding{ 6, 0, L"r_prepared_input_color" }, |
| 653 | // FSR2_BIND_SRV_LUMA_INSTABILITY(7) does not point to anything. |
| 654 | FfxResourceBinding{ 8, 0, L"r_lanczos_lut" }, |
| 655 | FfxResourceBinding{ 9, 0, L"r_upsample_maximum_bias_lut" }, |
| 656 | FfxResourceBinding{ 10, 0, L"r_imgMips" }, |
| 657 | FfxResourceBinding{ 11, 0, L"r_auto_exposure" }, |
| 658 | FfxResourceBinding{ 12, 0, L"r_luma_history" } |
| 659 | }; |
| 660 | |
| 661 | pass.storage_bindings = { |
| 662 | FfxResourceBinding{ 13, 0, L"rw_internal_upscaled_color" }, |
| 663 | FfxResourceBinding{ 14, 0, L"rw_lock_status" }, |
| 664 | FfxResourceBinding{ 15, 0, L"rw_upscaled_output" }, |
| 665 | FfxResourceBinding{ 16, 0, L"rw_new_locks" }, |
| 666 | FfxResourceBinding{ 17, 0, L"rw_luma_history" } |
| 667 | }; |
| 668 | |
| 669 | pass.uniform_bindings = { |
| 670 | FfxResourceBinding{ 18, 0, L"cbFSR2" } |
| 671 | }; |
| 672 | |
| 673 | // Sharpen pass is a clone of the accumulate pass with the sharpening variant. |
| 674 | Pass &sharpen_pass = device.passes[FFX_FSR2_PASS_ACCUMULATE_SHARPEN]; |
| 675 | sharpen_pass = pass; |
| 676 | sharpen_pass.shader_variant = pass.shader_variant + 1; |
| 677 | } |
| 678 | |
| 679 | { |
| 680 | Pass &pass = device.passes[FFX_FSR2_PASS_RCAS]; |
| 681 | pass.shader = &shaders.rcas; |
| 682 | pass.shader->initialize(modes_single, general_defines); |
| 683 | pass.shader_version = pass.shader->version_create(); |
| 684 | |
| 685 | pass.sampled_bindings = { |
| 686 | FfxResourceBinding{ 0, 0, L"r_input_exposure" }, |
| 687 | FfxResourceBinding{ 1, 0, L"r_rcas_input" } |
| 688 | }; |
| 689 | |
| 690 | pass.storage_bindings = { |
| 691 | FfxResourceBinding{ 2, 0, L"rw_upscaled_output" } |
| 692 | }; |
| 693 | |
| 694 | pass.uniform_bindings = { |
| 695 | FfxResourceBinding{ 3, 0, L"cbFSR2" }, |
| 696 | FfxResourceBinding{ 4, 0, L"cbRCAS" } |
| 697 | }; |
| 698 | } |
| 699 | |
| 700 | { |
| 701 | Pass &pass = device.passes[FFX_FSR2_PASS_COMPUTE_LUMINANCE_PYRAMID]; |
| 702 | pass.shader = &shaders.compute_luminance_pyramid; |
| 703 | pass.shader->initialize(modes_single, general_defines); |
| 704 | pass.shader_version = pass.shader->version_create(); |
| 705 | |
| 706 | pass.sampled_bindings = { |
| 707 | FfxResourceBinding{ 0, 0, L"r_input_color_jittered" } |
| 708 | }; |
| 709 | |
| 710 | pass.storage_bindings = { |
| 711 | FfxResourceBinding{ 1, 0, L"rw_spd_global_atomic" }, |
| 712 | FfxResourceBinding{ 2, 0, L"rw_img_mip_shading_change" }, |
| 713 | FfxResourceBinding{ 3, 0, L"rw_img_mip_5" }, |
| 714 | FfxResourceBinding{ 4, 0, L"rw_auto_exposure" } |
| 715 | }; |
| 716 | |
| 717 | pass.uniform_bindings = { |
| 718 | FfxResourceBinding{ 5, 0, L"cbFSR2" }, |
| 719 | FfxResourceBinding{ 6, 0, L"cbSPD" } |
| 720 | }; |
| 721 | } |
| 722 | |
| 723 | { |
| 724 | Pass &pass = device.passes[FFX_FSR2_PASS_GENERATE_REACTIVE]; |
| 725 | pass.shader = &shaders.autogen_reactive; |
| 726 | pass.shader->initialize(modes_with_fp16, general_defines); |
| 727 | pass.shader_version = pass.shader->version_create(); |
| 728 | pass.shader_variant = capabilities.fp16Supported ? 1 : 0; |
| 729 | |
| 730 | pass.sampled_bindings = { |
| 731 | FfxResourceBinding{ 0, 0, L"r_input_opaque_only" }, |
| 732 | FfxResourceBinding{ 1, 0, L"r_input_color_jittered" } |
| 733 | }; |
| 734 | |
| 735 | pass.storage_bindings = { |
| 736 | FfxResourceBinding{ 2, 0, L"rw_output_autoreactive" } |
| 737 | }; |
| 738 | |
| 739 | pass.uniform_bindings = { |
| 740 | FfxResourceBinding{ 3, 0, L"cbGenerateReactive" }, |
| 741 | FfxResourceBinding{ 4, 0, L"cbFSR2" } |
| 742 | }; |
| 743 | } |
| 744 | |
| 745 | { |
| 746 | Pass &pass = device.passes[FFX_FSR2_PASS_TCR_AUTOGENERATE]; |
| 747 | pass.shader = &shaders.tcr_autogen; |
| 748 | pass.shader->initialize(modes_with_fp16, general_defines); |
| 749 | pass.shader_version = pass.shader->version_create(); |
| 750 | pass.shader_variant = capabilities.fp16Supported ? 1 : 0; |
| 751 | |
| 752 | pass.sampled_bindings = { |
| 753 | FfxResourceBinding{ 0, 0, L"r_input_opaque_only" }, |
| 754 | FfxResourceBinding{ 1, 0, L"r_input_color_jittered" }, |
| 755 | FfxResourceBinding{ 2, 0, L"r_input_motion_vectors" }, |
| 756 | FfxResourceBinding{ 3, 0, L"r_input_prev_color_pre_alpha" }, |
| 757 | FfxResourceBinding{ 4, 0, L"r_input_prev_color_post_alpha" }, |
| 758 | FfxResourceBinding{ 5, 0, L"r_reactive_mask" }, |
| 759 | FfxResourceBinding{ 6, 0, L"r_transparency_and_composition_mask" }, |
| 760 | FfxResourceBinding{ 13, 0, L"r_input_depth" } |
| 761 | }; |
| 762 | |
| 763 | pass.storage_bindings = { |
| 764 | FfxResourceBinding{ 7, 0, L"rw_output_autoreactive" }, |
| 765 | FfxResourceBinding{ 8, 0, L"rw_output_autocomposition" }, |
| 766 | FfxResourceBinding{ 9, 0, L"rw_output_prev_color_pre_alpha" }, |
| 767 | FfxResourceBinding{ 10, 0, L"rw_output_prev_color_post_alpha" } |
| 768 | }; |
| 769 | |
| 770 | pass.uniform_bindings = { |
| 771 | FfxResourceBinding{ 11, 0, L"cbFSR2" }, |
| 772 | FfxResourceBinding{ 12, 0, L"cbGenerateReactive" } |
| 773 | }; |
| 774 | } |
| 775 | |
| 776 | RD::SamplerState state; |
| 777 | state.mag_filter = RD::SAMPLER_FILTER_NEAREST; |
| 778 | state.min_filter = RD::SAMPLER_FILTER_NEAREST; |
| 779 | state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE; |
| 780 | state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE; |
| 781 | state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE; |
| 782 | state.min_lod = -1000.0f; |
| 783 | state.max_lod = 1000.0f; |
| 784 | state.anisotropy_max = 1.0; |
| 785 | device.point_clamp_sampler = RD::get_singleton()->sampler_create(state); |
| 786 | ERR_FAIL_COND(device.point_clamp_sampler.is_null()); |
| 787 | |
| 788 | state.mag_filter = RD::SAMPLER_FILTER_LINEAR; |
| 789 | state.min_filter = RD::SAMPLER_FILTER_LINEAR; |
| 790 | device.linear_clamp_sampler = RD::get_singleton()->sampler_create(state); |
| 791 | ERR_FAIL_COND(device.linear_clamp_sampler.is_null()); |
| 792 | } |
| 793 | |
| 794 | FSR2Effect::~FSR2Effect() { |
| 795 | RD::get_singleton()->free_rid(device.point_clamp_sampler); |
| 796 | RD::get_singleton()->free_rid(device.linear_clamp_sampler); |
| 797 | |
| 798 | for (uint32_t i = 0; i < FFX_FSR2_PASS_COUNT; i++) { |
| 799 | device.passes[i].shader->version_free(device.passes[i].shader_version); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | FSR2Context *FSR2Effect::create_context(Size2i p_internal_size, Size2i p_target_size) { |
| 804 | FSR2Context *context = memnew(RendererRD::FSR2Context); |
| 805 | context->fsr_desc.flags = FFX_FSR2_ENABLE_HIGH_DYNAMIC_RANGE | FFX_FSR2_ENABLE_DEPTH_INVERTED; |
| 806 | context->fsr_desc.maxRenderSize.width = p_internal_size.x; |
| 807 | context->fsr_desc.maxRenderSize.height = p_internal_size.y; |
| 808 | context->fsr_desc.displaySize.width = p_target_size.x; |
| 809 | context->fsr_desc.displaySize.height = p_target_size.y; |
| 810 | context->fsr_desc.device = &device; |
| 811 | |
| 812 | FfxFsr2Interface &functions = context->fsr_desc.callbacks; |
| 813 | functions.fpCreateBackendContext = create_backend_context_rd; |
| 814 | functions.fpGetDeviceCapabilities = get_device_capabilities_rd; |
| 815 | functions.fpDestroyBackendContext = destroy_backend_context_rd; |
| 816 | functions.fpCreateResource = create_resource_rd; |
| 817 | functions.fpRegisterResource = register_resource_rd; |
| 818 | functions.fpUnregisterResources = unregister_resources_rd; |
| 819 | functions.fpGetResourceDescription = get_resource_description_rd; |
| 820 | functions.fpDestroyResource = destroy_resource_rd; |
| 821 | functions.fpCreatePipeline = create_pipeline_rd; |
| 822 | functions.fpDestroyPipeline = destroy_pipeline_rd; |
| 823 | functions.fpScheduleGpuJob = schedule_gpu_job_rd; |
| 824 | functions.fpExecuteGpuJobs = execute_gpu_jobs_rd; |
| 825 | functions.scratchBuffer = &context->scratch; |
| 826 | functions.scratchBufferSize = sizeof(context->scratch); |
| 827 | |
| 828 | FfxErrorCode result = ffxFsr2ContextCreate(&context->fsr_context, &context->fsr_desc); |
| 829 | if (result == FFX_OK) { |
| 830 | return context; |
| 831 | } else { |
| 832 | memdelete(context); |
| 833 | return nullptr; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | void FSR2Effect::upscale(const Parameters &p_params) { |
| 838 | // TODO: Transparency & Composition mask is not implemented. |
| 839 | FfxFsr2DispatchDescription dispatch_desc = {}; |
| 840 | RID color = p_params.color; |
| 841 | RID depth = p_params.depth; |
| 842 | RID velocity = p_params.velocity; |
| 843 | RID reactive = p_params.reactive; |
| 844 | RID exposure = p_params.exposure; |
| 845 | RID output = p_params.output; |
| 846 | dispatch_desc.commandList = nullptr; |
| 847 | dispatch_desc.color = get_resource_rd(&color, L"color"); |
| 848 | dispatch_desc.depth = get_resource_rd(&depth, L"depth"); |
| 849 | dispatch_desc.motionVectors = get_resource_rd(&velocity, L"velocity"); |
| 850 | dispatch_desc.reactive = get_resource_rd(&reactive, L"reactive"); |
| 851 | dispatch_desc.exposure = get_resource_rd(&exposure, L"exposure"); |
| 852 | dispatch_desc.transparencyAndComposition = {}; |
| 853 | dispatch_desc.output = get_resource_rd(&output, L"output"); |
| 854 | dispatch_desc.colorOpaqueOnly = {}; |
| 855 | dispatch_desc.jitterOffset.x = p_params.jitter.x; |
| 856 | dispatch_desc.jitterOffset.y = p_params.jitter.y; |
| 857 | dispatch_desc.motionVectorScale.x = float(p_params.internal_size.width); |
| 858 | dispatch_desc.motionVectorScale.y = float(p_params.internal_size.height); |
| 859 | dispatch_desc.reset = p_params.reset_accumulation; |
| 860 | dispatch_desc.renderSize.width = p_params.internal_size.width; |
| 861 | dispatch_desc.renderSize.height = p_params.internal_size.height; |
| 862 | dispatch_desc.enableSharpening = (p_params.sharpness > 1e-6f); |
| 863 | dispatch_desc.sharpness = p_params.sharpness; |
| 864 | dispatch_desc.frameTimeDelta = p_params.delta_time; |
| 865 | dispatch_desc.preExposure = 1.0f; |
| 866 | dispatch_desc.cameraNear = p_params.z_near; |
| 867 | dispatch_desc.cameraFar = p_params.z_far; |
| 868 | dispatch_desc.cameraFovAngleVertical = p_params.fovy; |
| 869 | dispatch_desc.viewSpaceToMetersFactor = 1.0f; |
| 870 | dispatch_desc.enableAutoReactive = false; |
| 871 | dispatch_desc.autoTcThreshold = 1.0f; |
| 872 | dispatch_desc.autoTcScale = 1.0f; |
| 873 | dispatch_desc.autoReactiveScale = 1.0f; |
| 874 | dispatch_desc.autoReactiveMax = 1.0f; |
| 875 | |
| 876 | RendererRD::MaterialStorage::store_camera(p_params.reprojection, dispatch_desc.reprojectionMatrix); |
| 877 | |
| 878 | FfxErrorCode result = ffxFsr2ContextDispatch(&p_params.context->fsr_context, &dispatch_desc); |
| 879 | ERR_FAIL_COND(result != FFX_OK); |
| 880 | } |