fsr2.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* fsr2.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef FSR2_RD_H
  31. #define FSR2_RD_H
  32. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_accumulate_pass.glsl.gen.h"
  33. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_autogen_reactive_pass.glsl.gen.h"
  34. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_compute_luminance_pyramid_pass.glsl.gen.h"
  35. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_depth_clip_pass.glsl.gen.h"
  36. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_lock_pass.glsl.gen.h"
  37. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_rcas_pass.glsl.gen.h"
  38. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_reconstruct_previous_depth_pass.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/shaders/effects/fsr2/fsr2_tcr_autogen_pass.glsl.gen.h"
  40. // This flag doesn't actually control anything GCC specific in FSR2. It determines
  41. // if symbols should be exported, which is not required for Godot.
  42. #ifndef FFX_GCC
  43. #define FFX_GCC
  44. #endif
  45. #include "thirdparty/amd-fsr2/ffx_fsr2.h"
  46. #define FSR2_MAX_QUEUED_FRAMES (4)
  47. #define FSR2_MAX_UNIFORM_BUFFERS (4)
  48. #define FSR2_MAX_BUFFERED_DESCRIPTORS (FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES)
  49. #define FSR2_UBO_RING_BUFFER_SIZE (FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS)
  50. namespace RendererRD {
  51. class FSR2Context {
  52. public:
  53. enum ResourceID : uint32_t {
  54. RESOURCE_ID_DYNAMIC = 0xFFFFFFFF
  55. };
  56. struct Resources {
  57. LocalVector<RID> rids;
  58. LocalVector<LocalVector<RID>> mip_slice_rids;
  59. LocalVector<uint32_t> ids;
  60. LocalVector<FfxResourceDescription> descriptions;
  61. LocalVector<uint32_t> dynamic_list;
  62. LocalVector<uint32_t> free_list;
  63. uint32_t add(RID p_rid, bool p_dynamic, uint32_t p_id, FfxResourceDescription p_description) {
  64. uint32_t ret_index;
  65. if (free_list.is_empty()) {
  66. ret_index = rids.size();
  67. uint32_t new_size = ret_index + 1;
  68. rids.resize(new_size);
  69. mip_slice_rids.resize(new_size);
  70. ids.resize(new_size);
  71. descriptions.resize(new_size);
  72. } else {
  73. uint32_t end_index = free_list.size() - 1;
  74. ret_index = free_list[end_index];
  75. free_list.resize(end_index);
  76. }
  77. rids[ret_index] = p_rid;
  78. mip_slice_rids[ret_index].clear();
  79. ids[ret_index] = p_id;
  80. descriptions[ret_index] = p_description;
  81. if (p_dynamic) {
  82. dynamic_list.push_back(ret_index);
  83. }
  84. return ret_index;
  85. }
  86. void remove(uint32_t p_index) {
  87. DEV_ASSERT(p_index < rids.size());
  88. free_list.push_back(p_index);
  89. rids[p_index] = RID();
  90. mip_slice_rids[p_index].clear();
  91. ids[p_index] = 0;
  92. descriptions[p_index] = {};
  93. dynamic_list.erase(p_index);
  94. }
  95. uint32_t size() const {
  96. return rids.size();
  97. }
  98. };
  99. struct Scratch {
  100. Resources resources;
  101. LocalVector<FfxGpuJobDescription> gpu_jobs;
  102. RID ubo_ring_buffer[FSR2_UBO_RING_BUFFER_SIZE];
  103. uint32_t ubo_ring_buffer_index = 0;
  104. FfxDevice device = nullptr;
  105. };
  106. Scratch scratch;
  107. FfxFsr2Context fsr_context;
  108. FfxFsr2ContextDescription fsr_desc;
  109. ~FSR2Context();
  110. };
  111. class FSR2Effect {
  112. public:
  113. struct RootSignature {
  114. // Proxy structure to store the shader required by RD that uses the terminology used by the FSR2 API.
  115. RID shader_rid;
  116. };
  117. struct Pipeline {
  118. RID pipeline_rid;
  119. };
  120. struct Pass {
  121. ShaderRD *shader;
  122. RID shader_version;
  123. RootSignature root_signature;
  124. uint32_t shader_variant = 0;
  125. Pipeline pipeline;
  126. Vector<FfxResourceBinding> sampled_bindings;
  127. Vector<FfxResourceBinding> storage_bindings;
  128. Vector<FfxResourceBinding> uniform_bindings;
  129. };
  130. struct Device {
  131. Pass passes[FFX_FSR2_PASS_COUNT];
  132. FfxDeviceCapabilities capabilities;
  133. RID point_clamp_sampler;
  134. RID linear_clamp_sampler;
  135. };
  136. struct Parameters {
  137. FSR2Context *context;
  138. Size2i internal_size;
  139. RID color;
  140. RID depth;
  141. RID velocity;
  142. RID reactive;
  143. RID exposure;
  144. RID output;
  145. float z_near = 0.0f;
  146. float z_far = 0.0f;
  147. float fovy = 0.0f;
  148. Vector2 jitter;
  149. float delta_time = 0.0f;
  150. float sharpness = 0.0f;
  151. bool reset_accumulation = false;
  152. Projection reprojection;
  153. };
  154. FSR2Effect();
  155. ~FSR2Effect();
  156. FSR2Context *create_context(Size2i p_internal_size, Size2i p_target_size);
  157. void upscale(const Parameters &p_params);
  158. private:
  159. struct {
  160. Fsr2DepthClipPassShaderRD depth_clip;
  161. Fsr2ReconstructPreviousDepthPassShaderRD reconstruct_previous_depth;
  162. Fsr2LockPassShaderRD lock;
  163. Fsr2AccumulatePassShaderRD accumulate;
  164. Fsr2AccumulatePassShaderRD accumulate_sharpen;
  165. Fsr2RcasPassShaderRD rcas;
  166. Fsr2ComputeLuminancePyramidPassShaderRD compute_luminance_pyramid;
  167. Fsr2AutogenReactivePassShaderRD autogen_reactive;
  168. Fsr2TcrAutogenPassShaderRD tcr_autogen;
  169. } shaders;
  170. Device device;
  171. };
  172. } // namespace RendererRD
  173. #endif // FSR2_RD_H