metal_fx.h 5.6 KB

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