Skip to content

Expose bound texture layer count via NativeEngine.getTextureLayerCount#1733

Draft
bghgary wants to merge 2 commits into
BabylonJS:masterfrom
bghgary:expose-texture-layer-count
Draft

Expose bound texture layer count via NativeEngine.getTextureLayerCount#1733
bghgary wants to merge 2 commits into
BabylonJS:masterfrom
bghgary:expose-texture-layer-count

Conversation

@bghgary

@bghgary bghgary commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Change

Adds NativeEngine::GetTextureLayerCount(handle) so wrapNativeTexture can populate is2DArray/depth on a wrapped Texture2DArray (today they stay at 2D defaults). Returns the bound layer count — Texture::ViewNumLayers() for a single-slice view (e.g. an ExternalTexture wrapped with a layerIndex), else Texture::NumLayers() — distinguishing a whole-array wrap (is2DArray) from a single array slice (plain 2D). Pure addition; existing callers unaffected.

Single-slice reporting keeps hosts correct: a host that wraps one slice of an NV12 array per video plane needs that plane to stay 2D, not be flagged is2DArray.

Consumer

BJS #18535 consumes this in wrapNativeTexture. Dormant until that PR ships in @babylonjs/core.

Test

ExternalTexture.WrapNativeTextureLayerCount (UnitTests) wraps three ExternalTextures end to end and asserts the InternalTexture:

  • whole array (arraySize 2, no layerIndex) → is2DArray, depth=baseDepth=2
  • single slice (arraySize 2, layerIndex 0) → plain 2D (one NV12 array slice)
  • single layer (arraySize 1) → plain 2D

Skips when the BJS consumer is absent, staying green across the cross-repo landing order.

[Created by Copilot on behalf of @bghgary]

bghgary added a commit to bghgary/Babylon.js that referenced this pull request Jun 4, 2026
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>
Babylon.js side wraps native textures with engine.wrapNativeTexture and
sets InternalTexture properties from the engine bindings. Today the
binding only exposes width and height, so consumers cannot detect that a
wrapped texture is a Texture2DArray and InternalTexture.is2DArray /
.depth stay at their defaults.

Add getTextureLayerCount so Babylon.js can populate is2DArray and depth
on the wrapped InternalTexture automatically. It returns the bound layer
count: ViewNumLayers() when the texture is wrapped as a single-slice view
(e.g. an ExternalTexture created with a layerIndex), otherwise
NumLayers(). This lets wrapNativeTexture distinguish a whole-array wrap
(is2DArray) from a single slice of an array texture (a plain 2D texture),
so a single-slice view is not misreported as an array.

[Created by Copilot on behalf of @bghgary]

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary force-pushed the expose-texture-layer-count branch from f9ca23c to efa0d70 Compare June 17, 2026 16:23
@bghgary bghgary changed the title Expose Texture::NumLayers via NativeEngine.getTextureLayerCount Expose bound texture layer count via NativeEngine.getTextureLayerCount 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant