Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/rendering_device_driver_metal3.h
21677 views
1
/**************************************************************************/
2
/* rendering_device_driver_metal3.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
#include "metal3_objects.h"
34
#include "rendering_device_driver_metal.h"
35
36
#include <Metal/Metal.hpp>
37
38
namespace MTL3 {
39
40
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingDeviceDriverMetal final : public ::RenderingDeviceDriverMetal {
41
friend class MDCommandBuffer;
42
#pragma mark - Generic
43
44
NS::SharedPtr<MTL::CommandQueue> device_queue;
45
46
struct Fence {
47
virtual void signal(MTL::CommandBuffer *p_cmd_buffer) = 0;
48
virtual Error wait(uint32_t p_timeout_ms) = 0;
49
virtual ~Fence() = default;
50
};
51
52
struct FenceEvent : Fence {
53
NS::SharedPtr<MTL::SharedEvent> event;
54
uint64_t value = 0;
55
FenceEvent(NS::SharedPtr<MTL::SharedEvent> p_event) :
56
event(p_event) {}
57
void signal(MTL::CommandBuffer *p_cb) override;
58
Error wait(uint32_t p_timeout_ms) override;
59
};
60
61
struct FenceSemaphore : Fence {
62
dispatch_semaphore_t semaphore;
63
FenceSemaphore() :
64
semaphore(dispatch_semaphore_create(0)) {}
65
void signal(MTL::CommandBuffer *p_cb) override;
66
Error wait(uint32_t p_timeout_ms) override;
67
};
68
69
struct Semaphore {
70
NS::SharedPtr<MTL::Event> event;
71
uint64_t value = 0;
72
Semaphore(NS::SharedPtr<MTL::Event> p_event) :
73
event(p_event) {}
74
};
75
76
Vector<MDCommandBuffer *> command_buffers;
77
78
Error _create_device() override;
79
Error _execute_and_present_barriers(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains);
80
Error _execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains);
81
82
protected:
83
MTL::CommandQueue *get_command_queue() const override { return device_queue.get(); }
84
void add_residency_set_to_main_queue(MTL::ResidencySet *p_set) override;
85
void remove_residency_set_to_main_queue(MTL::ResidencySet *p_set) override;
86
87
public:
88
Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override;
89
90
FenceID fence_create() override;
91
Error fence_wait(FenceID p_fence) override;
92
void fence_free(FenceID p_fence) override;
93
94
SemaphoreID semaphore_create() override;
95
void semaphore_free(SemaphoreID p_semaphore) override;
96
97
CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override;
98
Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) override;
99
void command_queue_free(CommandQueueID p_cmd_queue) override;
100
101
CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override;
102
bool command_pool_reset(CommandPoolID p_cmd_pool) override;
103
void command_pool_free(CommandPoolID p_cmd_pool) override;
104
105
CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override;
106
107
#pragma mark - Miscellaneous
108
109
String get_api_name() const override { return "Metal"; }
110
111
RenderingDeviceDriverMetal(RenderingContextDriverMetal *p_context_driver);
112
~RenderingDeviceDriverMetal();
113
};
114
115
} // namespace MTL3
116
117