internal/atlas, initernal/graphics: Better buffer size calculation

This commit is contained in:
Hajime Hoshi 2021-10-31 17:18:33 +09:00
parent d236d73a1f
commit 0238549cfb
2 changed files with 9 additions and 2 deletions

View File

@ -59,7 +59,7 @@ func temporaryPixelsByteSize(size int) int {
// Be careful that the returned pixels might not be zero-cleared.
func (t *temporaryPixels) alloc(size int) []byte {
if len(t.pixels) < t.pos+size {
t.pixels = make([]byte, temporaryPixelsByteSize(t.pos+size))
t.pixels = make([]byte, max(len(t.pixels)*2, temporaryPixelsByteSize(size)))
t.pos = 0
}
pix := t.pixels[t.pos : t.pos+size]

View File

@ -75,13 +75,20 @@ func verticesBackendFloat32Size(size int) int {
return l
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (v *verticesBackend) slice(n int) []float32 {
v.m.Lock()
defer v.m.Unlock()
need := n * VertexFloatNum
if len(v.backend) < v.pos+need {
v.backend = make([]float32, verticesBackendFloat32Size(v.pos+need))
v.backend = make([]float32, max(len(v.backend)*2, verticesBackendFloat32Size(need)))
v.pos = 0
}
s := v.backend[v.pos : v.pos+need]