replace view with texture, move plugin to Swift implementation

This commit is contained in:
Nick Fisher
2022-08-19 12:39:55 +08:00
parent 21fef19359
commit c6e429bcba
20 changed files with 658 additions and 491 deletions

View File

@@ -0,0 +1,44 @@
#ifndef RESOURCE_BUFFER_H
#define RESOURCE_BUFFER_H
#include <stdint.h>
#if defined(__cplusplus)
extern "C" {
#endif
//
// Pairs a memory buffer with an ID that can be used to unload the backing asset if needed.
// Use this when you want to load an asset from a resource that requires more than just `free` on the underlying buffer.
// e.g.
// ```
// uint64_t id = get_next_resource_id();
// AAsset *asset = AAssetManager_open(am, name, AASSET_MODE_BUFFER);
// off_t length = AAsset_getLength(asset);
// const void * buffer = AAsset_getBuffer(asset);
// uint8_t *buf = new uint8_t[length ];
// memcpy(buf,buffer, length);
// ResourceBuffer rb(buf, length, id);
// ...
// ...
// (elsewhere)
// AAsset* asset = get_asset_from_id(rb.id);
// AAsset_close(asset);
// free_asset_id(rb.id);
//
struct ResourceBuffer {
#if defined(__cplusplus)
ResourceBuffer(const void* data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
ResourceBuffer& operator=(ResourceBuffer other) {
data = other.data;
size = other.size;
id = other.id;
return *this;
}
#endif
const void* data;
uint32_t size;
uint32_t id;
};
#if defined(__cplusplus)
}
#endif
#endif

View File

@@ -139,7 +139,7 @@ void SceneAsset::setTexture(const char* resourcePath, int renderableIndex) {
delete inputStream;
_freeResource(imageResource);
_freeResource(imageResource.id);
uint32_t channels = image->getChannels();
uint32_t w = image->getWidth();

View File

@@ -52,7 +52,7 @@ SceneAsset *SceneAssetLoader::fromGltf(const char *uri,
ResourceLoader::BufferDescriptor b(buf.data, buf.size);
_resourceLoader->addResourceData(resourceUris[i], std::move(b));
_freeResource(buf);
_freeResource(buf.id);
}
_resourceLoader->loadResources(asset);
@@ -115,7 +115,7 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
asset->releaseSourceData();
Log("Source data released.");
_freeResource(rbuf);
_freeResource(rbuf.id);
Log("Successfully loaded GLB.");
return new SceneAsset(asset, _engine, _ncm, _loadResource, _freeResource);
@@ -128,4 +128,4 @@ void SceneAssetLoader::remove(SceneAsset *asset) {
_assetLoader->destroyAsset(asset->_asset);
delete asset;
}
} // namespace polyvox
} // namespace polyvox

View File

@@ -8,6 +8,7 @@
#include "SceneResources.hpp"
#include "SceneAsset.hpp"
#include "ResourceBuffer.hpp"
namespace polyvox {
using namespace filament;

View File

@@ -1,52 +1,21 @@
#pragma once
#include <functional>
#include <memory>
#include "ResourceBuffer.hpp"
namespace polyvox {
//
// Pairs a memory buffer with an ID that can be used to unload the backing asset if needed.
// Use this when you want to load an asset from a resource that requires more than just `free` on the underlying buffer.
// e.g.
// ```
// uint64_t id = get_next_resource_id();
// AAsset *asset = AAssetManager_open(am, name, AASSET_MODE_BUFFER);
// off_t length = AAsset_getLength(asset);
// const void * buffer = AAsset_getBuffer(asset);
// uint8_t *buf = new uint8_t[length ];
// memcpy(buf,buffer, length);
// ResourceBuffer rb(buf, length, id);
// ...
// ...
// (elsewhere)
// AAsset* asset = get_asset_from_id(rb.id);
// AAsset_close(asset);
// free_asset_id(rb.id);
//
struct ResourceBuffer {
ResourceBuffer(const void* data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
ResourceBuffer& operator=(ResourceBuffer other)
{
data = other.data;
size = other.size;
id = other.id;
return *this;
}
const void* data;
uint32_t size;
uint32_t id;
};
using namespace std;
//
// Typedef for any function that loads a resource into a ResourceBuffer from an asset URI.
// Typedef for a function that loads a resource into a ResourceBuffer from an asset URI.
//
using LoadResource = std::function<ResourceBuffer(const char* uri)>;
using LoadResource = function<ResourceBuffer(const char* uri)>;
//
// Typedef for any function that frees a ResourceBuffer.
// Typedef for a function that frees an ID associated with a ResourceBuffer.
//
using FreeResource = std::function<void (ResourceBuffer)>;
using FreeResource = function<void (uint32_t)>;
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
@@ -110,4 +79,5 @@ namespace polyvox {
float* frameData;
int numWeights;
};
}
}