Debian Codegraph @

servers/rendering/renderer_rd/effects/metal_fx.mm

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