Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/metal_objects_shared.h
12197 views
1
/**************************************************************************/
2
/* metal_objects_shared.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
#import "metal_device_properties.h"
34
#import "metal_utils.h"
35
36
using RDC = RenderingDeviceCommons;
37
38
// These types can be used in Vector and other containers that use
39
// pointer operations not supported by ARC.
40
namespace MTL {
41
#define MTL_CLASS(name) \
42
class name { \
43
public: \
44
name(id<MTL##name> obj = nil) : m_obj(obj) {} \
45
operator id<MTL##name>() const { \
46
return m_obj; \
47
} \
48
id<MTL##name> m_obj; \
49
};
50
51
MTL_CLASS(Texture)
52
53
} //namespace MTL
54
55
enum ShaderStageUsage : uint32_t {
56
None = 0,
57
Vertex = RDD::SHADER_STAGE_VERTEX_BIT,
58
Fragment = RDD::SHADER_STAGE_FRAGMENT_BIT,
59
TesselationControl = RDD::SHADER_STAGE_TESSELATION_CONTROL_BIT,
60
TesselationEvaluation = RDD::SHADER_STAGE_TESSELATION_EVALUATION_BIT,
61
Compute = RDD::SHADER_STAGE_COMPUTE_BIT,
62
};
63
64
_FORCE_INLINE_ ShaderStageUsage &operator|=(ShaderStageUsage &p_a, int p_b) {
65
p_a = ShaderStageUsage(uint32_t(p_a) | uint32_t(p_b));
66
return p_a;
67
}
68
69
struct ClearAttKey {
70
const static uint32_t COLOR_COUNT = MAX_COLOR_ATTACHMENT_COUNT;
71
const static uint32_t DEPTH_INDEX = COLOR_COUNT;
72
const static uint32_t STENCIL_INDEX = DEPTH_INDEX + 1;
73
const static uint32_t ATTACHMENT_COUNT = STENCIL_INDEX + 1;
74
75
enum Flags : uint16_t {
76
CLEAR_FLAGS_NONE = 0,
77
CLEAR_FLAGS_LAYERED = 1 << 0,
78
};
79
80
Flags flags = CLEAR_FLAGS_NONE;
81
uint16_t sample_count = 0;
82
uint16_t pixel_formats[ATTACHMENT_COUNT] = { 0 };
83
84
_FORCE_INLINE_ void set_color_format(uint32_t p_idx, MTLPixelFormat p_fmt) { pixel_formats[p_idx] = p_fmt; }
85
_FORCE_INLINE_ void set_depth_format(MTLPixelFormat p_fmt) { pixel_formats[DEPTH_INDEX] = p_fmt; }
86
_FORCE_INLINE_ void set_stencil_format(MTLPixelFormat p_fmt) { pixel_formats[STENCIL_INDEX] = p_fmt; }
87
_FORCE_INLINE_ MTLPixelFormat depth_format() const { return (MTLPixelFormat)pixel_formats[DEPTH_INDEX]; }
88
_FORCE_INLINE_ MTLPixelFormat stencil_format() const { return (MTLPixelFormat)pixel_formats[STENCIL_INDEX]; }
89
_FORCE_INLINE_ void enable_layered_rendering() { flags::set(flags, CLEAR_FLAGS_LAYERED); }
90
91
_FORCE_INLINE_ bool is_enabled(uint32_t p_idx) const { return pixel_formats[p_idx] != 0; }
92
_FORCE_INLINE_ bool is_depth_enabled() const { return pixel_formats[DEPTH_INDEX] != 0; }
93
_FORCE_INLINE_ bool is_stencil_enabled() const { return pixel_formats[STENCIL_INDEX] != 0; }
94
_FORCE_INLINE_ bool is_layered_rendering_enabled() const { return flags::any(flags, CLEAR_FLAGS_LAYERED); }
95
96
_FORCE_INLINE_ bool operator==(const ClearAttKey &p_rhs) const {
97
return memcmp(this, &p_rhs, sizeof(ClearAttKey)) == 0;
98
}
99
100
uint32_t hash() const {
101
uint32_t h = hash_murmur3_one_32(flags);
102
h = hash_murmur3_one_32(sample_count, h);
103
h = hash_murmur3_buffer(pixel_formats, ATTACHMENT_COUNT * sizeof(pixel_formats[0]), h);
104
return hash_fmix32(h);
105
}
106
};
107
108
/**
109
* Returns an index that can be used to map a shader stage to an index in a fixed-size array that is used for
110
* a single pipeline type.
111
*/
112
_FORCE_INLINE_ static uint32_t to_index(RDD::ShaderStage p_s) {
113
switch (p_s) {
114
case RenderingDeviceCommons::SHADER_STAGE_VERTEX:
115
case RenderingDeviceCommons::SHADER_STAGE_TESSELATION_CONTROL:
116
case RenderingDeviceCommons::SHADER_STAGE_TESSELATION_EVALUATION:
117
case RenderingDeviceCommons::SHADER_STAGE_COMPUTE:
118
default:
119
return 0;
120
case RenderingDeviceCommons::SHADER_STAGE_FRAGMENT:
121
return 1;
122
}
123
}
124
125
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDFrameBuffer {
126
Vector<MTL::Texture> textures;
127
128
public:
129
Size2i size;
130
MDFrameBuffer(Vector<MTL::Texture> p_textures, Size2i p_size) :
131
textures(p_textures), size(p_size) {}
132
MDFrameBuffer() {}
133
134
/// Returns the texture at the given index.
135
_ALWAYS_INLINE_ MTL::Texture get_texture(uint32_t p_idx) const {
136
return textures[p_idx];
137
}
138
139
/// Returns true if the texture at the given index is not nil.
140
_ALWAYS_INLINE_ bool has_texture(uint32_t p_idx) const {
141
return textures[p_idx] != nil;
142
}
143
144
/// Set the texture at the given index.
145
_ALWAYS_INLINE_ void set_texture(uint32_t p_idx, MTL::Texture p_texture) {
146
textures.write[p_idx] = p_texture;
147
}
148
149
/// Unset or nil the texture at the given index.
150
_ALWAYS_INLINE_ void unset_texture(uint32_t p_idx) {
151
textures.write[p_idx] = nil;
152
}
153
154
/// Resizes buffers to the specified size.
155
_ALWAYS_INLINE_ void set_texture_count(uint32_t p_size) {
156
textures.resize(p_size);
157
}
158
159
virtual ~MDFrameBuffer() = default;
160
};
161
162
// These functions are used to convert between Objective-C objects and
163
// the RIDs used by Godot, respecting automatic reference counting.
164
namespace rid {
165
166
// Converts an Objective-C object to a pointer, and incrementing the
167
// reference count.
168
_FORCE_INLINE_ void *owned(id p_id) {
169
return (__bridge_retained void *)p_id;
170
}
171
172
#define MAKE_ID(FROM, TO) \
173
_FORCE_INLINE_ TO make(FROM p_obj) { \
174
return TO(owned(p_obj)); \
175
}
176
177
// These are shared for Metal and Metal 4 drivers
178
179
MAKE_ID(id<MTLTexture>, RDD::TextureID)
180
MAKE_ID(id<MTLBuffer>, RDD::BufferID)
181
MAKE_ID(id<MTLSamplerState>, RDD::SamplerID)
182
MAKE_ID(MTLVertexDescriptor *, RDD::VertexFormatID)
183
184
#undef MAKE_ID
185
186
// Converts a pointer to an Objective-C object without changing the reference count.
187
_FORCE_INLINE_ auto get(RDD::ID p_id) {
188
return (p_id.id) ? (__bridge ::id)(void *)p_id.id : nil;
189
}
190
191
// Converts a pointer to an Objective-C object, and decrements the reference count.
192
_FORCE_INLINE_ auto release(RDD::ID p_id) {
193
return (__bridge_transfer ::id)(void *)p_id.id;
194
}
195
196
} // namespace rid
197
198