0.2.0-dev Download ZIP
330 lines ·11.9 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* rendering_device_driver_metal3.cpp */ |
| 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 | #include "rendering_device_driver_metal3.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/os/os.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | #include "drivers/metal/pixel_formats.h" |
| 37 | #include "drivers/metal/rendering_context_driver_metal.h" |
| 38 | |
| 39 | namespace MTL3 { |
| 40 | |
| 41 | #pragma mark - Fence |
| 42 | |
| 43 | void RenderingDeviceDriverMetal::Fence::signal(MTL::CommandBuffer *p_cb) { |
| 44 | if (p_cb) { |
| 45 | value++; |
| 46 | p_cb->encodeSignalEvent(event.get(), value); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | Error RenderingDeviceDriverMetal::Fence::wait(uint32_t p_timeout_ms) { |
| 51 | if (unlikely(value == 0)) { |
| 52 | WARN_PRINT_ONCE("Never signaled fence."); |
| 53 | return OK; |
| 54 | } |
| 55 | bool signaled = event->waitUntilSignaledValue(value, p_timeout_ms); |
| 56 | if (!signaled) { |
| 57 | #ifdef DEBUG_ENABLED |
| 58 | ERR_PRINT("timeout waiting for fence"); |
| 59 | #endif |
| 60 | return ERR_TIMEOUT; |
| 61 | } |
| 62 | return OK; |
| 63 | } |
| 64 | |
| 65 | #pragma mark - Constructor / Destructor |
| 66 | |
| 67 | RenderingDeviceDriverMetal::RenderingDeviceDriverMetal(RenderingContextDriverMetal *p_context_driver) : |
| 68 | ::RenderingDeviceDriverMetal(p_context_driver) { |
| 69 | } |
| 70 | |
| 71 | RenderingDeviceDriverMetal::~RenderingDeviceDriverMetal() { |
| 72 | for (MDCommandBuffer *cb : command_buffers) { |
| 73 | memdelete(cb); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #pragma mark - Initialization |
| 78 | |
| 79 | Error RenderingDeviceDriverMetal::_create_device() { |
| 80 | Error err = ::RenderingDeviceDriverMetal::_create_device(); |
| 81 | ERR_FAIL_COND_V(err, err); |
| 82 | |
| 83 | device_queue = NS::TransferPtr(device->newCommandQueue()); |
| 84 | ERR_FAIL_NULL_V(device_queue.get(), ERR_CANT_CREATE); |
| 85 | device_queue->setLabel(MTLSTR("Godot Main Command Queue")); |
| 86 | |
| 87 | return OK; |
| 88 | } |
| 89 | |
| 90 | void RenderingDeviceDriverMetal::_resolve_sync_mode() { |
| 91 | if (sync_mode == Barriers) { |
| 92 | print_verbose("Metal 3: Barrier synchronization enabled."); |
| 93 | base_hazard_tracking = MTL::ResourceHazardTrackingModeUntracked; |
| 94 | // Apple GPUs only sample counters at stage boundaries, which is all the |
| 95 | // timestamp encoders rely on. |
| 96 | timestamp_queries_supported = device->supportsCounterSampling(MTL::CounterSamplingPointAtStageBoundary); |
| 97 | } else { |
| 98 | print_verbose("Metal 3: Native hazard tracking enabled."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Error RenderingDeviceDriverMetal::initialize(uint32_t p_device_index, uint32_t p_frame_count) { |
| 103 | Error err = _initialize(p_device_index, p_frame_count); |
| 104 | ERR_FAIL_COND_V(err, err); |
| 105 | |
| 106 | return OK; |
| 107 | } |
| 108 | |
| 109 | #pragma mark - Residency |
| 110 | |
| 111 | void RenderingDeviceDriverMetal::add_residency_set_to_main_queue(MTL::ResidencySet *p_set) { |
| 112 | } |
| 113 | |
| 114 | void RenderingDeviceDriverMetal::remove_residency_set_to_main_queue(MTL::ResidencySet *p_set) { |
| 115 | } |
| 116 | |
| 117 | #pragma mark - Fences |
| 118 | |
| 119 | RDD::FenceID RenderingDeviceDriverMetal::fence_create() { |
| 120 | Fence *fence = memnew(Fence(NS::TransferPtr(device->newSharedEvent()))); |
| 121 | return FenceID(fence); |
| 122 | } |
| 123 | |
| 124 | Error RenderingDeviceDriverMetal::fence_wait(FenceID p_fence) { |
| 125 | Fence *fence = (Fence *)(p_fence.id); |
| 126 | return fence->wait(2000); |
| 127 | } |
| 128 | |
| 129 | void RenderingDeviceDriverMetal::fence_free(FenceID p_fence) { |
| 130 | Fence *fence = (Fence *)(p_fence.id); |
| 131 | memdelete(fence); |
| 132 | } |
| 133 | |
| 134 | #pragma mark - Semaphores |
| 135 | |
| 136 | RDD::SemaphoreID RenderingDeviceDriverMetal::semaphore_create() { |
| 137 | if (sync_mode != HazardTracking) { |
| 138 | Semaphore *sem = memnew(Semaphore(NS::TransferPtr(device->newEvent()))); |
| 139 | return SemaphoreID(sem); |
| 140 | } |
| 141 | return SemaphoreID(1); |
| 142 | } |
| 143 | |
| 144 | void RenderingDeviceDriverMetal::semaphore_free(SemaphoreID p_semaphore) { |
| 145 | if (sync_mode != HazardTracking) { |
| 146 | Semaphore *sem = (Semaphore *)(p_semaphore.id); |
| 147 | memdelete(sem); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #pragma mark - Command Queues |
| 152 | |
| 153 | RDD::CommandQueueID RenderingDeviceDriverMetal::command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue) { |
| 154 | return CommandQueueID(1); |
| 155 | } |
| 156 | |
| 157 | Error RenderingDeviceDriverMetal::_execute_and_present_barriers(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_sem, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_sem, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) { |
| 158 | uint32_t size = p_cmd_buffers.size(); |
| 159 | if (size == 0) { |
| 160 | return OK; |
| 161 | } |
| 162 | |
| 163 | if (p_wait_sem.size() > 0) { |
| 164 | MTL::CommandBuffer *cb = device_queue->commandBuffer(); |
| 165 | #ifdef DEV_ENABLED |
| 166 | cb->setLabel(MTLSTR("Wait Command Buffer")); |
| 167 | #endif |
| 168 | for (uint32_t i = 0; i < p_wait_sem.size(); i++) { |
| 169 | Semaphore *sem = (Semaphore *)p_wait_sem[i].id; |
| 170 | cb->encodeWait(sem->event.get(), sem->value); |
| 171 | } |
| 172 | cb->commit(); |
| 173 | } |
| 174 | |
| 175 | for (uint32_t i = 0; i < size - 1; i++) { |
| 176 | MDCommandBuffer *cmd_buffer = (MDCommandBuffer *)(p_cmd_buffers[i].id); |
| 177 | cmd_buffer->commit(); |
| 178 | } |
| 179 | |
| 180 | // The last command buffer will signal the fence and semaphores. |
| 181 | MDCommandBuffer *cmd_buffer = (MDCommandBuffer *)(p_cmd_buffers[size - 1].id); |
| 182 | Fence *fence = (Fence *)(p_cmd_fence.id); |
| 183 | if (fence != nullptr) { |
| 184 | cmd_buffer->end(); |
| 185 | MTL::CommandBuffer *cb = cmd_buffer->get_command_buffer(); |
| 186 | fence->signal(cb); |
| 187 | } |
| 188 | |
| 189 | for (uint32_t i = 0; i < p_swap_chains.size(); i++) { |
| 190 | SwapChain *swap_chain = (SwapChain *)(p_swap_chains[i].id); |
| 191 | RenderingContextDriverMetal::Surface *metal_surface = (RenderingContextDriverMetal::Surface *)(swap_chain->surface); |
| 192 | metal_surface->present(cmd_buffer); |
| 193 | } |
| 194 | |
| 195 | cmd_buffer->commit(); |
| 196 | |
| 197 | if (p_cmd_sem.size() > 0) { |
| 198 | MTL::CommandBuffer *cb = device_queue->commandBuffer(); |
| 199 | for (uint32_t i = 0; i < p_cmd_sem.size(); i++) { |
| 200 | Semaphore *sem = (Semaphore *)p_cmd_sem[i].id; |
| 201 | sem->value++; |
| 202 | cb->encodeSignalEvent(sem->event.get(), sem->value); |
| 203 | } |
| 204 | cb->commit(); |
| 205 | } |
| 206 | |
| 207 | return OK; |
| 208 | } |
| 209 | |
| 210 | Error RenderingDeviceDriverMetal::_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_sem, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_sem, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) { |
| 211 | uint32_t size = p_cmd_buffers.size(); |
| 212 | if (size == 0) { |
| 213 | return OK; |
| 214 | } |
| 215 | |
| 216 | for (uint32_t i = 0; i < size - 1; i++) { |
| 217 | MDCommandBuffer *cmd_buffer = (MDCommandBuffer *)(p_cmd_buffers[i].id); |
| 218 | cmd_buffer->commit(); |
| 219 | } |
| 220 | |
| 221 | // The last command buffer will signal the fence and semaphores. |
| 222 | MDCommandBuffer *cmd_buffer = (MDCommandBuffer *)(p_cmd_buffers[size - 1].id); |
| 223 | Fence *fence = (Fence *)(p_cmd_fence.id); |
| 224 | if (fence != nullptr) { |
| 225 | cmd_buffer->end(); |
| 226 | MTL::CommandBuffer *cb = cmd_buffer->get_command_buffer(); |
| 227 | fence->signal(cb); |
| 228 | } |
| 229 | |
| 230 | for (uint32_t i = 0; i < p_swap_chains.size(); i++) { |
| 231 | SwapChain *swap_chain = (SwapChain *)(p_swap_chains[i].id); |
| 232 | RenderingContextDriverMetal::Surface *metal_surface = (RenderingContextDriverMetal::Surface *)(swap_chain->surface); |
| 233 | metal_surface->present(cmd_buffer); |
| 234 | } |
| 235 | |
| 236 | cmd_buffer->commit(); |
| 237 | |
| 238 | return OK; |
| 239 | } |
| 240 | |
| 241 | Error RenderingDeviceDriverMetal::command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_sem, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_sem, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) { |
| 242 | Error res; |
| 243 | if (sync_mode != HazardTracking) { |
| 244 | res = _execute_and_present_barriers(p_cmd_queue, p_wait_sem, p_cmd_buffers, p_cmd_sem, p_cmd_fence, p_swap_chains); |
| 245 | } else { |
| 246 | res = _execute_and_present(p_cmd_queue, p_wait_sem, p_cmd_buffers, p_cmd_sem, p_cmd_fence, p_swap_chains); |
| 247 | } |
| 248 | ERR_FAIL_COND_V(res != OK, res); |
| 249 | |
| 250 | if (p_swap_chains.size() > 0) { |
| 251 | // Used as a signal that we're presenting, so this is the end of a frame. |
| 252 | MTL::CaptureScope *scope = device_scope.get(); |
| 253 | scope->endScope(); |
| 254 | scope->beginScope(); |
| 255 | } |
| 256 | |
| 257 | return OK; |
| 258 | } |
| 259 | |
| 260 | void RenderingDeviceDriverMetal::command_queue_free(CommandQueueID p_cmd_queue) { |
| 261 | } |
| 262 | |
| 263 | #pragma mark - Command Pools |
| 264 | |
| 265 | RDD::CommandPoolID RenderingDeviceDriverMetal::command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) { |
| 266 | DEV_ASSERT(p_cmd_buffer_type == COMMAND_BUFFER_TYPE_PRIMARY); |
| 267 | return CommandPoolID(reinterpret_cast<uint64_t>(device_queue.get())); |
| 268 | } |
| 269 | |
| 270 | bool RenderingDeviceDriverMetal::command_pool_reset(CommandPoolID p_cmd_pool) { |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | void RenderingDeviceDriverMetal::command_pool_free(CommandPoolID p_cmd_pool) { |
| 275 | // Nothing to free - the device_queue is managed by SharedPtr. |
| 276 | } |
| 277 | |
| 278 | #pragma mark - Timestamp |
| 279 | |
| 280 | RDD::QueryPoolID RenderingDeviceDriverMetal::timestamp_query_pool_create(uint32_t p_query_count) { |
| 281 | if (timestamp_queries_supported) { |
| 282 | QueryPool *pool = QueryPool::create(device, p_query_count); |
| 283 | if (pool != nullptr) { |
| 284 | return QueryPoolID(pool); |
| 285 | } |
| 286 | WARN_PRINT_ONCE("Metal 3: The device does not expose the timestamp counter set; GPU timestamps are unavailable."); |
| 287 | timestamp_queries_supported = false; |
| 288 | } |
| 289 | return ::RenderingDeviceDriverMetal::timestamp_query_pool_create(p_query_count); |
| 290 | } |
| 291 | |
| 292 | void RenderingDeviceDriverMetal::timestamp_query_pool_free(QueryPoolID p_pool_id) { |
| 293 | if (!timestamp_queries_supported) { |
| 294 | return ::RenderingDeviceDriverMetal::timestamp_query_pool_free(p_pool_id); |
| 295 | } |
| 296 | QueryPool *pool = (QueryPool *)(p_pool_id.id); |
| 297 | memdelete(pool); |
| 298 | } |
| 299 | |
| 300 | void RenderingDeviceDriverMetal::timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) { |
| 301 | if (!timestamp_queries_supported) { |
| 302 | return ::RenderingDeviceDriverMetal::timestamp_query_pool_get_results(p_pool_id, p_query_count, r_results); |
| 303 | } |
| 304 | QueryPool *pool = (QueryPool *)(p_pool_id.id); |
| 305 | pool->get_results(p_query_count, r_results); |
| 306 | } |
| 307 | |
| 308 | uint64_t RenderingDeviceDriverMetal::timestamp_query_result_to_time(uint64_t p_result) { |
| 309 | return p_result; // Already converted to nanoseconds in get_results. |
| 310 | } |
| 311 | |
| 312 | void RenderingDeviceDriverMetal::command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) { |
| 313 | if (!timestamp_queries_supported) { |
| 314 | return ::RenderingDeviceDriverMetal::command_timestamp_write(p_cmd_buffer, p_pool_id, p_index); |
| 315 | } |
| 316 | MDCommandBuffer *cmd = (MDCommandBuffer *)(p_cmd_buffer.id); |
| 317 | QueryPool *pool = (QueryPool *)(p_pool_id.id); |
| 318 | cmd->timestamp_write(pool, p_index); |
| 319 | } |
| 320 | |
| 321 | #pragma mark - Command Buffers |
| 322 | |
| 323 | RDD::CommandBufferID RenderingDeviceDriverMetal::command_buffer_create(CommandPoolID p_cmd_pool) { |
| 324 | MTL::CommandQueue *queue = reinterpret_cast<MTL::CommandQueue *>(p_cmd_pool.id); |
| 325 | MDCommandBuffer *obj = memnew(MDCommandBuffer(queue, this)); |
| 326 | command_buffers.push_back(obj); |
| 327 | return CommandBufferID(obj); |
| 328 | } |
| 329 | |
| 330 | } // namespace MTL3 |