Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions internal/gpu/render_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,12 +1673,6 @@ func (s *GPURenderSession) buildStencilResourcesBatch(paths []StencilPathCommand
result := make([]*stencilCoverBuffers, len(paths))
for i := range paths {
cmd := &paths[i]
color := gg.RGBA{
R: float64(cmd.Color[0]),
G: float64(cmd.Color[1]),
B: float64(cmd.Color[2]),
A: float64(cmd.Color[3]),
}

// Destroy old pooled entry and create fresh buffers.
// Stencil paths vary wildly per frame (different vertex counts, colors),
Expand All @@ -1687,7 +1681,7 @@ func (s *GPURenderSession) buildStencilResourcesBatch(paths []StencilPathCommand
s.stencilBufPool[i].destroy()
s.stencilBufPool[i] = nil
}
bufs, err := s.stencilRenderer.createRenderBuffers(w, h, cmd.Vertices, cmd.CoverQuad, color)
bufs, err := s.stencilRenderer.createRenderBuffers(w, h, cmd.Vertices, cmd.CoverQuad, cmd.Color)
if err != nil {
// Clean up buffers created in this batch.
for j := 0; j < i; j++ {
Expand Down
55 changes: 55 additions & 0 deletions internal/gpu/stencil_alpha_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build !nogpu

package gpu

import (
"fmt"
"testing"

"github.com/gogpu/gg"
)

// Exercise the color all the way from the paint, through tessellation, to
// the cover shader's uniform. This requires no device; opaque colors alone
// would not detect the second alpha multiplication that darkened violins.
func TestStencilCoverPreservesPremultipliedColor(t *testing.T) {
for _, kind := range []drawCommandKind{drawCmdFillPath, drawCmdStrokePath} {
for _, alpha := range []float64{0, 0.125, 0.35, 0.5, 0.75, 1} {
t.Run(fmt.Sprintf("kind=%d/alpha=%g", kind, alpha), func(t *testing.T) {
path := gg.NewPath()
path.MoveTo(8, 8)
path.LineTo(56, 8)
path.LineTo(40, 24)
path.LineTo(56, 56)
path.LineTo(8, 56)
path.Close()
fill := gg.RGBA{R: 0.2, G: 0.5, B: 0.8, A: alpha}
stroke := gg.RGBA{R: 0.9, G: 0.4, B: 0.1, A: alpha}
paint := gg.NewPaint()
paint.SetFillBrush(gg.Solid(fill))
paint.SetStrokeBrush(gg.Solid(stroke))
paint.SetStroke(gg.Stroke{Width: 4, Cap: gg.LineCapButt, Join: gg.LineJoinMiter, MiterLimit: 10})
cmd := drawCommand{kind: kind, path: path, paint: *paint}
rc := &GPURenderContext{}
source := fill
if kind == drawCmdStrokePath {
source = stroke
rc.preTessellateStroke(&cmd)
} else {
rc.preTessellateFill(&cmd)
}
if cmd.stencilCmd == nil {
t.Fatal("the path did not reach stencil-then-cover")
}
uniform := makeCoverUniform(64, 64, cmd.stencilCmd.Color)
want := [4]float32{float32(source.R * alpha), float32(source.G * alpha), float32(source.B * alpha), float32(alpha)}
for i, expected := range want {
got := decodeFloat32(uniform[16+i*4 : 20+i*4])
if testAbs32(got-expected) > 1e-6 {
t.Errorf("cover channel %d = %g, want %g (one alpha multiplication)", i, got, expected)
}
}
})
}
}
}
2 changes: 1 addition & 1 deletion internal/gpu/stencil_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestMakeStencilFillUniform(t *testing.T) {
}

func TestMakeCoverUniform(t *testing.T) {
color := gg.RGBA{R: 1.0, G: 0.5, B: 0.25, A: 0.8}
color := [4]float32{0.8, 0.4, 0.2, 0.8} // already premultiplied
buf := makeCoverUniform(1920, 1080, color)
if len(buf) != coverUniformSize {
t.Fatalf("expected %d bytes, got %d", coverUniformSize, len(buf))
Expand Down
28 changes: 15 additions & 13 deletions internal/gpu/stencil_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ func (sr *StencilRenderer) RenderPath(target gg.GPURenderTarget, path *gg.Path,
}

// Create GPU buffers and bind groups for the render pass.
bufs, err := sr.createRenderBuffers(w, h, fanVerts, tess.CoverQuad(), color)
// RenderPath accepts straight alpha; the buffer builder shares the
// premultiplied color contract used by queued StencilPathCommands.
premul := [4]float32{
float32(color.R * color.A), float32(color.G * color.A),
float32(color.B * color.A), float32(color.A),
}
bufs, err := sr.createRenderBuffers(w, h, fanVerts, tess.CoverQuad(), premul)
if err != nil {
return err
}
Expand All @@ -268,9 +274,9 @@ func (sr *StencilRenderer) ensureReady(w, h uint32) error {
}

// createRenderBuffers creates vertex buffers, uniform buffers, and bind groups
// for a stencil-then-cover render pass.
// for a stencil-then-cover render pass. color is already premultiplied.
func (sr *StencilRenderer) createRenderBuffers(
w, h uint32, fanVerts []float32, coverQuad [12]float32, color gg.RGBA,
w, h uint32, fanVerts []float32, coverQuad [12]float32, color [4]float32,
) (*stencilCoverBuffers, error) {
b := &stencilCoverBuffers{
fanVertexCount: uint32(len(fanVerts) / 2), //nolint:gosec // len/2 fits uint32
Expand Down Expand Up @@ -518,21 +524,17 @@ func makeStencilFillUniform(w, h uint32) []byte {

// makeCoverUniform creates the 32-byte uniform buffer for the cover pass.
// Layout: viewport (vec2<f32>) + padding (vec2<f32>) + color (vec4<f32>, premultiplied alpha).
func makeCoverUniform(w, h uint32, color gg.RGBA) []byte {
func makeCoverUniform(w, h uint32, color [4]float32) []byte {
buf := make([]byte, coverUniformSize)
binary.LittleEndian.PutUint32(buf[0:4], math.Float32bits(float32(w)))
binary.LittleEndian.PutUint32(buf[4:8], math.Float32bits(float32(h)))
// padding bytes 8..15 are zero

// Premultiply alpha for GPU blending.
premulR := float32(color.R * color.A)
premulG := float32(color.G * color.A)
premulB := float32(color.B * color.A)
premulA := float32(color.A)
binary.LittleEndian.PutUint32(buf[16:20], math.Float32bits(premulR))
binary.LittleEndian.PutUint32(buf[20:24], math.Float32bits(premulG))
binary.LittleEndian.PutUint32(buf[24:28], math.Float32bits(premulB))
binary.LittleEndian.PutUint32(buf[28:32], math.Float32bits(premulA))
// The command already carries premultiplied RGB. Applying alpha here
// again would darken every translucent stencil fill and stroke.
for i, channel := range color {
binary.LittleEndian.PutUint32(buf[16+i*4:20+i*4], math.Float32bits(channel))
}
return buf
}

Expand Down
Loading