Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

metal: Fix GPU timestamps reading high

Stuart Carnie2aca7ab26bad
diff --git a/drivers/metal/metal3_objects.cpp b/drivers/metal/metal3_objects.cpp
index 7d7a48f48b2..e8db8f6a78c 100644
--- a/drivers/metal/metal3_objects.cpp
+++ b/drivers/metal/metal3_objects.cpp
@@ -319,34 +319,11 @@ QueryPool *QueryPool::create(MTL::Device *p_device, uint32_t p_count) {
QueryPool *pool = memnew(QueryPool);
pool->sample_buffer = sample_buffer;
pool->dummy_buffer = NS::TransferPtr(p_device->newBuffer(1, MTL::ResourceStorageModePrivate | MTL::ResourceHazardTrackingModeUntracked));
- pool->device = p_device;
- p_device->sampleTimestamps(&pool->cpu_base, &pool->gpu_base);
pool->count = p_count;
return pool;
}
-double QueryPool::_resolve_timestamp_to_nano() {
- if (timestamp_to_nano != 0.0) {
- return timestamp_to_nano;
- }
-
- MTL::Timestamp cpu_now = 0;
- MTL::Timestamp gpu_now = 0;
- device->sampleTimestamps(&cpu_now, &gpu_now);
- if (gpu_now <= gpu_base || cpu_now <= cpu_base) {
- return 0.0; // Too soon to correlate; try again on the next readback.
- }
- timestamp_to_nano = (double)(cpu_now - cpu_base) / (double)(gpu_now - gpu_base);
- return timestamp_to_nano;
-}
-
void QueryPool::get_results(uint32_t p_count, uint64_t *r_results) {
- double to_nano = _resolve_timestamp_to_nano();
- if (to_nano == 0.0) {
- memset(r_results, 0, p_count * sizeof(uint64_t));
- return;
- }
-
NS::Data *data = sample_buffer->resolveCounterRange(NS::Range::Make(0, p_count));
ERR_FAIL_NULL(data);
const MTL::CounterResultTimestamp *src = (const MTL::CounterResultTimestamp *)data->bytes();
@@ -354,12 +331,12 @@ void QueryPool::get_results(uint32_t p_count, uint64_t *r_results) {
for (uint32_t i = 0; i < p_count; i++) {
// A slot the GPU never wrote resolves to the error value; hold the previous
// reading so the profiler reports a zero length span rather than garbage.
- last = src[i].timestamp == COUNTER_ERROR_VALUE ? last : uint64_t((double)src[i].timestamp * to_nano);
+ last = src[i].timestamp == COUNTER_ERROR_VALUE ? last : src[i].timestamp;
r_results[i] = last;
}
}
-void MDCommandBuffer::timestamp_write(QueryPool *p_pool, uint32_t p_index) {
+void MDCommandBuffer::timestamp_write(QueryPool *p_pool, uint32_t p_index, MTL::Fence *p_fence) {
end();
NS::SharedPtr<MTL::BlitPassDescriptor> desc = NS::TransferPtr(MTL::BlitPassDescriptor::alloc()->init());
@@ -375,9 +352,11 @@ void MDCommandBuffer::timestamp_write(QueryPool *p_pool, uint32_t p_index) {
enc->setLabel(NS::String::string(vformat("Timestamp %03d", p_index).utf8().get_data(), NS::UTF8StringEncoding));
#endif
_fence_wait(enc);
+ enc->waitForFence(p_fence);
// An encoder with no commands may be elided, which would drop the sample.
enc->fillBuffer(p_pool->get_dummy_buffer(), NS::Range::Make(0, 1), 0);
_fence_update(enc);
+ enc->updateFence(p_fence);
enc->endEncoding();
}
diff --git a/drivers/metal/metal3_objects.h b/drivers/metal/metal3_objects.h
index cf978baa9c1..22ec04be35a 100644
--- a/drivers/metal/metal3_objects.h
+++ b/drivers/metal/metal3_objects.h
@@ -284,21 +284,10 @@ class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) QueryPool {
// A one byte private buffer, filled by the timestamp encoder so it always
// carries at least one command.
NS::SharedPtr<MTL::Buffer> dummy_buffer;
- // The counter sample buffer timestamp domain is not queryable, so it is
- // correlated against the CPU clock. queryTimestampFrequency() describes the
- // Metal 4 counter heap domain instead, and is roughly 33x off here.
- MTL::Device *device = nullptr;
- MTL::Timestamp cpu_base = 0;
- MTL::Timestamp gpu_base = 0;
- double timestamp_to_nano = 0.0;
uint32_t count = 0;
QueryPool() = default;
- // Resolves the GPU to CPU tick ratio on first use, once enough time has
- // elapsed since creation for the correlation to be meaningful.
- double _resolve_timestamp_to_nano();
-
public:
// Returns nullptr if the device does not expose the timestamp counter set.
static QueryPool *create(MTL::Device *p_device, uint32_t p_count);
@@ -638,7 +627,7 @@ public:
#pragma mark - Timestamp
- void timestamp_write(QueryPool *p_pool, uint32_t p_index);
+ void timestamp_write(QueryPool *p_pool, uint32_t p_index, MTL::Fence *p_fence);
#pragma mark - Synchronization
diff --git a/drivers/metal/rendering_device_driver_metal3.cpp b/drivers/metal/rendering_device_driver_metal3.cpp
index 836d78e43bf..d3ec19fa4de 100644
--- a/drivers/metal/rendering_device_driver_metal3.cpp
+++ b/drivers/metal/rendering_device_driver_metal3.cpp
@@ -94,6 +94,9 @@ void RenderingDeviceDriverMetal::_resolve_sync_mode() {
// Apple GPUs only sample counters at stage boundaries, which is all the
// timestamp encoders rely on.
timestamp_queries_supported = device->supportsCounterSampling(MTL::CounterSamplingPointAtStageBoundary);
+ if (timestamp_queries_supported) {
+ timestamp_fence = NS::TransferPtr(device->newFence());
+ }
} else {
print_verbose("Metal 3: Native hazard tracking enabled.");
}
@@ -315,7 +318,7 @@ void RenderingDeviceDriverMetal::command_timestamp_write(CommandBufferID p_cmd_b
}
MDCommandBuffer *cmd = (MDCommandBuffer *)(p_cmd_buffer.id);
QueryPool *pool = (QueryPool *)(p_pool_id.id);
- cmd->timestamp_write(pool, p_index);
+ cmd->timestamp_write(pool, p_index, timestamp_fence.get());
}
#pragma mark - Command Buffers
diff --git a/drivers/metal/rendering_device_driver_metal3.h b/drivers/metal/rendering_device_driver_metal3.h
index 659b5be92e4..64e5857181b 100644
--- a/drivers/metal/rendering_device_driver_metal3.h
+++ b/drivers/metal/rendering_device_driver_metal3.h
@@ -64,6 +64,8 @@ class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingDeviceDriverMet
// GPU timestamps require barrier synchronization, so the level fences order the
// timestamp encoders against the work they bracket.
bool timestamp_queries_supported = false;
+ // Orders each timestamp encoder after the previous one, across command buffers.
+ NS::SharedPtr<MTL::Fence> timestamp_fence;
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);
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);