Skip to content

Fix nested canvas clipping and clipped clears - #1872

Open
bkaradzic-microsoft wants to merge 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:pr/canvas-clip-clear
Open

bkaradzic-microsoft wants to merge 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:pr/canvas-clip-clear

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

Summary

  • intersect rectangular Canvas clips with the active NanoVG scissor so nested, disjoint, translated, and restored clips retain parent bounds
  • implement clearRect with opaque destination-out coverage, independent of globalAlpha and filters, while restoring the prior drawing state
  • initialize empty NanoVG filter stacks and add GPU-readback regressions with existing render-test skip handling

Stack relationship

This is a focused correctness follow-up based directly on BabylonJS/BabylonNative@d44847a8 (master). It does not depend on or include the feature work from #1855 or the branding hardening from #1844, and it can merge independently. The changes use Canvas and DynamicTexture APIs already present on master.

Validation

  • cmake -G "Visual Studio 17 2022" -B build\Win32_x64 -A x64 -D BX_CONFIG_DEBUG=ON -D GRAPHICS_API=D3D11 -D BABYLON_DEBUG_TRACE=ON
  • cmake --build build\Win32_x64 --target UnitTests --config RelWithDebInfo -- /m:2
  • Old-production negative control with the new tests: 46 passing, 27 pending, 2 failing (nested clipping and clipped clear)
  • Fixed D3D11 run: UnitTests.exe --gtest_filter=JavaScript.All -> 48 passing, 27 pending, [ PASSED ] 1 test

Copilot AI lite review requested due to automatic review settings September 11, 2026 00:24

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

This PR fixes correctness issues in the Canvas2D NanoVG-backed implementation, specifically around nested clipping behavior and clearRect behavior under clipping/alpha/filters, and adds GPU readback regression tests to prevent regressions.

Changes:

  • Intersect Canvas rectangle clipping with the active NanoVG scissor to preserve parent clip bounds for nested clips.
  • Implement clearRect via DESTINATION_OUT coverage while forcing globalAlpha=1 and clearing filters, restoring prior state afterward.
  • Add GPU readback regression tests for nested clips and clipped clears, with skip handling wired from the native test runner.

Reviewed changes

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

Show a summary per file
File Description
Polyfills/Canvas/Source/nanovg/nanovg_filterstack.h Initializes filter stack count to avoid undefined state when creating “empty” stacks.
Polyfills/Canvas/Source/Context.cpp Updates clip handling to use scissor intersection and adjusts clearRect compositing to clear only clipped regions independent of alpha/filters.
Apps/UnitTests/Source/Tests.JavaScript.cpp Exposes a global flag to skip GPU canvas tests when render/external texture tests are disabled.
Apps/UnitTests/JavaScript/src/tests.javaScript.all.ts Adds GPU readback regression tests for nested clips and clipped clearRect.
Apps/UnitTests/JavaScript/dist/tests.javaScript.all.js Bundled output updated to include the new JS test cases.

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

Comment thread Polyfills/Canvas/Source/Context.cpp
Comment thread Polyfills/Canvas/Source/Context.cpp Outdated
Comment thread Apps/UnitTests/JavaScript/src/tests.javaScript.all.ts

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 5 changed files in this pull request and generated 1 comment.

Comment thread Apps/UnitTests/JavaScript/src/tests.javaScript.all.ts
@bkaradzic-microsoft

bkaradzic-microsoft commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

The Windows/Hermes failure occurred after a successful build: snippet requests returned HTTP 403 on that runner, so all 314 Playground cases failed during scene loading (three failed fetch attempts per case), before rendering.

The same commit passed all 314 cases in the Windows D3D11 and V8 jobs, and the failed snippet URLs now return HTTP 200 from a separate probe. No code or test changes are warranted by this evidence. The failed-only retry (attempt 2, job 103128300425) has now passed both Validation Tests and Unit Tests. All 34 checks are green on the same commit; no code or test changes were needed.

bkaradzic-microsoft and others added 2 commits September 14, 2026 12:45
Intersect rectangular clips with the existing NanoVG scissor so nested
clips preserve parent bounds and save/restore state. Clear clipped regions
with destination-out coverage while ignoring global alpha and filters.
Initialize empty filter stacks and add GPU-readback regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
Zero-initialize the filter element array because NanoVG copies the full
filter stack into state and draw calls. Clarify the existing asymmetric
anti-alias clip extension without changing its bounds.

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