-
Notifications
You must be signed in to change notification settings - Fork 152
Add cubemap auto-expand polyfill for Playground (re-enables 7 PBR tests) #1710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bkaradzic-microsoft
wants to merge
2
commits into
BabylonJS:master
Choose a base branch
from
bkaradzic-microsoft:weekend/tpc-1577-cubemap-6-files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| set(SOURCES | ||
| "Include/Babylon/Polyfills/CubeTexture.h" | ||
| "Source/CubeTexture.cpp") | ||
|
|
||
| add_library(CubeTexture ${SOURCES}) | ||
| warnings_as_errors(CubeTexture) | ||
|
|
||
| target_include_directories(CubeTexture | ||
| PUBLIC "Include") | ||
|
|
||
| target_link_libraries(CubeTexture | ||
| PUBLIC napi | ||
| PUBLIC Foundation) | ||
|
|
||
| set_property(TARGET CubeTexture PROPERTY FOLDER Polyfills) | ||
| source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES}) |
31 changes: 31 additions & 0 deletions
31
Polyfills/CubeTexture/Include/Babylon/Polyfills/CubeTexture.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #pragma once | ||
|
|
||
| #include <napi/env.h> | ||
| #include <Babylon/Api.h> | ||
|
|
||
| namespace Babylon::Polyfills::CubeTexture | ||
| { | ||
| // Patches BABYLON.NativeEngine.prototype.createCubeTexture to transparently | ||
| // retry .dds / .ktx / .ktx2 single-URL cubemap loads as .env (a format BN | ||
| // does support). Babylon's CDN co-hosts both, so the swap is invisible to | ||
| // consumer JS. Falls back to the original (which throws "Cannot load | ||
| // cubemap because 6 files were not defined") on 404 - preserving existing | ||
| // error semantics. | ||
| // | ||
| // Call Initialize AFTER babylon.max.js has been evaluated; the patch | ||
| // requires BABYLON.NativeEngine.prototype to exist. When using the | ||
| // jsruntimehost ScriptLoader, the simplest pattern is: | ||
| // | ||
| // scriptLoader.LoadScript("app:///Scripts/babylon.max.js"); | ||
| // scriptLoader.Dispatch([](Napi::Env env) { | ||
| // Babylon::Polyfills::CubeTexture::Initialize(env); | ||
| // }); | ||
| // | ||
| // The Dispatch queues onto the same ordered task chain as LoadScript, so | ||
| // Initialize is guaranteed to run after babylon.max.js finishes evaluating. | ||
| // | ||
| // This is a tactical bridge until Plugins/NativeEngine wires a proper | ||
| // loader registry path for cubemap formats. Safe to call multiple times; | ||
| // the patch is idempotent. | ||
| void BABYLON_API Initialize(Napi::Env env); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| #include <Babylon/Polyfills/CubeTexture.h> | ||
|
|
||
| #include <napi/env.h> | ||
|
|
||
| namespace Babylon::Polyfills::CubeTexture | ||
| { | ||
| namespace | ||
| { | ||
| // Embedded polyfill source. Kept verbatim from the original Playground | ||
| // cube_texture_polyfill.js (PR #1710 v0) so behaviour stays identical: | ||
| // - Patches BABYLON.NativeEngine.prototype.createCubeTexture. | ||
| // - Triggers only when the URL ends in .dds / .ktx / .ktx2 and no | ||
| // forcedExtension, 6-file array, or raw buffer was supplied. | ||
| // - Retries via NativeEngine._loadFile to fetch the .env counterpart; | ||
| // on 404 falls back to the original (which throws), preserving the | ||
| // existing failure mode. | ||
| // - Idempotent via the __cubeTexturePolyfillInstalled marker on the | ||
| // prototype. | ||
| constexpr const char* JS_SOURCE = R"javascript( | ||
| (function () { | ||
| "use strict"; | ||
|
|
||
| if (typeof BABYLON === "undefined") { | ||
| return; | ||
| } | ||
| if (!BABYLON.NativeEngine || !BABYLON.NativeEngine.prototype) { | ||
| return; | ||
| } | ||
|
|
||
| var proto = BABYLON.NativeEngine.prototype; | ||
| if (proto.__cubeTexturePolyfillInstalled) { | ||
| return; | ||
| } | ||
| proto.__cubeTexturePolyfillInstalled = true; | ||
|
|
||
| var original = proto.createCubeTexture; | ||
| if (typeof original !== "function") { | ||
| return; | ||
| } | ||
|
|
||
| var FALLBACK_EXTS = [".dds", ".ktx", ".ktx2"]; | ||
|
|
||
| function getExtension(url, forced) { | ||
| if (forced) { | ||
| return forced.toLowerCase(); | ||
| } | ||
| var dot = url.lastIndexOf("."); | ||
| if (dot < 0) { | ||
| return ""; | ||
| } | ||
| var ext = url.substring(dot).toLowerCase(); | ||
| var q = ext.indexOf("?"); | ||
| if (q >= 0) { | ||
| ext = ext.substring(0, q); | ||
| } | ||
| return ext; | ||
| } | ||
|
|
||
| function replaceExt(url, oldExt) { | ||
| return url.substring(0, url.length - oldExt.length) + ".env"; | ||
| } | ||
|
|
||
| proto.createCubeTexture = function (rootUrl, scene, files, noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, fallback, loaderOptions, useSRGBBuffer, buffer) { | ||
| var ext = getExtension(rootUrl, forcedExtension); | ||
| var hasFiles = files && files.length === 6; | ||
| var canFallback = !buffer && !forcedExtension && !hasFiles && FALLBACK_EXTS.indexOf(ext) >= 0; | ||
|
|
||
| if (!canFallback) { | ||
| return original.apply(this, arguments); | ||
| } | ||
|
|
||
| var self = this; | ||
| var envUrl = replaceExt(rootUrl, ext); | ||
| var texture = fallback || new BABYLON.InternalTexture(self, 7 /* Cube */); | ||
| texture.isCube = true; | ||
| texture.url = rootUrl; | ||
|
|
||
| var settled = false; | ||
| var settle = function (action) { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| settled = true; | ||
| try { | ||
| action(); | ||
| } catch (e) { | ||
| if (onError) { | ||
| onError(e && e.message ? e.message : String(e), e); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var onEnvLoaded = function (data) { | ||
| settle(function () { | ||
| var buf = (data && data.byteLength !== undefined && !(data instanceof Uint8Array)) ? new Uint8Array(data, 0, data.byteLength) : data; | ||
| original.call(self, envUrl, scene, files, noMipmap, onLoad, onError, format, ".env", createPolynomials, lodScale || 0, lodOffset || 0, texture, loaderOptions, useSRGBBuffer || false, buf); | ||
| }); | ||
| }; | ||
|
|
||
| var onEnvFailed = function (/* request, exception */) { | ||
| settle(function () { | ||
| original.call(self, rootUrl, scene, files, noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, texture, loaderOptions, useSRGBBuffer, buffer); | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| self._loadFile(envUrl, onEnvLoaded, undefined, undefined, true, onEnvFailed); | ||
| } catch (e) { | ||
| onEnvFailed(null, e); | ||
| } | ||
|
|
||
| return texture; | ||
| }; | ||
|
|
||
| if (typeof console !== "undefined" && console.log) { | ||
| console.log("[CubeTexture polyfill] NativeEngine.createCubeTexture patched with .env fallback"); | ||
| } | ||
| })(); | ||
| )javascript"; | ||
|
|
||
| constexpr const char* JS_SOURCE_URL = "babylon-native://polyfills/CubeTexture.js"; | ||
| } | ||
|
|
||
| void Initialize(Napi::Env env) | ||
| { | ||
| // The free function Napi::Eval(env, source, url) is declared by every | ||
| // engine-specific <napi/env.h> across both N-API trees (Chakra, V8, | ||
| // JavaScriptCore in Core/Node-API/Include/Engine/<X>/, and JSI in | ||
| // Core/Node-API-JSI/Include/napi/). Using it uniformly avoids the | ||
| // Shared-only env.RunScript() which is missing from the JSI tree. | ||
| Napi::HandleScope scope{env}; | ||
| Napi::Eval(env, JS_SOURCE, JS_SOURCE_URL); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three layered concerns:
.dds, we quietly load the.envco-located by Babylon's CDN. Tests "pass" but they're rendering a different file (different format, mip pyramid, precision). If BJS's.ddspath regresses, we don't catch it..env. Babylon's playground CDN does; arbitrary BN consumers don't. Shipping this inPolyfills/advertises a benefit non-Babylon consumers won't get.NativeEnginedoesn't dispatch.dds/.ktx/.ktx2cubemap loading even though bimg already supports the formats. The proper fix lives inPlugins/NativeEngine.Let's discuss offline before merging.