Skip to content

Commit

Permalink
[Olympus] Allow decoding textures externally
Browse files Browse the repository at this point in the history
- allow TextureProvider to work directly with cgltf_texture
- change ResourceLoader to call TextureProvider with cgltf_texture
  • Loading branch information
zbai-sc committed Jul 8, 2022
1 parent 0192d8e commit 944cb16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 12 additions & 1 deletion libs/gltfio/include/gltfio/TextureProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <utils/compiler.h>

class cgltf_texture;

namespace filament {
class Engine;
class Texture;
Expand Down Expand Up @@ -86,7 +88,16 @@ class UTILS_PUBLIC TextureProvider {
* Texture object, but it is only safe to do so after it has been popped from the queue.
*/
virtual Texture* pushTexture(const uint8_t* data, size_t byteCount,
const char* mimeType, FlagBits flags) = 0;
const char* mimeType, FlagBits flags) {
return nullptr;
}

/**
* Alternate version of pushTexture, takes raw cgltf_texture data to allow more control.
*/
virtual Texture* pushTexture(const cgltf_texture* srcTexture, FlagBits flags) {
return nullptr;
}

/**
* Checks if any texture is ready to be removed from the asynchronous decoding queue, and if so
Expand Down
20 changes: 15 additions & 5 deletions libs/gltfio/src/ResourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,16 +616,26 @@ void ResourceLoader::asyncUpdateLoad() {

Texture* ResourceLoader::Impl::getOrCreateTexture(FFilamentAsset* asset, const TextureSlot& tb) {
const cgltf_texture* srcTexture = tb.texture;
const cgltf_image* image = srcTexture->basisu_image ?
srcTexture->basisu_image : srcTexture->image;
const cgltf_buffer_view* bv = image->buffer_view;
const char* uri = image->uri;

TextureProvider::FlagBits flags = {};
if (tb.srgb) {
flags |= int(TextureProvider::Flags::sRGB);
}

// Check if there is a texture provider that can work with cgtlf_texture.
if (auto iter = mTextureProviders.find("cgltf_texture"); iter != mTextureProviders.end()) {
TextureProvider* provider = iter->second;
Texture* texture = provider->pushTexture(srcTexture, flags);
if (texture) {
// Note we didn't pass ownership to asset. Caller is responsible for cleaning up.
return texture;
}
}

const cgltf_image* image = srcTexture->basisu_image ?
srcTexture->basisu_image : srcTexture->image;
const cgltf_buffer_view* bv = image->buffer_view;
const char* uri = image->uri;

std::string mime = image->mime_type ? image->mime_type : "";
size_t dataUriSize;
const uint8_t* dataUriContent = uri ? parseDataUri(uri, &mime, &dataUriSize) : nullptr;
Expand Down

0 comments on commit 944cb16

Please sign in to comment.