From 008de78ceca9660556dd3117500cb5e619fd8cd9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 21 Jun 2019 01:53:05 +0900 Subject: [PATCH] graphics: Refactoring: Change QuadVertices -> PutQuadVertices for consistency --- image.go | 12 +++++++----- internal/graphics/vertices.go | 20 ++++++++++++-------- internal/graphicscommand/image_test.go | 6 ++++-- internal/restorable/image.go | 7 ++++--- internal/restorable/images_test.go | 4 +++- internal/shareable/shareable.go | 11 ++++++----- internal/shareable/shareable_test.go | 9 ++++++--- 7 files changed, 42 insertions(+), 27 deletions(-) diff --git a/image.go b/image.go index e88f02eaf..eaa95d806 100644 --- a/image.go +++ b/image.go @@ -71,13 +71,13 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image { } s := shareable.NewImage(w2, h2) var src *shareable.Image - var vs []float32 + vs := graphics.VertexSlice(4) if l := len(imgs); l == 0 { src = m.orig - vs = src.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) + src.PutQuadVertices(vs, r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) } else { src = m.level(r, l) - vs = src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) + src.PutQuadVertices(vs, 0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) } is := graphics.QuadIndices() s.DrawTriangles(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterLinear, graphics.AddressClampToZero) @@ -355,7 +355,8 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error { if level == 0 { src := img.mipmap.original() - vs := src.QuadVertices(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, a, b, c, d, tx, ty, cr, cg, cb, ca) + vs := graphics.VertexSlice(4) + src.PutQuadVertices(vs, bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, a, b, c, d, tx, ty, cr, cg, cb, ca) is := graphics.QuadIndices() i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero) } else if src := img.mipmap.level(bounds, level); src != nil { @@ -365,7 +366,8 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error { b *= float32(s) c *= float32(s) d *= float32(s) - vs := src.QuadVertices(0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca) + vs := graphics.VertexSlice(4) + src.PutQuadVertices(vs, 0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca) is := graphics.QuadIndices() i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero) } diff --git a/internal/graphics/vertices.go b/internal/graphics/vertices.go index 62ac2c97e..bcf22cb5a 100644 --- a/internal/graphics/vertices.go +++ b/internal/graphics/vertices.go @@ -59,7 +59,11 @@ func (v *verticesBackend) slice(n int) []float32 { return s } -func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 { +func VertexSlice(n int) []float32 { + return theVerticesBackend.slice(n) +} + +func PutQuadVertices(vs []float32, width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) { // width and height are the source image's size. // For performance reason, graphics.InternalImageSize is not applied to width/height here. @@ -72,27 +76,29 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty } if sx0 >= sx1 || sy0 >= sy1 { - return nil + // Do not modify vs. Here, it is assumed that vs is initialized with zero values. + return } if sx1 <= 0 || sy1 <= 0 { - return nil + // Do not modify vs. Here, it is assumed that vs is initialized with zero values. + return } wf := float32(width) hf := float32(height) u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf - return quadVerticesImpl(wf, hf, float32(sx1-sx0), float32(sy1-sy0), u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca) + putQuadVerticesImpl(vs, wf, hf, float32(sx1-sx0), float32(sy1-sy0), u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca) } const TexelAdjustmentFactor = 512.0 -func quadVerticesImpl(sw, sh, x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) []float32 { +func putQuadVerticesImpl(vs []float32, sw, sh, x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) { // Specifying a range explicitly here is redundant but this helps optimization // to eliminate boundary checks. // // 4*VertexFloatNum is better than 48 in terms of code maintenanceability, but in GopherJS, optimization // might not work. - vs := theVerticesBackend.slice(4)[0:48] + vs = vs[0:48] ax, by, cx, dy := a*x, b*y, c*x, d*y @@ -156,8 +162,6 @@ func quadVerticesImpl(sw, sh, x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, vs[45] = cg vs[46] = cb vs[47] = ca - - return vs } var ( diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index e0eed7156..ec5f85ae9 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -47,7 +47,8 @@ func TestClear(t *testing.T) { src := NewImage(w/2, h/2) dst := NewImage(w, h) - vs := graphics.QuadVertices(w/2, h/2, 0, 0, w/2, h/2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + graphics.PutQuadVertices(vs, w/2, h/2, 0, 0, w/2, h/2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero) @@ -74,7 +75,8 @@ func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) { clr := NewImage(w, h) src := NewImage(16, 16) dst := NewImage(w, h) - vs := graphics.QuadVertices(16, 16, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + graphics.PutQuadVertices(vs, 16, 16, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() dst.DrawTriangles(clr, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero) dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 60123fed3..2a2cc97fc 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -195,7 +195,8 @@ func (i *Image) fill(r, g, b, a uint8) { // As emptyImage is a priority image, this is restored before other regular images are restored. dw, dh := i.internalSize() sw, sh := emptyImage.Size() - vs := graphics.QuadVertices(dw, dh, 0, 0, sw, sh, + vs := graphics.VertexSlice(4) + graphics.PutQuadVertices(vs, dw, dh, 0, 0, sw, sh, float32(dw)/float32(sw), 0, 0, float32(dh)/float32(sh), 0, 0, rf, gf, bf, af) is := graphics.QuadIndices() @@ -238,9 +239,9 @@ func (i *Image) internalSize() (int, int) { return i.w2, i.h2 } -func (i *Image) QuadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 { +func (i *Image) PutQuadVertices(vs []float32, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) { w, h := i.internalSize() - return graphics.QuadVertices(w, h, sx0, sy0, sx1, sy1, a, b, c, d, tx, ty, cr, cg, cb, ca) + graphics.PutQuadVertices(vs, w, h, sx0, sy0, sx1, sy1, a, b, c, d, tx, ty, cr, cg, cb, ca) } func (i *Image) PutVertex(vs []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32, cr, cg, cb, ca float32) { diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 772b412cf..aed385f36 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -106,9 +106,11 @@ func TestRestoreWithoutDraw(t *testing.T) { } func quadVertices(src *Image, sw, sh, x, y int) []float32 { - return src.QuadVertices(0, 0, sw, sh, + vs := graphics.VertexSlice(4) + src.PutQuadVertices(vs, 0, 0, sw, sh, 1, 0, 0, 1, float32(x), float32(y), 1, 1, 1, 1) + return vs } func TestRestoreChain(t *testing.T) { diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index e951399bf..3a07504c9 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -171,7 +171,8 @@ func (i *Image) ensureNotShared() { x, y, w, h := i.region() newImg := restorable.NewImage(w, h) - vs := i.backend.restorable.QuadVertices(x, y, x+w, y+h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + i.backend.restorable.PutQuadVertices(vs, x, y, x+w, y+h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() newImg.DrawTriangles(i.backend.restorable, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) @@ -226,16 +227,16 @@ func (i *Image) Size() (width, height int) { return i.width, i.height } -// QuadVertices returns the vertices for rendering a quad. +// PutQuadVertices puts the given dest with vertices for rendering a quad. // -// QuadVertices is highly optimized for rendering quads, and that's the most significant difference from +// PutQuadVertices is highly optimized for rendering quads, and that's the most significant difference from // PutVertices. -func (i *Image) QuadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 { +func (i *Image) PutQuadVertices(vs []float32, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) { if i.backend == nil { i.allocate(true) } ox, oy, _, _ := i.region() - return i.backend.restorable.QuadVertices(sx0+ox, sy0+oy, sx1+ox, sy1+oy, a, b, c, d, tx, ty, cr, cg, cb, ca) + i.backend.restorable.PutQuadVertices(vs, sx0+ox, sy0+oy, sx1+ox, sy1+oy, a, b, c, d, tx, ty, cr, cg, cb, ca) } // PutVertices puts the given dest with vertices that can be passed to DrawTriangles. diff --git a/internal/shareable/shareable_test.go b/internal/shareable/shareable_test.go index 385300f6b..fc7115a60 100644 --- a/internal/shareable/shareable_test.go +++ b/internal/shareable/shareable_test.go @@ -84,7 +84,8 @@ func TestEnsureNotShared(t *testing.T) { dy1 = size * 3 / 4 ) // img4.ensureNotShared() should be called. - vs := img3.QuadVertices(0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + img3.PutQuadVertices(vs, 0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, 1, 1, 1, 1) is := graphics.QuadIndices() img4.DrawTriangles(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) want := false @@ -148,7 +149,8 @@ func TestReshared(t *testing.T) { } // Use img1 as a render target. - vs := img2.QuadVertices(0, 0, size, size, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + img2.PutQuadVertices(vs, 0, 0, size, size, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() img1.DrawTriangles(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) if got, want := img1.IsSharedForTesting(), false; got != want { @@ -274,7 +276,8 @@ func TestReplacePixelsAfterDrawTriangles(t *testing.T) { } src.ReplacePixels(pix) - vs := src.QuadVertices(0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) + vs := graphics.VertexSlice(4) + src.PutQuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) dst.ReplacePixels(pix)