// Lightweight non-blocking Vulkan GPU timestamp ring helper for live HUD ms telemetry. // Supports optional pause/resume around Sync vkCmdWaitEvents so GPU ms reports active // GPU shader time cleanly without counting NPU wait time inside GPU ms. #pragma once #include #include #include #include "fsr4_pipeline.h" #define WL_TIMER_SLOTS 4u typedef struct { VkQueryPool pool; int enabled; uint32_t slot_used[WL_TIMER_SLOTS]; /* 0 = unused, 1 = single (2 ts), 2 = split (4 ts) */ uint32_t next_slot; uint32_t last_us; } WlGpuTimer; static inline void wl_gpu_timer_init(WlGpuTimer *t, const WlVkDispatch *vk) { memset(t, 0, sizeof *t); if (!vk || !vk->has_timestamps || vk->timestamp_period <= 0.0f || !vk->CreateQueryPool || !vk->CmdResetQueryPool || !vk->CmdWriteTimestamp || !vk->GetQueryPoolResults) { return; } VkQueryPoolCreateInfo ci = { .sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, .queryType = VK_QUERY_TYPE_TIMESTAMP, .queryCount = WL_TIMER_SLOTS * 4u, }; if (vk->CreateQueryPool(vk->device, &ci, NULL, &t->pool) == VK_SUCCESS && t->pool) { t->enabled = 1; } } static inline void wl_gpu_timer_destroy(WlGpuTimer *t, const WlVkDispatch *vk) { if (t && t->pool && vk && vk->DestroyQueryPool) { vk->DestroyQueryPool(vk->device, t->pool, NULL); } if (t) memset(t, 0, sizeof *t); } static inline uint32_t wl_gpu_timer_begin(WlGpuTimer *t, const WlVkDispatch *vk, VkCommandBuffer cb) { if (!t || !t->enabled) return 0u; uint32_t s = t->next_slot; t->next_slot = (t->next_slot + 1u) % WL_TIMER_SLOTS; // Non-blocking read of the oldest completed slot (zero stall) uint32_t mode = t->slot_used[s]; if (mode == 1u) { uint64_t ts[2] = {0, 0}; VkResult r = vk->GetQueryPoolResults(vk->device, t->pool, s * 4u, 2u, sizeof ts, ts, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); if (r == VK_SUCCESS && ts[1] >= ts[0]) { double us = (double)(ts[1] - ts[0]) * (double)vk->timestamp_period * 1e-3; if (us >= 0.0 && us < 500000.0) t->last_us = (uint32_t)(us + 0.5); } } else if (mode == 2u) { uint64_t ts[4] = {0, 0, 0, 0}; VkResult r = vk->GetQueryPoolResults(vk->device, t->pool, s * 4u, 4u, sizeof ts, ts, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); if (r == VK_SUCCESS && ts[1] >= ts[0] && ts[3] >= ts[2]) { double ticks = (double)(ts[1] - ts[0]) + (double)(ts[3] - ts[2]); double us = ticks * (double)vk->timestamp_period * 1e-3; if (us >= 0.0 && us < 500000.0) t->last_us = (uint32_t)(us + 0.5); } } t->slot_used[s] = 0u; vk->CmdResetQueryPool(cb, t->pool, s * 4u, 4u); vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, t->pool, s * 4u); return s; } static inline void wl_gpu_timer_pause(WlGpuTimer *t, const WlVkDispatch *vk, VkCommandBuffer cb, uint32_t slot) { if (!t || !t->enabled) return; vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, t->pool, slot * 4u + 1u); } static inline void wl_gpu_timer_resume(WlGpuTimer *t, const WlVkDispatch *vk, VkCommandBuffer cb, uint32_t slot) { if (!t || !t->enabled) return; vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, t->pool, slot * 4u + 2u); t->slot_used[slot] = 2u; } static inline void wl_gpu_timer_end(WlGpuTimer *t, const WlVkDispatch *vk, VkCommandBuffer cb, uint32_t slot) { if (!t || !t->enabled) return; if (t->slot_used[slot] == 2u) { vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, t->pool, slot * 4u + 3u); } else { vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, t->pool, slot * 4u + 1u); t->slot_used[slot] = 1u; } }