ffx_fsr2_interface.h 19 KB

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