mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
Verbatim copy of app/src/main/cpp/fsr4npu/ (C sources, headers, shaders, weight pack scripts) from michaelisomer-cyber/Winlator-NeuralScale at a0ab6b0f770e1a6786b4e68def46b4edb8baafd0 (GPL-3.0, fork of StevenMXZ/Winlator-Ludashi). Not built yet; the DroidDeck port follows.
72 lines
2.5 KiB
C
72 lines
2.5 KiB
C
// Lightweight non-blocking Vulkan GPU timestamp ring helper for live HUD ms telemetry.
|
|
#pragma once
|
|
|
|
#include <vulkan/vulkan.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "fsr4_pipeline.h"
|
|
|
|
#define WL_TIMER_SLOTS 4u
|
|
|
|
typedef struct {
|
|
VkQueryPool pool;
|
|
int enabled;
|
|
uint32_t slot_used[WL_TIMER_SLOTS];
|
|
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 * 2u,
|
|
};
|
|
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)
|
|
if (t->slot_used[s]) {
|
|
uint64_t ts[2] = {0, 0};
|
|
VkResult r = vk->GetQueryPoolResults(vk->device, t->pool, s * 2u, 2u,
|
|
sizeof ts, ts, sizeof(uint64_t),
|
|
VK_QUERY_RESULT_64_BIT);
|
|
if (r == VK_SUCCESS && ts[1] >= ts[0]) {
|
|
double ns = (double)(ts[1] - ts[0]) * (double)vk->timestamp_period;
|
|
double us = ns * 1e-3;
|
|
if (us >= 0.0 && us < 500000.0) {
|
|
t->last_us = (uint32_t)(us + 0.5);
|
|
}
|
|
}
|
|
}
|
|
vk->CmdResetQueryPool(cb, t->pool, s * 2u, 2u);
|
|
vk->CmdWriteTimestamp(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, t->pool, s * 2u);
|
|
return s;
|
|
}
|
|
|
|
static inline void wl_gpu_timer_end(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 * 2u + 1u);
|
|
t->slot_used[slot] = 1u;
|
|
}
|