// (Triforce, 11-26, the GPU audit's C15) A plain draw's capture push left out while its command buffer holds zeros there. // // push_capture (vk_draw.cpp) pushes 16 bytes before every plain draw of every program whose vertex stage captures its // positions (every plain vertex stage): where the stage stores them (an address, a first, a count), zeros when the draw is // given no slot. Without a temporal upscaler's chain asking for positions no draw is given one, so every draw pushed the // same zeros: a vkCmdPushConstants a draw (about 2,000 a frame at Lake Hylia, 1.7% of the Presentation Thread's samples // there), each a packet for the recorder thread as well. Push constants stay on a command buffer until something pushes // over them or binds a pipeline whose layout has another block, so the zeros are pushed once and kept. // // What is kept is one word a command buffer (cmd_work.h CmdWork::cap): the push stages of the layouts whose capture bytes // are known to hold zeros, 0 for "not known". Only zeros are ever kept: a draw that is given a slot always pushes, and so // does the first draw without one after it. A stale address there would be written through by the next draw's vertex stage // (another draw's slot at best, a lost device at worst), so what forgets is central and errs towards pushing: // - every vkBeginCommandBuffer of the renderer (cmd_work.h work_begun: a handle recorded again holds nothing); // - every vkCmdPushConstants of the renderer, whatever its layout, stages and range (vk_work.h's macro: the compute // passes recorded between draws push over the same bytes of the command buffer; the draw's own pushes go through it // too and note what they left after); // - every vkCmdBindPipeline of the renderer (vk_work.h's macro). The program path's own bind in draw() goes through it // too, and takes back what was held before it only for a program with the same push stages (every program's layout // has the one range {its stages, 0, the block's size}, so equal stages are layouts compatible for push constants: the // strict reading of the specification, nothing a driver is trusted with beyond it), and never when the draw state was // reset before it (last_pipeline cleared: something was recorded that these hooks may not have seen, a library's pass); // - every recording a library makes into a command buffer itself, past those macros (Dear ImGui's overlays, NGX's, FFX's // and XeSS's passes): each is announced by the recorder's boundary(B_DIRECT) (vk_recorder.h; tests/test_recorder_sync.py // holds every such call to it), which moves direct_gen, and what any command buffer holds from before is void; // - a command buffer the table has no room for (CmdWork::known false) never keeps. // A program's whole block (push_constants) has zeros in the capture's bytes and is noted as such after its push. // // RX_VK_CAPTURE_PUSH_KEEP=0: pushed before every such draw, as before (every push and bind is still noted, so this is // not v0.8.4's cost). =check: pushed before every draw too, and each push the rule would have left out is counted, and // counted as differing when shadow_zero does not have the bytes as zeros. shadow_zero follows the pushes that vk_work.h's // macro sees (a push covering the 16 bytes sets it by their value, one covering a part of them clears it, one beside them // leaves it, a begin clears it): the rule is fed by the same calls, so the count is 0 by construction for every push and // bind vk_work.h sees. It compares what full_pushed and pushed claim with the bytes pushed through vk_work.h, and nothing // more: it ignores binds (program_bound is not checked by it), and a 0 is no evidence that a library's recording or a // call past vk_work.h is covered. Those are held by the B_DIRECT boundary before each library call // (tests/test_recorder_sync.py) and by the scan for calls past the macros in tests/test_capturepushkeep.py. Any other // value (not 0, 1 or check) keeps, as by default, and is told once. The lab's knob nocappush (RX_VK_LAB_AB) pushes before // every draw in its phases. // The frame statistics' "a frame's capture pushes" line counts the pushes kept and made. // // The rule alone, no Vulkan: tests/test_capturepushkeep.py compiles it. #pragma once #include #include #include namespace capturepushkeep { /// The capture's bytes in a program's push block (push_capture pins them to the shader back end's CAPTURE_PUSH_OFFSET) constexpr uint32_t kOffset = 88, kSize = 16; /// The switch's value: kept unless it is 0 or check enum Mode { OFF = 0, KEEP = 1, CHECK = 2 }; inline Mode mode_of(const char* asked) { if (asked && !std::strcmp(asked, "0")) return OFF; if (asked && !std::strcmp(asked, "check")) return CHECK; return KEEP; } /// Moved by every recording made into a command buffer past vk_work.h's macros (a library's: the recorder's B_DIRECT /// boundary), any thread's, any command buffer's: a few a frame at most inline std::atomic direct_gen{0}; inline void direct_recorded() { direct_gen.fetch_add(1, std::memory_order_relaxed); } /// One command buffer's struct State { uint32_t zero_stages = 0; // (the push stages whose layouts' capture bytes hold zeros; 0: not known) uint32_t gen = 0; // (direct_gen when zero_stages was set) uint8_t shadow_zero = 0; // (check mode's model: 1 when the last pushes over the bytes left them all zeros) }; inline bool all_zero(const uint8_t* data) { uint64_t a, b; std::memcpy(&a, data, 8), std::memcpy(&b, data + 8, 8); return (a | b) == 0; } /// The command buffer begun: nothing is held inline void begun(State& s) { s.zero_stages = 0, s.shadow_zero = 0; } /// Any push constants recorded into it (before the call): forgotten, whatever the layout, the stages and the range. The /// model: the bytes are zeros only by a push that covers all 16 of them with zeros inline void any_push(State& s, uint32_t offset, uint32_t size, const void* values) { s.zero_stages = 0; const uint64_t end = (uint64_t)offset + size; if (offset >= kOffset + kSize || end <= kOffset) return; // (beside the bytes) s.shadow_zero = values && offset <= kOffset && end >= kOffset + kSize && all_zero(static_cast(values) + (kOffset - offset)); } /// Any pipeline bound, at any bind point (before the call): forgotten inline void any_bind(State& s) { s.zero_stages = 0; } /// The program path bound a program's pipeline (after its any_bind; held: zero_stages as it was before the bind): held /// again only for the same push stages, and not across a draw state reset inline void program_bound(State& s, uint32_t held, uint32_t stages, bool from_reset) { s.zero_stages = !from_reset && held == stages ? held : 0; } /// A program's whole block was pushed, zeros in the capture's bytes (after its any_push) inline void full_pushed(State& s, uint32_t stages) { s.zero_stages = stages, s.gen = direct_gen.load(std::memory_order_relaxed); } /// Whether a draw's capture push (data, 16 bytes) may be left out: only zeros, over zeros known to be held for these stages, /// with no library's recording since they were pushed inline bool keep(const State& s, bool known, uint32_t stages, const uint8_t* data) { return known && stages != 0 && s.zero_stages == stages && s.gen == direct_gen.load(std::memory_order_relaxed) && all_zero(data); } /// The draw's capture push was made (after its any_push): zeros are held from here, anything else is not inline void pushed(State& s, uint32_t stages, const uint8_t* data) { s.zero_stages = all_zero(data) ? stages : 0, s.gen = direct_gen.load(std::memory_order_relaxed); } /// Check mode, at a push keep() would have left out: whether the model does not have zeros there (false by construction /// while every push and bind goes through vk_work.h: what pushed and full_pushed claim is checked, not the bind rule) inline bool check_differs(const State& s) { return !s.shadow_zero; } } // namespace capturepushkeep