EM_ASM callbacks

This commit is contained in:
Nick Fisher
2024-05-11 22:40:22 +08:00
parent c0315c3450
commit a5991c15c2

View File

@@ -1,16 +1,3 @@
#include "DartFilamentFFIApi.h"
#include "FilamentViewer.hpp"
#include "Log.hpp"
#include "ThreadPool.hpp"
#include "filament/LightManager.h"
#include <functional>
#include <mutex>
#include <thread>
#include <stdlib.h>
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include <GL/gl.h> #include <GL/gl.h>
@@ -29,11 +16,29 @@ extern "C"
extern EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_WEBGL_CONTEXT_HANDLE flutter_filament_web_create_gl_context(); extern EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_WEBGL_CONTEXT_HANDLE flutter_filament_web_create_gl_context();
} }
#include <pthread.h> #include <pthread.h>
#endif #endif
#include "DartFilamentFFIApi.h"
#include "FilamentViewer.hpp"
#include "Log.hpp"
#include "ThreadPool.hpp"
#include "filament/LightManager.h"
#include <functional>
#include <mutex>
#include <thread>
#include <stdlib.h>
using namespace flutter_filament; using namespace flutter_filament;
using namespace std::chrono_literals; using namespace std::chrono_literals;
void doSomeStuff(void* ptr) {
std::cout << "DOING SOME STUFF ON MAIN THREDA" << std::endl;
}
class RenderLoop class RenderLoop
{ {
public: public:
@@ -102,11 +107,18 @@ public:
std::cout << "Failed to make context current." << std::endl; std::cout << "Failed to make context current." << std::endl;
return (FilamentViewer*)nullptr; return (FilamentViewer*)nullptr;
} }
glClearColor(0.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
emscripten_webgl_commit_frame();
_viewer = new FilamentViewer((void* const) emContext, loader, platform, uberArchivePath); _viewer = new FilamentViewer((void* const) emContext, loader, platform, uberArchivePath);
MAIN_THREAD_EM_ASM({
window.resolveCallback($0, $1);
}, callback, _viewer);
#else #else
_viewer = new FilamentViewer(context, loader, platform, uberArchivePath); _viewer = new FilamentViewer(context, loader, platform, uberArchivePath);
callback(_viewer);
#endif #endif
callback(_viewer);
return _viewer; }); return _viewer; });
auto fut = add_task(lambda); auto fut = add_task(lambda);
} }
@@ -188,6 +200,7 @@ extern "C"
void *const renderCallbackOwner, void *const renderCallbackOwner,
void (*callback)(void *const)) void (*callback)(void *const))
{ {
if (!_rl) if (!_rl)
{ {
_rl = new RenderLoop(); _rl = new RenderLoop();
@@ -212,7 +225,13 @@ extern "C"
[=]() mutable [=]() mutable
{ {
create_swap_chain(viewer, surface, width, height); create_swap_chain(viewer, surface, width, height);
#ifdef __EMSCRIPTEN__
MAIN_THREAD_EM_ASM({
window.resolveCallback($0);
}, onComplete);
#else
onComplete(); onComplete();
#endif
}); });
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
} }
@@ -251,7 +270,14 @@ extern "C"
std::packaged_task<void()> lambda([=]() mutable std::packaged_task<void()> lambda([=]() mutable
{ {
update_viewport_and_camera_projection(viewer, width, height, scaleFactor); update_viewport_and_camera_projection(viewer, width, height, scaleFactor);
onComplete(); }); #ifdef __EMSCRIPTEN__
MAIN_THREAD_EM_ASM({
window.resolveCallback($0);
}, onComplete);
#else
onComplete();
#endif
});
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
} }
@@ -310,7 +336,13 @@ extern "C"
std::packaged_task<EntityId()> lambda([=]() mutable std::packaged_task<EntityId()> lambda([=]() mutable
{ {
auto entity = load_gltf(sceneManager, path, relativeResourcePath); auto entity = load_gltf(sceneManager, path, relativeResourcePath);
callback(entity); #ifdef __EMSCRIPTEN__
MAIN_THREAD_EM_ASM({
window.resolveCallback($0, $1);
}, callback, entity);
#else
callback(entity);
#endif
return entity; }); return entity; });
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
} }
@@ -322,7 +354,13 @@ extern "C"
[=]() mutable [=]() mutable
{ {
auto entity = load_glb(sceneManager, path, numInstances); auto entity = load_glb(sceneManager, path, numInstances);
callback(entity); #ifdef __EMSCRIPTEN__
MAIN_THREAD_EM_ASM({
window.resolveCallback($0, $1);
}, callback, entity);
#else
callback(entity);
#endif
return entity; return entity;
}); });
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
@@ -390,7 +428,14 @@ extern "C"
std::packaged_task<void()> lambda([=] std::packaged_task<void()> lambda([=]
{ {
load_skybox(viewer, skyboxPath); load_skybox(viewer, skyboxPath);
onComplete(); }); #ifdef __EMSCRIPTEN__
MAIN_THREAD_EM_ASM({
window.resolveCallback($0);
}, onComplete);
#else
onComplete();
#endif
});
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
} }