From 3bc7564bb03c2d9b3f96d9cc81b5a852f8af2c72 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Tue, 17 Dec 2024 09:42:40 -0800 Subject: [PATCH] vulkan: add vk_device_get_timestamp vk_device_get_timestamp returns the current timestamp for the specified time domain. device can be NULL unless the domain is VK_TIME_DOMAIN_DEVICE_KHR. Part-of: --- src/vulkan/runtime/vk_device.c | 45 ++++++++++++++++++++++++++++++++++ src/vulkan/runtime/vk_device.h | 7 ++++++ 2 files changed, 52 insertions(+) diff --git a/src/vulkan/runtime/vk_device.c b/src/vulkan/runtime/vk_device.c index 60af69b1faf..cab6a798594 100644 --- a/src/vulkan/runtime/vk_device.c +++ b/src/vulkan/runtime/vk_device.c @@ -35,6 +35,7 @@ #include "util/hash_table.h" #include "util/perf/cpu_trace.h" #include "util/ralloc.h" +#include "util/timespec.h" static enum vk_device_timeline_mode get_timeline_mode(struct vk_physical_device *physical_device) @@ -555,6 +556,50 @@ vk_common_DeviceWaitIdle(VkDevice _device) return VK_SUCCESS; } +VkResult +vk_device_get_timestamp(struct vk_device *device, VkTimeDomainKHR domain, + uint64_t *timestamp) +{ + if (domain == VK_TIME_DOMAIN_DEVICE_KHR) { + assert(device && device->get_timestamp); + return device->get_timestamp(device, timestamp); + } + + /* device is not used for host time domains */ +#ifndef _WIN32 + clockid_t clockid; + struct timespec ts; + + switch (domain) { + case VK_TIME_DOMAIN_CLOCK_MONOTONIC_KHR: + clockid = CLOCK_MONOTONIC; + break; + case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_KHR: + /* The "RAW" clocks on Linux are called "FAST" on FreeBSD */ +#if defined(CLOCK_MONOTONIC_RAW) + clockid = CLOCK_MONOTONIC_RAW; + break; +#elif defined(CLOCK_MONOTONIC_FAST) + clockid = CLOCK_MONOTONIC_FAST; + break; +#else + FALLTHROUGH; +#endif + default: + goto fail; + } + + if (clock_gettime(clockid, &ts) < 0) + goto fail; + + *timestamp = (uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; + return VK_SUCCESS; + +fail: +#endif /* _WIN32 */ + return VK_ERROR_FEATURE_NOT_PRESENT; +} + #ifndef _WIN32 uint64_t diff --git a/src/vulkan/runtime/vk_device.h b/src/vulkan/runtime/vk_device.h index 83d41afab0c..48f02dc4acb 100644 --- a/src/vulkan/runtime/vk_device.h +++ b/src/vulkan/runtime/vk_device.h @@ -204,6 +204,9 @@ struct vk_device { */ VkResult (*check_status)(struct vk_device *device); + /* Get the device timestamp in the VK_TIME_DOMAIN_DEVICE_KHR domain */ + VkResult (*get_timestamp)(struct vk_device *device, uint64_t *timestamp); + /** Creates a vk_sync that wraps a memory object * * This is always a one-shot object so it need not track any additional @@ -412,6 +415,10 @@ vk_device_check_status(struct vk_device *device) return result; } +VkResult +vk_device_get_timestamp(struct vk_device *device, VkTimeDomainKHR domain, + uint64_t *timestamp); + #ifndef _WIN32 uint64_t