Compare commits

..

6 Commits

Author SHA1 Message Date
Hajime Hoshi
cdb430b2a5 vector: reduce allocations 2024-08-10 05:35:29 +09:00
Hajime Hoshi
e8e458802d examples/vector: reduce allocations 2024-08-10 05:22:53 +09:00
Hajime Hoshi
071024e89f vector: reduce memory allocations in the utility functions 2024-08-10 05:11:46 +09:00
Hajime Hoshi
38b8ba5677 vector: lazy point calculation
This is a preparation for #2884.

Updates #2884
2024-08-10 04:09:39 +09:00
Hajime Hoshi
361da49887 .github/workflows: remove unnecessary environment variable
Updates #2944
2024-08-10 01:25:07 +09:00
Hajime Hoshi
a5235eea86 internal/graphicsdriver/opengl/gl: always prefer OpenGL ES to OpenGL
Closes #2944
2024-08-10 01:21:35 +09:00
6 changed files with 251 additions and 179 deletions

View File

@ -153,7 +153,7 @@ jobs:
if: runner.os == 'Linux' if: runner.os == 'Linux'
run: | run: |
sudo apt-get install libgles2-mesa-dev sudo apt-get install libgles2-mesa-dev
env EBITENGINE_GRAPHICS_LIBRARY=opengl EBITENGINE_OPENGL=es go test -shuffle=on -v -p=1 ./... env EBITENGINE_GRAPHICS_LIBRARY=opengl go test -shuffle=on -v -p=1 ./...
- name: go test (Windows) - name: go test (Windows)
if: runner.os == 'Windows' if: runner.os == 'Windows'
@ -168,7 +168,7 @@ jobs:
env GOARCH=386 EBITENGINE_DIRECTX=version=12 go test -shuffle=on -v ./... env GOARCH=386 EBITENGINE_DIRECTX=version=12 go test -shuffle=on -v ./...
- name: go test (Wasm) - name: go test (Wasm)
if: ${{ runner.os != 'macOS' }} if: runner.os != 'macOS'
run: | run: |
# Wasm tests don't work on macOS with the headless mode enabled, but the headless mode cannot be disabled in GitHub Actions (#2972). # Wasm tests don't work on macOS with the headless mode enabled, but the headless mode cannot be disabled in GitHub Actions (#2972).
env GOOS=js GOARCH=wasm cleanenv -remove-prefix GITHUB_ -remove-prefix JAVA_ -remove-prefix PSModulePath -remove-prefix STATS_ -remove-prefix RUNNER_ -- go test -shuffle=on -v ./... env GOOS=js GOARCH=wasm cleanenv -remove-prefix GITHUB_ -remove-prefix JAVA_ -remove-prefix PSModulePath -remove-prefix STATS_ -remove-prefix RUNNER_ -- go test -shuffle=on -v ./...

5
doc.go
View File

@ -93,11 +93,6 @@
// The option "featurelevel" is valid only for DirectX 12. // The option "featurelevel" is valid only for DirectX 12.
// The possible values are "11_0", "11_1", "12_0", "12_1", and "12_2". The default value is "11_0". // The possible values are "11_0", "11_1", "12_0", "12_1", and "12_2". The default value is "11_0".
// //
// `EBITENGINE_OPENGL` environment variable specifies various parameters for OpenGL.
// You can specify multiple values separated by a comma. The default value is empty (i.e. no parameters).
//
// "es": Use OpenGL ES. Without this, OpenGL and OpenGL ES are automatically chosen.
//
// # Build tags // # Build tags
// //
// `ebitenginedebug` outputs a log of graphics commands. This is useful to know what happens in Ebitengine. In general, the // `ebitenginedebug` outputs a log of graphics commands. This is useful to know what happens in Ebitengine. In general, the

View File

@ -44,7 +44,17 @@ const (
screenHeight = 480 screenHeight = 480
) )
func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) { type Game struct {
counter int
aa bool
line bool
vertices []ebiten.Vertex
indices []uint16
}
func (g *Game) drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
var path vector.Path var path vector.Path
// E // E
@ -116,26 +126,24 @@ func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
path.LineTo(290, 20) path.LineTo(290, 20)
path.Close() path.Close()
var vs []ebiten.Vertex
var is []uint16
if line { if line {
op := &vector.StrokeOptions{} op := &vector.StrokeOptions{}
op.Width = 5 op.Width = 5
op.LineJoin = vector.LineJoinRound op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op) g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else { } else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil) g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
} }
for i := range vs { for i := range g.vertices {
vs[i].DstX = (vs[i].DstX + float32(x)) g.vertices[i].DstX = (g.vertices[i].DstX + float32(x))
vs[i].DstY = (vs[i].DstY + float32(y)) g.vertices[i].DstY = (g.vertices[i].DstY + float32(y))
vs[i].SrcX = 1 g.vertices[i].SrcX = 1
vs[i].SrcY = 1 g.vertices[i].SrcY = 1
vs[i].ColorR = 0xdb / float32(0xff) g.vertices[i].ColorR = 0xdb / float32(0xff)
vs[i].ColorG = 0x56 / float32(0xff) g.vertices[i].ColorG = 0x56 / float32(0xff)
vs[i].ColorB = 0x20 / float32(0xff) g.vertices[i].ColorB = 0x20 / float32(0xff)
vs[i].ColorA = 1 g.vertices[i].ColorA = 1
} }
op := &ebiten.DrawTrianglesOptions{} op := &ebiten.DrawTrianglesOptions{}
@ -150,10 +158,10 @@ func drawEbitenText(screen *ebiten.Image, x, y int, aa bool, line bool) {
// For simplicity, this example always uses FillRuleNonZero, whichever strokes or filling is done. // For simplicity, this example always uses FillRuleNonZero, whichever strokes or filling is done.
op.FillRule = ebiten.FillRuleNonZero op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op) screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
} }
func drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) { func (g *Game) drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) {
const unit = 16 const unit = 16
var path vector.Path var path vector.Path
@ -179,35 +187,33 @@ func drawEbitenLogo(screen *ebiten.Image, x, y int, aa bool, line bool) {
path.LineTo(unit, 4*unit) path.LineTo(unit, 4*unit)
path.Close() path.Close()
var vs []ebiten.Vertex
var is []uint16
if line { if line {
op := &vector.StrokeOptions{} op := &vector.StrokeOptions{}
op.Width = 5 op.Width = 5
op.LineJoin = vector.LineJoinRound op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op) g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else { } else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil) g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
} }
for i := range vs { for i := range g.vertices {
vs[i].DstX = (vs[i].DstX + float32(x)) g.vertices[i].DstX = (g.vertices[i].DstX + float32(x))
vs[i].DstY = (vs[i].DstY + float32(y)) g.vertices[i].DstY = (g.vertices[i].DstY + float32(y))
vs[i].SrcX = 1 g.vertices[i].SrcX = 1
vs[i].SrcY = 1 g.vertices[i].SrcY = 1
vs[i].ColorR = 0xdb / float32(0xff) g.vertices[i].ColorR = 0xdb / float32(0xff)
vs[i].ColorG = 0x56 / float32(0xff) g.vertices[i].ColorG = 0x56 / float32(0xff)
vs[i].ColorB = 0x20 / float32(0xff) g.vertices[i].ColorB = 0x20 / float32(0xff)
vs[i].ColorA = 1 g.vertices[i].ColorA = 1
} }
op := &ebiten.DrawTrianglesOptions{} op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op) screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
} }
func drawArc(screen *ebiten.Image, count int, aa bool, line bool) { func (g *Game) drawArc(screen *ebiten.Image, count int, aa bool, line bool) {
var path vector.Path var path vector.Path
path.MoveTo(350, 100) path.MoveTo(350, 100)
@ -223,37 +229,35 @@ func drawArc(screen *ebiten.Image, count int, aa bool, line bool) {
path.Arc(550, 100, 50, float32(theta1), float32(theta2), vector.Clockwise) path.Arc(550, 100, 50, float32(theta1), float32(theta2), vector.Clockwise)
path.Close() path.Close()
var vs []ebiten.Vertex
var is []uint16
if line { if line {
op := &vector.StrokeOptions{} op := &vector.StrokeOptions{}
op.Width = 5 op.Width = 5
op.LineJoin = vector.LineJoinRound op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op) g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else { } else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil) g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
} }
for i := range vs { for i := range g.vertices {
vs[i].SrcX = 1 g.vertices[i].SrcX = 1
vs[i].SrcY = 1 g.vertices[i].SrcY = 1
vs[i].ColorR = 0x33 / float32(0xff) g.vertices[i].ColorR = 0x33 / float32(0xff)
vs[i].ColorG = 0xcc / float32(0xff) g.vertices[i].ColorG = 0xcc / float32(0xff)
vs[i].ColorB = 0x66 / float32(0xff) g.vertices[i].ColorB = 0x66 / float32(0xff)
vs[i].ColorA = 1 g.vertices[i].ColorA = 1
} }
op := &ebiten.DrawTrianglesOptions{} op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op) screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
} }
func maxCounter(index int) int { func maxCounter(index int) int {
return 128 + (17*index+32)%64 return 128 + (17*index+32)%64
} }
func drawWave(screen *ebiten.Image, counter int, aa bool, line bool) { func (g *Game) drawWave(screen *ebiten.Image, counter int, aa bool, line bool) {
var path vector.Path var path vector.Path
const npoints = 8 const npoints = 8
@ -278,37 +282,28 @@ func drawWave(screen *ebiten.Image, counter int, aa bool, line bool) {
path.LineTo(screenWidth, screenHeight) path.LineTo(screenWidth, screenHeight)
path.LineTo(0, screenHeight) path.LineTo(0, screenHeight)
var vs []ebiten.Vertex
var is []uint16
if line { if line {
op := &vector.StrokeOptions{} op := &vector.StrokeOptions{}
op.Width = 5 op.Width = 5
op.LineJoin = vector.LineJoinRound op.LineJoin = vector.LineJoinRound
vs, is = path.AppendVerticesAndIndicesForStroke(nil, nil, op) g.vertices, g.indices = path.AppendVerticesAndIndicesForStroke(g.vertices[:0], g.indices[:0], op)
} else { } else {
vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil) g.vertices, g.indices = path.AppendVerticesAndIndicesForFilling(g.vertices[:0], g.indices[:0])
} }
for i := range vs { for i := range g.vertices {
vs[i].SrcX = 1 g.vertices[i].SrcX = 1
vs[i].SrcY = 1 g.vertices[i].SrcY = 1
vs[i].ColorR = 0x33 / float32(0xff) g.vertices[i].ColorR = 0x33 / float32(0xff)
vs[i].ColorG = 0x66 / float32(0xff) g.vertices[i].ColorG = 0x66 / float32(0xff)
vs[i].ColorB = 0xff / float32(0xff) g.vertices[i].ColorB = 0xff / float32(0xff)
vs[i].ColorA = 1 g.vertices[i].ColorA = 1
} }
op := &ebiten.DrawTrianglesOptions{} op := &ebiten.DrawTrianglesOptions{}
op.AntiAlias = aa op.AntiAlias = aa
op.FillRule = ebiten.FillRuleNonZero op.FillRule = ebiten.FillRuleNonZero
screen.DrawTriangles(vs, is, whiteSubImage, op) screen.DrawTriangles(g.vertices, g.indices, whiteSubImage, op)
}
type Game struct {
counter int
aa bool
line bool
} }
func (g *Game) Update() error { func (g *Game) Update() error {
@ -331,10 +326,10 @@ func (g *Game) Draw(screen *ebiten.Image) {
dst := screen dst := screen
dst.Fill(color.RGBA{0xe0, 0xe0, 0xe0, 0xff}) dst.Fill(color.RGBA{0xe0, 0xe0, 0xe0, 0xff})
drawEbitenText(dst, 0, 50, g.aa, g.line) g.drawEbitenText(dst, 0, 50, g.aa, g.line)
drawEbitenLogo(dst, 20, 150, g.aa, g.line) g.drawEbitenLogo(dst, 20, 150, g.aa, g.line)
drawArc(dst, g.counter, g.aa, g.line) g.drawArc(dst, g.counter, g.aa, g.line)
drawWave(dst, g.counter, g.aa, g.line) g.drawWave(dst, g.counter, g.aa, g.line)
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()) msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS())
msg += "\nPress A to switch anti-alias." msg += "\nPress A to switch anti-alias."

View File

@ -18,8 +18,6 @@ package gl
import ( import (
"fmt" "fmt"
"os"
"runtime"
"strings" "strings"
"github.com/ebitengine/purego" "github.com/ebitengine/purego"
@ -31,40 +29,10 @@ var (
) )
func (c *defaultContext) init() error { func (c *defaultContext) init() error {
var preferES bool
if runtime.GOOS == "android" {
preferES = true
}
if !preferES {
for _, t := range strings.Split(os.Getenv("EBITENGINE_OPENGL"), ",") {
switch strings.TrimSpace(t) {
case "es":
preferES = true
break
}
}
}
// TODO: Use multiple %w-s as of Go 1.20. // TODO: Use multiple %w-s as of Go 1.20.
var errors []string var errors []string
// Try OpenGL first. OpenGL is preferable as this doesn't cause context losses. // Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with OpenGL ES.
if !preferES {
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
// TODO: Should "libOpenGL.so.0" [1] and "libGLX.so.0" [2] be added? These were added as of GLFW 3.3.9.
// [1] https://github.com/glfw/glfw/commit/55aad3c37b67f17279378db52da0a3ab81bbf26d
// [2] https://github.com/glfw/glfw/commit/c18851f52ec9704eb06464058a600845ec1eada1
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGL = lib
return nil
}
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
}
}
// Try OpenGL ES.
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} { for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL) lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil { if err == nil {
@ -75,6 +43,20 @@ func (c *defaultContext) init() error {
errors = append(errors, fmt.Sprintf("%s: %v", name, err)) errors = append(errors, fmt.Sprintf("%s: %v", name, err))
} }
// Try OpenGL next.
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
// TODO: Should "libOpenGL.so.0" [1] and "libGLX.so.0" [2] be added? These were added as of GLFW 3.3.9.
// [1] https://github.com/glfw/glfw/commit/55aad3c37b67f17279378db52da0a3ab81bbf26d
// [2] https://github.com/glfw/glfw/commit/c18851f52ec9704eb06464058a600845ec1eada1
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGL = lib
return nil
}
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
}
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", ")) return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", "))
} }

View File

@ -31,6 +31,23 @@ const (
CounterClockwise CounterClockwise
) )
type opType int
const (
opTypeMoveTo opType = iota
opTypeLineTo
opTypeQuadTo
opTypeCubicTo
opTypeClose
)
type op struct {
typ opType
p1 point
p2 point
p3 point
}
func abs(x float32) float32 { func abs(x float32) float32 {
if x < 0 { if x < 0 {
return -x return -x
@ -48,16 +65,6 @@ type subpath struct {
closed bool closed bool
} }
func (s *subpath) currentPosition() (point, bool) {
if len(s.points) == 0 {
return point{}, false
}
if s.closed {
return point{}, false
}
return s.points[len(s.points)-1], true
}
func (s *subpath) pointCount() int { func (s *subpath) pointCount() int {
return len(s.points) return len(s.points)
} }
@ -91,15 +98,51 @@ func (s *subpath) close() {
// Path represents a collection of path subpathments. // Path represents a collection of path subpathments.
type Path struct { type Path struct {
ops []op
subpaths []*subpath subpaths []*subpath
} }
func (p *Path) ensureSubpaths() []*subpath {
// TODO: Probably it is better to avoid returning a slice since allocation is heavy.
// What about walkSubpaths(func(*subpath))?
if len(p.subpaths) > 0 || len(p.ops) == 0 {
return p.subpaths
}
var cur point
for _, op := range p.ops {
switch op.typ {
case opTypeMoveTo:
p.subpaths = append(p.subpaths, &subpath{
points: []point{op.p1},
})
cur = op.p1
case opTypeLineTo:
p.lineTo(op.p1)
cur = op.p1
case opTypeQuadTo:
p.quadTo(cur, op.p1, op.p2, 0)
cur = op.p2
case opTypeCubicTo:
p.cubicTo(cur, op.p1, op.p2, op.p3, 0)
cur = op.p3
case opTypeClose:
p.close()
cur = point{}
}
}
return p.subpaths
}
// MoveTo starts a new subpath with the given position (x, y) without adding a subpath, // MoveTo starts a new subpath with the given position (x, y) without adding a subpath,
func (p *Path) MoveTo(x, y float32) { func (p *Path) MoveTo(x, y float32) {
p.subpaths = append(p.subpaths, &subpath{ p.subpaths = p.subpaths[:0]
points: []point{ p.ops = append(p.ops, op{
{x: x, y: y}, typ: opTypeMoveTo,
}, p1: point{x: x, y: y},
}) })
} }
@ -107,22 +150,54 @@ func (p *Path) MoveTo(x, y float32) {
// and ends to the given position (x, y). // and ends to the given position (x, y).
// If p doesn't have any subpaths or the last subpath is closed, LineTo sets (x, y) as the start position of a new subpath. // If p doesn't have any subpaths or the last subpath is closed, LineTo sets (x, y) as the start position of a new subpath.
func (p *Path) LineTo(x, y float32) { func (p *Path) LineTo(x, y float32) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed { p.subpaths = p.subpaths[:0]
p.subpaths = append(p.subpaths, &subpath{ p.ops = append(p.ops, op{
points: []point{ typ: opTypeLineTo,
{x: x, y: y}, p1: point{x: x, y: y},
}, })
})
return
}
p.subpaths[len(p.subpaths)-1].appendPoint(point{x: x, y: y})
} }
// QuadTo adds a quadratic Bézier curve to the path. // QuadTo adds a quadratic Bézier curve to the path.
// (x1, y1) is the control point, and (x2, y2) is the destination. // (x1, y1) is the control point, and (x2, y2) is the destination.
func (p *Path) QuadTo(x1, y1, x2, y2 float32) { func (p *Path) QuadTo(x1, y1, x2, y2 float32) {
p.quadTo(point{x: x1, y: y1}, point{x: x2, y: y2}, 0) p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeQuadTo,
p1: point{x: x1, y: y1},
p2: point{x: x2, y: y2},
})
}
// CubicTo adds a cubic Bézier curve to the path.
// (x1, y1) and (x2, y2) are the control points, and (x3, y3) is the destination.
func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float32) {
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeCubicTo,
p1: point{x: x1, y: y1},
p2: point{x: x2, y: y2},
p3: point{x: x3, y: y3},
})
}
// Close adds a new line from the last position of the current subpath to the first position of the current subpath,
// and marks the current subpath closed.
// Following operations for this path will start with a new subpath.
func (p *Path) Close() {
p.subpaths = p.subpaths[:0]
p.ops = append(p.ops, op{
typ: opTypeClose,
})
}
func (p *Path) lineTo(pt point) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
p.subpaths = append(p.subpaths, &subpath{
points: []point{pt},
})
return
}
p.subpaths[len(p.subpaths)-1].appendPoint(pt)
} }
// lineForTwoPoints returns parameters for a line passing through p0 and p1. // lineForTwoPoints returns parameters for a line passing through p0 and p1.
@ -154,24 +229,13 @@ func crossingPointForTwoLines(p00, p01, p10, p11 point) point {
} }
} }
func (p *Path) currentPosition() (point, bool) { func (p *Path) quadTo(p0, p1, p2 point, level int) {
if len(p.subpaths) == 0 {
return point{}, false
}
return p.subpaths[len(p.subpaths)-1].currentPosition()
}
func (p *Path) quadTo(p1, p2 point, level int) {
if level > 10 { if level > 10 {
return return
} }
p0, ok := p.currentPosition()
if !ok {
p0 = p1
}
if isPointCloseToSegment(p1, p0, p2, 0.5) { if isPointCloseToSegment(p1, p0, p2, 0.5) {
p.LineTo(p2.x, p2.y) p.lineTo(p2)
return return
} }
@ -187,27 +251,17 @@ func (p *Path) quadTo(p1, p2 point, level int) {
x: (p01.x + p12.x) / 2, x: (p01.x + p12.x) / 2,
y: (p01.y + p12.y) / 2, y: (p01.y + p12.y) / 2,
} }
p.quadTo(p01, p012, level+1) p.quadTo(p0, p01, p012, level+1)
p.quadTo(p12, p2, level+1) p.quadTo(p012, p12, p2, level+1)
} }
// CubicTo adds a cubic Bézier curve to the path. func (p *Path) cubicTo(p0, p1, p2, p3 point, level int) {
// (x1, y1) and (x2, y2) are the control points, and (x3, y3) is the destination.
func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float32) {
p.cubicTo(point{x: x1, y: y1}, point{x: x2, y: y2}, point{x: x3, y: y3}, 0)
}
func (p *Path) cubicTo(p1, p2, p3 point, level int) {
if level > 10 { if level > 10 {
return return
} }
p0, ok := p.currentPosition()
if !ok {
p0 = p1
}
if isPointCloseToSegment(p1, p0, p3, 0.5) && isPointCloseToSegment(p2, p0, p3, 0.5) { if isPointCloseToSegment(p1, p0, p3, 0.5) && isPointCloseToSegment(p2, p0, p3, 0.5) {
p.LineTo(p3.x, p3.y) p.lineTo(p3)
return return
} }
@ -235,8 +289,8 @@ func (p *Path) cubicTo(p1, p2, p3 point, level int) {
x: (p012.x + p123.x) / 2, x: (p012.x + p123.x) / 2,
y: (p012.y + p123.y) / 2, y: (p012.y + p123.y) / 2,
} }
p.cubicTo(p01, p012, p0123, level+1) p.cubicTo(p0, p01, p012, p0123, level+1)
p.cubicTo(p123, p23, p3, level+1) p.cubicTo(p0123, p123, p23, p3, level+1)
} }
func normalize(p point) point { func normalize(p point) point {
@ -248,6 +302,26 @@ func cross(p0, p1 point) float32 {
return p0.x*p1.y - p1.x*p0.y return p0.x*p1.y - p1.x*p0.y
} }
func (p *Path) currentPosition() (point, bool) {
if len(p.ops) == 0 {
return point{}, false
}
op := p.ops[len(p.ops)-1]
switch op.typ {
case opTypeMoveTo:
return op.p1, true
case opTypeLineTo:
return op.p1, true
case opTypeQuadTo:
return op.p2, true
case opTypeCubicTo:
return op.p3, true
case opTypeClose:
return point{}, false
}
return point{}, false
}
// ArcTo adds an arc curve to the path. // ArcTo adds an arc curve to the path.
// (x1, y1) is the first control point, and (x2, y2) is the second control point. // (x1, y1) is the first control point, and (x2, y2) is the second control point.
func (p *Path) ArcTo(x1, y1, x2, y2, radius float32) { func (p *Path) ArcTo(x1, y1, x2, y2, radius float32) {
@ -362,7 +436,7 @@ func (p *Path) Arc(x, y, radius, startAngle, endAngle float32, dir Direction) {
p.LineTo(x0, y0) p.LineTo(x0, y0)
// Calculate the control points for an approximated Bézier curve. // Calculate the control points for an approximated Bézier curve.
// See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/beziers. // See https://learn.microsoft.com/en-us/previous-versions/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/beziers.
l := radius * float32(math.Tan(da/4)*4/3) l := radius * float32(math.Tan(da/4)*4/3)
var cx0, cy0, cx1, cy1 float32 var cx0, cy0, cx1, cy1 float32
if dir == Clockwise { if dir == Clockwise {
@ -379,10 +453,7 @@ func (p *Path) Arc(x, y, radius, startAngle, endAngle float32, dir Direction) {
p.CubicTo(cx0, cy0, cx1, cy1, x1, y1) p.CubicTo(cx0, cy0, cx1, cy1, x1, y1)
} }
// Close adds a new line from the last position of the current subpath to the first position of the current subpath, func (p *Path) close() {
// and marks the current subpath closed.
// Following operations for this path will start with a new subpath.
func (p *Path) Close() {
if len(p.subpaths) == 0 { if len(p.subpaths) == 0 {
return return
} }
@ -405,7 +476,7 @@ func (p *Path) AppendVerticesAndIndicesForFilling(vertices []ebiten.Vertex, indi
// TODO: Add tests. // TODO: Add tests.
base := uint16(len(vertices)) base := uint16(len(vertices))
for _, subpath := range p.subpaths { for _, subpath := range p.ensureSubpaths() {
if subpath.pointCount() < 3 { if subpath.pointCount() < 3 {
continue continue
} }
@ -486,12 +557,13 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
return vertices, indices return vertices, indices
} }
for _, subpath := range p.subpaths { var rects [][4]point
for _, subpath := range p.ensureSubpaths() {
if subpath.pointCount() < 2 { if subpath.pointCount() < 2 {
continue continue
} }
var rects [][4]point rects = rects[:0]
for i := 0; i < subpath.pointCount()-1; i++ { for i := 0; i < subpath.pointCount()-1; i++ {
pt := subpath.points[i] pt := subpath.points[i]

View File

@ -18,6 +18,7 @@ import (
"image" "image"
"image/color" "image/color"
"math" "math"
"sync"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@ -27,6 +28,18 @@ var (
whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image) whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
) )
var (
cachedVertices []ebiten.Vertex
cachedIndices []uint16
cacheM sync.Mutex
)
func useCachedVerticesAndIndices(fn func([]ebiten.Vertex, []uint16) (vs []ebiten.Vertex, is []uint16)) {
cacheM.Lock()
defer cacheM.Unlock()
cachedVertices, cachedIndices = fn(cachedVertices[:0], cachedIndices[:0])
}
func init() { func init() {
b := whiteImage.Bounds() b := whiteImage.Bounds()
pix := make([]byte, 4*b.Dx()*b.Dy()) pix := make([]byte, 4*b.Dx()*b.Dy())
@ -63,9 +76,12 @@ func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32,
path.LineTo(x1, y1) path.LineTo(x1, y1)
strokeOp := &StrokeOptions{} strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias) useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
} }
// DrawFilledRect fills a rectangle with the specified width and color. // DrawFilledRect fills a rectangle with the specified width and color.
@ -75,9 +91,12 @@ func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Co
path.LineTo(x, y+height) path.LineTo(x, y+height)
path.LineTo(x+width, y+height) path.LineTo(x+width, y+height)
path.LineTo(x+width, y) path.LineTo(x+width, y)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr, antialias) useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
} }
// StrokeRect strokes a rectangle with the specified width and color. // StrokeRect strokes a rectangle with the specified width and color.
@ -94,18 +113,24 @@ func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth floa
strokeOp := &StrokeOptions{} strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth strokeOp.Width = strokeWidth
strokeOp.MiterLimit = 10 strokeOp.MiterLimit = 10
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias) useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
} }
// DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color. // DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color.
func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) { func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) {
var path Path var path Path
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise) path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr, antialias) useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
} }
// StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color. // StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color.
@ -118,7 +143,10 @@ func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr
strokeOp := &StrokeOptions{} strokeOp := &StrokeOptions{}
strokeOp.Width = strokeWidth strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias) useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
drawVerticesForUtil(dst, vs, is, clr, antialias)
return vs, is
})
} }