graphicsdriver: Bug fix: Tie-breaking when picking a texel

When a texel is picked on texel-borders of a texture, the behavior
(tie-breaking) depends on GPU and unexpected. This change fixes this
issue by shifting 1/512 [texel] when picking a texel up.

Updates #1212
This commit is contained in:
Hajime Hoshi 2020-06-24 20:46:32 +09:00
parent ebb3bcfeb5
commit 20b5be0886
3 changed files with 60 additions and 9 deletions

View File

@ -1327,13 +1327,59 @@ func TestImageLinearFilterGlitch(t *testing.T) {
want = color.RGBA{0, 0, 0, 0xff} want = color.RGBA{0, 0, 0, 0xff}
} }
if got != want { if got != want {
t.Errorf("src.At(%d, %d): filter: %d, got: %v, want: %v", i, j, f, got, want) t.Errorf("dst.At(%d, %d): filter: %d, got: %v, want: %v", i, j, f, got, want)
} }
} }
} }
} }
} }
// Issue #1212
func TestImageLinearFilterGlitch2(t *testing.T) {
const w, h = 100, 100
src, _ := NewImage(w, h, FilterDefault)
dst, _ := NewImage(w, h, FilterDefault)
idx := 0
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
if i+j < 100 {
pix[4*idx] = 0
pix[4*idx+1] = 0
pix[4*idx+2] = 0
pix[4*idx+3] = 0xff
} else {
pix[4*idx] = 0xff
pix[4*idx+1] = 0xff
pix[4*idx+2] = 0xff
pix[4*idx+3] = 0xff
}
idx++
}
}
src.ReplacePixels(pix)
op := &DrawImageOptions{}
op.Filter = FilterLinear
dst.DrawImage(src, op)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
var want color.RGBA
if i+j < 100 {
want = color.RGBA{0, 0, 0, 0xff}
} else {
want = color.RGBA{0xff, 0xff, 0xff, 0xff}
}
if !sameColors(got, want, 1) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}
func TestImageAddressRepeat(t *testing.T) { func TestImageAddressRepeat(t *testing.T) {
const w, h = 16, 16 const w, h = 16, 16
src, _ := NewImage(w, h, FilterDefault) src, _ := NewImage(w, h, FilterDefault)

View File

@ -139,8 +139,10 @@ struct ColorFromTexel<FILTER_LINEAR, address> {
constexpr sampler texture_sampler(filter::nearest); constexpr sampler texture_sampler(filter::nearest);
const float2 texel_size = 1 / source_size; const float2 texel_size = 1 / source_size;
float2 p0 = v.tex - texel_size / 2.0; // Shift 1/512 [texel] to avoid the tie-breaking issue.
float2 p1 = v.tex + texel_size / 2.0; // As all the vertex positions are aligned to 1/16 [pixel], this shiting should work in most cases.
float2 p0 = v.tex - texel_size / 2.0 + (texel_size / 512.0);
float2 p1 = v.tex + texel_size / 2.0 + (texel_size / 512.0);
p0 = AdjustTexelByAddress<address>(p0, v.tex_region); p0 = AdjustTexelByAddress<address>(p0, v.tex_region);
p1 = AdjustTexelByAddress<address>(p1, v.tex_region); p1 = AdjustTexelByAddress<address>(p1, v.tex_region);
@ -177,8 +179,8 @@ struct ColorFromTexel<FILTER_SCREEN, address> {
constexpr sampler texture_sampler(filter::nearest); constexpr sampler texture_sampler(filter::nearest);
const float2 texel_size = 1 / source_size; const float2 texel_size = 1 / source_size;
float2 p0 = v.tex - texel_size / 2.0 / scale; float2 p0 = v.tex - texel_size / 2.0 / scale + (texel_size / 512.0);
float2 p1 = v.tex + texel_size / 2.0 / scale; float2 p1 = v.tex + texel_size / 2.0 / scale + (texel_size / 512.0);
float4 c0 = texture.sample(texture_sampler, p0); float4 c0 = texture.sample(texture_sampler, p0);
float4 c1 = texture.sample(texture_sampler, float2(p1.x, p0.y)); float4 c1 = texture.sample(texture_sampler, float2(p1.x, p0.y));

View File

@ -194,8 +194,8 @@ void main(void) {
#if defined(FILTER_LINEAR) #if defined(FILTER_LINEAR)
vec4 color; vec4 color;
highp vec2 texel_size = 1.0 / source_size; highp vec2 texel_size = 1.0 / source_size;
highp vec2 p0 = pos - texel_size / 2.0; highp vec2 p0 = pos - (texel_size) / 2.0 + (texel_size / 512.0);
highp vec2 p1 = pos + texel_size / 2.0; highp vec2 p1 = pos + (texel_size) / 2.0 + (texel_size / 512.0);
p0 = adjustTexelByAddress(p0, varying_tex_region); p0 = adjustTexelByAddress(p0, varying_tex_region);
p1 = adjustTexelByAddress(p1, varying_tex_region); p1 = adjustTexelByAddress(p1, varying_tex_region);
@ -228,8 +228,11 @@ void main(void) {
#if defined(FILTER_SCREEN) #if defined(FILTER_SCREEN)
highp vec2 texel_size = 1.0 / source_size; highp vec2 texel_size = 1.0 / source_size;
highp vec2 half_scaled_texel_size = texel_size / 2.0 / scale; highp vec2 half_scaled_texel_size = texel_size / 2.0 / scale;
highp vec2 p0 = pos - half_scaled_texel_size;
highp vec2 p1 = pos + half_scaled_texel_size; // Shift 1/512 [texel] to avoid the tie-breaking issue.
// As all the vertex positions are aligned to 1/16 [pixel], this shiting should work in most cases.
highp vec2 p0 = pos - half_scaled_texel_size + (texel_size / 512.0);
highp vec2 p1 = pos + half_scaled_texel_size + (texel_size / 512.0);
vec4 c0 = texture2D(texture, p0); vec4 c0 = texture2D(texture, p0);
vec4 c1 = texture2D(texture, vec2(p1.x, p0.y)); vec4 c1 = texture2D(texture, vec2(p1.x, p0.y));