summaryrefslogtreecommitdiffstats
path: root/src/audio_core/renderer/system.cpp
blob: 8f02754c52a1c0c5662f72cfd191d4f5c93104e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <chrono>
#include <span>

#include "audio_core/adsp/apps/audio_renderer/audio_renderer.h"
#include "audio_core/adsp/apps/audio_renderer/command_buffer.h"
#include "audio_core/audio_core.h"
#include "audio_core/common/audio_renderer_parameter.h"
#include "audio_core/common/common.h"
#include "audio_core/common/feature_support.h"
#include "audio_core/common/workbuffer_allocator.h"
#include "audio_core/renderer/behavior/info_updater.h"
#include "audio_core/renderer/command/command_buffer.h"
#include "audio_core/renderer/command/command_generator.h"
#include "audio_core/renderer/command/command_list_header.h"
#include "audio_core/renderer/effect/effect_info_base.h"
#include "audio_core/renderer/effect/effect_result_state.h"
#include "audio_core/renderer/memory/memory_pool_info.h"
#include "audio_core/renderer/memory/pool_mapper.h"
#include "audio_core/renderer/mix/mix_info.h"
#include "audio_core/renderer/nodes/edge_matrix.h"
#include "audio_core/renderer/nodes/node_states.h"
#include "audio_core/renderer/sink/sink_info_base.h"
#include "audio_core/renderer/system.h"
#include "audio_core/renderer/upsampler/upsampler_info.h"
#include "audio_core/renderer/voice/voice_channel_resource.h"
#include "audio_core/renderer/voice/voice_info.h"
#include "audio_core/renderer/voice/voice_state.h"
#include "common/alignment.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_transfer_memory.h"
#include "core/memory.h"

namespace AudioCore::Renderer {

u64 System::GetWorkBufferSize(const AudioRendererParameterInternal& params) {
    BehaviorInfo behavior;
    behavior.SetUserLibRevision(params.revision);

    u64 size{0};

    size += Common::AlignUp(params.mixes * sizeof(s32), 0x40);
    size += params.sub_mixes * MaxEffects * sizeof(s32);
    size += (params.sub_mixes + 1) * sizeof(MixInfo);
    size += params.voices * (sizeof(VoiceInfo) + sizeof(VoiceChannelResource) + sizeof(VoiceState));
    size += Common::AlignUp((params.sub_mixes + 1) * sizeof(MixInfo*), 0x10);
    size += Common::AlignUp(params.voices * sizeof(VoiceInfo*), 0x10);
    size += Common::AlignUp(((params.sinks + params.sub_mixes) * TargetSampleCount * sizeof(s32) +
                             params.sample_count * sizeof(s32)) *
                                (params.mixes + MaxChannels),
                            0x40);

    if (behavior.IsSplitterSupported()) {
        const auto node_size{NodeStates::GetWorkBufferSize(params.sub_mixes + 1)};
        const auto edge_size{EdgeMatrix::GetWorkBufferSize(params.sub_mixes + 1)};
        size += Common::AlignUp(node_size + edge_size, 0x10);
    }

    size += SplitterContext::CalcWorkBufferSize(behavior, params);
    size += (params.effects + params.voices * MaxWaveBuffers) * sizeof(MemoryPoolInfo);

    if (behavior.IsEffectInfoVersion2Supported()) {
        size += params.effects * sizeof(EffectResultState);
    }
    size += 0x50;

    size = Common::AlignUp(size, 0x40);

    size += (params.sinks + params.sub_mixes) * sizeof(UpsamplerInfo);
    size += params.effects * sizeof(EffectInfoBase);
    size += Common::AlignUp(params.voices * sizeof(VoiceState), 0x40);
    size += params.sinks * sizeof(SinkInfoBase);

    if (behavior.IsEffectInfoVersion2Supported()) {
        size += params.effects * sizeof(EffectResultState);
    }

    if (params.perf_frames > 0) {
        auto perf_size{PerformanceManager::GetRequiredBufferSizeForPerformanceMetricsPerFrame(
            behavior, params)};
        size += Common::AlignUp(perf_size * (params.perf_frames + 1) + 0xC0, 0x100);
    }

    if (behavior.IsVariadicCommandBufferSizeSupported()) {
        size += CommandGenerator::CalculateCommandBufferSize(behavior, params) + (0x40 - 1) * 2;
    } else {
        size += 0x18000 + (0x40 - 1) * 2;
    }

    size = Common::AlignUp(size, 0x1000);
    return size;
}

System::System(Core::System& core_, Kernel::KEvent* adsp_rendered_event_)
    : core{core_}, audio_renderer{core.AudioCore().ADSP().AudioRenderer()},
      adsp_rendered_event{adsp_rendered_event_} {}

Result System::Initialize(const AudioRendererParameterInternal& params,
                          Kernel::KTransferMemory* transfer_memory, u64 transfer_memory_size,
                          u32 process_handle_, u64 applet_resource_user_id_, s32 session_id_) {
    if (!CheckValidRevision(params.revision)) {
        return Service::Audio::ResultInvalidRevision;
    }

    if (GetWorkBufferSize(params) > transfer_memory_size) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    if (process_handle_ == 0) {
        return Service::Audio::ResultInvalidHandle;
    }

    behavior.SetUserLibRevision(params.revision);

    process_handle = process_handle_;
    applet_resource_user_id = applet_resource_user_id_;
    session_id = session_id_;

    sample_rate = params.sample_rate;
    sample_count = params.sample_count;
    mix_buffer_count = static_cast<s16>(params.mixes);
    voice_channels = MaxChannels;
    upsampler_count = params.sinks + params.sub_mixes;
    memory_pool_count = params.effects + params.voices * MaxWaveBuffers;
    render_device = params.rendering_device;
    execution_mode = params.execution_mode;

    core.ApplicationMemory().ZeroBlock(transfer_memory->GetSourceAddress(), transfer_memory_size);

    // Note: We're not actually using the transfer memory because it's a pain to code for.
    // Allocate the memory normally instead and hope the game doesn't try to read anything back
    workbuffer = std::make_unique<u8[]>(transfer_memory_size);
    workbuffer_size = transfer_memory_size;

    PoolMapper pool_mapper(process_handle, false);
    pool_mapper.InitializeSystemPool(memory_pool_info, workbuffer.get(), workbuffer_size);

    WorkbufferAllocator allocator({workbuffer.get(), workbuffer_size}, workbuffer_size);

    samples_workbuffer =
        allocator.Allocate<s32>((voice_channels + mix_buffer_count) * sample_count, 0x10);
    if (samples_workbuffer.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    auto upsampler_workbuffer{allocator.Allocate<s32>(
        (voice_channels + mix_buffer_count) * TargetSampleCount * upsampler_count, 0x10)};
    if (upsampler_workbuffer.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    depop_buffer =
        allocator.Allocate<s32>(Common::AlignUp(static_cast<u32>(mix_buffer_count), 0x40), 0x40);
    if (depop_buffer.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    // invalidate samples_workbuffer DSP cache

    auto voice_infos{allocator.Allocate<VoiceInfo>(params.voices, 0x10)};
    for (auto& voice_info : voice_infos) {
        std::construct_at<VoiceInfo>(&voice_info);
    }

    if (voice_infos.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    auto sorted_voice_infos{allocator.Allocate<VoiceInfo*>(params.voices, 0x10)};
    if (sorted_voice_infos.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    std::memset(sorted_voice_infos.data(), 0, sorted_voice_infos.size_bytes());

    auto voice_channel_resources{allocator.Allocate<VoiceChannelResource>(params.voices, 0x10)};
    u32 i{0};
    for (auto& voice_channel_resource : voice_channel_resources) {
        std::construct_at<VoiceChannelResource>(&voice_channel_resource, i++);
    }

    if (voice_channel_resources.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    auto voice_cpu_states{allocator.Allocate<VoiceState>(params.voices, 0x10)};
    if (voice_cpu_states.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    for (auto& voice_state : voice_cpu_states) {
        voice_state = {};
    }

    auto mix_infos{allocator.Allocate<MixInfo>(params.sub_mixes + 1, 0x10)};

    if (mix_infos.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    u32 effect_process_order_count{0};
    std::span<s32> effect_process_order_buffer{};

    if (params.effects > 0) {
        effect_process_order_count = params.effects * (params.sub_mixes + 1);
        effect_process_order_buffer = allocator.Allocate<s32>(effect_process_order_count, 0x10);
        if (effect_process_order_buffer.empty()) {
            return Service::Audio::ResultInsufficientBuffer;
        }
    }

    i = 0;
    for (auto& mix_info : mix_infos) {
        std::construct_at<MixInfo>(
            &mix_info, effect_process_order_buffer.subspan(i * params.effects, params.effects),
            params.effects, this->behavior);
        i++;
    }

    auto sorted_mix_infos{allocator.Allocate<MixInfo*>(params.sub_mixes + 1, 0x10)};
    if (sorted_mix_infos.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    std::memset(sorted_mix_infos.data(), 0, sorted_mix_infos.size_bytes());

    if (behavior.IsSplitterSupported()) {
        u64 node_state_size{NodeStates::GetWorkBufferSize(params.sub_mixes + 1)};
        u64 edge_matrix_size{EdgeMatrix::GetWorkBufferSize(params.sub_mixes + 1)};

        auto node_states_workbuffer{allocator.Allocate<u8>(node_state_size, 1)};
        auto edge_matrix_workbuffer{allocator.Allocate<u8>(edge_matrix_size, 1)};

        if (node_states_workbuffer.empty() || edge_matrix_workbuffer.size() == 0) {
            return Service::Audio::ResultInsufficientBuffer;
        }

        mix_context.Initialize(sorted_mix_infos, mix_infos, params.sub_mixes + 1,
                               effect_process_order_buffer, effect_process_order_count,
                               node_states_workbuffer, node_state_size, edge_matrix_workbuffer,
                               edge_matrix_size);
    } else {
        mix_context.Initialize(sorted_mix_infos, mix_infos, params.sub_mixes + 1,
                               effect_process_order_buffer, effect_process_order_count, {}, 0, {},
                               0);
    }

    upsampler_manager = allocator.Allocate<UpsamplerManager>(1, 0x10).data();
    if (upsampler_manager == nullptr) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    memory_pool_workbuffer = allocator.Allocate<MemoryPoolInfo>(memory_pool_count, 0x10);
    for (auto& memory_pool : memory_pool_workbuffer) {
        std::construct_at<MemoryPoolInfo>(&memory_pool, MemoryPoolInfo::Location::DSP);
    }

    if (memory_pool_workbuffer.empty() && memory_pool_count > 0) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    if (!splitter_context.Initialize(behavior, params, allocator)) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    std::span<EffectResultState> effect_result_states_cpu{};
    if (behavior.IsEffectInfoVersion2Supported() && params.effects > 0) {
        effect_result_states_cpu = allocator.Allocate<EffectResultState>(params.effects, 0x10);
        if (effect_result_states_cpu.empty()) {
            return Service::Audio::ResultInsufficientBuffer;
        }
        std::memset(effect_result_states_cpu.data(), 0, effect_result_states_cpu.size_bytes());
    }

    allocator.Align(0x40);

    unk_2B0 = allocator.GetSize() - allocator.GetCurrentOffset();
    unk_2A8 = {&workbuffer[allocator.GetCurrentOffset()], unk_2B0};

    upsampler_infos = allocator.Allocate<UpsamplerInfo>(upsampler_count, 0x40);
    for (auto& upsampler_info : upsampler_infos) {
        std::construct_at<UpsamplerInfo>(&upsampler_info);
    }

    std::construct_at<UpsamplerManager>(upsampler_manager, upsampler_count, upsampler_infos,
                                        upsampler_workbuffer);

    if (upsampler_infos.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    auto effect_infos{allocator.Allocate<EffectInfoBase>(params.effects, 0x40)};
    for (auto& effect_info : effect_infos) {
        std::construct_at<EffectInfoBase>(&effect_info);
    }

    if (effect_infos.empty() && params.effects > 0) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    std::span<EffectResultState> effect_result_states_dsp{};
    if (behavior.IsEffectInfoVersion2Supported() && params.effects > 0) {
        effect_result_states_dsp = allocator.Allocate<EffectResultState>(params.effects, 0x40);
        if (effect_result_states_dsp.empty()) {
            return Service::Audio::ResultInsufficientBuffer;
        }
        std::memset(effect_result_states_dsp.data(), 0, effect_result_states_dsp.size_bytes());
    }

    effect_context.Initialize(effect_infos, params.effects, effect_result_states_cpu,
                              effect_result_states_dsp, effect_result_states_dsp.size());

    auto sinks{allocator.Allocate<SinkInfoBase>(params.sinks, 0x10)};
    for (auto& sink : sinks) {
        std::construct_at<SinkInfoBase>(&sink);
    }

    if (sinks.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    sink_context.Initialize(sinks, params.sinks);

    auto voice_dsp_states{allocator.Allocate<VoiceState>(params.voices, 0x40)};
    if (voice_dsp_states.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    for (auto& voice_state : voice_dsp_states) {
        voice_state = {};
    }

    voice_context.Initialize(sorted_voice_infos, voice_infos, voice_channel_resources,
                             voice_cpu_states, voice_dsp_states, params.voices);

    if (params.perf_frames > 0) {
        const auto perf_workbuffer_size{
            PerformanceManager::GetRequiredBufferSizeForPerformanceMetricsPerFrame(behavior,
                                                                                   params) *
                (params.perf_frames + 1) +
            0xC};
        performance_workbuffer = allocator.Allocate<u8>(perf_workbuffer_size, 0x40);
        if (performance_workbuffer.empty()) {
            return Service::Audio::ResultInsufficientBuffer;
        }
        std::memset(performance_workbuffer.data(), 0, performance_workbuffer.size_bytes());
        performance_manager.Initialize(performance_workbuffer, performance_workbuffer.size_bytes(),
                                       params, behavior, memory_pool_info);
    }

    render_time_limit_percent = 100;
    drop_voice = params.voice_drop_enabled && params.execution_mode == ExecutionMode::Auto;
    drop_voice_param = 1.0f;
    num_voices_dropped = 0;

    allocator.Align(0x40);
    command_workbuffer_size = allocator.GetRemainingSize();
    command_workbuffer = allocator.Allocate<u8>(command_workbuffer_size, 0x40);
    if (command_workbuffer.empty()) {
        return Service::Audio::ResultInsufficientBuffer;
    }

    command_buffer_size = 0;
    reset_command_buffers = true;

    // nn::audio::dsp::FlushDataCache(transferMemory, transferMemorySize);

    if (behavior.IsCommandProcessingTimeEstimatorVersion5Supported()) {
        command_processing_time_estimator =
            std::make_unique<CommandProcessingTimeEstimatorVersion5>(sample_count,
                                                                     mix_buffer_count);
    } else if (behavior.IsCommandProcessingTimeEstimatorVersion4Supported()) {
        command_processing_time_estimator =
            std::make_unique<CommandProcessingTimeEstimatorVersion4>(sample_count,
                                                                     mix_buffer_count);
    } else if (behavior.IsCommandProcessingTimeEstimatorVersion3Supported()) {
        command_processing_time_estimator =
            std::make_unique<CommandProcessingTimeEstimatorVersion3>(sample_count,
                                                                     mix_buffer_count);
    } else if (behavior.IsCommandProcessingTimeEstimatorVersion2Supported()) {
        command_processing_time_estimator =
            std::make_unique<CommandProcessingTimeEstimatorVersion2>(sample_count,
                                                                     mix_buffer_count);
    } else {
        command_processing_time_estimator =
            std::make_unique<CommandProcessingTimeEstimatorVersion1>(sample_count,
                                                                     mix_buffer_count);
    }

    initialized = true;
    return ResultSuccess;
}

void System::Finalize() {
    if (!initialized) {
        return;
    }

    if (active) {
        Stop();
    }

    applet_resource_user_id = 0;

    PoolMapper pool_mapper(process_handle, false);
    pool_mapper.Unmap(memory_pool_info);

    if (process_handle) {
        pool_mapper.ClearUseState(memory_pool_workbuffer, memory_pool_count);
        for (auto& memory_pool : memory_pool_workbuffer) {
            if (memory_pool.IsMapped()) {
                pool_mapper.Unmap(memory_pool);
            }
        }

        // dsp::ProcessCleanup
        // close handle
    }
    initialized = false;
}

void System::Start() {
    std::scoped_lock l{lock};
    frames_elapsed = 0;
    state = State::Started;
    active = true;
}

void System::Stop() {
    {
        std::scoped_lock l{lock};
        state = State::Stopped;
        active = false;
    }

    if (execution_mode == ExecutionMode::Auto) {
        terminate_event.Wait();
    }
}

Result System::Update(std::span<const u8> input, std::span<u8> performance, std::span<u8> output) {
    std::scoped_lock l{lock};

    const auto start_time{core.CoreTiming().GetGlobalTimeNs().count()};
    std::memset(output.data(), 0, output.size());

    InfoUpdater info_updater(input, output, process_handle, behavior);

    auto result{info_updater.UpdateBehaviorInfo(behavior)};
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update BehaviorInfo!");
        return result;
    }

    result = info_updater.UpdateMemoryPools(memory_pool_workbuffer, memory_pool_count);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update MemoryPools!");
        return result;
    }

    result = info_updater.UpdateVoiceChannelResources(voice_context);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update VoiceChannelResources!");
        return result;
    }

    result = info_updater.UpdateVoices(voice_context, memory_pool_workbuffer, memory_pool_count);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update Voices!");
        return result;
    }

    result = info_updater.UpdateEffects(effect_context, active, memory_pool_workbuffer,
                                        memory_pool_count);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update Effects!");
        return result;
    }

    if (behavior.IsSplitterSupported()) {
        result = info_updater.UpdateSplitterInfo(splitter_context);
        if (result.IsError()) {
            LOG_ERROR(Service_Audio, "Failed to update SplitterInfo!");
            return result;
        }
    }

    result =
        info_updater.UpdateMixes(mix_context, mix_buffer_count, effect_context, splitter_context);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update Mixes!");
        return result;
    }

    result = info_updater.UpdateSinks(sink_context, memory_pool_workbuffer, memory_pool_count);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update Sinks!");
        return result;
    }

    PerformanceManager* perf_manager{nullptr};
    if (performance_manager.IsInitialized()) {
        perf_manager = &performance_manager;
    }

    result =
        info_updater.UpdatePerformanceBuffer(performance, performance.size_bytes(), perf_manager);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update PerformanceBuffer!");
        return result;
    }

    result = info_updater.UpdateErrorInfo(behavior);
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Failed to update ErrorInfo!");
        return result;
    }

    if (behavior.IsElapsedFrameCountSupported()) {
        result = info_updater.UpdateRendererInfo(frames_elapsed);
        if (result.IsError()) {
            LOG_ERROR(Service_Audio, "Failed to update RendererInfo!");
            return result;
        }
    }

    result = info_updater.CheckConsumedSize();
    if (result.IsError()) {
        LOG_ERROR(Service_Audio, "Invalid consume size!");
        return result;
    }

    adsp_rendered_event->Clear();
    num_times_updated++;

    const auto end_time{core.CoreTiming().GetGlobalTimeNs().count()};
    ticks_spent_updating += end_time - start_time;

    return ResultSuccess;
}

u32 System::GetRenderingTimeLimit() const {
    return render_time_limit_percent;
}

void System::SetRenderingTimeLimit(u32 limit) {
    render_time_limit_percent = limit;
}

u32 System::GetSessionId() const {
    return session_id;
}

u32 System::GetSampleRate() const {
    return sample_rate;
}

u32 System::GetSampleCount() const {
    return sample_count;
}

u32 System::GetMixBufferCount() const {
    return mix_buffer_count;
}

ExecutionMode System::GetExecutionMode() const {
    return execution_mode;
}

u32 System::GetRenderingDevice() const {
    return render_device;
}

bool System::IsActive() const {
    return active;
}

void System::SendCommandToDsp() {
    std::scoped_lock l{lock};

    if (initialized) {
        if (active) {
            terminate_event.Reset();
            const auto remaining_command_count{audio_renderer.GetRemainCommandCount(session_id)};
            u64 command_size{0};

            if (remaining_command_count) {
                adsp_behind = true;
                command_size = command_buffer_size;
            } else {
                command_size = GenerateCommand(command_workbuffer, command_workbuffer_size);
            }

            auto translated_addr{
                memory_pool_info.Translate(CpuAddr(command_workbuffer.data()), command_size)};

            auto time_limit_percent{70.0f};
            if (behavior.IsAudioRendererProcessingTimeLimit80PercentSupported()) {
                time_limit_percent = 80.0f;
            } else if (behavior.IsAudioRendererProcessingTimeLimit75PercentSupported()) {
                time_limit_percent = 75.0f;
            } else {
                // result ignored and 70 is used anyway
                behavior.IsAudioRendererProcessingTimeLimit70PercentSupported();
                time_limit_percent = 70.0f;
            }

            AudioRenderer::CommandBuffer command_buffer{
                .buffer{translated_addr},
                .size{command_size},
                .time_limit{
                    static_cast<u64>((time_limit_percent / 100) * 2'880'000.0 *
                                     (static_cast<f32>(render_time_limit_percent) / 100.0f))},
                .applet_resource_user_id{applet_resource_user_id},
                .reset_buffer{reset_command_buffers},
            };

            audio_renderer.SetCommandBuffer(session_id, command_buffer);
            reset_command_buffers = false;
            command_buffer_size = command_size;
            if (remaining_command_count == 0) {
                adsp_rendered_event->Signal();
            }
        } else {
            audio_renderer.ClearRemainCommandCount(session_id);
            terminate_event.Set();
        }
    }
}

u64 System::GenerateCommand(std::span<u8> in_command_buffer,
                            [[maybe_unused]] u64 command_buffer_size_) {
    PoolMapper::ClearUseState(memory_pool_workbuffer, memory_pool_count);
    const auto start_time{core.CoreTiming().GetGlobalTimeNs().count()};

    auto command_list_header{reinterpret_cast<CommandListHeader*>(in_command_buffer.data())};

    command_list_header->buffer_count = static_cast<s16>(voice_channels + mix_buffer_count);
    command_list_header->sample_count = sample_count;
    command_list_header->sample_rate = sample_rate;
    command_list_header->samples_buffer = samples_workbuffer;

    const auto performance_initialized{performance_manager.IsInitialized()};
    if (performance_initialized) {
        performance_manager.TapFrame(adsp_behind, num_voices_dropped, render_start_tick);
        adsp_behind = false;
        num_voices_dropped = 0;
        render_start_tick = 0;
    }

    s8 channel_count{2};
    if (execution_mode == ExecutionMode::Auto) {
        const auto& sink{core.AudioCore().GetOutputSink()};
        channel_count = static_cast<s8>(sink.GetDeviceChannels());
    }

    AudioRendererSystemContext render_context{
        .session_id{session_id},
        .channels{channel_count},
        .mix_buffer_count{mix_buffer_count},
        .behavior{&behavior},
        .depop_buffer{depop_buffer},
        .upsampler_manager{upsampler_manager},
        .memory_pool_info{&memory_pool_info},
    };

    CommandBuffer command_buffer{
        .command_list{in_command_buffer},
        .sample_count{sample_count},
        .sample_rate{sample_rate},
        .size{sizeof(CommandListHeader)},
        .count{0},
        .estimated_process_time{0},
        .memory_pool{&memory_pool_info},
        .time_estimator{command_processing_time_estimator.get()},
        .behavior{&behavior},
    };

    PerformanceManager* perf_manager{nullptr};
    if (performance_initialized) {
        perf_manager = &performance_manager;
    }

    CommandGenerator command_generator{command_buffer, *command_list_header, render_context,
                                       voice_context,  mix_context,          effect_context,
                                       sink_context,   splitter_context,     perf_manager};

    voice_context.SortInfo();

    const auto start_estimated_time{drop_voice_param *
                                    static_cast<f32>(command_buffer.estimated_process_time)};

    command_generator.GenerateVoiceCommands();
    command_generator.GenerateSubMixCommands();
    command_generator.GenerateFinalMixCommands();
    command_generator.GenerateSinkCommands();

    if (drop_voice) {
        f32 time_limit_percent{70.0f};
        if (render_context.behavior->IsAudioRendererProcessingTimeLimit80PercentSupported()) {
            time_limit_percent = 80.0f;
        } else if (render_context.behavior
                       ->IsAudioRendererProcessingTimeLimit75PercentSupported()) {
            time_limit_percent = 75.0f;
        } else {
            // result is ignored
            render_context.behavior->IsAudioRendererProcessingTimeLimit70PercentSupported();
            time_limit_percent = 70.0f;
        }

        const auto end_estimated_time{drop_voice_param *
                                      static_cast<f32>(command_buffer.estimated_process_time)};
        const auto estimated_time{start_estimated_time - end_estimated_time};

        const auto time_limit{static_cast<u32>(
            estimated_time + (((time_limit_percent / 100.0f) * 2'880'000.0) *
                              (static_cast<f32>(render_time_limit_percent) / 100.0f)))};
        num_voices_dropped =
            DropVoices(command_buffer, static_cast<u32>(start_estimated_time), time_limit);
    }

    command_list_header->buffer_size = command_buffer.size;
    command_list_header->command_count = command_buffer.count;

    voice_context.UpdateStateByDspShared();

    if (render_context.behavior->IsEffectInfoVersion2Supported()) {
        effect_context.UpdateStateByDspShared();
    }

    const auto end_time{core.CoreTiming().GetGlobalTimeNs().count()};
    total_ticks_elapsed += end_time - start_time;
    num_command_lists_generated++;
    render_start_tick = audio_renderer.GetRenderingStartTick(session_id);
    frames_elapsed++;

    return command_buffer.size;
}

f32 System::GetVoiceDropParameter() const {
    return drop_voice_param;
}

void System::SetVoiceDropParameter(f32 voice_drop_) {
    drop_voice_param = voice_drop_;
}

u32 System::DropVoices(CommandBuffer& command_buffer, u32 estimated_process_time, u32 time_limit) {
    u32 i{0};
    auto command_list{command_buffer.command_list.data() + sizeof(CommandListHeader)};
    ICommand* cmd{nullptr};

    // Find a first valid voice to drop
    while (i < command_buffer.count) {
        cmd = reinterpret_cast<ICommand*>(command_list);
        if (cmd->type == CommandId::Performance ||
            cmd->type == CommandId::DataSourcePcmInt16Version1 ||
            cmd->type == CommandId::DataSourcePcmInt16Version2 ||
            cmd->type == CommandId::DataSourcePcmFloatVersion1 ||
            cmd->type == CommandId::DataSourcePcmFloatVersion2 ||
            cmd->type == CommandId::DataSourceAdpcmVersion1 ||
            cmd->type == CommandId::DataSourceAdpcmVersion2) {
            break;
        }
        command_list += cmd->size;
        i++;
    }

    if (cmd == nullptr || command_buffer.count == 0 || i >= command_buffer.count) {
        return 0;
    }

    auto voices_dropped{0};
    while (i < command_buffer.count) {
        const auto node_id{cmd->node_id};
        const auto node_id_type{cmd->node_id >> 28};
        const auto node_id_base{(cmd->node_id >> 16) & 0xFFF};

        // If the new estimated process time falls below the limit, we're done dropping.
        if (estimated_process_time <= time_limit) {
            break;
        }

        if (node_id_type != 1) {
            break;
        }

        // Don't drop voices marked with the highest priority.
        auto& voice_info{voice_context.GetInfo(node_id_base)};
        if (voice_info.priority == HighestVoicePriority) {
            break;
        }

        voices_dropped++;
        voice_info.voice_dropped = true;

        // First iteration should drop the voice, and then iterate through all of the commands tied
        // to the voice. We don't need reverb on a voice which we've just removed, for example.
        // Depops can't be removed otherwise we'll introduce audio popping, and we don't
        // remove perf commands. Lower the estimated time for each command dropped.
        while (i < command_buffer.count && cmd->node_id == node_id) {
            if (cmd->type == CommandId::DepopPrepare) {
                cmd->enabled = true;
            } else if (cmd->enabled && cmd->type != CommandId::Performance) {
                cmd->enabled = false;
                estimated_process_time -= static_cast<u32>(
                    drop_voice_param * static_cast<f32>(cmd->estimated_process_time));
            }
            command_list += cmd->size;
            cmd = reinterpret_cast<ICommand*>(command_list);
            i++;
        }
        i++;
    }
    return voices_dropped;
}

} // namespace AudioCore::Renderer