internal/graphics: use pixels for offsets

This commit is contained in:
Hajime Hoshi 2023-04-26 00:34:13 +09:00
parent 9d2178cf71
commit 56b4cdc3c4
3 changed files with 53 additions and 6 deletions

View File

@ -56,7 +56,7 @@ func imageDstRegionOnTexture() (vec2, vec2) {
return __textureDestinationRegionOrigin, __textureDestinationRegionSize return __textureDestinationRegionOrigin, __textureDestinationRegionSize
} }
// The unit is the source texture's pixel or texel. // The unit is the source texture's pixel.
var __textureSourceOffsets [%[2]d]vec2 var __textureSourceOffsets [%[2]d]vec2
// The unit is the source texture's pixel or texel. // The unit is the source texture's pixel or texel.
@ -82,7 +82,7 @@ func imageSrcRegionOnTexture() (vec2, vec2) {
case shaderir.Pixel: case shaderir.Pixel:
pos = fmt.Sprintf("pos + __textureSourceOffsets[%d]", i-1) pos = fmt.Sprintf("pos + __textureSourceOffsets[%d]", i-1)
case shaderir.Texel: case shaderir.Texel:
pos = fmt.Sprintf("(pos + __textureSourceOffsets[%d]) * __textureSizes[0] / __textureSizes[%d]", i-1, i) pos = fmt.Sprintf("(pos * __textureSizes[0] + __textureSourceOffsets[%d]) / __textureSizes[%d]", i-1, i)
default: default:
return "", fmt.Errorf("graphics: unexpected unit: %d", unit) return "", fmt.Errorf("graphics: unexpected unit: %d", unit)
} }

View File

@ -621,10 +621,6 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade
srcRegion.Y /= float32(h) srcRegion.Y /= float32(h)
srcRegion.Width /= float32(w) srcRegion.Width /= float32(w)
srcRegion.Height /= float32(h) srcRegion.Height /= float32(h)
for i := range offsets {
offsets[i][0] /= float32(w)
offsets[i][1] /= float32(h)
}
} }
// Set the source offsets. // Set the source offsets.

View File

@ -1761,3 +1761,54 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
} }
} }
} }
func TestShaderDifferentTextureSizes(t *testing.T) {
src0 := ebiten.NewImageWithOptions(image.Rect(0, 0, 20, 4000), &ebiten.NewImageOptions{
Unmanaged: true,
}).SubImage(image.Rect(4, 1025, 6, 1028)).(*ebiten.Image)
defer src0.Dispose()
src1 := ebiten.NewImageWithOptions(image.Rect(0, 0, 4000, 20), &ebiten.NewImageOptions{
Unmanaged: true,
}).SubImage(image.Rect(2047, 7, 2049, 10)).(*ebiten.Image)
defer src1.Dispose()
src0.Fill(color.RGBA{0x10, 0x20, 0x30, 0xff})
src1.Fill(color.RGBA{0x30, 0x20, 0x10, 0xff})
for _, unit := range []string{"texel", "pixel"} {
unit := unit
t.Run(fmt.Sprintf("unit %s", unit), func(t *testing.T) {
shader, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit %s
package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return imageSrc0At(texCoord) + imageSrc1At(texCoord)
}
`, unit)))
if err != nil {
t.Fatal(err)
}
defer shader.Dispose()
dst := ebiten.NewImage(2, 3)
defer dst.Dispose()
op := &ebiten.DrawRectShaderOptions{}
op.Images[0] = src0
op.Images[1] = src1
dst.DrawRectShader(2, 3, shader, op)
for j := 0; j < 3; j++ {
for i := 0; i < 2; i++ {
got := dst.At(i, j).(color.RGBA)
want := color.RGBA{0x40, 0x40, 0x40, 0xff}
if !sameColors(got, want, 1) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
})
}
}