"""(Triforce, 11-23) The recordings' timestamps through a ring the GPU copies them into (RX_VK_QUERY_RING, gpu/vk/vk_replay.cpp; no game, no GPU). vkGetQueryPoolResults cost about 0.6 ms a frame on AMD's Windows driver on the thread that replays the frame, and with the recorder thread each call is a boundary that waits for it. A slot's next use copies its last use's timestamps into a mapped, host-coherent ring before it resets them; the slot's turn after that reads them from memory. Read from the source: - the ring: made with the pools, host-coherent or not used (an invalidate would be a recorder boundary), the switch; - the copy: in the command buffer just begun (no pass open), before the reset (query commands run in submission order), each value with its availability word, never waiting on the GPU (a use that was never submitted would hang it), behind a transfer-to-host barrier, through vk_work.h (the recorder's list, so the recorder thread keeps it in order); - the read: only what was copied, only when the copying use was submitted, each value's availability checked; the old path kept when there is no ring; the upload command buffer's timestamps as before. python -I -B tests/test_query_ring.py """ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def block(text, marker): start = text.index(marker) end = text.index('{', start) + 1 depth = 1 while depth: depth += (text[end] == '{') - (text[end] == '}') end += 1 return text[start:end] def before(text, *parts): at = -1 for p in parts: i = text.find(p, at + 1) if i < 0: return False at = i return True def main(): vk = ROOT / "gpu/vk" replay = (vk / "vk_replay.cpp").read_text(encoding="utf-8") rec = (vk / "vk_recorder.h").read_text(encoding="utf-8") work = (vk / "vk_work.h").read_text(encoding="utf-8") fails = [] def check(ok, why): if not ok: fails.append(why) check("V(vkCmdCopyQueryPoolResults)" in rec, "the copy is not in the recorder's list of calls") check("#define vkCmdCopyQueryPoolResults(...) (::mkwvk::rec::rx_vkCmdCopyQueryPoolResults(RX_WORK_ON(__VA_ARGS__)))" in work, "vk_work.h does not send the copy through the recorder") make = block(replay, "void query_ring_make() {") check('getenv("RX_VK_QUERY_RING")' in block(replay, "bool query_ring_on() {"), "RX_VK_QUERY_RING is not read") check(before(make, "create_buffer_cached(", "VK_MEMORY_PROPERTY_HOST_COHERENT_BIT", "destroy_buffer(ts_ring, ts_ring_alloc);", "ts_ring_map = "), "the ring is used without host-coherent memory") begin = block(replay, "void gpu_time_begin(Recording& r) {") check(before(begin, "query_ring_make();", "if (ts_ring_map && ts_pending[r.slot]) {", "vkCmdCopyQueryPoolResults(r.cmd, ts_pool,", "VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT", "VK_PIPELINE_STAGE_2_HOST_BIT", "vkCmdPipelineBarrier2(r.cmd, &di);", "ts_ring_copied[r.slot] = true;", "vkCmdResetQueryPool(r.cmd, ts_pool, ts_count() * r.slot, ts_count());"), "the copy is not made before the reset, with availability, behind a host barrier") copy = begin[begin.index("vkCmdCopyQueryPoolResults("):begin.index("VkMemoryBarrier2")] check("WAIT_BIT" not in copy, "the copy waits on the GPU for queries a use that was never submitted would never write") collect = block(replay, "void gpu_time_collect(Recording& r) {") check(before(collect, "if (ts_ring_map) {", "if (!ts_ring_copied[r.slot] || !r.submitted) return;", "ts_ring_copied[r.slot] = false;", "got = got && w[2 * i + 1] != 0;", "} else {", "vkGetQueryPoolResults(dev.device, ts_pool,"), "the ring is read without its copy, its submit or the availability words, or the old read is gone") check(collect.index("ts_up_pool && ts_up_pending[r.slot]") < collect.index("if (ts_ring_map) {"), "the upload command buffer's timestamps are no longer read as before") for f in sorted(vk.glob("*.cpp")): text = f.read_text(encoding="utf-8", errors="replace") if "vkCmdCopyQueryPoolResults(" in text and f.name != "vk_replay.cpp" and '#include "vk_work.h"' not in text: check(False, f.name + " copies query results outside the recorder") if fails: for f in fails: print("FAIL: " + f) raise SystemExit(1) print("query ring: ok") if __name__ == "__main__": main()