mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 19:10:01 +02:00
add loading from KTXCache
This commit is contained in:
parent
a6a0fd3851
commit
e8319f967d
4 changed files with 42 additions and 10 deletions
|
@ -11,7 +11,7 @@
|
|||
|
||||
#include "KTXCache.h"
|
||||
|
||||
#include <memory>
|
||||
#include <ktx/KTX.h>
|
||||
|
||||
KTXFilePointer KTXCache::writeFile(Data data) {
|
||||
return std::static_pointer_cast<KTXFile>(FileCache::writeFile(data.key, data.data, data.length, (void*)&data));
|
||||
|
@ -48,3 +48,8 @@ void KTXCache::evictedFile(const FilePointer& file) {
|
|||
Lock lock(_urlMutex);
|
||||
_urlMap.erase(url);
|
||||
}
|
||||
|
||||
std::unique_ptr<ktx::KTX> KTXFile::getKTX() const {
|
||||
ktx::StoragePointer storage = std::make_shared<storage::FileStorage>(getFilepath().c_str());
|
||||
return ktx::KTX::create(storage);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,10 @@
|
|||
|
||||
#include <FileCache.h>
|
||||
|
||||
namespace ktx {
|
||||
class KTX;
|
||||
}
|
||||
|
||||
class KTXFile;
|
||||
using KTXFilePointer = std::shared_ptr<KTXFile>;
|
||||
|
||||
|
@ -55,6 +59,7 @@ class KTXFile : public File {
|
|||
|
||||
public:
|
||||
QUrl getUrl() const { return _url; }
|
||||
std::unique_ptr<ktx::KTX> getKTX() const;
|
||||
|
||||
protected:
|
||||
KTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url) :
|
||||
|
|
|
@ -451,12 +451,32 @@ void ImageReader::listSupportedImageFormats() {
|
|||
}
|
||||
|
||||
void FileReader::read() {
|
||||
PROFILE_RANGE_EX(resource_parse_ktx, __FUNCTION__, 0xffff0000, 0);
|
||||
gpu::TexturePointer texture;
|
||||
{
|
||||
auto resource = _resource.lock(); // to ensure the resource is still needed
|
||||
if (!resource) {
|
||||
qCDebug(modelnetworking) << _url << "loading stopped; resource out of scope";
|
||||
return;
|
||||
}
|
||||
|
||||
PROFILE_RANGE_EX(resource_parse_ktx, __FUNCTION__, 0xffff0000, 0);
|
||||
auto ktx = resource.staticCast<NetworkTexture>()->_file->getKTX();
|
||||
gpu::Texture::Usage usage;
|
||||
gpu::TextureUsageType usageType(gpu::TextureUsageType::RESOURCE);
|
||||
gpu::Sampler sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR);
|
||||
texture.reset(gpu::Texture::unserialize(usage, usageType, ktx, sampler));
|
||||
texture->setKtxBacking(ktx);
|
||||
}
|
||||
|
||||
auto resource = _resource.lock(); // to ensure the resource is still needed
|
||||
if (resource) {
|
||||
QMetaObject::invokeMethod(resource.data(), "setImage",
|
||||
Q_ARG(gpu::TexturePointer, texture),
|
||||
Q_ARG(int, texture->getWidth()), Q_ARG(int, texture->getHeight()));
|
||||
} else {
|
||||
qCDebug(modelnetworking) << _url << "loading stopped; resource out of scope";
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// auto ktx = ktx::KTX::create();
|
||||
// auto texture = gpu::Texture::unserialize(getUsage(), getUsageType(), ktx, getSampler());
|
||||
// FIXME: do I need to set the file as a backing file here?
|
||||
}
|
||||
|
||||
void ImageReader::read() {
|
||||
|
@ -503,7 +523,7 @@ void ImageReader::read() {
|
|||
auto url = _url.toString().toStdString();
|
||||
|
||||
PROFILE_RANGE_EX(resource_parse_image, __FUNCTION__, 0xffff0000, 0);
|
||||
texture.reset(resource.dynamicCast<NetworkTexture>()->getTextureLoader()(image, url));
|
||||
texture.reset(resource.staticCast<NetworkTexture>()->getTextureLoader()(image, url));
|
||||
texture->setSource(url);
|
||||
}
|
||||
|
||||
|
@ -531,12 +551,13 @@ void ImageReader::read() {
|
|||
if (!ktx || !(file = ktxCache.writeFile({ _url, hash, data, length }))) {
|
||||
qCWarning(modelnetworking) << _url << "file cache failed";
|
||||
} else {
|
||||
resource.dynamicCast<NetworkTexture>()->_file = file;
|
||||
// FIXME: do I need to set the file as a backing file here?
|
||||
resource.staticCast<NetworkTexture>()->_file = file;
|
||||
auto ktx = file->getKTX();
|
||||
texture->setKtxBacking(ktx);
|
||||
}
|
||||
}
|
||||
|
||||
auto resource = _resource.toStrongRef(); // to ensure the resource is still needed
|
||||
auto resource = _resource.lock(); // to ensure the resource is still needed
|
||||
if (resource) {
|
||||
QMetaObject::invokeMethod(resource.data(), "setImage",
|
||||
Q_ARG(gpu::TexturePointer, texture),
|
||||
|
|
|
@ -92,6 +92,7 @@ protected:
|
|||
Q_INVOKABLE void setImage(gpu::TexturePointer texture, int originalWidth, int originalHeight);
|
||||
|
||||
private:
|
||||
friend class FileReader;
|
||||
friend class ImageReader;
|
||||
|
||||
Type _type;
|
||||
|
|
Loading…
Reference in a new issue