run all Android work on separate thread, add HotReloadPathHelper, separate loadTexture/setTexture

This commit is contained in:
Nick Fisher
2022-08-25 17:09:54 +10:00
parent 051085f440
commit f4c1f59399
12 changed files with 392 additions and 227 deletions

View File

@@ -111,7 +111,7 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource,
.presentationDeadlineNanos = (uint64_t)0,
.vsyncOffsetNanos = (uint64_t)0});
Renderer::FrameRateOptions fro;
fro.interval = 30;
fro.interval = 60;
_renderer->setFrameRateOptions(fro);
_scene = _engine->createScene();
@@ -119,6 +119,7 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource,
Log("Scene created");
Entity camera = EntityManager::get().create();
_mainCamera = _engine->createCamera(camera);
Log("Main camera created");
_view = _engine->createView();
@@ -185,14 +186,15 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource,
_scene->addEntity(_sun);
Log("Added sun");
_sceneAssetLoader = new SceneAssetLoader(_loadResource,
_freeResource,
_assetLoader,
_resourceLoader,
_ncm,
_engine,
_scene);
_scene,
tp);
}
static constexpr float4 sFullScreenTriangleVertices[3] = {
@@ -336,23 +338,20 @@ void FilamentViewer::setBackgroundImage(const char *resourcePath) {
FilamentViewer::~FilamentViewer() {
clearAssets();
_resourceLoader->asyncCancelLoad();
Log("c1");
_materialProvider->destroyMaterials();
Log("c2");
AssetLoader::destroy(&_assetLoader);
Log("c3");
Log("Deleting SceneAssetLoader");
delete _sceneAssetLoader;
_resourceLoader->asyncCancelLoad();
_materialProvider->destroyMaterials();
AssetLoader::destroy(&_assetLoader);
_engine->destroy(_sun);
_engine->destroyCameraComponent(_mainCamera->getEntity());
_mainCamera = nullptr;
_engine->destroy(_view);
Log("c4");
_engine->destroy(_scene);
Log("c5");
_engine->destroy(_renderer);
Log("c6");
_engine->destroy(_swapChain);
Log("c7");
Engine::destroy(&_engine); // clears engine*
Log("c8");
}
Renderer *FilamentViewer::getRenderer() { return _renderer; }
@@ -401,8 +400,11 @@ SceneAsset *FilamentViewer::loadGltf(const char *const uri,
void FilamentViewer::clearAssets() {
Log("Clearing all assets");
mtx.lock();
_view->setCamera(_mainCamera);
// mtx.lock();
if(_mainCamera) {
_view->setCamera(_mainCamera);
}
int i = 0;
for (auto asset : _assets) {
_sceneAssetLoader->remove(asset);
@@ -410,7 +412,8 @@ void FilamentViewer::clearAssets() {
i++;
}
_assets.clear();
mtx.unlock();
// mtx.unlock();
Log("Cleared all assets");
}
void FilamentViewer::removeAsset(SceneAsset *asset) {
@@ -555,7 +558,7 @@ void FilamentViewer::render() {
return;
}
mtx.lock();
// mtx.lock();
for (auto &asset : _assets) {
asset->updateAnimations();
}
@@ -569,7 +572,7 @@ void FilamentViewer::render() {
_renderer->render(_view);
_renderer->endFrame();
}
mtx.unlock();
// mtx.unlock();
}
void FilamentViewer::updateViewportAndCameraProjection(

View File

@@ -127,7 +127,6 @@ namespace polyvox {
Material* _imageMaterial = nullptr;
TextureSampler _imageSampler;
ColorGrading *colorGrading = nullptr;
};

View File

@@ -15,7 +15,6 @@ extern "C" {
void* filament_viewer_new(void* texture, ResourceBuffer (*loadResource)(const char*), void (*freeResource)(uint32_t)) {
return nullptr;
}
void filament_viewer_delete(void* viewer) {
delete((FilamentViewer*)viewer);
@@ -135,8 +134,12 @@ extern "C" {
((FilamentViewer*)viewer)->clearAssets();
}
void set_texture(void* asset, const char* assetPath, int renderableIndex) {
((SceneAsset*)asset)->setTexture(assetPath, renderableIndex);
void load_texture(void* asset, const char* assetPath, int renderableIndex) {
((SceneAsset*)asset)->loadTexture(assetPath, renderableIndex);
}
void set_texture(void* asset) {
((SceneAsset*)asset)->setTexture();
}
void transform_to_unit_cube(void* asset) {

View File

@@ -46,7 +46,8 @@ void remove_asset(void* viewer, void* asset);
void clear_assets(void* viewer);
void set_texture(void* asset, const char* assetPath, int renderableIndex);
void load_texture(void* asset, const char* assetPath, int renderableIndex);
void set_texture(void* asset);
void transform_to_unit_cube(void* asset);

View File

@@ -41,7 +41,11 @@ SceneAsset::SceneAsset(FilamentAsset *asset, Engine *engine,
}
SceneAsset::~SceneAsset() {
// we defer all destructor work to SceneAssetLoader so we don't need to do anything here
// most other destructor work is deferred to SceneAssetLoader so we don't need to do anything here
if(_texture) {
_engine->destroy(_texture);
_texture = nullptr;
}
}
void SceneAsset::applyWeights(float *weights, int count) {
@@ -121,26 +125,35 @@ void SceneAsset::stopAnimation(int index) {
_embeddedAnimationStatus[index].started = false;
}
void SceneAsset::setTexture(const char* resourcePath, int renderableIndex) {
Log("Setting texture to %s for renderableIndex %d", resourcePath, renderableIndex);
ResourceBuffer imageResource = _loadResource(resourcePath);
void SceneAsset::loadTexture(const char* resourcePath, int renderableIndex) {
polyvox::StreamBufferAdapter sb((char *)imageResource.data, (char *)imageResource.data + imageResource.size);
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
std::istream *inputStream = new std::istream(&sb);
string rp("flutter_assets/assets/background.png");
if(_texture) {
_engine->destroy(_texture);
_texture = nullptr;
}
ResourceBuffer imageResource = _loadResource(rp.c_str());
StreamBufferAdapter sb((char *)imageResource.data, (char *)imageResource.data + imageResource.size);
istream *inputStream = new std::istream(&sb);
LinearImage *image = new LinearImage(ImageDecoder::decode(
*inputStream, resourcePath, ImageDecoder::ColorSpace::SRGB));
*inputStream, rp.c_str(), ImageDecoder::ColorSpace::SRGB));
if (!image->isValid()) {
Log("Invalid image : %s", resourcePath);
Log("Invalid image : %s", rp.c_str());
return;
}
uint32_t channels = image->getChannels();
uint32_t w = image->getWidth();
uint32_t h = image->getHeight();
auto texture = Texture::Builder()
_texture = Texture::Builder()
.width(w)
.height(h)
.levels(0xff)
@@ -149,8 +162,10 @@ void SceneAsset::setTexture(const char* resourcePath, int renderableIndex) {
.sampler(Texture::Sampler::SAMPLER_2D)
.build(*_engine);
Log("build texture");
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
void *data) {
void *data) {
delete reinterpret_cast<LinearImage *>(data);
};
@@ -159,20 +174,26 @@ void SceneAsset::setTexture(const char* resourcePath, int renderableIndex) {
channels == 3 ? Texture::Format::RGB : Texture::Format::RGBA,
Texture::Type::FLOAT, freeCallback);
texture->setImage(*_engine, 0, std::move(buffer));
_texture->setImage(*_engine, 0, std::move(buffer));
Log("set image");
setTexture();
delete inputStream;
size_t mic = _asset->getMaterialInstanceCount();
_freeResource(imageResource.id);
}
void SceneAsset::setTexture() {
MaterialInstance* const* inst = _asset->getMaterialInstances();
size_t mic = _asset->getMaterialInstanceCount();
Log("Material instance count : %d", mic);
RenderableManager &rm = _engine->getRenderableManager();
auto sampler = TextureSampler();
inst[0]->setParameter("baseColorIndex",0);
inst[0]->setParameter("baseColorMap",texture,sampler);
inst[0]->setParameter("baseColorMap",_texture,sampler);
delete inputStream;
_freeResource(imageResource.id);
}
void SceneAsset::updateEmbeddedAnimations() {

View File

@@ -4,16 +4,17 @@
#include <filament/RenderableManager.h>
#include <filament/Renderer.h>
#include <filament/Scene.h>
#include <filament/Texture.h>
#include <gltfio/AssetLoader.h>
#include <gltfio/FilamentAsset.h>
#include <gltfio/ResourceLoader.h>
#include <utils/NameComponentManager.h>
#include "SceneResources.hpp"
namespace polyvox {
using namespace filament;
using namespace filament::gltfio;
@@ -32,7 +33,8 @@ namespace polyvox {
///
///
///
void setTexture(const char* resourcePath, int renderableIndex);
void loadTexture(const char* resourcePath, int renderableIndex);
void setTexture();
///
/// Update the bone/morph target animations to reflect the current frame (if applicable).
@@ -79,6 +81,7 @@ namespace polyvox {
private:
FilamentAsset* _asset = nullptr;
Engine* _engine = nullptr;
NameComponentManager* _ncm;
@@ -86,6 +89,7 @@ namespace polyvox {
void updateMorphAnimation();
void updateEmbeddedAnimations();
Animator* _animator;
// animation flags;
@@ -95,5 +99,8 @@ namespace polyvox {
LoadResource _loadResource;
FreeResource _freeResource;
// a slot to preload textures
filament::Texture* _texture = nullptr;
};
}

View File

@@ -24,7 +24,8 @@ namespace polyvox {
ResourceLoader* resourceLoader,
NameComponentManager* ncm,
Engine* engine,
Scene* scene);
Scene* scene,
workqueue::threadpool& threadpool);
SceneAsset* fromGltf(const char* uri, const char* relativeResourcePath);
SceneAsset* fromGlb(const char* uri);
void remove(SceneAsset* asset);
@@ -37,6 +38,7 @@ namespace polyvox {
NameComponentManager* _ncm;
Scene* _scene;
Engine* _engine;
workqueue::threadpool& _threadpool;
};