ebiten: enable texCoord at DrawRectShader even without a source image

imageSrcRegionOnTexture will return (0, 0) to (width, height) for
the pixel-unit mode.

Closes #2166
This commit is contained in:
Hajime Hoshi 2023-08-01 01:24:23 +09:00
parent ae9781cd53
commit a0ffd8dd25
3 changed files with 110 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
)
@ -730,12 +731,23 @@ var _ [len(DrawRectShaderOptions{}.Images)]struct{} = [graphics.ShaderImageCount
//
// For the details about the shader, see https://ebitengine.org/en/documents/shader.html.
//
// When one of the specified image is non-nil and its size is different from (width, height), DrawRectShader panics.
// When one of the specified image is non-nil and is disposed, DrawRectShader panics.
//
// If a specified uniform variable's length or type doesn't match with an expected one, DrawRectShader panics.
//
// If a non-existent uniform variable name is specified, DrawRectShader panics.
//
// In a shader, texCoord in Fragment represents a position in a source image.
// If no source images are specified, texCoord represents the position from (0, 0) to (width, height) in pixels.
// If the unit is pixels by a compiler directive `//kage:unit pixels`, texCoord values are valid.
// If the unit is texels (default), texCoord values still take from (0, 0) to (width, height),
// but these are invalid since texCoord is expected to be in texels in the texel-unit mode.
// This behavior is preserved for backward compatibility. It is recommended to use the pixel-unit mode to avoid confusion.
//
// If no source images are specified, imageSrcRegionOnTexture returns a valid size only when the unit is pixels,
// but always returns 0 when the unit is texels (default).
//
// When the image i is disposed, DrawRectShader does nothing.
func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawRectShaderOptions) {
i.copyCheck()
@ -775,6 +787,13 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
b := img.Bounds()
sx, sy = img.adjustPosition(b.Min.X, b.Min.Y)
sr = img.adjustedRegion()
} else if shader.unit == shaderir.Pixel {
// Give the source size as pixels only when the unit is pixels so that users can get the source size via imageSrcRegionOnTexture (#2166).
// With the texel mode, the imageSrcRegionOnTexture values should be in texels so the source position in pixels would not match.
sr = graphicsdriver.Region{
Width: float32(width),
Height: float32(height),
}
}
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {

View File

@ -20,6 +20,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
)
@ -28,6 +29,7 @@ import (
// For the details about the shader, see https://ebitengine.org/en/documents/shader.html.
type Shader struct {
shader *ui.Shader
unit shaderir.Unit
}
// NewShader compiles a shader program in the shading language Kage, and returns the result.
@ -42,6 +44,7 @@ func NewShader(src []byte) (*Shader, error) {
}
return &Shader{
shader: ui.NewShader(ir),
unit: ir.Unit,
}, nil
}

View File

@ -2024,3 +2024,91 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
}
}
}
// Issue #2166
func TestShaderDrawRectWithoutSource(t *testing.T) {
const (
dstW = 16
dstH = 16
srcW = 8
srcH = 8
)
src := ebiten.NewImage(srcW, srcH)
for _, unit := range []string{"pixel", "texel"} {
s, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit %s
package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t := texCoord
origin, size := imageSrcRegionOnTexture()
// If the unit is texels and no source images are specified, size is always 0.
if size == vec2(0) {
// Even in this case, t is in pixels (0, 0) to (8, 8).
if t.x >= 4 && t.y >= 4 {
return vec4(1, 0, 1, 1)
}
return vec4(0, 1, 1, 1)
}
// Adjust texCoord into [0, 1].
t -= origin
if size != vec2(0) {
t /= size
}
if t.x >= 0.5 && t.y >= 0.5 {
return vec4(1, 0, 0, 1)
}
return vec4(0, 1, 0, 1)
}
`, unit)))
if err != nil {
t.Fatal(err)
}
for _, withSrc := range []bool{false, true} {
withSrc := withSrc
title := "WithSrc,unit=" + unit
if !withSrc {
title = "WithoutSrc,unit=" + unit
}
t.Run(title, func(t *testing.T) {
dst := ebiten.NewImage(dstW, dstH)
const (
offsetX = (dstW - srcW) / 2
offsetY = (dstH - srcH) / 2
)
op := &ebiten.DrawRectShaderOptions{}
op.GeoM.Translate(offsetX, offsetY)
if withSrc {
op.Images[0] = src
}
dst.DrawRectShader(srcW, srcH, s, op)
for j := 0; j < dstH; j++ {
for i := 0; i < dstW; i++ {
got := dst.At(i, j).(color.RGBA)
var want color.RGBA
if offsetX <= i && i < offsetX+srcW && offsetY <= j && j < offsetY+srcH {
var blue byte
if !withSrc && unit == "texel" {
blue = 0xff
}
if offsetX+srcW/2 <= i && offsetY+srcH/2 <= j {
want = color.RGBA{0xff, 0, blue, 0xff}
} else {
want = color.RGBA{0, 0xff, blue, 0xff}
}
}
if got != want {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
})
}
}
}