taisei/src/resource/bgm.c
Andrei Alexeyev ae8194ae78
Various fixes & improvements for concurrent loading (#235)
- RESF_UNSAFE is removed.
- Resources that don't have to be finalized on the main thread can load
completely asynchronously.
- A thread waiting for a concurrent task to complete can start executing
that task itself if it hasn't started yet.
- Refactor the resource loading interface, add support for load-time
dependencies.
- Main-thread finalization of asynchronously loaded resources is now
spread out across multiple frames to mitigate frametime spikes.
- Remove some archaisms from the resource management code.
- Fix potential hashtable synchronization issue.
- Fix some deadlock edge cases.
- Don't spawn more worker threads than there are CPU cores (degrades
performance).
- Add TAISEI_AGGRESSIVE_PRELOAD env variable to attempt to aggressively
discover and preload every possible resource.
- Make r_texture_fill{,_region} expect optimal pixmaps, so that it's
never forced to convert them on the main thread. The optimal format may
be queried with the new r_texture_optimal_pixmap_format_for_type API.
These functions will also no longer needlessly copy the entire image
into a staging buffer - previously they did this even if no conversion
was needed.
- Other random changes to facilitate the stuff above.

The overall effect is somewhat faster load times.

Of course it's still all terrible and full of lock contention because I
suck at concurrent programming, but it's not worse than it was.
Probably.
2020-06-09 03:01:53 +03:00

77 lines
1.7 KiB
C

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#include "taisei.h"
#include "resource.h"
#include "bgm.h"
#include "audio/backend.h"
#include "sfxbgm_common.h"
#include "util.h"
static char *bgm_path(const char *name) {
return sfxbgm_make_path(BGM_PATH_PREFIX, name, true);
}
static bool check_bgm_path(const char *path) {
return sfxbgm_check_path(BGM_PATH_PREFIX, path, true);
}
static MusicImpl *load_music(const char *path) {
if(!path) {
return NULL;
}
return _a_backend.funcs.music_load(path);
}
static void load_bgm(ResourceLoadState *st) {
Music *mus = calloc(1, sizeof(Music));
if(strendswith(st->path, ".bgm")) {
mus->meta = get_resource_data(RES_BGM_METADATA, st->name, st->flags);
if(mus->meta) {
mus->impl = load_music(mus->meta->loop_path);
}
} else {
mus->impl = load_music(st->path);
}
if(!mus->impl) {
free(mus);
mus = NULL;
log_error("Failed to load bgm '%s'", st->path);
res_load_failed(st);
} else {
if(mus->meta && mus->meta->loop_point > 0) {
_a_backend.funcs.music_set_loop_point(mus->impl, mus->meta->loop_point);
}
res_load_finished(st, mus);
}
}
static void unload_bgm(void *vmus) {
Music *mus = vmus;
_a_backend.funcs.music_unload(mus->impl);
free(mus);
}
ResourceHandler bgm_res_handler = {
.type = RES_BGM,
.typename = "bgm",
.subdir = BGM_PATH_PREFIX,
.procs = {
.find = bgm_path,
.check = check_bgm_path,
.load = load_bgm,
.unload = unload_bgm,
},
};