initial work to re-implement FFI with background thread render loop

This commit is contained in:
Nick Fisher
2023-09-29 13:54:04 +08:00
parent 1b49706eca
commit a6506e6346
39 changed files with 6819 additions and 53973 deletions

View File

@@ -78,11 +78,13 @@ namespace polyvox {
Renderer* getRenderer();
void setBackgroundColor(const float r, const float g, const float b, const float a);
void setBackgroundImage(const char* resourcePath);
void setBackgroundImage(const char* resourcePath, bool fillHeight);
void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp);
void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled);
void moveCameraToAsset(EntityId entityId);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z);

View File

@@ -6,12 +6,12 @@
typedef int32_t EntityId;
const void* create_filament_viewer(const void* const context, const ResourceLoaderWrapper* const loader);
ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* owner);
void delete_filament_viewer(const void* const viewer);
ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* owner);
void destroy_filament_viewer(const void* const viewer);
void* get_asset_manager(const void* const viewer);
void create_render_target(const void* const viewer, intptr_t textureId, uint32_t width, uint32_t height);
void create_render_target(const void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void clear_background_image(const void* const viewer);
void set_background_image(const void* const viewer, const char *path);
void set_background_image(const void* const viewer, const char *path, bool fillHeight);
void set_background_image_position(const void* const viewer, float x, float y, bool clamp);
void set_background_color(const void* const viewer, const float r, const float g, const float b, const float a);
void set_tone_mapping(const void* const viewer, int toneMapping);
@@ -26,8 +26,9 @@ void clear_lights(const void* const viewer);
EntityId load_glb(void *assetManager, const char *assetPath, bool unlit);
EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath);
bool set_camera(const void* const viewer, EntityId asset, const char *nodeName);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void render(const void* const viewer, uint64_t frameTimeInNanos);
void create_swap_chain(const void* const viewer, const void* const surface, uint32_t width, uint32_t height);
void create_swap_chain(const void* const viewer, const void* const window, uint32_t width, uint32_t height);
void destroy_swap_chain(const void* const viewer);
void set_frame_interval(const void* const viewer, float interval);
void update_viewport_and_camera_projection(const void* const viewer, uint32_t width, uint32_t height, float scaleFactor);
@@ -82,15 +83,12 @@ void get_morph_target_name(void* assetManager, EntityId asset, const char *meshN
int get_morph_target_name_count(void* assetManager, EntityId asset, const char *meshName);
void remove_asset(const void* const viewer, EntityId asset);
void clear_assets(const void* const viewer);
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
void set_texture(void* assetManager, EntityId asset);
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a);
void transform_to_unit_cube(void* assetManager, EntityId asset);
void set_position(void* assetManager, EntityId asset, float x, float y, float z);
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
void set_scale(void* assetManager, EntityId asset, float scale);
void move_camera_to_asset(const void* const viewer, EntityId asset);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity);
void set_camera_position(const void* const viewer, float x, float y, float z);
void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z);

View File

@@ -0,0 +1,83 @@
#ifndef _POLYVOX_FILAMENT_FFI_API_H
#define _POLYVOX_FILAMENT_FFI_API_H
#include "PolyvoxFilamentApi.h"
///
/// This header replicates most of the methods in PolyvoxFilamentApi.h, and is only intended to be used to generate client FFI bindings.
/// The intention is that calling one of these methods will call its respective method in PolyvoxFilamentApi.h, but wrapped in some kind of thread runner to ensure thread safety.
///
typedef int32_t EntityId;
const void* create_filament_viewer_ffi(void* const context, const ResourceLoaderWrapper* const loader);
void destroy_filament_viewer_ffi(void* const viewer);
void render_ffi(void* const viewer);
void set_rendering_ffi(void* const viewer, bool rendering);
void set_frame_interval_ffi(float frameInterval);
void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);
void create_render_target_ffi(void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void clear_background_image_ffi(void* const viewer);
void set_background_image_ffi(void* const viewer, const char *path, bool fillHeight);
void set_background_image_position_ffi(void* const viewer, float x, float y, bool clamp);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void set_tone_mapping_ffi(void* const viewer, int toneMapping);
void set_bloom_ffi(void* const viewer, float strength);
void load_skybox_ffi(void* const viewer, const char *skyboxPath);
void load_ibl_ffi(void* const viewer, const char *iblPath, float intensity);
void remove_skybox_ffi(void* const viewer);
void remove_ibl_ffi(void* const viewer);
EntityId add_light_ffi(void* const viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
void remove_light_ffi(void* const viewer, EntityId entityId);
void clear_lights_ffi(void* const viewer);
EntityId load_glb_ffi(void* const assetManager, const char *assetPath, bool unlit);
EntityId load_gltf_ffi(void* const assetManager, const char *assetPath, const char *relativePath);
void remove_asset_ffi(const void* const viewer, EntityId asset);
void clear_assets_ffi(const void* const viewer);
bool set_camera_ffi(void* const viewer, EntityId asset, const char *nodeName);
void apply_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
float *const weights,
int count
);
void set_morph_target_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
int numWeights
);
bool set_morph_animation_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs);
void set_bone_animation_ffi(
void* assetManager,
EntityId asset,
const float* const frameData,
int numFrames,
int numBones,
const char** const boneNames,
const char** const meshName,
int numMeshTargets,
float frameLengthInMs);
void play_animation_ffi(void* assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade);
void set_animation_frame_ffi(void* assetManager, EntityId asset, int animationIndex, int animationFrame);
void stop_animation_ffi(void* assetManager, EntityId asset, int index);
int get_animation_count_ffi(void* assetManager, EntityId asset);
void get_animation_name_ffi(void* assetManager, EntityId asset, char *const outPtr, int index);
float get_animation_duration_ffi(void* assetManager, EntityId asset, int index);
void get_morph_target_name_ffi(void* assetManager, EntityId asset, const char *meshName, char *const outPtr, int index);
int get_morph_target_name_count_ffi(void* assetManager, EntityId asset, const char *meshName);
#endif // _POLYVOX_FILAMENT_FFI_API_H

View File

@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface PolyvoxFilamentPlugin : NSObject<FlutterPlugin>
@end

View File

@@ -3,7 +3,6 @@
#include <stdint.h>
#if defined(__cplusplus)
#include "Log.hpp"
extern "C" {
#endif
//
@@ -28,6 +27,10 @@ extern "C" {
struct ResourceBuffer {
#if defined(__cplusplus)
ResourceBuffer(const void* const data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
ResourceBuffer(const ResourceBuffer& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer(const ResourceBuffer&& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer& operator=(const ResourceBuffer& other) = delete;
#endif
const void * const data;
const uint32_t size;
@@ -35,41 +38,41 @@ extern "C" {
};
typedef struct ResourceBuffer ResourceBuffer;
typedef ResourceBuffer (*FlutterFilamentLoadResource)(const char* uri);
typedef ResourceBuffer (*LoadResourceFromOwner)(const char* const, void* const owner);
typedef void (*FlutterFilamentFreeResource)(ResourceBuffer);
typedef void (*FreeResourceFromOwner)(ResourceBuffer, void* const owner);
typedef ResourceBuffer (*LoadFilamentResource)(const char* uri);
typedef ResourceBuffer (*LoadFilamentResourceFromOwner)(const char* const, void* const owner);
typedef void (*FreeFilamentResource)(ResourceBuffer);
typedef void (*FreeFilamentResourceFromOwner)(ResourceBuffer, void* const owner);
// this may be compiled as either C or C++, depending on which compiler is being invoked (e.g. binding to Swift will compile as C).
// the former does not allow default initialization to be specified inline), so we need to explicitly set the unused members to nullptr
struct ResourceLoaderWrapper {
#if defined(__cplusplus)
ResourceLoaderWrapper(FlutterFilamentLoadResource loader, FlutterFilamentFreeResource freeResource) : mLoadResource(loader), mFreeResource(freeResource), mLoadResourceFromOwner(nullptr), mFreeResourceFromOwner(nullptr),
ResourceLoaderWrapper(LoadFilamentResource loader, FreeFilamentResource freeResource) : mLoadFilamentResource(loader), mFreeFilamentResource(freeResource), mLoadFilamentResourceFromOwner(nullptr), mFreeFilamentResourceFromOwner(nullptr),
mOwner(nullptr) {}
ResourceLoaderWrapper(LoadResourceFromOwner loader, FreeResourceFromOwner freeResource, void* const owner) : mLoadResource(nullptr), mFreeResource(nullptr), mLoadResourceFromOwner(loader), mFreeResourceFromOwner(freeResource), mOwner(owner) {
ResourceLoaderWrapper(LoadFilamentResourceFromOwner loader, FreeFilamentResourceFromOwner freeResource, void* const owner) : mLoadFilamentResource(nullptr), mFreeFilamentResource(nullptr), mLoadFilamentResourceFromOwner(loader), mFreeFilamentResourceFromOwner(freeResource), mOwner(owner) {
};
ResourceBuffer load(const char* uri) const {
if(mLoadResourceFromOwner) {
return mLoadResourceFromOwner(uri, mOwner);
if(mLoadFilamentResourceFromOwner) {
return mLoadFilamentResourceFromOwner(uri, mOwner);
}
return mLoadResource(uri);
return mLoadFilamentResource(uri);
}
void free(ResourceBuffer rb) const {
if(mFreeResourceFromOwner) {
mFreeResourceFromOwner(rb, mOwner);
if(mFreeFilamentResourceFromOwner) {
mFreeFilamentResourceFromOwner(rb, mOwner);
} else {
mFreeResource(rb);
mFreeFilamentResource(rb);
}
}
#endif
FlutterFilamentLoadResource mLoadResource;
FlutterFilamentFreeResource mFreeResource;
LoadResourceFromOwner mLoadResourceFromOwner;
FreeResourceFromOwner mFreeResourceFromOwner;
LoadFilamentResource mLoadFilamentResource;
FreeFilamentResource mFreeFilamentResource;
LoadFilamentResourceFromOwner mLoadFilamentResourceFromOwner;
FreeFilamentResourceFromOwner mFreeFilamentResourceFromOwner;
void* mOwner;
};
typedef struct ResourceLoaderWrapper ResourceLoaderWrapper;

View File

@@ -1,53 +0,0 @@
#ifndef UNLIT_MATERIAL_PROVIDER
#define UNLIT_MATERIAL_PROVIDER
#include "material/standard.h"
namespace polyvox {
class StandardMaterialProvider : public MaterialProvider {
const Material* _m;
const Material* _ms[1];
const Engine* _engine;
public:
StandardMaterialProvider(Engine* engine) {
_engine = engine;
_m = Material::Builder()
.package( STANDARD_STANDARD_DATA, STANDARD_STANDARD_SIZE)
.build(*engine);
_ms[0] = _m;
}
filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap,
const char* label = "material", const char* extras = nullptr) {
MaterialInstance* d = (MaterialInstance*)_m->getDefaultInstance();
return d;
}
/**
* Gets a weak reference to the array of cached materials.
*/
const filament::Material* const* getMaterials() const noexcept {
return _ms;
}
/**
* Gets the number of cached materials.
*/
size_t getMaterialsCount() const noexcept {
return (size_t)1;
}
void destroyMaterials() {
// TODO - do we need to do anything here?
}
bool needsDummyData(filament::VertexAttribute attrib) const noexcept {
return false;
}
};
}
#endif

View File

@@ -1,12 +0,0 @@
.global STANDARD_STANDARD_OFFSET;
.global STANDARD_STANDARD_SIZE;
.global STANDARD_PACKAGE
.section .rodata
STANDARD_PACKAGE:
.incbin "standard.bin"
STANDARD_STANDARD_OFFSET:
.int 0
STANDARD_STANDARD_SIZE:
.int 1031374

View File

@@ -1,12 +0,0 @@
.global _STANDARD_STANDARD_OFFSET;
.global _STANDARD_STANDARD_SIZE;
.global _STANDARD_PACKAGE
.section __TEXT,__const
_STANDARD_PACKAGE:
.incbin "standard.bin"
_STANDARD_STANDARD_OFFSET:
.int 0
_STANDARD_STANDARD_SIZE:
.int 1031374

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +0,0 @@
#ifndef STANDARD_H_
#define STANDARD_H_
#include <stdint.h>
extern "C" {
extern const uint8_t STANDARD_PACKAGE[];
extern int STANDARD_STANDARD_OFFSET;
extern int STANDARD_STANDARD_SIZE;
}
#define STANDARD_STANDARD_DATA (STANDARD_PACKAGE + STANDARD_STANDARD_OFFSET)
#endif