Skip to content

fix(ngs2): retain PCM input when reporting decoded bytes - #5548

Closed
JJenkx wants to merge 7 commits into
boykopovar:mainfrom
JJenkx:fix/ngs2-owned-pcm-input
Closed

JJenkx wants to merge 7 commits into
boykopovar:mainfrom
JJenkx:fix/ngs2-owned-pcm-input

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

Retain PCM input before reporting its bytes as decoded, so a caller reusing those reported bytes cannot alter samples still waiting in the resampler. This remains an experimental draft: the console's decode granularity, repeat visibility and waveform-pointer timing are not measured.

An ordinary playable block or a clipped stream piece is copied on first use and counted in decoded_data_size then. Submission does not copy or count future input. Resampling and num_decoded_samples drain the retained data independently. Repeat callbacks release the old copy before the next iteration decodes again. Callback metadata and waveform_data keep caller addresses and playback position. Integer-phase playback avoids unused lookahead, and high-pitch advancement accounts for skipped decoded input.

Two reusable per-voice buffers each retain at most 64 KiB after consumption. Larger allocations, including a final drained piece of a starved stream, are released. Active whole-block copies remain proportional to the submitted playable range; large repeated blocks may allocate again on each loop. These limits and unverified console behavior are documented in library debt. No whole-waveform copy at submission is used.

Main now includes Ondra97's #6029 callback waveform-replacement correction. A callback replacing the waveform stops the old render path.

Tested

New current-main lifetime evidence, 2026-11-13: main e8b95881c391 already converts PCM into resampler scratch/history before reporting the bytes consumed. Its sampler path includes #11315. The focused regression proposed in #19349 passes with unchanged main production code in both NGS2 variants: six formats, mono/stereo, 44.1/48/96 kHz and pitch 0.75/1/2, 108 combinations per library.

Every case overwrites only the reported prefix after each render, patches an unreported tail within the same submitted block after the first render, compares exact samples with the expected-tail reference, and confirms the late write affects output. Separate private controls for whole-block first-read snapshots and early byte reporting both fail at the sample comparison in both libraries; restoring main returns both complete PCM suites to green. Playback stays active throughout because empty-voice state clears counters.

This establishes the tested current-main ownership property, not console decode granularity, repeat visibility, waveform-pointer timing or current Quake audio. The experimental whole-block retention/accounting changes in this draft are not thereby qualified; its public head remains unchanged. A fresh title/audio result is still needed before claiming the historical static is resolved or assigning a remaining cause.

Exact pins, verbose tests, fault-control patches, regression and manifest, SHA-256 89476a4f9853fb89c2fdc17060e279cb2f014b9a26f46c5c17c2eed284193c96; downloaded archive and all 15 manifest members verified.

Current-main integration still blocked, 2026-10-27 UTC: reviewed main 74acfad65b3f0fd4eab653f3a3989d298fba1157; the public head 3b3710efcf760296bb594a748e8ec1c64fd1dbd3 was not rewritten and this remains an open technical draft. The #10127 format/accounting integration requirement is substantive. Main's U8/I16/I24/I32/F32/F64 conversion, consumed-byte assertions, partial-input restrictions and additional NGS2 test registrations were preserved in the local analysis.

Changing only the report counter while retaining a whole-block snapshot would hide legal later writes to an unreported tail in the same block. The earlier late-fill case uses a different future block and does not establish this case. A cache containing only already reported samples has no demonstrated future use in main's monotonic consumed-cursor and repeat-reset flow. A replacement decode/read-ahead policy needs reference evidence of granularity, repeat visibility and waveform-pointer lifetime; neither weakening main's assertions nor an I16-only workaround qualifies as an integration.

The attached same-block unreported-tail and 44.1 kHz cases are test designs from source review, not executed runtime tests. No new native, console, Windows or audio validation is claimed. The local integration remains stopped at its reproducible CMake conflicts; no unqualified source change was pushed.

Exact pins, unchanged upstream PCM-test hash, staged/stopped patches, counterexample design and bounded handoff. SHA-256: aa2d6b9083a61cbd4227ff67ff88425dd29b86b608ddbe5b9fb1cd5d12a1aa2a; downloaded copy verified. Earlier passing tests below retain their exact historical scope.

2026-10-25: head ebfaae7a040a009579e4392300cfb5343234ed79, six linear topic commits on main b90d009d9f73354cbea239a74a8f7e2dcd138772. Linux GCC 16.2.1 Release. Built both NGS2 libraries and all 18 focused test executables, including main's new voice-event tests. All 20 registered CTest groups passed three repetitions (60 passes, zero skips). Restoring the candidate after the main control reproduced all 60 passes and identical hashes for both libraries and all 18 test executables.

The new --reported-regions mode runs the existing ring-buffer cases separately: mono and stereo at 48 kHz and 44.1 kHz, recycling only the bytes reported decoded, without assuming a whole-block decoded-byte count. Their function bodies are unchanged. Both main and the candidate pass these isolated cases. They do not independently demonstrate a buffer-lifetime failure on main.

The control uses this branch's tests/CMake with exactly main's Render.cpp, Voice.cpp and Ngs2Internal.hpp. Fourteen groups pass three repetitions; six fail: render, PCM-input and PCM-memory in each library variant. Full PCM-input fails at the decoded accounting assertion before the overwrite check. These failures show differences from the tests' accounting/memory policy; they must not be presented as independent proof of a copying bug or console equivalence.

The rebase retains main's input-channel/custom-mastering changes and voice-event tests. Existing PCM helper bodies are unchanged from former public head ddf93fbe14b4f036131e5c870a43874414414597; the complete render source also contains newer upstream changes. Source/whitespace checks pass. Conventions: zero errors, two existing debt-length warnings. Local merge-tree is clean against the exact tested main.

Validation archive: verbose logs, controls, source comparisons and patches. SHA-256: ecee379ddbd1c1758547169ae101f9b16340e0f820d25248275436761ad7e722; downloaded copy verified.

No new native title/audio, Windows, GPU or performance result is claimed. The draft blockers remain console decode granularity, repeat visibility and waveform-pointer timing; CPU test success does not resolve them.

Historical title evidence, retaining its original scope: @DavidAmsellem's randomized blind Quake PPSA01880 comparison used main 0fbfd91e5 against main plus this PR at 89d293cc6, the same machine/runtime and stationary scene. The tester identified the PR run as clean and main as static before revealing the order. This supports that one-scene audio result, not console semantic equivalence or a freshly repeated result. The preceding report used different scenes and is correspondingly weaker for quantitative attribution. Earlier local diagnostic audio captures also used different rendered scenes. Prior revision-specific details are preserved in the comments and archive's original description.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (fix(ngs2): stop stale rendering after waveform callbacks #6029 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (CPU NGS2 tests)
  • AI-assisted: yes

@CnRJay CnRJay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The analysis is good: Quake refills a buffer for each positive delta of decoded_data_size, so on the console those bytes must already be independent of the guest buffer by the time the counter reports them. That points at where the copy should happen. Copying the whole block at AddWaveformBlocks time is earlier than the protocol implies:

  • what the title relies on is that bytes counted in decoded_data_size are no longer read from its buffer;
  • it doesn't follow that bytes not yet counted have been copied. A title that submits a block and fills or patches it shortly afterwards (common with streaming rings that queue the next block before the decoder thread writes it) would play the old contents here, and the console would play the new ones if it reads lazily.

A version that matches both observations: render from the guest buffer as main does, and copy a block's bytes into the voice's retained buffer at the moment they are counted into decoded_data_size (the same point at which the title is allowed to reuse them). Then the counter and the data lifetime are tied together by construction. Your "changing counter timing only reduced it" experiment suggests the counter currently runs ahead of the rendered position somewhere, and this would also surface that.

Smaller: the pcm vectors are allocated per block on the submission path, which is often an audio thread with tight deadlines. Reserving per voice, or pooling the vectors, avoids allocator calls there.

@JJenkx
JJenkx marked this pull request as draft October 14, 2026 03:17
@JJenkx

JJenkx commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Agreed that the refill observation only establishes ownership for bytes counted as decoded; it does not establish that the console copies unread bytes at submission. I have returned this PR to draft and corrected the description to make that limitation explicit. I will not claim this snapshot implementation is ready to merge. The next revision needs late-fill/reuse coverage with retention tied to decoded-byte accounting, reusable storage, and a fresh Quake audio comparison.

AI-assisted: yes.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 14, 2026
@JJenkx
JJenkx force-pushed the fix/ngs2-owned-pcm-input branch from f0a55cd to 3feddf9 Compare October 15, 2026 09:05
@JJenkx JJenkx changed the title fix(ngs2): retain queued PCM input until playback completes fix(ngs2): retain PCM input when reporting decoded bytes Oct 15, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1384 core/libs/prx/libSceNgs2.native/CMakeLists.txt
core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp
core/libs/prx/libSceNgs2/CMakeLists.txt
PR 13491 docs/dev/debt/libSceNgs2.native.md
PR 18501 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp
docs/dev/debt/libSceNgs2.native.md
PR 22536 core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp
core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp

@JJenkx

JJenkx commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up to the submission-time-copy review: current draft head b21f78b0 copies neither PCM nor decoded-byte accounting at submission. DecodePcm retains a playable block or clipped stream piece when first decoded and adds exactly those retained bytes to decoded_data_size; late-fill-before-decode and reuse-after-accounting are covered in both NGS2 variants. Two reusable per-voice buffers retain at most 64 KiB each after consumption; larger consumed buffers are released. The existing description records the 14 passing tests and the stereo-accounting negative control.

This resolves the eager-submission-copy approach, but it does not establish that whole-block first-use decoding matches console granularity, repeat visibility or waveform-pointer timing. The PR stays draft. The Quake producer/SDL captures also show different scenes, so their discontinuity statistics do not prove a controlled audible improvement or resolve the earlier static-audio reports.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Current-main integration still blocked, 2026-10-27 UTC: reviewed main 74acfad65b3f0fd4eab653f3a3989d298fba1157; the public head 3b3710efcf760296bb594a748e8ec1c64fd1dbd3 was not rewritten and this remains an open technical draft. The #10127 format/accounting integration requirement is substantive. Main's U8/I16/I24/I32/F32/F64 conversion, consumed-byte assertions, partial-input restrictions and additional NGS2 test registrations were preserved in the local analysis.

Changing only the report counter while retaining a whole-block snapshot would hide legal later writes to an unreported tail in the same block. The earlier late-fill case uses a different future block and does not establish this case. A cache containing only already reported samples has no demonstrated future use in main's monotonic consumed-cursor and repeat-reset flow. A replacement decode/read-ahead policy needs reference evidence of granularity, repeat visibility and waveform-pointer lifetime; neither weakening main's assertions nor an I16-only workaround qualifies as an integration.

The attached same-block unreported-tail and 44.1 kHz cases are test designs from source review, not executed runtime tests. No new native, console, Windows or audio validation is claimed. The local integration remains stopped at its reproducible CMake conflicts; no unqualified source change was pushed.

Exact pins, unchanged upstream PCM-test hash, staged/stopped patches, counterexample design and bounded handoff. SHA-256: aa2d6b9083a61cbd4227ff67ff88425dd29b86b608ddbe5b9fb1cd5d12a1aa2a; downloaded copy verified. Earlier passing tests below retain their exact historical scope.

AI-assisted: yes.

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Current rebase disposition: this remains a technical draft at 3b3710efcf760296bb594a748e8ec1c64fd1dbd3. The read-only merge preview against main 5454cd2e455994d992aca39335ce60507eeda1eb conflicts in the PCM implementation/header, PCM regression and two build files. I have kept the branch open and unchanged.

The unresolved behavior is substantive: snapshotting a whole PCM block before unreported bytes are consumed can hide legal later writes to that same block. Counter timing, repeat visibility and waveform-pointer lifetime still need reference evidence. A resolution must preserve main’s multiple PCM formats, partial-input checks and consumed-byte assertions; a mechanical conflict edit alone would not establish that.

Source-continuity and blocker receipt, SHA-256 d86d77d86fd17f401bd1b6a042e483c338a25821f9806c8cb67c2965b211fa81. This audit ran no new build, audio/native/title or Windows test and makes no new behavior claim.

AI-assisted: yes (Codex).

@JJenkx

JJenkx commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

The branch remains a technical draft at 3b3710efcf760296bb594a748e8ec1c64fd1dbd3. A fresh read-only preview against main 08a10d225306ad102b57408e2ef7566bcfb829b2 still has five conflicts. The PCM semantic blocker is unchanged: a whole-block snapshot can hide later writes to unreported bytes; consumption granularity, repeat visibility and pointer lifetime still need reference evidence.

The PCM regression file matches the earlier5454 snapshot, but newer main also adds rack-limit and default-mapping width guards that a future resolution must preserve. No mechanical source refresh was pushed, and no new build, audio, native-title or Windows test is claimed.

Source-continuity receipt, SHA-256 913f4b2dc781ebd924cb431ed9e0892757d53a8935cb3eaf2a5281011f2e96a4 (download verified).

AI-assisted: yes (Codex).

@JJenkx

JJenkx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

New current-main lifetime evidence, 2026-11-13: main e8b95881c391 already converts PCM into resampler scratch/history before reporting the bytes consumed. Its sampler path includes #11315. The focused regression proposed in #19349 passes with unchanged main production code in both NGS2 variants: six formats, mono/stereo, 44.1/48/96 kHz and pitch 0.75/1/2, 108 combinations per library.

Every case overwrites only the reported prefix after each render, patches an unreported tail within the same submitted block after the first render, compares exact samples with the expected-tail reference, and confirms the late write affects output. Separate private controls for whole-block first-read snapshots and early byte reporting both fail at the sample comparison in both libraries; restoring main returns both complete PCM suites to green. Playback stays active throughout because empty-voice state clears counters.

This establishes the tested current-main ownership property, not console decode granularity, repeat visibility, waveform-pointer timing or current Quake audio. The experimental whole-block retention/accounting changes in this draft are not thereby qualified; its public head remains unchanged. A fresh title/audio result is still needed before claiming the historical static is resolved or assigning a remaining cause.

Exact pins, verbose tests, fault-control patches, regression and manifest, SHA-256 89476a4f9853fb89c2fdc17060e279cb2f014b9a26f46c5c17c2eed284193c96; downloaded archive and all 15 manifest members verified.

AI-assisted: yes.

@JJenkx

JJenkx commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Closing this experimental implementation as superseded by main's sampler architecture, rather than mechanically restoring the old sampler during a rebase. The public head and branch are preserved unchanged at 3b3710efcf760296bb594a748e8ec1c64fd1dbd3.

Main 194964cd3ce2 decodes PCM into resampler scratch/history before reporting the consumed bytes. The previously added single-block ownership/late-write coverage is already merged. New PR #22736 extends current-main coverage to repeated ring-region overwrite and requeue: six formats, mono/stereo, three input rates and three pitches, 108 combinations per library, 100 renders each, with an immutable-input reference. Both complete PCM suites and both isolated ring runs pass. A private doubled-byte-reporting control fails both isolated ring runs; restoring main's production code returns both complete suites to green.

The new coverage retains the default four-block queue and changes no production code. This draft's whole-block snapshot, accounting, repeat visibility and pointer-timing policy is not adopted or qualified. These results do not establish console decode granularity or resolve the historical native-game audio/static report.

Reproducible tests, logs, control patch and source pins, SHA-256 b73b5c11d0270673af5bdbeed5ecf161b7a4dc6e6e05bddc968b5038fc923bff; downloaded archive and all 19 payload hashes verified.

AI-assisted: yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants