Correct gl_FragCoord Y orientation on D3D, Metal and Vulkan - #1840
Correct gl_FragCoord Y orientation on D3D, Metal and Vulkan#1840bkaradzic-microsoft wants to merge 6 commits into
Conversation
Babylon Native's shader model is "shader-visible coordinates are GL-logical; convert to physical at each sampler access". This is implemented by FlipSamplerCoordinatesTraverser (texture() v -> 1-v, texelFetch y -> h-1-y) and InvertYDerivativeOperandsTraverser (negate dFdy), which run for DXBC/DXIL/Metal/Vulkan but not OpenGL. gl_FragCoord was the one shader input left in physical space. D3D, Metal and Vulkan rasterize with a top-left origin while GL uses bottom-left, and BN does not flip geometry (ProcessShaderCoordinates only remaps depth). So for GL row y the hardware yields height - y - 0.5 instead of y + 0.5, i.e. gl_FragCoord.y arrives mirrored. Shaders using the symmetric "sample at my own position" pattern are unaffected because the physical/physical pairing is self-consistent. The mismatch only shows up where the row index itself is meaningful: prefix sums (iblCdfy), neighbour offsets, and copies into a differently-oriented target (copyTexture3DLayerToTexture). That is why 39 shaders reference gl_FragCoord but only a handful render incorrectly. Add FragCoordYFlipTraverser, which rewrites every gl_FragCoord read in the fragment stage to vec4(fc.x, targetHeight - fc.y, fc.z, fc.w). The correction is exactly `height - y` with no -1 term (see derivation above). Shaders that never read gl_FragCoord are left byte-for-byte unchanged. The target height comes from a new vec4 uniform, bnFragCoordTargetSize, declared as a linker object so MoveNonSamplerUniformsIntoStruct sweeps it into the "Frame" struct like every other uniform and it is emitted by name into the bgfx uniform table. NativeEngine sets it in DrawInternal from the bound framebuffer's dimensions. bgfx's predefined u_viewRect is deliberately not used: it is narrowed to the viewport by FrameBuffer::SetBgfxViewPortAndScissor whenever one is set, whereas gl_FragCoord is relative to the whole render target. FlipFragCoordY must run before ChangeUniformTypes / MoveNonSamplerUniformsIntoStruct so the uniform is collected with the rest. A fresh replacement subtree is built per occurrence rather than reusing MakeReplacements, which maps one node per symbol name and would give that node multiple parents - something later traversers do not expect. OpenGL is intentionally left alone, as with the other flip traversers. Validated on D3D11: 149 tests validated with 0 pixel-diff failures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
Two render-and-readback tests in UnitTests, both gated off where the existing render tests are (D3D12, noop Metal device). FragCoordYIncreasesUpwards writes gl_FragCoord.y / height into a render target and checks the ramp is brightest at the top row, matching GL's bottom-left origin. Values come out as 253 / 126 / 2 for the top, middle and bottom rows of a 64-row target, exactly the (height - row - 0.5) / height ramp the correction is derived from. FragCoordAndUVAddressATextureIdentically samples one texture twice, once through the interpolated UVs of a full-screen quad and once through gl_FragCoord.xy / targetSize, and requires the two images to match. That is the addressing pattern used by order-independent transparency, TAA and screen space curvature, and it only holds if the gl_FragCoord correction and FlipSamplerCoordinatesTraverser compose to a no-op. Comparing the two addressing modes against each other rather than against the source pixels keeps the test independent of how createRawTexture orients its upload. Both fail without FlipFragCoordY: the first ramp inverts to 2 / 129 / 253 and the second renders vertically mirrored (255..3 against 3..255). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
There was a problem hiding this comment.
Pull request overview
This PR normalizes gl_FragCoord.y to OpenGL/WebGL’s bottom-left-origin convention on the non-OpenGL backends (D3D, Metal, Vulkan) by injecting an AST rewrite during shader compilation and supplying the render-target dimensions at draw time.
Changes:
- Add a new shader-compiler traverser to rewrite every fragment-stage
gl_FragCoordread to a Y-flipped equivalent using a new target-size uniform. - Populate the injected target-size uniform from the currently bound framebuffer dimensions in
NativeEngine::DrawInternal. - Add two render-and-readback unit tests to pin down
gl_FragCoord.yorientation and its composition with sampler coordinate flips.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Plugins/ShaderCompiler/Source/ShaderCompilerVulkan.cpp | Runs FlipFragCoordY in the Vulkan compilation pipeline before uniform transforms. |
| Plugins/ShaderCompiler/Source/ShaderCompilerMetal.cpp | Runs FlipFragCoordY in the Metal compilation pipeline before uniform transforms. |
| Plugins/ShaderCompiler/Source/ShaderCompilerDXIL.cpp | Runs FlipFragCoordY in the DXIL compilation pipeline before uniform transforms. |
| Plugins/ShaderCompiler/Source/ShaderCompilerDXBC.cpp | Runs FlipFragCoordY in the DXBC compilation pipeline before uniform transforms. |
| Plugins/ShaderCompiler/Source/ShaderCompilerTraversers.h | Declares and documents the new FlipFragCoordY traverser API. |
| Plugins/ShaderCompiler/Source/ShaderCompilerTraversers.cpp | Implements FragCoordYFlipTraverser, declares bnFragCoordTargetSize, and rewrites gl_FragCoord reads. |
| Core/Graphics/InternalInclude/Babylon/Graphics/BgfxShaderInfo.h | Introduces FRAGCOORD_TARGET_SIZE_UNIFORM_NAME constant for the injected uniform name. |
| Plugins/NativeEngine/Source/Program.h | Adds cached lookup accessor for the injected uniform’s UniformInfo. |
| Plugins/NativeEngine/Source/Program.cpp | Caches bnFragCoordTargetSize uniform info during program initialization. |
| Plugins/NativeEngine/Source/NativeEngine.cpp | Sets bnFragCoordTargetSize each draw based on the bound framebuffer size (when present). |
| Apps/UnitTests/Source/Tests.ShaderCompilation.FragCoord.cpp | Adds render/readback tests validating gl_FragCoord.y orientation and UV-vs-fragcoord addressing equivalence. |
| Apps/UnitTests/CMakeLists.txt | Adds the new test source file to the UnitTests build. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Matches Tests.ShaderCompilation.cpp and Tests.UniformPadding.cpp, and avoids relying on the caps property being configurable.
Helpers::ReadPixels is a plain glReadPixels on OpenGL, which returns the bottom scanline first, whereas the D3D11 path returns the top scanline first. The test asserted an absolute ramp direction over readback rows, so it encoded the D3D11 readback convention and failed on Linux even though gl_FragCoord.y was correct there. Compare normalized gl_FragCoord.y against the interpolated vUV.y written by the same fragment invocation instead. The quad maps uv.y to clip y, so the two ramps must agree on every backend regardless of readback row order, and a flipped gl_FragCoord.y still misses by the full range of the ramp. Renamed to FragCoordYMatchesInterpolatedUV to match what it now checks.
|
CI caught a real problem with the test, though not with the fix. Pushed 666e1ad. What failed: Why: the test, not the traverser. Worth stating plainly: Fix: the test now writes normalized Re-verified the negative control — with the and with it restored: So the reworked assertion still fails by the full range of the ramp when the fix is absent — it did not become weaker by becoming portable. |
|
Short answer: no — and I can now show why rather than just reporting that the candidates I tried didn't flip. I instrumented Result: the traverser is invoked constantly and matches nothing. Across a 37-test spread sampling the whole 720-test catalog, plus ~40 hand-picked tests covering every feature whose shaders reference The reason is that in
and the features that do use it unconditionally — OIT, TAA, curvature, volumetric, clustered lighting, the FrameGraph tests — abort before any shader is compiled, e.g.: Those depend on engine APIs added to Babylon.js after 9.15.0, so they fail during scene construction regardless of this change. Two consequences worth stating explicitly:
This is precisely why the change ships with the two GPU-readback unit tests: they exercise Once the pinned Babylon.js moves past 9.15.0 the OIT / TAA / curvature / clustered-lighting tests become the natural integration coverage for this, and I'm happy to follow up with a PR enabling them at that point. |
Clustered lighting, the FrameGraph tests and several prepass/SSAO tests construct scenes against engine APIs added after 9.15.0 and throw before any shader is compiled, so they cannot be validated on the pinned version.
|
Pushed three follow-up commits: the Babylon.js 9.21.2 bump, an engine fix the bump exposed, and the validation tests it unlocks. The bump exposed a real bug
Babylon.js 9.21 draws thin instances with render-self motion blur using Regression checkAll 302 previously-enabled tests, one process per test, on Win32 D3D11: 299 pass, 0 fail. The only non-passing are 53-55 (scissor), which crash identically on 9.15.0 — pre-existing on Windows/D3D11 and unrelated to this change. 9 tests enabledSeveral of these exclusions describe order-dependent behaviour, which a per-test sweep structurally cannot reproduce, so I also ran a single sequential process over indices 56-719: ran=256 passed=256 failed=0.
Two things I want to flag honestly1. Three of these can only be judged by CI. Their exclusion reasons are backend-specific and I have no way to reproduce them on a D3D11 host: 137 "fails on Linux (large diff)", 287 "fails to compile on desktop GL", and 299 OpenGL 2. Tests 321 and 323 are now marginal — 99.1% and 94.2% of their error budget (2.478% and 2.355% against 2.5%). Bit-identical across three runs, so not flaky, but that margin is unlikely to survive a different backend. The cause is visible in the render: Babylon Native leaves a soft motion-blur halo around objects where Babylon.js converges to zero velocity, so there is a residual gap in the motion-blur path beyond the instance-limit bug. I ruled out stale reference images (substituting Babylon.js's own PNGs gives identical diffs). Worth a follow-up issue; I did not want to hide it behind a raised Note that none of the newly enabled tests exercise |
These were excluded against Babylon.js 9.15.0 and now pass. Verified on
Win32 D3D11 both per-test in isolation and in a single sequential process
covering indices 56-719 (ran=256 passed=256 failed=0), since several of
these exclusions describe order-dependent behaviour that a per-test sweep
cannot reproduce.
137 Volumetric Light Scattering Post Process with Morph Targets exact
287 Prepass SSAO + particles exact
289 Prepass SSAO + instanced bones 0.012%
299 Prepass SSAO + GUI 1.064%
302 Prepass SSAO + highlight layer 0.018%
304 Prepass SSAO + on/off post-process exact
305 Prepass SSAO + thin instances 0.003%
306 Prepass SSAO + depth renderer 0.044%
363 Screen Space Reflections 2 1.315%
Three carried backend-specific exclusion reasons that cannot be reproduced
on a D3D11 host, so CI is the arbiter for them:
137 "Pixel comparison fails on Linux (large diff)"
287 "SSAO2 blur post-process shader fails to compile on desktop GL"
299 OpenGL "mediump float" compile failure in PrePassRenderer, plus an
order-dependent state leak that produced a ~6000 px diff right at the
2.5% threshold; it now measures 1.064% in sequential order.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae
e5e1799 to
b6bc698
Compare
|
Correction to my previous comment: the instance-limit commit I pushed here duplicated work that already exists in #1839, so I have dropped it and force-pushed. This PR now depends on #1839. The Babylon.js 9.21.2 bump regresses three tests that #1839 fixes:
Until #1839 merges, those three will fail here. They should not be worked around in this PR. For the record, my dropped commit and #1839 converged on the same guard independently ( #1839 also explains the residual halo I flagged, and it is a third, separate bug: The rest of this PR is unchanged: the |
Babylon Native's shader model is "shader-visible coordinates are GL-logical, converted to physical at each sampler access".
FlipSamplerCoordinatesTraverser(texturev->1-v, texelFetchy->h-1-y) andInvertYDerivativeOperandsTraverser(negatedFdy) implement that for DXBC/DXIL/Metal/Vulkan, and are skipped for OpenGL.gl_FragCoordwas the one shader input still left in physical space. D3D, Metal and Vulkan rasterize with a top-left origin while GL uses bottom-left, and Babylon Native does not flip geometry (ProcessShaderCoordinatesonly remaps depth), so for GL rowythe hardware yieldsheight - y - 0.5instead ofy + 0.5.gl_FragCoord.yarrives mirrored.Shaders that sample at their own position are unaffected, because the physical/physical pairing is self-consistent. The mismatch only shows up where the row index itself is meaningful: prefix sums (
iblCdfy), neighbour offsets, and copies into a differently-oriented target (copyTexture3DLayerToTexture). That is why 39 shaders referencegl_FragCoordbut only a handful render incorrectly.Change
FragCoordYFlipTraverserrewrites everygl_FragCoordread in the fragment stage tovec4(fc.x, targetHeight - fc.y, fc.z, fc.w). The correction is exactlyheight - y, with no-1term. Shaders that never readgl_FragCoordare left byte-for-byte unchanged.The height comes from a new
vec4uniform,bnFragCoordTargetSize, declared as a linker object soMoveNonSamplerUniformsIntoStructsweeps it into theFramestruct like every other uniform.NativeEnginesets it inDrawInternalfrom the bound framebuffer's dimensions.Notes:
u_viewRectis deliberately not reused:FrameBuffer::SetBgfxViewPortAndScissornarrows it to the viewport whenever one is set, whereasgl_FragCoordis relative to the whole render target.FlipFragCoordYmust run beforeChangeUniformTypes/MoveNonSamplerUniformsIntoStructso its uniform is collected with the rest.MakeReplacements, which maps one node per symbol name and would give that node multiple parents.Tests
Two render-and-readback tests in
UnitTests, gated off where the existing render tests are (D3D12, noop Metal device):FragCoordYIncreasesUpwardswritesgl_FragCoord.y / heightinto a render target and checks the ramp is brightest at the top row. On a 64-row target it reads 253 / 126 / 2 for the top, middle and bottom rows, exactly the(height - row - 0.5) / heightramp the correction is derived from.FragCoordAndUVAddressATextureIdenticallysamples one texture twice, once through the interpolated UVs of a full-screen quad and once throughgl_FragCoord.xy / targetSize, and requires the two images to match. This is the addressing pattern used by order-independent transparency, TAA and screen space curvature, and it holds only if thegl_FragCoordcorrection andFlipSamplerCoordinatesTraversercompose to a no-op.Both fail without the fix: the first ramp inverts to 2 / 129 / 253, and the second renders vertically mirrored (255..3 against 3..255).
Validation
UnitTestssuite on D3D11. The only failure is the pre-existingJavaScript.AllTextEncoderassertion, which is unrelated and also fails without this change.IBL Voxel Shadowing Right-Handed13.455% -> 13.392% andLeft-Handed14.995% -> 14.592% pixel difference.Risk
The blast radius is every shader that reads
gl_FragCoordon D3D/Metal/Vulkan, so this is worth close review even though the shipped shaders that change behaviour are few.Only DXBC was exercised on hardware. The DXIL, Metal and Vulkan call sites are the same one-line addition in the same position, but they are untested and would benefit from a run on those backends before merging.
No Playground validation test flips from failing to passing here. The tests that would exercise this most directly (order-independent transparency, TAA, screen space curvature, IBL voxel shadowing) are currently excluded for unrelated reasons, and the voxel tests additionally need a newer
babylonjsthan the pinned 9.15.0 to run at all. Hence the unit tests above, which pin the behaviour down independently of the npm version.