Skip to content

Correct native render-target MSAA allocation - #1884

Open
bkaradzic-microsoft wants to merge 4 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:pr/native-msaa-allocation
Open

bkaradzic-microsoft wants to merge 4 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:pr/native-msaa-allocation

Conversation

@bkaradzic-microsoft

@bkaradzic-microsoft bkaradzic-microsoft commented Sep 17, 2026

Copy link
Copy Markdown
Member

Summary

  • Select the bgfx render-target/MSAA field instead of ORing its mutually exclusive values. Previously a requested 2x became 4x, and 8x became 16x, mismatching the generated depth attachment.
  • Detect render targets with the field mask and do not pass initial pixel data for sampled multisample resources. D3D11 forbids CPU initialization data for that storage.
  • Retain zero initialization for ordinary render targets; multisampled sampled storage must be initialized with a framebuffer clear.
  • Add native regression coverage for 1x/2x/4x/8x flag selection and matching color/depth framebuffers, plus supported sampled-MSAA allocations.

Independence

Rebased onto master (2a9dc944). No Babylon.js changes, protocol changes, dependency updates, fixture changes, or tolerance changes. This does not include the separate D24 format fallback in #1874.

Validation

Windows x64 / D3D11 / Chakra / RelWithDebInfo, using pinned upstream dependencies and published Babylon.js 9.21.2:

  • Native MSAA allocation regression passed.
  • Existing external-texture 1x and 4x render controls passed; 4x produced 288 partial-coverage pixels versus zero at 1x.

External-texture review follow-up

Commit b0ea3fc6 also applies exclusive RT/MSAA field selection to the D3D11, D3D12, and Metal external-texture paths through their shared sample-count helper. This addresses the suppressed finding in the earlier Copilot review; the independent BGFX_TEXTURE_MSAA_SAMPLE bit is preserved.

A new bounded native wrapping regression checks exact 1x/2x/4x metadata. Before the fix, its 2x case reports the 4x field; after the fix it passes. All four selected D3D11 tests pass: that regression, native MSAA allocation, and the unchanged external 1x/4x rendering controls. D3D12/Metal runtime coverage is left to their supported CI configurations; the new case respects the existing external/render/multisample skip settings.

Copilot AI lite review requested due to automatic review settings September 17, 2026 20:03

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

Gate unsupported MSAA test cases and apply corrected flag selection to external-texture paths.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Corrects native render-target MSAA flag selection and initialization behavior, with regression coverage.

Changes:

  • Selects mutually exclusive MSAA flags correctly.
  • Omits CPU initialization data for sampled multisample storage.
  • Adds native MSAA tests and registers them in CMake.
File summaries
File Summary
Plugins/NativeEngine/Source/NativeEngine.cpp Corrects render-target MSAA flag selection; external-texture paths still need the same fix.
Core/Graphics/Source/Texture.cpp Adjusts initialization behavior for multisample render targets.
Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp Adds MSAA allocation and framebuffer coverage; unsupported sample levels need to be skipped.
Apps/UnitTests/CMakeLists.txt Registers the new native test source.
Review details

Suppressed comments (2)

Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp:62

  • The capability probe does not use the flags that Texture::Create2D actually passes to bgfx: owned textures add BGFX_TEXTURE_BLIT_DST in createFlags above. A backend can therefore report this combination as valid here while rejecting the effective flags used by sampled.Create2D, making the test fail instead of skipping an unsupported allocation. Probe the effective flag set.
                if (!bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::RGBA8, sampledFlags))

Plugins/NativeEngine/Source/NativeEngine.cpp:1648

  • Please apply this field selection to the external-texture path as well. The D3D11/D3D12/Metal GetInfo implementations still OR BGFX_TEXTURE_RT with RenderTargetSamplesToBgfxMsaaFlag(...) (for example, ExternalTexture_D3D11.cpp:204-210), and those flags flow unchanged into Texture::Create2D. Thus wrapped sampled-MSAA resources still carry the same mutually exclusive RT/MSAA combination this change fixes here and can be interpreted as the next sample level. Centralize or reuse the same conditional selection for that shared path.
            // RT and the MSAA levels are values in the same field, not independent bits.
            const auto msaaFlag = RenderTargetSamplesToBgfxMsaaFlag(samples);
            flags |= msaaFlag != BGFX_TEXTURE_NONE ? msaaFlag : BGFX_TEXTURE_RT;
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • 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 Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp
@bkaradzic-microsoft
bkaradzic-microsoft requested review from CedricGuillemet, bghgary and ryantrem and a balanced review from Copilot September 18, 2026 01:26

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp Outdated
Comment thread Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp
Comment thread Core/Graphics/Source/Texture.cpp Outdated
@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

Addressed the suppressed external-texture MSAA finding from the earlier Copilot review in b0ea3fc as well. D3D11/D3D12/Metal now share field selection that does not OR the single-sample RT value into a multisample level. The new wrapping regression reproduces 2x being labeled as 4x before the fix and verifies exact 1x/2x/4x flags afterward; all four selected D3D11 allocation/rendering controls pass. This complements the effective-BLIT_DST capability-probe fix already published in a1523bb.

@bkaradzic-microsoft
bkaradzic-microsoft requested a balanced review from Copilot September 18, 2026 15:48

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Comment thread Apps/UnitTests/Source/Tests.ExternalTexture.Msaa.cpp
Comment thread Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp
Comment thread Apps/UnitTests/Source/Tests.ExternalTexture.Msaa.cpp
Comment thread Apps/UnitTests/Source/Tests.NativeEngine.Msaa.cpp
Comment thread Plugins/ExternalTexture/Source/ExternalTexture_Base.h
bkaradzic-microsoft and others added 4 commits September 18, 2026 12:28
Select the render-target sample-count field rather than ORing its values,
and avoid supplying CPU initialization data to sampled multisample storage.
Retain zero initialization for ordinary render targets.

Add native coverage for 1x/2x/4x/8x flags, matching color/depth attachments,
and sampled multisample allocations. Keep protocol and dependency pins
unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
Check the color and selected depth framebuffer flags before each sample
count allocation. Skip unsupported backends/combinations explicitly and
report unsupported sampled-storage cases instead of silently omitting them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
Guard invalid framebuffer destruction, fail stalled allocation tasks after
30 seconds without unwinding resources they may still reference, and probe
the effective texture creation flags. Spell out the sampled-MSAA mask test
while preserving ordinary resolve-texture initialization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
Apply the RT/MSAA field selection to D3D11, D3D12 and Metal external
textures through their shared sample-count helper. Do not OR single-sample
RT into a multisample field; preserve the independent MSAA sampling bit.

Add a bounded native wrapping regression for 1x/2x/4x metadata. The 2x
case reports the incorrect 4x field before the fix and passes afterward.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
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.

2 participants