Skip to content

Commit

Permalink
Fix Avatar Texture Loading (#57)
Browse files Browse the repository at this point in the history
* Fix extension support

* Patch expo GL
  • Loading branch information
hmallen99 authored Feb 1, 2025
1 parent 68c5569 commit d09cc89
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 5 deletions.
6 changes: 1 addition & 5 deletions packages/engine/src/assets/functions/createGLTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ export const initializeKTX2Loader = (loader: GLTFLoader) => {
hasFeature: () => false,
extensions: new Map([
['WEBGL_compressed_texture_astc', true],
['WEBGL_compressed_texture_etc1', false],
['WEBGL_compressed_texture_etc', true],
['WEBGL_compressed_texture_s3tc', false],
['EXT_texture_compression_bptc', false],
['WEBGL_compressed_texture_pvrtc', true],
['WEBKIT_WEBGL_compressed_texture_pvrtc', false]
['WEBGL_compressed_texture_pvrtc', true]
]),
capabilities: {
isWebGL2: true
Expand Down
152 changes: 152 additions & 0 deletions patches/expo-gl+15.0.2.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,155 @@
diff --git a/node_modules/expo-gl/common/EXGLImageUtils.cpp b/node_modules/expo-gl/common/EXGLImageUtils.cpp
index 82cd012..7f85718 100644
--- a/node_modules/expo-gl/common/EXGLImageUtils.cpp
+++ b/node_modules/expo-gl/common/EXGLImageUtils.cpp
@@ -135,5 +135,23 @@ std::shared_ptr<uint8_t> loadImage(
}
return std::shared_ptr<uint8_t>(nullptr);
}
+
+GLsizei calculateImageSize(GLsizei width, GLsizei height, GLint internalFormat) {
+ switch (internalFormat) {
+ case GL_COMPRESSED_R11_EAC:
+ case GL_COMPRESSED_SIGNED_R11_EAC:
+ case GL_COMPRESSED_RGB8_ETC2:
+ case GL_COMPRESSED_SRGB8_ETC2:
+ case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
+ case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
+ return (width / 4) * (height / 4) * 8;
+ case GL_COMPRESSED_RG11_EAC:
+ case GL_COMPRESSED_SIGNED_RG11_EAC:
+ case GL_COMPRESSED_RGBA8_ETC2_EAC:
+ case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
+ default:
+ return (width / 4) * (height / 4) * 16;
+ }
+}
} // namespace gl_cpp
} // namespace expo
diff --git a/node_modules/expo-gl/common/EXGLImageUtils.h b/node_modules/expo-gl/common/EXGLImageUtils.h
index 230ea99..4745a52 100644
--- a/node_modules/expo-gl/common/EXGLImageUtils.h
+++ b/node_modules/expo-gl/common/EXGLImageUtils.h
@@ -24,5 +24,7 @@ std::shared_ptr<uint8_t> loadImage(
int *fileWidth,
int *fileHeight,
int *fileComp);
+
+GLsizei calculateImageSize(GLsizei width, GLsizei height, GLint internalFormat);
} // namespace gl_cpp
} // namespace expo
diff --git a/node_modules/expo-gl/common/EXWebGLMethods.cpp b/node_modules/expo-gl/common/EXWebGLMethods.cpp
index 66f8755..b3ec652 100644
--- a/node_modules/expo-gl/common/EXWebGLMethods.cpp
+++ b/node_modules/expo-gl/common/EXWebGLMethods.cpp
@@ -597,9 +597,82 @@ NATIVE_METHOD(bindTexture) {
return nullptr;
}

-UNIMPL_NATIVE_METHOD(compressedTexImage2D)
+NATIVE_METHOD(compressedTexImage2D) {
+ CTX();
+ auto target = ARG(0, GLenum);
+ auto level = ARG(1, GLint);
+ auto internalformat = ARG(2, GLenum);
+ if (argc == 7) {
+ auto width = ARG(3, GLsizei);
+ auto height = ARG(4, GLsizei);
+ auto border = ARG(5, GLsizei);
+ auto imageSize = calculateImageSize(width, height, internalformat);
+ if (ARG(6, const jsi::Value &).isNull()) {
+ ctx->addToNextBatch([=] {
+ glCompressedTexImage2D(target, level, internalformat, width, height, border, imageSize, nullptr);
+ });
+ return nullptr;
+ }
+ auto data = ARG(6, jsi::Object);
+
+ if (data.isArrayBuffer(runtime) || isTypedArray(runtime, data)) {
+ std::vector<uint8_t> vec = rawTypedArray(runtime, std::move(data));
+ ctx->addToNextBatch([=, vec{std::move(vec)}] {
+ glCompressedTexImage2D(
+ target, level, internalformat, width, height, border, imageSize, vec.data());
+ });
+ } else {
+ auto image = loadImage(runtime, data, &width, &height, nullptr);
+ ctx->addToNextBatch([=] {
+ glCompressedTexImage2D(
+ target, level, internalformat, width, height, border, imageSize, image.get());
+ });
+ }
+ } else {
+ throw std::runtime_error("EXGL: Invalid number of arguments to gl.compressedTexImage2D()!");
+ }
+ return nullptr;
+}
+
+
+NATIVE_METHOD(compressedTexSubImage2D) {
+ CTX();
+ auto target = ARG(0, GLenum);
+ auto level = ARG(1, GLint);
+ auto xoffset = ARG(2, GLint);
+ auto yoffset = ARG(3, GLint);
+ if (argc == 8) {
+ auto width = ARG(4, GLsizei);
+ auto height = ARG(5, GLsizei);
+ auto format = ARG(6, GLenum);
+ auto imageSize = calculateImageSize(width, height, format);
+ if (ARG(7, const jsi::Value &).isNull()) {
+ ctx->addToNextBatch([=] {
+ auto empty = std::make_unique<uint8_t>(imageSize);
+ std::memset(empty.get(), 0, imageSize);
+ glCompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, empty.get());
+ });
+ return nullptr;
+ }

-UNIMPL_NATIVE_METHOD(compressedTexSubImage2D)
+ auto data = ARG(7, jsi::Object);
+
+ if (data.isArrayBuffer(runtime) || isTypedArray(runtime, data)) {
+ std::vector<uint8_t> vec = rawTypedArray(runtime, std::move(data));
+ ctx->addToNextBatch([=, vec{std::move(vec)}] {
+ glCompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, vec.data());
+ });
+ } else {
+ auto image = loadImage(runtime, data, &width, &height, nullptr);
+ ctx->addToNextBatch([=] {
+ glCompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, image.get());
+ });
+ }
+ } else {
+ throw std::runtime_error("EXGL: Invalid number of arguments to gl.compressedTexSubImage2D()!");
+ }
+ return nullptr;
+}

SIMPLE_NATIVE_METHOD(
copyTexImage2D,
@@ -1945,6 +2018,22 @@ NATIVE_METHOD(getExtension) {
runtime, "MAX_TEXTURE_MAX_ANISOTROPY_EXT", jsi::Value(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT));
return result;
}
+ if (name == "WEBGL_compressed_texture_etc") {
+ jsi::Object result(runtime);
+ result.setProperty(runtime, "COMPRESSED_R11_EAC", jsi::Value(GL_COMPRESSED_R11_EAC));
+ result.setProperty(runtime, "COMPRESSED_SIGNED_R11_EAC", jsi::Value(GL_COMPRESSED_SIGNED_R11_EAC));
+ result.setProperty(runtime, "COMPRESSED_RG11_EAC", jsi::Value(GL_COMPRESSED_RG11_EAC));
+ result.setProperty(runtime, "COMPRESSED_SIGNED_RG11_EAC", jsi::Value(GL_COMPRESSED_SIGNED_RG11_EAC));
+ result.setProperty(runtime, "COMPRESSED_RGB8_ETC2", jsi::Value(GL_COMPRESSED_RGB8_ETC2));
+ result.setProperty(runtime, "COMPRESSED_RGBA8_ETC2_EAC", jsi::Value(GL_COMPRESSED_RGBA8_ETC2_EAC));
+ result.setProperty(runtime, "COMPRESSED_SIGNED_R11_EAC", jsi::Value(GL_COMPRESSED_SIGNED_R11_EAC));
+ result.setProperty(runtime, "COMPRESSED_SRGB8_ETC2", jsi::Value(GL_COMPRESSED_SRGB8_ETC2));
+ result.setProperty(runtime, "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", jsi::Value(GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC));
+ result.setProperty(runtime, "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", jsi::Value(GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2));
+ result.setProperty(runtime, "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", jsi::Value(GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2));
+
+ return result;
+ }
return jsi::Object(runtime);
}

diff --git a/node_modules/expo-gl/ios/GLView.swift b/node_modules/expo-gl/ios/GLView.swift
index e3ce5fe..104d004 100644
--- a/node_modules/expo-gl/ios/GLView.swift
Expand Down

0 comments on commit d09cc89

Please sign in to comment.