iOS fixes

This commit is contained in:
Nick Fisher
2023-04-20 22:15:50 +08:00
parent 9dc30f6eff
commit 80a781a3c4
19 changed files with 154 additions and 138 deletions

View File

@@ -8,8 +8,11 @@
#import "polyvox_filament-Swift.h"
#endif
#include "PolyvoxFilamentApi.h"
@implementation PolyvoxFilamentPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftPolyvoxFilamentPlugin registerWithRegistrar:registrar];
ios_dummy();
}
@end

View File

@@ -5,11 +5,13 @@ import GLKit
public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture {
var registrar : FlutterPluginRegistrar
var textureId: Int64?
var flutterTextureId: Int64?
var registry: FlutterTextureRegistry
var pixelBuffer: CVPixelBuffer?;
var createdAt = Date()
var pixelBufferAttrs = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32BGRA),
kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanTrue,
@@ -27,11 +29,11 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
var freeResourcePtr: UnsafeMutableRawPointer? = nil
var loadResource : @convention(c) (UnsafeRawPointer, UnsafeMutableRawPointer) -> ResourceBuffer = { uri, resourcesPtr in
var loadResource : @convention(c) (UnsafePointer<Int8>?, UnsafeMutableRawPointer?) -> ResourceBuffer = { uri, resourcesPtr in
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr).takeUnretainedValue()
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue()
let uriString = String(cString:uri.assumingMemoryBound(to: UInt8.self))
let uriString = String(cString:uri!)
var path:String? = nil
@@ -99,7 +101,13 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
return ResourceBuffer()
}
} else {
// TODO
let key = instance.registrar.lookupKey(forAsset:uriString)
path = Bundle.main.path(forResource: key, ofType:nil)
print("Found path \(path) for uri \(uriString)")
guard path != nil else {
print("File not present in bundle : \(uri)")
return ResourceBuffer()
}
}
}
do {
@@ -116,9 +124,13 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
return ResourceBuffer()
}
var freeResource : @convention(c) (UInt32,UnsafeMutableRawPointer) -> () = { rid, resourcesPtr in
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr).takeUnretainedValue()
instance.resources.removeObject(forKey:rid)
var freeResource : @convention(c) (ResourceBuffer,UnsafeMutableRawPointer?) -> () = { rbuf, resourcesPtr in
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue()
instance.resources.removeObject(forKey:rbuf.id)
}
@objc func doRender() {
}
func createDisplayLink() {
@@ -135,7 +147,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
return Unmanaged.passRetained(pixelBuffer!);
}
public func onTextureUnregistered(texture:FlutterTexture) {
public func onTextureUnregistered(_ texture:FlutterTexture) {
print("Texture unregistered")
}
@@ -157,37 +169,45 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
kCVPixelFormatType_32BGRA, pixelBufferAttrs, &pixelBuffer) != kCVReturnSuccess) {
print("Error allocating pixel buffer")
}
self.textureId = self.registry.register(self)
self.flutterTextureId = self.registry.register(self)
}
private func resize(width:Int32, height:Int32) {
if(self.textureId != nil) {
self.registry.unregisterTexture(self.textureId!)
if(self.flutterTextureId != nil) {
self.registry.unregisterTexture(self.flutterTextureId!)
}
createPixelBuffer(width: Int(width), height:Int(height))
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let methodName = call.method;
print(methodName)
switch methodName {
case "createTexture":
let args = call.arguments as! Array<Int32>
createPixelBuffer(width:args[0], height:args[1])
createPixelBuffer(width:Int(args[0]), height:Int(args[1]))
createDisplayLink()
result(unsafeBitCast(pixelBuffer!, to: UnsafeMutableRawPointer.self))
case "getLoadResourceFn":
result(unsafeBitCast(loadResource, to: UnsafeMutableRawPointer.self))
result(self.flutterTextureId)
// print("texture id \(pixelBufferflutterTextureId)")
case "getLoadResourceFn":
let callback = make_resource_loader(loadResource, freeResource, Unmanaged.passUnretained(self).toOpaque())
result(unsafeBitCast(callback, to:Int64.self))
case "getFreeResourceFn":
result(unsafeBitCast(freeResource, to: UnsafeMutableRawPointer.self))
result(unsafeBitCast(freeResource, to:Int64.self))
case "getGlTextureId":
result(Unmanaged.passUnretained(self).toOpaque())
result(FlutterMethodNotImplemented)
case "getSurface":
var pixelBufferTextureId = Int64(Int(bitPattern:unsafeBitCast(pixelBuffer!, to: UnsafeMutableRawPointer.self)))
result(pixelBufferTextureId)
case "getContext":
result(nil)
result(0) //nullptr
case "resize":
result(self.textureId);
result(self.flutterTextureId);
case "tick":
self.registry.textureFrameAvailable(textureId)
self.registry.textureFrameAvailable(flutterTextureId!)
result(true)
default:
result(FlutterMethodNotImplemented)

View File

@@ -6,7 +6,6 @@
#include <gltfio/FilamentAsset.h>
#include <gltfio/ResourceLoader.h>
#include "ResourceManagement.hpp"
#include "SceneAsset.hpp"
#include "ResourceBuffer.hpp"
@@ -18,8 +17,7 @@ namespace polyvox {
class AssetManager {
public:
AssetManager(LoadResource loadResource,
FreeResource freeResource,
AssetManager(ResourceLoaderWrapper* loader,
NameComponentManager *ncm,
Engine *engine,
Scene *scene);
@@ -64,10 +62,8 @@ namespace polyvox {
void setAnimationFrame(EntityId entity, int animationIndex, int animationFrame);
private:
LoadResource _loadResource;
FreeResource _freeResource;
AssetLoader* _assetLoader = nullptr;
ResourceLoader* _resourceLoader = nullptr;
ResourceLoaderWrapper* _resourceLoaderWrapper;
NameComponentManager* _ncm = nullptr;
Engine* _engine;
Scene* _scene;

View File

@@ -32,7 +32,6 @@
#include <chrono>
#include "AssetManager.hpp"
#include "ResourceManagement.hpp"
using namespace std;
using namespace filament;
@@ -46,7 +45,7 @@ namespace polyvox {
class FilamentViewer {
public:
// FilamentViewer(void* layer, LoadResource loadResource, FreeResource freeResource);
FilamentViewer(void* context, LoadResource loadResource, FreeResource freeResource);
FilamentViewer(void* context, ResourceLoaderWrapper* resourceLoaderWrapper);
~FilamentViewer();
void loadSkybox(const char* const skyboxUri);
@@ -107,8 +106,7 @@ namespace polyvox {
math::mat4f _cameraPosition;
math::mat4f _cameraRotation;
LoadResource _loadResource;
FreeResource _freeResource;
ResourceLoaderWrapper* _resourceLoaderWrapper;
Scene* _scene;
View* _view;

View File

@@ -5,12 +5,10 @@
#include <stddef.h>
typedef struct ResourceBuffer ResourceBuffer;
typedef int32_t EntityId;
intptr_t init_dart_api_dl(void* data);
void* create_filament_viewer(void *context, ResourceBuffer (*loadResource)(const char *), void (*freeResource)(uint32_t));
void* create_filament_viewer(void *context, ResourceLoaderWrapper* loader);
ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* owner);
void delete_filament_viewer(void *viewer);
void* get_asset_manager(void* viewer);
void create_render_target(void *viewer, uint32_t textureId, uint32_t width, uint32_t height);
@@ -103,5 +101,6 @@ void set_camera_rotation(void *viewer, float rads, float x, float y, float z);
void set_camera_model_matrix(void *viewer, const float *const matrix);
void set_camera_focal_length(void *viewer, float focalLength);
void set_camera_focus_distance(void *viewer, float focusDistance);
void ios_dummy();
#endif

View File

@@ -1,2 +0,0 @@
void* create_filament_viewer_ios(void* texture, void* loadResource, void* freeResource, void* resources);

View File

@@ -3,6 +3,7 @@
#include <stdint.h>
#if defined(__cplusplus)
#include "Log.hpp"
extern "C" {
#endif
//
@@ -38,6 +39,43 @@ extern "C" {
uint32_t size;
uint32_t id;
};
typedef struct ResourceBuffer ResourceBuffer;
typedef ResourceBuffer (*LoadResource)(const char* uri);
typedef ResourceBuffer (*LoadResourceFromOwner)(const char* const, void* const owner);
typedef void (*FreeResource)(ResourceBuffer);
typedef void (*FreeResourceFromOwner)(ResourceBuffer, void* const owner);
struct ResourceLoaderWrapper {
#if defined(__cplusplus)
ResourceLoaderWrapper(LoadResource loader, FreeResource freeResource) : mLoadResource(loader), mFreeResource(freeResource) {};
ResourceLoaderWrapper(LoadResourceFromOwner loader, FreeResourceFromOwner freeResource, void* const owner) : mLoadResourceFromOwner(loader), mFreeResourceFromOwner(freeResource), mOwner(owner) {};
ResourceBuffer load(const char* uri) {
Log("LOADING %s", uri);
if(mLoadResourceFromOwner) {
return mLoadResourceFromOwner(uri, mOwner);
}
return mLoadResource(uri);
}
void free(ResourceBuffer rb) {
if(mFreeResourceFromOwner) {
mFreeResourceFromOwner(rb, mOwner);
} else {
mFreeResource(rb);
}
}
#endif
void* mOwner;
LoadResource mLoadResource;
FreeResource mFreeResource;
LoadResourceFromOwner mLoadResourceFromOwner;
FreeResourceFromOwner mFreeResourceFromOwner;
};
typedef struct ResourceLoaderWrapper ResourceLoaderWrapper;
#if defined(__cplusplus)
}
#endif

View File

@@ -1,27 +0,0 @@
#pragma once
#include <functional>
#include <memory>
#include <chrono>
#include <iostream>
#include <vector>
#include "ResourceBuffer.hpp"
namespace polyvox {
using namespace std;
//
// Typedef for a function that loads a resource into a ResourceBuffer from an asset URI.
//
using LoadResource = function<ResourceBuffer(const char* uri)>;
//
// Typedef for a function that frees an ID associated with a ResourceBuffer.
//
using FreeResource = function<void (uint32_t)>;
}

View File

@@ -18,8 +18,6 @@
#include <gltfio/ResourceLoader.h>
#include <utils/NameComponentManager.h>
#include "ResourceManagement.hpp"
extern "C" {
#include "PolyvoxFilamentApi.h"
}

View File

@@ -1,7 +1,6 @@
#ifndef SwiftPolyvoxFilamentPlugin_Bridging_Header_h
#define SwiftPolyvoxFilamentPlugin_Bridging_Header_h
#import "PolyvoxFilamentIOSApi.h"
#import "PolyvoxFilamentApi.h"
#endif

3
ios/lib/libimageio.a Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:08902f0dbd7ec01e30d31243a58f9707b31c69b8e7b658a3a3c160e4fe2a1f69
size 372376

3
ios/lib/libpng.a Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6f40a337ca4ae110bbfee332e03ee496b0f3706b6b9d264a44f3c248733f4f23
size 376488

3
ios/lib/libtinyexr.a Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:60e5e22f31b84e116475a8232310b3a452134587e81a2d0f9bf5c1a0af834f06
size 511480

View File

@@ -14,7 +14,7 @@ A new flutter plugin project.
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/*', 'src/*', 'src/ios/*', 'src/shaders/*.c', 'src/shaders/*.h', 'include/filament/*', 'include/*', 'include/material/*.c'
s.public_header_files = 'include/SwiftPolyvoxFilamentPlugin-Bridging-Header.h', 'include/PolyvoxFilamentIOSApi.h', 'include/PolyvoxFilamentApi.h', 'include/ResourceBuffer.hpp' #, 'include/filament/*'
s.public_header_files = 'include/SwiftPolyvoxFilamentPlugin-Bridging-Header.h', 'include/PolyvoxFilamentApi.h', 'include/ResourceBuffer.hpp' #, 'include/filament/*'
# s.header_mappings_dir = 'include'
s.dependency 'Flutter'
s.platform = :ios, '12.1'
@@ -28,7 +28,7 @@ A new flutter plugin project.
'OTHER_CFLAGS' => '"-fvisibility=default" "$(inherited)"',
'USER_HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/include" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/image" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/shaders" "$(inherited)"',
'ALWAYS_SEARCH_USER_PATHS' => 'YES',
"OTHER_LDFLAGS" => '-lfilament -lbackend -lfilameshio -lviewer -lfilamat -lgeometry -lutils -lfilabridge -lgltfio_core -lfilament-iblprefilter -limage -lcamutils -lgltfio_core -lfilaflat -ldracodec -libl -lktxreader -limageio -lpng -lpng16 -ltinyexr -lz -lstb -luberzlib -lsmol-v -luberarchive -lzstd -lstdc++',
"OTHER_LDFLAGS" => '-lfilament -lbackend -lfilameshio -lviewer -lfilamat -lgeometry -lutils -lfilabridge -lgltfio_core -lfilament-iblprefilter -limage -limageio -ltinyexr -lcamutils -lgltfio_core -lfilaflat -ldracodec -libl -lktxreader -lpng -lpng16 -lz -lstb -luberzlib -lsmol-v -luberarchive -lzstd -lstdc++',
'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/lib" "$(inherited)"',
}
@@ -40,7 +40,7 @@ A new flutter plugin project.
'OTHER_CFLAGS' => '"-fvisibility=default" "$(inherited)"',
'USER_HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/include" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/image" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/shaders" "$(inherited)"',
'ALWAYS_SEARCH_USER_PATHS' => 'YES',
"OTHER_LDFLAGS" => '-lfilament -lbackend -lfilameshio -lviewer -lfilamat -lgeometry -lutils -lfilabridge -lgltfio_core -lfilament-iblprefilter -limage -lcamutils -lgltfio_core -lfilaflat -ldracodec -libl -lktxreader -limageio -lpng -lpng16 -ltinyexr -lz -lstb -luberzlib -lsmol-v -luberarchive -lzstd -lstdc++',
"OTHER_LDFLAGS" => '-lfilament -lbackend -lfilameshio -lviewer -lfilamat -lgeometry -lutils -lfilabridge -lgltfio_core -lfilament-iblprefilter -limage -limageio -ltinyexr -lcamutils -lgltfio_core -lfilaflat -ldracodec -libl -lktxreader -lpng -lpng16 -lz -lstb -luberzlib -lsmol-v -luberarchive -lzstd -lstdc++',
'LIBRARY_SEARCH_PATHS' => '"${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/lib" "$(inherited)"',
}

View File

@@ -17,7 +17,7 @@
#include "StreamBufferAdapter.hpp"
#include "SceneAsset.hpp"
#include "Log.hpp"
#include "ResourceManagement.hpp"
#include "material/UnlitMaterialProvider.hpp"
#include "material/FileMaterialProvider.hpp"
#include "gltfio/materials/uberarchive.h"
@@ -36,13 +36,11 @@ using namespace utils;
using namespace filament;
using namespace filament::gltfio;
AssetManager::AssetManager(LoadResource loadResource,
FreeResource freeResource,
AssetManager::AssetManager(ResourceLoaderWrapper* resourceLoaderWrapper,
NameComponentManager *ncm,
Engine *engine,
Scene *scene)
: _loadResource(loadResource),
_freeResource(freeResource),
: _resourceLoaderWrapper(resourceLoaderWrapper),
_ncm(ncm),
_engine(engine),
_scene(scene) {
@@ -72,7 +70,7 @@ AssetManager::~AssetManager() {
EntityId AssetManager::loadGltf(const char *uri,
const char *relativeResourcePath) {
ResourceBuffer rbuf = _loadResource(uri);
ResourceBuffer rbuf = _resourceLoaderWrapper->load(uri);
// Parse the glTF file and create Filament entities.
FilamentAsset *asset =
@@ -89,11 +87,11 @@ EntityId AssetManager::loadGltf(const char *uri,
for (size_t i = 0; i < resourceUriCount; i++) {
string uri =
string(relativeResourcePath) + string("/") + string(resourceUris[i]);
ResourceBuffer buf = _loadResource(uri.c_str());
ResourceBuffer buf = _resourceLoaderWrapper->load(uri.c_str());
ResourceLoader::BufferDescriptor b(buf.data, buf.size);
_gltfResourceLoader->addResourceData(resourceUris[i], std::move(b));
_freeResource(buf.id);
_resourceLoaderWrapper->free(buf);
}
_gltfResourceLoader->loadResources(asset);
@@ -130,7 +128,7 @@ EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
Log("Loading GLB at URI %s", uri);
ResourceBuffer rbuf = _loadResource(uri);
ResourceBuffer rbuf = _resourceLoaderWrapper->load(uri);
FilamentAsset *asset = _assetLoader->createAsset(
(const uint8_t *)rbuf.data, rbuf.size);
@@ -172,7 +170,7 @@ EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
asset->releaseSourceData();
_freeResource(rbuf.id);
_resourceLoaderWrapper->free(rbuf);
SceneAsset sceneAsset(asset);
@@ -567,7 +565,7 @@ void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int re
asset.mTexture = nullptr;
}
ResourceBuffer imageResource = _loadResource(rp.c_str());
ResourceBuffer imageResource = _resourceLoaderWrapper->load(rp.c_str());
StreamBufferAdapter sb((char *)imageResource.data, (char *)imageResource.data + imageResource.size);
@@ -579,7 +577,7 @@ void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int re
if (!image->isValid()) {
Log("Invalid image : %s", rp.c_str());
delete inputStream;
_freeResource(imageResource.id);
_resourceLoaderWrapper->free(imageResource);
return;
}
@@ -615,7 +613,7 @@ void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int re
inst[0]->setParameter("baseColorMap",asset.mTexture,sampler);
delete inputStream;
_freeResource(imageResource.id);
_resourceLoaderWrapper->free(imageResource);
}

View File

@@ -107,9 +107,8 @@ static constexpr float4 sFullScreenTriangleVertices[3] = {
static const uint16_t sFullScreenTriangleIndices[3] = {0, 1, 2};
FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
FreeResource freeResource)
: _loadResource(loadResource), _freeResource(freeResource) {
FilamentViewer::FilamentViewer(void* context, ResourceLoaderWrapper* resourceLoaderWrapper)
: _resourceLoaderWrapper(resourceLoaderWrapper) {
#if TARGET_OS_IPHONE
_engine = Engine::create(Engine::Backend::METAL);
@@ -179,7 +178,7 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
_view->setAntiAliasing(AntiAliasing::NONE);
// auto materialRb = _loadResource("file:///mnt/hdd_2tb/home/hydroxide/projects/filament/unlit.filamat");
// auto materialRb = _resourceLoader->load("file:///mnt/hdd_2tb/home/hydroxide/projects/filament/unlit.filamat");
// Log("Loaded resource of size %d", materialRb.size);
// _materialProvider = new FileMaterialProvider(_engine, (void*) materialRb.data, (size_t)materialRb.size);
@@ -188,8 +187,7 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
_ncm = new NameComponentManager(em);
_assetManager = new AssetManager(
_loadResource,
_freeResource,
_resourceLoaderWrapper,
_ncm,
_engine,
_scene);
@@ -371,7 +369,7 @@ void FilamentViewer::loadTextureFromPath(string path) {
return;
}
ResourceBuffer rb = _loadResource(path.c_str());
ResourceBuffer rb = _resourceLoaderWrapper->load(path.c_str());
if(endsWith(path, ktxExt)) {
loadKtxTexture(path, rb);
@@ -381,7 +379,7 @@ void FilamentViewer::loadTextureFromPath(string path) {
loadPngTexture(path, rb);
}
_freeResource(rb.id);
_resourceLoaderWrapper->free(rb);
}
@@ -697,7 +695,7 @@ void FilamentViewer::loadSkybox(const char *const skyboxPath) {
removeSkybox();
if (skyboxPath) {
ResourceBuffer skyboxBuffer = _loadResource(skyboxPath);
ResourceBuffer skyboxBuffer = _resourceLoaderWrapper->load(skyboxPath);
if(skyboxBuffer.size <= 0) {
Log("Could not load skybox resource.");
@@ -717,7 +715,7 @@ void FilamentViewer::loadSkybox(const char *const skyboxPath) {
filament::Skybox::Builder().environment(_skyboxTexture).build(*_engine);
_scene->setSkybox(_skybox);
_freeResource(skyboxBuffer.id);
_resourceLoaderWrapper->free(skyboxBuffer);
}
}
@@ -749,7 +747,7 @@ void FilamentViewer::loadIbl(const char *const iblPath, float intensity) {
Log("Loading IBL from %s", iblPath);
// Load IBL.
ResourceBuffer iblBuffer = _loadResource(iblPath);
ResourceBuffer iblBuffer = _resourceLoaderWrapper->load(iblPath);
if(iblBuffer.size == 0) {
Log("Error loading IBL, resource could not be loaded.");
@@ -773,7 +771,7 @@ void FilamentViewer::loadIbl(const char *const iblPath, float intensity) {
.build(*_engine);
_scene->setIndirectLight(_indirectLight);
_freeResource(iblBuffer.id);
_resourceLoaderWrapper->free(iblBuffer);
Log("Skybox/IBL load complete.");
}

View File

@@ -4,30 +4,40 @@
#include "filament/LightManager.h"
#include "Log.hpp"
#include "ThreadPool.hpp"
#include <thread>
#include <functional>
using namespace polyvox;
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
// static ThreadPool* _tp;
extern "C" {
#include "PolyvoxFilamentApi.h"
FLUTTER_PLUGIN_EXPORT void* create_filament_viewer(void* context, ResourceBuffer (*loadResource)(char const*), void (*freeResource)(unsigned int)) {
FLUTTER_PLUGIN_EXPORT void* create_filament_viewer(void* context, ResourceLoaderWrapper* loader) {
// if(!_tp) {
// _tp = new ThreadPool();
// }
// //std::packaged_task<void*()> lambda([=]() mutable {
return (void*) new FilamentViewer(context, loadResource, freeResource);
return (void*) new FilamentViewer(context, loader);
// });
// auto fut = _tp->add_task(lambda);
// fut.wait();
// //return fut.get();
}
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* const owner) {
return new ResourceLoaderWrapper(loadFn, freeFn, owner);
// ResourceLoaderWrapper* lod(loadFn, freeFn, owner);
// return &lod;
}
FLUTTER_PLUGIN_EXPORT void create_render_target(void* viewer, uint32_t textureId, uint32_t width, uint32_t height) {
// //std::packaged_task<void()> lambda([=]() mutable {
((FilamentViewer*)viewer)->createRenderTarget(textureId, width, height);
@@ -535,5 +545,8 @@ extern "C" {
// auto fut = _tp->add_task(lambda);
// fut.wait();
}
FLUTTER_PLUGIN_EXPORT void ios_dummy() {
Log("Dummy called");
}
}

View File

@@ -1,29 +0,0 @@
#include "PolyvoxFilamentApi.h"
#include "FilamentViewer.hpp"
#include "ResourceBuffer.hpp"
#include "ResourceManagement.hpp"
#include <functional>
using namespace polyvox;
using namespace std;
extern "C" {
using RawLoadType = ResourceBuffer(const char*, void* resource);
using RawFreeType = void(uint32_t, void*);
void* create_filament_viewer_ios(void* pb, void* loadResource, void* freeResource, void* resources) {
FreeResource _freeResource = [=](uint32_t rid) {
reinterpret_cast<RawFreeType*>(freeResource)(rid, resources);
};
function<ResourceBuffer(const char*)> _loadResource = [=](const char* uri){
auto cast = reinterpret_cast<RawLoadType*>(loadResource);
return cast(uri, resources);
};
auto viewer = new FilamentViewer(pb,_loadResource, _freeResource);
return (void*)viewer;
}
}

View File

@@ -71,7 +71,6 @@ class FilamentController {
Future setRendering(bool render) async {
_rendering = render;
_channel.invokeMethod("setRendering", render);
}
void render() {
@@ -91,6 +90,7 @@ class FilamentController {
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
_textureIdController.add(_textureId);
print("Got texture id $_textureId");
var glContext =
Pointer<Void>.fromAddress(await _channel.invokeMethod("getContext"));
@@ -103,25 +103,30 @@ class FilamentController {
await _channel.invokeMethod("getFreeResourceFn"));
_viewer = _nativeLibrary.create_filament_viewer(
glContext, loadResource, freeResource);
// don't pass a surface to the SwapChain as we are effectively creating a headless SwapChain that will render into a RenderTarget associated with a texture
_nativeLibrary.create_swap_chain(
_viewer, nullptr, size.width.toInt(), size.height.toInt());
if (Platform.isLinux) {
// don't pass a surface to the SwapChain as we are effectively creating a headless SwapChain that will render into a RenderTarget associated with a texture
_nativeLibrary.create_swap_chain(
_viewer, nullptr, size.width.toInt(), size.height.toInt());
var glTextureId = await _channel.invokeMethod("getGlTextureId");
var glTextureId = await _channel.invokeMethod("getGlTextureId");
_nativeLibrary.create_render_target(
_viewer, glTextureId, size.width.toInt(), size.height.toInt());
} else {
var surface =
Pointer<Void>.fromAddress(await _channel.invokeMethod("getSurface"));
_nativeLibrary.create_swap_chain(
_viewer, surface, size.width.toInt(), size.height.toInt());
}
_nativeLibrary.create_render_target(
_viewer, glTextureId, size.width.toInt(), size.height.toInt());
_nativeLibrary.update_viewport_and_camera_projection(
_viewer, size.width.toInt(), size.height.toInt(), 1.0);
_initialized.complete(true);
_assetManager = _nativeLibrary.get_asset_manager(_viewer);
// await _channel.invokeMethod("setRenderTicker", _viewer.address);
_ticker = _tickerProvider.createTicker((elapsed) async {
_nativeLibrary.render(_viewer, 0);
await _channel.invokeMethod("tick");
});
_ticker!.start();
}