Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/effects/metal_fx.h
10279 views
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
31
#pragma once
32
33
#if defined(METAL_ENABLED) && !defined(VISIONOS_ENABLED)
34
#define METAL_MFXTEMPORAL_ENABLED
35
#endif
36
37
#ifdef METAL_ENABLED
38
39
#include "spatial_upscaler.h"
40
41
#include "core/templates/paged_allocator.h"
42
#include "servers/rendering/renderer_scene_render.h"
43
44
#ifdef __OBJC__
45
@protocol MTLFXSpatialScaler;
46
@protocol MTLFXTemporalScaler;
47
#endif
48
49
namespace RendererRD {
50
51
struct MFXSpatialContext {
52
#ifdef __OBJC__
53
id<MTLFXSpatialScaler> scaler = nullptr;
54
#else
55
void *scaler = nullptr;
56
#endif
57
MFXSpatialContext() = default;
58
~MFXSpatialContext();
59
};
60
61
class MFXSpatialEffect : public SpatialUpscaler {
62
struct CallbackArgs {
63
MFXSpatialEffect *owner;
64
RDD::TextureID src;
65
RDD::TextureID dst;
66
MFXSpatialContext ctx;
67
68
CallbackArgs(MFXSpatialEffect *p_owner, RDD::TextureID p_src, RDD::TextureID p_dst, MFXSpatialContext p_ctx) :
69
owner(p_owner), src(p_src), dst(p_dst), ctx(p_ctx) {}
70
71
static void free(CallbackArgs **p_args) {
72
(*p_args)->owner->args_allocator.free(*p_args);
73
*p_args = nullptr;
74
}
75
};
76
77
PagedAllocator<CallbackArgs, true, 16> args_allocator;
78
static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
79
80
public:
81
virtual const Span<char> get_label() const final { return "MetalFX Spatial Upscale"; }
82
virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) final;
83
virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) final;
84
85
struct CreateParams {
86
Vector2i input_size;
87
Vector2i output_size;
88
RD::DataFormat input_format;
89
RD::DataFormat output_format;
90
};
91
92
MFXSpatialContext *create_context(CreateParams p_params) const;
93
94
MFXSpatialEffect();
95
~MFXSpatialEffect();
96
};
97
98
#ifdef METAL_MFXTEMPORAL_ENABLED
99
100
struct MFXTemporalContext {
101
#ifdef __OBJC__
102
id<MTLFXTemporalScaler> scaler = nullptr;
103
#else
104
void *scaler = nullptr;
105
#endif
106
MFXTemporalContext() = default;
107
~MFXTemporalContext();
108
};
109
110
class MFXTemporalEffect {
111
struct CallbackArgs {
112
MFXTemporalEffect *owner;
113
RDD::TextureID src;
114
RDD::TextureID depth;
115
RDD::TextureID motion;
116
RDD::TextureID exposure;
117
Vector2 jitter_offset;
118
RDD::TextureID dst;
119
MFXTemporalContext ctx;
120
bool reset = false;
121
122
CallbackArgs(
123
MFXTemporalEffect *p_owner,
124
RDD::TextureID p_src,
125
RDD::TextureID p_depth,
126
RDD::TextureID p_motion,
127
RDD::TextureID p_exposure,
128
Vector2 p_jitter_offset,
129
RDD::TextureID p_dst,
130
MFXTemporalContext p_ctx,
131
bool p_reset) :
132
owner(p_owner),
133
src(p_src),
134
depth(p_depth),
135
motion(p_motion),
136
exposure(p_exposure),
137
jitter_offset(p_jitter_offset),
138
dst(p_dst),
139
ctx(p_ctx),
140
reset(p_reset) {}
141
142
static void free(CallbackArgs **p_args) {
143
(*p_args)->owner->args_allocator.free(*p_args);
144
*p_args = nullptr;
145
}
146
};
147
148
PagedAllocator<CallbackArgs, true, 16> args_allocator;
149
150
static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
151
152
public:
153
MFXTemporalEffect();
154
~MFXTemporalEffect();
155
156
struct CreateParams {
157
Vector2i input_size;
158
Vector2i output_size;
159
RD::DataFormat input_format;
160
RD::DataFormat depth_format;
161
RD::DataFormat motion_format;
162
RD::DataFormat reactive_format;
163
RD::DataFormat output_format;
164
Vector2 motion_vector_scale;
165
};
166
167
MFXTemporalContext *create_context(CreateParams p_params) const;
168
169
struct Params {
170
RID src;
171
RID depth;
172
RID motion;
173
RID exposure;
174
RID dst;
175
Vector2 jitter_offset;
176
bool reset = false;
177
};
178
179
void process(MFXTemporalContext *p_ctx, Params p_params);
180
};
181
182
#endif
183
184
} //namespace RendererRD
185
186
#endif // METAL_ENABLED
187
188