Skip to content

Auto-populate is2DArray/depth in wrapNativeTexture from native layer count#18535

Merged
bghgary merged 7 commits into
BabylonJS:masterfrom
bghgary:wrap-native-texture-auto-array
Jun 17, 2026
Merged

Auto-populate is2DArray/depth in wrapNativeTexture from native layer count#18535
bghgary merged 7 commits into
BabylonJS:masterfrom
bghgary:wrap-native-texture-auto-array

Conversation

@bghgary

@bghgary bghgary commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

wrapNativeTexture now auto-populates is2DArray and depth on the wrapped InternalTexture via a new engine.getTextureLayerCount(...) binding, so hosts no longer have to thread the layer count through their own bridge to wrap a Texture2DArray correctly. updateWrappedNativeTexture gains a matching layer-count validation.

The binding is optional on INativeEngine; both call sites guard with a truthy check, so older Babylon Native builds without the binding (BabylonJS/BabylonNative#1733) keep their current behavior — this PR is a no-op until that lands.

[Created by Copilot on behalf of @bghgary]

Babylon Native used to require consumers to thread a texture's layer count
through their own bridge to detect when a wrapped native texture was a
Texture2DArray, because `wrapNativeTexture` only read width and height
from the engine binding. Defaulting `is2DArray` and `depth` to unset/0
caused downstream code that picks a sampler variant by
`InternalTexture.is2DArray` to mismatch the bgfx SRV view dimension and
hit undefined behavior on some D3D11 drivers.

With the matching `getTextureLayerCount` binding on the Babylon Native
side, `wrapNativeTexture` can populate `is2DArray` and `depth` itself,
removing the host-side plumbing.

Also tighten `updateWrappedNativeTexture` to validate that the new
handle's layer count matches the wrapped texture's recorded layer count,
matching the existing dimension check.

Requires the matching getTextureLayerCount binding in Babylon Native;
older Babylon Native builds throw on the missing method.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bjsplat

bjsplat commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat

bjsplat commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Snapshot stored with reference name:
refs/pull/18535/merge

Test environment:
https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18535/merge/index.html

To test a playground add it to the URL, for example:

https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18535/merge/index.html#WGZLGJ#4600

Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves):

https://playground.babylonjs.com/?snapshot=refs/pull/18535/merge
https://sandbox.babylonjs.com/?snapshot=refs/pull/18535/merge
https://gui.babylonjs.com/?snapshot=refs/pull/18535/merge
https://nme.babylonjs.com/?snapshot=refs/pull/18535/merge

To test the snapshot in the playground with a playground ID add it after the snapshot query string:

https://playground.babylonjs.com/?snapshot=refs/pull/18535/merge#BCU1XR#0

If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools.

bghgary and others added 2 commits June 4, 2026 08:29
Mark `INativeEngine.getTextureLayerCount` optional and feature-detect
(`typeof === "function"`) before calling, so older Babylon Native builds
that don't expose the binding keep working:

- `wrapNativeTexture`: if the binding is absent, skip auto-populating
  `is2DArray` / `depth`. The wrapped InternalTexture stays at the
  defaults, matching pre-existing behavior.
- `updateWrappedNativeTexture`: if the binding is absent, skip the
  layer-count validation. Dimensions are still validated.

Removes the runtime dependency on BabylonJS/BabylonNative#1733, so this
PR can land independently. Hosts running on an updated native engine
get the auto-detect for free; hosts on older native engines see no
change.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Optional method on the interface is either a function (truthy) or undefined (falsy); the typeof === 'function' guard adds no value over a plain truthy check.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary marked this pull request as ready for review June 4, 2026 15:31
Copilot AI review requested due to automatic review settings June 4, 2026 15:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves correctness when wrapping externally-owned native textures in the Native engine by introspecting the native resource’s array-layer count (when available) and using it to configure the resulting InternalTexture as a 2D array texture, preventing sampler/view-dimension mismatches.

Changes:

  • Add optional INativeEngine.getTextureLayerCount() to query a native texture’s array-layer count.
  • In ThinNativeEngine.wrapNativeTexture, detect layer count and set InternalTexture.is2DArray / depth accordingly.
  • In ThinNativeEngine.updateWrappedNativeTexture, validate that a replacement native handle has the same layer count as the wrapped texture.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/dev/core/src/Engines/thinNativeEngine.pure.ts Auto-detects 2D array textures via native layer count and validates layer-count stability when swapping wrapped handles.
packages/dev/core/src/Engines/Native/nativeInterfaces.ts Extends the native engine interface with an optional getTextureLayerCount capability.

Comment thread packages/dev/core/src/Engines/thinNativeEngine.pure.ts
@deltakosh deltakosh enabled auto-merge (squash) June 10, 2026 14:56
@sebavan

sebavan commented Jun 15, 2026

Copy link
Copy Markdown
Member

cc @bghgary :-)

@bghgary bghgary marked this pull request as draft June 16, 2026 21:43
auto-merge was automatically disabled June 16, 2026 21:43

Pull request was converted to draft

bghgary and others added 2 commits June 16, 2026 14:45
- wrapNativeTexture: set baseDepth alongside depth so a wrapped
  Texture2DArray reports its initial depth consistently with the rest of
  the engine (baseDepth = depth = layerCount).
- updateWrappedNativeTexture: put the layer-count mismatch error on a
  single line to satisfy prettier (printWidth 180).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Match the surrounding INativeEngine members (getTextureWidth/Height etc.),
which carry no doc comments; the optional `?` already conveys availability.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bjsplat

bjsplat commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat

bjsplat commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Snapshot stored with reference name:
refs/pull/18535/merge

Test environment:
https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18535/merge/index.html

To test a playground add it to the URL, for example:

https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18535/merge/index.html#WGZLGJ#4600

Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves):

https://playground.babylonjs.com/?snapshot=refs/pull/18535/merge
https://sandbox.babylonjs.com/?snapshot=refs/pull/18535/merge
https://gui.babylonjs.com/?snapshot=refs/pull/18535/merge
https://nme.babylonjs.com/?snapshot=refs/pull/18535/merge

To test the snapshot in the playground with a playground ID add it after the snapshot query string:

https://playground.babylonjs.com/?snapshot=refs/pull/18535/merge#BCU1XR#0

If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools.

bghgary added a commit to bghgary/BabylonNative that referenced this pull request Jun 17, 2026
Exercises the cross-repo path end to end: getTextureLayerCount feeds
Babylon.js wrapNativeTexture (BabylonJS/Babylon.js#18535), which sets
is2DArray / depth / baseDepth when a wrapped texture presents more than
one layer.

Wraps three ExternalTextures and asserts the resulting InternalTexture:
  - whole array  (arraySize 2, no layerIndex) -> is2DArray, depth=baseDepth=2
  - single slice (arraySize 2, layerIndex 0)  -> plain 2D (as Teams wraps an NV12 plane)
  - single layer (arraySize 1)                -> plain 2D

The single-slice case guards against regressing a single array slice into
a 2D array. Skips cleanly when the native binding or the consuming
Babylon.js change is absent so it stays green across the cross-repo
landing order.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bghgary added a commit to bghgary/BabylonNative that referenced this pull request Jun 17, 2026
Exercises the cross-repo path end to end: getTextureLayerCount feeds
Babylon.js wrapNativeTexture (BabylonJS/Babylon.js#18535), which sets
is2DArray / depth / baseDepth when a wrapped texture presents more than
one layer.

Wraps three ExternalTextures and asserts the resulting InternalTexture:
  - whole array  (arraySize 2, no layerIndex) -> is2DArray, depth=baseDepth=2
  - single slice (arraySize 2, layerIndex 0)  -> plain 2D (one slice of an NV12 array)
  - single layer (arraySize 1)                -> plain 2D

The single-slice case guards against regressing a single array slice into
a 2D array. Skips cleanly when the native binding or the consuming
Babylon.js change is absent so it stays green across the cross-repo
landing order.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bjsplat

bjsplat commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

🟢 Memory Leak Test Results

13 passed, 0 leaked out of 13 scenarios

🟢 All memory leak tests passed — no leaks detected.

Passed Scenarios (13)
Scenario Package
Core Feature Stack @babylonjs/core
Core Rendering Materials Shadows Stack @babylonjs/core
Core Textures Render Targets PostProcess Stack @babylonjs/core
GUI Fullscreen UI Controls @babylonjs/gui
GUI Mesh ADT Controls @babylonjs/gui
Loaders Boombox Import @babylonjs/loaders
Loaders OBJ Direct Load @babylonjs/loaders
Loaders STL Direct Load @babylonjs/loaders
Materials Library Stack @babylonjs/materials
Serializers glTF Export @babylonjs/serializers
Serializers GLB Export @babylonjs/serializers
PostProcesses Digital Rain Stack @babylonjs/post-processes
Procedural Textures Stack @babylonjs/procedural-textures

@bjsplat

bjsplat commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

⚡ Performance Test Results

Baseline: Latest · Candidate: Dev

Metric Value
🔴 Average 6.4%25 slower
Median 6.4%25 slower
Tests 1 conclusive, 0 inconclusive
🔘 Not Significant — p ≥ 0.05 (1)
Test Baseline Candidate Diff p-value
Convolution Post Process [#FBH4J7#281] (webgl2) 3.4ms 3.6ms 6.4%25 slower 0.1452

@bghgary bghgary marked this pull request as ready for review June 17, 2026 23:27
@bghgary bghgary merged commit 9b38142 into BabylonJS:master Jun 17, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants