mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-25 18:48:51 -04:00
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
1595 lines
96 KiB
C++
1595 lines
96 KiB
C++
// (hookshot) RX_VK_RECORDER: the command buffer calls as packets, step 1 of a recorder thread (the "sync" phase).
|
|
//
|
|
// Every vkCmd* call vk_work.h routes here is copied into a packet: its value arguments, and every array and struct it points to (the
|
|
// descriptor writes and the image/buffer/texel infos they point to, push constant bytes, vertex buffer arrays, the rendering info and
|
|
// its attachments, the barriers, the copy regions, the dynamic state arrays), so a packet owns all it needs and no longer reads the
|
|
// caller's memory. A packet is run by one decoder (execute: the packet's thunk calls the real ::vkx function with the copies).
|
|
//
|
|
// RX_VK_RECORDER unset / anything else: off, the calls go straight to the driver as before (one relaxed load and a branch a call)
|
|
// RX_VK_RECORDER=sync: each call packed, then run at once on the same thread, in the same order: the same calls with the same
|
|
// values, made from the packet's copies (the proof that the copies are complete and exact)
|
|
// RX_VK_RECORDER=defer: (step 1.5) the packets held in the thread's arena and run at drain(), in order, on the
|
|
// same thread; the caller reuses its memory in between, as it will once a recorder thread runs them. Drained
|
|
// before every call of RX_REC_BOUNDARIES (vk_work.h: command buffer begin/end/reset, submits, presents, waits,
|
|
// query and fault reads, every destruction), when vk_replay.cpp's replay_mu is let go (DrainingMutex), before
|
|
// a fallback, at the soft byte budget, and when the arena has no memory (then the call is made directly).
|
|
// While par_active (a parallel replay's segments), packets run at once; drained at segments_begin.
|
|
// RX_VK_RECORDER_POISON=1: the arena filled with 0xCD after each drain. tests/test_recorder_sync.py
|
|
// RX_VK_RECORDER=thread: (step 2) the thread that holds vk_replay.cpp's replay_mu is the producer: its packets
|
|
// go into one bounded ring (Recorder, below) and a recorder thread runs them, in order, while the replay goes
|
|
// on. Before every call of RX_REC_BOUNDARIES on the producer (but those of unordered()), and when it lets
|
|
// replay_mu go, it waits until
|
|
// the recorder has run everything it put in the ring: outside a replay_mu hold the recorder is idle, and no
|
|
// command buffer, pool or queue call of the replay's is made while its packets are in flight. Threads without
|
|
// replay_mu make their calls directly (after the recorder has run what was published, should it be busy:
|
|
// counted and logged once). The ring full: the producer waits (backpressure); a packet bigger than the room
|
|
// it is given, or no ring: the producer waits until the recorder is idle, then makes the call directly.
|
|
// (Triforce, 11-15) Beside the parallel replay's helper threads too (it was refused with RX_VK_PARALLEL>=2,
|
|
// which only the switch, not the Debug row, refused): their segments' calls are made directly, the
|
|
// producer's too while par_active, after segments_begin has run all it put in the ring (below), so the
|
|
// recorder is idle while any segment records. RX_VK_RECORDER_RING_MB (default 8),
|
|
// RX_VK_RECORDER_POISON=1 (each packet filled with 0xCD once run), RX_VK_RECORDER_STATS=0 (no 10 s stats
|
|
// line), RX_VK_RECORDER_SPIN_US (the recorder's spin with nothing to run, before it sleeps: default adaptive 20-200)
|
|
//
|
|
// A packet: [Packet header: thunk, next, op, size][the call's closure: its value arguments and the arena pointers of its copies],
|
|
// preceded in the arena by the copied arrays. The arena is the recording thread's own (thread_local: the parallel replay's workers each
|
|
// have theirs) and in sync mode is rewound after each packet runs, so it stays at one packet's size. A call whose structs carry a
|
|
// pNext chain (none does today) or a descriptor type the packer does not know is not packed: what is held is drained, then the call
|
|
// is made directly (counted in Arena::fallbacks).
|
|
//
|
|
// Include through vk_work.h, not by itself: the names below are the wrappers its macros call.
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <exception>
|
|
#include <mutex>
|
|
#include <new>
|
|
#include <condition_variable>
|
|
#include <thread>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
|
#include <immintrin.h>
|
|
#endif
|
|
|
|
#include "vk_loader.h"
|
|
#include "lock_census.h" // ((Triforce, 11-22, THREAD-68) who holds the lock a thread waits for, RX_VK_LOCK_CENSUS: DrainingMutex)
|
|
#include "new_wait.h" // ((Triforce, 11-22, OS-04) a nothrow new that must answer at once: NoWait)
|
|
|
|
namespace mkwvk {
|
|
void logf(const char* fmt, ...); // (vk_main.cpp)
|
|
extern std::atomic<bool> par_active; // (vk_replay.cpp: a parallel replay's segments are being recorded)
|
|
} // namespace mkwvk
|
|
|
|
// The calls the recorder carries. V: value arguments only (handles, numbers, enums), one generic packer; A: arrays or structs behind
|
|
// pointers, a packer of their own below. vk_work.h has a macro for each (the test checks the two lists agree).
|
|
#define RX_REC_CALLS(V, A) \
|
|
A(vkCmdPipelineBarrier2) A(vkCmdBeginRendering) V(vkCmdEndRendering) \
|
|
V(vkCmdDraw) V(vkCmdDrawIndexed) V(vkCmdDrawIndirect) V(vkCmdDrawIndexedIndirect) V(vkCmdDrawIndirectCount) \
|
|
V(vkCmdDrawIndexedIndirectCount) V(vkCmdDrawMeshTasksEXT) V(vkCmdDrawMeshTasksIndirectEXT) V(vkCmdDrawMeshTasksIndirectCountEXT) \
|
|
V(vkCmdDispatch) V(vkCmdDispatchIndirect) \
|
|
A(vkCmdCopyBuffer) A(vkCmdCopyBufferToImage) A(vkCmdCopyImageToBuffer) A(vkCmdCopyImage) A(vkCmdBlitImage) A(vkCmdResolveImage) \
|
|
A(vkCmdClearColorImage) A(vkCmdClearDepthStencilImage) A(vkCmdClearAttachments) V(vkCmdFillBuffer) A(vkCmdUpdateBuffer) \
|
|
V(vkCmdBeginQuery) V(vkCmdEndQuery) \
|
|
V(vkCmdBindPipeline) A(vkCmdBindDescriptorSets) V(vkCmdBindIndexBuffer) A(vkCmdBindVertexBuffers2) A(vkCmdPushConstants) \
|
|
A(vkCmdPushDescriptorSetKHR) A(vkCmdPushDescriptorSetWithTemplateKHR) \
|
|
A(vkCmdSetViewport) A(vkCmdSetScissor) A(vkCmdSetViewportWithCount) A(vkCmdSetScissorWithCount) A(vkCmdSetBlendConstants) \
|
|
V(vkCmdSetStencilReference) V(vkCmdSetStencilCompareMask) V(vkCmdSetStencilWriteMask) V(vkCmdSetStencilOp) V(vkCmdSetDepthBias) \
|
|
V(vkCmdSetDepthBiasEnable) V(vkCmdSetLineWidth) V(vkCmdSetDepthBoundsTestEnable) V(vkCmdSetCullMode) V(vkCmdSetFrontFace) \
|
|
V(vkCmdSetPrimitiveTopology) V(vkCmdSetPrimitiveRestartEnable) V(vkCmdSetDepthTestEnable) V(vkCmdSetDepthWriteEnable) \
|
|
V(vkCmdSetDepthCompareOp) V(vkCmdSetStencilTestEnable) V(vkCmdSetRasterizerDiscardEnable) V(vkCmdSetPolygonModeEXT) \
|
|
V(vkCmdSetDepthClampEnableEXT) V(vkCmdSetAlphaToCoverageEnableEXT) \
|
|
A(vkCmdSetColorBlendEnableEXT) A(vkCmdSetColorBlendEquationEXT) A(vkCmdSetColorWriteMaskEXT) \
|
|
A(vkCmdSetFragmentShadingRateKHR) V(vkCmdWriteBufferMarkerAMD) A(vkCmdSetCheckpointNV) V(vkCmdWriteTimestamp2) V(vkCmdResetQueryPool) V(vkCmdCopyQueryPoolResults)
|
|
|
|
// (DEFER) The calls that drain the thread's held packets first (vk_work.h has a macro for each: the test checks the two lists agree,
|
|
// and that every file making one of these calls includes vk_work.h or is on its list of known exceptions). K: a ::vkx function,
|
|
// M: VMA's (global)
|
|
#define RX_REC_BOUNDARIES(K, M) \
|
|
K(vkBeginCommandBuffer) K(vkEndCommandBuffer) K(vkResetCommandPool) K(vkDestroyCommandPool) \
|
|
K(vkQueueSubmit2) K(vkQueueWaitIdle) K(vkQueueBindSparse) K(vkQueuePresentKHR) K(vkAcquireNextImageKHR) K(vkDeviceWaitIdle) \
|
|
K(vkWaitForFences) K(vkResetFences) K(vkWaitSemaphores) K(vkGetSemaphoreCounterValue) \
|
|
K(vkGetQueryPoolResults) K(vkResetQueryPool) K(vkGetQueueCheckpointDataNV) K(vkGetDeviceFaultInfoEXT) K(vkResetDescriptorPool) \
|
|
K(vkDestroyBuffer) K(vkDestroyImage) K(vkDestroyImageView) K(vkDestroyBufferView) K(vkDestroySampler) K(vkDestroyPipeline) \
|
|
K(vkDestroyPipelineLayout) K(vkDestroyDescriptorSetLayout) K(vkDestroyDescriptorPool) K(vkDestroyQueryPool) \
|
|
K(vkDestroyShaderModule) K(vkDestroyPipelineCache) K(vkDestroyFence) K(vkDestroySemaphore) K(vkDestroySwapchainKHR) \
|
|
K(vkDestroySurfaceKHR) K(vkDestroyDevice) K(vkDestroyInstance) K(vkFreeMemory) \
|
|
M(vmaDestroyBuffer) M(vmaDestroyImage) M(vmaInvalidateAllocation) M(vmaFreeMemory)
|
|
|
|
namespace mkwvk::rec {
|
|
|
|
enum Op : uint16_t {
|
|
#define RX_REC_OP(f) OP_##f,
|
|
RX_REC_CALLS(RX_REC_OP, RX_REC_OP)
|
|
#undef RX_REC_OP
|
|
OP_COUNT,
|
|
OP_PART = OP_COUNT // ((Triforce) RX_VK_RECORDER_PARTS: a part's end and submit, post_part; no vkCmd* call)
|
|
};
|
|
inline const char* op_name(uint32_t op) {
|
|
static const char* const names[] = {
|
|
#define RX_REC_NAME(f) #f,
|
|
RX_REC_CALLS(RX_REC_NAME, RX_REC_NAME)
|
|
#undef RX_REC_NAME
|
|
};
|
|
return op < OP_COUNT ? names[op] : op == OP_PART ? "a part's end and submit" : "?";
|
|
}
|
|
|
|
/// (THREAD) where the producer waited for the recorder: each boundary call, then the recorder's own reasons
|
|
enum Bound : uint16_t {
|
|
#define RX_REC_BOUND(f) B_##f,
|
|
RX_REC_BOUNDARIES(RX_REC_BOUND, RX_REC_BOUND)
|
|
#undef RX_REC_BOUND
|
|
B_REPLAY_UNLOCK, // replay_mu let go
|
|
B_FALLBACK, // a call not packed (pNext chain, unknown descriptor type)
|
|
B_NOMEM, // a packet bigger than the ring's room for it, or no ring
|
|
B_PAR, // a parallel replay's segments being recorded (par_active)
|
|
B_RING_FULL, // backpressure: the ring had no room
|
|
B_FOREIGN, // a thread without replay_mu made a call while the recorder was busy
|
|
B_DIRECT, // code about to record into a command buffer directly, outside vk_work.h (Dear ImGui: vk_menu.cpp)
|
|
B_PART, // ((Triforce) parts) a part's submit: its slot's reuse, a wait for its recording (vk_replay.cpp)
|
|
B_QUEUE_LOCK, // ((Triforce) parts) the queue's lock about to be taken: the parts handed over submitted first (QueueMutex)
|
|
B_COUNT
|
|
};
|
|
inline const char* bound_name(uint32_t b) {
|
|
static const char* const names[] = {
|
|
#define RX_REC_BNAME(f) #f,
|
|
RX_REC_BOUNDARIES(RX_REC_BNAME, RX_REC_BNAME)
|
|
#undef RX_REC_BNAME
|
|
"replay_mu unlock", "fallback", "no ring room", "par_active", "ring full", "off the replay lock", "direct recording",
|
|
"a part's submit", "the queue's lock"};
|
|
static_assert(sizeof names / sizeof *names == B_COUNT);
|
|
return b < B_COUNT ? names[b] : "?";
|
|
}
|
|
|
|
enum Mode : int { OFF = 0, SYNC = 1, DEFER = 2, THREAD = 3, PASS = 4 /* (an encoder's own: the call made directly) */ };
|
|
inline std::atomic<int> g_mode{-1};
|
|
inline std::atomic<bool> g_poison{false}; // (RX_VK_RECORDER_POISON=1: the arena filled with 0xCD after each drain)
|
|
/// (DEFER) the bytes held before the next packet drains them first (soft), and the arena's most (hard: past it a call is not
|
|
/// packed: what is held is drained, then the call made directly). Tests change them; the renderer does not
|
|
inline size_t g_soft_budget = size_t(8) << 20, g_hard_budget = size_t(64) << 20;
|
|
/// (THREAD) the ring's bytes (a power of two; RX_VK_RECORDER_RING_MB) and the room each packet is given (a vkCmdUpdateBuffer of
|
|
/// 64 KB fits; a bigger packet is made directly, after the recorder is idle). Read when the recorder starts: tests change them
|
|
inline size_t g_ring_bytes = size_t(8) << 20, g_ring_room = size_t(80) << 10;
|
|
/// (THREAD) the bytes of packets the producer writes before it publishes them (W): fewer stores to the line the recorder polls.
|
|
/// Everything is published before any wait, so a boundary still waits for every packet before it. Tests change it
|
|
inline size_t g_publish_bytes = 1024;
|
|
/// (THREAD) how long the recorder spins with nothing to run before it sleeps. Spinning keeps it awake through the replay's short gaps
|
|
/// (no wake-up call on the replay thread, no wake-up delay at its next wait); sleeping gives the core back. 0 (the default):
|
|
/// adaptive, between 20 and 200 us: back to 200 when a sleep ended within 200 us (a gap the spin would have covered: the replay paid
|
|
/// a wake-up for it), halved when a sleep lasted longer (a long gap: the spin was wasted). RX_VK_RECORDER_SPIN_US=<us>: fixed.
|
|
/// Read when the recorder starts: tests change it
|
|
inline uint64_t g_spin_ns = 0;
|
|
constexpr uint64_t kSpinLo = 20000, kSpinHi = 200000;
|
|
inline bool g_stats_log = true; // (RX_VK_RECORDER_STATS=0: no stats line every 10 s)
|
|
/// ((Triforce) RX_VK_RECORDER_IDLE=wait, opt-in) with nothing to run the recorder spins 20 us, then waits on W itself
|
|
/// (WaitOnAddress; the futex under host/posix's on Linux), and a publish wakes it; unset: the spin and a condition variable, as
|
|
/// before. Read when the mode is; off again at the recorder's start without the two functions below: tests change it
|
|
inline bool g_idle_wait = false;
|
|
/// (set by vk_replay.cpp: WaitOnAddress and WakeByAddressSingle, which need windows.h; tests set fakes)
|
|
inline bool (*g_wait_on_address)(volatile void* addr, void* compare, size_t size, uint32_t ms) = nullptr;
|
|
inline void (*g_wake_by_address)(void* addr) = nullptr;
|
|
// ((Triforce) RX_VK_RECORDER_PARTS: the hand-off by part, Threaded rendering's recorder (threaded_rendering.h). The producer
|
|
// publishes a part's packets when the part ends (a long part in g_chunk_bytes chunks), not packet by packet, and the part's two
|
|
// vkEndCommandBuffer and its queue submit go into the ring as one more packet (post_part), which the recorder runs in order: the
|
|
// producer goes on into the next part at once. What needs a part submitted waits for that (parts_done, wait_part, settle_parts),
|
|
// not for the ring to empty; replay_mu's unlock publishes and does not wait; the recorder waits on W between batches. A thread
|
|
// about to take the queue's lock (QueueMutex, vk_internal.h) waits for the parts handed over first; one that holds a queue lock
|
|
// never waits for a recorder that waits for that lock (queue_escaped). Unset: on with Threaded rendering (g_parts_default), so
|
|
// off by default; =1 on with any recorder thread, =0 off.
|
|
/// Set by mode_init from the switch; false until then, so the checks that set a mode themselves keep a boundary's drain
|
|
inline std::atomic<bool> g_parts{false};
|
|
/// (vk_replay.cpp: Threaded rendering's say with RX_VK_RECORDER_PARTS unset; null: off)
|
|
inline bool (*g_parts_default)() = nullptr;
|
|
/// (parts) a part's packets published in chunks of this many bytes while it is translated (a part of 512 draws is ~100-200 KB:
|
|
/// most go over at their end, in one batch; a longer one is begun on by the recorder before its end). Tests change it
|
|
inline size_t g_chunk_bytes = size_t(256) << 10;
|
|
constexpr uint64_t kPartsSpin = 10000; // (parts: the recorder's spin with nothing to run before it waits on W, ns)
|
|
|
|
/// RX_VK_RECORDER: sync, defer, thread; anything else, or unset, off. (Triforce, 11-15) defer and thread are no longer refused with
|
|
/// RX_VK_PARALLEL>=2: a parallel replay's segments make their calls directly, after segments_begin (tests/test_recorder_sync.py)
|
|
/// (Triforce, 11-16) Unset: thread on an NVIDIA card with 12 logical processors or more (set by vk_main.cpp once the device is
|
|
/// known, before any command is recorded), else off. Lab, his settings (1440p, RTX 4070 SUPER, i7-13700K): Market +11%, Hyrule
|
|
/// Field +6.4%, no flicker; at the default Auto resolution no change; with the game held to 8 logical processors -10% (the
|
|
/// recorder needs a core of its own). NX2navi has its recorder on by default on NVIDIA too. RX_VK_RECORDER=off: off
|
|
inline bool g_auto_thread = false;
|
|
inline int mode_from_env() {
|
|
const char* e = getenv("RX_VK_RECORDER");
|
|
if (!e) return g_auto_thread ? THREAD : OFF;
|
|
if (!strcmp(e, "sync")) return SYNC;
|
|
if (!strcmp(e, "defer")) return DEFER;
|
|
if (!strcmp(e, "thread")) return THREAD;
|
|
return OFF;
|
|
}
|
|
inline int mode_init() {
|
|
const int m = mode_from_env();
|
|
const char* p = getenv("RX_VK_RECORDER_POISON");
|
|
g_poison.store((m == DEFER || m == THREAD) && p && atoi(p) != 0, std::memory_order_relaxed);
|
|
if (m == THREAD) {
|
|
if (const char* mb = getenv("RX_VK_RECORDER_RING_MB"))
|
|
if (atoi(mb) > 0) g_ring_bytes = size_t(std::min(atoi(mb), 1024)) << 20;
|
|
if (const char* s = getenv("RX_VK_RECORDER_STATS")) g_stats_log = atoi(s) != 0;
|
|
if (const char* s = getenv("RX_VK_RECORDER_SPIN_US")) g_spin_ns = (uint64_t)std::max(1, atoi(s)) * 1000;
|
|
const char* idle = getenv("RX_VK_RECORDER_IDLE"); // ((Triforce) opt-in)
|
|
g_idle_wait = idle && !strcmp(idle, "wait");
|
|
const char* pe = getenv("RX_VK_RECORDER_PARTS"); // ((Triforce) unset: Threaded rendering's say)
|
|
g_parts.store(pe && *pe ? atoi(pe) != 0 : g_parts_default && g_parts_default(), std::memory_order_relaxed);
|
|
}
|
|
int was = -1;
|
|
if (!g_mode.compare_exchange_strong(was, m, std::memory_order_relaxed)) return was; // (another thread read it first)
|
|
if (m == DEFER) // (once: the thread that set the mode)
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=defer: vkCmd* packed and held, drained in order at each command buffer begin, end and "
|
|
"reset, submit, wait, readback, destruction and replay lock release (a parallel replay's segments: made "
|
|
"directly)%s",
|
|
g_poison.load(std::memory_order_relaxed) ? "; the arena poisoned after each drain" : "");
|
|
else if (m == THREAD && g_parts.load(std::memory_order_relaxed))
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: vkCmd* packed on the replay thread and run in order by a recorder thread, "
|
|
"handed over by part (RX_VK_RECORDER_PARTS): a part's packets at its end, and its end and submit made by "
|
|
"the recorder; the replay waits for it at a reset, begin, destruction or direct recording, and for a part's "
|
|
"submit where a wait, a fence or the queue needs it; ring %zu KB, the recorder waits on the ring after %llu us%s",
|
|
g_ring_bytes >> 10, (unsigned long long)(kPartsSpin / 1000),
|
|
g_poison.load(std::memory_order_relaxed) ? "; packets poisoned once run" : "");
|
|
else if (m == THREAD)
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: vkCmd* packed on the replay thread and run in order by a recorder thread; the "
|
|
"replay waits for it at each command buffer begin, end and reset, submit, wait, readback, destruction and "
|
|
"replay lock release (a parallel replay's segments: made directly, the recorder idle); ring %zu KB, spin "
|
|
"%s%llu us%s",
|
|
g_ring_bytes >> 10, g_spin_ns ? "" : "adaptive 20-", (unsigned long long)(g_spin_ns ? g_spin_ns / 1000 : 200),
|
|
g_poison.load(std::memory_order_relaxed) ? "; packets poisoned once run" : "");
|
|
return m;
|
|
}
|
|
inline int mode() {
|
|
const int m = g_mode.load(std::memory_order_relaxed);
|
|
return m >= 0 ? m : mode_init();
|
|
}
|
|
inline bool on() { return mode() != OFF; }
|
|
|
|
struct alignas(16) Packet {
|
|
void (*run)(const Packet*); // the decoder for this packet's call
|
|
Packet* next; // (DEFER) the next held packet
|
|
uint32_t op; // Op, for diagnostics
|
|
uint32_t size; // bytes of the closure after the header
|
|
};
|
|
|
|
/// The thread's packets. Chunks in a fixed table, each from the non-throwing aligned new: alloc never throws and never loses a
|
|
/// block. It returns null when the table is full, the hard budget would be passed, or the system has no memory; the encoder then
|
|
/// drains what is held (in order) and makes the call directly
|
|
struct Arena {
|
|
static constexpr size_t kChunk = 1u << 20;
|
|
static constexpr size_t kMaxChunks = 64;
|
|
struct Chunk {
|
|
uint8_t* p;
|
|
size_t cap;
|
|
};
|
|
struct Mark {
|
|
size_t ci, used, held;
|
|
};
|
|
Chunk chunks[kMaxChunks] = {};
|
|
size_t nchunks = 0, capacity = 0; // (the chunks made, and their bytes)
|
|
size_t ci = 0, used = 0; // the chunk being filled and its bytes used
|
|
size_t held = 0; // (DEFER) bytes encoded since the last drain
|
|
Packet* head = nullptr; // (DEFER) the packets not run yet, in order
|
|
Packet** tail = &head;
|
|
uint64_t packets = 0, bytes = 0, fallbacks = 0, failures = 0, budget_drains = 0;
|
|
|
|
void* alloc(size_t n, size_t align) noexcept {
|
|
for (;;) {
|
|
if (ci < nchunks) {
|
|
const size_t at = (used + align - 1) & ~(align - 1);
|
|
if (at + n <= chunks[ci].cap) {
|
|
used = at + n;
|
|
bytes += n, held += n;
|
|
return chunks[ci].p + at;
|
|
}
|
|
if (ci + 1 < nchunks || used) { // (the next chunk, or a new one; a chunk too small even empty is passed)
|
|
ci++, used = 0;
|
|
continue;
|
|
}
|
|
}
|
|
const size_t cap = n + align > kChunk ? n + align : kChunk;
|
|
if (nchunks == kMaxChunks || cap > g_hard_budget || capacity > g_hard_budget - cap) return nullptr;
|
|
new_wait::NoWait at_once; // ((Triforce, OS-04) no chunk: the call is made directly; not a wait at the commit limit)
|
|
void* p = ::operator new(cap, std::align_val_t(64), std::nothrow);
|
|
if (!p) return nullptr;
|
|
chunks[nchunks++] = {static_cast<uint8_t*>(p), cap};
|
|
capacity += cap;
|
|
ci = nchunks - 1, used = 0;
|
|
}
|
|
}
|
|
Mark mark() const { return {ci, used, held}; }
|
|
void rewind(Mark m) { ci = m.ci, used = m.used, held = m.held; }
|
|
void poison() { // (the bytes used since the last drain: the chunks before ci whole, ci up to used)
|
|
for (size_t i = 0; i < nchunks && i <= ci; i++) std::memset(chunks[i].p, 0xCD, i < ci ? chunks[i].cap : used);
|
|
}
|
|
~Arena();
|
|
};
|
|
// Function-local ownership keeps the nontrivial TLS initializer with its use under COFF ThinLTO.
|
|
// The arena's self-referential tail prevents constant initialization; its destructor still drains and frees it.
|
|
inline Arena& t_arena() {
|
|
static thread_local Arena arena;
|
|
return arena;
|
|
}
|
|
|
|
/// The decoder: runs one packet's call with its copies
|
|
inline void execute(const Packet* p) { p->run(p); }
|
|
|
|
/// (DEFER) runs every held packet, in order, and empties the arena
|
|
inline void drain() {
|
|
Arena& a = t_arena();
|
|
const Packet* p = a.head;
|
|
a.head = nullptr, a.tail = &a.head;
|
|
while (p) {
|
|
const Packet* next = p->next;
|
|
execute(p);
|
|
p = next;
|
|
}
|
|
if (g_poison.load(std::memory_order_relaxed)) a.poison();
|
|
a.ci = 0, a.used = 0, a.held = 0;
|
|
}
|
|
/// (a thread that ends with packets held, which the drains leave none of: they are run, never dropped)
|
|
inline Arena::~Arena() {
|
|
if (head) drain();
|
|
for (size_t i = 0; i < nchunks; i++) ::operator delete(chunks[i].p, std::align_val_t(64));
|
|
}
|
|
|
|
// ---- (THREAD) the ring and the recorder thread ----
|
|
// One ring, one recorder thread (started at the first packet, joined by stop()). The producer is whichever thread holds replay_mu
|
|
// (DrainingMutex sets t_prod): one at a time, handed on by the mutex, so the ring has one producer and one consumer. Byte counters:
|
|
// W, what the producer published; R, what the recorder has run (every packet before R has reached the driver). A packet is
|
|
// [Packet: run, next = its end, op, size = its closure's offset][copied arrays][closure], contiguous, 16-aligned; one never wraps:
|
|
// the producer gives each packet g_ring_room contiguous bytes (a wrap marker, run = null, ends the ring's tail when less is left,
|
|
// and a tail too small for a header is passed over by both sides). The memory never moves: the packets' pointers into it hold
|
|
inline void cpu_pause() {
|
|
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
|
_mm_pause();
|
|
#endif
|
|
}
|
|
inline uint64_t now_ns() {
|
|
return (uint64_t)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
|
}
|
|
/// (set by vk_replay.cpp: the recorder thread's name and priority, which need windows.h)
|
|
inline void (*g_thread_start_hook)() = nullptr;
|
|
/// (set by vk_replay.cpp: its end_process, which on Windows ends the process without running every library's detach)
|
|
inline void (*g_end_process_hook)(int) = nullptr;
|
|
/// (set by vk_replay.cpp, lab: RX_VK_LAB_AB's knob norec) true: this hold of the replay's lock makes its calls directly, with no
|
|
/// producer (DrainingMutex::enter), so one run measures the recorder thread against none in its phases
|
|
inline bool (*g_direct_hook)() = nullptr;
|
|
#ifdef RX_REC_TEST_HOOKS
|
|
inline void (*g_test_idle_locked)() = nullptr; // (tests: called in idle() with sleep_mu held)
|
|
#endif
|
|
/// The recorder thread cannot go on (an exception out of a driver call): logged, written to stderr and the process ended, as
|
|
/// vk_replay.cpp's replay_fatal does (exit code 3). Never swallowed: a packet not run is a command lost
|
|
[[noreturn]] inline void recorder_fatal(const char* why) {
|
|
::mkwvk::logf("vk: FATAL: RX_VK_RECORDER=thread: the recorder thread: %s", why);
|
|
std::fprintf(stderr, "vk: FATAL: RX_VK_RECORDER=thread: the recorder thread: %s\n", why);
|
|
std::fflush(stderr);
|
|
if (g_end_process_hook) g_end_process_hook(3);
|
|
std::_Exit(3);
|
|
}
|
|
|
|
/// ((Triforce) parts) the recorder thread itself: the calls a part's packet makes (its ends, the queue's lock, its submit) are
|
|
/// the ring's own, made in its order, and wait for nothing (boundary(), Enc, settle)
|
|
inline constinit thread_local bool t_recorder = false;
|
|
/// ((Triforce) parts) the queue locks this thread holds (QueueMutex, vk_internal.h: dev.present_mu and the present queue's lock).
|
|
/// The recorder takes dev.present_mu for a part's submit: a thread holding one of them never waits for a recorder stopped at that
|
|
/// lock (queue_escaped)
|
|
inline constinit thread_local int t_queue_held = 0;
|
|
/// (parts) of those, dev.present_mu (the one the recorder takes); and whether this thread, holding it, is blocked on another queue
|
|
/// lock: a holder of that one may then not wait for the recorder either (Recorder::chain_waiters)
|
|
inline constinit thread_local int t_present_held = 0;
|
|
inline constinit thread_local bool t_chain_wait = false;
|
|
/// (parts) Settled scopes: this thread has waited for what it needs of the recorder itself (a slot's own part, a recording's
|
|
/// submit) or needs none (the present thread): its waits and the queue's lock do not wait for every part
|
|
inline constinit thread_local int t_settled = 0;
|
|
struct Recorder;
|
|
/// (THREAD) the ring, while this thread holds replay_mu; null otherwise
|
|
inline constinit thread_local Recorder* t_prod = nullptr;
|
|
|
|
struct Recorder {
|
|
alignas(64) std::atomic<uint64_t> W{0}; // bytes published (the producer)
|
|
alignas(64) std::atomic<uint64_t> R{0}; // bytes run (the recorder)
|
|
alignas(64) std::atomic<uint64_t> parts_sent{0}; // ((Triforce) parts) parts posted (the producer: post_part)
|
|
alignas(64) std::atomic<uint64_t> parts_done{0}; // (parts) parts ended and submitted (the recorder), in order: after R
|
|
std::atomic<bool> at_queue{false}; // (parts) the recorder blocked on dev.present_mu (held by another), at a part's submit
|
|
std::atomic<int> chain_waiters{0}; // (parts) holders of dev.present_mu blocked on another queue lock (t_chain_wait)
|
|
// (parts) a blocking wait for the parts ended unmet: the part with this number (the waiter's own, or the newest when it gave
|
|
// up), once submitted, sends what is held (take_flush_owed); 0: none owed
|
|
std::atomic<uint64_t> flush_owed_upto{0};
|
|
std::atomic<int> part_waiters{0}; // (parts) threads asleep in wait_until or spin
|
|
std::mutex part_mu;
|
|
std::condition_variable part_cv;
|
|
alignas(64) std::atomic<bool> sleeping{false};
|
|
std::atomic<bool> stop_req{false};
|
|
std::atomic<bool> ready{false}; // ring made, thread running
|
|
std::atomic<bool> broken{false}; // no ring or no thread: the calls made directly
|
|
std::atomic<bool> logged_direct{false}, logged_foreign{false}, logged_escape{false};
|
|
uint8_t* base = nullptr;
|
|
size_t cap = 0, mask = 0, room = 0;
|
|
// (the producer's: its cursor, what of it is published (W), R as it last read it, and how much it writes before publishing)
|
|
alignas(64) uint64_t w = 0, w_pub = 0, r_seen = 0, batch = 0;
|
|
uint64_t spin_limit = 0; // (the recorder's: g_spin_ns when it started)
|
|
// ((Triforce) set at start: RX_VK_RECORDER_PARTS, the bytes a part publishes at most before its end, and whether idle() waits
|
|
// on W: RX_VK_RECORDER_IDLE=wait, or the hand-off by part where WaitOnAddress is there)
|
|
bool parts = false, idle_on_w = false;
|
|
uint64_t chunk = 0;
|
|
// (parts, stats: publications, parts ended and submitted, waits given up under a queue lock, the recorder's time in parts'
|
|
// packets and blocked on the queue's lock)
|
|
std::atomic<uint64_t> batches{0}, parts_run{0}, escapes{0}, part_ns{0}, lock_ns{0}, lock_n{0};
|
|
uint64_t lock_t0 = 0;
|
|
// (stats: written by the producer or the recorder, read by the recorder's stats line; never on a packet's path)
|
|
std::atomic<uint64_t> wait_n[B_COUNT]{}, wait_ns[B_COUNT]{}, wait_max[B_COUNT]{};
|
|
std::atomic<uint64_t> spin_ns{0}, sleeps{0}, short_sleeps{0}; // (the recorder idle: time spun, sleeps, those under 200 us)
|
|
std::atomic<uint64_t> packets{0}, busy_ns{0}, starts{0}, direct{0}; // (direct: packets that did not fit, made directly)
|
|
// (Triforce, 11-15) the same as the stats line's, never reset: the frame statistics take a frame's share (totals())
|
|
std::atomic<uint64_t> all_packets{0}, all_busy_ns{0}, all_waits{0}, all_wait_ns{0};
|
|
std::mutex life_mu, sleep_mu;
|
|
std::condition_variable sleep_cv;
|
|
std::thread th;
|
|
|
|
bool start();
|
|
void stop();
|
|
void main();
|
|
void loop();
|
|
bool idle(uint64_t r, uint64_t& seen, uint64_t& last_log);
|
|
void log_stats(double secs);
|
|
|
|
void wake() {
|
|
if (idle_on_w) { // ((Triforce) RX_VK_RECORDER_IDLE=wait, or parts: idle() waits on W)
|
|
g_wake_by_address(&W);
|
|
return;
|
|
}
|
|
{ std::lock_guard lk(sleep_mu); }
|
|
sleep_cv.notify_one();
|
|
}
|
|
/// ((Triforce) parts) a thread holding a queue lock while the recorder waits for dev.present_mu at a part's submit: it does not
|
|
/// wait for the recorder, or neither would go on. Its own queue work was ordered after every part handed over before it took
|
|
/// the lock (QueueMutex: settle_parts); what is past the recorder's place now was published after that
|
|
bool queue_escaped() const {
|
|
if (!t_queue_held || !at_queue.load(std::memory_order_seq_cst)) return false;
|
|
return t_present_held || chain_waiters.load(std::memory_order_seq_cst) > 0; // (the recorder waits for this thread)
|
|
}
|
|
void note_escape(Bound k) {
|
|
escapes.fetch_add(1, std::memory_order_relaxed);
|
|
if (!logged_escape.exchange(true, std::memory_order_relaxed))
|
|
::mkwvk::logf("vk: RX_VK_RECORDER_PARTS: a wait for the recorder at %s, under the queue's lock, while the recorder waited "
|
|
"for that lock for a part's submit: not waited for (counted; logged once)",
|
|
bound_name(k));
|
|
}
|
|
void kick() { // (the recorder asleep with work published: a wake-up the packet's cheap check may have missed)
|
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
|
if (sleeping.load(std::memory_order_relaxed)) wake();
|
|
}
|
|
/// Waits until done(): pauses, then yields. ((Triforce) parts: asleep on part_cv after the pauses, woken at each part's
|
|
/// submit, when the recorder runs out of work, when it blocks at the queue's lock; 0.2 ms at most: a part's submit can wait
|
|
/// behind a present for milliseconds, and a thread yielding through that keeps its core busy)
|
|
template <class C>
|
|
void spin(C done) {
|
|
kick();
|
|
for (uint32_t i = 0; !done(); i++) {
|
|
if (i < 4096) cpu_pause();
|
|
else if (parts) {
|
|
part_waiters.fetch_add(1, std::memory_order_seq_cst);
|
|
{
|
|
std::unique_lock lk(part_mu);
|
|
while (!done()) {
|
|
kick();
|
|
part_cv.wait_for(lk, std::chrono::microseconds(200));
|
|
}
|
|
}
|
|
part_waiters.fetch_sub(1, std::memory_order_relaxed);
|
|
return;
|
|
} else {
|
|
std::this_thread::yield();
|
|
if ((i & 255) == 0) kick();
|
|
}
|
|
}
|
|
}
|
|
bool idle_at(uint64_t target) const { return R.load(std::memory_order_acquire) >= target; }
|
|
void note_wait(Bound k, uint64_t ns) { // (a wait's count, time and longest, for the stats line)
|
|
wait_n[k].fetch_add(1, std::memory_order_relaxed), wait_ns[k].fetch_add(ns, std::memory_order_relaxed);
|
|
all_waits.fetch_add(1, std::memory_order_relaxed), all_wait_ns.fetch_add(ns, std::memory_order_relaxed);
|
|
uint64_t m = wait_max[k].load(std::memory_order_relaxed);
|
|
while (ns > m && !wait_max[k].compare_exchange_weak(m, ns, std::memory_order_relaxed)) {}
|
|
}
|
|
/// (the producer) what it has written, published
|
|
void flush() {
|
|
if (w == w_pub) return;
|
|
W.store(w, std::memory_order_release);
|
|
w_pub = w;
|
|
batches.fetch_add(1, std::memory_order_relaxed);
|
|
// ((Triforce) parts: the recorder sleeps between batches, and a wake-up missed is a part late by the sleep's 2 ms: W's store
|
|
// before the look at sleeping, as idle() sets sleeping before its look at W)
|
|
if (parts) std::atomic_thread_fence(std::memory_order_seq_cst);
|
|
if (sleeping.load(std::memory_order_relaxed)) [[unlikely]]
|
|
wake();
|
|
}
|
|
/// (the producer) everything it wrote has reached the driver
|
|
void wait_idle(Bound k) {
|
|
flush();
|
|
const uint64_t target = w;
|
|
if (idle_at(target)) return;
|
|
const uint64_t t0 = now_ns();
|
|
spin([&] { return idle_at(target) || queue_escaped(); });
|
|
note_wait(k, now_ns() - t0);
|
|
if (!idle_at(target)) note_escape(k);
|
|
}
|
|
/// (a thread without replay_mu) what was published so far has reached the driver: its direct call comes after it. Idle in
|
|
/// the replay's design without RX_VK_RECORDER_PARTS (replay_mu's unlock waits); otherwise counted, logged once
|
|
void foreign_wait(Bound k = B_FOREIGN) {
|
|
const uint64_t target = W.load(std::memory_order_acquire);
|
|
if (idle_at(target)) return;
|
|
if (!logged_foreign.exchange(true, std::memory_order_relaxed) && !parts)
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: a command buffer or recorded call on a thread without the replay lock while the "
|
|
"recorder was busy: made after the recorder caught up (counted in the stats line; logged once)");
|
|
const uint64_t t0 = now_ns();
|
|
spin([&] { return idle_at(target) || queue_escaped(); });
|
|
note_wait(k, now_ns() - t0);
|
|
if (!idle_at(target)) note_escape(k);
|
|
}
|
|
/// ((Triforce) parts) wakes the threads asleep in wait_until or spin (a part submitted, or the recorder at the queue's lock)
|
|
void notify_parts() {
|
|
if (!part_waiters.load(std::memory_order_seq_cst)) return;
|
|
{ std::lock_guard lk(part_mu); }
|
|
part_cv.notify_all();
|
|
}
|
|
/// (parts, any thread) waits until done(), at most timeout_ns: a short spin, then asleep on part_cv (woken at each part's
|
|
/// submit, 0.5 ms at most). A thread holding a queue lock stops waiting when the recorder waits for it (queue_escaped).
|
|
/// Whether done(). Unmet (the time up, or the escape) with a time to wait (not a poll: polls come many times a frame, and
|
|
/// each would send the held batch), part `owe` (0: the newest handed over) owes a flush of what is held once submitted
|
|
/// (flush_owed_upto), as the caller's own flush before its driver wait may not find it held yet
|
|
template <class P>
|
|
bool wait_until(P done, Bound k, uint64_t timeout_ns = UINT64_MAX, uint64_t owe = 0) {
|
|
if (done()) return true;
|
|
if (t_prod == this) flush(); // (what it waits for, published)
|
|
const uint64_t t0 = now_ns();
|
|
const uint64_t until = timeout_ns > UINT64_MAX - t0 ? UINT64_MAX : t0 + timeout_ns;
|
|
bool ok = false;
|
|
const auto over = [&] { return queue_escaped() || now_ns() >= until; };
|
|
for (uint32_t i = 0; i < 2048 && !(ok = done()) && !((i & 63) == 63 && over()); i++) cpu_pause();
|
|
if (!ok && !over()) {
|
|
part_waiters.fetch_add(1, std::memory_order_seq_cst);
|
|
{
|
|
std::unique_lock lk(part_mu);
|
|
while (!(ok = done()) && !over()) {
|
|
kick();
|
|
part_cv.wait_for(lk, std::chrono::microseconds(500));
|
|
}
|
|
}
|
|
part_waiters.fetch_sub(1, std::memory_order_relaxed);
|
|
}
|
|
if (timeout_ns) note_wait(k, now_ns() - t0);
|
|
if (!ok) ok = done();
|
|
if (!ok) {
|
|
if (timeout_ns) {
|
|
const uint64_t want = owe ? owe : parts_sent.load(std::memory_order_acquire);
|
|
uint64_t was = flush_owed_upto.load(std::memory_order_relaxed);
|
|
while (want > was && !flush_owed_upto.compare_exchange_weak(was, want, std::memory_order_acq_rel)) {}
|
|
}
|
|
if (queue_escaped()) note_escape(k);
|
|
}
|
|
return ok;
|
|
}
|
|
/// (parts) part `t` (post_part's number; 0: none) ended and submitted by the recorder, waited for at most timeout_ns
|
|
bool wait_part(uint64_t t, Bound k = B_PART, uint64_t timeout_ns = UINT64_MAX) {
|
|
return !t || wait_until([&] { return parts_done.load(std::memory_order_seq_cst) >= t; }, k, timeout_ns, t);
|
|
}
|
|
/// (parts) every part handed over so far submitted: what a wait, a fence or the queue's next use may need
|
|
bool settle_parts(Bound k, uint64_t timeout_ns = UINT64_MAX) {
|
|
return wait_part(parts_sent.load(std::memory_order_acquire), k, timeout_ns);
|
|
}
|
|
/// (the producer) `need` contiguous-or-not free bytes: waits for the recorder (backpressure) when the ring has less
|
|
void wait_room(uint64_t need) {
|
|
if (cap - (w - r_seen) >= need) return;
|
|
r_seen = R.load(std::memory_order_acquire);
|
|
if (cap - (w - r_seen) >= need) return;
|
|
flush(); // (what the recorder is to make room by running)
|
|
const uint64_t t0 = now_ns();
|
|
spin([&] { return cap - (w - (r_seen = R.load(std::memory_order_acquire))) >= need; });
|
|
note_wait(B_RING_FULL, now_ns() - t0);
|
|
}
|
|
/// (the producer, an encoder starting) the next packet's place, with `room` contiguous bytes: its header, and the end of what it
|
|
/// may use. Never more than `room`, even when the ring has more free: whether a packet is made directly depends on its size
|
|
/// alone, not on how far the recorder had got (tests/test_recorder_sync.py: the oversize runs)
|
|
Packet* reserve(uint8_t*& lim) {
|
|
size_t off = (size_t)(w & mask), tail = cap - off;
|
|
if (tail < room) { // (the ring's tail: passed over, the packet at its start)
|
|
wait_room(tail + room);
|
|
if (tail >= sizeof(Packet)) {
|
|
Packet* mk = reinterpret_cast<Packet*>(base + off);
|
|
mk->run = nullptr, mk->next = nullptr, mk->op = 0, mk->size = 0;
|
|
}
|
|
w += tail; // (published with the next packet)
|
|
off = 0, tail = cap;
|
|
} else {
|
|
wait_room(room);
|
|
}
|
|
lim = base + off + room; // (free and contiguous: tail >= room, and wait_room() made room bytes free past w)
|
|
return reinterpret_cast<Packet*>(base + off);
|
|
}
|
|
void publish(const Packet* p, const uint8_t* end) {
|
|
w += (uint64_t)(end - reinterpret_cast<const uint8_t*>(p));
|
|
// ((Triforce) parts: held until the part's end (post_part), or a chunk of a long part: one wake-up a batch, not one a call)
|
|
if (parts) {
|
|
if (w - w_pub >= chunk) flush();
|
|
return;
|
|
}
|
|
// (the recorder asleep: this packet now, and the wake-up with it, so that it runs while the replay goes on, not at its
|
|
// next wait)
|
|
if (w - w_pub >= batch || sleeping.load(std::memory_order_relaxed)) flush();
|
|
}
|
|
// (no destructor: the recorder lives as long as the process, below)
|
|
};
|
|
/// The recorder, for the process's lifetime: made once, never destroyed, as vk_jobs' detached workers. Windows ends every other
|
|
/// thread before a DLL's global destructors run (ExitProcess: the window's close, svcExitProcess, the updater, main's return,
|
|
/// hos_fatal), possibly while the recorder holds sleep_mu in idle(); a destructor that woke (sleep_mu) and joined it would then wait
|
|
/// for a mutex a dead thread owns. No global destructor touches its mutexes or thread: at exit nothing needs it (what is in the
|
|
/// ring is commands never to be submitted; it writes nothing else). stop() is the explicit teardown (vkDestroyDevice's boundary).
|
|
/// tests/test_recorder_sync.py: recorder_exit_test.cpp
|
|
inline Recorder& g_rec = *new Recorder;
|
|
|
|
inline bool Recorder::start() { // (the producer, its first packet)
|
|
std::lock_guard lk(life_mu);
|
|
if (ready.load(std::memory_order_relaxed)) return true;
|
|
if (broken.load(std::memory_order_relaxed)) return false;
|
|
if (!base) {
|
|
size_t c = 4096;
|
|
while (c < g_ring_bytes && c < (size_t(1) << 30)) c <<= 1;
|
|
new_wait::NoWait at_once; // ((Triforce, OS-04) no ring: the recorder is off; not a wait at the commit limit)
|
|
base = static_cast<uint8_t*>(::operator new(c, std::align_val_t(64), std::nothrow));
|
|
if (!base) {
|
|
broken.store(true);
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: no memory for the recorder's ring (%zu KB): every call made directly", c >> 10);
|
|
return false;
|
|
}
|
|
cap = c, mask = c - 1;
|
|
}
|
|
room = std::max<size_t>(std::min(g_ring_room, cap / 4) & ~size_t(15), 2 * sizeof(Packet));
|
|
// (the counters go on from where a stopped recorder left them, never back to 0: a foreign_wait() that read W just before
|
|
// a restart still sees R reach it. Anything a stopped recorder left unrun is dropped, as a fresh ring did; offsets are & mask)
|
|
const uint64_t at = W.load(std::memory_order_acquire);
|
|
R.store(at, std::memory_order_release), w = w_pub = r_seen = at, stop_req.store(false);
|
|
batch = std::min(g_publish_bytes, room);
|
|
spin_limit = 0; // (set by the recorder: g_spin_ns, or adaptive)
|
|
if (g_idle_wait && (!g_wait_on_address || !g_wake_by_address)) {
|
|
g_idle_wait = false;
|
|
::mkwvk::logf("vk: RX_VK_RECORDER_IDLE=wait: no WaitOnAddress here: the recorder idles with the spin and a condition variable");
|
|
}
|
|
parts = g_parts.load(std::memory_order_relaxed);
|
|
chunk = std::min<uint64_t>(g_chunk_bytes, cap / 4);
|
|
idle_on_w = g_idle_wait || (parts && g_wait_on_address && g_wake_by_address);
|
|
parts_done.store(parts_sent.load(std::memory_order_acquire), std::memory_order_seq_cst); // (none left behind: stop() ran them)
|
|
try {
|
|
th = std::thread([this] { main(); });
|
|
} catch (...) {
|
|
broken.store(true);
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: the recorder thread could not be started: every call made directly");
|
|
return false;
|
|
}
|
|
starts.fetch_add(1);
|
|
ready.store(true);
|
|
return true;
|
|
}
|
|
/// Teardown: everything published run, the thread joined, the ring let go (made again at the next packet). Any thread, with no
|
|
/// producer encoding at the time (the renderer's teardown, or the producer itself: vkDestroyDevice's boundary)
|
|
inline void Recorder::stop() {
|
|
std::lock_guard lk(life_mu);
|
|
if (!th.joinable()) return;
|
|
if (t_prod == this) flush(); // (the producer's own stop: what it wrote, published first)
|
|
const uint64_t target = W.load(std::memory_order_acquire);
|
|
spin([&] { return idle_at(target) || queue_escaped(); });
|
|
if (!idle_at(target)) // ((Triforce) parts: the recorder waits for the queue's lock this thread holds: neither can go on)
|
|
recorder_fatal("stopped (the device destroyed) under the queue's lock, with a part's submit waiting for that lock");
|
|
stop_req.store(true);
|
|
wake();
|
|
th.join();
|
|
ready.store(false);
|
|
if (g_stats_log) log_stats(0);
|
|
::operator delete(base, std::align_val_t(64));
|
|
base = nullptr, cap = 0;
|
|
}
|
|
inline void Recorder::main() {
|
|
t_recorder = true;
|
|
if (g_thread_start_hook) g_thread_start_hook();
|
|
try {
|
|
loop();
|
|
} catch (const std::exception& e) {
|
|
recorder_fatal(e.what());
|
|
} catch (...) {
|
|
recorder_fatal("an exception out of a driver call");
|
|
}
|
|
}
|
|
/// (the recorder, nothing published to run) spins a while, then sleeps until woken (or 2 ms); false: stop asked and nothing left
|
|
inline bool Recorder::idle(uint64_t r, uint64_t& seen, uint64_t& last_log) {
|
|
const uint64_t t0 = now_ns();
|
|
// (fixed, or adaptive from the top; (Triforce) parts: a batch comes at a part's end, the gaps between are a part's translation:
|
|
// short; RX_VK_RECORDER_IDLE=wait: 20 us)
|
|
if (!spin_limit) spin_limit = parts ? kPartsSpin : g_idle_wait ? kSpinLo : g_spin_ns ? g_spin_ns : kSpinHi;
|
|
for (uint32_t i = 0;; i++) {
|
|
if ((seen = W.load(std::memory_order_acquire)) != r || stop_req.load(std::memory_order_acquire)) {
|
|
spin_ns.fetch_add(now_ns() - t0, std::memory_order_relaxed);
|
|
return seen != r || W.load(std::memory_order_acquire) != r;
|
|
}
|
|
cpu_pause();
|
|
if ((i & 15) == 15 && now_ns() - t0 > spin_limit) break; // (spin_limit: a replay's gaps between calls, spun through)
|
|
}
|
|
spin_ns.fetch_add(now_ns() - t0, std::memory_order_relaxed);
|
|
sleeps.fetch_add(1, std::memory_order_relaxed);
|
|
if (g_stats_log && t0 - last_log > 10000000000ull) {
|
|
if (last_log) log_stats((double)(t0 - last_log) / 1e9);
|
|
last_log = t0;
|
|
}
|
|
const uint64_t s0 = now_ns();
|
|
if (idle_on_w) {
|
|
// ((Triforce) RX_VK_RECORDER_IDLE=wait, or parts: sleeping set before W is compared, and WaitOnAddress returns at once when
|
|
// W is no longer r: a publish before the wait is not missed. The producer wakes the address only when it saw sleeping set
|
|
// (flush, kick); a publish that looked before it was set finds the recorder at the 2 ms timeout, or kick() while it waits)
|
|
sleeping.store(true, std::memory_order_seq_cst);
|
|
uint64_t compare = r;
|
|
if (!stop_req.load()) g_wait_on_address(&W, &compare, sizeof compare, 2);
|
|
sleeping.store(false, std::memory_order_relaxed);
|
|
} else {
|
|
std::unique_lock lk(sleep_mu);
|
|
#ifdef RX_REC_TEST_HOOKS
|
|
if (g_test_idle_locked) g_test_idle_locked();
|
|
#endif
|
|
sleeping.store(true, std::memory_order_seq_cst);
|
|
if (W.load(std::memory_order_seq_cst) == r && !stop_req.load()) sleep_cv.wait_for(lk, std::chrono::milliseconds(2));
|
|
sleeping.store(false, std::memory_order_relaxed);
|
|
}
|
|
seen = W.load(std::memory_order_acquire);
|
|
const uint64_t slept = now_ns() - s0;
|
|
if (slept < kSpinHi) short_sleeps.fetch_add(1, std::memory_order_relaxed);
|
|
if (!g_spin_ns && !idle_on_w && !parts) { // (adaptive: a short sleep means a gap the longest spin covers; a long one, a spin to shorten)
|
|
if (slept < kSpinHi && seen != r) spin_limit = kSpinHi;
|
|
else if (slept >= kSpinHi) spin_limit = std::max(kSpinLo, spin_limit / 2);
|
|
}
|
|
return true;
|
|
}
|
|
inline void Recorder::loop() {
|
|
uint64_t r = R.load(std::memory_order_relaxed), seen = r, last_log = now_ns(), n = 0, busy0 = 0;
|
|
const bool poison = g_poison.load(std::memory_order_relaxed);
|
|
for (;;) {
|
|
if (r == seen && (seen = W.load(std::memory_order_acquire)) == r) {
|
|
if (busy0) {
|
|
const uint64_t busy = now_ns() - busy0;
|
|
busy_ns.fetch_add(busy, std::memory_order_relaxed), all_busy_ns.fetch_add(busy, std::memory_order_relaxed), busy0 = 0;
|
|
}
|
|
if (n) packets.fetch_add(n, std::memory_order_relaxed), all_packets.fetch_add(n, std::memory_order_relaxed), n = 0;
|
|
notify_parts(); // ((Triforce) parts: a thread asleep in spin() for R)
|
|
if (!idle(r, seen, last_log)) return;
|
|
continue;
|
|
}
|
|
if (!busy0) busy0 = now_ns();
|
|
const size_t off = (size_t)(r & mask), tail = cap - off;
|
|
const Packet* p = reinterpret_cast<const Packet*>(base + off);
|
|
if (tail < sizeof(Packet) || !p->run) { // (the ring's tail: the next packet is at its start)
|
|
r += tail;
|
|
R.store(r, std::memory_order_release);
|
|
continue;
|
|
}
|
|
const bool part = p->op == OP_PART;
|
|
const uint64_t p0 = part ? now_ns() : 0;
|
|
p->run(p);
|
|
if (part) part_ns.fetch_add(now_ns() - p0, std::memory_order_relaxed);
|
|
const size_t len = (size_t)(reinterpret_cast<const uint8_t*>(p->next) - reinterpret_cast<const uint8_t*>(p));
|
|
if (poison) std::memset(base + off, 0xCD, len);
|
|
r += len, n++;
|
|
R.store(r, std::memory_order_release);
|
|
if (part) { // ((Triforce) parts: its end and submit made; said after R, so that a slot's wait for it finds R past it)
|
|
parts_done.store(parts_done.load(std::memory_order_relaxed) + 1, std::memory_order_seq_cst);
|
|
parts_run.fetch_add(1, std::memory_order_relaxed);
|
|
notify_parts();
|
|
}
|
|
}
|
|
}
|
|
inline void Recorder::log_stats(double secs) {
|
|
uint64_t n[B_COUNT], ns[B_COUNT], mx[B_COUNT], tot_n = 0, tot_ns = 0, max_ns = 0;
|
|
for (int k = 0; k < B_COUNT; k++)
|
|
n[k] = wait_n[k].exchange(0, std::memory_order_relaxed), ns[k] = wait_ns[k].exchange(0, std::memory_order_relaxed),
|
|
mx[k] = wait_max[k].exchange(0, std::memory_order_relaxed), tot_n += n[k], tot_ns += ns[k], max_ns = std::max(max_ns, mx[k]);
|
|
const uint64_t spun = spin_ns.exchange(0, std::memory_order_relaxed), slept = sleeps.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t short_slept = short_sleeps.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t pk = packets.exchange(0, std::memory_order_relaxed), busy = busy_ns.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t dr = direct.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t pr = parts_run.exchange(0, std::memory_order_relaxed), bt = batches.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t esc = escapes.exchange(0, std::memory_order_relaxed), pns = part_ns.exchange(0, std::memory_order_relaxed);
|
|
const uint64_t lns = lock_ns.exchange(0, std::memory_order_relaxed), ln = lock_n.exchange(0, std::memory_order_relaxed);
|
|
if (!pk && !tot_n && !dr && !pr) return;
|
|
if (pr || esc) // ((Triforce) parts: what of its busy time was parts' ends and submits, and blocked on the queue's lock)
|
|
::mkwvk::logf("vk: recorder, by part (RX_VK_RECORDER_PARTS): %llu parts ended and submitted in %llu batches, %.2f s in their "
|
|
"ends and submits, %.2f s of that blocked on the queue's lock (%llu times); %llu waits given up under the "
|
|
"queue's lock, the recorder waiting for it",
|
|
(unsigned long long)pr, (unsigned long long)bt, pns / 1e9, lns / 1e9, (unsigned long long)ln,
|
|
(unsigned long long)esc);
|
|
char top[512];
|
|
size_t at = 0;
|
|
top[0] = 0;
|
|
for (int shown = 0; shown < 6; shown++) { // (the six it waited longest at)
|
|
int best = -1;
|
|
for (int k = 0; k < B_COUNT; k++)
|
|
if (n[k] && (best < 0 || ns[k] > ns[best])) best = k;
|
|
if (best < 0) break;
|
|
const int c = std::snprintf(top + at, sizeof top - at, "%s %s %llu/%.2f ms (max %.2f)", shown ? "," : ";", bound_name(best),
|
|
(unsigned long long)n[best], ns[best] / 1e6, mx[best] / 1e6);
|
|
if (c < 0 || (size_t)c >= sizeof top - at) break;
|
|
at += (size_t)c;
|
|
n[best] = 0;
|
|
}
|
|
::mkwvk::logf("vk: recorder: %llu packets run in %.1f s (busy %.2f s, spun idle %.2f s, slept %llu times, %llu of them under 200 "
|
|
"us), %llu calls made directly (no room); the replay waited for it %llu times, %.2f ms in all, the longest %.2f ms%s",
|
|
(unsigned long long)pk, secs, busy / 1e9, spun / 1e9, (unsigned long long)slept, (unsigned long long)short_slept,
|
|
(unsigned long long)dr, (unsigned long long)tot_n, tot_ns / 1e6, max_ns / 1e6, top);
|
|
}
|
|
|
|
/// (THREAD) The boundary calls the producer makes without waiting for the recorder: only a call that touches no command buffer or
|
|
/// pool and can read or change nothing a command still in the ring could do. The ring holds vkCmd* recordings only, and every
|
|
/// submission (vkQueueSubmit2, vkQueueBindSparse) waits for it: what has been submitted when such a call runs is what it would be
|
|
/// with the recorder off. Each case's reason is beside it;
|
|
/// tests/test_recorder_sync.py checks each listed call runs with packets in flight and every other one waits, and that adding
|
|
/// vkQueueSubmit2, vkDestroyBuffer or vkEndCommandBuffer here fails it
|
|
constexpr bool unordered(Bound k) {
|
|
switch (k) {
|
|
case B_vkGetSemaphoreCounterValue: return true; // (a timeline's value: signalled only by submissions, each made after the ring is empty)
|
|
case B_vkWaitSemaphores: return true; // (a timeline's value: signalled only by submissions, each made after the ring is empty, or by
|
|
// the driver itself: vk_reflex.cpp's latency-sleep semaphore, which no recorded command signals)
|
|
case B_vkGetQueueCheckpointDataNV: return true; // (what the device ran of submitted work, after a loss)
|
|
case B_vkGetDeviceFaultInfoEXT: return true; // (what the device ran of submitted work, after a loss)
|
|
default: return false; // (unordered: the end of the list)
|
|
}
|
|
}
|
|
|
|
/// ((Triforce) THREAD, RX_VK_RECORDER_PARTS) The ring holds submissions too (a part's end and submit, post_part), so unordered()'s
|
|
/// reason (each submission made after the ring is empty) no longer holds for the waits: a wait for a fence or a timeline value,
|
|
/// and a fence's reset, need the parts handed over before them submitted (settle_parts), and nothing more of the ring: they name
|
|
/// no command buffer or pool. A thread in a Settled scope has waited for what it needs itself and waits for no more
|
|
constexpr bool parts_settle(Bound k) {
|
|
return k == B_vkWaitForFences || k == B_vkResetFences || k == B_vkWaitSemaphores;
|
|
}
|
|
/// (THREAD, parts) The calls made without any wait: unordered()'s, the query pools' host calls and a mapped allocation's
|
|
/// invalidation. They touch no command buffer; the commands still in the ring are recordings not submitted, so no query they
|
|
/// name is in use on the GPU and no memory they write is written yet, and whatever the caller reads of submitted work its own
|
|
/// wait (on the timeline or a fence, settled) has made sure of
|
|
constexpr bool parts_unordered(Bound k) {
|
|
return unordered(k) || k == B_vkGetQueryPoolResults || k == B_vkResetQueryPool || k == B_vmaInvalidateAllocation;
|
|
}
|
|
|
|
/// ((Triforce) THREAD, parts) A recording slot's own calls at its reuse: vk_replay.cpp's begin_recording resets the slot's two
|
|
/// command pools and begins its command buffers, vk_draw.cpp's draw_slot_reset (or draw_begin_recording) resets its descriptor
|
|
/// pool and destroys the extra pools it grew. Nothing still in the ring can name those: the slot's last part was ended and
|
|
/// submitted by the recorder (its ends in the ring), and its submit is done (recording_wait: wait_part, then its fence), so what
|
|
/// the ring holds since is other command buffers' and the new use's own, none from these pools. The calls stay direct, on the
|
|
/// producer: only the wait goes. slot_begin() checks the recorder has run the slot's last end and submit (the ring's position
|
|
/// after them); if not, the calls wait as before. OwnSlot marks the call sites, and only those: every other reset, begin and
|
|
/// destruction still waits (the frame's command buffer, ImGui, NGX, FSR). With the hand-off by part only
|
|
inline constinit thread_local bool t_slot_ready = false, t_own_slot = false;
|
|
constexpr bool own_slot_call(Bound k) {
|
|
return k == B_vkResetCommandPool || k == B_vkBeginCommandBuffer || k == B_vkResetDescriptorPool || k == B_vkDestroyDescriptorPool;
|
|
}
|
|
/// (the producer) the ring's position after all it has written: at a slot's end, for that slot's next slot_begin()
|
|
inline uint64_t produced() {
|
|
const Recorder* g = t_prod;
|
|
return g ? g->w : 0;
|
|
}
|
|
/// (the producer, a slot's reuse, before its own calls) whether OwnSlot may skip the wait for this slot's calls
|
|
inline void slot_begin(uint64_t end_pos) {
|
|
t_slot_ready = false;
|
|
if (!g_parts.load(std::memory_order_relaxed) || g_mode.load(std::memory_order_relaxed) != THREAD) return;
|
|
const Recorder* g = t_prod;
|
|
if (g && end_pos && g->idle_at(end_pos)) t_slot_ready = true;
|
|
}
|
|
inline void slot_done() { t_slot_ready = false; }
|
|
/// (a slot's own calls, within slot_begin() and slot_done())
|
|
struct OwnSlot {
|
|
const bool was = t_own_slot;
|
|
OwnSlot() { t_own_slot = t_slot_ready; }
|
|
~OwnSlot() { t_own_slot = was; }
|
|
OwnSlot(const OwnSlot&) = delete;
|
|
OwnSlot& operator=(const OwnSlot&) = delete;
|
|
};
|
|
|
|
/// (THREAD, parts) the longest a boundary's wait for the parts is (a fence or timeline wait, a fence's reset): a part's submit
|
|
/// can wait behind a present holding the queue's lock; past this the call is made, as it was without the recorder
|
|
constexpr uint64_t kSettleNs = 250000000;
|
|
/// ((Triforce) THREAD, RX_VK_RECORDER_PARTS) boundary()'s waits with the hand-off by part. replay_mu's unlock publishes what its
|
|
/// holder wrote and does not wait: the next holder goes on producing after it, and outside a hold the recorder may still be busy,
|
|
/// so a thread without replay_mu waits for what was published before its call (foreign_wait), as the producer does for all it wrote
|
|
inline void boundary_parts(Bound k) {
|
|
if (k == B_REPLAY_UNLOCK) {
|
|
if (Recorder* g = t_prod) g->flush();
|
|
return;
|
|
}
|
|
if (parts_settle(k)) { // (bounded, as every wait is: unmet, the wait itself goes on, and the next part's submit flushes)
|
|
if (!t_settled) g_rec.settle_parts(k, kSettleNs);
|
|
return;
|
|
}
|
|
if (parts_unordered(k)) return;
|
|
if (t_own_slot && own_slot_call(k)) return; // (a slot's own resets: its part waited for, OwnSlot)
|
|
if (t_settled && (k == B_vkQueuePresentKHR || k == B_vkAcquireNextImageKHR)) return; // (the present thread: no part named)
|
|
if (Recorder* g = t_prod) g->wait_idle(k);
|
|
else g_rec.foreign_wait(k);
|
|
}
|
|
|
|
/// Before a call that begins, ends or resets command buffers, submits, presents or waits, reads back what the GPU did, or lets go of
|
|
/// what recorded commands use (vk_work.h's second list), and when the replay's lock is let go: the thread's held packets reach the
|
|
/// driver first. DEFER: drained; THREAD: on the producer, waits until the recorder has run all it published (vkDestroyDevice and
|
|
/// vkDestroyInstance: the recorder stopped and joined first); ((Triforce) RX_VK_RECORDER_PARTS: boundary_parts; the recorder's own
|
|
/// calls, a part's ends and submit, wait for nothing); otherwise a relaxed load and a branch
|
|
inline void boundary(Bound k) {
|
|
const int m = g_mode.load(std::memory_order_relaxed);
|
|
if (m == DEFER) {
|
|
drain();
|
|
} else if (m == THREAD) {
|
|
if (t_recorder) return; // ((Triforce) parts: the recorder's own, in the ring's order)
|
|
if (k == B_vkDestroyDevice || k == B_vkDestroyInstance) g_rec.stop();
|
|
else if (g_parts.load(std::memory_order_relaxed)) boundary_parts(k);
|
|
else if (unordered(k)) return;
|
|
else if (Recorder* g = t_prod) g->wait_idle(k);
|
|
}
|
|
}
|
|
|
|
/// (Triforce, 11-15) Just before a parallel replay's segments start (vk_replay.cpp replay_handle, before par_active): what this
|
|
/// thread holds (DEFER) or put in the ring (THREAD) reaches the driver first. The segments' command buffers already have commands of
|
|
/// the producer's in them (their timestamps), and the helper threads record into them directly: a helper's own wait before a direct
|
|
/// call (settle, foreign_wait) sees only what was published, and up to g_publish_bytes may not be. While par_active every call is
|
|
/// made directly (Enc), the producer's too, so the ring stays empty until par_active is let go. tests/test_recorder_sync.py: the
|
|
/// segments scenario, and this left out
|
|
inline void segments_begin() { boundary(B_PAR); }
|
|
|
|
/// A std::mutex whose unlock drains the thread's held packets first (vk_replay.cpp's replay_mu): what a thread recorded into the
|
|
/// replay's command buffers reaches the driver before another thread may record into them. THREAD: the holder is the ring's
|
|
/// producer; the unlock waits for the recorder to run all it published, so the recorder is idle whenever no thread holds it
|
|
struct DrainingMutex {
|
|
std::mutex m;
|
|
std::atomic<const lock_census::Slot*> owner{nullptr}; // ((Triforce, THREAD-68) the holder, for the lock census; unset when it is off)
|
|
void lock() {
|
|
lock_census::lock(m, owner, "replay_mu"); // (m.lock(), as before, unless RX_VK_LOCK_CENSUS counts)
|
|
enter();
|
|
}
|
|
bool try_lock() {
|
|
if (!lock_census::try_lock(m, owner)) return false;
|
|
enter();
|
|
return true;
|
|
}
|
|
void unlock() {
|
|
boundary(B_REPLAY_UNLOCK);
|
|
t_prod = nullptr;
|
|
lock_census::unlocking(owner); // (after the drain: a waiter is behind this thread until here)
|
|
m.unlock();
|
|
}
|
|
static void enter() {
|
|
// (the previous holder's unlock waited until the recorder had run all it published: the ring is empty here, so a hold
|
|
// with no producer makes its calls directly, in order after them)
|
|
if (mode() == THREAD && !(g_direct_hook && g_direct_hook())) t_prod = &g_rec;
|
|
}
|
|
};
|
|
|
|
/// (for tests) a mode, or back; what is held is run first (THREAD: what was published)
|
|
inline void set_mode(int m) {
|
|
drain();
|
|
if (Recorder* g = t_prod) g->flush();
|
|
if (g_rec.ready.load()) {
|
|
const uint64_t target = g_rec.W.load(std::memory_order_acquire);
|
|
g_rec.spin([&] { return g_rec.idle_at(target); });
|
|
}
|
|
g_mode.store(m, std::memory_order_relaxed);
|
|
}
|
|
|
|
/// THREAD: the recorder has run what this thread may depend on (the producer: all it published; another thread: what was published)
|
|
inline void settle(Bound k) {
|
|
if (t_recorder) return; // ((Triforce) parts: the recorder's own calls, in the ring's order)
|
|
if (Recorder* g = t_prod) g->wait_idle(k);
|
|
else g_rec.foreign_wait();
|
|
}
|
|
|
|
/// ((Triforce) THREAD, parts) a scope whose waits need nothing more of the recorder (t_settled): the caller waited for what it
|
|
/// needs itself
|
|
struct Settled {
|
|
Settled() { t_settled++; }
|
|
~Settled() { t_settled--; }
|
|
Settled(const Settled&) = delete;
|
|
Settled& operator=(const Settled&) = delete;
|
|
};
|
|
/// (THREAD) whether a part's end and submit go to the recorder (post_part): RX_VK_RECORDER_PARTS, with the recorder the mode
|
|
inline bool parts_wanted() { return g_mode.load(std::memory_order_relaxed) == THREAD && g_parts.load(std::memory_order_relaxed); }
|
|
/// (parts, any thread) every part handed over so far submitted by the recorder, at most timeout_ns (none handed over, a Settled
|
|
/// scope, or no hand-off by part: at once). Whether they are
|
|
inline bool settle_parts(Bound k, uint64_t timeout_ns = UINT64_MAX) {
|
|
if (!parts_wanted() || t_recorder || t_settled) return true;
|
|
return g_rec.settle_parts(k, timeout_ns);
|
|
}
|
|
/// (parts, any thread) part `ticket` (post_part's number) submitted; 0, or no recorder: at once. Whether it is (false: only
|
|
/// under a queue lock the recorder waits for)
|
|
inline bool wait_part(uint64_t ticket) {
|
|
return !ticket || !parts_wanted() || t_recorder || g_rec.wait_part(ticket);
|
|
}
|
|
/// (parts, any thread) until done(), at most timeout_ns (less the time waited, on return): a recording's submit the caller knows
|
|
/// by its own number (vk_replay.cpp wait_rec_seq), woken at each part's submit; at once when done() already, or without the
|
|
/// hand-off by part. Whether done()
|
|
template <class P>
|
|
inline bool wait_submitted(P done, uint64_t& timeout_ns) {
|
|
if (!parts_wanted() || t_recorder) return true;
|
|
const uint64_t t0 = now_ns();
|
|
const bool ok = g_rec.wait_until(done, B_PART, timeout_ns);
|
|
const uint64_t spent = now_ns() - t0;
|
|
if (timeout_ns != UINT64_MAX) timeout_ns = spent < timeout_ns ? timeout_ns - spent : 0;
|
|
return ok;
|
|
}
|
|
/// (parts, the recorder's part submit) whether a wait for the parts ended unmet since: what is held is to be sent now, as the
|
|
/// waiter's own flush may have come before this part was held. The recorder asks it for the part it is submitting (parts_done +
|
|
/// 1): owed once that part reaches the waiter's; cleared then, unless a waiter has asked for a later part meanwhile. Another
|
|
/// thread (a part made here) is owed whatever is owed
|
|
inline bool take_flush_owed() {
|
|
uint64_t upto = g_rec.flush_owed_upto.load(std::memory_order_acquire);
|
|
if (!upto) return false;
|
|
const uint64_t cur = t_recorder ? g_rec.parts_done.load(std::memory_order_relaxed) + 1 : UINT64_MAX;
|
|
if (cur < upto) return false;
|
|
g_rec.flush_owed_upto.compare_exchange_strong(upto, 0, std::memory_order_acq_rel);
|
|
return true;
|
|
}
|
|
/// (parts) the recorder's part made on the calling thread instead (no recorder, or the queue's lock held): those that wait for
|
|
/// parts are told
|
|
inline void part_made_here() { g_rec.notify_parts(); }
|
|
/// QueueMutex's events (vk_internal.h: dev.present_mu and the present queue's lock; vk_replay.cpp installs this as
|
|
/// queue_lock_hook). 0: about to lock (not try_lock); 3: the lock is held by another, about to block for it; 1: locked; 2: about
|
|
/// to unlock. settles: the replay's queue's lock (dev.present_mu), the one the recorder takes for a part's submit: a thread about
|
|
/// to take it first waits for the parts handed over to be submitted (the queue's order: its submit, present or idle wait after
|
|
/// them), unless it holds a queue lock already or is Settled. The recorder says it is blocked on the lock (at_queue), and a holder
|
|
/// of present_mu blocked on another queue lock says so (chain_waiters), so that a thread the recorder waits for, through either,
|
|
/// does not wait for it in turn (queue_escaped). Nothing at all without the hand-off by part
|
|
inline void queue_lock_event(int phase, bool settles) {
|
|
if (!g_parts.load(std::memory_order_relaxed)) return;
|
|
if (phase == 2) { // (never below 0: a lock taken before the mode was read)
|
|
if (t_queue_held > 0) t_queue_held--;
|
|
if (settles && t_present_held > 0) t_present_held--;
|
|
return;
|
|
}
|
|
if (phase == 1) {
|
|
t_queue_held++;
|
|
if (settles) t_present_held++;
|
|
if (t_recorder && settles) {
|
|
g_rec.at_queue.store(false, std::memory_order_seq_cst);
|
|
if (g_rec.lock_t0) { // (the stats: the time it was blocked on the lock)
|
|
g_rec.lock_ns.fetch_add(now_ns() - g_rec.lock_t0, std::memory_order_relaxed);
|
|
g_rec.lock_n.fetch_add(1, std::memory_order_relaxed);
|
|
g_rec.lock_t0 = 0;
|
|
}
|
|
}
|
|
if (t_chain_wait) t_chain_wait = false, g_rec.chain_waiters.fetch_sub(1, std::memory_order_seq_cst);
|
|
return;
|
|
}
|
|
if (phase == 3) {
|
|
if (t_recorder && settles) g_rec.lock_t0 = now_ns(), g_rec.at_queue.store(true, std::memory_order_seq_cst), g_rec.notify_parts();
|
|
else if (!settles && t_present_held && !t_chain_wait) {
|
|
t_chain_wait = true, g_rec.chain_waiters.fetch_add(1, std::memory_order_seq_cst);
|
|
g_rec.notify_parts();
|
|
}
|
|
return;
|
|
}
|
|
if (settles && !t_recorder && !t_queue_held && !t_settled && parts_wanted()) g_rec.settle_parts(B_QUEUE_LOCK);
|
|
}
|
|
|
|
/// A call that is not packed (a pNext chain, an unknown descriptor type): what is held goes first, then the call itself
|
|
template <class F>
|
|
inline void fallback(const F& direct) {
|
|
if (g_mode.load(std::memory_order_relaxed) == THREAD) settle(B_FALLBACK);
|
|
else drain();
|
|
t_arena().fallbacks++;
|
|
direct();
|
|
}
|
|
|
|
template <class F>
|
|
void thunk(const Packet* p) {
|
|
(*reinterpret_cast<const F*>(p + 1))();
|
|
}
|
|
template <class F>
|
|
void thunk_at(const Packet* p) { // (THREAD: the closure at its offset in the packet, after the copied arrays)
|
|
(*reinterpret_cast<const F*>(reinterpret_cast<const uint8_t*>(p) + p->size))();
|
|
}
|
|
|
|
inline uint8_t* align_up(uint8_t* p, size_t a) { return reinterpret_cast<uint8_t*>(((uintptr_t)p + a - 1) & ~(uintptr_t)(a - 1)); }
|
|
/// ((Triforce) THREAD, RX_VK_RECORDER_PARTS, the producer) A part's end and submit, f (a closure of values: its command buffers'
|
|
/// ends and its queue submit with what the producer chose), into the ring after the part's packets, and every packet published:
|
|
/// the recorder makes them in order and the producer goes on at once. Its number (parts_sent), for wait_part; 0 when it is not
|
|
/// taken (no hand-off by part, no ring, a queue lock held here, a parallel replay's segments): the caller runs f itself, whose
|
|
/// ends wait for the ring as before
|
|
template <class F>
|
|
inline uint64_t post_part(const F& f) {
|
|
static_assert(std::is_trivially_copyable_v<F> && std::is_trivially_destructible_v<F>, "a packet holds values only");
|
|
static_assert(alignof(F) <= alignof(Packet), "a closure aligned beyond the header");
|
|
if (!parts_wanted() || t_queue_held || t_recorder || par_active.load(std::memory_order_relaxed)) return 0;
|
|
Recorder* g = t_prod;
|
|
if (!g || (!g->ready.load(std::memory_order_relaxed) && !g->start()) || !g->parts) return 0;
|
|
uint8_t* lim;
|
|
Packet* hdr = g->reserve(lim);
|
|
uint8_t* at = align_up(reinterpret_cast<uint8_t*>(hdr + 1), alignof(Packet));
|
|
if (at > lim || (size_t)(lim - at) < sizeof(F)) return 0; // (a few dozen bytes: never, with the room a packet is given)
|
|
new (static_cast<void*>(at)) F(f);
|
|
hdr->run = &thunk_at<F>, hdr->op = OP_PART, hdr->size = (uint32_t)(at - reinterpret_cast<uint8_t*>(hdr));
|
|
uint8_t* end = align_up(at + sizeof(F), alignof(Packet));
|
|
hdr->next = reinterpret_cast<Packet*>(end);
|
|
const uint64_t ticket = g->parts_sent.load(std::memory_order_relaxed) + 1;
|
|
g->parts_sent.store(ticket, std::memory_order_release);
|
|
g->w += (uint64_t)(end - reinterpret_cast<uint8_t*>(hdr));
|
|
g->flush();
|
|
return ticket;
|
|
}
|
|
|
|
/// One packet being encoded: its copies, then emit() with the closure that makes the call and the direct call. Sync: run at emit, the
|
|
/// arena rewound when the encoder goes out of scope. Defer: held; drained first when the held bytes reached the soft budget, and
|
|
/// made at once (after a drain) while a parallel replay records (par_active: never held then). An allocation that fails (the hard
|
|
/// budget, the table, the system) makes emit drain what is held, in order, then make the direct call: no call lost or reordered
|
|
///
|
|
/// THREAD, on the producer (the replay lock's holder): the packet is written straight into the ring (header first, then the copies,
|
|
/// then the closure) and published at emit; no room for it (bigger than the room a packet is given) or no ring: the producer waits
|
|
/// until the recorder has run everything published, then makes the call directly. On another thread, or while par_active: the
|
|
/// call made directly (PASS), after what was published has run. ((Triforce) parts: on the recorder thread itself, inside a part's
|
|
/// packet, made directly at once: it is the ring's own place)
|
|
struct Enc {
|
|
int md = mode();
|
|
Arena* a = nullptr; // (SYNC, DEFER)
|
|
Arena::Mark m{};
|
|
bool failed = false;
|
|
Recorder* g = nullptr; // (THREAD) the ring, its packet's header, where the copies go and how far they may
|
|
Packet* hdr = nullptr;
|
|
uint8_t *cur = nullptr, *lim = nullptr;
|
|
|
|
Enc() {
|
|
if (md == THREAD) {
|
|
begin_thread();
|
|
return;
|
|
}
|
|
a = &t_arena();
|
|
if (md == DEFER) {
|
|
if (par_active.load(std::memory_order_relaxed)) {
|
|
drain();
|
|
md = SYNC;
|
|
} else if (a->held >= g_soft_budget) {
|
|
drain();
|
|
a->budget_drains++;
|
|
}
|
|
}
|
|
m = a->mark();
|
|
}
|
|
void begin_thread() {
|
|
g = t_prod;
|
|
if (t_recorder) [[unlikely]] { // ((Triforce) parts: in the ring's order already)
|
|
md = PASS, failed = true;
|
|
return;
|
|
}
|
|
if (!g || par_active.load(std::memory_order_relaxed)) [[unlikely]] {
|
|
settle(B_PAR);
|
|
md = PASS, failed = true;
|
|
return;
|
|
}
|
|
if (!g->ready.load(std::memory_order_relaxed) && !g->start()) [[unlikely]] {
|
|
md = PASS, failed = true;
|
|
return;
|
|
}
|
|
hdr = g->reserve(lim);
|
|
cur = reinterpret_cast<uint8_t*>(hdr + 1);
|
|
}
|
|
template <class T>
|
|
T* alloc(size_t n) {
|
|
if (failed) return nullptr;
|
|
if (md == THREAD) {
|
|
uint8_t* at = align_up(cur, alignof(T) < 8 ? 8 : alignof(T));
|
|
if (at > lim || (size_t)(lim - at) < sizeof(T) * n) {
|
|
failed = true;
|
|
return nullptr;
|
|
}
|
|
cur = at + sizeof(T) * n;
|
|
return reinterpret_cast<T*>(at);
|
|
}
|
|
T* p = static_cast<T*>(a->alloc(sizeof(T) * n, alignof(T) < 8 ? 8 : alignof(T)));
|
|
if (!p) failed = true;
|
|
return p;
|
|
}
|
|
template <class T>
|
|
const T* dup(const T* p, size_t n) {
|
|
if (!p || !n) return nullptr;
|
|
T* d = alloc<T>(n);
|
|
if (!d) return nullptr;
|
|
std::memcpy(static_cast<void*>(d), p, sizeof(T) * n);
|
|
return d;
|
|
}
|
|
const void* dup_bytes(const void* p, size_t n) { return dup(static_cast<const uint8_t*>(p), n); }
|
|
|
|
template <class F, class D>
|
|
void emit(Op op, const F& f, const D& direct) {
|
|
static_assert(std::is_trivially_copyable_v<F> && std::is_trivially_destructible_v<F>, "a packet holds values only");
|
|
static_assert(alignof(F) <= alignof(Packet), "a closure aligned beyond the header");
|
|
if (md == THREAD || md == PASS) {
|
|
emit_thread(op, f, direct);
|
|
return;
|
|
}
|
|
Packet* p = failed ? nullptr : static_cast<Packet*>(a->alloc(sizeof(Packet) + sizeof(F), alignof(Packet)));
|
|
if (!p) {
|
|
const size_t held = a->held;
|
|
if (md == DEFER) drain(); // (what is held first, in order; this call's partial copies let go with it)
|
|
else a->rewind(m);
|
|
if (a->failures++ == 0)
|
|
::mkwvk::logf("vk: RX_VK_RECORDER: no arena memory for %s (%zu bytes held, %zu in chunks, hard budget %zu): the "
|
|
"held packets drained, the call made directly (logged once a thread)",
|
|
op_name(op), held, a->capacity, g_hard_budget);
|
|
direct();
|
|
return;
|
|
}
|
|
p->run = &thunk<F>, p->next = nullptr, p->op = op, p->size = (uint32_t)sizeof(F);
|
|
new (static_cast<void*>(p + 1)) F(f);
|
|
a->packets++;
|
|
if (md == SYNC) {
|
|
execute(p);
|
|
} else {
|
|
*a->tail = p;
|
|
a->tail = &p->next;
|
|
}
|
|
}
|
|
template <class F, class D>
|
|
void emit_thread(Op op, const F& f, const D& direct) {
|
|
uint8_t* at = failed ? nullptr : align_up(cur, alignof(Packet));
|
|
if (md == PASS) {
|
|
direct();
|
|
return;
|
|
}
|
|
if (!at || at > lim || (size_t)(lim - at) < sizeof(F)) [[unlikely]] { // (nothing published: the recorder idle, then direct)
|
|
g->wait_idle(B_NOMEM);
|
|
g->direct.fetch_add(1, std::memory_order_relaxed);
|
|
if (!g->logged_direct.exchange(true, std::memory_order_relaxed))
|
|
::mkwvk::logf("vk: RX_VK_RECORDER=thread: %s did not fit in the room a packet is given (%zu bytes): made directly once "
|
|
"the recorder had run what was before it (logged once)",
|
|
op_name(op), g->room);
|
|
direct();
|
|
return;
|
|
}
|
|
new (static_cast<void*>(at)) F(f);
|
|
hdr->run = &thunk_at<F>, hdr->op = op, hdr->size = (uint32_t)(at - reinterpret_cast<uint8_t*>(hdr));
|
|
uint8_t* end = align_up(at + sizeof(F), alignof(Packet));
|
|
hdr->next = reinterpret_cast<Packet*>(end);
|
|
g->publish(hdr, end);
|
|
}
|
|
~Enc() {
|
|
if (md == SYNC) a->rewind(m);
|
|
}
|
|
};
|
|
|
|
// ---- The wrapper each vk_work.h macro calls: the driver's function when the recorder is off (the branch is all the off path costs: the
|
|
// packer is a function of its own, out of line, so the caller's code stays as it was), else the call's packer
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#define RX_REC_PACKER __attribute__((noinline)) inline
|
|
#else
|
|
#define RX_REC_PACKER __declspec(noinline) inline
|
|
#endif
|
|
template <auto& F, auto& PK, class Sig = std::remove_cv_t<std::remove_reference_t<decltype(F)>>>
|
|
struct Route;
|
|
template <auto& F, auto& PK, class... P>
|
|
struct Route<F, PK, void(VKAPI_PTR*)(P...)> {
|
|
static void call(P... p) {
|
|
if (mode() == OFF) [[likely]]
|
|
return F(p...);
|
|
PK(p...);
|
|
}
|
|
};
|
|
|
|
// ---- V: value arguments only. The closure holds them as the call's own parameter types (converted at the wrapper, as the direct call
|
|
// converts them). A pointer to const data here would be a pointer kept past the call: refused at compile time (handles are pointers to
|
|
// non-const opaque types and pass)
|
|
template <auto& F, Op OP, class Sig = std::remove_cv_t<std::remove_reference_t<decltype(F)>>>
|
|
struct Value;
|
|
template <auto& F, Op OP, class... P>
|
|
struct Value<F, OP, void(VKAPI_PTR*)(P...)> {
|
|
static_assert(((!std::is_pointer_v<P> || !std::is_const_v<std::remove_pointer_t<P>>) && ...),
|
|
"a call with a pointer to data needs a packer of its own");
|
|
RX_REC_PACKER static void pack(P... p) {
|
|
Enc e;
|
|
const auto call = [=] { F(p...); };
|
|
e.emit(OP, call, call);
|
|
}
|
|
};
|
|
#define RX_REC_VALUE(f) inline constexpr auto& rx_##f = Route<::vkx::f, Value<::vkx::f, OP_##f>::pack>::call;
|
|
#define RX_REC_SKIP(f)
|
|
RX_REC_CALLS(RX_REC_VALUE, RX_REC_SKIP)
|
|
#undef RX_REC_VALUE
|
|
#undef RX_REC_SKIP
|
|
|
|
// ---- A: the calls with arrays and structs behind pointers. Each copies what the driver reads, by the counts the driver reads by
|
|
template <class T>
|
|
inline bool chained(const T* p, uint32_t n) {
|
|
for (uint32_t i = 0; i < n; i++)
|
|
if (p[i].pNext) return true;
|
|
return false;
|
|
}
|
|
|
|
RX_REC_PACKER void pk_vkCmdPipelineBarrier2(VkCommandBuffer cb, const VkDependencyInfo* di) {
|
|
if (di->pNext || chained(di->pMemoryBarriers, di->memoryBarrierCount) ||
|
|
chained(di->pBufferMemoryBarriers, di->bufferMemoryBarrierCount) ||
|
|
chained(di->pImageMemoryBarriers, di->imageMemoryBarrierCount))
|
|
return fallback([&] { ::vkx::vkCmdPipelineBarrier2(cb, di); });
|
|
Enc e;
|
|
VkDependencyInfo c = *di;
|
|
c.pMemoryBarriers = e.dup(di->pMemoryBarriers, di->memoryBarrierCount);
|
|
c.pBufferMemoryBarriers = e.dup(di->pBufferMemoryBarriers, di->bufferMemoryBarrierCount);
|
|
c.pImageMemoryBarriers = e.dup(di->pImageMemoryBarriers, di->imageMemoryBarrierCount);
|
|
e.emit(OP_vkCmdPipelineBarrier2, [=] { ::vkx::vkCmdPipelineBarrier2(cb, &c); }, [&] { ::vkx::vkCmdPipelineBarrier2(cb, di); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdPipelineBarrier2 = Route<::vkx::vkCmdPipelineBarrier2, pk_vkCmdPipelineBarrier2>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdBeginRendering(VkCommandBuffer cb, const VkRenderingInfo* ri) {
|
|
if (ri->pNext || chained(ri->pColorAttachments, ri->colorAttachmentCount) || (ri->pDepthAttachment && ri->pDepthAttachment->pNext) ||
|
|
(ri->pStencilAttachment && ri->pStencilAttachment->pNext))
|
|
return fallback([&] { ::vkx::vkCmdBeginRendering(cb, ri); });
|
|
Enc e;
|
|
VkRenderingInfo c = *ri;
|
|
c.pColorAttachments = e.dup(ri->pColorAttachments, ri->colorAttachmentCount);
|
|
c.pDepthAttachment = e.dup(ri->pDepthAttachment, 1);
|
|
c.pStencilAttachment = e.dup(ri->pStencilAttachment, 1);
|
|
e.emit(OP_vkCmdBeginRendering, [=] { ::vkx::vkCmdBeginRendering(cb, &c); }, [&] { ::vkx::vkCmdBeginRendering(cb, ri); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdBeginRendering = Route<::vkx::vkCmdBeginRendering, pk_vkCmdBeginRendering>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdCopyBuffer(VkCommandBuffer cb, VkBuffer src, VkBuffer dst, uint32_t n, const VkBufferCopy* r) {
|
|
Enc e;
|
|
const VkBufferCopy* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdCopyBuffer, [=] { ::vkx::vkCmdCopyBuffer(cb, src, dst, n, c); }, [&] { ::vkx::vkCmdCopyBuffer(cb, src, dst, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdCopyBuffer = Route<::vkx::vkCmdCopyBuffer, pk_vkCmdCopyBuffer>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdCopyBufferToImage(VkCommandBuffer cb, VkBuffer src, VkImage dst, VkImageLayout layout, uint32_t n,
|
|
const VkBufferImageCopy* r) {
|
|
Enc e;
|
|
const VkBufferImageCopy* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdCopyBufferToImage, [=] { ::vkx::vkCmdCopyBufferToImage(cb, src, dst, layout, n, c); },
|
|
[&] { ::vkx::vkCmdCopyBufferToImage(cb, src, dst, layout, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdCopyBufferToImage = Route<::vkx::vkCmdCopyBufferToImage, pk_vkCmdCopyBufferToImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdCopyImageToBuffer(VkCommandBuffer cb, VkImage src, VkImageLayout layout, VkBuffer dst, uint32_t n,
|
|
const VkBufferImageCopy* r) {
|
|
Enc e;
|
|
const VkBufferImageCopy* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdCopyImageToBuffer, [=] { ::vkx::vkCmdCopyImageToBuffer(cb, src, layout, dst, n, c); },
|
|
[&] { ::vkx::vkCmdCopyImageToBuffer(cb, src, layout, dst, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdCopyImageToBuffer = Route<::vkx::vkCmdCopyImageToBuffer, pk_vkCmdCopyImageToBuffer>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdCopyImage(VkCommandBuffer cb, VkImage src, VkImageLayout sl, VkImage dst, VkImageLayout dl, uint32_t n,
|
|
const VkImageCopy* r) {
|
|
Enc e;
|
|
const VkImageCopy* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdCopyImage, [=] { ::vkx::vkCmdCopyImage(cb, src, sl, dst, dl, n, c); },
|
|
[&] { ::vkx::vkCmdCopyImage(cb, src, sl, dst, dl, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdCopyImage = Route<::vkx::vkCmdCopyImage, pk_vkCmdCopyImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdBlitImage(VkCommandBuffer cb, VkImage src, VkImageLayout sl, VkImage dst, VkImageLayout dl, uint32_t n,
|
|
const VkImageBlit* r, VkFilter filter) {
|
|
Enc e;
|
|
const VkImageBlit* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdBlitImage, [=] { ::vkx::vkCmdBlitImage(cb, src, sl, dst, dl, n, c, filter); },
|
|
[&] { ::vkx::vkCmdBlitImage(cb, src, sl, dst, dl, n, r, filter); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdBlitImage = Route<::vkx::vkCmdBlitImage, pk_vkCmdBlitImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdResolveImage(VkCommandBuffer cb, VkImage src, VkImageLayout sl, VkImage dst, VkImageLayout dl, uint32_t n,
|
|
const VkImageResolve* r) {
|
|
Enc e;
|
|
const VkImageResolve* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdResolveImage, [=] { ::vkx::vkCmdResolveImage(cb, src, sl, dst, dl, n, c); },
|
|
[&] { ::vkx::vkCmdResolveImage(cb, src, sl, dst, dl, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdResolveImage = Route<::vkx::vkCmdResolveImage, pk_vkCmdResolveImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdClearColorImage(VkCommandBuffer cb, VkImage image, VkImageLayout layout, const VkClearColorValue* color, uint32_t n,
|
|
const VkImageSubresourceRange* ranges) {
|
|
Enc e;
|
|
const VkClearColorValue* col = e.dup(color, 1);
|
|
const VkImageSubresourceRange* c = e.dup(ranges, n);
|
|
e.emit(OP_vkCmdClearColorImage, [=] { ::vkx::vkCmdClearColorImage(cb, image, layout, col, n, c); },
|
|
[&] { ::vkx::vkCmdClearColorImage(cb, image, layout, color, n, ranges); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdClearColorImage = Route<::vkx::vkCmdClearColorImage, pk_vkCmdClearColorImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdClearDepthStencilImage(VkCommandBuffer cb, VkImage image, VkImageLayout layout, const VkClearDepthStencilValue* ds,
|
|
uint32_t n, const VkImageSubresourceRange* ranges) {
|
|
Enc e;
|
|
const VkClearDepthStencilValue* v = e.dup(ds, 1);
|
|
const VkImageSubresourceRange* c = e.dup(ranges, n);
|
|
e.emit(OP_vkCmdClearDepthStencilImage, [=] { ::vkx::vkCmdClearDepthStencilImage(cb, image, layout, v, n, c); },
|
|
[&] { ::vkx::vkCmdClearDepthStencilImage(cb, image, layout, ds, n, ranges); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdClearDepthStencilImage = Route<::vkx::vkCmdClearDepthStencilImage, pk_vkCmdClearDepthStencilImage>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdClearAttachments(VkCommandBuffer cb, uint32_t na, const VkClearAttachment* att, uint32_t nr, const VkClearRect* rects) {
|
|
Enc e;
|
|
const VkClearAttachment* a = e.dup(att, na);
|
|
const VkClearRect* r = e.dup(rects, nr);
|
|
e.emit(OP_vkCmdClearAttachments, [=] { ::vkx::vkCmdClearAttachments(cb, na, a, nr, r); },
|
|
[&] { ::vkx::vkCmdClearAttachments(cb, na, att, nr, rects); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdClearAttachments = Route<::vkx::vkCmdClearAttachments, pk_vkCmdClearAttachments>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdUpdateBuffer(VkCommandBuffer cb, VkBuffer dst, VkDeviceSize offset, VkDeviceSize size, const void* data) {
|
|
Enc e;
|
|
const void* d = e.dup_bytes(data, (size_t)size);
|
|
e.emit(OP_vkCmdUpdateBuffer, [=] { ::vkx::vkCmdUpdateBuffer(cb, dst, offset, size, d); },
|
|
[&] { ::vkx::vkCmdUpdateBuffer(cb, dst, offset, size, data); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdUpdateBuffer = Route<::vkx::vkCmdUpdateBuffer, pk_vkCmdUpdateBuffer>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdBindDescriptorSets(VkCommandBuffer cb, VkPipelineBindPoint bp, VkPipelineLayout layout, uint32_t first, uint32_t n,
|
|
const VkDescriptorSet* sets, uint32_t nd, const uint32_t* dyn) {
|
|
Enc e;
|
|
const VkDescriptorSet* s = e.dup(sets, n);
|
|
const uint32_t* d = e.dup(dyn, nd);
|
|
e.emit(OP_vkCmdBindDescriptorSets, [=] { ::vkx::vkCmdBindDescriptorSets(cb, bp, layout, first, n, s, nd, d); },
|
|
[&] { ::vkx::vkCmdBindDescriptorSets(cb, bp, layout, first, n, sets, nd, dyn); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdBindDescriptorSets = Route<::vkx::vkCmdBindDescriptorSets, pk_vkCmdBindDescriptorSets>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdBindVertexBuffers2(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkBuffer* buffers, const VkDeviceSize* offsets,
|
|
const VkDeviceSize* sizes, const VkDeviceSize* strides) {
|
|
Enc e;
|
|
const VkBuffer* b = e.dup(buffers, n);
|
|
const VkDeviceSize* o = e.dup(offsets, n);
|
|
const VkDeviceSize* s = e.dup(sizes, n); // (null stays null: the whole buffer)
|
|
const VkDeviceSize* st = e.dup(strides, n); // (null stays null: the pipeline's strides)
|
|
e.emit(OP_vkCmdBindVertexBuffers2, [=] { ::vkx::vkCmdBindVertexBuffers2(cb, first, n, b, o, s, st); },
|
|
[&] { ::vkx::vkCmdBindVertexBuffers2(cb, first, n, buffers, offsets, sizes, strides); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdBindVertexBuffers2 = Route<::vkx::vkCmdBindVertexBuffers2, pk_vkCmdBindVertexBuffers2>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdPushConstants(VkCommandBuffer cb, VkPipelineLayout layout, VkShaderStageFlags stages, uint32_t offset, uint32_t size,
|
|
const void* values) {
|
|
Enc e;
|
|
const void* v = e.dup_bytes(values, size);
|
|
e.emit(OP_vkCmdPushConstants, [=] { ::vkx::vkCmdPushConstants(cb, layout, stages, offset, size, v); },
|
|
[&] { ::vkx::vkCmdPushConstants(cb, layout, stages, offset, size, values); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdPushConstants = Route<::vkx::vkCmdPushConstants, pk_vkCmdPushConstants>::call;
|
|
|
|
/// Which of a write's three arrays the driver reads, by its type (the other two are ignored, and left null in the copy); 0: a type
|
|
/// the packer does not know (inline uniform blocks, acceleration structures: their data is in pNext)
|
|
inline int write_kind(VkDescriptorType t) {
|
|
switch (t) {
|
|
case VK_DESCRIPTOR_TYPE_SAMPLER:
|
|
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
|
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
|
|
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
|
|
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return 1;
|
|
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
|
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
|
|
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
|
|
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return 2;
|
|
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
|
|
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return 3;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
RX_REC_PACKER void pk_vkCmdPushDescriptorSetKHR(VkCommandBuffer cb, VkPipelineBindPoint bp, VkPipelineLayout layout, uint32_t set, uint32_t n,
|
|
const VkWriteDescriptorSet* w) {
|
|
for (uint32_t i = 0; i < n; i++)
|
|
if (w[i].pNext || !write_kind(w[i].descriptorType))
|
|
return fallback([&] { ::vkx::vkCmdPushDescriptorSetKHR(cb, bp, layout, set, n, w); });
|
|
Enc e;
|
|
VkWriteDescriptorSet* c = n ? e.alloc<VkWriteDescriptorSet>(n) : nullptr;
|
|
for (uint32_t i = 0; c && i < n; i++) { // (no memory: emit makes the direct call)
|
|
c[i] = w[i];
|
|
c[i].pImageInfo = nullptr, c[i].pBufferInfo = nullptr, c[i].pTexelBufferView = nullptr;
|
|
switch (write_kind(w[i].descriptorType)) {
|
|
case 1: c[i].pImageInfo = e.dup(w[i].pImageInfo, w[i].descriptorCount); break;
|
|
case 2: c[i].pBufferInfo = e.dup(w[i].pBufferInfo, w[i].descriptorCount); break;
|
|
default: c[i].pTexelBufferView = e.dup(w[i].pTexelBufferView, w[i].descriptorCount); break;
|
|
}
|
|
}
|
|
e.emit(OP_vkCmdPushDescriptorSetKHR, [=] { ::vkx::vkCmdPushDescriptorSetKHR(cb, bp, layout, set, n, c); },
|
|
[&] { ::vkx::vkCmdPushDescriptorSetKHR(cb, bp, layout, set, n, w); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdPushDescriptorSetKHR = Route<::vkx::vkCmdPushDescriptorSetKHR, pk_vkCmdPushDescriptorSetKHR>::call;
|
|
|
|
/// (Triforce) vkCmdPushDescriptorSetWithTemplateKHR reads its data by the template's entries, which the call does not carry: its
|
|
/// caller says how many bytes the next such call on its thread reads (vk_draw.cpp push_write_template), and the packer copies that
|
|
/// many. A call it was not told of (0) is not packed: what is held goes first, then the call is made directly (fallback), in order.
|
|
/// (Told only while the recorder is on: with it off the call goes straight to the driver and nothing would take the count)
|
|
inline thread_local size_t t_template_bytes = 0;
|
|
inline void push_template_bytes(size_t n) {
|
|
if (mode() != OFF) t_template_bytes = n;
|
|
}
|
|
RX_REC_PACKER void pk_vkCmdPushDescriptorSetWithTemplateKHR(VkCommandBuffer cb, VkDescriptorUpdateTemplate tpl, VkPipelineLayout layout,
|
|
uint32_t set, const void* data) {
|
|
const size_t bytes = t_template_bytes;
|
|
t_template_bytes = 0;
|
|
if (!bytes) return fallback([&] { ::vkx::vkCmdPushDescriptorSetWithTemplateKHR(cb, tpl, layout, set, data); });
|
|
Enc e;
|
|
const void* d = e.dup_bytes(data, bytes); // (no memory: emit makes the direct call)
|
|
e.emit(OP_vkCmdPushDescriptorSetWithTemplateKHR, [=] { ::vkx::vkCmdPushDescriptorSetWithTemplateKHR(cb, tpl, layout, set, d); },
|
|
[&] { ::vkx::vkCmdPushDescriptorSetWithTemplateKHR(cb, tpl, layout, set, data); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdPushDescriptorSetWithTemplateKHR = Route<::vkx::vkCmdPushDescriptorSetWithTemplateKHR, pk_vkCmdPushDescriptorSetWithTemplateKHR>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetViewport(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkViewport* v) {
|
|
Enc e;
|
|
const VkViewport* c = e.dup(v, n);
|
|
e.emit(OP_vkCmdSetViewport, [=] { ::vkx::vkCmdSetViewport(cb, first, n, c); }, [&] { ::vkx::vkCmdSetViewport(cb, first, n, v); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetViewport = Route<::vkx::vkCmdSetViewport, pk_vkCmdSetViewport>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetScissor(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkRect2D* r) {
|
|
Enc e;
|
|
const VkRect2D* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdSetScissor, [=] { ::vkx::vkCmdSetScissor(cb, first, n, c); }, [&] { ::vkx::vkCmdSetScissor(cb, first, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetScissor = Route<::vkx::vkCmdSetScissor, pk_vkCmdSetScissor>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetViewportWithCount(VkCommandBuffer cb, uint32_t n, const VkViewport* v) {
|
|
Enc e;
|
|
const VkViewport* c = e.dup(v, n);
|
|
e.emit(OP_vkCmdSetViewportWithCount, [=] { ::vkx::vkCmdSetViewportWithCount(cb, n, c); },
|
|
[&] { ::vkx::vkCmdSetViewportWithCount(cb, n, v); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetViewportWithCount = Route<::vkx::vkCmdSetViewportWithCount, pk_vkCmdSetViewportWithCount>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetScissorWithCount(VkCommandBuffer cb, uint32_t n, const VkRect2D* r) {
|
|
Enc e;
|
|
const VkRect2D* c = e.dup(r, n);
|
|
e.emit(OP_vkCmdSetScissorWithCount, [=] { ::vkx::vkCmdSetScissorWithCount(cb, n, c); },
|
|
[&] { ::vkx::vkCmdSetScissorWithCount(cb, n, r); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetScissorWithCount = Route<::vkx::vkCmdSetScissorWithCount, pk_vkCmdSetScissorWithCount>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetBlendConstants(VkCommandBuffer cb, const float blend[4]) {
|
|
Enc e;
|
|
const float* c = e.dup(blend, 4);
|
|
e.emit(OP_vkCmdSetBlendConstants, [=] { ::vkx::vkCmdSetBlendConstants(cb, c); }, [&] { ::vkx::vkCmdSetBlendConstants(cb, blend); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetBlendConstants = Route<::vkx::vkCmdSetBlendConstants, pk_vkCmdSetBlendConstants>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetColorBlendEnableEXT(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkBool32* en) {
|
|
Enc e;
|
|
const VkBool32* c = e.dup(en, n);
|
|
e.emit(OP_vkCmdSetColorBlendEnableEXT, [=] { ::vkx::vkCmdSetColorBlendEnableEXT(cb, first, n, c); },
|
|
[&] { ::vkx::vkCmdSetColorBlendEnableEXT(cb, first, n, en); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetColorBlendEnableEXT = Route<::vkx::vkCmdSetColorBlendEnableEXT, pk_vkCmdSetColorBlendEnableEXT>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetColorBlendEquationEXT(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkColorBlendEquationEXT* eq) {
|
|
Enc e;
|
|
const VkColorBlendEquationEXT* c = e.dup(eq, n);
|
|
e.emit(OP_vkCmdSetColorBlendEquationEXT, [=] { ::vkx::vkCmdSetColorBlendEquationEXT(cb, first, n, c); },
|
|
[&] { ::vkx::vkCmdSetColorBlendEquationEXT(cb, first, n, eq); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetColorBlendEquationEXT = Route<::vkx::vkCmdSetColorBlendEquationEXT, pk_vkCmdSetColorBlendEquationEXT>::call;
|
|
|
|
RX_REC_PACKER void pk_vkCmdSetColorWriteMaskEXT(VkCommandBuffer cb, uint32_t first, uint32_t n, const VkColorComponentFlags* wm) {
|
|
Enc e;
|
|
const VkColorComponentFlags* c = e.dup(wm, n);
|
|
e.emit(OP_vkCmdSetColorWriteMaskEXT, [=] { ::vkx::vkCmdSetColorWriteMaskEXT(cb, first, n, c); },
|
|
[&] { ::vkx::vkCmdSetColorWriteMaskEXT(cb, first, n, wm); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetColorWriteMaskEXT = Route<::vkx::vkCmdSetColorWriteMaskEXT, pk_vkCmdSetColorWriteMaskEXT>::call;
|
|
|
|
/// ((Triforce) foliage_shadow.h: the fragment size and the two combiner operations the driver reads)
|
|
RX_REC_PACKER void pk_vkCmdSetFragmentShadingRateKHR(VkCommandBuffer cb, const VkExtent2D* size, const VkFragmentShadingRateCombinerOpKHR ops[2]) {
|
|
Enc e;
|
|
const VkExtent2D* s = e.dup(size, 1);
|
|
const VkFragmentShadingRateCombinerOpKHR* o = e.dup(ops, 2);
|
|
e.emit(OP_vkCmdSetFragmentShadingRateKHR, [=] { ::vkx::vkCmdSetFragmentShadingRateKHR(cb, s, o); },
|
|
[&] { ::vkx::vkCmdSetFragmentShadingRateKHR(cb, size, ops); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetFragmentShadingRateKHR = Route<::vkx::vkCmdSetFragmentShadingRateKHR, pk_vkCmdSetFragmentShadingRateKHR>::call;
|
|
|
|
/// (the marker is an opaque value the driver hands back, never read through: kept as the pointer value)
|
|
RX_REC_PACKER void pk_vkCmdSetCheckpointNV(VkCommandBuffer cb, const void* marker) {
|
|
Enc e;
|
|
e.emit(OP_vkCmdSetCheckpointNV, [=] { ::vkx::vkCmdSetCheckpointNV(cb, marker); }, [&] { ::vkx::vkCmdSetCheckpointNV(cb, marker); });
|
|
}
|
|
inline constexpr auto& rx_vkCmdSetCheckpointNV = Route<::vkx::vkCmdSetCheckpointNV, pk_vkCmdSetCheckpointNV>::call;
|
|
|
|
} // namespace mkwvk::rec
|