internal/scene: extract compute shader encoding to a separate package

We're about to encode clip.Paths with the format compatible with the
compute renderer. This change extracts the encoding to a re-usable
package.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-03-11 09:21:24 +01:00
parent fdfa481082
commit 9366fce0f3
2 changed files with 146 additions and 95 deletions
+125
View File
@@ -0,0 +1,125 @@
// SPDX-License-Identifier: Unlicense OR MIT
// Package scene encodes and decodes graphics commands in the format used by the
// compute renderer.
package scene
import (
"image/color"
"math"
"gioui.org/f32"
)
type Command [sceneElemSize / 4]uint32
const sceneElemSize = 36
// GPU commands from scene.h
const (
elemNop = iota
elemStrokeLine
elemFillLine
elemStrokeQuad
elemFillQuad
elemStrokeCubic
elemFillCubic
elemStroke
elemFill
elemLineWidth
elemTransform
elemBeginClip
elemEndClip
elemFillImage
)
func Line(start, end f32.Point, stroke bool, flags uint32) Command {
tag := uint32(elemFillLine)
if stroke {
tag = elemStrokeLine
}
return Command{
0: flags<<16 | tag,
1: math.Float32bits(start.X),
2: math.Float32bits(start.Y),
3: math.Float32bits(end.X),
4: math.Float32bits(end.Y),
}
}
func Quad(start, ctrl, end f32.Point, stroke bool) Command {
tag := uint32(elemFillQuad)
if stroke {
tag = elemStrokeQuad
}
return Command{
0: tag,
1: math.Float32bits(start.X),
2: math.Float32bits(start.Y),
3: math.Float32bits(ctrl.X),
4: math.Float32bits(ctrl.Y),
5: math.Float32bits(end.X),
6: math.Float32bits(end.Y),
}
}
func Transform(m f32.Affine2D) Command {
sx, hx, ox, hy, sy, oy := m.Elems()
return Command{
0: elemTransform,
1: math.Float32bits(sx),
2: math.Float32bits(hy),
3: math.Float32bits(hx),
4: math.Float32bits(sy),
5: math.Float32bits(ox),
6: math.Float32bits(oy),
}
}
func LineWidth(width float32) Command {
return Command{
0: elemLineWidth,
1: math.Float32bits(width),
}
}
func Stroke(col color.RGBA) Command {
return Command{
0: elemStroke,
1: uint32(col.R)<<24 | uint32(col.G)<<16 | uint32(col.B)<<8 | uint32(col.A),
}
}
func BeginClip(bbox f32.Rectangle) Command {
return Command{
0: elemBeginClip,
1: math.Float32bits(bbox.Min.X),
2: math.Float32bits(bbox.Min.Y),
3: math.Float32bits(bbox.Max.X),
4: math.Float32bits(bbox.Max.Y),
}
}
func EndClip(bbox f32.Rectangle) Command {
return Command{
0: elemEndClip,
1: math.Float32bits(bbox.Min.X),
2: math.Float32bits(bbox.Min.Y),
3: math.Float32bits(bbox.Max.X),
4: math.Float32bits(bbox.Max.Y),
}
}
func Fill(col color.RGBA) Command {
return Command{
0: elemFill,
1: uint32(col.R)<<24 | uint32(col.G)<<16 | uint32(col.B)<<8 | uint32(col.A),
}
}
func FillImage(index int) Command {
return Command{
0: elemFillImage,
1: uint32(index),
}
}