Skip to content

Add external texture array render test and fix numLayers bug#1646

Draft
bghgary wants to merge 19 commits intoBabylonJS:masterfrom
bghgary:external-texture-render-test
Draft

Add external texture array render test and fix numLayers bug#1646
bghgary wants to merge 19 commits intoBabylonJS:masterfrom
bghgary:external-texture-render-test

Conversation

@bghgary
Copy link
Copy Markdown
Contributor

@bghgary bghgary commented Mar 25, 2026

Overview

This PR changes the ExternalTexture plugin from an asynchronous API to a synchronous API, adds thread safety, fixes a numLayers bug, and adds an end-to-end rendering test.

ExternalTexture API Change: Async to Sync

The main change replaces AddToContextAsync (which returned a Napi::Promise and required an extra render frame to resolve) with CreateForJavaScript (which synchronously returns a Napi::Value).

Before (async — required promises, callbacks, and an extra frame pump):

C++:

std::promise<void> textureCreationSubmitted{};
std::promise<void> textureCreationDone{};

auto externalTexture = std::make_shared<Babylon::Plugins::ExternalTexture>(d3d12Resource);

jsRuntime.Dispatch([&](Napi::Env env) {
    auto jsPromise = externalTexture->AddToContextAsync(env);
    env.Global().Get("setup").As<Napi::Function>().Call({
        jsPromise,
        Napi::Value::From(env, width),
        Napi::Value::From(env, height),
        Napi::Function::New(env, [&](const Napi::CallbackInfo&) {
            textureCreationDone.set_value();
        })
    });
    textureCreationSubmitted.set_value();
});

textureCreationSubmitted.get_future().get();

// Extra frame pump required for the texture to be created
m_update->Finish();
m_device->FinishRenderingCurrentFrame();
m_device->StartRenderingCurrentFrame();
m_update->Start();

textureCreationDone.get_future().get();

JS:

function setup(externalTexturePromise, width, height, textureCreatedCallback) {
    externalTexturePromise.then((externalTexture) => {
        const outputTexture = engine.wrapNativeTexture(externalTexture);
        scene.activeCamera.outputRenderTarget = new BABYLON.RenderTargetTexture(
            "rt", { width, height }, scene,
            { colorAttachment: outputTexture, generateDepthBuffer: true, generateStencilBuffer: true }
        );
        textureCreatedCallback();
    });
}

After (sync — no promises, no callbacks, no extra frame pump):

C++:

auto externalTexture = Babylon::Plugins::ExternalTexture{d3d12Resource};

jsRuntime.Dispatch([&](Napi::Env env) {
    auto jsTexture = externalTexture.CreateForJavaScript(env);
    env.Global().Get("setup").As<Napi::Function>().Call({
        jsTexture,
        Napi::Value::From(env, width),
        Napi::Value::From(env, height),
    });
});

JS:

function setup(externalTexture, width, height) {
    const outputTexture = engine.wrapNativeTexture(externalTexture);
    scene.activeCamera.outputRenderTarget = new BABYLON.RenderTargetTexture(
        "rt", { width, height }, scene,
        { colorAttachment: outputTexture, generateDepthBuffer: true, generateStencilBuffer: true }
    );
}

Other ExternalTexture Changes

  • Fix numLayers bug: CreateForJavaScript passed hardcoded 1 for numLayers in Attach(). Changed to m_impl->NumLayers() so texture array metadata is preserved.
  • Thread safety: Added std::scoped_lock to Width(), Height(), Get(), and Update().
  • Simplified Update API: Removed layerIndex parameter — the full texture (all layers) is always updated.
  • Texture lifecycle: Replaced handle-based tracking with texture-object tracking (AddTexture/RemoveTexture/UpdateTextures).

New: External Texture Array Render Test

  • Tests.ExternalTexture.Render.cpp: Creates a 3-slice texture array (R/G/B), renders each slice via sampler2DArray shader to an external render target, verifies pixels.
  • tests.externalTexture.render.ts: JS test with GLSL shaders sampling from texture array.
  • RenderDoc.h/cpp: Optional GPU capture support (disabled by default).
  • Platform Utils: CreateTestTextureArrayWithData, CreateRenderTargetTexture, ReadBackRenderTarget (D3D11, Metal; stubs for D3D12/OpenGL).

Testing

All 7 unit tests pass on Win32 (RelWithDebInfo).

bghgary and others added 8 commits March 24, 2026 22:05
- Add Tests.ExternalTexture.Render.cpp: end-to-end test that renders a
  texture array through a ShaderMaterial to an external render target,
  verifying each slice (red, green, blue) via pixel readback.
- Add tests.externalTexture.render.ts: JS test with sampler2DArray shader.
- Add RenderDoc.h/cpp to UnitTests for optional GPU capture support.
- Add Utils helpers: CreateTestTextureArrayWithData, CreateRenderTargetTexture,
  ReadBackRenderTarget, DestroyRenderTargetTexture (D3D11, Metal, stubs for
  D3D12/OpenGL).
- Fix ExternalTexture_Shared.h: pass m_impl->NumLayers() instead of
  hardcoded 1 in Attach(), preserving texture array metadata.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove layerIndex parameter from Impl::Update declaration to match
the updated signature in ExternalTexture_Shared.h.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Migrate HeadlessScreenshotApp, StyleTransferApp, and PrecompiledShaderTest
from AddToContextAsync (promise-based) to CreateForJavaScript (synchronous).
This removes the extra frame pump and promise callbacks that were previously
required.

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

- Move RenderDoc.h/cpp to D3D11-only CMake block with HAS_RENDERDOC define
- Guard RenderDoc calls with HAS_RENDERDOC instead of WIN32
- Update StyleTransferApp and PrecompiledShaderTest to use CreateForJavaScript

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
On OpenGL, TextureT is unsigned int (not a pointer), so reinterpret_cast
fails. Add NativeHandleToUintPtr helper using if constexpr to handle both
pointer types (D3D11/Metal/D3D12) and integer types (OpenGL).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- RenderDoc.h/cpp now accept void* device instead of ID3D11Device*
- Move RenderDoc to WIN32 block (not D3D11-only) since it works with any API
- Fix OpenGL build: use NativeHandleToUintPtr helper for TextureT cast
- Add Linux support (dlopen librenderdoc.so)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move DestroyTestTexture after FinishRenderingCurrentFrame so bgfx::frame()
processes the texture creation command before the native resource is released.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary force-pushed the external-texture-render-test branch from 8d48bcb to bb4ec86 Compare March 25, 2026 16:13
bghgary and others added 11 commits March 25, 2026 09:13
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Ensure the JS startup dispatch completes before calling
deviceUpdate.Finish() and FinishRenderingCurrentFrame(). This
guarantees that bgfx::frame() processes the CreateForJavaScript
texture creation command, making the texture available for subsequent
render frames. The old async API had an implicit sync point
(addToContext.wait) that the new sync API lost.

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

- Rename CreateForJavaScriptWithTextureArray to CreateForJavaScript and
  use arraySize=1 since texture array rendering is covered by
  RenderTextureArray. The old test crashed on CI (STATUS_BREAKPOINT in
  bgfx when creating texture arrays via encoder on WARP).
- Revert two-step create+override approach back to single createTexture2D
  call with _external parameter (overrideInternal from JS thread doesn't
  work since the D3D11 resource isn't created until bgfx::frame).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CreateForJavaScript already exists in Tests.ExternalTexture.cpp,
causing a linker duplicate symbol error.

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

The D3D11-specific CreateForJavaScript test crashed on CI due to
bgfx assertions when calling createTexture2D with _external on
the encoder thread. The cross-platform CreateForJavaScript test
in Tests.ExternalTexture.cpp already covers this functionality.
The texture array rendering is covered by RenderTextureArray.

Also revert app startup ordering to Finish->Wait (matching the
pattern used by HeadlessScreenshotApp on master).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The bgfx callback's fatal() handler was silently calling debugBreak()
on DebugCheck assertions with no output, making CI crashes impossible
to diagnose. Now logs the file, line, error code and message to stderr
before breaking, so the assertion details appear in CI logs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add DISM/d3dconfig step to CI to enable D3D debug layer, which will
provide detailed D3D11 validation messages for the CreateShaderResourceView
E_INVALIDARG failure. Kept the _external createTexture2D path (reverted
the AfterRenderScheduler approach) so we can see the actual D3D debug
output that explains the SRV mismatch.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The bgfx _external texture path triggers E_INVALIDARG in
CreateShaderResourceView on CI's WARP D3D11 driver. The overrideInternal
alternative doesn't support full array textures (hardcodes ArraySize=1).
Since the _external path works on real GPUs, skip the render test on CI
via BABYLON_NATIVE_SKIP_RENDER_TESTS and keep the direct _external path.

Also adds D3D debug layer enablement to CI for future diagnostics, and
logs bgfx fatal errors to stderr before crashing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Switch from _external parameter (crashes on WARP) to create+overrideInternal
two-step approach. The overrideInternal path is compatible with WARP but
sets ArraySize=1 in the SRV, so the RenderTextureArray test (which needs
full array access) is skipped on CI. The render test works on real GPUs
where the _external path succeeds.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The overrideInternal call fires on AfterRenderScheduler after the first
bgfx::frame(). An additional frame pump ensures the native texture
backing is applied before the scene render.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove deleted Tests.ExternalTexture.D3D11.cpp from Install/Test/CMakeLists.txt
- Add extra frame pump after CreateForJavaScript in HeadlessScreenshotApp
  and StyleTransferApp so overrideInternal has time to apply the native
  texture backing before the first render.

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