Skip to content

Commit

Permalink
Time budget asset loading (#891)
Browse files Browse the repository at this point in the history
* debug: Auto unfreeze trace when tweaking filter

* asset: Add asset-load trace scope

* asset: Limit max asset loads per task to 1

* asset: Run asset loads earlier in the frame

* asset: Add time-budget to asset loading

* debug: Grow the trace trigger popup downward
  • Loading branch information
BastianBlokland committed Jun 15, 2024
1 parent 15e2687 commit 759bc01
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libs/asset/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ add_library(lib_asset STATIC
)
target_include_directories(lib_asset PUBLIC include)
target_link_libraries(lib_asset PUBLIC lib_core lib_data lib_ecs lib_geo lib_script)
target_link_libraries(lib_asset PRIVATE lib_json lib_log)
target_link_libraries(lib_asset PRIVATE lib_json lib_log lib_trace)

add_executable(test_lib_asset
test/config.c
Expand Down
2 changes: 1 addition & 1 deletion libs/asset/include/asset_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ecs_def.h"

enum {
AssetOrder_Update = 0,
AssetOrder_Update = -900,
};

/**
Expand Down
18 changes: 12 additions & 6 deletions libs/asset/src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
#include "core_dynarray.h"
#include "core_path.h"
#include "core_search.h"
#include "core_time.h"
#include "ecs_utils.h"
#include "ecs_world.h"
#include "log_logger.h"
#include "trace_tracer.h"

#include "loader_internal.h"
#include "repo_internal.h"

#define asset_max_loads_per_task 3
#define asset_num_load_tasks 3
#define asset_max_load_time_per_task time_milliseconds(2)
#define asset_num_load_tasks 2
#define asset_id_chunk_size (16 * usize_kibibyte)

/**
Expand Down Expand Up @@ -154,6 +156,7 @@ static bool asset_manager_load(
if (!source) {
return false;
}
trace_begin_msg("asset_manager_load", TraceColor_Blue, "{}", fmt_text(path_filename(asset->id)));

if (manager->flags & AssetManagerFlags_TrackChanges) {
asset_repo_changes_watch(manager->repo, asset->id, (u64)assetEntity);
Expand All @@ -169,6 +172,8 @@ static bool asset_manager_load(

AssetLoader loader = asset_loader(source->format);
loader(world, asset->id, assetEntity, source);

trace_end();
return true;
}

Expand Down Expand Up @@ -224,8 +229,8 @@ ecs_system_define(AssetUpdateDirtySys) {
return;
}

u32 startedLoads = 0;
EcsView* assetsView = ecs_world_view_t(world, DirtyAssetView);
TimeDuration loadTime = 0;
EcsView* assetsView = ecs_world_view_t(world, DirtyAssetView);

for (EcsIterator* itr = ecs_view_itr_step(assetsView, parCount, parIndex); ecs_view_walk(itr);) {
const EcsEntityId entity = ecs_view_entity(itr);
Expand Down Expand Up @@ -264,11 +269,12 @@ ecs_system_define(AssetUpdateDirtySys) {
* Asset ref-count is non-zero; start loading.
* NOTE: Loading can fail to start, for example the asset doesn't exist in the manager's repo.
*/
const bool canLoad = startedLoads < asset_max_loads_per_task;
const bool canLoad = loadTime < asset_max_load_time_per_task;
if (canLoad) {
assetComp->flags |= AssetFlags_Loading;
const TimeSteady loadStart = time_steady_clock();
if (asset_manager_load(world, manager, assetComp, entity)) {
startedLoads++;
loadTime += time_steady_duration(loadStart, time_steady_clock());
} else {
ecs_world_add_empty_t(world, entity, AssetFailedComp);
}
Expand Down
8 changes: 6 additions & 2 deletions libs/debug/src/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void trace_options_trigger_draw(

ui_layout_push(c);
ui_layout_move(c, ui_vector(0.5f, 0.5f), UiBase_Current, Ui_XY);
ui_layout_resize(c, UiAlign_BottomCenter, g_popupSize, UiBase_Absolute, Ui_XY);
ui_layout_resize(c, UiAlign_TopCenter, g_popupSize, UiBase_Absolute, Ui_XY);

// Popup background.
ui_style_push(c);
Expand Down Expand Up @@ -217,7 +217,11 @@ static void trace_options_trigger_draw(
ui_table_next_row(c, &table);
ui_label(c, string_lit("Message"));
ui_table_next_column(c, &table);
ui_textbox(c, &t->msgFilter, .placeholder = string_lit("*"));
if (ui_textbox(c, &t->msgFilter, .placeholder = string_lit("*"))) {
if (t->enabled) {
panel->freeze = false;
}
}

ui_table_next_row(c, &table);
ui_label(c, string_lit("Threshold"));
Expand Down
2 changes: 1 addition & 1 deletion libs/snd/src/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ ecs_system_define(SndMixerUpdateSys) {
* but we do check if the ref-count is zero when accessing the asset. If its zero then its not
* safe to access the asset as it might be queued for unload.
*/
ASSERT((u32)SndOrder_Update > AssetOrder_Update, "Sound update has to be after asset update");
ASSERT((i32)SndOrder_Update > AssetOrder_Update, "Sound update has to be after asset update");
// Fallthrough.
case SndObjectPhase_Acquired:
if (obj->flags & SndObjectFlags_Stop) {
Expand Down

0 comments on commit 759bc01

Please sign in to comment.