summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Lioncash2019-04-29 08:47:24 -0400
committerGravatar Lioncash2019-04-30 23:51:59 -0400
commitde93507a5a93ca12b5dd91aed51238923cfd6f86 (patch)
treee4ab3165a0da9a6726413667d59f6b541e1c54d9 /src
parentMerge pull request #2416 from lioncash/wait (diff)
downloadyuzu-de93507a5a93ca12b5dd91aed51238923cfd6f86.tar.gz
yuzu-de93507a5a93ca12b5dd91aed51238923cfd6f86.tar.xz
yuzu-de93507a5a93ca12b5dd91aed51238923cfd6f86.zip
service/audren_u: Clean up work buffer calculations
"Unmagics" quite a few magic constants within this code, making it much easier to understand. Particularly given this factors out specific sections into their own self-contained lambda functions.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/audio/audren_u.cpp263
1 files changed, 214 insertions, 49 deletions
diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp
index 1dde6edb7..4c9e124cb 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"
@@ -263,63 +264,227 @@ void AudRenU::OpenAudioRenderer(Kernel::HLERequestContext& ctx) {
263} 264}
264 265
265void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) { 266void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) {
266 IPC::RequestParser rp{ctx};
267 auto params = rp.PopRaw<AudioCore::AudioRendererParameter>();
268 LOG_DEBUG(Service_Audio, "called"); 267 LOG_DEBUG(Service_Audio, "called");
269 268
270 u64 buffer_sz = Common::AlignUp(4 * params.mix_buffer_count, 0x40); 269 IPC::RequestParser rp{ctx};
271 buffer_sz += params.submix_count * 1024; 270 const auto params = rp.PopRaw<AudioCore::AudioRendererParameter>();
272 buffer_sz += 0x940 * (params.submix_count + 1); 271
273 buffer_sz += 0x3F0 * params.voice_count; 272 // Several calculations below align the sizes being calculated
274 buffer_sz += Common::AlignUp(8 * (params.submix_count + 1), 0x10); 273 // onto a 64 byte boundary.
275 buffer_sz += Common::AlignUp(8 * params.voice_count, 0x10); 274 static constexpr u64 buffer_alignment_size = 64;
276 buffer_sz += Common::AlignUp( 275
277 (0x3C0 * (params.sink_count + params.submix_count) + 4 * params.sample_count) * 276 // Some calculations that calculate portions of the buffer
278 (params.mix_buffer_count + 6), 277 // that will contain information, on the other hand, align
279 0x40); 278 // the result of some of their calcularions on a 16 byte boundary.
280 279 static constexpr u64 info_field_alignment_size = 16;
281 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) { 280
282 const u32 count = params.submix_count + 1; 281 // Size of the data structure representing the bulk of the voice-related state.
283 u64 node_count = Common::AlignUp(count, 0x40); 282 static constexpr u64 voice_state_size = 0x100;
284 const u64 node_state_buffer_sz = 283
285 4 * (node_count * node_count) + 0xC * node_count + 2 * (node_count / 8); 284 // Size of the upsampler manager data structure
286 u64 edge_matrix_buffer_sz = 0; 285 constexpr u64 upsampler_manager_size = 0x48;
287 node_count = Common::AlignUp(count * count, 0x40); 286
288 if (node_count >> 31 != 0) { 287 // Calculates the part of the size that relates to mix buffers.
289 edge_matrix_buffer_sz = (node_count | 7) / 8; 288 const auto calculate_mix_buffer_sizes = [](const AudioCore::AudioRendererParameter& params) {
290 } else { 289 // As of 8.0.0 this is the maximum on voice channels.
291 edge_matrix_buffer_sz = node_count / 8; 290 constexpr u64 max_voice_channels = 6;
291
292 // The service expects the sample_count member of the parameters to either be
293 // a value of 160 or 240, so the maximum sample count is assumed in order
294 // to adequately handle all values at runtime.
295 constexpr u64 default_max_sample_count = 240;
296
297 const u64 total_mix_buffers = params.mix_buffer_count + max_voice_channels;
298
299 u64 size = 0;
300 size += total_mix_buffers * (sizeof(s32) * params.sample_count);
301 size += total_mix_buffers * (sizeof(s32) * default_max_sample_count);
302 size += u64{params.submix_count} + params.sink_count;
303 size = Common::AlignUp(size, buffer_alignment_size);
304 size += Common::AlignUp(params.unknown_30, buffer_alignment_size);
305 size += Common::AlignUp(sizeof(s32) * params.mix_buffer_count, buffer_alignment_size);
306 return size;
307 };
308
309 // Calculates the portion of the size related to the mix data (and the sorting thereof).
310 const auto calculate_mix_info_size = [this](const AudioCore::AudioRendererParameter& params) {
311 // The size of the mixing info data structure.
312 constexpr u64 mix_info_size = 0x940;
313
314 // Consists of total submixes with the final mix included.
315 const u64 total_mix_count = u64{params.submix_count} + 1;
316
317 // The total number of effects that may be available to the audio renderer at any time.
318 constexpr u64 max_effects = 256;
319
320 // Calculates the part of the size related to the audio node state.
321 // This will only be used if the audio revision supports the splitter.
322 const auto calculate_node_state_size = [](std::size_t num_nodes) {
323 // Internally within a nodestate, it appears to use a data structure
324 // similar to a std::bitset<64> twice.
325 constexpr u64 bit_size = Common::BitSize<u64>();
326 constexpr u64 num_bitsets = 2;
327
328 // Node state instances have three states internally for performing
329 // depth-first searches of nodes. Initialized, Found, and Done Sorting.
330 constexpr u64 num_states = 3;
331
332 u64 size = 0;
333 size += (num_nodes * num_nodes) * sizeof(s32);
334 size += num_states * (num_nodes * sizeof(s32));
335 size += num_bitsets * (Common::AlignUp(num_nodes, bit_size) / Common::BitSize<u8>());
336 return size;
337 };
338
339 // Calculates the part of the size related to the adjacency (aka edge) matrix.
340 const auto calculate_edge_matrix_size = [](std::size_t num_nodes) {
341 return (num_nodes * num_nodes) * sizeof(s32);
342 };
343
344 u64 size = 0;
345 size += Common::AlignUp(sizeof(void*) * total_mix_count, info_field_alignment_size);
346 size += Common::AlignUp(mix_info_size * total_mix_count, info_field_alignment_size);
347 size += Common::AlignUp(sizeof(s32) * max_effects * params.submix_count,
348 info_field_alignment_size);
349
350 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
351 size += Common::AlignUp(calculate_node_state_size(total_mix_count) +
352 calculate_edge_matrix_size(total_mix_count),
353 info_field_alignment_size);
292 } 354 }
293 buffer_sz += Common::AlignUp(node_state_buffer_sz + edge_matrix_buffer_sz, 0x10);
294 }
295 355
296 buffer_sz += 0x20 * (params.effect_count + 4 * params.voice_count) + 0x50; 356 return size;
297 if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) { 357 };
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 358
317 IPC::ResponseBuilder rb{ctx, 4}; 359 // Calculates the part of the size related to voice channel info.
360 const auto calculate_voice_info_size = [](const AudioCore::AudioRendererParameter& params) {
361 constexpr u64 voice_info_size = 0x220;
362 constexpr u64 voice_resource_size = 0xD0;
363
364 u64 size = 0;
365 size += Common::AlignUp(sizeof(void*) * params.voice_count, info_field_alignment_size);
366 size += Common::AlignUp(voice_info_size * params.voice_count, info_field_alignment_size);
367 size +=
368 Common::AlignUp(voice_resource_size * params.voice_count, info_field_alignment_size);
369 size += Common::AlignUp(voice_state_size * params.voice_count, info_field_alignment_size);
370 return size;
371 };
372
373 // Calculates the part of the size related to memory pools.
374 const auto calculate_memory_pools_size = [](const AudioCore::AudioRendererParameter& params) {
375 const u64 num_memory_pools = sizeof(s32) * (u64{params.effect_count} + params.voice_count);
376 const u64 memory_pool_info_size = 0x20;
377 return Common::AlignUp(num_memory_pools * memory_pool_info_size, info_field_alignment_size);
378 };
379
380 // Calculates the part of the size related to the splitter context.
381 const auto calculate_splitter_context_size =
382 [this](const AudioCore::AudioRendererParameter& params) -> u64 {
383 if (!IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
384 return 0;
385 }
318 386
387 constexpr u64 splitter_info_size = 0x20;
388 constexpr u64 splitter_destination_data_size = 0xE0;
389
390 u64 size = 0;
391 size += params.num_splitter_send_channels;
392 size +=
393 Common::AlignUp(splitter_info_size * params.splitter_count, info_field_alignment_size);
394 size += Common::AlignUp(splitter_destination_data_size * params.num_splitter_send_channels,
395 info_field_alignment_size);
396
397 return size;
398 };
399
400 // Calculates the part of the size related to the upsampler info.
401 const auto calculate_upsampler_info_size = [](const AudioCore::AudioRendererParameter& params) {
402 constexpr u64 upsampler_info_size = 0x280;
403 // Yes, using the buffer size over info alignment size is intentional here.
404 return Common::AlignUp(upsampler_info_size * (u64{params.submix_count} + params.sink_count),
405 buffer_alignment_size);
406 };
407
408 // Calculates the part of the size related to effect info.
409 const auto calculate_effect_info_size = [](const AudioCore::AudioRendererParameter& params) {
410 constexpr u64 effect_info_size = 0x2B0;
411 return Common::AlignUp(effect_info_size * params.effect_count, info_field_alignment_size);
412 };
413
414 // Calculates the part of the size related to audio sink info.
415 const auto calculate_sink_info_size = [](const AudioCore::AudioRendererParameter& params) {
416 const u64 sink_info_size = 0x170;
417 return Common::AlignUp(sink_info_size * params.sink_count, info_field_alignment_size);
418 };
419
420 // Calculates the part of the size related to voice state info.
421 const auto calculate_voice_state_size = [](const AudioCore::AudioRendererParameter& params) {
422 const u64 voice_state_size = 0x100;
423 const u64 additional_size = buffer_alignment_size - 1;
424 return Common::AlignUp(voice_state_size * params.voice_count + additional_size,
425 info_field_alignment_size);
426 };
427
428 // Calculates the part of the size related to performance statistics.
429 const auto calculate_performance_size = [](const AudioCore::AudioRendererParameter& params) {
430 // Extra size value appended to the end of the calculation.
431 constexpr u64 appended = 128;
432
433 // Data structure sizes
434 constexpr u64 perf_statistics_size = 0x0C;
435 constexpr u64 header_size = 0x18;
436 constexpr u64 entry_size = 0x10;
437 constexpr u64 detail_size = 0x10;
438
439 constexpr u64 max_detail_entries = 100;
440
441 // + 1 to include the final mix, similar to calculating mix info.
442 const u64 entry_count = u64{params.effect_count} + params.submix_count + params.sink_count +
443 params.voice_count + 1;
444
445 const u64 size_per_frame =
446 header_size + (entry_size * entry_count) + (detail_size * max_detail_entries);
447
448 u64 size = 0;
449 size += Common::AlignUp(size_per_frame * params.performance_frame_count + 1,
450 buffer_alignment_size);
451 size += Common::AlignUp(perf_statistics_size, buffer_alignment_size);
452 size += appended;
453 return size;
454 };
455
456 // Calculates the part of the size that relates to the audio command buffer.
457 const auto calculate_command_buffer_size = [] {
458 constexpr u64 command_buffer_size = 0x18000;
459 constexpr u64 alignment = (buffer_alignment_size - 1) * 2;
460 return command_buffer_size + alignment;
461 };
462
463 u64 size = 0;
464 size += calculate_mix_buffer_sizes(params);
465 size += calculate_mix_info_size(params);
466 size += calculate_voice_info_size(params);
467 size += upsampler_manager_size;
468 size += calculate_memory_pools_size(params);
469 size += calculate_splitter_context_size(params);
470
471 size = Common::AlignUp(size, buffer_alignment_size);
472
473 size += calculate_upsampler_info_size(params);
474 size += calculate_effect_info_size(params);
475 size += calculate_sink_info_size(params);
476 size += calculate_voice_state_size(params);
477 size += calculate_performance_size(params);
478 size += calculate_command_buffer_size();
479
480 // finally, 4KB page align the size, and we're done.
481 size = Common::AlignUp(size, 4096);
482
483 IPC::ResponseBuilder rb{ctx, 4};
319 rb.Push(RESULT_SUCCESS); 484 rb.Push(RESULT_SUCCESS);
320 rb.Push<u64>(output_sz); 485 rb.Push<u64>(size);
321 486
322 LOG_DEBUG(Service_Audio, "buffer_size=0x{:X}", output_sz); 487 LOG_DEBUG(Service_Audio, "buffer_size=0x{:X}", size);
323} 488}
324 489
325void AudRenU::GetAudioDeviceService(Kernel::HLERequestContext& ctx) { 490void AudRenU::GetAudioDeviceService(Kernel::HLERequestContext& ctx) {