diff options
Diffstat (limited to 'runtime/shims/acphal.cpp')
| -rw-r--r-- | runtime/shims/acphal.cpp | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index 4b516cf8f..f1c33ddd9 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp | |||
| @@ -390,25 +390,28 @@ struct XmaContextView { | |||
| 390 | return (get(index) >> shift) & mask; | 390 | return (get(index) >> shift) & mask; |
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | void set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const { | 393 | uint32_t set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const { |
| 394 | const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u); | 394 | const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u); |
| 395 | const uint32_t mask = raw_mask << shift; | 395 | const uint32_t mask = raw_mask << shift; |
| 396 | const uint32_t field_value = (value << shift) & mask; | 396 | const uint32_t field_value = (value << shift) & mask; |
| 397 | std::atomic_ref word(words[index]); | 397 | std::atomic_ref word(words[index]); |
| 398 | uint32_t previous = word.load(); | 398 | uint32_t previous = word.load(); |
| 399 | while (!word.compare_exchange_weak(previous, (previous & ~mask) | field_value)) {} | 399 | while (!word.compare_exchange_weak(previous, (previous & ~mask) | field_value)) {} |
| 400 | return previous; | ||
| 400 | } | 401 | } |
| 401 | 402 | ||
| 402 | }; | 403 | }; |
| 403 | 404 | ||
| 404 | struct XmaVoice { | 405 | struct XmaVoice { |
| 405 | explicit XmaVoice(uint32_t format_bits) : format(format_bits) {} | 406 | explicit XmaVoice(uint32_t format_bits) : pcm(0), format(format_bits) {} |
| 406 | std::size_t channels() const { return 1u + (format >> 2); } | 407 | std::size_t channels() const { return 1u + (format >> 2); } |
| 408 | std::size_t queued_samples() const { return pcm.size() - pcm_read; } | ||
| 407 | 409 | ||
| 408 | #if defined(LEONIDA_HAVE_XMA2_DECODER) | 410 | #if defined(LEONIDA_HAVE_XMA2_DECODER) |
| 409 | std::unique_ptr<LeonidaXma2Decoder, Xma2DecoderFree> decoder; | 411 | std::unique_ptr<LeonidaXma2Decoder, Xma2DecoderFree> decoder; |
| 410 | #endif | 412 | #endif |
| 411 | std::deque<int16_t> pcm; | 413 | std::vector<int16_t> pcm; |
| 414 | std::size_t pcm_read = 0; | ||
| 412 | std::size_t loop_skip = 0; // samples per channel to drop after a rewind | 415 | std::size_t loop_skip = 0; // samples per channel to drop after a rewind |
| 413 | uint32_t format; // SHAPE word 1 bits 27..29, rate and channel layout | 416 | uint32_t format; // SHAPE word 1 bits 27..29, rate and channel layout |
| 414 | bool ended = false; | 417 | bool ended = false; |
| @@ -503,6 +506,7 @@ public: | |||
| 503 | active_count_.fetch_add(1, std::memory_order_relaxed); | 506 | active_count_.fetch_add(1, std::memory_order_relaxed); |
| 504 | } else if (voice->format != format) { | 507 | } else if (voice->format != format) { |
| 505 | voice->pcm.clear(); | 508 | voice->pcm.clear(); |
| 509 | voice->pcm_read = 0; | ||
| 506 | voice->loop_skip = 0; | 510 | voice->loop_skip = 0; |
| 507 | voice->format = format; | 511 | voice->format = format; |
| 508 | #if defined(LEONIDA_HAVE_XMA2_DECODER) | 512 | #if defined(LEONIDA_HAVE_XMA2_DECODER) |
| @@ -550,10 +554,9 @@ private: | |||
| 550 | return XmaContextView{array_[index].words}; | 554 | return XmaContextView{array_[index].words}; |
| 551 | } | 555 | } |
| 552 | 556 | ||
| 553 | // Fills `pcm` (interleaved s16) for one 2048-byte packet. Without the Rust decoder, or when it | 557 | // Appends interleaved s16 for one 2048-byte packet. Without the Rust decoder, or when it |
| 554 | // fails, the packet becomes silence for its frame count (header bits 26..31) so timing holds. | 558 | // fails, the packet becomes silence for its frame count (header bits 26..31) so timing holds. |
| 555 | static void decode_packet(XmaVoice& voice, const uint8_t* bytes, | 559 | static void decode_packet(XmaVoice& voice, const uint8_t* bytes) { |
| 556 | std::vector<int16_t>& pcm) { | ||
| 557 | const std::size_t channels = voice.channels(); | 560 | const std::size_t channels = voice.channels(); |
| 558 | #if defined(LEONIDA_HAVE_XMA2_DECODER) | 561 | #if defined(LEONIDA_HAVE_XMA2_DECODER) |
| 559 | if (!voice.decoder) { | 562 | if (!voice.decoder) { |
| @@ -569,13 +572,13 @@ private: | |||
| 569 | const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw); | 572 | const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw); |
| 570 | const int16_t* samples = leonida_xma2_pcm_data(block.get()); | 573 | const int16_t* samples = leonida_xma2_pcm_data(block.get()); |
| 571 | const std::size_t count = leonida_xma2_pcm_sample_count(block.get()); | 574 | const std::size_t count = leonida_xma2_pcm_sample_count(block.get()); |
| 572 | if (count != 0) pcm.assign(samples, samples + count); | 575 | if (count != 0) voice.pcm.insert(voice.pcm.end(), samples, samples + count); |
| 573 | return; | 576 | return; |
| 574 | } | 577 | } |
| 575 | voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence. | 578 | voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence. |
| 576 | } | 579 | } |
| 577 | #endif | 580 | #endif |
| 578 | pcm.assign(static_cast<std::size_t>(bytes[0] >> 2) * kXmaFrameSamples * channels, 0); | 581 | voice.pcm.resize(voice.pcm.size() + static_cast<std::size_t>(bytes[0] >> 2) * kXmaFrameSamples * channels); |
| 579 | } | 582 | } |
| 580 | 583 | ||
| 581 | // Decodes the packet at the read offset of the current input buffer and queues its PCM. | 584 | // Decodes the packet at the read offset of the current input buffer and queues its PCM. |
| @@ -591,7 +594,7 @@ private: | |||
| 591 | if (valid == 0) { | 594 | if (valid == 0) { |
| 592 | voice.ended = true; | 595 | voice.ended = true; |
| 593 | ctx.set_field(2, 26, 6, kXmaErrorInputInvalid | 32u); | 596 | ctx.set_field(2, 26, 6, kXmaErrorInputInvalid | 32u); |
| 594 | return !voice.pcm.empty(); | 597 | return voice.queued_samples() != 0; |
| 595 | } | 598 | } |
| 596 | if ((valid & (1u << current)) == 0) { | 599 | if ((valid & (1u << current)) == 0) { |
| 597 | current ^= 1u; | 600 | current ^= 1u; |
| @@ -609,20 +612,28 @@ private: | |||
| 609 | 612 | ||
| 610 | const uint8_t* packet_data = data + packet * kXmaPacketBytes; | 613 | const uint8_t* packet_data = data + packet * kXmaPacketBytes; |
| 611 | const uint8_t packet_skip = packet_data[3]; | 614 | const uint8_t packet_skip = packet_data[3]; |
| 612 | // The sized constructor can propagate even an MSVC Debug allocation failure. | 615 | // Decoding is only requested when less than a ring block remains. Keep that |
| 613 | std::vector<int16_t> pcm(0); | 616 | // tail and reuse the packet storage as the voice consumes subsequent packets. |
| 614 | decode_packet(voice, packet_data, pcm); | 617 | voice.pcm.erase(voice.pcm.begin(), voice.pcm.begin() + static_cast<std::ptrdiff_t>(voice.pcm_read)); |
| 618 | voice.pcm_read = 0; | ||
| 619 | const std::size_t packet_start = voice.pcm.size(); | ||
| 620 | decode_packet(voice, packet_data); | ||
| 621 | const std::size_t packet_samples = voice.pcm.size() - packet_start; | ||
| 615 | const std::size_t subframe_samples = kXmaSubframeSamples * channels; | 622 | const std::size_t subframe_samples = kXmaSubframeSamples * channels; |
| 616 | const std::size_t initial_skip = ctx.field(1, 24, 3); | 623 | const std::size_t initial_skip = ctx.field(1, 24, 3); |
| 617 | const std::size_t skipped_subframes = std::min(initial_skip, pcm.size() / subframe_samples); | 624 | const std::size_t skipped_subframes = std::min(initial_skip, packet_samples / subframe_samples); |
| 625 | const std::size_t initial_samples = skipped_subframes * subframe_samples; | ||
| 626 | const std::size_t loop_samples = std::min(voice.loop_skip * channels, packet_samples - initial_samples); | ||
| 627 | const std::size_t skip_samples = initial_samples + loop_samples; | ||
| 628 | if (skip_samples != 0) { | ||
| 629 | const auto first = voice.pcm.begin() + static_cast<std::ptrdiff_t>(packet_start); | ||
| 630 | voice.pcm.erase(first, first + static_cast<std::ptrdiff_t>(skip_samples)); | ||
| 631 | } | ||
| 632 | // Update the context only after the PCM append succeeds. | ||
| 618 | if (skipped_subframes != 0) { | 633 | if (skipped_subframes != 0) { |
| 619 | ctx.set_field(1, 24, 3, static_cast<uint32_t>(initial_skip - skipped_subframes)); | 634 | ctx.set_field(1, 24, 3, static_cast<uint32_t>(initial_skip - skipped_subframes)); |
| 620 | } | 635 | } |
| 621 | const std::size_t initial_samples = skipped_subframes * subframe_samples; | ||
| 622 | const std::size_t loop_samples = std::min(voice.loop_skip * channels, pcm.size() - initial_samples); | ||
| 623 | voice.loop_skip -= loop_samples / channels; | 636 | voice.loop_skip -= loop_samples / channels; |
| 624 | const std::size_t skip_samples = initial_samples + loop_samples; | ||
| 625 | voice.pcm.insert(voice.pcm.end(), pcm.begin() + static_cast<std::ptrdiff_t>(skip_samples), pcm.end()); | ||
| 626 | 637 | ||
| 627 | // XMA2 packet headers link packets from the same stream; 0xff ends the chain. | 638 | // XMA2 packet headers link packets from the same stream; 0xff ends the chain. |
| 628 | std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip; | 639 | std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip; |
| @@ -651,8 +662,10 @@ private: | |||
| 651 | // Publish the next offset before telling the title it can refill this input. | 662 | // Publish the next offset before telling the title it can refill this input. |
| 652 | ctx.set_field(2, 0, 26, static_cast<uint32_t>(skip_packets * kXmaPacketBits)); | 663 | ctx.set_field(2, 0, 26, static_cast<uint32_t>(skip_packets * kXmaPacketBits)); |
| 653 | const uint32_t other = 1u - current; | 664 | const uint32_t other = 1u - current; |
| 654 | if (ctx.field(0, 20 + other, 1) != 0) ctx.set_field(4, 31, 1, other); | 665 | const uint32_t retired = ctx.set_field(0, 20 + current, 1, 0); |
| 655 | ctx.set_field(0, 20 + current, 1, 0); | 666 | // Take the other input if it was queued at retirement. Otherwise a double |
| 667 | // refill of the empty pair starts at input 0. A lone input can still run. | ||
| 668 | ctx.set_field(4, 31, 1, (retired & (1u << (20 + other))) != 0 ? other : 0u); | ||
| 656 | } | 669 | } |
| 657 | 670 | ||
| 658 | static void pump_voice(XmaContextView ctx, XmaVoice& voice) { | 671 | static void pump_voice(XmaContextView ctx, XmaVoice& voice) { |
| @@ -663,19 +676,18 @@ private: | |||
| 663 | 676 | ||
| 664 | for (uint32_t blocks = 0; blocks < size && ctx.field(1, 31, 1) == 1; ++blocks) { | 677 | for (uint32_t blocks = 0; blocks < size && ctx.field(1, 31, 1) == 1; ++blocks) { |
| 665 | int decode_guard = 0; | 678 | int decode_guard = 0; |
| 666 | while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) { | 679 | while (voice.queued_samples() < kXmaSamplesPerBlock && decode_guard++ < 16) { |
| 667 | if (!decode_one_packet(ctx, voice)) break; | 680 | if (!decode_one_packet(ctx, voice)) break; |
| 668 | } | 681 | } |
| 669 | if (voice.pcm.empty()) return; | 682 | if (voice.queued_samples() == 0) return; |
| 670 | 683 | ||
| 671 | const uint32_t write = ctx.field(0, 27, 5) % size; | 684 | const uint32_t write = ctx.field(0, 27, 5) % size; |
| 672 | uint8_t* out = ring + write * kXmaBlockBytes; | 685 | uint8_t* out = ring + write * kXmaBlockBytes; |
| 673 | 686 | ||
| 674 | for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) { | 687 | for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) { |
| 675 | int16_t sample = 0; | 688 | int16_t sample = 0; |
| 676 | if (!voice.pcm.empty()) { | 689 | if (voice.pcm_read != voice.pcm.size()) { |
| 677 | sample = voice.pcm.front(); | 690 | sample = voice.pcm[voice.pcm_read++]; |
| 678 | voice.pcm.pop_front(); | ||
| 679 | } | 691 | } |
| 680 | const uint16_t raw = static_cast<uint16_t>(sample); | 692 | const uint16_t raw = static_cast<uint16_t>(sample); |
| 681 | out[2 * i] = static_cast<uint8_t>(raw & 0xFFu); | 693 | out[2 * i] = static_cast<uint8_t>(raw & 0xFFu); |
| @@ -1111,6 +1123,7 @@ HRESULT __stdcall ApuAlloc(void** virtualAddress, uint32_t* physicalAddress, | |||
| 1111 | 1123 | ||
| 1112 | if (!target->base) { | 1124 | if (!target->base) { |
| 1113 | if (!prefer_noncached) { | 1125 | if (!prefer_noncached) { |
| 1126 | if (g_apu_heap_created) return E_OUTOFMEMORY; | ||
| 1114 | // Lazy default cached heap if ApuCreateHeap wasn't called yet | 1127 | // Lazy default cached heap if ApuCreateHeap wasn't called yet |
| 1115 | constexpr uint32_t kDefaultHeapSize = 64 * 1024 * 1024; | 1128 | constexpr uint32_t kDefaultHeapSize = 64 * 1024 * 1024; |
| 1116 | target->base = VirtualAlloc(nullptr, kDefaultHeapSize, | 1129 | target->base = VirtualAlloc(nullptr, kDefaultHeapSize, |