graphics: Refactoring: Change QuadVertices -> PutQuadVertices for consistency

This commit is contained in:
Hajime Hoshi 2019-06-21 01:53:05 +09:00
parent c7ab66e0e1
commit 008de78cec
7 changed files with 42 additions and 27 deletions

View File

@ -71,13 +71,13 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image {
} }
s := shareable.NewImage(w2, h2) s := shareable.NewImage(w2, h2)
var src *shareable.Image var src *shareable.Image
var vs []float32 vs := graphics.VertexSlice(4)
if l := len(imgs); l == 0 { if l := len(imgs); l == 0 {
src = m.orig 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 { } else {
src = m.level(r, l) 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() is := graphics.QuadIndices()
s.DrawTriangles(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterLinear, graphics.AddressClampToZero) 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 { if level == 0 {
src := img.mipmap.original() 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() is := graphics.QuadIndices()
i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero) i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero)
} else if src := img.mipmap.level(bounds, level); src != nil { } 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) b *= float32(s)
c *= float32(s) c *= float32(s)
d *= 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() is := graphics.QuadIndices()
i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero) i.mipmap.original().DrawTriangles(src, vs, is, colorm, mode, filter, graphics.AddressClampToZero)
} }

View File

@ -59,7 +59,11 @@ func (v *verticesBackend) slice(n int) []float32 {
return s 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. // width and height are the source image's size.
// For performance reason, graphics.InternalImageSize is not applied to width/height here. // 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 { 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 { 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) wf := float32(width)
hf := float32(height) hf := float32(height)
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf 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 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 // Specifying a range explicitly here is redundant but this helps optimization
// to eliminate boundary checks. // to eliminate boundary checks.
// //
// 4*VertexFloatNum is better than 48 in terms of code maintenanceability, but in GopherJS, optimization // 4*VertexFloatNum is better than 48 in terms of code maintenanceability, but in GopherJS, optimization
// might not work. // 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 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[45] = cg
vs[46] = cb vs[46] = cb
vs[47] = ca vs[47] = ca
return vs
} }
var ( var (

View File

@ -47,7 +47,8 @@ func TestClear(t *testing.T) {
src := NewImage(w/2, h/2) src := NewImage(w/2, h/2)
dst := NewImage(w, h) 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() is := graphics.QuadIndices()
dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero) 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) clr := NewImage(w, h)
src := NewImage(16, 16) src := NewImage(16, 16)
dst := NewImage(w, h) 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() is := graphics.QuadIndices()
dst.DrawTriangles(clr, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero) dst.DrawTriangles(clr, vs, is, nil, graphics.CompositeModeClear, graphics.FilterNearest, graphics.AddressClampToZero)
dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero) dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)

View File

@ -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. // As emptyImage is a priority image, this is restored before other regular images are restored.
dw, dh := i.internalSize() dw, dh := i.internalSize()
sw, sh := emptyImage.Size() 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, float32(dw)/float32(sw), 0, 0, float32(dh)/float32(sh), 0, 0,
rf, gf, bf, af) rf, gf, bf, af)
is := graphics.QuadIndices() is := graphics.QuadIndices()
@ -238,9 +239,9 @@ func (i *Image) internalSize() (int, int) {
return i.w2, i.h2 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() 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) { func (i *Image) PutVertex(vs []float32, dx, dy, sx, sy float32, bx0, by0, bx1, by1 float32, cr, cg, cb, ca float32) {

View File

@ -106,9 +106,11 @@ func TestRestoreWithoutDraw(t *testing.T) {
} }
func quadVertices(src *Image, sw, sh, x, y int) []float32 { 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, 0, 0, 1, float32(x), float32(y),
1, 1, 1, 1) 1, 1, 1, 1)
return vs
} }
func TestRestoreChain(t *testing.T) { func TestRestoreChain(t *testing.T) {

View File

@ -171,7 +171,8 @@ func (i *Image) ensureNotShared() {
x, y, w, h := i.region() x, y, w, h := i.region()
newImg := restorable.NewImage(w, h) 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() is := graphics.QuadIndices()
newImg.DrawTriangles(i.backend.restorable, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) 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 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. // 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 { if i.backend == nil {
i.allocate(true) i.allocate(true)
} }
ox, oy, _, _ := i.region() 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. // PutVertices puts the given dest with vertices that can be passed to DrawTriangles.

View File

@ -84,7 +84,8 @@ func TestEnsureNotShared(t *testing.T) {
dy1 = size * 3 / 4 dy1 = size * 3 / 4
) )
// img4.ensureNotShared() should be called. // 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() is := graphics.QuadIndices()
img4.DrawTriangles(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) img4.DrawTriangles(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
want := false want := false
@ -148,7 +149,8 @@ func TestReshared(t *testing.T) {
} }
// Use img1 as a render target. // 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() is := graphics.QuadIndices()
img1.DrawTriangles(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) img1.DrawTriangles(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
if got, want := img1.IsSharedForTesting(), false; got != want { if got, want := img1.IsSharedForTesting(), false; got != want {
@ -274,7 +276,8 @@ func TestReplacePixelsAfterDrawTriangles(t *testing.T) {
} }
src.ReplacePixels(pix) 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() is := graphics.QuadIndices()
dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero) dst.DrawTriangles(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
dst.ReplacePixels(pix) dst.ReplacePixels(pix)