Repository navigation
Conversation
The GPU tiers paint every draw in one colour, read from the brush at (0, 0), so a linear gradient handed to them rendered as a solid block of its first stop: a colour bar came out as one colour. tryGPUFill and tryGPUStroke now return ErrFallbackToCPU when the paint side they draw is not a single colour. The CPU samples the brush per pixel, and doFill/doStroke flush pending GPU work before it draws, so draw order is kept. Solid draws still go to the GPU.
There was a problem hiding this comment.
Right fix for now. Gradients drop to the CPU instead of painting the first stop. Once CI is green this is good to go from my side.
There was a problem hiding this comment.
Right fix, and the conservative direction is the correct one: a gradient that comes out as its first stop is a wrong picture, while a gradient that takes the CPU path is merely slower.
I checked the parts that decide whether this is safe:
ErrFallbackToCPUfromtryGPUFill/tryGPUStrokereachesdoFill/doStroke, which callflushGPUAccelerator()before the CPU rasterizer runs. That function flushes the per-context render context when there is one and the global accelerator otherwise, so queued GPU draws are committed before the CPU paints — draw order is kept, which is the part that could silently break.gpuPaintsOneColouris right to accept the inline solid (IsFillSolid) and to assertgg.SolidBrushby type. A custom brush that happens to be one colour still falls to the CPU, which is the correct default for a decision made from a type.- The guard runs before
setupGPUMask, so a fallback draw does not upload a mask it will not use.
The tests are what I want from this kind of change: a fake accelerator that claims everything and does nothing, so an unwanted GPU hand-off shows up as a missing draw, plus pixel assertions that the gradient is actually painted left-to-right. That is stronger than asserting the call count alone.
Nothing to change. Approving on the code; the September CI runs need a fresh push before merge.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Problem
With a GPU accelerator registered, a gradient fill comes out as one flat colour: the gradient's first stop.
The GPU tiers paint every draw in a single colour.
getFillColorFromPaint/getStrokeColorFromPaint(ininternal/gpu/sdf_gpu.go) take the inline solid colour, and for any other brush they fall back toColorAt(0, 0). So aLinearGradientBrushhanded toFillShape,FillPath,StrokeShapeorStrokePathis painted as one solid block. Nothing intryGPUFillortryGPUStrokechecks the brush before the accelerator gets it.Fix
tryGPUFillandtryGPUStrokenow returnErrFallbackToCPUwhen the paint side they draw is not a single colour. The CPU rasterizer samples the brush per pixel.doFillanddoStrokealready flush pending GPU work before a CPU draw, so draw order is kept.Solid draws are unaffected. That covers an inline solid colour, a
SolidBrush, and the default brush.Minimal example
In the wild: figure
We hit this in figure, a grammar-of-graphics plotting library for Go that renders through gg. On the GPU tier, its colour bars are drawn as gradient fills and came out as a solid block of the ramp's first colour.
The chart below is a heatmap of a device's gain with isolines, rendered with figure's GPU tier (
github.com/timzifer/figure/backend/gg/gpu):figure builds against our fork (timzifer/gg, tag
v0.52.6-figure.3) until this lands upstream.Tests
New
gradient_gpu_fallback_test.goregisters an accelerator that claims every operation, counts what reaches it and paints nothing:TestGradientFillsAreLeftToTheCPU: a gradient fill must not reach the accelerator, and the painted pixels must actually go from red to blue. Without the fix, the fill reaches the accelerator and nothing is painted.TestGradientStrokesAreLeftToTheCPU: the same rule for strokes.TestSolidFillsStillReachTheGPU: solid fills still go to the accelerator.go test -race ./...andgolangci-lint run --timeout=5mpass (Windows amd64, Go 1.25.3).Possible follow-up
Real gradient support in the GPU tiers (per-vertex or per-fragment colour) would keep these draws on the GPU. This PR only makes them correct.