metal_fx.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**************************************************************************/
  2. /* metal_fx.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. #pragma once
  31. #if defined(METAL_ENABLED) && !defined(VISIONOS_ENABLED)
  32. #define METAL_MFXTEMPORAL_ENABLED
  33. #endif
  34. #ifdef METAL_ENABLED
  35. #include "spatial_upscaler.h"
  36. #include "core/templates/paged_allocator.h"
  37. #include "servers/rendering/renderer_scene_render.h"
  38. #ifdef __OBJC__
  39. @protocol MTLFXSpatialScaler;
  40. @protocol MTLFXTemporalScaler;
  41. #endif
  42. namespace RendererRD {
  43. struct MFXSpatialContext {
  44. #ifdef __OBJC__
  45. id<MTLFXSpatialScaler> scaler = nullptr;
  46. #else
  47. void *scaler = nullptr;
  48. #endif
  49. MFXSpatialContext() = default;
  50. ~MFXSpatialContext();
  51. };
  52. class MFXSpatialEffect : public SpatialUpscaler {
  53. struct CallbackArgs {
  54. MFXSpatialEffect *owner;
  55. RDD::TextureID src;
  56. RDD::TextureID dst;
  57. MFXSpatialContext ctx;
  58. CallbackArgs(MFXSpatialEffect *p_owner, RDD::TextureID p_src, RDD::TextureID p_dst, MFXSpatialContext p_ctx) :
  59. owner(p_owner), src(p_src), dst(p_dst), ctx(p_ctx) {}
  60. static void free(CallbackArgs **p_args) {
  61. (*p_args)->owner->args_allocator.free(*p_args);
  62. *p_args = nullptr;
  63. }
  64. };
  65. PagedAllocator<CallbackArgs, true, 16> args_allocator;
  66. static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
  67. String name = "MetalFX Spatial Upscale";
  68. public:
  69. virtual String get_label() const final { return name; }
  70. virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) final;
  71. virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) final;
  72. struct CreateParams {
  73. Vector2i input_size;
  74. Vector2i output_size;
  75. RD::DataFormat input_format;
  76. RD::DataFormat output_format;
  77. };
  78. MFXSpatialContext *create_context(CreateParams p_params) const;
  79. MFXSpatialEffect();
  80. ~MFXSpatialEffect();
  81. };
  82. #ifdef METAL_MFXTEMPORAL_ENABLED
  83. struct MFXTemporalContext {
  84. #ifdef __OBJC__
  85. id<MTLFXTemporalScaler> scaler = nullptr;
  86. #else
  87. void *scaler = nullptr;
  88. #endif
  89. MFXTemporalContext() = default;
  90. ~MFXTemporalContext();
  91. };
  92. class MFXTemporalEffect {
  93. struct CallbackArgs {
  94. MFXTemporalEffect *owner;
  95. RDD::TextureID src;
  96. RDD::TextureID depth;
  97. RDD::TextureID motion;
  98. RDD::TextureID exposure;
  99. Vector2 jitter_offset;
  100. RDD::TextureID dst;
  101. MFXTemporalContext ctx;
  102. bool reset = false;
  103. CallbackArgs(
  104. MFXTemporalEffect *p_owner,
  105. RDD::TextureID p_src,
  106. RDD::TextureID p_depth,
  107. RDD::TextureID p_motion,
  108. RDD::TextureID p_exposure,
  109. Vector2 p_jitter_offset,
  110. RDD::TextureID p_dst,
  111. MFXTemporalContext p_ctx,
  112. bool p_reset) :
  113. owner(p_owner),
  114. src(p_src),
  115. depth(p_depth),
  116. motion(p_motion),
  117. exposure(p_exposure),
  118. jitter_offset(p_jitter_offset),
  119. dst(p_dst),
  120. ctx(p_ctx),
  121. reset(p_reset) {}
  122. static void free(CallbackArgs **p_args) {
  123. (*p_args)->owner->args_allocator.free(*p_args);
  124. *p_args = nullptr;
  125. }
  126. };
  127. PagedAllocator<CallbackArgs, true, 16> args_allocator;
  128. static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
  129. public:
  130. MFXTemporalEffect();
  131. ~MFXTemporalEffect();
  132. struct CreateParams {
  133. Vector2i input_size;
  134. Vector2i output_size;
  135. RD::DataFormat input_format;
  136. RD::DataFormat depth_format;
  137. RD::DataFormat motion_format;
  138. RD::DataFormat reactive_format;
  139. RD::DataFormat output_format;
  140. Vector2 motion_vector_scale;
  141. };
  142. MFXTemporalContext *create_context(CreateParams p_params) const;
  143. struct Params {
  144. RID src;
  145. RID depth;
  146. RID motion;
  147. RID exposure;
  148. RID dst;
  149. Vector2 jitter_offset;
  150. bool reset = false;
  151. };
  152. void process(MFXTemporalContext *p_ctx, Params p_params);
  153. };
  154. #endif
  155. } //namespace RendererRD
  156. #endif // METAL_ENABLED