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