Polyfills/Canvas: fix invisible text, font-load races, and blit ordering#1683
Merged
bkaradzic-microsoft merged 1 commit intoBabylonJS:masterfrom Apr 29, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes several NativeCanvas rendering issues around text visibility/metrics, font-load timing, style save/restore correctness, and canvas-to-texture copy ordering; also removes a redundant shader uniform by switching to bgfx’s predefined u_viewRect.
Changes:
- Canvas Context: add
EnsureFontsLoaded()and style save/restore stacking; adjust emptyfillStyledefault and improvemeasureTextbehavior. - MeasureText: zero-initialize NanoVG metric buffers to prevent garbage widths.
- Rendering pipeline: remove
u_viewSizeuniform usage across shaders/nanovg glue; adjust CopyTexture blit view-id scheduling viaDeviceContext::PeekNextViewId().
Reviewed changes
Copilot reviewed 36 out of 36 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| Polyfills/Canvas/Source/nanovg/nanovg_babylon.cpp | Removes u_viewSize uniform plumbing and per-pass uniform sets. |
| Polyfills/Canvas/Source/Shaders/vs_nanovg_fill.sc | Switches math from u_viewSize to predefined u_viewRect. |
| Polyfills/Canvas/Source/Shaders/vs_fspass.sc | Removes unused u_viewSize uniform. |
| Polyfills/Canvas/Source/Shaders/fs_nanovg_fill.sc | Removes unused u_viewSize uniform. |
| Polyfills/Canvas/Source/Shaders/fs_gaussblur.sc | Uses u_viewRect.zw for texel size (drops u_viewSize). |
| Polyfills/Canvas/Source/Shaders/fs_boxblur.sc | Uses u_viewRect.zw for texel size (drops u_viewSize). |
| Polyfills/Canvas/Source/Shaders/spirv/vs_fspass.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/spirv/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/metal/vs_nanovg_fill.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/metal/vs_fspass.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/metal/fs_gaussblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/metal/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/glsl/vs_nanovg_fill.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/glsl/fs_gaussblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/glsl/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/essl/vs_nanovg_fill.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/essl/fs_gaussblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/essl/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxil/vs_nanovg_fill.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxil/fs_gaussblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxil/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxbc/vs_nanovg_fill.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxbc/fs_gaussblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/Shaders/dxbc/fs_boxblur.h | Regenerated shader binary/header reflecting uniform changes. |
| Polyfills/Canvas/Source/MeasureText.cpp | Zero-initializes NanoVG bounds/metrics buffers before calls. |
| Polyfills/Canvas/Source/Context.h | Adds EnsureFontsLoaded() declaration and style stack storage. |
| Polyfills/Canvas/Source/Context.cpp | Implements font refresh, style stacking, fillStyle defaulting, and measureText fallback behavior. |
| Plugins/NativeEngine/Source/NativeEngine.cpp | Changes CopyTexture to use encoder->blit with a later view id. |
| Core/Graphics/Source/DeviceImpl.h | Exposes PeekNextViewId() on graphics implementation. |
| Core/Graphics/Source/DeviceImpl.cpp | Implements PeekNextViewId() via atomic load. |
| Core/Graphics/Source/DeviceContext.cpp | Adds DeviceContext::PeekNextViewId() forwarding call. |
| Core/Graphics/InternalInclude/Babylon/Graphics/DeviceContext.h | Adds PeekNextViewId() to the public DeviceContext API. |
Comments suppressed due to low confidence (1)
Polyfills/Canvas/Source/Context.cpp:621
SetFontFaceId()callsEnsureFontsLoaded(), but ifm_currentFontIdis-1because the requested family wasn't available whenctx.fontwas set, this function will still fall back tom_fonts.begin()even if the requested family has since become available. To actually resolve the font-load race, consider: whenm_currentFontId < 0, attempt to look upm_font.Familiy()inm_fontsafterEnsureFontsLoaded()and bind that id when present (otherwise fall back).
bool Context::SetFontFaceId()
{
EnsureFontsLoaded();
if (m_fonts.empty())
{
return false;
}
else if (m_currentFontId >= 0)
{
nvgFontFaceId(*m_nvg, m_currentFontId);
}
else
{
nvgFontFaceId(*m_nvg, m_fonts.begin()->second);
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several long-standing NativeCanvas bugs caused text to be invisible or mis-rendered, fonts loaded after Context creation to be ignored, and canvas-to-texture copies to capture stale/empty content when more than one canvas was flushed per frame.
Context (Polyfills/Canvas/Source/Context.{h,cpp}):
MeasureText (Polyfills/Canvas/Source/MeasureText.cpp):
Shaders (Polyfills/Canvas/Source/Shaders/*.sc and regenerated headers for dxbc/dxil/essl/glsl/metal/spirv):
NativeEngine + DeviceContext (Plugins/NativeEngine/Source/NativeEngine.cpp, Core/Graphics/...):