Skip to content

Bump bgfx.cmake (TextureRegion blit/read, shader bin v12, Vulkan bindings) - #1857

Merged
bkaradzic-microsoft merged 8 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:update/bgfx-cmake-1a135db
Sep 1, 2026
Merged

Bump bgfx.cmake (TextureRegion blit/read, shader bin v12, Vulkan bindings)#1857
bkaradzic-microsoft merged 8 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:update/bgfx-cmake-1a135db

Conversation

@bkaradzic-microsoft

@bkaradzic-microsoft bkaradzic-microsoft commented Aug 31, 2026

Copy link
Copy Markdown
Member

Summary

Bumps bgfx.cmake to 9a569c775917e3dd2a8cfb8836aeddbe790a8deb (bgfx 0837ead via BabylonJS/bgfx#78 / bgfx.cmake#144) and adapts Babylon Native to the Graphics/shader packaging changes in that range.

API adaptations

  • Blit / read: encoder.blit / bgfx::blit take TextureRegion pairs; bgfx::readTexturebgfx::read(TextureRegion, data). Updated in FrameBuffer, DeviceImpl, NativeCapture, NativeEngine.
  • Shader binary v12: CreateBgfxShader writes BGFX_CHUNK_MAGIC_*S_VERSION 12, emits per-uniform texture meta (AppendUniformTextureMeta), and carries D3D SRV/UAV masks.
  • Canvas headers: embedded canvas shader headers bumped v11 → v12.
  • Depth formats: D3D depth RT prefers D32F → D24 → D24S8 (Android depth-only stays D24S8). Backbuffer Depth32 maps to D32F (D3D has no DSV for plain D32).
  • ShaderCache: CACHE_VERSION 4 → 5 (v12 binaries are not compatible with prior cache entries).

Vulkan packaging (shaderc / bgfx v12 binding model)

  • Shared VS/FS layoutBinding per sampler name (no per-stage compact remap that desynced UniformStages).
  • Image bindings start at 2; separate samplers at image + 16; UBOs stay 0/1.
  • AppendSamplersVulkan: separate_images, strip Texture suffix, set regIndex from binding.
  • Reject shaders with >16 distinct sampler textures (bgfx texture stage limit) with an explicit error.
  • Packaging helpers live in ShaderCompilerVulkan.cpp / common CreateBgfxShader sampler-set path (not #if VULKAN in Common).

Tests / CI

  • Stage-sampler tests: disjoint VS/FS names and partially shared names (D3D).
  • ShaderCompilation.VulkanRejectsMoreThanSixteenSamplers (compiled only when GRAPHICS_API=Vulkan): unexpected success fails outside the catch path (no false pass via a thrown "…>16…" message).
  • New Ubuntu_Clang_Vulkan CI job: GRAPHICS_API=Vulkan, lavapipe ICD, runs that gtest.
  • Minimal Vulkan RendererType / DeviceImpl_Vulkan / ExternalTexture_Vulkan stub / Helpers.Vulkan so the API can configure and link. ExternalTexture remains unimplemented (SKIP_EXTERNAL_TEXTURE_TESTS).

Upstream deps already merged

Test plan

  • Existing Win32 D3D11/D3D12 CI paths
  • Linux OpenGL UnitTests / validation (existing jobs)
  • Ubuntu_Clang_Vulkan green (build + VulkanRejectsMoreThanSixteenSamplers under lavapipe)
  • Spot-check motion blur / thin-instance scenarios if any residual bgfx behavior changes show up in playground validation

- Pin bgfx.cmake GIT_TAG to 1a135dbf (bgfx 3b85a1c): TextureRegion blit/read, shader bin v12
- Rewrite FrameBuffer/DeviceImpl/NativeCapture/NativeEngine blit and readTexture call sites for TextureRegion / bgfx::read
- ShaderCompilerCommon: package shaders as BGFX_SHADER_BIN_VERSION 12 (raw SRV/UAV masks + texComponent/texDimension/texFormat on uniforms)
- Canvas precompiled shaders: bump embedded headers from bin v11 to v12 (insert raw binding masks)

Local Win32 Debug validation: Nested BBG + the three previously failing motion-blur instance tests all pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
Copilot AI lite review requested due to automatic review settings August 31, 2026 17:50

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.

🔵 Needs a closer look

It changes shader binary packaging and core GPU blit/readback paths across multiple backends, which is high-impact and needs full CI/platform validation.

Pull request overview

Updates Babylon Native’s bgfx integration to a newer bgfx.cmake/bgfx revision and adapts engine/shader tooling to match breaking API and shader-binary format changes introduced in that range.

Changes:

  • Bump bgfx.cmake to 1a135dbf... (bgfx 3b85a1c) via CMakeLists.txt.
  • Update runtime code to use TextureRegion-based blit/read APIs (encoder->blit(...) and bgfx::read(TextureRegion, ...)).
  • Update shader binary emission to bgfx shader package v12 (including v12-required fields) and bump embedded Canvas precompiled shader headers to v12.
File summaries
File Description
Polyfills/Canvas/Source/Shaders/spirv/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/spirv/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/spirv/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/spirv/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/spirv/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/metal/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/metal/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/metal/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/metal/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/metal/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/glsl/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/glsl/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/glsl/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/glsl/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/glsl/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/essl/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/essl/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/essl/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/essl/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/essl/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxil/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxil/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxil/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxil/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxil/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxbc/vs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxbc/vs_fspass.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxbc/fs_nanovg_fill.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxbc/fs_gaussblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Polyfills/Canvas/Source/Shaders/dxbc/fs_boxblur.h Bump embedded shader binary header to v12 format (adds v12 fields).
Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp Emit bgfx shader binary v12 (raw SRV/UAV masks + per-uniform texture metadata).
Plugins/NativeEngine/Source/NativeEngine.cpp Update blit + readback paths to TextureRegion APIs (copy + readTexture pipeline).
Plugins/NativeCapture/Source/NativeCapture.cpp Update framebuffer readback blit to TextureRegion API.
Core/Graphics/Source/FrameBuffer.cpp Update FrameBuffer::Blit to TextureRegion API and preserve “whole texture” semantics.
Core/Graphics/Source/DeviceImpl.cpp Switch async readback to bgfx::read(TextureRegion, ...).
CMakeLists.txt Bump bgfx.cmake dependency tag to the targeted commit.
Review details

Suppressed comments (2)

Plugins/NativeEngine/Source/NativeEngine.cpp:2375

  • This ReadTexture blit fallback block is over-indented compared to the rest of the else-branch, which makes it look like it is nested differently than it actually is.
            // If the image needs to be cropped, the texture lacks the READ_BACK flag, or we are reading a
                        // specific cube-map face, blit to a temp 2D texture. bgfx::read addresses a whole mip of one
                        // slice via TextureRegion::z, but a cropped sub-rect still needs the blit path. Cube-face
                        // reads use srcZ = face index on the source region of that blit.
                        if (isCubeFace || x != 0 || y != 0 || width != mipWidth || height != mipHeight || (texture->Flags() & BGFX_TEXTURE_READ_BACK) == 0)

Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp:312

  • This BGFX_SHADER_BIN_VERSION/comment block is over-indented compared to the rest of CreateBgfxShader, which makes the shader-binary header layout harder to follow.
        // Must match BGFX_SHADER_BIN_VERSION in bgfx tools/shaderc/shaderc.cpp.
                // v12 requires raw SRV/UAV binding masks after the in/out hashes, and
                // uniform entries always carry texComponent/texDimension/texFormat (v8/v10).
                constexpr uint8_t BGFX_SHADER_BIN_VERSION{12};

  • Files reviewed: 36/36 changed files
  • Comments generated: 5
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread Plugins/NativeEngine/Source/NativeEngine.cpp Outdated
Comment thread Core/Graphics/Source/FrameBuffer.cpp Outdated
Comment thread Core/Graphics/Source/DeviceImpl.cpp Outdated
Comment thread Plugins/NativeCapture/Source/NativeCapture.cpp Outdated
Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp
Plain D32 is not a valid D3D11 depth render-target (bgfx maps it to
R24G8 with no DSV). Newer bgfx asserts in createTexture2D when
isTextureValid fails, which broke CI on "NME Shadow Map". Prefer D32F
for depth-only framebuffers when supported, otherwise fall back to
D24/D24S8; Android still always uses D24S8. Also fix over-indented
blit/read/shader packaging blocks flagged by Copilot review.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
UWP CI failed compiling bgfx dxgi.cpp: SetMaximumFrameLatency is not
on IDXGISwapChain1 (WinRT SwapChainI). Pulls BabylonJS/bgfx.cmake#143
/ BabylonJS/bgfx#77 which QI for IDXGISwapChain2 first.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

UWP-only remaining failure was a bgfx compile error from present-pacing:

dxgi.cpp: SetMaximumFrameLatency is not a member of IDXGISwapChain1

WinRT SwapChainI is IDXGISwapChain1; the method is on IDXGISwapChain2+.

Fixed upstream:

BN pin bumped in 02c0586.

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.

🟡 Changes recommended

The v12 shader package selects bgfx’s new Vulkan binding model without emitting its required shifted bindings.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 36/36 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp Outdated
bkaradzic-microsoft and others added 2 commits August 31, 2026 13:53
v12 selects bgfx's new Vulkan binding model (m_oldBindingModel=false),
which computes sampler stage as regIndex - kSpirvBindShift (2). BN still
wrote regIndex=0 and left SPIR-V image/sampler bindings unshifted, so a
sampler could index m_bindInfo[65534].

- Rewrite glslang AST bindings to match shaderc (UBO 0/1, images at 2+i,
  samplers at image+16)
- Package separate_images with regIndex = image binding
- Strip the SplitSamplers "Texture" suffix and map UniformStages to the
  unshifted bgfx sampler slot

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
Pulls BabylonJS/bgfx.cmake#144 / bgfx#78. bgfx defines DXGI IIDs locally
instead of linking dxguid.lib; the UWP frame-latency QI path referenced
IID_IDXGISwapChain2 without a local GUID, breaking all MSVC app links
(LNK2001).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601

@bghgary bghgary 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.

[Reviewed by Copilot on behalf of @bghgary]

Concerns inline.

Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerVulkan.cpp Outdated
Comment thread Plugins/NativeEngine/Source/NativeEngine.cpp
Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp Outdated
- Preserve SamplerSplitter shared layoutBinding when applying bgfx
  Vulkan shifts (do not compact per-stage)
- Map BackBufferDepthStencilFormat::Depth32 to D32F (D32 has no DSV
  on D3D11/12 in current bgfx)
- Bump ShaderCache CACHE_VERSION 4 -> 5 for shader bin v12 packaging

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
@bkaradzic-microsoft bkaradzic-microsoft changed the title Bump bgfx.cmake to 1a135db (TextureRegion blit/read + shader bin v12) Bump bgfx.cmake (TextureRegion blit/read, shader bin v12, Vulkan bindings) Aug 31, 2026

@bghgary bghgary 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.

[Reviewed by Copilot on behalf of @bghgary]

Concern and nits inline.

Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerVulkan.cpp
Comment thread Core/Graphics/Source/DeviceImpl.cpp Outdated
Comment thread Plugins/ShaderCache/Source/ShaderCacheImpl.cpp Outdated
Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerCommon.cpp Outdated
bkaradzic-microsoft and others added 2 commits September 1, 2026 10:05
- Reject Vulkan programs with >16 distinct sampler textures (bgfx stage limit)
- Move Vulkan sampler packaging out of ShaderCompilerCommon into
  ShaderCompilerVulkan (CreateBgfxShader takes SamplerResourceSet + appender)
- Align Depth32/D32F case and ShaderCache CACHE_VERSION comments
- Add Disjoint/PartiallyShared stage-sampler compile tests; Vulkan-only
  >16 rejection test when BABYLON_NATIVE_GRAPHICS_API_VULKAN

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
@bkaradzic-microsoft
bkaradzic-microsoft enabled auto-merge (squash) September 1, 2026 17:23
@bkaradzic-microsoft
bkaradzic-microsoft requested review from bghgary and removed request for CedricGuillemet and ryantrem September 1, 2026 18:37
@bkaradzic-microsoft
bkaradzic-microsoft merged commit 9994111 into BabylonJS:master Sep 1, 2026
34 checks passed
bkaradzic-microsoft added a commit to bkaradzic-microsoft/BabylonNative that referenced this pull request Sep 1, 2026
Resolve conflicts keeping shotgun fork pins and particle/depth/readback work:
- CMakeLists: bkaradzic-microsoft/* @ shotgun (bgfx.cmake already carries bump)
- Texture: SetMetadata + Attach(ownsHandle) for sampleable depth ownership
- ShaderCompiler: dual CreateBgfxShader (v12) + CreateBgfxComputeShader @ v12 with raw SRV/UAV masks
- NativeEngine readTexture: TextureRegion blit API + D3D y-flip/crop
- Framebuffer depth: D32F/D24/D24S8 selection inside shotgun requestDepthStencil path
@bghgary

bghgary commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

[Commented by Copilot on behalf of @bghgary]

The Vulkan sampler-limit regression test merged with two gaps: no CI job configures GRAPHICS_API=Vulkan, so it is compiled out everywhere; and an unexpectedly ready effect throws "expected compile failure for >16 samplers" inside the same try, so the catch plus "16" assertion treats success as the expected rejection. Please make unexpected success fail outside the caught path and add a Vulkan CI configuration that runs the test.

@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

Addressed:

  1. False pass on unexpected successVulkanRejectsMoreThanSixteenSamplers now records unexpected_success / threw / not_ready without throwing a “>16” string inside the try. Unexpected success fails with ASSERT_NE(status, "unexpected_success"); the “16” check only runs on a real rejection path (JS throw, not-ready, or native unhandled exception).

  2. Vulkan CI — added Ubuntu_Clang_Vulkan (GRAPHICS_API=Vulkan, Mesa lavapipe, gtest filter ShaderCompilation.VulkanRejectsMoreThanSixteenSamplers). Also added the minimal Graphics/ExternalTexture/UnitTest Vulkan stubs needed so that configuration can configure and link (ExternalTexture still skipped).

Pushed in 79b363c.

@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

#1857 was already merged when this landed on the fork branch. Follow-up is in a new PR:

#1859

That PR has:

  1. Test fix so unexpected success no longer false-passes via a thrown “>16” string inside the catch path
  2. Ubuntu_Clang_Vulkan CI (GRAPHICS_API=Vulkan + lavapipe) running ShaderCompilation.VulkanRejectsMoreThanSixteenSamplers
  3. Minimal Vulkan Graphics/ExternalTexture/UnitTest stubs so the job can configure and link

bkaradzic-microsoft added a commit to bkaradzic-microsoft/BabylonNative that referenced this pull request Sep 1, 2026
Master BabylonJS#1857 changed encoder.blit to TextureRegion pairs; Context.cpp still
used the old 14-arg form and broke CI after merging master.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
bkaradzic-microsoft added a commit to bkaradzic-microsoft/BabylonNative that referenced this pull request Sep 2, 2026
Master BabylonJS#1857 changed encoder.blit to TextureRegion pairs; Context.cpp still
used the old 14-arg form and broke CI after merging master.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
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.

3 participants