Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/metal_objects.h
9973 views
1
/**************************************************************************/
2
/* metal_objects.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
/**************************************************************************/
34
/* */
35
/* Portions of this code were derived from MoltenVK. */
36
/* */
37
/* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */
38
/* (http://www.brenwill.com) */
39
/* */
40
/* Licensed under the Apache License, Version 2.0 (the "License"); */
41
/* you may not use this file except in compliance with the License. */
42
/* You may obtain a copy of the License at */
43
/* */
44
/* http://www.apache.org/licenses/LICENSE-2.0 */
45
/* */
46
/* Unless required by applicable law or agreed to in writing, software */
47
/* distributed under the License is distributed on an "AS IS" BASIS, */
48
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
49
/* implied. See the License for the specific language governing */
50
/* permissions and limitations under the License. */
51
/**************************************************************************/
52
53
#import "metal_device_properties.h"
54
#import "metal_utils.h"
55
#import "pixel_formats.h"
56
#import "sha256_digest.h"
57
58
#include "servers/rendering/rendering_device_driver.h"
59
60
#import <CommonCrypto/CommonDigest.h>
61
#import <Foundation/Foundation.h>
62
#import <Metal/Metal.h>
63
#import <QuartzCore/CAMetalLayer.h>
64
#import <simd/simd.h>
65
#import <zlib.h>
66
#import <initializer_list>
67
#import <optional>
68
69
// These types can be used in Vector and other containers that use
70
// pointer operations not supported by ARC.
71
namespace MTL {
72
#define MTL_CLASS(name) \
73
class name { \
74
public: \
75
name(id<MTL##name> obj = nil) : m_obj(obj) {} \
76
operator id<MTL##name>() const { \
77
return m_obj; \
78
} \
79
id<MTL##name> m_obj; \
80
};
81
82
MTL_CLASS(Texture)
83
84
} //namespace MTL
85
86
enum ShaderStageUsage : uint32_t {
87
None = 0,
88
Vertex = RDD::SHADER_STAGE_VERTEX_BIT,
89
Fragment = RDD::SHADER_STAGE_FRAGMENT_BIT,
90
TesselationControl = RDD::SHADER_STAGE_TESSELATION_CONTROL_BIT,
91
TesselationEvaluation = RDD::SHADER_STAGE_TESSELATION_EVALUATION_BIT,
92
Compute = RDD::SHADER_STAGE_COMPUTE_BIT,
93
};
94
95
_FORCE_INLINE_ ShaderStageUsage &operator|=(ShaderStageUsage &p_a, int p_b) {
96
p_a = ShaderStageUsage(uint32_t(p_a) | uint32_t(p_b));
97
return p_a;
98
}
99
100
enum StageResourceUsage : uint32_t {
101
VertexRead = (MTLResourceUsageRead << RDD::SHADER_STAGE_VERTEX * 2),
102
VertexWrite = (MTLResourceUsageWrite << RDD::SHADER_STAGE_VERTEX * 2),
103
FragmentRead = (MTLResourceUsageRead << RDD::SHADER_STAGE_FRAGMENT * 2),
104
FragmentWrite = (MTLResourceUsageWrite << RDD::SHADER_STAGE_FRAGMENT * 2),
105
TesselationControlRead = (MTLResourceUsageRead << RDD::SHADER_STAGE_TESSELATION_CONTROL * 2),
106
TesselationControlWrite = (MTLResourceUsageWrite << RDD::SHADER_STAGE_TESSELATION_CONTROL * 2),
107
TesselationEvaluationRead = (MTLResourceUsageRead << RDD::SHADER_STAGE_TESSELATION_EVALUATION * 2),
108
TesselationEvaluationWrite = (MTLResourceUsageWrite << RDD::SHADER_STAGE_TESSELATION_EVALUATION * 2),
109
ComputeRead = (MTLResourceUsageRead << RDD::SHADER_STAGE_COMPUTE * 2),
110
ComputeWrite = (MTLResourceUsageWrite << RDD::SHADER_STAGE_COMPUTE * 2),
111
};
112
113
typedef LocalVector<__unsafe_unretained id<MTLResource>> ResourceVector;
114
typedef HashMap<StageResourceUsage, ResourceVector> ResourceUsageMap;
115
116
enum class MDCommandBufferStateType {
117
None,
118
Render,
119
Compute,
120
Blit,
121
};
122
123
enum class MDPipelineType {
124
None,
125
Render,
126
Compute,
127
};
128
129
class MDRenderPass;
130
class MDPipeline;
131
class MDRenderPipeline;
132
class MDComputePipeline;
133
class MDFrameBuffer;
134
class RenderingDeviceDriverMetal;
135
class MDUniformSet;
136
class MDShader;
137
138
#pragma mark - Resource Factory
139
140
struct ClearAttKey {
141
const static uint32_t COLOR_COUNT = MAX_COLOR_ATTACHMENT_COUNT;
142
const static uint32_t DEPTH_INDEX = COLOR_COUNT;
143
const static uint32_t STENCIL_INDEX = DEPTH_INDEX + 1;
144
const static uint32_t ATTACHMENT_COUNT = STENCIL_INDEX + 1;
145
146
enum Flags : uint16_t {
147
CLEAR_FLAGS_NONE = 0,
148
CLEAR_FLAGS_LAYERED = 1 << 0,
149
};
150
151
Flags flags = CLEAR_FLAGS_NONE;
152
uint16_t sample_count = 0;
153
uint16_t pixel_formats[ATTACHMENT_COUNT] = { 0 };
154
155
_FORCE_INLINE_ void set_color_format(uint32_t p_idx, MTLPixelFormat p_fmt) { pixel_formats[p_idx] = p_fmt; }
156
_FORCE_INLINE_ void set_depth_format(MTLPixelFormat p_fmt) { pixel_formats[DEPTH_INDEX] = p_fmt; }
157
_FORCE_INLINE_ void set_stencil_format(MTLPixelFormat p_fmt) { pixel_formats[STENCIL_INDEX] = p_fmt; }
158
_FORCE_INLINE_ MTLPixelFormat depth_format() const { return (MTLPixelFormat)pixel_formats[DEPTH_INDEX]; }
159
_FORCE_INLINE_ MTLPixelFormat stencil_format() const { return (MTLPixelFormat)pixel_formats[STENCIL_INDEX]; }
160
_FORCE_INLINE_ void enable_layered_rendering() { flags::set(flags, CLEAR_FLAGS_LAYERED); }
161
162
_FORCE_INLINE_ bool is_enabled(uint32_t p_idx) const { return pixel_formats[p_idx] != 0; }
163
_FORCE_INLINE_ bool is_depth_enabled() const { return pixel_formats[DEPTH_INDEX] != 0; }
164
_FORCE_INLINE_ bool is_stencil_enabled() const { return pixel_formats[STENCIL_INDEX] != 0; }
165
_FORCE_INLINE_ bool is_layered_rendering_enabled() const { return flags::any(flags, CLEAR_FLAGS_LAYERED); }
166
167
_FORCE_INLINE_ bool operator==(const ClearAttKey &p_rhs) const {
168
return memcmp(this, &p_rhs, sizeof(ClearAttKey)) == 0;
169
}
170
171
uint32_t hash() const {
172
uint32_t h = hash_murmur3_one_32(flags);
173
h = hash_murmur3_one_32(sample_count, h);
174
h = hash_murmur3_buffer(pixel_formats, ATTACHMENT_COUNT * sizeof(pixel_formats[0]), h);
175
return hash_fmix32(h);
176
}
177
};
178
179
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDResourceFactory {
180
private:
181
RenderingDeviceDriverMetal *device_driver;
182
183
id<MTLFunction> new_func(NSString *p_source, NSString *p_name, NSError **p_error);
184
id<MTLFunction> new_clear_vert_func(ClearAttKey &p_key);
185
id<MTLFunction> new_clear_frag_func(ClearAttKey &p_key);
186
NSString *get_format_type_string(MTLPixelFormat p_fmt);
187
188
public:
189
id<MTLRenderPipelineState> new_clear_pipeline_state(ClearAttKey &p_key, NSError **p_error);
190
id<MTLDepthStencilState> new_depth_stencil_state(bool p_use_depth, bool p_use_stencil);
191
192
MDResourceFactory(RenderingDeviceDriverMetal *p_device_driver) :
193
device_driver(p_device_driver) {}
194
~MDResourceFactory() = default;
195
};
196
197
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDResourceCache {
198
private:
199
typedef HashMap<ClearAttKey, id<MTLRenderPipelineState>, HashableHasher<ClearAttKey>> HashMap;
200
std::unique_ptr<MDResourceFactory> resource_factory;
201
HashMap clear_states;
202
203
struct {
204
id<MTLDepthStencilState> all;
205
id<MTLDepthStencilState> depth_only;
206
id<MTLDepthStencilState> stencil_only;
207
id<MTLDepthStencilState> none;
208
} clear_depth_stencil_state;
209
210
public:
211
id<MTLRenderPipelineState> get_clear_render_pipeline_state(ClearAttKey &p_key, NSError **p_error);
212
id<MTLDepthStencilState> get_depth_stencil_state(bool p_use_depth, bool p_use_stencil);
213
214
explicit MDResourceCache(RenderingDeviceDriverMetal *p_device_driver) :
215
resource_factory(new MDResourceFactory(p_device_driver)) {}
216
~MDResourceCache() = default;
217
};
218
219
enum class MDAttachmentType : uint8_t {
220
None = 0,
221
Color = 1 << 0,
222
Depth = 1 << 1,
223
Stencil = 1 << 2,
224
};
225
226
_FORCE_INLINE_ MDAttachmentType &operator|=(MDAttachmentType &p_a, MDAttachmentType p_b) {
227
flags::set(p_a, p_b);
228
return p_a;
229
}
230
231
_FORCE_INLINE_ bool operator&(MDAttachmentType p_a, MDAttachmentType p_b) {
232
return uint8_t(p_a) & uint8_t(p_b);
233
}
234
235
struct MDSubpass {
236
uint32_t subpass_index = 0;
237
uint32_t view_count = 0;
238
LocalVector<RDD::AttachmentReference> input_references;
239
LocalVector<RDD::AttachmentReference> color_references;
240
RDD::AttachmentReference depth_stencil_reference;
241
LocalVector<RDD::AttachmentReference> resolve_references;
242
243
MTLFmtCaps getRequiredFmtCapsForAttachmentAt(uint32_t p_index) const;
244
};
245
246
struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDAttachment {
247
private:
248
uint32_t index = 0;
249
uint32_t firstUseSubpassIndex = 0;
250
uint32_t lastUseSubpassIndex = 0;
251
252
public:
253
MTLPixelFormat format = MTLPixelFormatInvalid;
254
MDAttachmentType type = MDAttachmentType::None;
255
MTLLoadAction loadAction = MTLLoadActionDontCare;
256
MTLStoreAction storeAction = MTLStoreActionDontCare;
257
MTLLoadAction stencilLoadAction = MTLLoadActionDontCare;
258
MTLStoreAction stencilStoreAction = MTLStoreActionDontCare;
259
uint32_t samples = 1;
260
261
/*!
262
* @brief Returns true if this attachment is first used in the given subpass.
263
* @param p_subpass
264
* @return
265
*/
266
_FORCE_INLINE_ bool isFirstUseOf(MDSubpass const &p_subpass) const {
267
return p_subpass.subpass_index == firstUseSubpassIndex;
268
}
269
270
/*!
271
* @brief Returns true if this attachment is last used in the given subpass.
272
* @param p_subpass
273
* @return
274
*/
275
_FORCE_INLINE_ bool isLastUseOf(MDSubpass const &p_subpass) const {
276
return p_subpass.subpass_index == lastUseSubpassIndex;
277
}
278
279
void linkToSubpass(MDRenderPass const &p_pass);
280
281
MTLStoreAction getMTLStoreAction(MDSubpass const &p_subpass,
282
bool p_is_rendering_entire_area,
283
bool p_has_resolve,
284
bool p_can_resolve,
285
bool p_is_stencil) const;
286
bool configureDescriptor(MTLRenderPassAttachmentDescriptor *p_desc,
287
PixelFormats &p_pf,
288
MDSubpass const &p_subpass,
289
id<MTLTexture> p_attachment,
290
bool p_is_rendering_entire_area,
291
bool p_has_resolve,
292
bool p_can_resolve,
293
bool p_is_stencil) const;
294
/** Returns whether this attachment should be cleared in the subpass. */
295
bool shouldClear(MDSubpass const &p_subpass, bool p_is_stencil) const;
296
};
297
298
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDRenderPass {
299
public:
300
Vector<MDAttachment> attachments;
301
Vector<MDSubpass> subpasses;
302
303
uint32_t get_sample_count() const {
304
return attachments.is_empty() ? 1 : attachments[0].samples;
305
}
306
307
MDRenderPass(Vector<MDAttachment> &p_attachments, Vector<MDSubpass> &p_subpasses);
308
};
309
310
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDCommandBuffer {
311
private:
312
#pragma mark - Common State
313
314
// From RenderingDevice
315
static constexpr uint32_t MAX_PUSH_CONSTANT_SIZE = 128;
316
317
RenderingDeviceDriverMetal *device_driver = nullptr;
318
id<MTLCommandQueue> queue = nil;
319
id<MTLCommandBuffer> commandBuffer = nil;
320
bool state_begin = false;
321
322
_FORCE_INLINE_ id<MTLCommandBuffer> command_buffer() {
323
DEV_ASSERT(state_begin);
324
if (commandBuffer == nil) {
325
commandBuffer = queue.commandBuffer;
326
}
327
return commandBuffer;
328
}
329
330
void _end_compute_dispatch();
331
void _end_blit();
332
333
#pragma mark - Render
334
335
void _render_set_dirty_state();
336
void _render_bind_uniform_sets();
337
338
void _populate_vertices(simd::float4 *p_vertices, Size2i p_fb_size, VectorView<Rect2i> p_rects);
339
uint32_t _populate_vertices(simd::float4 *p_vertices, uint32_t p_index, Rect2i const &p_rect, Size2i p_fb_size);
340
void _end_render_pass();
341
void _render_clear_render_area();
342
343
#pragma mark - Compute
344
345
void _compute_set_dirty_state();
346
void _compute_bind_uniform_sets();
347
348
public:
349
MDCommandBufferStateType type = MDCommandBufferStateType::None;
350
351
struct RenderState {
352
MDRenderPass *pass = nullptr;
353
MDFrameBuffer *frameBuffer = nullptr;
354
MDRenderPipeline *pipeline = nullptr;
355
LocalVector<RDD::RenderPassClearValue> clear_values;
356
LocalVector<MTLViewport> viewports;
357
LocalVector<MTLScissorRect> scissors;
358
std::optional<Color> blend_constants;
359
uint32_t current_subpass = UINT32_MAX;
360
Rect2i render_area = {};
361
bool is_rendering_entire_area = false;
362
MTLRenderPassDescriptor *desc = nil;
363
id<MTLRenderCommandEncoder> encoder = nil;
364
id<MTLBuffer> __unsafe_unretained index_buffer = nil; // Buffer is owned by RDD.
365
MTLIndexType index_type = MTLIndexTypeUInt16;
366
uint32_t index_offset = 0;
367
LocalVector<id<MTLBuffer> __unsafe_unretained> vertex_buffers;
368
LocalVector<NSUInteger> vertex_offsets;
369
ResourceUsageMap resource_usage;
370
// clang-format off
371
enum DirtyFlag: uint16_t {
372
DIRTY_NONE = 0,
373
DIRTY_PIPELINE = 1 << 0, //! pipeline state
374
DIRTY_UNIFORMS = 1 << 1, //! uniform sets
375
DIRTY_PUSH = 1 << 2, //! push constants
376
DIRTY_DEPTH = 1 << 3, //! depth / stencil state
377
DIRTY_VERTEX = 1 << 4, //! vertex buffers
378
DIRTY_VIEWPORT = 1 << 5, //! viewport rectangles
379
DIRTY_SCISSOR = 1 << 6, //! scissor rectangles
380
DIRTY_BLEND = 1 << 7, //! blend state
381
DIRTY_RASTER = 1 << 8, //! encoder state like cull mode
382
DIRTY_ALL = (1 << 9) - 1,
383
};
384
// clang-format on
385
BitField<DirtyFlag> dirty = DIRTY_NONE;
386
387
LocalVector<MDUniformSet *> uniform_sets;
388
// Bit mask of the uniform sets that are dirty, to prevent redundant binding.
389
uint64_t uniform_set_mask = 0;
390
uint8_t push_constant_data[MAX_PUSH_CONSTANT_SIZE];
391
uint32_t push_constant_data_len = 0;
392
uint32_t push_constant_bindings[2] = { 0 };
393
394
_FORCE_INLINE_ void reset();
395
void end_encoding();
396
397
_ALWAYS_INLINE_ const MDSubpass &get_subpass() const {
398
DEV_ASSERT(pass != nullptr);
399
return pass->subpasses[current_subpass];
400
}
401
402
_FORCE_INLINE_ void mark_viewport_dirty() {
403
if (viewports.is_empty()) {
404
return;
405
}
406
dirty.set_flag(DirtyFlag::DIRTY_VIEWPORT);
407
}
408
409
_FORCE_INLINE_ void mark_scissors_dirty() {
410
if (scissors.is_empty()) {
411
return;
412
}
413
dirty.set_flag(DirtyFlag::DIRTY_SCISSOR);
414
}
415
416
_FORCE_INLINE_ void mark_vertex_dirty() {
417
if (vertex_buffers.is_empty()) {
418
return;
419
}
420
dirty.set_flag(DirtyFlag::DIRTY_VERTEX);
421
}
422
423
_FORCE_INLINE_ void mark_uniforms_dirty(std::initializer_list<uint32_t> l) {
424
if (uniform_sets.is_empty()) {
425
return;
426
}
427
for (uint32_t i : l) {
428
if (i < uniform_sets.size() && uniform_sets[i] != nullptr) {
429
uniform_set_mask |= 1 << i;
430
}
431
}
432
dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS);
433
}
434
435
_FORCE_INLINE_ void mark_uniforms_dirty(void) {
436
if (uniform_sets.is_empty()) {
437
return;
438
}
439
for (uint32_t i = 0; i < uniform_sets.size(); i++) {
440
if (uniform_sets[i] != nullptr) {
441
uniform_set_mask |= 1 << i;
442
}
443
}
444
dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS);
445
}
446
447
_FORCE_INLINE_ void mark_push_constants_dirty() {
448
if (push_constant_data_len == 0) {
449
return;
450
}
451
dirty.set_flag(DirtyFlag::DIRTY_PUSH);
452
}
453
454
_FORCE_INLINE_ void mark_blend_dirty() {
455
if (!blend_constants.has_value()) {
456
return;
457
}
458
dirty.set_flag(DirtyFlag::DIRTY_BLEND);
459
}
460
461
MTLScissorRect clip_to_render_area(MTLScissorRect p_rect) const {
462
uint32_t raLeft = render_area.position.x;
463
uint32_t raRight = raLeft + render_area.size.width;
464
uint32_t raBottom = render_area.position.y;
465
uint32_t raTop = raBottom + render_area.size.height;
466
467
p_rect.x = CLAMP(p_rect.x, raLeft, MAX(raRight - 1, raLeft));
468
p_rect.y = CLAMP(p_rect.y, raBottom, MAX(raTop - 1, raBottom));
469
p_rect.width = MIN(p_rect.width, raRight - p_rect.x);
470
p_rect.height = MIN(p_rect.height, raTop - p_rect.y);
471
472
return p_rect;
473
}
474
475
Rect2i clip_to_render_area(Rect2i p_rect) const {
476
int32_t raLeft = render_area.position.x;
477
int32_t raRight = raLeft + render_area.size.width;
478
int32_t raBottom = render_area.position.y;
479
int32_t raTop = raBottom + render_area.size.height;
480
481
p_rect.position.x = CLAMP(p_rect.position.x, raLeft, MAX(raRight - 1, raLeft));
482
p_rect.position.y = CLAMP(p_rect.position.y, raBottom, MAX(raTop - 1, raBottom));
483
p_rect.size.width = MIN(p_rect.size.width, raRight - p_rect.position.x);
484
p_rect.size.height = MIN(p_rect.size.height, raTop - p_rect.position.y);
485
486
return p_rect;
487
}
488
489
} render;
490
491
// State specific for a compute pass.
492
struct ComputeState {
493
MDComputePipeline *pipeline = nullptr;
494
id<MTLComputeCommandEncoder> encoder = nil;
495
ResourceUsageMap resource_usage;
496
// clang-format off
497
enum DirtyFlag: uint16_t {
498
DIRTY_NONE = 0,
499
DIRTY_PIPELINE = 1 << 0, //! pipeline state
500
DIRTY_UNIFORMS = 1 << 1, //! uniform sets
501
DIRTY_PUSH = 1 << 2, //! push constants
502
DIRTY_ALL = (1 << 3) - 1,
503
};
504
// clang-format on
505
BitField<DirtyFlag> dirty = DIRTY_NONE;
506
507
LocalVector<MDUniformSet *> uniform_sets;
508
// Bit mask of the uniform sets that are dirty, to prevent redundant binding.
509
uint64_t uniform_set_mask = 0;
510
uint8_t push_constant_data[MAX_PUSH_CONSTANT_SIZE];
511
uint32_t push_constant_data_len = 0;
512
uint32_t push_constant_bindings[1] = { 0 };
513
514
_FORCE_INLINE_ void reset();
515
void end_encoding();
516
517
_FORCE_INLINE_ void mark_uniforms_dirty(void) {
518
if (uniform_sets.is_empty()) {
519
return;
520
}
521
for (uint32_t i = 0; i < uniform_sets.size(); i++) {
522
if (uniform_sets[i] != nullptr) {
523
uniform_set_mask |= 1 << i;
524
}
525
}
526
dirty.set_flag(DirtyFlag::DIRTY_UNIFORMS);
527
}
528
529
_FORCE_INLINE_ void mark_push_constants_dirty() {
530
if (push_constant_data_len == 0) {
531
return;
532
}
533
dirty.set_flag(DirtyFlag::DIRTY_PUSH);
534
}
535
536
} compute;
537
538
// State specific to a blit pass.
539
struct {
540
id<MTLBlitCommandEncoder> encoder = nil;
541
_FORCE_INLINE_ void reset() {
542
encoder = nil;
543
}
544
} blit;
545
546
_FORCE_INLINE_ id<MTLCommandBuffer> get_command_buffer() const {
547
return commandBuffer;
548
}
549
550
void begin();
551
void commit();
552
void end();
553
554
id<MTLBlitCommandEncoder> blit_command_encoder();
555
void encodeRenderCommandEncoderWithDescriptor(MTLRenderPassDescriptor *p_desc, NSString *p_label);
556
557
void bind_pipeline(RDD::PipelineID p_pipeline);
558
void encode_push_constant_data(RDD::ShaderID p_shader, VectorView<uint32_t> p_data);
559
560
#pragma mark - Render Commands
561
562
void render_bind_uniform_set(RDD::UniformSetID p_uniform_set, RDD::ShaderID p_shader, uint32_t p_set_index);
563
void render_bind_uniform_sets(VectorView<RDD::UniformSetID> p_uniform_sets, RDD::ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count);
564
void render_clear_attachments(VectorView<RDD::AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects);
565
void render_set_viewport(VectorView<Rect2i> p_viewports);
566
void render_set_scissor(VectorView<Rect2i> p_scissors);
567
void render_set_blend_constants(const Color &p_constants);
568
void render_begin_pass(RDD::RenderPassID p_render_pass,
569
RDD::FramebufferID p_frameBuffer,
570
RDD::CommandBufferType p_cmd_buffer_type,
571
const Rect2i &p_rect,
572
VectorView<RDD::RenderPassClearValue> p_clear_values);
573
void render_next_subpass();
574
void render_draw(uint32_t p_vertex_count,
575
uint32_t p_instance_count,
576
uint32_t p_base_vertex,
577
uint32_t p_first_instance);
578
void render_bind_vertex_buffers(uint32_t p_binding_count, const RDD::BufferID *p_buffers, const uint64_t *p_offsets);
579
void render_bind_index_buffer(RDD::BufferID p_buffer, RDD::IndexBufferFormat p_format, uint64_t p_offset);
580
581
void render_draw_indexed(uint32_t p_index_count,
582
uint32_t p_instance_count,
583
uint32_t p_first_index,
584
int32_t p_vertex_offset,
585
uint32_t p_first_instance);
586
587
void render_draw_indexed_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
588
void render_draw_indexed_indirect_count(RDD::BufferID p_indirect_buffer, uint64_t p_offset, RDD::BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride);
589
void render_draw_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride);
590
void render_draw_indirect_count(RDD::BufferID p_indirect_buffer, uint64_t p_offset, RDD::BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride);
591
592
void render_end_pass();
593
594
#pragma mark - Compute Commands
595
596
void compute_bind_uniform_set(RDD::UniformSetID p_uniform_set, RDD::ShaderID p_shader, uint32_t p_set_index);
597
void compute_bind_uniform_sets(VectorView<RDD::UniformSetID> p_uniform_sets, RDD::ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count);
598
void compute_dispatch(uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups);
599
void compute_dispatch_indirect(RDD::BufferID p_indirect_buffer, uint64_t p_offset);
600
601
MDCommandBuffer(id<MTLCommandQueue> p_queue, RenderingDeviceDriverMetal *p_device_driver) :
602
device_driver(p_device_driver), queue(p_queue) {
603
type = MDCommandBufferStateType::None;
604
}
605
606
MDCommandBuffer() = default;
607
};
608
609
#if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED < 140000) || (TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED < 170000)
610
#define MTLBindingAccess MTLArgumentAccess
611
#define MTLBindingAccessReadOnly MTLArgumentAccessReadOnly
612
#define MTLBindingAccessReadWrite MTLArgumentAccessReadWrite
613
#define MTLBindingAccessWriteOnly MTLArgumentAccessWriteOnly
614
#endif
615
616
struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) BindingInfo {
617
MTLDataType dataType = MTLDataTypeNone;
618
uint32_t index = 0;
619
MTLBindingAccess access = MTLBindingAccessReadOnly;
620
MTLResourceUsage usage = 0;
621
MTLTextureType textureType = MTLTextureType2D;
622
int imageFormat = 0;
623
uint32_t arrayLength = 0;
624
bool isMultisampled = false;
625
626
inline MTLArgumentDescriptor *new_argument_descriptor() const {
627
MTLArgumentDescriptor *desc = MTLArgumentDescriptor.argumentDescriptor;
628
desc.dataType = dataType;
629
desc.index = index;
630
desc.access = access;
631
desc.textureType = textureType;
632
desc.arrayLength = arrayLength;
633
return desc;
634
}
635
};
636
637
using RDC = RenderingDeviceCommons;
638
639
typedef API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) HashMap<RDC::ShaderStage, BindingInfo> BindingInfoMap;
640
641
struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) UniformInfo {
642
uint32_t binding;
643
ShaderStageUsage active_stages = None;
644
BindingInfoMap bindings;
645
BindingInfoMap bindings_secondary;
646
};
647
648
struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) UniformSet {
649
LocalVector<UniformInfo> uniforms;
650
uint32_t buffer_size = 0;
651
HashMap<RDC::ShaderStage, uint32_t> offsets;
652
HashMap<RDC::ShaderStage, id<MTLArgumentEncoder>> encoders;
653
};
654
655
struct ShaderCacheEntry;
656
657
enum class ShaderLoadStrategy {
658
IMMEDIATE,
659
LAZY,
660
661
/// The default strategy is to load the shader immediately.
662
DEFAULT = IMMEDIATE,
663
};
664
665
/// A Metal shader library.
666
@interface MDLibrary : NSObject {
667
ShaderCacheEntry *_entry;
668
NSString *_original_source;
669
};
670
- (id<MTLLibrary>)library;
671
- (NSError *)error;
672
- (void)setLabel:(NSString *)label;
673
#ifdef DEV_ENABLED
674
- (NSString *)originalSource;
675
#endif
676
677
+ (instancetype)newLibraryWithCacheEntry:(ShaderCacheEntry *)entry
678
device:(id<MTLDevice>)device
679
source:(NSString *)source
680
options:(MTLCompileOptions *)options
681
strategy:(ShaderLoadStrategy)strategy;
682
683
+ (instancetype)newLibraryWithCacheEntry:(ShaderCacheEntry *)entry
684
device:(id<MTLDevice>)device
685
#ifdef DEV_ENABLED
686
source:(NSString *)source
687
#endif
688
data:(dispatch_data_t)data;
689
@end
690
691
template <>
692
struct HashMapComparatorDefault<SHA256Digest> {
693
static bool compare(const SHA256Digest &p_lhs, const SHA256Digest &p_rhs) {
694
return memcmp(p_lhs.data, p_rhs.data, CC_SHA256_DIGEST_LENGTH) == 0;
695
}
696
};
697
698
/// A cache entry for a Metal shader library.
699
struct ShaderCacheEntry {
700
RenderingDeviceDriverMetal &owner;
701
/// A hash of the Metal shader source code.
702
SHA256Digest key;
703
CharString name;
704
RD::ShaderStage stage = RD::SHADER_STAGE_VERTEX;
705
/// This reference must be weak, to ensure that when the last strong reference to the library
706
/// is released, the cache entry is freed.
707
MDLibrary *__weak library = nil;
708
709
/// Notify the cache that this entry is no longer needed.
710
void notify_free() const;
711
712
ShaderCacheEntry(RenderingDeviceDriverMetal &p_owner, SHA256Digest p_key) :
713
owner(p_owner), key(p_key) {
714
}
715
~ShaderCacheEntry() = default;
716
};
717
718
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDShader {
719
public:
720
CharString name;
721
Vector<UniformSet> sets;
722
bool uses_argument_buffers = true;
723
724
MDShader(CharString p_name, Vector<UniformSet> p_sets, bool p_uses_argument_buffers) :
725
name(p_name), sets(p_sets), uses_argument_buffers(p_uses_argument_buffers) {}
726
virtual ~MDShader() = default;
727
};
728
729
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDComputeShader final : public MDShader {
730
public:
731
struct {
732
int32_t binding = -1;
733
uint32_t size = 0;
734
} push_constants;
735
MTLSize local = {};
736
737
MDLibrary *kernel;
738
739
MDComputeShader(CharString p_name, Vector<UniformSet> p_sets, bool p_uses_argument_buffers, MDLibrary *p_kernel);
740
};
741
742
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDRenderShader final : public MDShader {
743
public:
744
struct {
745
struct {
746
int32_t binding = -1;
747
uint32_t size = 0;
748
} vert;
749
struct {
750
int32_t binding = -1;
751
uint32_t size = 0;
752
} frag;
753
} push_constants;
754
bool needs_view_mask_buffer = false;
755
756
MDLibrary *vert;
757
MDLibrary *frag;
758
759
MDRenderShader(CharString p_name,
760
Vector<UniformSet> p_sets,
761
bool p_needs_view_mask_buffer,
762
bool p_uses_argument_buffers,
763
MDLibrary *p_vert, MDLibrary *p_frag);
764
};
765
766
_FORCE_INLINE_ StageResourceUsage &operator|=(StageResourceUsage &p_a, uint32_t p_b) {
767
p_a = StageResourceUsage(uint32_t(p_a) | p_b);
768
return p_a;
769
}
770
771
_FORCE_INLINE_ StageResourceUsage stage_resource_usage(RDC::ShaderStage p_stage, MTLResourceUsage p_usage) {
772
return StageResourceUsage(p_usage << (p_stage * 2));
773
}
774
775
_FORCE_INLINE_ MTLResourceUsage resource_usage_for_stage(StageResourceUsage p_usage, RDC::ShaderStage p_stage) {
776
return MTLResourceUsage((p_usage >> (p_stage * 2)) & 0b11);
777
}
778
779
template <>
780
struct HashMapComparatorDefault<RDD::ShaderID> {
781
static bool compare(const RDD::ShaderID &p_lhs, const RDD::ShaderID &p_rhs) {
782
return p_lhs.id == p_rhs.id;
783
}
784
};
785
786
struct BoundUniformSet {
787
id<MTLBuffer> buffer;
788
ResourceUsageMap usage_to_resources;
789
790
/// Perform a 2-way merge each key of `ResourceVector` resources from this set into the
791
/// destination set.
792
///
793
/// Assumes the vectors of resources are sorted.
794
void merge_into(ResourceUsageMap &p_dst) const;
795
};
796
797
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDUniformSet {
798
private:
799
void bind_uniforms_argument_buffers(MDShader *p_shader, MDCommandBuffer::RenderState &p_state, uint32_t p_set_index);
800
void bind_uniforms_direct(MDShader *p_shader, MDCommandBuffer::RenderState &p_state, uint32_t p_set_index);
801
void bind_uniforms_argument_buffers(MDShader *p_shader, MDCommandBuffer::ComputeState &p_state, uint32_t p_set_index);
802
void bind_uniforms_direct(MDShader *p_shader, MDCommandBuffer::ComputeState &p_state, uint32_t p_set_index);
803
804
public:
805
uint32_t index;
806
LocalVector<RDD::BoundUniform> uniforms;
807
HashMap<MDShader *, BoundUniformSet> bound_uniforms;
808
809
void bind_uniforms(MDShader *p_shader, MDCommandBuffer::RenderState &p_state, uint32_t p_set_index);
810
void bind_uniforms(MDShader *p_shader, MDCommandBuffer::ComputeState &p_state, uint32_t p_set_index);
811
812
BoundUniformSet &bound_uniform_set(MDShader *p_shader, id<MTLDevice> p_device, ResourceUsageMap &p_resource_usage, uint32_t p_set_index);
813
};
814
815
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDPipeline {
816
public:
817
MDPipelineType type;
818
819
explicit MDPipeline(MDPipelineType p_type) :
820
type(p_type) {}
821
virtual ~MDPipeline() = default;
822
};
823
824
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDRenderPipeline final : public MDPipeline {
825
public:
826
id<MTLRenderPipelineState> state = nil;
827
id<MTLDepthStencilState> depth_stencil = nil;
828
uint32_t push_constant_size = 0;
829
uint32_t push_constant_stages_mask = 0;
830
SampleCount sample_count = SampleCount1;
831
832
struct {
833
MTLCullMode cull_mode = MTLCullModeNone;
834
MTLTriangleFillMode fill_mode = MTLTriangleFillModeFill;
835
MTLDepthClipMode clip_mode = MTLDepthClipModeClip;
836
MTLWinding winding = MTLWindingClockwise;
837
MTLPrimitiveType render_primitive = MTLPrimitiveTypePoint;
838
839
struct {
840
bool enabled = false;
841
} depth_test;
842
843
struct {
844
bool enabled = false;
845
float depth_bias = 0.0;
846
float slope_scale = 0.0;
847
float clamp = 0.0;
848
_FORCE_INLINE_ void apply(id<MTLRenderCommandEncoder> __unsafe_unretained p_enc) const {
849
if (!enabled) {
850
return;
851
}
852
[p_enc setDepthBias:depth_bias slopeScale:slope_scale clamp:clamp];
853
}
854
} depth_bias;
855
856
struct {
857
bool enabled = false;
858
uint32_t front_reference = 0;
859
uint32_t back_reference = 0;
860
_FORCE_INLINE_ void apply(id<MTLRenderCommandEncoder> __unsafe_unretained p_enc) const {
861
if (!enabled) {
862
return;
863
}
864
[p_enc setStencilFrontReferenceValue:front_reference backReferenceValue:back_reference];
865
}
866
} stencil;
867
868
struct {
869
bool enabled = false;
870
float r = 0.0;
871
float g = 0.0;
872
float b = 0.0;
873
float a = 0.0;
874
875
_FORCE_INLINE_ void apply(id<MTLRenderCommandEncoder> __unsafe_unretained p_enc) const {
876
//if (!enabled)
877
// return;
878
[p_enc setBlendColorRed:r green:g blue:b alpha:a];
879
}
880
} blend;
881
882
_FORCE_INLINE_ void apply(id<MTLRenderCommandEncoder> __unsafe_unretained p_enc) const {
883
[p_enc setCullMode:cull_mode];
884
[p_enc setTriangleFillMode:fill_mode];
885
[p_enc setDepthClipMode:clip_mode];
886
[p_enc setFrontFacingWinding:winding];
887
depth_bias.apply(p_enc);
888
stencil.apply(p_enc);
889
blend.apply(p_enc);
890
}
891
892
} raster_state;
893
894
MDRenderShader *shader = nil;
895
896
MDRenderPipeline() :
897
MDPipeline(MDPipelineType::Render) {}
898
~MDRenderPipeline() final = default;
899
};
900
901
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDComputePipeline final : public MDPipeline {
902
public:
903
id<MTLComputePipelineState> state = nil;
904
struct {
905
MTLSize local = {};
906
} compute_state;
907
908
MDComputeShader *shader = nil;
909
910
explicit MDComputePipeline(id<MTLComputePipelineState> p_state) :
911
MDPipeline(MDPipelineType::Compute), state(p_state) {}
912
~MDComputePipeline() final = default;
913
};
914
915
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDFrameBuffer {
916
Vector<MTL::Texture> textures;
917
918
public:
919
Size2i size;
920
MDFrameBuffer(Vector<MTL::Texture> p_textures, Size2i p_size) :
921
textures(p_textures), size(p_size) {}
922
MDFrameBuffer() {}
923
924
/// Returns the texture at the given index.
925
_ALWAYS_INLINE_ MTL::Texture get_texture(uint32_t p_idx) const {
926
return textures[p_idx];
927
}
928
929
/// Returns true if the texture at the given index is not nil.
930
_ALWAYS_INLINE_ bool has_texture(uint32_t p_idx) const {
931
return textures[p_idx] != nil;
932
}
933
934
/// Set the texture at the given index.
935
_ALWAYS_INLINE_ void set_texture(uint32_t p_idx, MTL::Texture p_texture) {
936
textures.write[p_idx] = p_texture;
937
}
938
939
/// Unset or nil the texture at the given index.
940
_ALWAYS_INLINE_ void unset_texture(uint32_t p_idx) {
941
textures.write[p_idx] = nil;
942
}
943
944
/// Resizes buffers to the specified size.
945
_ALWAYS_INLINE_ void set_texture_count(uint32_t p_size) {
946
textures.resize(p_size);
947
}
948
949
virtual ~MDFrameBuffer() = default;
950
};
951
952
// These functions are used to convert between Objective-C objects and
953
// the RIDs used by Godot, respecting automatic reference counting.
954
namespace rid {
955
956
// Converts an Objective-C object to a pointer, and incrementing the
957
// reference count.
958
_FORCE_INLINE_ void *owned(id p_id) {
959
return (__bridge_retained void *)p_id;
960
}
961
962
#define MAKE_ID(FROM, TO) \
963
_FORCE_INLINE_ TO make(FROM p_obj) { \
964
return TO(owned(p_obj)); \
965
}
966
967
MAKE_ID(id<MTLTexture>, RDD::TextureID)
968
MAKE_ID(id<MTLBuffer>, RDD::BufferID)
969
MAKE_ID(id<MTLSamplerState>, RDD::SamplerID)
970
MAKE_ID(MTLVertexDescriptor *, RDD::VertexFormatID)
971
MAKE_ID(id<MTLCommandQueue>, RDD::CommandPoolID)
972
973
// Converts a pointer to an Objective-C object without changing the reference count.
974
_FORCE_INLINE_ auto get(RDD::ID p_id) {
975
return (p_id.id) ? (__bridge ::id)(void *)p_id.id : nil;
976
}
977
978
// Converts a pointer to an Objective-C object, and decrements the reference count.
979
_FORCE_INLINE_ auto release(RDD::ID p_id) {
980
return (__bridge_transfer ::id)(void *)p_id.id;
981
}
982
983
} // namespace rid
984
985