mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
An NVN event signal no longer ends its Metal command buffer, and with it the open render pass: a replayed piece keeps every signal it records (issue #2112, PR #2116)
Sinceea0c98dfthe queue cut a recording at each nvnCommandBufferSignalEvent. Each piece is replayed as a Metal command buffer of its own, so the open render pass ended there and loaded its attachments again. BioShock Infinite signals an event per occlusion query: its title screen went from 88 to 189 render passes a frame, from 734 MB to 2.58 GB of attachment loads and from 3.8 to 7.1-8.4 ms of submit CPU (measured by the reporter). The cut bought nothing: the fence node appended after a signal had a receipt nothing referred to, and a signal's value is published when its submission completes, any number per submission. The cut's one effect was that each piece had a single signal for the queue to record. A piece now keeps a list of its signals and the queue records each at the piece's commit, so a wait on any of them, in flight, is still released at encode (04419bc2). Only what waits or is waited for cuts: an event wait, a sync wait, a recorded fence. A waiter on another queue is released after the rest of the signalling piece, not after the signal's prefix, as beforeea0c98df. No wait can sit in the same piece as its signal, since waits cut, and every piece commits with no further guest action. Checked: t_wait_event 97 checks in both encoder modes, with a new case (two signals with commands between them are one submission, and a wait on the first is encoded behind it) that fails without the change; t_queue_lifetime, t_render_enable_queue, t_render_counter_overlap, t_nvn, t_capture and test_docs pass; the Mac host builds. No game ran: BioShock Infinite is not on this Mac, and no local game that records signals has shader libraries for chain cb2e91c138e3. The pass counts above are the reporter's. Co-authored-by: Stephen Schloesser <[email protected]>
This commit is contained in:
1 parent
ab41d6a177
commit
25c2546ed8
4 files changed
+52
-8
No files matched your search
@@ -7,11 +7,13 @@ void objc_autoreleasePoolPop(void *);
|
||||
// Ready continuations of NVN queues share the renderer's one ordered Metal queue.
|
||||
// An unresolved wait parks its queue, never the submitting CPU or another queue.
|
||||
typedef struct nq_receipt { unsigned refs, published; uint64_t fence, present; } nq_receipt;
|
||||
typedef struct nq_signal { volatile uint32_t *event; unsigned mode; uint32_t value; } nq_signal;
|
||||
typedef struct nq_node {
|
||||
struct nq_node *next;
|
||||
uint8_t *bytes; size_t length, capacity;
|
||||
unsigned kind, mode, closed, waiting; volatile uint32_t *event; uint32_t value;
|
||||
uint64_t wait_fence, event_predecessor; unsigned wait_kind; const sync_t *sync_source; nq_receipt *receipt;
|
||||
nq_signal *signals; unsigned signal_count; // NQ_COMMANDS: every event signal it records, in order (nq_event_issued at its commit)
|
||||
tex_t texture; double period; int crop[4];
|
||||
} nq_node;
|
||||
typedef struct nq_owned { struct nq_owned *next; unsigned kind; uint64_t data[]; } nq_owned;
|
||||
@@ -168,15 +170,22 @@ static void *nq_worker(void *unused) {
|
||||
}
|
||||
nq_cost_end(cost); gfx_leave(); objc_autoreleasePoolPop(pool); pthread_mutex_unlock(&g_sub_mu);
|
||||
}
|
||||
pthread_mutex_lock(&nq_mu); if(n->kind==NQ_PRESENT) { q->present=present; j->receipt->present=present; } if(fence) { q->physical=fence; if(n->event) nq_event_issued(n->event,fence,n->mode,n->value); } if(n->receipt) nq_put(n->receipt); free(n->bytes); free(n); pthread_cond_broadcast(&nq_cv); pthread_mutex_unlock(&nq_mu);
|
||||
pthread_mutex_lock(&nq_mu); if(n->kind==NQ_PRESENT) { q->present=present; j->receipt->present=present; } if(fence) { q->physical=fence; for(unsigned i=0;i<n->signal_count;i++) nq_event_issued(n->signals[i].event,fence,n->signals[i].mode,n->signals[i].value); } if(n->receipt) nq_put(n->receipt); free(n->signals); free(n->bytes); free(n); pthread_cond_broadcast(&nq_cv); pthread_mutex_unlock(&nq_mu);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static void nq_start(void) { pthread_t t; if(pthread_create(&t,NULL,nq_worker,NULL)) die("NVN queue worker failed"); pthread_detach(t); }
|
||||
static nq_node *nq_append(nq_job *j,unsigned kind) { nq_node *n=calloc(1,sizeof *n); if(!n) die("queue continuation allocation failed"); n->kind=kind; if(j->last) j->last->next=n; else j->first=n; j->last=n; return n; }
|
||||
static void nq_bytes(nq_node *n,const void *bytes,size_t length) { if(n->length+length>n->capacity) { n->capacity=(n->length+length+4095)&~(size_t)4095; n->bytes=realloc(n->bytes,n->capacity); if(!n->bytes) die("queue commands allocation failed"); } memcpy(n->bytes+n->length,bytes,length); n->length+=length; } // NOLINT(bugprone-suspicious-realloc-usage): allocation failure aborts, so the old buffer cannot leak into a continuing process
|
||||
static void nq_signal_add(nq_node *n,nq_signal s) {
|
||||
if(!(n->signal_count&(n->signal_count-1))) { n->signals=realloc(n->signals,(n->signal_count?2*n->signal_count:1)*sizeof *n->signals); if(!n->signals) die("queue signals allocation failed"); } // NOLINT(bugprone-suspicious-realloc-usage): allocation failure aborts
|
||||
n->signals[n->signal_count++]=s;
|
||||
}
|
||||
static void nq_end(nq_job *j) { if(j->last && j->last->kind==NQ_COMMANDS && !j->last->closed) { op_hdr end={OP_END,8}; nq_bytes(j->last,&end,8); j->last->closed=1; } }
|
||||
// One record of a recording, copied into the job (its object pointers snapshotted); waits and fences cut the job
|
||||
// One record of a recording, copied into the job (its object pointers snapshotted); waits and fences cut the job. A signal does not: its value is published when the commands
|
||||
// it is among complete (mtl_sub_event), and a wait on it is ordered by that value or, while it is in flight, by the fence of those commands (nq_event_write), so the node keeps
|
||||
// every signal it records. Each node is a Metal command buffer of its own, so a cut ends the open render pass: BioShock Infinite signals an event per occlusion query, and a cut
|
||||
// at each took its title screen from 88 to 189 passes a frame and from 734 MB to 2.58 GB of attachment loads (issue #2112)
|
||||
static void nq_record_one(nq_job *j,const uint8_t *p,op_hdr h) {
|
||||
const uint8_t *a=p+8;
|
||||
if(h.op==OP_END || h.op==OP_JUMP || h.op==OP_CALL) die("NVN command list: a %s record inside a recording's piece at %p",h.op==OP_END?"END":h.op==OP_JUMP?"JUMP":"CALL",(const void *)p);
|
||||
@@ -188,7 +197,9 @@ static void nq_record_one(nq_job *j,const uint8_t *p,op_hdr h) {
|
||||
nq_node *n=j->last; if(!n || n->kind!=NQ_COMMANDS || n->closed) n=nq_append(j,NQ_COMMANDS);
|
||||
size_t offset=n->length; nq_bytes(n,p,h.size); if(!n->bytes) die("queue commands: a record of %u bytes",h.size);
|
||||
if(!cap_rec_fields(h.op,n->bytes+offset+8,h.size,&nq_snapshot_ops,j)) die("queue snapshot: unknown command %u",h.op);
|
||||
if(h.op==OP_SIGNAL_EVENT) { memcpy(&n->event,a,8); memcpy(&n->mode,a+8,4); memcpy(&n->value,a+12,4); nq_end(j); nq_append(j,NQ_FENCE)->receipt=nq_new(); }
|
||||
if(h.op==OP_SIGNAL_EVENT) {
|
||||
nq_signal sig; memcpy(&sig.event,a,8); memcpy(&sig.mode,a+8,4); memcpy(&sig.value,a+12,4); nq_signal_add(n,sig);
|
||||
}
|
||||
}
|
||||
}
|
||||
// A compact record (nvn_obj.h OPC_*) goes into the job as the canonical records it stands for
|
||||
|
||||
@@ -259,6 +259,37 @@ static void event_queued_write(int mode) {
|
||||
CHECK(done && *result==after,"the consumer's copy reads the producer's payload"); if(!done) { P(nvnEventSignal)(&event,NVN_EVENT_SIGNAL_MODE_WRITE,1); _Exit(2); }
|
||||
P(nvnSyncFinalize)(&produced); P(nvnSyncFinalize)(&consumed);
|
||||
}
|
||||
// A signal does not cut its recording (issue #2112): two signals with commands between them are one Metal submission, and a wait on the first of them, which is not the
|
||||
// recording's last, is still encoded behind the in-flight write
|
||||
static void event_signal_no_cut(void) {
|
||||
printf("CASE signals stay in their recording's submission\n");
|
||||
*source=before; *result=sentinel; P(nvnEventSignal)(&event,NVN_EVENT_SIGNAL_MODE_WRITE,0);
|
||||
NVNevent second; NVNeventBuilder eb={0}; P(nvnEventBuilderSetDefaults)(&eb); P(nvnEventBuilderSetStorage)(&eb,&pool,65548); CHECK(P(nvnEventInitialize)(&second,&eb),"second event initialized");
|
||||
P(nvnEventSignal)(&second,NVN_EVENT_SIGNAL_MODE_WRITE,0);
|
||||
id<MTLSharedEvent> gate=[g_dev newSharedEvent];
|
||||
if (!gate) abort();
|
||||
pthread_mutex_lock(&g_sub_mu); pthread_mutex_lock(&g_res_mu); [g_q4 waitForEvent:gate value:1]; uint64_t first_fence=mtl_last_fence(); pthread_mutex_unlock(&g_res_mu); pthread_mutex_unlock(&g_sub_mu);
|
||||
NVNsync produced,consumed; P(nvnSyncInitialize)(&produced,&device); P(nvnSyncInitialize)(&consumed,&device);
|
||||
begin_cmd(1); uint32_t payload=after, other=before;
|
||||
P(nvnCommandBufferSignalEvent)(&commands[1],&event,NVN_EVENT_SIGNAL_MODE_WRITE,NVN_EVENT_SIGNAL_LOCATION_BOTTOM,0,1);
|
||||
P(nvnCommandBufferUpdateUniformBuffer)(&commands[1],source_addr,4,0,4,&other);
|
||||
P(nvnCommandBufferUpdateUniformBuffer)(&commands[1],source_addr,4,0,4,&payload);
|
||||
P(nvnCommandBufferSignalEvent)(&commands[1],&second,NVN_EVENT_SIGNAL_MODE_WRITE,NVN_EVENT_SIGNAL_LOCATION_BOTTOM,0,7);
|
||||
P(nvnCommandBufferFenceSync)(&commands[1],&produced,NVN_SYNC_CONDITION_ALL_GPU_COMMANDS_COMPLETE,0);
|
||||
job pj,cj; enqueue(&pj,&queues[1],P(nvnCommandBufferEndRecording)(&commands[1])); int pr=returned(&pj,250000000ull);
|
||||
CHECK(pr && receipt_encoded(&produced,1000000000ull),"producer encodes while its GPU work is held by a shared-event gate");
|
||||
pthread_mutex_lock(&g_sub_mu); uint64_t submissions=mtl_last_fence()-first_fence; pthread_mutex_unlock(&g_sub_mu);
|
||||
CHECK(submissions==1,"both signals and the commands between them are one Metal submission");
|
||||
enqueue(&cj,&queues[0],consumer(NVN_EVENT_WAIT_MODE_EQUAL,1,&consumed,1)); int cr=returned(&cj,250000000ull);
|
||||
CHECK(receipt_encoded(&consumed,1000000000ull),"a wait on the first signal is encoded behind the in-flight submission");
|
||||
CHECK(!landed(&produced,0) && !landed(&consumed,0) && *result==sentinel && P(nvnEventGetValue)(&event)==0,"nothing has run while the gate holds the GPU");
|
||||
gate.signaledValue=1; // always release the GPU before cleanup, including assertion failures
|
||||
require_returned(&pj,pr); require_returned(&cj,cr);
|
||||
int done=landed(&produced,5000000000ull) && landed(&consumed,5000000000ull);
|
||||
CHECK(done && *result==after,"the consumer's copy reads what the producer wrote after the first signal"); if(!done) { P(nvnEventSignal)(&event,NVN_EVENT_SIGNAL_MODE_WRITE,1); _Exit(2); }
|
||||
CHECK(P(nvnEventGetValue)(&event)==1 && P(nvnEventGetValue)(&second)==7,"both events hold their signalled values");
|
||||
P(nvnSyncFinalize)(&produced); P(nvnSyncFinalize)(&consumed); P(nvnEventFinalize)(&second);
|
||||
}
|
||||
// A wait on a sync no fence has reached when it is submitted is already satisfied: the queue's own later work, its fence included, runs (issue #1808)
|
||||
static void receipt_unfenced(int command_wait) {
|
||||
printf("CASE %s wait on a never-fenced sync does not park its queue\n",command_wait?"command-buffer":"queue");
|
||||
@@ -325,6 +356,7 @@ int main(void) { @autoreleasepool {
|
||||
event_stale_gpu_reset();
|
||||
event_queued_write(NVN_EVENT_SIGNAL_MODE_WRITE);
|
||||
event_queued_write(NVN_EVENT_SIGNAL_MODE_ADD);
|
||||
event_signal_no_cut();
|
||||
receipt_tests();
|
||||
event_mirror_publication();
|
||||
pthread_mutex_lock(&jobs_mu); jobs_stop=1; pthread_cond_signal(&jobs_cv); pthread_mutex_unlock(&jobs_mu); pthread_join(worker,NULL);
|
||||
|
||||
@@ -567,7 +567,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- a jump to pc=0 on a worker thread, with the backtrace `0x0` ← `cmd_alloc_keep` ← `h_nvnCommandBufferCopyBufferToTexture` and `lr` in the game's `main`, just after the instruction that called the game's command memory callback. → [Copy strides are command buffer state](../reference/nvn-objects.md#copy-strides-are-command-buffer-state)
|
||||
- `CRASH sig=11 addr=0x50` on a game thread, at `pc=main+0x385201c` in Super Smash Bros. Ultimate (`ldr x0, [x8, #0x10]` with `x8 = 0x40`), as a battle starts. Rare: it crashed on 1 of 2 battle starts in one run on a Mac mini and on none of 11 in another. → [`GetCommandMemorySize` read while another thread records](../reference/nvn-objects.md#getcommandmemorysize-read-while-another-thread-records)
|
||||
- the title showed one stretched 64×64 icon over the 3D scene, a new game a white screen; post-process passes with stale texture handles; `[RED] bind record … overwritten after recording`. → [Recorded commands patched in place by the game (`OP_BIND_TEX1`)](../reference/nvn-objects.md#recorded-commands-patched-in-place-by-the-game-op_bind_tex1)
|
||||
- intermittent scene-wide lighting flashes and a persistent pale outline around a character. Extra GPU readbacks or synchronous screenshots can make the flashes disappear temporarily. → [GPU event waits order work across queues](../reference/nvn-objects.md#gpu-event-waits-order-work-across-queues)
|
||||
- intermittent scene-wide lighting flashes and a persistent pale outline around a character. Extra GPU readbacks or synchronous screenshots can make the flashes disappear temporarily. Separately: twice the render passes and attachment loads a frame, with a correct picture, in a game that signals events inside its passes. → [GPU event waits order work across queues](../reference/nvn-objects.md#gpu-event-waits-order-work-across-queues)
|
||||
- choosing X-2 after the collection launcher crashes in `keys_hash`, reading control address `0x8000000000`; the program object now contains command-buffer data. → [Bindings belong to each queue](../reference/nvn-objects.md#bindings-belong-to-each-queue)
|
||||
- about 23 s after launch, when the title screen hands over to the opening demo, a crash in `keys_hash` (`prog_key` ← `prog_get` ← `bind_stages` ← `prog_resolve` ← `mtl_queue_activate`), reading a control address made of float data; the program object now holds other data. → [Finalized programs leave every queue's bindings](../reference/nvn-objects.md#finalized-programs-leave-every-queues-bindings)
|
||||
- `nvnCommandBufferFenceSync` unimplemented, every `SyncWait` timed out at once. → [Fence syncs recorded in command buffers](../reference/nvn-objects.md#fence-syncs-recorded-in-command-buffers)
|
||||
|
||||
@@ -320,13 +320,14 @@ Games read this count as a position in their own command memory and size their h
|
||||
|
||||
### GPU event waits order work across queues
|
||||
|
||||
- **Symptom:** intermittent scene-wide lighting flashes and a persistent pale outline around a character. Extra GPU readbacks or synchronous screenshots can make the flashes disappear temporarily.
|
||||
- **Symptom:** intermittent scene-wide lighting flashes and a persistent pale outline around a character. Extra GPU readbacks or synchronous screenshots can make the flashes disappear temporarily. Separately: twice the render passes and attachment loads a frame, with a correct picture, in a game that signals events inside its passes.
|
||||
- **Cause:** `nvnCommandBufferWaitEvent` was registered as a no-op. Serializing NVN submissions onto one Metal queue only orders them by CPU arrival; a consumer can be submitted before its producer on another NVN queue. Its acquire must park that queue until the producer releases the event. The native SDK implements exact equality and wrapping greater-or-equal (`(uint32_t)(current - target) < 0x80000000`), and its CPU event signal publishes earlier writes with a store barrier. Ignoring either dependency lets a consumer read cleared or stale shared data.
|
||||
- **Fix:** `nvn_queue.inc` retains submitted command streams and replays ready continuations. An unresolved event or sync wait parks only its NVN queue; another queue can submit and execute its producer. GPU event signals end physical submission prefixes and publish after their completion writes, including render-target readback. CPU signals publish with release ordering. Logical queue receipts remain separate from physical Metal fences: a fence on parked work cannot accidentally inherit another queue's completion, and a queued sync wait retains its original target through later refencing. Presents remain in queue order. Descriptor snapshots and retained renderer objects survive deferred replay; the caller still owns backing-memory lifetime.
|
||||
- **Fix:** `nvn_queue.inc` retains submitted command streams and replays ready continuations. An unresolved event or sync wait parks only its NVN queue; another queue can submit and execute its producer. GPU event signals publish after the completion writes of the submission they are in, including render-target readback. CPU signals publish with release ordering. Logical queue receipts remain separate from physical Metal fences: a fence on parked work cannot accidentally inherit another queue's completion, and a queued sync wait retains its original target through later refencing. Presents remain in queue order. Descriptor snapshots and retained renderer objects survive deferred replay; the caller still owns backing-memory lifetime.
|
||||
- **Waits on a queued signal:** a wait the last GPU `WRITE` signal of its event satisfies, still in flight, is released when the host encodes it: its continuation runs after the writer on the one ordered Metal queue, as a later submission of the same NVN queue would, and the reports and readbacks the writer's completion lands are handled as for any in-flight submission. Waiting for the writer's completion instead left the GPU idle while the host encoded the next submission (Ocarina of Time: about 25 waits a frame, 2 ms of a 14.5 ms frame, which its dynamic resolution counted as GPU time: [Dynamic resolution at its floor](frame-rate.md#dynamic-resolution-at-its-floor-while-the-gpu-has-time-to-spare)). An `ADD` signal's result depends on the writes landing before it, so a wait on it still waits for its completion.
|
||||
- **Signals do not cut a submission:** each piece the queue replays is a Metal command buffer of its own, so a cut ends the open render pass, which then loads its attachments again. Only what must wait or be waited for cuts: an event wait, a sync wait, a recorded fence. A signal stays among its commands, and the piece keeps every signal it records: each is published when the piece completes, and a wait on any of them, in flight, is ordered by the piece's fence as above. Cutting at each signal doubled the passes of a game that signals inside them: BioShock Infinite (Unreal Engine 3) signals an event per occlusion query, and its title screen went from 88 to 189 render passes a frame, from 734 MB to 2.58 GB of attachment loads and from 3.8 to 7.1-8.4 ms of submit CPU ([#2112](https://github.com/seedds/nxnative/issues/2112), measured by its reporter). A waiter on another queue is released after the rest of the signalling piece, not after the signal's prefix; no wait can sit in the same piece as its signal, since waits cut.
|
||||
- **Waits on a sync not yet fenced:** a queue or command-buffer `WaitSync` on a sync that no fence had reached when the wait was submitted passes at once: the wait keeps the sync as it was then, and there is nothing to wait for. Parking it instead hung any game whose wait comes before its first fence of that sync. Sparking! ZERO, on a single NVN queue, waits on such a sync at its first load after the title, a wait that runs on the console. Its queue's own fence, behind the parked wait, never published. The RHI interrupt thread's `nvnSyncWait` then blocked, then the render thread and the game thread: the picture went black with sound going on, and Unreal's GPU-hang check aborted a minute later ([#1808](https://github.com/seedds/nxnative/issues/1808)).
|
||||
- **Check:** `t_wait_event` exercises public NVN calls, CPU and GPU producers, wrapping predicates, a consumer submitted before its producer, a wait on an in-flight `WRITE` (encoded before it completes) and `ADD` (parked until it lands), a queue and a command-buffer wait on a never-fenced sync (the work behind it runs), and logical sync lifetime. `t_queue_lifetime` checks submitted program/texture descriptor reuse and inherited queue bindings. The focused event, receipt, descriptor and lifecycle checks pass; combined gameplay and performance validation are still pending.
|
||||
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#open), M4 Pro. Its traced lighting producer and ambient-cache consumer have an explicit event dependency; this is a general queue contract, with no shader-key or game check in the fix. The wait on a sync not yet fenced: [DRAGON BALL Sparking! ZERO](../../dm/games/010035F022078000/README.md#found-on-this-game).
|
||||
- **Check:** `t_wait_event` exercises public NVN calls, CPU and GPU producers, wrapping predicates, a consumer submitted before its producer, a wait on an in-flight `WRITE` (encoded before it completes) and `ADD` (parked until it lands), two signals with commands between them as one submission with a wait on the first encoded behind it, a queue and a command-buffer wait on a never-fenced sync (the work behind it runs), and logical sync lifetime. `t_queue_lifetime` checks submitted program/texture descriptor reuse and inherited queue bindings. The focused event, receipt, descriptor and lifecycle checks pass; combined gameplay and performance validation are still pending.
|
||||
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#open), M4 Pro. Its traced lighting producer and ambient-cache consumer have an explicit event dependency; this is a general queue contract, with no shader-key or game check in the fix. The wait on a sync not yet fenced: [DRAGON BALL Sparking! ZERO](../../dm/games/010035F022078000/README.md#found-on-this-game). The signal cuts: BioShock Infinite, which has no profile here ([#2112](https://github.com/seedds/nxnative/issues/2112)).
|
||||
|
||||
### Bindings belong to each queue
|
||||
|
||||
|
||||
Reference in new issue
Block a user