Skip to content

Keep DynamicTexture invertY and premulAlpha when rebuilding after a context loss - #19167

Merged
deltakosh merged 2 commits into
BabylonJS:masterfrom
vladosskins178:fix/dynamic-texture-rebuild-invert-y
Nov 16, 2026
Merged

deltakosh merged 2 commits into
BabylonJS:masterfrom
vladosskins178:fix/dynamic-texture-rebuild-invert-y

Conversation

@vladosskins178

Copy link
Copy Markdown
Contributor

Fixes #19164

A DynamicTexture updated with invertY = false comes back flipped after a context loss and restore.

Cause: on restore, InternalTexture._rebuild() re-uploads _dynamicTextureSource with the stored invertY and _premulAlpha. Then DynamicTexture._rebuild() calls update() with no arguments, which uploads again with invertY = true and premulAlpha = false and overwrites the correct upload.

Fix: _rebuild() passes the invertY and _premulAlpha that the engine recorded on the internal texture during the last update(). A texture that was never updated keeps the old behavior (update() defaults). AdvancedDynamicTexture goes through the same _rebuild(), so applyYInversionOnUpdate = false / premulAlpha now survive a restore too.

Tests: new dynamicTexture.rebuild.test.ts (NullEngine, updateDynamicTexture spied):

  • last update (false, true) → rebuild uploads (false, true) (fails on master: uploads true, false)
  • last update (true) → rebuild uploads (true, false)
  • never updated → rebuild uses the update() defaults

Unit tests for Materials/Textures, Engines and gui: 74 files, 752 passed. eslint, prettier and tsc -p packages/dev/core/tsconfig.build.json are clean.

Checked in a browser: Chromium (WebGL2, SwiftShader), the gradient repro from the issue on a plane, 9.35.0 from the CDN, WEBGL_lose_context lose/restore, reading the top pixel:

  • unpatched: before [5,0,249], after restore [250,0,6] (flipped)
  • with this _rebuild(): before [5,0,249], after restore [5,0,249]
  • with update(true) both versions stay the same before and after.

…ontext loss

InternalTexture._rebuild re-uploads the dynamic texture source with the
stored invertY/premulAlpha, but DynamicTexture._rebuild then called
update() with no arguments, which uploads again with invertY = true and
premulAlpha = false. A texture updated with invertY = false came back
flipped after a context restore (WebGL and WebGPU).

Reuse the values the internal texture recorded on the last update().
A texture that was never updated keeps the old behavior.

Fixes BabylonJS#19164
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@deltakosh deltakosh self-assigned this Nov 15, 2026

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

Hello @vladosskins178,

The fix is right. Both the WebGL and WebGPU updateDynamicTexture store _dynamicTextureSource, _premulAlpha and invertY on the internal texture, so replaying them in _rebuild() matches what InternalTexture._rebuild() re-uploads. The _dynamicTextureSource guard keeps never-updated textures, and Native (which doesn't set it), on the old path.

One thing in the tests. "uses the update defaults when the texture was never updated" hard-codes update()'s defaults (true / false), and the second test does the same for the premulAlpha default. If those defaults ever change, the tests break without the behavior being wrong. For the never-updated case, please assert that _rebuild() calls update() with no arguments (spy on texture.update) instead of copying its defaults. For the second test, pass premulAlpha explicitly (update(true, false)) so the expectation comes from the test's own input.

Thanks!

…ults

The never-updated case now checks that _rebuild() calls update() with no
arguments, and the invertY = true case passes premulAlpha explicitly.
@vladosskins178

Copy link
Copy Markdown
Contributor Author

Thanks, done in f5d8125. The never-updated test now spies on texture.update and asserts _rebuild() calls it with no arguments, and the second test passes update(true, false) explicitly. I checked that the first test still fails on the old _rebuild() and the never-updated test fails if the _dynamicTextureSource guard is removed.

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

Complete read-only review of the context-restoration fix, backend metadata preservation, GUI integration, and regression tests found no actionable issues.

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

Re-reviewed the incremental change at f5d8125.

Both test points are addressed: the never-updated case now asserts _rebuild() calls update() with no arguments, and the second test passes premulAlpha explicitly, so every expectation comes from the test's own inputs. The fix itself is unchanged and still correct. Approving.

Thanks!

@deltakosh
deltakosh enabled auto-merge (squash) November 15, 2026 19:22
@RaananW

RaananW commented Nov 16, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
5 pipeline(s) were filtered out due to trigger conditions.

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Snapshot stored with reference name:
refs/pull/19167/merge

Test environment:
https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/19167/merge/index.html

To test a playground add it to the URL, for example:

https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/19167/merge/index.html#WGZLGJ#4600

Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves):

https://playground.babylonjs.com/?snapshot=refs/pull/19167/merge
https://sandbox.babylonjs.com/?snapshot=refs/pull/19167/merge
https://gui.babylonjs.com/?snapshot=refs/pull/19167/merge
https://nme.babylonjs.com/?snapshot=refs/pull/19167/merge

To test the snapshot in the playground with a playground ID add it after the snapshot query string:

https://playground.babylonjs.com/?snapshot=refs/pull/19167/merge#BCU1XR#0

If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools.

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

🟢 Memory Leak Test Results

4 passed, 0 leaked out of 4 scenarios

🟢 All memory leak tests passed — no leaks detected.

Passed Scenarios (4)
Scenario Package
Core Playground #2FDQT5#1508 @babylonjs/core
Core Playground #T90MQ4#14 @babylonjs/core
Core Playground #8EDB5N#2 @babylonjs/core
Core Playground #LL5BIQ#636 @babylonjs/core

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@bjsplat

bjsplat commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

⚡ Performance Test Results

🟢 All performance tests passed — no regressions detected.

@deltakosh
deltakosh merged commit 3ca95e2 into BabylonJS:master Nov 16, 2026
28 checks passed
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.

DynamicTexture is restored with the wrong invertY after context loss (_rebuild calls update() with default invertY)

5 participants