add async gltf resource loading

This commit is contained in:
Nick Fisher
2025-04-16 17:06:51 +08:00
parent 02b6bc4ee6
commit 2d4342607d
5 changed files with 45 additions and 6 deletions

View File

@@ -787,9 +787,16 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
cb));
}
if (loadResourcesAsync) {
// GltfResourceLoader_asyncBeginLoad(gltfResourceLoader)
throw UnimplementedError(
"TODO"); // need to use a NativeFinalizer to ensure the pointer is still valid until resource loader has finished
if(!GltfResourceLoader_asyncBeginLoad(gltfResourceLoader, filamentAsset)) {
throw Exception("Failed to begin async loading");
}
GltfResourceLoader_asyncUpdateLoad(gltfResourceLoader);
var progress = GltfResourceLoader_asyncGetLoadProgress(gltfResourceLoader);
while(progress < 1.0) {
GltfResourceLoader_asyncUpdateLoad(gltfResourceLoader);
progress = GltfResourceLoader_asyncGetLoadProgress(gltfResourceLoader);
}
} else {
final result = await withBoolCallback((cb) =>
GltfResourceLoader_loadResourcesRenderThread(

View File

@@ -2847,13 +2847,25 @@ external void GltfResourceLoader_destroy(
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TGltfResourceLoader>,
ffi.Bool Function(ffi.Pointer<TGltfResourceLoader>,
ffi.Pointer<TFilamentAsset>)>(isLeaf: true)
external void GltfResourceLoader_asyncBeginLoad(
external bool GltfResourceLoader_asyncBeginLoad(
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TFilamentAsset> tFilamentAsset,
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TGltfResourceLoader>)>(isLeaf: true)
external void GltfResourceLoader_asyncUpdateLoad(
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader
);
@ffi.Native<
ffi.Double Function(ffi.Pointer<TGltfResourceLoader>)>(isLeaf: true)
external double GltfResourceLoader_asyncGetLoadProgress(
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TGltfResourceLoader>, ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Uint8>, ffi.Size)>(isLeaf: true)

View File

@@ -10,7 +10,9 @@ extern "C"
EMSCRIPTEN_KEEPALIVE TGltfResourceLoader *GltfResourceLoader_create(TEngine *tEngine, const char *relativeResourcePath);
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_destroy(TEngine *tEngine, TGltfResourceLoader *tGltfResourceLoader);
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_asyncBeginLoad(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset);
EMSCRIPTEN_KEEPALIVE bool GltfResourceLoader_asyncBeginLoad(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset);
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_asyncUpdateLoad(TGltfResourceLoader *tGltfResourceLoader);
EMSCRIPTEN_KEEPALIVE float GltfResourceLoader_asyncGetLoadProgress(TGltfResourceLoader *tGltfResourceLoader);
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_addResourceData(TGltfResourceLoader *tGltfResourceLoader, const char *uri, uint8_t *data, size_t length);
EMSCRIPTEN_KEEPALIVE bool GltfResourceLoader_loadResources(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset);

View File

@@ -258,6 +258,7 @@ namespace thermion
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_destroyRenderThread(TEngine *tEngine, TGltfResourceLoader *tResourceLoader, void (*callback)());
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_loadResourcesRenderThread(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset, void (*callback)(bool));
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_addResourceDataRenderThread(TGltfResourceLoader *tGltfResourceLoader, const char *uri, uint8_t *data, size_t length, void (*callback)());
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_asyncBeginLoadRenderThread(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset, void (*callback)(bool));
EMSCRIPTEN_KEEPALIVE void GltfAssetLoader_loadRenderThread(
TEngine *tEngine,

View File

@@ -69,6 +69,23 @@ EMSCRIPTEN_KEEPALIVE bool GltfResourceLoader_loadResources(TGltfResourceLoader *
return gltfResourceLoader->loadResources(filamentAsset);
}
EMSCRIPTEN_KEEPALIVE bool GltfResourceLoader_asyncBeginLoad(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset) {
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
auto *filamentAsset = reinterpret_cast<gltfio::FilamentAsset *>(tFilamentAsset);
return gltfResourceLoader->asyncBeginLoad(filamentAsset);
}
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_asyncUpdateLoad(TGltfResourceLoader *tGltfResourceLoader) {
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
gltfResourceLoader->asyncUpdateLoad();
}
EMSCRIPTEN_KEEPALIVE float GltfResourceLoader_asyncGetLoadProgress(TGltfResourceLoader *tGltfResourceLoader) {
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
return gltfResourceLoader->asyncGetLoadProgress();
}
#ifdef __cplusplus
}
}