add further TEngine methods

This commit is contained in:
Nick Fisher
2025-03-17 16:52:05 +08:00
parent 09b6aca08f
commit 74502ba5b3
2 changed files with 145 additions and 1 deletions

View File

@@ -13,8 +13,20 @@ extern "C"
{
#endif
enum TBackend {
BACKEND_DEFAULT = 0, //!< Automatically selects an appropriate driver for the platform.
BACKEND_OPENGL = 1, //!< Selects the OpenGL/ES driver (default on Android)
BACKEND_VULKAN = 2, //!< Selects the Vulkan driver if the platform supports it (default on Linux/Windows)
BACKEND_METAL = 3, //!< Selects the Metal driver if the platform supports it (default on MacOS/iOS).
BACKEND_NOOP = 4, //!< Selects the no-op driver for testing purposes.
};
EMSCRIPTEN_KEEPALIVE TEngine *Engine_create(TBackend backend);
EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createSwapChain(TEngine *tEngine, void *window, uint64_t flags);
EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createHeadlessSwapChain(TEngine *tEngine, uint32_t width, uint32_t height, uint64_t flags);
EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine* tEngine);
EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine* tEngine, EntityId entityId);
EMSCRIPTEN_KEEPALIVE TTransformManager *Engine_getTransformManager(TEngine *engine);
EMSCRIPTEN_KEEPALIVE TRenderableManager *Engine_getRenderableManager(TEngine *engine);
@@ -29,9 +41,16 @@ EMSCRIPTEN_KEEPALIVE TTexture *Engine_buildTexture(TEngine *engine,
TTextureFormat format);
EMSCRIPTEN_KEEPALIVE void Engine_destroyTexture(TEngine *tEngine, TTexture *tTexture);
EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence);
EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE TMaterial *Engine_buildMaterial(TEngine *tEngine, const uint8_t* materialData, size_t length);
EMSCRIPTEN_KEEPALIVE void Engine_destroyMaterial(TEngine *tEngine, TMaterial *tMaterial);
EMSCRIPTEN_KEEPALIVE TScene *Engine_createScene(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE TSkybox *Engine_buildSkybox(TEngine *tEngine, uint8_t* ktxData, size_t length, void(*onTextureUploadComplete)());
#ifdef __cplusplus
}

View File

@@ -1,13 +1,24 @@
#include "c_api/TEngine.h"
#include <filament/Engine.h>
#include <filament/Fence.h>
#include <filament/Material.h>
#include <filament/Scene.h>
#include <filament/Skybox.h>
#include <filament/Texture.h>
#include <filament/TextureSampler.h>
#include <filament/TransformManager.h>
#include <filament/View.h>
#include <ktxreader/Ktx1Reader.h>
#include <ktxreader/Ktx2Reader.h>
#include <imageio/ImageDecoder.h>
#include <imageio/ImageEncoder.h>
#include <image/ColorTransform.h>
#include <utils/EntityManager.h>
#include "Log.hpp"
#ifdef __cplusplus
@@ -18,6 +29,41 @@ namespace thermion
using namespace filament;
#endif
uint64_t TSWAP_CHAIN_CONFIG_TRANSPARENT = filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT;
uint64_t TSWAP_CHAIN_CONFIG_READABLE = filament::backend::SWAP_CHAIN_CONFIG_READABLE;
uint64_t TSWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER = filament::backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER;
uint64_t TSWAP_CHAIN_CONFIG_HAS_STENCIL_BUFFER = filament::backend::SWAP_CHAIN_CONFIG_HAS_STENCIL_BUFFER;
EMSCRIPTEN_KEEPALIVE TEngine *Engine_create(TBackend backend) {
auto *engine = filament::Engine::create(static_cast<filament::Engine::Backend>(backend));
return reinterpret_cast<TEngine *>(engine);
}
EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *renderer = engine->createRenderer();
return reinterpret_cast<TRenderer *>(renderer);
}
EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createSwapChain(TEngine *tEngine, void *window, uint64_t flags) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *swapChain = engine->createSwapChain(window, flags);
return reinterpret_cast<TSwapChain*>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createHeadlessSwapChain(TEngine *tEngine, uint32_t width, uint32_t height, uint64_t flags) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *swapChain = engine->createSwapChain(width, height, flags);
return reinterpret_cast<TSwapChain*>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *view = engine->createView();
return reinterpret_cast<TView *>(view);
}
EMSCRIPTEN_KEEPALIVE TTransformManager *Engine_getTransformManager(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
@@ -39,6 +85,13 @@ namespace thermion
return reinterpret_cast<TLightManager *>(&lightManager);
}
EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine* tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
utils::Entity entity = utils::EntityManager::get().create();
auto *camera = engine->createCamera(entity);
return reinterpret_cast<TCamera *>(camera);
}
EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine *tEngine, EntityId entityId)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
@@ -258,6 +311,78 @@ namespace thermion
engine->destroy(texture);
}
EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *fence = engine->createFence();
return reinterpret_cast<TFence *>(fence);
}
EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *fence = reinterpret_cast<Fence *>(tFence);
Fence::waitAndDestroy(fence);
}
EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
#ifdef __EMSCRIPTEN__
engine->execute();
emscripten_webgl_commit_frame();
#else
engine->flushAndWait();
#endif
}
EMSCRIPTEN_KEEPALIVE TScene *Engine_createScene(TEngine *tEngine) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *scene = engine->createScene();
return reinterpret_cast<TScene *>(scene);
}
EMSCRIPTEN_KEEPALIVE TSkybox *Engine_buildSkybox(TEngine *tEngine, uint8_t* ktxData, size_t length, void(*onTextureUploadComplete)()) {
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto copy = new std::vector<uint8_t>(ktxData, ktxData + length);
image::Ktx1Bundle *skyboxBundle =
new image::Ktx1Bundle(static_cast<const uint8_t *>(copy->data()),
static_cast<uint32_t>(length));
std::vector<void *> *callbackData = new std::vector<void *>{
reinterpret_cast<void *>(onTextureUploadComplete),
reinterpret_cast<void *>(skyboxBundle),
reinterpret_cast<void *>(copy)
};
auto *texture =
ktxreader::Ktx1Reader::createTexture(
engine, *skyboxBundle, false, [](void *userdata)
{
std::vector<void*>* vec = (std::vector<void*>*)userdata;
void *callbackPtr = vec->at(0);
image::Ktx1Bundle *skyboxBundle = reinterpret_cast<image::Ktx1Bundle *>(vec->at(1));
std::vector<uint8_t> *copy = reinterpret_cast<std::vector<uint8_t>*>(vec->at(2));
delete vec;
if (callbackPtr)
{
Log("NON NULL CLLABCK");
void (*callback)(void) = (void (*)(void))callbackPtr;
callback();
}
delete skyboxBundle;
delete copy;
},
(void*)callbackData);
auto *skybox =
filament::Skybox::Builder()
.environment(texture)
.build(*engine);
return reinterpret_cast<TSkybox *>(skybox);
}
#ifdef __cplusplus
}
}