graphicscommand: Merge DrawTriangles and DrawShader

This commit is contained in:
Hajime Hoshi 2020-05-24 02:36:09 +09:00
parent 3ed9f8ee3b
commit a4d419bab1
3 changed files with 24 additions and 34 deletions

View File

@ -142,7 +142,9 @@ func (i *Image) InternalSize() (int, int) {
// 9: Color G
// 10: Color B
// 11: Color Y
func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, clr *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) {
//
// src and shader are exclusive and only either is non-nil.
func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, clr *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, shader *Shader, uniforms map[int]interface{}) {
if src.screen {
panic("graphicscommand: the screen image cannot be the rendering source")
}
@ -153,36 +155,24 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
}
}
src.resolveBufferedReplacePixels()
i.resolveBufferedReplacePixels()
theCommandQueue.EnqueueDrawTrianglesCommand(i, src, vertices, indices, clr, mode, filter, address, nil, nil)
if i.lastCommand == lastCommandNone && !i.screen {
i.lastCommand = lastCommandClear
var us map[int]interface{}
if src != nil {
src.resolveBufferedReplacePixels()
} else {
i.lastCommand = lastCommandDrawTriangles
}
}
func (i *Image) DrawShader(shader *Shader, vertices []float32, indices []uint16, mode driver.CompositeMode, uniforms map[int]interface{}) {
if i.lastCommand == lastCommandNone {
panic("graphicscommand: the image must be cleared first before DrawShader")
}
us := map[int]interface{}{}
for k, v := range uniforms {
switch v := v.(type) {
case *Image:
v.resolveBufferedReplacePixels()
us[k] = v.image
default:
us[k] = v
us = map[int]interface{}{}
for k, v := range uniforms {
switch v := v.(type) {
case *Image:
v.resolveBufferedReplacePixels()
us[k] = v.image
default:
us[k] = v
}
}
}
i.resolveBufferedReplacePixels()
theCommandQueue.EnqueueDrawTrianglesCommand(i, nil, vertices, indices, nil, mode, 0, 0, shader, us)
theCommandQueue.EnqueueDrawTrianglesCommand(i, src, vertices, indices, clr, mode, filter, address, shader, us)
if i.lastCommand == lastCommandNone && !i.screen {
i.lastCommand = lastCommandClear

View File

@ -46,7 +46,7 @@ func TestClear(t *testing.T) {
vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices()
dst.DrawTriangles(src, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero)
dst.DrawTriangles(src, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero, nil, nil)
pix, err := dst.Pixels()
if err != nil {
@ -76,8 +76,8 @@ func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
dst := NewImage(w, h)
vs := quadVertices(w/2, h/2)
is := graphics.QuadIndices()
dst.DrawTriangles(clr, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero)
dst.DrawTriangles(src, vs, is, nil, driver.CompositeModeSourceOver, driver.FilterNearest, driver.AddressClampToZero)
dst.DrawTriangles(clr, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero, nil, nil)
dst.DrawTriangles(src, vs, is, nil, driver.CompositeModeSourceOver, driver.FilterNearest, driver.AddressClampToZero, nil, nil)
dst.ReplacePixels(make([]byte, 4), 0, 0, 1, 1)
}
@ -94,7 +94,7 @@ func TestShader(t *testing.T) {
dst := NewImage(w, h)
vs := quadVertices(w, h)
is := graphics.QuadIndices()
dst.DrawTriangles(clr, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero)
dst.DrawTriangles(clr, vs, is, nil, driver.CompositeModeClear, driver.FilterNearest, driver.AddressClampToZero, nil, nil)
mat := shaderir.Expr{
Type: shaderir.Call,
@ -306,7 +306,7 @@ func TestShader(t *testing.T) {
us := map[int]interface{}{
0: []float32{w, h},
}
dst.DrawShader(s, vs, is, driver.CompositeModeSourceOver, us)
dst.DrawTriangles(nil, vs, is, nil, driver.CompositeModeSourceOver, 0, 0, s, us)
pix, err := dst.Pixels()
if err != nil {

View File

@ -252,7 +252,7 @@ func fillImage(i *graphicscommand.Image, clr color.RGBA) {
vs := quadVertices(0, 0, float32(dw), float32(dh), 0, 0, float32(sw), float32(sh), rf, gf, bf, af)
is := graphics.QuadIndices()
i.DrawTriangles(emptyImage.image, vs, is, nil, compositemode, driver.FilterNearest, driver.AddressClampToZero)
i.DrawTriangles(emptyImage.image, vs, is, nil, compositemode, driver.FilterNearest, driver.AddressClampToZero, nil, nil)
}
// BasePixelsForTesting returns the image's basePixels for testing.
@ -369,7 +369,7 @@ func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16,
} else {
i.appendDrawTrianglesHistory(img, vertices, indices, colorm, mode, filter, address)
}
i.image.DrawTriangles(img.image, vertices, indices, colorm, mode, filter, address)
i.image.DrawTriangles(img.image, vertices, indices, colorm, mode, filter, address, nil, nil)
}
// appendDrawTrianglesHistory appends a draw-image history item to the image.
@ -536,7 +536,7 @@ func (i *Image) restore() error {
if c.image.hasDependency() {
panic("restorable: all dependencies must be already resolved but not")
}
gimg.DrawTriangles(c.image.image, c.vertices, c.indices, c.colorm, c.mode, c.filter, c.address)
gimg.DrawTriangles(c.image.image, c.vertices, c.indices, c.colorm, c.mode, c.filter, c.address, nil, nil)
}
if len(i.drawTrianglesHistory) > 0 {