graphicscommand: Remove the source-region parts from the uniform variables

This commit is contained in:
Hajime Hoshi 2020-06-30 00:44:05 +09:00
parent 780465b702
commit 1fe2f47701
5 changed files with 8 additions and 62 deletions

View File

@ -367,7 +367,6 @@ func (i *Image) DrawTrianglesWithShader(vertices []Vertex, indices []uint16, sha
mode := driver.CompositeMode(options.CompositeMode) mode := driver.CompositeMode(options.CompositeMode)
us := []interface{}{} us := []interface{}{}
var firstImage *Image
for _, v := range options.Uniforms { for _, v := range options.Uniforms {
switch v := v.(type) { switch v := v.(type) {
case *Image: case *Image:
@ -375,17 +374,6 @@ func (i *Image) DrawTrianglesWithShader(vertices []Vertex, indices []uint16, sha
panic("ebiten: the given image to DrawTriangles must not be disposed") panic("ebiten: the given image to DrawTriangles must not be disposed")
} }
us = append(us, v.buffered) us = append(us, v.buffered)
if firstImage == nil {
firstImage = v
} else {
b := v.Bounds()
us = append(us, []float32{
float32(b.Min.X),
float32(b.Min.Y),
float32(b.Max.X),
float32(b.Max.Y),
})
}
default: default:
us = append(us, v) us = append(us, v)
} }
@ -395,25 +383,17 @@ func (i *Image) DrawTrianglesWithShader(vertices []Vertex, indices []uint16, sha
// The actual value is set at graphicscommand package. // The actual value is set at graphicscommand package.
us = append([]interface{}{[]float32{0, 0}}, us...) us = append([]interface{}{[]float32{0, 0}}, us...)
var bx0, by0, bx1, by1 float32
if firstImage != nil {
b := firstImage.Bounds()
bx0 = float32(b.Min.X)
by0 = float32(b.Min.Y)
bx1 = float32(b.Max.X)
by1 = float32(b.Max.Y)
}
vs := make([]float32, len(vertices)*graphics.VertexFloatNum) vs := make([]float32, len(vertices)*graphics.VertexFloatNum)
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
vs[i*graphics.VertexFloatNum+2] = v.SrcX vs[i*graphics.VertexFloatNum+2] = v.SrcX
vs[i*graphics.VertexFloatNum+3] = v.SrcY vs[i*graphics.VertexFloatNum+3] = v.SrcY
vs[i*graphics.VertexFloatNum+4] = bx0 // TODO: Remove these values for the source region.
vs[i*graphics.VertexFloatNum+5] = by0 vs[i*graphics.VertexFloatNum+4] = 0
vs[i*graphics.VertexFloatNum+6] = bx1 vs[i*graphics.VertexFloatNum+5] = 0
vs[i*graphics.VertexFloatNum+7] = by1 vs[i*graphics.VertexFloatNum+6] = 0
vs[i*graphics.VertexFloatNum+7] = 0
vs[i*graphics.VertexFloatNum+8] = v.ColorR vs[i*graphics.VertexFloatNum+8] = v.ColorR
vs[i*graphics.VertexFloatNum+9] = v.ColorG vs[i*graphics.VertexFloatNum+9] = v.ColorG
vs[i*graphics.VertexFloatNum+10] = v.ColorB vs[i*graphics.VertexFloatNum+10] = v.ColorB

View File

@ -410,28 +410,10 @@ func (c *drawTrianglesCommand) Exec(indexOffset int) error {
if c.shader != nil { if c.shader != nil {
us := make([]interface{}, len(c.uniforms)) us := make([]interface{}, len(c.uniforms))
firstImage := true
for i := 0; i < len(c.uniforms); i++ { for i := 0; i < len(c.uniforms); i++ {
switch v := c.uniforms[i].(type) { switch v := c.uniforms[i].(type) {
case *Image: case *Image:
us[i] = v.image.ID() us[i] = v.image.ID()
if firstImage {
firstImage = false
continue
}
// Convert pixels to texels.
w, h := v.InternalSize()
i++
region := c.uniforms[i].([]float32)
vs := []float32{
region[0] / float32(w),
region[1] / float32(h),
region[2] / float32(w),
region[3] / float32(h),
}
us[i] = vs
default: default:
us[i] = v us[i] = v
} }

View File

@ -116,9 +116,7 @@ func TestShaderMultipleSources(t *testing.T) {
[]float32{0, 0}, []float32{0, 0},
srcs[0], srcs[0],
srcs[1], srcs[1],
[]float32{0, 0, 1, 1},
srcs[2], srcs[2],
[]float32{0, 0, 1, 1},
} }
dst.DrawTriangles(nil, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), nil, driver.CompositeModeCopy, driver.FilterNearest, driver.AddressUnsafe, s, us) dst.DrawTriangles(nil, quadVertices(1, 1, 0, 0), graphics.QuadIndices(), nil, driver.CompositeModeCopy, driver.FilterNearest, driver.AddressUnsafe, s, us)

View File

@ -357,23 +357,11 @@ func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16,
s = shader.shader s = shader.shader
} }
firstImage := true
us := make([]interface{}, len(uniforms)) us := make([]interface{}, len(uniforms))
for i := 0; i < len(uniforms); i++ { for i := 0; i < len(uniforms); i++ {
switch v := uniforms[i].(type) { switch v := uniforms[i].(type) {
case *Image: case *Image:
us[i] = v.backend.restorable us[i] = v.backend.restorable
if !firstImage {
i++
region := uniforms[i].([]float32)
us[i] = []float32{
region[0] + oxf,
region[1] + oyf,
region[2] + oxf,
region[3] + oyf,
}
}
firstImage = false
default: default:
us[i] = v us[i] = v
} }

View File

@ -289,9 +289,7 @@ func ShaderProgramFill(r, g, b, a byte) shaderir.Program {
// Uniform variables's indices and their values are: // Uniform variables's indices and their values are:
// //
// 0: the framebuffer size (Vec2) // 0: the framebuffer size (Vec2)
// 1: the first images (Texture2D) // 1-: the images (Texture2D)
// 2n: the n-th image (Texture2D)
// 2n+1: the n-th image's region (Vec4)
// //
// The first image's size and region are represented in attribute variables. // The first image's size and region are represented in attribute variables.
// //
@ -363,7 +361,7 @@ func ShaderProgramImages(imageNum int) shaderir.Program {
}, },
{ {
Type: shaderir.UniformVariable, Type: shaderir.UniformVariable,
Index: 2 * i, Index: i + 1,
}, },
texPos, texPos,
}, },