8000 fix(gpu): leave gradient fills and strokes to the CPU by timzifer · Pull Request #539 · gogpu/gg · GitHub
[go: up one dir, main page]

Skip to content

fix(gpu): leave gradient fills and strokes to the CPU - #539

Open
timzifer wants to merge 2 commits into
gogpu:mainfrom
timzifer:fix/gpu-gradient-cpu-fallback
Open

timzifer wants to merge 2 commits into
gogpu:mainfrom
timzifer:fix/gpu-gradient-cpu-fallback

Conversation

@timzifer
Copy link
Copy Markdown
Contributor

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 (in internal/gpu/sdf_gpu.go) take the inline solid colour, and for any other brush they fall back to ColorAt(0, 0). So a LinearGradientBrush handed to FillShape, FillPath, StrokeShape or StrokePath is painted as one solid block. Nothing in tryGPUFill or tryGPUStroke checks the brush before the accelerator gets it.

Fix

tryGPUFill and tryGPUStroke now return ErrFallbackToCPU when the paint side they draw is not a single colour. The CPU rasterizer samples the brush per pixel. doFill and doStroke already 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

dc := gg.NewContext(100, 40) // with github.com/gogpu/gg/gpu imported
grad := gg.NewLinearGradientBrush(0, 0, 100, 0).
	AddColorStop(0, gg.RGBA{R: 1, A: 1}).
	AddColorStop(1, gg.RGBA{B: 1, A: 1})
dc.SetFillBrush(grad)
dc.DrawRectangle(0, 0, 100, 40)
_ = dc.Fill()
_ = dc.FlushGPU()
// v0.52.5: solid red. With this PR: red to blue.

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):

GPU, v0.52.5 GPU, this PR CPU reference
gradient gpu before gradient gpu after gradient cpu reference

figure builds against our fork (timzifer/gg, tag v0.52.6-figure.3) until this lands upstream.

Tests

New gradient_gpu_fallback_test.go registers 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 ./... and golangci-lint run --timeout=5m pass (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.

< 8000 /div>
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.
@kolkov kolkov 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.

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.

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

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:

  • ErrFallbackToCPU from tryGPUFill/tryGPUStroke reaches doFill/doStroke, which call flushGPUAccelerator() 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.
  • gpuPaintsOneColour is right to accept the inline solid (IsFillSolid) and to assert gg.SolidBrush by 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
codecov Bot commented Nov 9, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kolkov kolkov added type: bug Something isn't working priority: high Important for next release effort: 2 Small, 1-4 hours status: review In code review labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort: 2 Small, 1-4 hours priority: high Important for next release status: review In code review type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0