mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 09:19:58 -04:00
Seal the texture pool set when an early replay part is submitted.
A replay part handed to the queue early kept reading the bindless pool set while later parts of the same submission wrote new entries into it. That updates descriptors a pending command buffer reads, and on RADV a texture whose memory was mapped after the part was submitted is absent from its page tables. This caused a gfxhub read fault in the 128x128 pass of the first world frame in Kokiri Forest. Seal the set when a part goes, take another ring set at the next sync that has entries to write, and keep recording the same part while every other set belongs to the submission. Document the descriptor lifetime contract in maxwell.md 4.9. Fixes #257.
This commit is contained in:
@@ -89,6 +89,7 @@ Source shorthands:
|
||||
| 4.6 | **Compute shared memory**: 48 KiB per block on GM20B. | AMD 64 KiB. | Ours: `QueryComputeSharedMemorySize` from job (`Program.cs`). | OGD `clb1c0.h` |
|
||||
| 4.7 | **Virtual memory pools** map physical pool pages; the same bytes reachable at two GPU addresses. | Two `VkBuffer`s over one allocation need alias-aware barriers. | — | — |
|
||||
| 4.8 | **Command list calls**: a `CallCommands` / `CallList` is not a GPU subroutine. A call is stored as a control entry; at submit the CPU walks the entries and recurses into the called list, emitting gpfifo entries that point at the command words, which the GPU fetches later (prefetch can be switched off per entry). So list structure is fixed at submit, command words are read when fetched. | n/a | Ours: `OP_CALL` is walked at replay, `CallCommands` inlines the recording at record time (`vk_exec.c` `walk`, `nvn_api_cmd.c` `inline_recording`). Equivalent for control flow; deferred bind handles are the data read late (2.4). | — |
|
||||
| 4.9 | **The texture pool is read as the GPU goes** (bindless `TEX.B` handles index it), and the GPU runs a submission right after it is submitted, so it sees the pool as of about the submit; Ocarina registers the next submission's textures right after submitting. | A descriptor a pending command buffer may read must not be updated (update-after-bind allows only unused ones), and on RADV a view of memory allocated after a submit is not in that submit's page tables. | A replay part handed to the queue early (`NX2NAVI_SPLIT_DRAWS`) kept reading the set later parts wrote: GPU page fault (`gfxhub`, TCP read) on the 128×128 probe pass of Kokiri Forest's first world frame, about 40 s in, nearly every run on RX 9070 XT. Ours: one pool set per submission from a ring, sealed when a part goes; the next sync that has entries to write takes another set, and no part is split off while every other set belongs to the same submission (`vk_exec_draw.c` `vk_exec_pool_split`, `pool_take_set`). | — |
|
||||
|
||||
## 5. Fixed function
|
||||
|
||||
|
||||
@@ -870,7 +870,8 @@ static void set_rt(VkExec *ex, const p_set_rt *r) {
|
||||
// command-buffer state is lost there (bindings and dynamic state: vk_exec_draw_new_cb, ex->dirty); barriers, layouts and
|
||||
// queries are not: a barrier's first scope is everything earlier in submission order, image layouts belong to the image,
|
||||
// and occlusion queries end with their pass. Fences, completion callbacks and reports stay per submission: only the last
|
||||
// part signals the value.
|
||||
// part signals the value. The texture pool set a part binds is sealed when the part goes (vk_exec_pool_split): later
|
||||
// parts write and bind another, and a part is not split off while no other set is free of this submission.
|
||||
// A part also spans at least NX2NAVI_SPLIT_US (1000) of replay time: a split costs about 40 us of the render thread
|
||||
// (vkQueueSubmit2, a command buffer, the mirror prologue), and a fight frame of 2,000 draws made 15 parts on the 7900X;
|
||||
// a millisecond of recording is a fraction of a millisecond of GPU work, so the GPU still starts early.
|
||||
@@ -922,8 +923,8 @@ static void maybe_split(VkExec *ex) {
|
||||
}
|
||||
split_draws = 0;
|
||||
split_at = split_n;
|
||||
if (idle && ex->recorded != part_start) n_idle_parts++;
|
||||
if (ex->recorded == part_start) return;
|
||||
if (ex->recorded == part_start || !vk_exec_pool_split(ex->fence)) return;
|
||||
if (idle) n_idle_parts++;
|
||||
if (ex->in_pass) vk_exec_pass_end(ex), n_pass_splits++; // the next draw begins the pass again, in the new part
|
||||
ex->cb = vk_sync_split();
|
||||
part_start = ex->recorded;
|
||||
|
||||
@@ -137,6 +137,10 @@ void vk_exec_inline_end(int reopen);
|
||||
// vk_exec_draw.c: forget what the command buffer being recorded has bound (pipeline, sets, dynamic state, vertex
|
||||
// buffers): the replay continues in a new one (vk_sync_split).
|
||||
void vk_exec_draw_new_cb(void);
|
||||
// vk_exec_draw.c, before a part of submission fence goes to the queue (vk_exec.c maybe_split): 1 when it may go. The
|
||||
// texture pool set it binds is then sealed and the next draw that needs the pools takes another; 0 when every other set
|
||||
// is bound by this submission's earlier parts, and the replay keeps recording into the same part.
|
||||
int vk_exec_pool_split(uint64_t fence);
|
||||
// vk_exec_draw.c: the vertex and fragment shader of the last draw recorded into this command buffer (NULL: none yet).
|
||||
void vk_exec_draw_shaders(const GfxShader **vs, const GfxShader **fs);
|
||||
VkBuffer vk_exec_inline_buffer(VkDeviceSize size);
|
||||
|
||||
@@ -1702,8 +1702,13 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto
|
||||
// dialogue frames, a fifth of them; none once each submission waited for its GPU work, NX2NAVI_SUBMIT_FINISH). A
|
||||
// submission's first pool_sync takes the next set (pool_take_set), waits for the GPU only if that set's last
|
||||
// submission is still running, and brings it up to date from the change log (every entry written since the set was
|
||||
// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, as
|
||||
// before.
|
||||
// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, until a
|
||||
// part of the submission goes to the queue early (vk_exec.c maybe_split): that part may read any entry of the set, so
|
||||
// the set is sealed (vk_exec_pool_split) and the next sync takes the following set as if a new submission began.
|
||||
// Writing into it instead updated descriptors a pending command buffer reads, which Vulkan does not allow: the part
|
||||
// sampled textures registered after it went, and on RADV a texture whose memory was mapped after the part's submit is
|
||||
// absent from that submit's page tables, a GPU page fault on the 128x128 probe pass of Kokiri Forest's first frame
|
||||
// (RADV's radv_wait_for_vm_map_updates hid it, as did NX2NAVI_SPLIT_DRAWS=0).
|
||||
volatile uint32_t vk_pool_kinds_used;
|
||||
#define POOL_SETS 4
|
||||
#define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write
|
||||
@@ -1722,7 +1727,7 @@ static struct {
|
||||
uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh;
|
||||
uint64_t n_by_reg, n_by_addr; // refills whose token changed by a registration, by an address generation only
|
||||
uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot)
|
||||
int ready, uab;
|
||||
int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur]
|
||||
uint32_t kinds, gen, epoch, sgen;
|
||||
int retry; // the last sync left slots to the creation budget
|
||||
uint64_t *tok; // per texture slot: vk_texture_slot_token it was filled under (0: stand-ins)
|
||||
@@ -1871,18 +1876,29 @@ static void pool_entry_to(VkDescriptorSet set, uint32_t e) {
|
||||
// A submission's first pool use: the next set of the ring, once the GPU is done with it. 1 when it switched; *from is
|
||||
// the log position the set holds writes up to (pool_catch_up brings it level once pool_sync has refreshed the entries:
|
||||
// a view written into a set must be alive, and the refresh is what replaces the views of destroyed textures).
|
||||
static int pool_current(void);
|
||||
|
||||
static int pool_take_set(VkExec *ex, uint64_t *from) {
|
||||
if (PL.cur_fence == ex->fence) return 0;
|
||||
// a sealed set stays bound while it is current: reading it is what the pending part does too
|
||||
if (PL.cur_fence == ex->fence && (!PL.sealed || pool_current())) return 0;
|
||||
int i = (PL.cur + 1) % PL.nsets;
|
||||
if (PL.nsets > 1) {
|
||||
const GfxBackend *b = gfx_backend();
|
||||
if (b->fence_done() < PL.set_fence[i]) PL.waits++, b->fence_wait(PL.set_fence[i], UINT64_MAX);
|
||||
}
|
||||
*from = PL.set_synced[i];
|
||||
PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence;
|
||||
PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence, PL.sealed = 0;
|
||||
return PL.nsets > 1;
|
||||
}
|
||||
|
||||
int vk_exec_pool_split(uint64_t fence) {
|
||||
if (PL.cur_fence != fence || PL.sealed) return 1; // no part of this submission has the current set unsealed
|
||||
// pool_take_set moves to the next set of the ring: it must not be bound by this submission's earlier parts too
|
||||
if (PL.nsets < 2 || PL.set_fence[(PL.cur + 1) % PL.nsets] == fence) return 0;
|
||||
PL.sealed = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now.
|
||||
static uint64_t pool_memo_hits; // pool_fill: slots that shared another slot's lookup (defined with pool_memo below)
|
||||
|
||||
@@ -2008,6 +2024,7 @@ static void pool_sync(VkExec *ex) {
|
||||
if (!PL.layout) vk_pool_set_layout();
|
||||
uint64_t from = 0, pre = PL.logn;
|
||||
int switched = pool_take_set(ex, &from); // every submission its own set, before the shortcut
|
||||
if (PL.sealed) return; // current when checked: a change since waits for the next sync, which takes a new set
|
||||
uint64_t t0 = kern_now_ns();
|
||||
pool_sync_entries(ex);
|
||||
uint64_t t1 = kern_now_ns();
|
||||
@@ -2035,6 +2052,12 @@ static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_loc
|
||||
else d->retry = 1;
|
||||
}
|
||||
|
||||
// Every entry as the slots, kinds and samplers are now (pool_sync_entries has nothing to write).
|
||||
static int pool_current(void) {
|
||||
return PL.ready && !PL.retry && __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED) == PL.kinds &&
|
||||
vk_texture_slots_gen() == PL.gen && vk_texture_epoch() == PL.epoch && vk_sampler_gen() == PL.sgen;
|
||||
}
|
||||
|
||||
static void pool_sync_entries(VkExec *ex) {
|
||||
uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED);
|
||||
uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen();
|
||||
|
||||
Reference in New Issue
Block a user