Path: blob/master/thirdparty/amd-fsr2/ffx_fsr2_interface.h
9896 views
// This file is part of the FidelityFX SDK.1//2// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.3//4// Permission is hereby granted, free of charge, to any person obtaining a copy5// of this software and associated documentation files (the "Software"), to deal6// in the Software without restriction, including without limitation the rights7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8// copies of the Software, and to permit persons to whom the Software is9// furnished to do so, subject to the following conditions:10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19// THE SOFTWARE.2021#pragma once2223#include "ffx_assert.h"24#include "ffx_types.h"25#include "ffx_error.h"2627// Include the FSR2 resources defined in the HLSL code. This shared here to avoid getting out of sync.28#define FFX_CPU29#include "shaders/ffx_fsr2_resources.h"30#include "shaders/ffx_fsr2_common.h"3132#if defined(__cplusplus)33extern "C" {34#endif // #if defined(__cplusplus)3536FFX_FORWARD_DECLARE(FfxFsr2Interface);3738/// An enumeration of all the passes which constitute the FSR2 algorithm.39///40/// FSR2 is implemented as a composite of several compute passes each41/// computing a key part of the final result. Each call to the42/// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c> callback function will43/// correspond to a single pass included in <c><i>FfxFsr2Pass</i></c>. For a44/// more comprehensive description of each pass, please refer to the FSR245/// reference documentation.46///47/// Please note in some cases e.g.: <c><i>FFX_FSR2_PASS_ACCUMULATE</i></c>48/// and <c><i>FFX_FSR2_PASS_ACCUMULATE_SHARPEN</i></c> either one pass or the49/// other will be used (they are mutually exclusive). The choice of which will50/// depend on the way the <c><i>FfxFsr2Context</i></c> is created and the51/// precise contents of <c><i>FfxFsr2DispatchParamters</i></c> each time a call52/// is made to <c><i>ffxFsr2ContextDispatch</i></c>.53///54/// @ingroup FSR255typedef enum FfxFsr2Pass {5657FFX_FSR2_PASS_DEPTH_CLIP = 0, ///< A pass which performs depth clipping.58FFX_FSR2_PASS_RECONSTRUCT_PREVIOUS_DEPTH = 1, ///< A pass which performs reconstruction of previous frame's depth.59FFX_FSR2_PASS_LOCK = 2, ///< A pass which calculates pixel locks.60FFX_FSR2_PASS_ACCUMULATE = 3, ///< A pass which performs upscaling.61FFX_FSR2_PASS_ACCUMULATE_SHARPEN = 4, ///< A pass which performs upscaling when sharpening is used.62FFX_FSR2_PASS_RCAS = 5, ///< A pass which performs sharpening.63FFX_FSR2_PASS_COMPUTE_LUMINANCE_PYRAMID = 6, ///< A pass which generates the luminance mipmap chain for the current frame.64FFX_FSR2_PASS_GENERATE_REACTIVE = 7, ///< An optional pass to generate a reactive mask65FFX_FSR2_PASS_TCR_AUTOGENERATE = 8, ///< An optional pass to generate a texture-and-composition and reactive masks6667FFX_FSR2_PASS_COUNT ///< The number of passes performed by FSR2.68} FfxFsr2Pass;6970typedef enum FfxFsr2MsgType {71FFX_FSR2_MESSAGE_TYPE_ERROR = 0,72FFX_FSR2_MESSAGE_TYPE_WARNING = 1,73FFX_FSR2_MESSAGE_TYPE_COUNT74} FfxFsr2MsgType;7576/// Create and initialize the backend context.77///78/// The callback function sets up the backend context for rendering.79/// It will create or reference the device and create required internal data structures.80///81/// @param [in] backendInterface A pointer to the backend interface.82/// @param [in] device The FfxDevice obtained by ffxGetDevice(DX12/VK/...).83///84/// @retval85/// FFX_OK The operation completed successfully.86/// @retval87/// Anything else The operation failed.88///89/// @ingroup FSR290typedef FfxErrorCode (*FfxFsr2CreateBackendContextFunc)(91FfxFsr2Interface* backendInterface,92FfxDevice device);9394/// Get a list of capabilities of the device.95///96/// When creating an <c><i>FfxFsr2Context</i></c> it is desirable for the FSR297/// core implementation to be aware of certain characteristics of the platform98/// that is being targetted. This is because some optimizations which FSR299/// attempts to perform are more effective on certain classes of hardware than100/// others, or are not supported by older hardware. In order to avoid cases101/// where optimizations actually have the effect of decreasing performance, or102/// reduce the breadth of support provided by FSR2, FSR2 queries the103/// capabilities of the device to make such decisions.104///105/// For target platforms with fixed hardware support you need not implement106/// this callback function by querying the device, but instead may hardcore107/// what features are available on the platform.108///109/// @param [in] backendInterface A pointer to the backend interface.110/// @param [out] outDeviceCapabilities The device capabilities structure to fill out.111/// @param [in] device The device to query for capabilities.112///113/// @retval114/// FFX_OK The operation completed successfully.115/// @retval116/// Anything else The operation failed.117///118/// @ingroup FSR2119typedef FfxErrorCode(*FfxFsr2GetDeviceCapabilitiesFunc)(120FfxFsr2Interface* backendInterface,121FfxDeviceCapabilities* outDeviceCapabilities,122FfxDevice device);123124/// Destroy the backend context and dereference the device.125///126/// This function is called when the <c><i>FfxFsr2Context</i></c> is destroyed.127///128/// @param [in] backendInterface A pointer to the backend interface.129///130/// @retval131/// FFX_OK The operation completed successfully.132/// @retval133/// Anything else The operation failed.134///135/// @ingroup FSR2136typedef FfxErrorCode(*FfxFsr2DestroyBackendContextFunc)(137FfxFsr2Interface* backendInterface);138139/// Create a resource.140///141/// This callback is intended for the backend to create internal resources.142///143/// Please note: It is also possible that the creation of resources might144/// itself cause additional resources to be created by simply calling the145/// <c><i>FfxFsr2CreateResourceFunc</i></c> function pointer again. This is146/// useful when handling the initial creation of resources which must be147/// initialized. The flow in such a case would be an initial call to create the148/// CPU-side resource, another to create the GPU-side resource, and then a call149/// to schedule a copy render job to move the data between the two. Typically150/// this type of function call flow is only seen during the creation of an151/// <c><i>FfxFsr2Context</i></c>.152///153/// @param [in] backendInterface A pointer to the backend interface.154/// @param [in] createResourceDescription A pointer to a <c><i>FfxCreateResourceDescription</i></c>.155/// @param [out] outResource A pointer to a <c><i>FfxResource</i></c> object.156///157/// @retval158/// FFX_OK The operation completed successfully.159/// @retval160/// Anything else The operation failed.161///162/// @ingroup FSR2163typedef FfxErrorCode (*FfxFsr2CreateResourceFunc)(164FfxFsr2Interface* backendInterface,165const FfxCreateResourceDescription* createResourceDescription,166FfxResourceInternal* outResource);167168/// Register a resource in the backend for the current frame.169///170/// Since FSR2 and the backend are not aware how many different171/// resources will get passed to FSR2 over time, it's not safe172/// to register all resources simultaneously in the backend.173/// Also passed resources may not be valid after the dispatch call.174/// As a result it's safest to register them as FfxResourceInternal175/// and clear them at the end of the dispatch call.176///177/// @param [in] backendInterface A pointer to the backend interface.178/// @param [in] inResource A pointer to a <c><i>FfxResource</i></c>.179/// @param [out] outResource A pointer to a <c><i>FfxResourceInternal</i></c> object.180///181/// @retval182/// FFX_OK The operation completed successfully.183/// @retval184/// Anything else The operation failed.185///186/// @ingroup FSR2187typedef FfxErrorCode(*FfxFsr2RegisterResourceFunc)(188FfxFsr2Interface* backendInterface,189const FfxResource* inResource,190FfxResourceInternal* outResource);191192/// Unregister all temporary FfxResourceInternal from the backend.193///194/// Unregister FfxResourceInternal referencing resources passed to195/// a function as a parameter.196///197/// @param [in] backendInterface A pointer to the backend interface.198///199/// @retval200/// FFX_OK The operation completed successfully.201/// @retval202/// Anything else The operation failed.203///204/// @ingroup FSR2205typedef FfxErrorCode(*FfxFsr2UnregisterResourcesFunc)(206FfxFsr2Interface* backendInterface);207208/// Retrieve a <c><i>FfxResourceDescription</i></c> matching a209/// <c><i>FfxResource</i></c> structure.210///211/// @param [in] backendInterface A pointer to the backend interface.212/// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object.213///214/// @returns215/// A description of the resource.216///217/// @ingroup FSR2218typedef FfxResourceDescription (*FfxFsr2GetResourceDescriptionFunc)(219FfxFsr2Interface* backendInterface,220FfxResourceInternal resource);221222/// Destroy a resource223///224/// This callback is intended for the backend to release an internal resource.225///226/// @param [in] backendInterface A pointer to the backend interface.227/// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object.228///229/// @retval230/// FFX_OK The operation completed successfully.231/// @retval232/// Anything else The operation failed.233///234/// @ingroup FSR2235typedef FfxErrorCode (*FfxFsr2DestroyResourceFunc)(236FfxFsr2Interface* backendInterface,237FfxResourceInternal resource);238239/// Create a render pipeline.240///241/// A rendering pipeline contains the shader as well as resource bindpoints242/// and samplers.243///244/// @param [in] backendInterface A pointer to the backend interface.245/// @param [in] pass The identifier for the pass.246/// @param [in] pipelineDescription A pointer to a <c><i>FfxPipelineDescription</i></c> describing the pipeline to be created.247/// @param [out] outPipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be populated.248///249/// @retval250/// FFX_OK The operation completed successfully.251/// @retval252/// Anything else The operation failed.253///254/// @ingroup FSR2255typedef FfxErrorCode (*FfxFsr2CreatePipelineFunc)(256FfxFsr2Interface* backendInterface,257FfxFsr2Pass pass,258const FfxPipelineDescription* pipelineDescription,259FfxPipelineState* outPipeline);260261/// Destroy a render pipeline.262///263/// @param [in] backendInterface A pointer to the backend interface.264/// @param [out] pipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be released.265///266/// @retval267/// FFX_OK The operation completed successfully.268/// @retval269/// Anything else The operation failed.270///271/// @ingroup FSR2272typedef FfxErrorCode (*FfxFsr2DestroyPipelineFunc)(273FfxFsr2Interface* backendInterface,274FfxPipelineState* pipeline);275276/// Schedule a render job to be executed on the next call of277/// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c>.278///279/// Render jobs can perform one of three different tasks: clear, copy or280/// compute dispatches.281///282/// @param [in] backendInterface A pointer to the backend interface.283/// @param [in] job A pointer to a <c><i>FfxGpuJobDescription</i></c> structure.284///285/// @retval286/// FFX_OK The operation completed successfully.287/// @retval288/// Anything else The operation failed.289///290/// @ingroup FSR2291typedef FfxErrorCode (*FfxFsr2ScheduleGpuJobFunc)(292FfxFsr2Interface* backendInterface,293const FfxGpuJobDescription* job);294295/// Execute scheduled render jobs on the <c><i>comandList</i></c> provided.296///297/// The recording of the graphics API commands should take place in this298/// callback function, the render jobs which were previously enqueued (via299/// callbacks made to <c><i>FfxFsr2ScheduleGpuJobFunc</i></c>) should be300/// processed in the order they were received. Advanced users might choose to301/// reorder the rendering jobs, but should do so with care to respect the302/// resource dependencies.303///304/// Depending on the precise contents of <c><i>FfxFsr2DispatchDescription</i></c> a305/// different number of render jobs might have previously been enqueued (for306/// example if sharpening is toggled on and off).307///308/// @param [in] backendInterface A pointer to the backend interface.309/// @param [in] commandList A pointer to a <c><i>FfxCommandList</i></c> structure.310///311/// @retval312/// FFX_OK The operation completed successfully.313/// @retval314/// Anything else The operation failed.315///316/// @ingroup FSR2317typedef FfxErrorCode (*FfxFsr2ExecuteGpuJobsFunc)(318FfxFsr2Interface* backendInterface,319FfxCommandList commandList);320321/// Pass a string message322///323/// Used for debug messages.324///325/// @param [in] type The type of message.326/// @param [in] message A string message to pass.327///328///329/// @ingroup FSR2330typedef void(*FfxFsr2Message)(331FfxFsr2MsgType type,332const wchar_t* message);333334/// A structure encapsulating the interface between the core implentation of335/// the FSR2 algorithm and any graphics API that it should ultimately call.336///337/// This set of functions serves as an abstraction layer between FSR2 and the338/// API used to implement it. While FSR2 ships with backends for DirectX12 and339/// Vulkan, it is possible to implement your own backend for other platforms or340/// which sits ontop of your engine's own abstraction layer. For details on the341/// expectations of what each function should do you should refer the342/// description of the following function pointer types:343///344/// <c><i>FfxFsr2CreateDeviceFunc</i></c>345/// <c><i>FfxFsr2GetDeviceCapabilitiesFunc</i></c>346/// <c><i>FfxFsr2DestroyDeviceFunc</i></c>347/// <c><i>FfxFsr2CreateResourceFunc</i></c>348/// <c><i>FfxFsr2GetResourceDescriptionFunc</i></c>349/// <c><i>FfxFsr2DestroyResourceFunc</i></c>350/// <c><i>FfxFsr2CreatePipelineFunc</i></c>351/// <c><i>FfxFsr2DestroyPipelineFunc</i></c>352/// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c>353/// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c>354///355/// Depending on the graphics API that is abstracted by the backend, it may be356/// required that the backend is to some extent stateful. To ensure that357/// applications retain full control to manage the memory used by FSR2, the358/// <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields are359/// provided. A backend should provide a means of specifying how much scratch360/// memory is required for its internal implementation (e.g: via a function361/// or constant value). The application is that responsible for allocating that362/// memory and providing it when setting up the FSR2 backend. Backends provided363/// with FSR2 do not perform dynamic memory allocations, and instead364/// suballocate all memory from the scratch buffers provided.365///366/// The <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields367/// should be populated according to the requirements of each backend. For368/// example, if using the DirectX 12 backend you should call the369/// <c><i>ffxFsr2GetScratchMemorySizeDX12</i></c> function. It is not required370/// that custom backend implementations use a scratch buffer.371///372/// @ingroup FSR2373typedef struct FfxFsr2Interface {374375FfxFsr2CreateBackendContextFunc fpCreateBackendContext; ///< A callback function to create and initialize the backend context.376FfxFsr2GetDeviceCapabilitiesFunc fpGetDeviceCapabilities; ///< A callback function to query device capabilites.377FfxFsr2DestroyBackendContextFunc fpDestroyBackendContext; ///< A callback function to destroy the backendcontext. This also dereferences the device.378FfxFsr2CreateResourceFunc fpCreateResource; ///< A callback function to create a resource.379FfxFsr2RegisterResourceFunc fpRegisterResource; ///< A callback function to register an external resource.380FfxFsr2UnregisterResourcesFunc fpUnregisterResources; ///< A callback function to unregister external resource.381FfxFsr2GetResourceDescriptionFunc fpGetResourceDescription; ///< A callback function to retrieve a resource description.382FfxFsr2DestroyResourceFunc fpDestroyResource; ///< A callback function to destroy a resource.383FfxFsr2CreatePipelineFunc fpCreatePipeline; ///< A callback function to create a render or compute pipeline.384FfxFsr2DestroyPipelineFunc fpDestroyPipeline; ///< A callback function to destroy a render or compute pipeline.385FfxFsr2ScheduleGpuJobFunc fpScheduleGpuJob; ///< A callback function to schedule a render job.386FfxFsr2ExecuteGpuJobsFunc fpExecuteGpuJobs; ///< A callback function to execute all queued render jobs.387388void* scratchBuffer; ///< A preallocated buffer for memory utilized internally by the backend.389size_t scratchBufferSize; ///< Size of the buffer pointed to by <c><i>scratchBuffer</i></c>.390} FfxFsr2Interface;391392#if defined(__cplusplus)393}394#endif // #if defined(__cplusplus)395396397