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
22517 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
#include "servers/rendering/rendering_device_driver.h"
44
45
namespace MTLFX {
46
class SpatialScalerBase;
47
class TemporalScalerBase;
48
} //namespace MTLFX
49
50
namespace RendererRD {
51
52
struct MFXSpatialContext {
53
MTLFX::SpatialScalerBase *scaler = nullptr;
54
MFXSpatialContext() = default;
55
~MFXSpatialContext();
56
};
57
58
class MFXSpatialEffect : public SpatialUpscaler {
59
struct CallbackArgs {
60
MFXSpatialEffect *owner = nullptr;
61
MTLFX::SpatialScalerBase *scaler = nullptr;
62
RDD::TextureID src;
63
RDD::TextureID dst;
64
65
CallbackArgs(MFXSpatialEffect *p_owner, RDD::TextureID p_src, RDD::TextureID p_dst, const MFXSpatialContext &p_ctx) :
66
owner(p_owner), scaler(p_ctx.scaler), src(p_src), dst(p_dst) {}
67
68
static void free(CallbackArgs **p_args) {
69
(*p_args)->owner->args_allocator.free(*p_args);
70
*p_args = nullptr;
71
}
72
};
73
74
PagedAllocator<CallbackArgs, true, 16> args_allocator;
75
static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
76
77
public:
78
virtual const Span<char> get_label() const final { return "MetalFX Spatial Upscale"; }
79
virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) final;
80
virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) final;
81
82
struct CreateParams {
83
Vector2i input_size;
84
Vector2i output_size;
85
RDD::DataFormat input_format;
86
RDD::DataFormat output_format;
87
};
88
89
MFXSpatialContext *create_context(CreateParams p_params) const;
90
91
MFXSpatialEffect();
92
~MFXSpatialEffect();
93
};
94
95
#ifdef METAL_MFXTEMPORAL_ENABLED
96
97
struct MFXTemporalContext {
98
MTLFX::TemporalScalerBase *scaler = nullptr;
99
MFXTemporalContext() = default;
100
~MFXTemporalContext();
101
};
102
103
class MFXTemporalEffect {
104
struct CallbackArgs {
105
MFXTemporalEffect *owner = nullptr;
106
MTLFX::TemporalScalerBase *scaler = nullptr;
107
RDD::TextureID src;
108
RDD::TextureID depth;
109
RDD::TextureID motion;
110
RDD::TextureID exposure;
111
Vector2 jitter_offset;
112
RDD::TextureID dst;
113
bool reset = false;
114
115
CallbackArgs(
116
MFXTemporalEffect *p_owner,
117
RDD::TextureID p_src,
118
RDD::TextureID p_depth,
119
RDD::TextureID p_motion,
120
RDD::TextureID p_exposure,
121
Vector2 p_jitter_offset,
122
RDD::TextureID p_dst,
123
const MFXTemporalContext &p_ctx,
124
bool p_reset) :
125
owner(p_owner),
126
scaler(p_ctx.scaler),
127
src(p_src),
128
depth(p_depth),
129
motion(p_motion),
130
exposure(p_exposure),
131
jitter_offset(p_jitter_offset),
132
dst(p_dst),
133
reset(p_reset) {}
134
135
static void free(CallbackArgs **p_args) {
136
(*p_args)->owner->args_allocator.free(*p_args);
137
*p_args = nullptr;
138
}
139
};
140
141
PagedAllocator<CallbackArgs, true, 16> args_allocator;
142
143
static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
144
145
public:
146
MFXTemporalEffect();
147
~MFXTemporalEffect();
148
149
struct CreateParams {
150
Vector2i input_size;
151
Vector2i output_size;
152
RDD::DataFormat input_format;
153
RDD::DataFormat depth_format;
154
RDD::DataFormat motion_format;
155
RDD::DataFormat reactive_format;
156
RDD::DataFormat output_format;
157
Vector2 motion_vector_scale;
158
};
159
160
MFXTemporalContext *create_context(CreateParams p_params) const;
161
162
struct Params {
163
RID src;
164
RID depth;
165
RID motion;
166
RID exposure;
167
RID dst;
168
Vector2 jitter_offset;
169
bool reset = false;
170
};
171
172
void process(MFXTemporalContext *p_ctx, Params p_params);
173
};
174
175
#endif
176
177
} //namespace RendererRD
178
179
#endif // METAL_ENABLED
180
181