diff options
| author | 2023-05-23 14:45:54 +0100 | |
|---|---|---|
| committer | 2023-06-22 08:05:10 +0100 | |
| commit | 5da70f719703084482933e103e561cc98163f370 (patch) | |
| tree | 1926842ed2b90bf92b89cec6a314bb28c7287fe9 /src/audio_core/device | |
| parent | Merge pull request #10086 from Morph1984/coretiming-ng-1 (diff) | |
| download | yuzu-5da70f719703084482933e103e561cc98163f370.tar.gz yuzu-5da70f719703084482933e103e561cc98163f370.tar.xz yuzu-5da70f719703084482933e103e561cc98163f370.zip | |
Remove memory allocations in some hot paths
Diffstat (limited to 'src/audio_core/device')
| -rw-r--r-- | src/audio_core/device/audio_buffers.h | 8 | ||||
| -rw-r--r-- | src/audio_core/device/device_session.cpp | 12 | ||||
| -rw-r--r-- | src/audio_core/device/device_session.h | 7 |
3 files changed, 15 insertions, 12 deletions
diff --git a/src/audio_core/device/audio_buffers.h b/src/audio_core/device/audio_buffers.h index 15082f6c6..5d8ed0ef7 100644 --- a/src/audio_core/device/audio_buffers.h +++ b/src/audio_core/device/audio_buffers.h | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <mutex> | 7 | #include <mutex> |
| 8 | #include <span> | 8 | #include <span> |
| 9 | #include <vector> | 9 | #include <vector> |
| 10 | #include <boost/container/static_vector.hpp> | ||
| 10 | 11 | ||
| 11 | #include "audio_buffer.h" | 12 | #include "audio_buffer.h" |
| 12 | #include "audio_core/device/device_session.h" | 13 | #include "audio_core/device/device_session.h" |
| @@ -48,7 +49,7 @@ public: | |||
| 48 | * | 49 | * |
| 49 | * @param out_buffers - The buffers which were registered. | 50 | * @param out_buffers - The buffers which were registered. |
| 50 | */ | 51 | */ |
| 51 | void RegisterBuffers(std::vector<AudioBuffer>& out_buffers) { | 52 | void RegisterBuffers(boost::container::static_vector<AudioBuffer, N>& out_buffers) { |
| 52 | std::scoped_lock l{lock}; | 53 | std::scoped_lock l{lock}; |
| 53 | const s32 to_register{std::min(std::min(appended_count, BufferAppendLimit), | 54 | const s32 to_register{std::min(std::min(appended_count, BufferAppendLimit), |
| 54 | BufferAppendLimit - registered_count)}; | 55 | BufferAppendLimit - registered_count)}; |
| @@ -162,7 +163,8 @@ public: | |||
| 162 | * @param max_buffers - Maximum number of buffers to released. | 163 | * @param max_buffers - Maximum number of buffers to released. |
| 163 | * @return The number of buffers released. | 164 | * @return The number of buffers released. |
| 164 | */ | 165 | */ |
| 165 | u32 GetRegisteredAppendedBuffers(std::vector<AudioBuffer>& buffers_flushed, u32 max_buffers) { | 166 | u32 GetRegisteredAppendedBuffers( |
| 167 | boost::container::static_vector<AudioBuffer, N>& buffers_flushed, u32 max_buffers) { | ||
| 166 | std::scoped_lock l{lock}; | 168 | std::scoped_lock l{lock}; |
| 167 | if (registered_count + appended_count == 0) { | 169 | if (registered_count + appended_count == 0) { |
| 168 | return 0; | 170 | return 0; |
| @@ -270,7 +272,7 @@ public: | |||
| 270 | */ | 272 | */ |
| 271 | bool FlushBuffers(u32& buffers_released) { | 273 | bool FlushBuffers(u32& buffers_released) { |
| 272 | std::scoped_lock l{lock}; | 274 | std::scoped_lock l{lock}; |
| 273 | std::vector<AudioBuffer> buffers_flushed{}; | 275 | boost::container::static_vector<AudioBuffer, N> buffers_flushed{}; |
| 274 | 276 | ||
| 275 | buffers_released = GetRegisteredAppendedBuffers(buffers_flushed, append_limit); | 277 | buffers_released = GetRegisteredAppendedBuffers(buffers_flushed, append_limit); |
| 276 | 278 | ||
diff --git a/src/audio_core/device/device_session.cpp b/src/audio_core/device/device_session.cpp index b5c0ef0e6..86811fcb8 100644 --- a/src/audio_core/device/device_session.cpp +++ b/src/audio_core/device/device_session.cpp | |||
| @@ -79,7 +79,7 @@ void DeviceSession::ClearBuffers() { | |||
| 79 | } | 79 | } |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) const { | 82 | void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) { |
| 83 | for (const auto& buffer : buffers) { | 83 | for (const auto& buffer : buffers) { |
| 84 | Sink::SinkBuffer new_buffer{ | 84 | Sink::SinkBuffer new_buffer{ |
| 85 | .frames = buffer.size / (channel_count * sizeof(s16)), | 85 | .frames = buffer.size / (channel_count * sizeof(s16)), |
| @@ -88,13 +88,13 @@ void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) const { | |||
| 88 | .consumed = false, | 88 | .consumed = false, |
| 89 | }; | 89 | }; |
| 90 | 90 | ||
| 91 | tmp_samples.resize_destructive(buffer.size / sizeof(s16)); | ||
| 91 | if (type == Sink::StreamType::In) { | 92 | if (type == Sink::StreamType::In) { |
| 92 | std::vector<s16> samples{}; | 93 | stream->AppendBuffer(new_buffer, tmp_samples); |
| 93 | stream->AppendBuffer(new_buffer, samples); | ||
| 94 | } else { | 94 | } else { |
| 95 | std::vector<s16> samples(buffer.size / sizeof(s16)); | 95 | system.ApplicationMemory().ReadBlockUnsafe(buffer.samples, tmp_samples.data(), |
| 96 | system.ApplicationMemory().ReadBlockUnsafe(buffer.samples, samples.data(), buffer.size); | 96 | buffer.size); |
| 97 | stream->AppendBuffer(new_buffer, samples); | 97 | stream->AppendBuffer(new_buffer, tmp_samples); |
| 98 | } | 98 | } |
| 99 | } | 99 | } |
| 100 | } | 100 | } |
diff --git a/src/audio_core/device/device_session.h b/src/audio_core/device/device_session.h index 75f766c68..7d52f362d 100644 --- a/src/audio_core/device/device_session.h +++ b/src/audio_core/device/device_session.h | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | #include "audio_core/common/common.h" | 11 | #include "audio_core/common/common.h" |
| 12 | #include "audio_core/sink/sink.h" | 12 | #include "audio_core/sink/sink.h" |
| 13 | #include "common/scratch_buffer.h" | ||
| 13 | #include "core/hle/service/audio/errors.h" | 14 | #include "core/hle/service/audio/errors.h" |
| 14 | 15 | ||
| 15 | namespace Core { | 16 | namespace Core { |
| @@ -62,7 +63,7 @@ public: | |||
| 62 | * | 63 | * |
| 63 | * @param buffers - The buffers to play. | 64 | * @param buffers - The buffers to play. |
| 64 | */ | 65 | */ |
| 65 | void AppendBuffers(std::span<const AudioBuffer> buffers) const; | 66 | void AppendBuffers(std::span<const AudioBuffer> buffers); |
| 66 | 67 | ||
| 67 | /** | 68 | /** |
| 68 | * (Audio In only) Pop samples from the backend, and write them back to this buffer's address. | 69 | * (Audio In only) Pop samples from the backend, and write them back to this buffer's address. |
| @@ -146,8 +147,8 @@ private: | |||
| 146 | std::shared_ptr<Core::Timing::EventType> thread_event; | 147 | std::shared_ptr<Core::Timing::EventType> thread_event; |
| 147 | /// Is this session initialised? | 148 | /// Is this session initialised? |
| 148 | bool initialized{}; | 149 | bool initialized{}; |
| 149 | /// Buffer queue | 150 | /// Temporary sample buffer |
| 150 | std::vector<AudioBuffer> buffer_queue{}; | 151 | Common::ScratchBuffer<s16> tmp_samples{}; |
| 151 | }; | 152 | }; |
| 152 | 153 | ||
| 153 | } // namespace AudioCore | 154 | } // namespace AudioCore |