metal_fx.mm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**************************************************************************/
  2. /* metal_fx.mm */
  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. #import "metal_fx.h"
  31. #import "../storage_rd/render_scene_buffers_rd.h"
  32. #import "drivers/metal/pixel_formats.h"
  33. #import "drivers/metal/rendering_device_driver_metal.h"
  34. #import <Metal/Metal.h>
  35. #import <MetalFX/MetalFX.h>
  36. using namespace RendererRD;
  37. #pragma mark - Spatial Scaler
  38. MFXSpatialContext::~MFXSpatialContext() {
  39. }
  40. MFXSpatialEffect::MFXSpatialEffect() {
  41. }
  42. MFXSpatialEffect::~MFXSpatialEffect() {
  43. }
  44. void MFXSpatialEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {
  45. #pragma clang diagnostic push
  46. #pragma clang diagnostic ignored "-Wunguarded-availability"
  47. MDCommandBuffer *obj = (MDCommandBuffer *)(p_command_buffer.id);
  48. obj->end();
  49. id<MTLTexture> src_texture = rid::get(p_userdata->src);
  50. id<MTLTexture> dst_texture = rid::get(p_userdata->dst);
  51. __block id<MTLFXSpatialScaler> scaler = p_userdata->ctx.scaler;
  52. scaler.colorTexture = src_texture;
  53. scaler.outputTexture = dst_texture;
  54. [scaler encodeToCommandBuffer:obj->get_command_buffer()];
  55. // TODO(sgc): add API to retain objects until the command buffer completes
  56. [obj->get_command_buffer() addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) {
  57. // This block retains a reference to the scaler until the command buffer.
  58. // completes.
  59. scaler = nil;
  60. }];
  61. CallbackArgs::free(&p_userdata);
  62. #pragma clang diagnostic pop
  63. }
  64. void MFXSpatialEffect::ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) {
  65. p_render_buffers->ensure_mfx(this);
  66. }
  67. void MFXSpatialEffect::process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) {
  68. MFXSpatialContext *ctx = p_render_buffers->get_mfx_spatial_context();
  69. DEV_ASSERT(ctx); // this should have been done by the caller via ensure_context
  70. CallbackArgs *userdata = args_allocator.alloc(
  71. this,
  72. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_src)),
  73. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_dst)),
  74. *ctx);
  75. RD::CallbackResource res[2] = {
  76. { .rid = p_src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
  77. { .rid = p_dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE }
  78. };
  79. RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXSpatialEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 2));
  80. }
  81. MFXSpatialContext *MFXSpatialEffect::create_context(CreateParams p_params) const {
  82. DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_SPATIAL));
  83. #pragma clang diagnostic push
  84. #pragma clang diagnostic ignored "-Wunguarded-availability"
  85. RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();
  86. PixelFormats &pf = rdd->get_pixel_formats();
  87. id<MTLDevice> dev = rdd->get_device();
  88. MTLFXSpatialScalerDescriptor *desc = [MTLFXSpatialScalerDescriptor new];
  89. desc.inputWidth = (NSUInteger)p_params.input_size.width;
  90. desc.inputHeight = (NSUInteger)p_params.input_size.height;
  91. desc.outputWidth = (NSUInteger)p_params.output_size.width;
  92. desc.outputHeight = (NSUInteger)p_params.output_size.height;
  93. desc.colorTextureFormat = pf.getMTLPixelFormat(p_params.input_format);
  94. desc.outputTextureFormat = pf.getMTLPixelFormat(p_params.output_format);
  95. desc.colorProcessingMode = MTLFXSpatialScalerColorProcessingModeLinear;
  96. id<MTLFXSpatialScaler> scaler = [desc newSpatialScalerWithDevice:dev];
  97. MFXSpatialContext *context = memnew(MFXSpatialContext);
  98. context->scaler = scaler;
  99. #pragma clang diagnostic pop
  100. return context;
  101. }
  102. #pragma mark - Temporal Scaler
  103. MFXTemporalContext::~MFXTemporalContext() {}
  104. MFXTemporalEffect::MFXTemporalEffect() {}
  105. MFXTemporalEffect::~MFXTemporalEffect() {}
  106. MFXTemporalContext *MFXTemporalEffect::create_context(CreateParams p_params) const {
  107. DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_TEMPORAL));
  108. #pragma clang diagnostic push
  109. #pragma clang diagnostic ignored "-Wunguarded-availability"
  110. RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();
  111. PixelFormats &pf = rdd->get_pixel_formats();
  112. id<MTLDevice> dev = rdd->get_device();
  113. MTLFXTemporalScalerDescriptor *desc = [MTLFXTemporalScalerDescriptor new];
  114. desc.inputWidth = (NSUInteger)p_params.input_size.width;
  115. desc.inputHeight = (NSUInteger)p_params.input_size.height;
  116. desc.outputWidth = (NSUInteger)p_params.output_size.width;
  117. desc.outputHeight = (NSUInteger)p_params.output_size.height;
  118. desc.colorTextureFormat = pf.getMTLPixelFormat(p_params.input_format);
  119. desc.depthTextureFormat = pf.getMTLPixelFormat(p_params.depth_format);
  120. desc.motionTextureFormat = pf.getMTLPixelFormat(p_params.motion_format);
  121. desc.autoExposureEnabled = NO;
  122. desc.outputTextureFormat = pf.getMTLPixelFormat(p_params.output_format);
  123. id<MTLFXTemporalScaler> scaler = [desc newTemporalScalerWithDevice:dev];
  124. MFXTemporalContext *context = memnew(MFXTemporalContext);
  125. context->scaler = scaler;
  126. scaler.motionVectorScaleX = p_params.motion_vector_scale.x;
  127. scaler.motionVectorScaleY = p_params.motion_vector_scale.y;
  128. scaler.depthReversed = true; // Godot uses reverse Z per https://github.com/godotengine/godot/pull/88328
  129. #pragma clang diagnostic pop
  130. return context;
  131. }
  132. void MFXTemporalEffect::process(RendererRD::MFXTemporalContext *p_ctx, RendererRD::MFXTemporalEffect::Params p_params) {
  133. CallbackArgs *userdata = args_allocator.alloc(
  134. this,
  135. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.src)),
  136. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.depth)),
  137. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.motion)),
  138. p_params.exposure.is_valid() ? RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.exposure)) : RDD::TextureID(),
  139. p_params.jitter_offset,
  140. RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.dst)),
  141. *p_ctx,
  142. p_params.reset);
  143. RD::CallbackResource res[3] = {
  144. { .rid = p_params.src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
  145. { .rid = p_params.depth, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
  146. { .rid = p_params.dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE },
  147. };
  148. RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXTemporalEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 3));
  149. }
  150. void MFXTemporalEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {
  151. #pragma clang diagnostic push
  152. #pragma clang diagnostic ignored "-Wunguarded-availability"
  153. MDCommandBuffer *obj = (MDCommandBuffer *)(p_command_buffer.id);
  154. obj->end();
  155. id<MTLTexture> src_texture = rid::get(p_userdata->src);
  156. id<MTLTexture> depth = rid::get(p_userdata->depth);
  157. id<MTLTexture> motion = rid::get(p_userdata->motion);
  158. id<MTLTexture> exposure = rid::get(p_userdata->exposure);
  159. id<MTLTexture> dst_texture = rid::get(p_userdata->dst);
  160. __block id<MTLFXTemporalScaler> scaler = p_userdata->ctx.scaler;
  161. scaler.reset = p_userdata->reset;
  162. scaler.colorTexture = src_texture;
  163. scaler.depthTexture = depth;
  164. scaler.motionTexture = motion;
  165. scaler.exposureTexture = exposure;
  166. scaler.jitterOffsetX = p_userdata->jitter_offset.x;
  167. scaler.jitterOffsetY = p_userdata->jitter_offset.y;
  168. scaler.outputTexture = dst_texture;
  169. [scaler encodeToCommandBuffer:obj->get_command_buffer()];
  170. // TODO(sgc): add API to retain objects until the command buffer completes
  171. [obj->get_command_buffer() addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) {
  172. // This block retains a reference to the scaler until the command buffer.
  173. // completes.
  174. scaler = nil;
  175. }];
  176. CallbackArgs::free(&p_userdata);
  177. #pragma clang diagnostic pop
  178. }