summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Hexagon122019-05-19 15:23:04 +0100
committerGravatar GitHub2019-05-19 15:23:04 +0100
commit6fd247c84aa94945c99ed6ca32b268bf7afb044a (patch)
tree2ad82be44ca8b05cc47bf47cb0a8164fde491fe5 /src
parentMerge pull request #2467 from lioncash/move (diff)
parentservice/audren_u: Handle variadic command buffers in GetWorkBufferSize() (diff)
downloadyuzu-6fd247c84aa94945c99ed6ca32b268bf7afb044a.tar.gz
yuzu-6fd247c84aa94945c99ed6ca32b268bf7afb044a.tar.xz
yuzu-6fd247c84aa94945c99ed6ca32b268bf7afb044a.zip
Merge pull request #2439 from lioncash/audren
service/audren_u: Get rid of magic values within GetAudioRendererWorkBufferSize
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/audio/audren_u.cpp348
-rw-r--r--src/core/hle/service/audio/audren_u.h2
2 files changed, 299 insertions, 51 deletions
diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp
index e69f6cf7f..75db0c2dc 100644
--- a/src/core/hle/service/audio/audren_u.cpp
+++ b/src/core/hle/service/audio/audren_u.cpp
@@ -8,6 +8,7 @@
8 8
9#include "audio_core/audio_renderer.h" 9#include "audio_core/audio_renderer.h"
10#include "common/alignment.h" 10#include "common/alignment.h"
11#include "common/bit_util.h"
11#include "common/common_funcs.h" 12#include "common/common_funcs.h"
12#include "common/logging/log.h" 13#include "common/logging/log.h"
13#include "common/string_util.h" 14#include "common/string_util.h"
@@ -262,64 +263,304 @@ void AudRenU::OpenAudioRenderer(Kernel::HLERequestContext& ctx) {
262 OpenAudioRendererImpl(ctx); 263 OpenAudioRendererImpl(ctx);
263} 264}
264 265
266static u64 CalculateNumPerformanceEntries(const AudioCore::AudioRendererParameter& params) {
267 // +1 represents the final mix.
268 return u64{params.effect_count} + params.submix_count + params.sink_count + params.voice_count +
269 1;
270}
271
265void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { 272void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) {
266 IPC::RequestParser rp{ctx};
267 auto params = rp.PopRaw<AudioCore::AudioRendererParameter>();
268 LOG_DEBUG(Service_Audio, "called"); 273 LOG_DEBUG(Service_Audio, "called");
269 274
270 u64 buffer_sz = Common::AlignUp(4 * params.mix_buffer_count, 0x40); 275 // Several calculations below align the sizes being calculated
271 buffer_sz += params.submix_count * 1024; 276 // onto a 64 byte boundary.
272 buffer_sz += 0x940 * (params.submix_count + 1); 277 static constexpr u64 buffer_alignment_size = 64;
273 buffer_sz += 0x3F0 * params.voice_count; 278
274 buffer_sz += Common::AlignUp(8 * (params.submix_count + 1), 0x10); 279 // Some calculations that calculate portions of the buffer
275 buffer_sz += Common::AlignUp(8 * params.voice_count, 0x10); 280 // that will contain information, on the other hand, align
276 buffer_sz += Common::AlignUp( 281 // the result of some of their calcularions on a 16 byte boundary.
277 (0x3C0 * (params.sink_count + params.submix_count) + 4 * params.sample_count) * 282 static constexpr u64 info_field_alignment_size = 16;
278 (params.mix_buffer_count + 6), 283
279 0x40); 284 // Maximum detail entries that may exist at one time for performance
280 285 // frame statistics.
281 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) { 286 static constexpr u64 max_perf_detail_entries = 100;
282 const u32 count = params.submix_count + 1; 287
283 u64 node_count = Common::AlignUp(count, 0x40); 288 // Size of the data structure representing the bulk of the voice-related state.
284 const u64 node_state_buffer_sz = 289 static constexpr u64 voice_state_size = 0x100;
285 4 * (node_count * node_count) + 0xC * node_count + 2 * (node_count / 8); 290
286 u64 edge_matrix_buffer_sz = 0; 291 // Size of the upsampler manager data structure
287 node_count = Common::AlignUp(count * count, 0x40); 292 constexpr u64 upsampler_manager_size = 0x48;
288 if (node_count >> 31 != 0) { 293
289 edge_matrix_buffer_sz = (node_count | 7) / 8; 294 // Calculates the part of the size that relates to mix buffers.
290 } else { 295 const auto calculate_mix_buffer_sizes = [](const AudioCore::AudioRendererParameter& params) {
291 edge_matrix_buffer_sz = node_count / 8; 296 // As of 8.0.0 this is the maximum on voice channels.
297 constexpr u64 max_voice_channels = 6;
298
299 // The service expects the sample_count member of the parameters to either be
300 // a value of 160 or 240, so the maximum sample count is assumed in order
301 // to adequately handle all values at runtime.
302 constexpr u64 default_max_sample_count = 240;
303
304 const u64 total_mix_buffers = params.mix_buffer_count + max_voice_channels;
305
306 u64 size = 0;
307 size += total_mix_buffers * (sizeof(s32) * params.sample_count);
308 size += total_mix_buffers * (sizeof(s32) * default_max_sample_count);
309 size += u64{params.submix_count} + params.sink_count;
310 size = Common::AlignUp(size, buffer_alignment_size);
311 size += Common::AlignUp(params.unknown_30, buffer_alignment_size);
312 size += Common::AlignUp(sizeof(s32) * params.mix_buffer_count, buffer_alignment_size);
313 return size;
314 };
315
316 // Calculates the portion of the size related to the mix data (and the sorting thereof).
317 const auto calculate_mix_info_size = [this](const AudioCore::AudioRendererParameter& params) {
318 // The size of the mixing info data structure.
319 constexpr u64 mix_info_size = 0x940;
320
321 // Consists of total submixes with the final mix included.
322 const u64 total_mix_count = u64{params.submix_count} + 1;
323
324 // The total number of effects that may be available to the audio renderer at any time.
325 constexpr u64 max_effects = 256;
326
327 // Calculates the part of the size related to the audio node state.
328 // This will only be used if the audio revision supports the splitter.
329 const auto calculate_node_state_size = [](std::size_t num_nodes) {
330 // Internally within a nodestate, it appears to use a data structure
331 // similar to a std::bitset<64> twice.
332 constexpr u64 bit_size = Common::BitSize<u64>();
333 constexpr u64 num_bitsets = 2;
334
335 // Node state instances have three states internally for performing
336 // depth-first searches of nodes. Initialized, Found, and Done Sorting.
337 constexpr u64 num_states = 3;
338
339 u64 size = 0;
340 size += (num_nodes * num_nodes) * sizeof(s32);
341 size += num_states * (num_nodes * sizeof(s32));
342 size += num_bitsets * (Common::AlignUp(num_nodes, bit_size) / Common::BitSize<u8>());
343 return size;
344 };
345
346 // Calculates the part of the size related to the adjacency (aka edge) matrix.
347 const auto calculate_edge_matrix_size = [](std::size_t num_nodes) {
348 return (num_nodes * num_nodes) * sizeof(s32);
349 };
350
351 u64 size = 0;
352 size += Common::AlignUp(sizeof(void*) * total_mix_count, info_field_alignment_size);
353 size += Common::AlignUp(mix_info_size * total_mix_count, info_field_alignment_size);
354 size += Common::AlignUp(sizeof(s32) * max_effects * params.submix_count,
355 info_field_alignment_size);
356
357 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
358 size += Common::AlignUp(calculate_node_state_size(total_mix_count) +
359 calculate_edge_matrix_size(total_mix_count),
360 info_field_alignment_size);
292 } 361 }
293 buffer_sz += Common::AlignUp(node_state_buffer_sz + edge_matrix_buffer_sz, 0x10);
294 }
295 362
296 buffer_sz += 0x20 * (params.effect_count + 4 * params.voice_count) + 0x50; 363 return size;
297 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) { 364 };
298 buffer_sz += 0xE0 * params.num_splitter_send_channels;
299 buffer_sz += 0x20 * params.splitter_count;
300 buffer_sz += Common::AlignUp(4 * params.num_splitter_send_channels, 0x10);
301 }
302 buffer_sz = Common::AlignUp(buffer_sz, 0x40) + 0x170 * params.sink_count;
303 u64 output_sz = buffer_sz + 0x280 * params.sink_count + 0x4B0 * params.effect_count +
304 ((params.voice_count * 256) | 0x40);
305
306 if (params.performance_frame_count >= 1) {
307 output_sz = Common::AlignUp(((16 * params.sink_count + 16 * params.effect_count +
308 16 * params.voice_count + 16) +
309 0x658) *
310 (params.performance_frame_count + 1) +
311 0xc0,
312 0x40) +
313 output_sz;
314 }
315 output_sz = Common::AlignUp(output_sz + 0x1807e, 0x1000);
316 365
317 IPC::ResponseBuilder rb{ctx, 4}; 366 // Calculates the part of the size related to voice channel info.
367 const auto calculate_voice_info_size = [](const AudioCore::AudioRendererParameter& params) {
368 constexpr u64 voice_info_size = 0x220;
369 constexpr u64 voice_resource_size = 0xD0;
370
371 u64 size = 0;
372 size += Common::AlignUp(sizeof(void*) * params.voice_count, info_field_alignment_size);
373 size += Common::AlignUp(voice_info_size * params.voice_count, info_field_alignment_size);
374 size +=
375 Common::AlignUp(voice_resource_size * params.voice_count, info_field_alignment_size);
376 size += Common::AlignUp(voice_state_size * params.voice_count, info_field_alignment_size);
377 return size;
378 };
379
380 // Calculates the part of the size related to memory pools.
381 const auto calculate_memory_pools_size = [](const AudioCore::AudioRendererParameter& params) {
382 const u64 num_memory_pools = sizeof(s32) * (u64{params.effect_count} + params.voice_count);
383 const u64 memory_pool_info_size = 0x20;
384 return Common::AlignUp(num_memory_pools * memory_pool_info_size, info_field_alignment_size);
385 };
386
387 // Calculates the part of the size related to the splitter context.
388 const auto calculate_splitter_context_size =
389 [this](const AudioCore::AudioRendererParameter& params) -> u64 {
390 if (!IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
391 return 0;
392 }
393
394 constexpr u64 splitter_info_size = 0x20;
395 constexpr u64 splitter_destination_data_size = 0xE0;
396
397 u64 size = 0;
398 size += params.num_splitter_send_channels;
399 size +=
400 Common::AlignUp(splitter_info_size * params.splitter_count, info_field_alignment_size);
401 size += Common::AlignUp(splitter_destination_data_size * params.num_splitter_send_channels,
402 info_field_alignment_size);
403
404 return size;
405 };
406
407 // Calculates the part of the size related to the upsampler info.
408 const auto calculate_upsampler_info_size = [](const AudioCore::AudioRendererParameter& params) {
409 constexpr u64 upsampler_info_size = 0x280;
410 // Yes, using the buffer size over info alignment size is intentional here.
411 return Common::AlignUp(upsampler_info_size * (u64{params.submix_count} + params.sink_count),
412 buffer_alignment_size);
413 };
414
415 // Calculates the part of the size related to effect info.
416 const auto calculate_effect_info_size = [](const AudioCore::AudioRendererParameter& params) {
417 constexpr u64 effect_info_size = 0x2B0;
418 return Common::AlignUp(effect_info_size * params.effect_count, info_field_alignment_size);
419 };
420
421 // Calculates the part of the size related to audio sink info.
422 const auto calculate_sink_info_size = [](const AudioCore::AudioRendererParameter& params) {
423 const u64 sink_info_size = 0x170;
424 return Common::AlignUp(sink_info_size * params.sink_count, info_field_alignment_size);
425 };
426
427 // Calculates the part of the size related to voice state info.
428 const auto calculate_voice_state_size = [](const AudioCore::AudioRendererParameter& params) {
429 const u64 voice_state_size = 0x100;
430 const u64 additional_size = buffer_alignment_size - 1;
431 return Common::AlignUp(voice_state_size * params.voice_count + additional_size,
432 info_field_alignment_size);
433 };
434
435 // Calculates the part of the size related to performance statistics.
436 const auto calculate_perf_size = [this](const AudioCore::AudioRendererParameter& params) {
437 // Extra size value appended to the end of the calculation.
438 constexpr u64 appended = 128;
439
440 // Whether or not we assume the newer version of performance metrics data structures.
441 const bool is_v2 =
442 IsFeatureSupported(AudioFeatures::PerformanceMetricsVersion2, params.revision);
443
444 // Data structure sizes
445 constexpr u64 perf_statistics_size = 0x0C;
446 const u64 header_size = is_v2 ? 0x30 : 0x18;
447 const u64 entry_size = is_v2 ? 0x18 : 0x10;
448 const u64 detail_size = is_v2 ? 0x18 : 0x10;
449
450 const u64 entry_count = CalculateNumPerformanceEntries(params);
451 const u64 size_per_frame =
452 header_size + (entry_size * entry_count) + (detail_size * max_perf_detail_entries);
453
454 u64 size = 0;
455 size += Common::AlignUp(size_per_frame * params.performance_frame_count + 1,
456 buffer_alignment_size);
457 size += Common::AlignUp(perf_statistics_size, buffer_alignment_size);
458 size += appended;
459 return size;
460 };
461
462 // Calculates the part of the size that relates to the audio command buffer.
463 const auto calculate_command_buffer_size =
464 [this](const AudioCore::AudioRendererParameter& params) {
465 constexpr u64 alignment = (buffer_alignment_size - 1) * 2;
466
467 if (!IsFeatureSupported(AudioFeatures::VariadicCommandBuffer, params.revision)) {
468 constexpr u64 command_buffer_size = 0x18000;
469
470 return command_buffer_size + alignment;
471 }
472
473 // When the variadic command buffer is supported, this means
474 // the command generator for the audio renderer can issue commands
475 // that are (as one would expect), variable in size. So what we need to do
476 // is determine the maximum possible size for a few command data structures
477 // then multiply them by the amount of present commands indicated by the given
478 // respective audio parameters.
479
480 constexpr u64 max_biquad_filters = 2;
481 constexpr u64 max_mix_buffers = 24;
482
483 constexpr u64 biquad_filter_command_size = 0x2C;
484
485 constexpr u64 depop_mix_command_size = 0x24;
486 constexpr u64 depop_setup_command_size = 0x50;
487
488 constexpr u64 effect_command_max_size = 0x540;
489
490 constexpr u64 mix_command_size = 0x1C;
491 constexpr u64 mix_ramp_command_size = 0x24;
492 constexpr u64 mix_ramp_grouped_command_size = 0x13C;
493
494 constexpr u64 perf_command_size = 0x28;
495
496 constexpr u64 sink_command_size = 0x130;
497
498 constexpr u64 submix_command_max_size =
499 depop_mix_command_size + (mix_command_size * max_mix_buffers) * max_mix_buffers;
500
501 constexpr u64 volume_command_size = 0x1C;
502 constexpr u64 volume_ramp_command_size = 0x20;
503
504 constexpr u64 voice_biquad_filter_command_size =
505 biquad_filter_command_size * max_biquad_filters;
506 constexpr u64 voice_data_command_size = 0x9C;
507 const u64 voice_command_max_size =
508 (params.splitter_count * depop_setup_command_size) +
509 (voice_data_command_size + voice_biquad_filter_command_size +
510 volume_ramp_command_size + mix_ramp_grouped_command_size);
511
512 // Now calculate the individual elements that comprise the size and add them together.
513 const u64 effect_commands_size = params.effect_count * effect_command_max_size;
514
515 const u64 final_mix_commands_size =
516 depop_mix_command_size + volume_command_size * max_mix_buffers;
318 517
518 const u64 perf_commands_size =
519 perf_command_size *
520 (CalculateNumPerformanceEntries(params) + max_perf_detail_entries);
521
522 const u64 sink_commands_size = params.sink_count * sink_command_size;
523
524 const u64 splitter_commands_size =
525 params.num_splitter_send_channels * max_mix_buffers * mix_ramp_command_size;
526
527 const u64 submix_commands_size = params.submix_count * submix_command_max_size;
528
529 const u64 voice_commands_size = params.voice_count * voice_command_max_size;
530
531 return effect_commands_size + final_mix_commands_size + perf_commands_size +
532 sink_commands_size + splitter_commands_size + submix_commands_size +
533 voice_commands_size + alignment;
534 };
535
536 IPC::RequestParser rp{ctx};
537 const auto params = rp.PopRaw<AudioCore::AudioRendererParameter>();
538
539 u64 size = 0;
540 size += calculate_mix_buffer_sizes(params);
541 size += calculate_mix_info_size(params);
542 size += calculate_voice_info_size(params);
543 size += upsampler_manager_size;
544 size += calculate_memory_pools_size(params);
545 size += calculate_splitter_context_size(params);
546
547 size = Common::AlignUp(size, buffer_alignment_size);
548
549 size += calculate_upsampler_info_size(params);
550 size += calculate_effect_info_size(params);
551 size += calculate_sink_info_size(params);
552 size += calculate_voice_state_size(params);
553 size += calculate_perf_size(params);
554 size += calculate_command_buffer_size(params);
555
556 // finally, 4KB page align the size, and we're done.
557 size = Common::AlignUp(size, 4096);
558
559 IPC::ResponseBuilder rb{ctx, 4};
319 rb.Push(RESULT_SUCCESS); 560 rb.Push(RESULT_SUCCESS);
320 rb.Push<u64>(output_sz); 561 rb.Push<u64>(size);
321 562
322 LOG_DEBUG(Service_Audio, "buffer_size=0x{:X}", output_sz); 563 LOG_DEBUG(Service_Audio, "buffer_size=0x{:X}", size);
323} 564}
324 565
325void AudRenU::GetAudioDeviceService(Kernel::HLERequestContext& ctx) { 566void AudRenU::GetAudioDeviceService(Kernel::HLERequestContext& ctx) {
@@ -357,10 +598,15 @@ void AudRenU::OpenAudioRendererImpl(Kernel::HLERequestContext& ctx) {
357} 598}
358 599
359bool AudRenU::IsFeatureSupported(AudioFeatures feature, u32_le revision) const { 600bool AudRenU::IsFeatureSupported(AudioFeatures feature, u32_le revision) const {
360 u32_be version_num = (revision - Common::MakeMagic('R', 'E', 'V', '0')); // Byte swap 601 // Byte swap
602 const u32_be version_num = revision - Common::MakeMagic('R', 'E', 'V', '0');
603
361 switch (feature) { 604 switch (feature) {
362 case AudioFeatures::Splitter: 605 case AudioFeatures::Splitter:
363 return version_num >= 2u; 606 return version_num >= 2U;
607 case AudioFeatures::PerformanceMetricsVersion2:
608 case AudioFeatures::VariadicCommandBuffer:
609 return version_num >= 5U;
364 default: 610 default:
365 return false; 611 return false;
366 } 612 }
diff --git a/src/core/hle/service/audio/audren_u.h b/src/core/hle/service/audio/audren_u.h
index e55d25973..1d3c8df61 100644
--- a/src/core/hle/service/audio/audren_u.h
+++ b/src/core/hle/service/audio/audren_u.h
@@ -28,6 +28,8 @@ private:
28 28
29 enum class AudioFeatures : u32 { 29 enum class AudioFeatures : u32 {
30 Splitter, 30 Splitter,
31 PerformanceMetricsVersion2,
32 VariadicCommandBuffer,
31 }; 33 };
32 34
33 bool IsFeatureSupported(AudioFeatures feature, u32_le revision) const; 35 bool IsFeatureSupported(AudioFeatures feature, u32_le revision) const;