Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/effects/metal_fx.cpp
20971 views
1
/**************************************************************************/
2
/* metal_fx.cpp */
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
#ifdef METAL_ENABLED
32
33
#include "metal_fx.h"
34
35
#include "../storage_rd/render_scene_buffers_rd.h"
36
#include "drivers/metal/pixel_formats.h"
37
#include "drivers/metal/rendering_device_driver_metal3.h"
38
39
#include <MetalFX/MetalFX.hpp>
40
41
using namespace RendererRD;
42
43
#pragma mark - Spatial Scaler
44
45
MFXSpatialContext::~MFXSpatialContext() {
46
if (scaler) {
47
scaler->release();
48
}
49
}
50
51
MFXSpatialEffect::MFXSpatialEffect() {
52
}
53
54
MFXSpatialEffect::~MFXSpatialEffect() {
55
}
56
57
void MFXSpatialEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {
58
MDCommandBufferBase *obj = (MDCommandBufferBase *)(p_command_buffer.id);
59
obj->end();
60
61
MTL::Texture *src_texture = reinterpret_cast<MTL::Texture *>(p_userdata->src.id);
62
MTL::Texture *dst_texture = reinterpret_cast<MTL::Texture *>(p_userdata->dst.id);
63
64
MTLFX::SpatialScalerBase *scaler = p_userdata->scaler;
65
scaler->setColorTexture(src_texture);
66
scaler->setOutputTexture(dst_texture);
67
MTLFX::SpatialScaler *s = static_cast<MTLFX::SpatialScaler *>(scaler);
68
MTL3::MDCommandBuffer *cmd = (MTL3::MDCommandBuffer *)(p_command_buffer.id);
69
s->encodeToCommandBuffer(cmd->get_command_buffer());
70
obj->retain_resource(scaler);
71
72
CallbackArgs::free(&p_userdata);
73
}
74
75
void MFXSpatialEffect::ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) {
76
p_render_buffers->ensure_mfx(this);
77
}
78
79
void MFXSpatialEffect::process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) {
80
MFXSpatialContext *ctx = p_render_buffers->get_mfx_spatial_context();
81
DEV_ASSERT(ctx); // this should have been done by the caller via ensure_context
82
83
CallbackArgs *userdata = args_allocator.alloc(
84
this,
85
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_src)),
86
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_dst)),
87
*ctx);
88
RD::CallbackResource res[2] = {
89
{ .rid = p_src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
90
{ .rid = p_dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE }
91
};
92
RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXSpatialEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 2));
93
}
94
95
MFXSpatialContext *MFXSpatialEffect::create_context(CreateParams p_params) const {
96
DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_SPATIAL));
97
98
RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();
99
PixelFormats &pf = rdd->get_pixel_formats();
100
MTL::Device *dev = rdd->get_device();
101
102
NS::SharedPtr<MTLFX::SpatialScalerDescriptor> desc = NS::TransferPtr(MTLFX::SpatialScalerDescriptor::alloc()->init());
103
desc->setInputWidth((NS::UInteger)p_params.input_size.width);
104
desc->setInputHeight((NS::UInteger)p_params.input_size.height);
105
106
desc->setOutputWidth((NS::UInteger)p_params.output_size.width);
107
desc->setOutputHeight((NS::UInteger)p_params.output_size.height);
108
109
desc->setColorTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.input_format));
110
desc->setOutputTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.output_format));
111
desc->setColorProcessingMode(MTLFX::SpatialScalerColorProcessingModeLinear);
112
113
MFXSpatialContext *context = memnew(MFXSpatialContext);
114
context->scaler = desc->newSpatialScaler(dev);
115
116
return context;
117
}
118
119
#ifdef METAL_MFXTEMPORAL_ENABLED
120
121
#pragma mark - Temporal Scaler
122
123
MFXTemporalContext::~MFXTemporalContext() {
124
if (scaler) {
125
scaler->release();
126
}
127
}
128
129
MFXTemporalEffect::MFXTemporalEffect() {}
130
MFXTemporalEffect::~MFXTemporalEffect() {}
131
132
MFXTemporalContext *MFXTemporalEffect::create_context(CreateParams p_params) const {
133
DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_TEMPORAL));
134
135
RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();
136
PixelFormats &pf = rdd->get_pixel_formats();
137
MTL::Device *dev = rdd->get_device();
138
139
NS::SharedPtr<MTLFX::TemporalScalerDescriptor> desc = NS::TransferPtr(MTLFX::TemporalScalerDescriptor::alloc()->init());
140
desc->setInputWidth((NS::UInteger)p_params.input_size.width);
141
desc->setInputHeight((NS::UInteger)p_params.input_size.height);
142
143
desc->setOutputWidth((NS::UInteger)p_params.output_size.width);
144
desc->setOutputHeight((NS::UInteger)p_params.output_size.height);
145
146
desc->setColorTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.input_format));
147
desc->setDepthTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.depth_format));
148
desc->setMotionTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.motion_format));
149
desc->setAutoExposureEnabled(false);
150
151
desc->setOutputTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.output_format));
152
153
MFXTemporalContext *context = memnew(MFXTemporalContext);
154
context->scaler = desc->newTemporalScaler(dev);
155
context->scaler->setMotionVectorScaleX(p_params.motion_vector_scale.x);
156
context->scaler->setMotionVectorScaleY(p_params.motion_vector_scale.y);
157
context->scaler->setDepthReversed(true); // Godot uses reverse Z per https://github.com/godotengine/godot/pull/88328
158
159
return context;
160
}
161
162
void MFXTemporalEffect::process(RendererRD::MFXTemporalContext *p_ctx, RendererRD::MFXTemporalEffect::Params p_params) {
163
CallbackArgs *userdata = args_allocator.alloc(
164
this,
165
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.src)),
166
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.depth)),
167
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.motion)),
168
p_params.exposure.is_valid() ? RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.exposure)) : RDD::TextureID(),
169
p_params.jitter_offset,
170
RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.dst)),
171
*p_ctx,
172
p_params.reset);
173
RD::CallbackResource res[3] = {
174
{ .rid = p_params.src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
175
{ .rid = p_params.depth, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },
176
{ .rid = p_params.dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE },
177
};
178
RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXTemporalEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 3));
179
}
180
181
void MFXTemporalEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {
182
MDCommandBufferBase *obj = (MDCommandBufferBase *)(p_command_buffer.id);
183
obj->end();
184
185
MTL::Texture *src_texture = reinterpret_cast<MTL::Texture *>(p_userdata->src.id);
186
MTL::Texture *depth = reinterpret_cast<MTL::Texture *>(p_userdata->depth.id);
187
MTL::Texture *motion = reinterpret_cast<MTL::Texture *>(p_userdata->motion.id);
188
MTL::Texture *exposure = reinterpret_cast<MTL::Texture *>(p_userdata->exposure.id);
189
190
MTL::Texture *dst_texture = reinterpret_cast<MTL::Texture *>(p_userdata->dst.id);
191
192
MTLFX::TemporalScalerBase *scaler = p_userdata->scaler;
193
scaler->setReset(p_userdata->reset);
194
scaler->setColorTexture(src_texture);
195
scaler->setDepthTexture(depth);
196
scaler->setMotionTexture(motion);
197
scaler->setExposureTexture(exposure);
198
scaler->setJitterOffsetX(p_userdata->jitter_offset.x);
199
scaler->setJitterOffsetY(p_userdata->jitter_offset.y);
200
scaler->setOutputTexture(dst_texture);
201
MTLFX::TemporalScaler *s = static_cast<MTLFX::TemporalScaler *>(scaler);
202
MTL3::MDCommandBuffer *cmd = (MTL3::MDCommandBuffer *)(p_command_buffer.id);
203
s->encodeToCommandBuffer(cmd->get_command_buffer());
204
obj->retain_resource(scaler);
205
206
CallbackArgs::free(&p_userdata);
207
}
208
209
#endif
210
211
#endif
212
213