mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
graphics: Reuse vertices backend and reduce allocating
This commit is contained in:
parent
791f96f980
commit
74f1e5519f
23
mipmap.go
23
mipmap.go
@ -121,9 +121,14 @@ func (m *mipmap) drawImage(src *mipmap, bounds image.Rectangle, geom *GeoM, colo
|
|||||||
colorm = nil
|
colorm = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
screen := filter == driver.FilterScreen
|
||||||
|
if screen && level != 0 {
|
||||||
|
panic("ebiten: Mipmap must not be used when the filter is FilterScreen")
|
||||||
|
}
|
||||||
|
|
||||||
a, b, c, d, tx, ty := geom.elements()
|
a, b, c, d, tx, ty := geom.elements()
|
||||||
if level == 0 {
|
if level == 0 {
|
||||||
vs := quadVertices(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, a, b, c, d, tx, ty, cr, cg, cb, ca)
|
vs := quadVertices(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, a, b, c, d, tx, ty, cr, cg, cb, ca, screen)
|
||||||
is := graphics.QuadIndices()
|
is := graphics.QuadIndices()
|
||||||
m.orig.DrawTriangles(src.orig, vs, is, colorm, mode, filter, driver.AddressClampToZero)
|
m.orig.DrawTriangles(src.orig, vs, is, colorm, mode, filter, driver.AddressClampToZero)
|
||||||
} else if buf := src.level(bounds, level); buf != nil {
|
} else if buf := src.level(bounds, level); buf != nil {
|
||||||
@ -133,7 +138,7 @@ func (m *mipmap) drawImage(src *mipmap, bounds image.Rectangle, geom *GeoM, colo
|
|||||||
b *= s
|
b *= s
|
||||||
c *= s
|
c *= s
|
||||||
d *= s
|
d *= s
|
||||||
vs := quadVertices(0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca)
|
vs := quadVertices(0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca, false)
|
||||||
is := graphics.QuadIndices()
|
is := graphics.QuadIndices()
|
||||||
m.orig.DrawTriangles(buf, vs, is, colorm, mode, filter, driver.AddressClampToZero)
|
m.orig.DrawTriangles(buf, vs, is, colorm, mode, filter, driver.AddressClampToZero)
|
||||||
}
|
}
|
||||||
@ -149,7 +154,7 @@ func (m *mipmap) drawTriangles(src *mipmap, bounds image.Rectangle, vertices []V
|
|||||||
// TODO: Needs boundary check optimization?
|
// TODO: Needs boundary check optimization?
|
||||||
// See https://go101.org/article/bounds-check-elimination.html
|
// See https://go101.org/article/bounds-check-elimination.html
|
||||||
|
|
||||||
vs := vertexSlice(len(vertices))
|
vs := vertexSlice(len(vertices), false)
|
||||||
for i, v := range vertices {
|
for i, v := range vertices {
|
||||||
vs[i*graphics.VertexFloatNum] = v.DstX
|
vs[i*graphics.VertexFloatNum] = v.DstX
|
||||||
vs[i*graphics.VertexFloatNum+1] = v.DstY
|
vs[i*graphics.VertexFloatNum+1] = v.DstY
|
||||||
@ -194,7 +199,7 @@ func (m *mipmap) level(r image.Rectangle, level int) *buffered.Image {
|
|||||||
switch {
|
switch {
|
||||||
case level == 1:
|
case level == 1:
|
||||||
src = m.orig
|
src = m.orig
|
||||||
vs = quadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
|
vs = quadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1, false)
|
||||||
filter = driver.FilterLinear
|
filter = driver.FilterLinear
|
||||||
case level > 1:
|
case level > 1:
|
||||||
src = m.level(r, level-1)
|
src = m.level(r, level-1)
|
||||||
@ -203,11 +208,11 @@ func (m *mipmap) level(r image.Rectangle, level int) *buffered.Image {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
w, h := sizeForLevel(r.Dx(), r.Dy(), level-1)
|
w, h := sizeForLevel(r.Dx(), r.Dy(), level-1)
|
||||||
vs = quadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
|
vs = quadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1, false)
|
||||||
filter = driver.FilterLinear
|
filter = driver.FilterLinear
|
||||||
case level == -1:
|
case level == -1:
|
||||||
src = m.orig
|
src = m.orig
|
||||||
vs = quadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 2, 0, 0, 2, 0, 0, 1, 1, 1, 1)
|
vs = quadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 2, 0, 0, 2, 0, 0, 1, 1, 1, 1, false)
|
||||||
filter = driver.FilterNearest
|
filter = driver.FilterNearest
|
||||||
case level < -1:
|
case level < -1:
|
||||||
src = m.level(r, level+1)
|
src = m.level(r, level+1)
|
||||||
@ -216,7 +221,7 @@ func (m *mipmap) level(r image.Rectangle, level int) *buffered.Image {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
w, h := sizeForLevel(r.Dx(), r.Dy(), level+1)
|
w, h := sizeForLevel(r.Dx(), r.Dy(), level+1)
|
||||||
vs = quadVertices(0, 0, w, h, 2, 0, 0, 2, 0, 0, 1, 1, 1, 1)
|
vs = quadVertices(0, 0, w, h, 2, 0, 0, 2, 0, 0, 1, 1, 1, 1, false)
|
||||||
filter = driver.FilterNearest
|
filter = driver.FilterNearest
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("ebiten: invalid level: %d", level))
|
panic(fmt.Sprintf("ebiten: invalid level: %d", level))
|
||||||
@ -292,6 +297,10 @@ func (m *mipmap) mipmapLevel(geom *GeoM, width, height int, filter driver.Filter
|
|||||||
panic("ebiten: dst must be non zero at mipmapLevel")
|
panic("ebiten: dst must be non zero at mipmapLevel")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filter == driver.FilterScreen {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
// Use 'negative' mipmap to render edges correctly (#611, #907).
|
// Use 'negative' mipmap to render edges correctly (#611, #907).
|
||||||
// It looks like 128 is the enlargement factor that causes edge missings to pass the test TestImageStretch.
|
// It looks like 128 is the enlargement factor that causes edge missings to pass the test TestImageStretch.
|
||||||
const tooBigScale = 128
|
const tooBigScale = 128
|
||||||
|
35
vertex.go
35
vertex.go
@ -21,7 +21,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
theVerticesBackend = &verticesBackend{}
|
theVerticesBackend = &verticesBackend{
|
||||||
|
backend: make([]float32, graphics.VertexFloatNum*1024),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type verticesBackend struct {
|
type verticesBackend struct {
|
||||||
@ -30,42 +32,43 @@ type verticesBackend struct {
|
|||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *verticesBackend) slice(n int) []float32 {
|
func (v *verticesBackend) slice(n int, last bool) []float32 {
|
||||||
v.m.Lock()
|
v.m.Lock()
|
||||||
|
|
||||||
need := n * graphics.VertexFloatNum
|
need := n * graphics.VertexFloatNum
|
||||||
if v.head+need > len(v.backend) {
|
if l := len(v.backend); v.head+need > l {
|
||||||
v.backend = nil
|
for v.head+need > l {
|
||||||
|
l *= 2
|
||||||
|
}
|
||||||
|
v.backend = make([]float32, l)
|
||||||
|
println(l)
|
||||||
v.head = 0
|
v.head = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.backend == nil {
|
|
||||||
l := 1024
|
|
||||||
if n > l {
|
|
||||||
l = n
|
|
||||||
}
|
|
||||||
v.backend = make([]float32, graphics.VertexFloatNum*l)
|
|
||||||
}
|
|
||||||
|
|
||||||
s := v.backend[v.head : v.head+need]
|
s := v.backend[v.head : v.head+need]
|
||||||
|
if last {
|
||||||
|
// If last is true, the vertices backend is sent to GPU and it is fine to reuse the slice.
|
||||||
|
v.head = 0
|
||||||
|
} else {
|
||||||
v.head += need
|
v.head += need
|
||||||
|
}
|
||||||
|
|
||||||
v.m.Unlock()
|
v.m.Unlock()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func vertexSlice(n int) []float32 {
|
func vertexSlice(n int, last bool) []float32 {
|
||||||
return theVerticesBackend.slice(n)
|
return theVerticesBackend.slice(n, last)
|
||||||
}
|
}
|
||||||
|
|
||||||
func quadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 {
|
func quadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32, last bool) []float32 {
|
||||||
x := float32(sx1 - sx0)
|
x := float32(sx1 - sx0)
|
||||||
y := float32(sy1 - sy0)
|
y := float32(sy1 - sy0)
|
||||||
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
||||||
u0, v0, u1, v1 := float32(sx0), float32(sy0), float32(sx1), float32(sy1)
|
u0, v0, u1, v1 := float32(sx0), float32(sy0), float32(sx1), float32(sy1)
|
||||||
|
|
||||||
// This function is very performance-sensitive and implement in a very dumb way.
|
// This function is very performance-sensitive and implement in a very dumb way.
|
||||||
vs := vertexSlice(4)
|
vs := vertexSlice(4, last)
|
||||||
_ = vs[:48]
|
_ = vs[:48]
|
||||||
|
|
||||||
// For each values, see the comment at shareable.(*Image).DrawTriangles.
|
// For each values, see the comment at shareable.(*Image).DrawTriangles.
|
||||||
|
Loading…
Reference in New Issue
Block a user