Skip to content

Commit

Permalink
Web: Load sound files on the main thread. This massively speeds it up.
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Jul 25, 2024
1 parent 2883af8 commit 276cf1a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
69 changes: 46 additions & 23 deletions src/view/viewables/streaming/viewables_streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "application/setups/client/demo_file_meta.h"
#include "augs/misc/scope_guard.h"

#if PLATFORM_WEB && !WEB_SINGLETHREAD
#include "augs/templates/main_thread_queue.h"
#endif

void web_sdk_loading_start();
void web_sdk_loading_stop();

Expand Down Expand Up @@ -315,36 +319,51 @@ void viewables_streaming::load_all(const viewables_load_input in) {

sounds_progress->max_sounds.store(sound_paths_info.size());

future_loaded_buffers = launch_async(
[&](){
web_sdk_loading_start();
auto scoped_stop = augs::scope_guard([]() { web_sdk_loading_stop(); });

using value_type = decltype(future_loaded_buffers.get());
auto buffer_loader = [&](){
web_sdk_loading_start();
auto scoped_stop = augs::scope_guard([]() { web_sdk_loading_stop(); });

value_type result;
using value_type = decltype(future_loaded_buffers.get());

for (const auto& r : sound_requests) {
if (r.second.source_sound.empty()) {
/* A request to unload. */
result.push_back(std::nullopt);
continue;
}
value_type result;

try {
augs::sound_buffer b = r.second;
result.emplace_back(std::move(b));
}
catch (...) {
result.push_back(std::nullopt);
}
for (const auto& r : sound_requests) {
if (r.second.source_sound.empty()) {
/* A request to unload. */
result.push_back(std::nullopt);
continue;
}

sounds_progress->current_sound_num += 1;
try {
augs::sound_buffer b = r.second;
result.emplace_back(std::move(b));
}
catch (...) {
result.push_back(std::nullopt);
}

return result;
sounds_progress->current_sound_num += 1;
}
);

return result;
};

in.audio_buffers.finish();

#if PLATFORM_WEB && !WEB_SINGLETHREAD
using V = decltype(future_loaded_buffers.get());

std::optional<V> result;

main_thread_queue::execute([&]() {
result.emplace(buffer_loader());
});

future_loaded_buffers = launch_async([&result]() { return std::move(*result); });
future_loaded_buffers.wait();
#else
future_loaded_buffers = launch_async(buffer_loader);
#endif

future_sound_definitions = new_all_defs.sounds;
}
Expand Down Expand Up @@ -555,6 +574,10 @@ void viewables_streaming::recompress_demos() {
});
}

bool viewables_streaming::is_loading_sounds() const {
return sounds_progress.has_value();
}

bool viewables_streaming::completed_all_loading() const {
return !general_atlas_progress.has_value() && !sounds_progress.has_value();
}
Expand Down
2 changes: 2 additions & 0 deletions src/view/viewables/streaming/viewables_streaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct viewables_load_input {
const unsigned max_atlas_size;
std::optional<arena_player_metas>& new_player_metas;
std::optional<ad_hoc_atlas_subjects> ad_hoc_subjects;
augs::audio_command_buffers& audio_buffers;
};

struct viewables_finalize_input {
Expand Down Expand Up @@ -149,6 +150,7 @@ class viewables_streaming {
bool general_atlas_in_progress() const;
void display_loading_progress() const;
bool completed_all_loading() const;
bool is_loading_sounds() const;

void request_rescan();
void recompress_demos();
Expand Down
15 changes: 9 additions & 6 deletions src/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,8 @@ work_result work(
renderer_backend.get_max_texture_size(),

new_player_metas,
new_ad_hoc_images
new_ad_hoc_images,
audio_buffers
});
};

Expand Down Expand Up @@ -5226,11 +5227,13 @@ work_result work(

auto audio_renderer = std::optional<augs::audio_renderer>();

if (const auto audio_buffer = audio_buffers.map_write_buffer()) {
audio_renderer.emplace(augs::audio_renderer {
audio_buffers.num_currently_processed_buffers(),
*audio_buffer
});
if (!streaming.is_loading_sounds()) {
if (const auto audio_buffer = audio_buffers.map_write_buffer()) {
audio_renderer.emplace(augs::audio_renderer {
audio_buffers.num_currently_processed_buffers(),
*audio_buffer
});
}
}

advance_current_setup(
Expand Down

0 comments on commit 276cf1a

Please sign in to comment.