| 1 | // This file is part of the FidelityFX SDK. |
| 2 | // |
| 3 | // Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved. |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | // of this software and associated documentation files (the "Software"), to deal |
| 7 | // in the Software without restriction, including without limitation the rights |
| 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | // copies of the Software, and to permit persons to whom the Software is |
| 10 | // furnished to do so, subject to the following conditions: |
| 11 | // The above copyright notice and this permission notice shall be included in |
| 12 | // all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | // THE SOFTWARE. |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #include "ffx_assert.h" |
| 25 | #include "ffx_types.h" |
| 26 | #include "ffx_error.h" |
| 27 | |
| 28 | // Include the FSR2 resources defined in the HLSL code. This shared here to avoid getting out of sync. |
| 29 | #define FFX_CPU |
| 30 | #include "shaders/ffx_fsr2_resources.h" |
| 31 | #include "shaders/ffx_fsr2_common.h" |
| 32 | |
| 33 | #if defined(__cplusplus) |
| 34 | extern "C" { |
| 35 | #endif // #if defined(__cplusplus) |
| 36 | |
| 37 | FFX_FORWARD_DECLARE(FfxFsr2Interface); |
| 38 | |
| 39 | /// An enumeration of all the passes which constitute the FSR2 algorithm. |
| 40 | /// |
| 41 | /// FSR2 is implemented as a composite of several compute passes each |
| 42 | /// computing a key part of the final result. Each call to the |
| 43 | /// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c> callback function will |
| 44 | /// correspond to a single pass included in <c><i>FfxFsr2Pass</i></c>. For a |
| 45 | /// more comprehensive description of each pass, please refer to the FSR2 |
| 46 | /// reference documentation. |
| 47 | /// |
| 48 | /// Please note in some cases e.g.: <c><i>FFX_FSR2_PASS_ACCUMULATE</i></c> |
| 49 | /// and <c><i>FFX_FSR2_PASS_ACCUMULATE_SHARPEN</i></c> either one pass or the |
| 50 | /// other will be used (they are mutually exclusive). The choice of which will |
| 51 | /// depend on the way the <c><i>FfxFsr2Context</i></c> is created and the |
| 52 | /// precise contents of <c><i>FfxFsr2DispatchParamters</i></c> each time a call |
| 53 | /// is made to <c><i>ffxFsr2ContextDispatch</i></c>. |
| 54 | /// |
| 55 | /// @ingroup FSR2 |
| 56 | typedef enum FfxFsr2Pass { |
| 57 | |
| 58 | FFX_FSR2_PASS_DEPTH_CLIP = 0, ///< A pass which performs depth clipping. |
| 59 | FFX_FSR2_PASS_RECONSTRUCT_PREVIOUS_DEPTH = 1, ///< A pass which performs reconstruction of previous frame's depth. |
| 60 | FFX_FSR2_PASS_LOCK = 2, ///< A pass which calculates pixel locks. |
| 61 | FFX_FSR2_PASS_ACCUMULATE = 3, ///< A pass which performs upscaling. |
| 62 | FFX_FSR2_PASS_ACCUMULATE_SHARPEN = 4, ///< A pass which performs upscaling when sharpening is used. |
| 63 | FFX_FSR2_PASS_RCAS = 5, ///< A pass which performs sharpening. |
| 64 | FFX_FSR2_PASS_COMPUTE_LUMINANCE_PYRAMID = 6, ///< A pass which generates the luminance mipmap chain for the current frame. |
| 65 | FFX_FSR2_PASS_GENERATE_REACTIVE = 7, ///< An optional pass to generate a reactive mask |
| 66 | FFX_FSR2_PASS_TCR_AUTOGENERATE = 8, ///< An optional pass to generate a texture-and-composition and reactive masks |
| 67 | |
| 68 | FFX_FSR2_PASS_COUNT ///< The number of passes performed by FSR2. |
| 69 | } FfxFsr2Pass; |
| 70 | |
| 71 | typedef enum FfxFsr2MsgType { |
| 72 | FFX_FSR2_MESSAGE_TYPE_ERROR = 0, |
| 73 | FFX_FSR2_MESSAGE_TYPE_WARNING = 1, |
| 74 | FFX_FSR2_MESSAGE_TYPE_COUNT |
| 75 | } FfxFsr2MsgType; |
| 76 | |
| 77 | /// Create and initialize the backend context. |
| 78 | /// |
| 79 | /// The callback function sets up the backend context for rendering. |
| 80 | /// It will create or reference the device and create required internal data structures. |
| 81 | /// |
| 82 | /// @param [in] backendInterface A pointer to the backend interface. |
| 83 | /// @param [in] device The FfxDevice obtained by ffxGetDevice(DX12/VK/...). |
| 84 | /// |
| 85 | /// @retval |
| 86 | /// FFX_OK The operation completed successfully. |
| 87 | /// @retval |
| 88 | /// Anything else The operation failed. |
| 89 | /// |
| 90 | /// @ingroup FSR2 |
| 91 | typedef FfxErrorCode (*FfxFsr2CreateBackendContextFunc)( |
| 92 | FfxFsr2Interface* backendInterface, |
| 93 | FfxDevice device); |
| 94 | |
| 95 | /// Get a list of capabilities of the device. |
| 96 | /// |
| 97 | /// When creating an <c><i>FfxFsr2Context</i></c> it is desirable for the FSR2 |
| 98 | /// core implementation to be aware of certain characteristics of the platform |
| 99 | /// that is being targetted. This is because some optimizations which FSR2 |
| 100 | /// attempts to perform are more effective on certain classes of hardware than |
| 101 | /// others, or are not supported by older hardware. In order to avoid cases |
| 102 | /// where optimizations actually have the effect of decreasing performance, or |
| 103 | /// reduce the breadth of support provided by FSR2, FSR2 queries the |
| 104 | /// capabilities of the device to make such decisions. |
| 105 | /// |
| 106 | /// For target platforms with fixed hardware support you need not implement |
| 107 | /// this callback function by querying the device, but instead may hardcore |
| 108 | /// what features are available on the platform. |
| 109 | /// |
| 110 | /// @param [in] backendInterface A pointer to the backend interface. |
| 111 | /// @param [out] outDeviceCapabilities The device capabilities structure to fill out. |
| 112 | /// @param [in] device The device to query for capabilities. |
| 113 | /// |
| 114 | /// @retval |
| 115 | /// FFX_OK The operation completed successfully. |
| 116 | /// @retval |
| 117 | /// Anything else The operation failed. |
| 118 | /// |
| 119 | /// @ingroup FSR2 |
| 120 | typedef FfxErrorCode(*FfxFsr2GetDeviceCapabilitiesFunc)( |
| 121 | FfxFsr2Interface* backendInterface, |
| 122 | FfxDeviceCapabilities* outDeviceCapabilities, |
| 123 | FfxDevice device); |
| 124 | |
| 125 | /// Destroy the backend context and dereference the device. |
| 126 | /// |
| 127 | /// This function is called when the <c><i>FfxFsr2Context</i></c> is destroyed. |
| 128 | /// |
| 129 | /// @param [in] backendInterface A pointer to the backend interface. |
| 130 | /// |
| 131 | /// @retval |
| 132 | /// FFX_OK The operation completed successfully. |
| 133 | /// @retval |
| 134 | /// Anything else The operation failed. |
| 135 | /// |
| 136 | /// @ingroup FSR2 |
| 137 | typedef FfxErrorCode(*FfxFsr2DestroyBackendContextFunc)( |
| 138 | FfxFsr2Interface* backendInterface); |
| 139 | |
| 140 | /// Create a resource. |
| 141 | /// |
| 142 | /// This callback is intended for the backend to create internal resources. |
| 143 | /// |
| 144 | /// Please note: It is also possible that the creation of resources might |
| 145 | /// itself cause additional resources to be created by simply calling the |
| 146 | /// <c><i>FfxFsr2CreateResourceFunc</i></c> function pointer again. This is |
| 147 | /// useful when handling the initial creation of resources which must be |
| 148 | /// initialized. The flow in such a case would be an initial call to create the |
| 149 | /// CPU-side resource, another to create the GPU-side resource, and then a call |
| 150 | /// to schedule a copy render job to move the data between the two. Typically |
| 151 | /// this type of function call flow is only seen during the creation of an |
| 152 | /// <c><i>FfxFsr2Context</i></c>. |
| 153 | /// |
| 154 | /// @param [in] backendInterface A pointer to the backend interface. |
| 155 | /// @param [in] createResourceDescription A pointer to a <c><i>FfxCreateResourceDescription</i></c>. |
| 156 | /// @param [out] outResource A pointer to a <c><i>FfxResource</i></c> object. |
| 157 | /// |
| 158 | /// @retval |
| 159 | /// FFX_OK The operation completed successfully. |
| 160 | /// @retval |
| 161 | /// Anything else The operation failed. |
| 162 | /// |
| 163 | /// @ingroup FSR2 |
| 164 | typedef FfxErrorCode (*FfxFsr2CreateResourceFunc)( |
| 165 | FfxFsr2Interface* backendInterface, |
| 166 | const FfxCreateResourceDescription* createResourceDescription, |
| 167 | FfxResourceInternal* outResource); |
| 168 | |
| 169 | /// Register a resource in the backend for the current frame. |
| 170 | /// |
| 171 | /// Since FSR2 and the backend are not aware how many different |
| 172 | /// resources will get passed to FSR2 over time, it's not safe |
| 173 | /// to register all resources simultaneously in the backend. |
| 174 | /// Also passed resources may not be valid after the dispatch call. |
| 175 | /// As a result it's safest to register them as FfxResourceInternal |
| 176 | /// and clear them at the end of the dispatch call. |
| 177 | /// |
| 178 | /// @param [in] backendInterface A pointer to the backend interface. |
| 179 | /// @param [in] inResource A pointer to a <c><i>FfxResource</i></c>. |
| 180 | /// @param [out] outResource A pointer to a <c><i>FfxResourceInternal</i></c> object. |
| 181 | /// |
| 182 | /// @retval |
| 183 | /// FFX_OK The operation completed successfully. |
| 184 | /// @retval |
| 185 | /// Anything else The operation failed. |
| 186 | /// |
| 187 | /// @ingroup FSR2 |
| 188 | typedef FfxErrorCode(*FfxFsr2RegisterResourceFunc)( |
| 189 | FfxFsr2Interface* backendInterface, |
| 190 | const FfxResource* inResource, |
| 191 | FfxResourceInternal* outResource); |
| 192 | |
| 193 | /// Unregister all temporary FfxResourceInternal from the backend. |
| 194 | /// |
| 195 | /// Unregister FfxResourceInternal referencing resources passed to |
| 196 | /// a function as a parameter. |
| 197 | /// |
| 198 | /// @param [in] backendInterface A pointer to the backend interface. |
| 199 | /// |
| 200 | /// @retval |
| 201 | /// FFX_OK The operation completed successfully. |
| 202 | /// @retval |
| 203 | /// Anything else The operation failed. |
| 204 | /// |
| 205 | /// @ingroup FSR2 |
| 206 | typedef FfxErrorCode(*FfxFsr2UnregisterResourcesFunc)( |
| 207 | FfxFsr2Interface* backendInterface); |
| 208 | |
| 209 | /// Retrieve a <c><i>FfxResourceDescription</i></c> matching a |
| 210 | /// <c><i>FfxResource</i></c> structure. |
| 211 | /// |
| 212 | /// @param [in] backendInterface A pointer to the backend interface. |
| 213 | /// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object. |
| 214 | /// |
| 215 | /// @returns |
| 216 | /// A description of the resource. |
| 217 | /// |
| 218 | /// @ingroup FSR2 |
| 219 | typedef FfxResourceDescription (*FfxFsr2GetResourceDescriptionFunc)( |
| 220 | FfxFsr2Interface* backendInterface, |
| 221 | FfxResourceInternal resource); |
| 222 | |
| 223 | /// Destroy a resource |
| 224 | /// |
| 225 | /// This callback is intended for the backend to release an internal resource. |
| 226 | /// |
| 227 | /// @param [in] backendInterface A pointer to the backend interface. |
| 228 | /// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object. |
| 229 | /// |
| 230 | /// @retval |
| 231 | /// FFX_OK The operation completed successfully. |
| 232 | /// @retval |
| 233 | /// Anything else The operation failed. |
| 234 | /// |
| 235 | /// @ingroup FSR2 |
| 236 | typedef FfxErrorCode (*FfxFsr2DestroyResourceFunc)( |
| 237 | FfxFsr2Interface* backendInterface, |
| 238 | FfxResourceInternal resource); |
| 239 | |
| 240 | /// Create a render pipeline. |
| 241 | /// |
| 242 | /// A rendering pipeline contains the shader as well as resource bindpoints |
| 243 | /// and samplers. |
| 244 | /// |
| 245 | /// @param [in] backendInterface A pointer to the backend interface. |
| 246 | /// @param [in] pass The identifier for the pass. |
| 247 | /// @param [in] pipelineDescription A pointer to a <c><i>FfxPipelineDescription</i></c> describing the pipeline to be created. |
| 248 | /// @param [out] outPipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be populated. |
| 249 | /// |
| 250 | /// @retval |
| 251 | /// FFX_OK The operation completed successfully. |
| 252 | /// @retval |
| 253 | /// Anything else The operation failed. |
| 254 | /// |
| 255 | /// @ingroup FSR2 |
| 256 | typedef FfxErrorCode (*FfxFsr2CreatePipelineFunc)( |
| 257 | FfxFsr2Interface* backendInterface, |
| 258 | FfxFsr2Pass pass, |
| 259 | const FfxPipelineDescription* pipelineDescription, |
| 260 | FfxPipelineState* outPipeline); |
| 261 | |
| 262 | /// Destroy a render pipeline. |
| 263 | /// |
| 264 | /// @param [in] backendInterface A pointer to the backend interface. |
| 265 | /// @param [out] pipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be released. |
| 266 | /// |
| 267 | /// @retval |
| 268 | /// FFX_OK The operation completed successfully. |
| 269 | /// @retval |
| 270 | /// Anything else The operation failed. |
| 271 | /// |
| 272 | /// @ingroup FSR2 |
| 273 | typedef FfxErrorCode (*FfxFsr2DestroyPipelineFunc)( |
| 274 | FfxFsr2Interface* backendInterface, |
| 275 | FfxPipelineState* pipeline); |
| 276 | |
| 277 | /// Schedule a render job to be executed on the next call of |
| 278 | /// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c>. |
| 279 | /// |
| 280 | /// Render jobs can perform one of three different tasks: clear, copy or |
| 281 | /// compute dispatches. |
| 282 | /// |
| 283 | /// @param [in] backendInterface A pointer to the backend interface. |
| 284 | /// @param [in] job A pointer to a <c><i>FfxGpuJobDescription</i></c> structure. |
| 285 | /// |
| 286 | /// @retval |
| 287 | /// FFX_OK The operation completed successfully. |
| 288 | /// @retval |
| 289 | /// Anything else The operation failed. |
| 290 | /// |
| 291 | /// @ingroup FSR2 |
| 292 | typedef FfxErrorCode (*FfxFsr2ScheduleGpuJobFunc)( |
| 293 | FfxFsr2Interface* backendInterface, |
| 294 | const FfxGpuJobDescription* job); |
| 295 | |
| 296 | /// Execute scheduled render jobs on the <c><i>comandList</i></c> provided. |
| 297 | /// |
| 298 | /// The recording of the graphics API commands should take place in this |
| 299 | /// callback function, the render jobs which were previously enqueued (via |
| 300 | /// callbacks made to <c><i>FfxFsr2ScheduleGpuJobFunc</i></c>) should be |
| 301 | /// processed in the order they were received. Advanced users might choose to |
| 302 | /// reorder the rendering jobs, but should do so with care to respect the |
| 303 | /// resource dependencies. |
| 304 | /// |
| 305 | /// Depending on the precise contents of <c><i>FfxFsr2DispatchDescription</i></c> a |
| 306 | /// different number of render jobs might have previously been enqueued (for |
| 307 | /// example if sharpening is toggled on and off). |
| 308 | /// |
| 309 | /// @param [in] backendInterface A pointer to the backend interface. |
| 310 | /// @param [in] commandList A pointer to a <c><i>FfxCommandList</i></c> structure. |
| 311 | /// |
| 312 | /// @retval |
| 313 | /// FFX_OK The operation completed successfully. |
| 314 | /// @retval |
| 315 | /// Anything else The operation failed. |
| 316 | /// |
| 317 | /// @ingroup FSR2 |
| 318 | typedef FfxErrorCode (*FfxFsr2ExecuteGpuJobsFunc)( |
| 319 | FfxFsr2Interface* backendInterface, |
| 320 | FfxCommandList commandList); |
| 321 | |
| 322 | /// Pass a string message |
| 323 | /// |
| 324 | /// Used for debug messages. |
| 325 | /// |
| 326 | /// @param [in] type The type of message. |
| 327 | /// @param [in] message A string message to pass. |
| 328 | /// |
| 329 | /// |
| 330 | /// @ingroup FSR2 |
| 331 | typedef void(*FfxFsr2Message)( |
| 332 | FfxFsr2MsgType type, |
| 333 | const wchar_t* message); |
| 334 | |
| 335 | /// A structure encapsulating the interface between the core implentation of |
| 336 | /// the FSR2 algorithm and any graphics API that it should ultimately call. |
| 337 | /// |
| 338 | /// This set of functions serves as an abstraction layer between FSR2 and the |
| 339 | /// API used to implement it. While FSR2 ships with backends for DirectX12 and |
| 340 | /// Vulkan, it is possible to implement your own backend for other platforms or |
| 341 | /// which sits ontop of your engine's own abstraction layer. For details on the |
| 342 | /// expectations of what each function should do you should refer the |
| 343 | /// description of the following function pointer types: |
| 344 | /// |
| 345 | /// <c><i>FfxFsr2CreateDeviceFunc</i></c> |
| 346 | /// <c><i>FfxFsr2GetDeviceCapabilitiesFunc</i></c> |
| 347 | /// <c><i>FfxFsr2DestroyDeviceFunc</i></c> |
| 348 | /// <c><i>FfxFsr2CreateResourceFunc</i></c> |
| 349 | /// <c><i>FfxFsr2GetResourceDescriptionFunc</i></c> |
| 350 | /// <c><i>FfxFsr2DestroyResourceFunc</i></c> |
| 351 | /// <c><i>FfxFsr2CreatePipelineFunc</i></c> |
| 352 | /// <c><i>FfxFsr2DestroyPipelineFunc</i></c> |
| 353 | /// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c> |
| 354 | /// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c> |
| 355 | /// |
| 356 | /// Depending on the graphics API that is abstracted by the backend, it may be |
| 357 | /// required that the backend is to some extent stateful. To ensure that |
| 358 | /// applications retain full control to manage the memory used by FSR2, the |
| 359 | /// <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields are |
| 360 | /// provided. A backend should provide a means of specifying how much scratch |
| 361 | /// memory is required for its internal implementation (e.g: via a function |
| 362 | /// or constant value). The application is that responsible for allocating that |
| 363 | /// memory and providing it when setting up the FSR2 backend. Backends provided |
| 364 | /// with FSR2 do not perform dynamic memory allocations, and instead |
| 365 | /// suballocate all memory from the scratch buffers provided. |
| 366 | /// |
| 367 | /// The <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields |
| 368 | /// should be populated according to the requirements of each backend. For |
| 369 | /// example, if using the DirectX 12 backend you should call the |
| 370 | /// <c><i>ffxFsr2GetScratchMemorySizeDX12</i></c> function. It is not required |
| 371 | /// that custom backend implementations use a scratch buffer. |
| 372 | /// |
| 373 | /// @ingroup FSR2 |
| 374 | typedef struct FfxFsr2Interface { |
| 375 | |
| 376 | FfxFsr2CreateBackendContextFunc fpCreateBackendContext; ///< A callback function to create and initialize the backend context. |
| 377 | FfxFsr2GetDeviceCapabilitiesFunc fpGetDeviceCapabilities; ///< A callback function to query device capabilites. |
| 378 | FfxFsr2DestroyBackendContextFunc fpDestroyBackendContext; ///< A callback function to destroy the backendcontext. This also dereferences the device. |
| 379 | FfxFsr2CreateResourceFunc fpCreateResource; ///< A callback function to create a resource. |
| 380 | FfxFsr2RegisterResourceFunc fpRegisterResource; ///< A callback function to register an external resource. |
| 381 | FfxFsr2UnregisterResourcesFunc fpUnregisterResources; ///< A callback function to unregister external resource. |
| 382 | FfxFsr2GetResourceDescriptionFunc fpGetResourceDescription; ///< A callback function to retrieve a resource description. |
| 383 | FfxFsr2DestroyResourceFunc fpDestroyResource; ///< A callback function to destroy a resource. |
| 384 | FfxFsr2CreatePipelineFunc fpCreatePipeline; ///< A callback function to create a render or compute pipeline. |
| 385 | FfxFsr2DestroyPipelineFunc fpDestroyPipeline; ///< A callback function to destroy a render or compute pipeline. |
| 386 | FfxFsr2ScheduleGpuJobFunc fpScheduleGpuJob; ///< A callback function to schedule a render job. |
| 387 | FfxFsr2ExecuteGpuJobsFunc fpExecuteGpuJobs; ///< A callback function to execute all queued render jobs. |
| 388 | |
| 389 | void* scratchBuffer; ///< A preallocated buffer for memory utilized internally by the backend. |
| 390 | size_t scratchBufferSize; ///< Size of the buffer pointed to by <c><i>scratchBuffer</i></c>. |
| 391 | } FfxFsr2Interface; |
| 392 | |
| 393 | #if defined(__cplusplus) |
| 394 | } |
| 395 | #endif // #if defined(__cplusplus) |