graphics: Bug fix: More strict calculation for the nearest filter (#558)

This commit is contained in:
Hajime Hoshi 2018-03-20 03:51:32 +09:00
parent aaa603dd08
commit bae8f9d874
3 changed files with 31 additions and 25 deletions

View File

@ -504,7 +504,7 @@ func TestImageFill(t *testing.T) {
}
}
// Issue #317
// Issue #317, #558
func TestImageEdge(t *testing.T) {
const (
img0Width = 16
@ -537,7 +537,7 @@ func TestImageEdge(t *testing.T) {
transparent := color.RGBA{0, 0, 0, 0}
for _, f := range []Filter{FilterNearest, FilterLinear} {
for a := 0; a < 360; a += 5 {
for a := 0; a < 360; a++ {
img1.Clear()
op := &DrawImageOptions{}
w, h := img0.Size()

View File

@ -321,7 +321,6 @@ func (s *openGLState) useProgram(proj []float32, texture opengl.Texture, dst, sr
s.lastColorMatrixTranslation = esTranslate
}
if program == s.programLinear || program == s.programScreen {
sw, sh := src.Size()
sw = emath.NextPowerOf2Int(sw)
sh = emath.NextPowerOf2Int(sh)
@ -331,7 +330,7 @@ func (s *openGLState) useProgram(proj []float32, texture opengl.Texture, dst, sr
s.lastSourceWidth = sw
s.lastSourceHeight = sh
}
}
if program == s.programScreen {
sw, _ := src.Size()
dw, _ := dst.Size()

View File

@ -76,9 +76,7 @@ uniform sampler2D texture;
uniform mat4 color_matrix;
uniform vec4 color_matrix_translation;
#if defined(FILTER_LINEAR) || defined(FILTER_SCREEN)
uniform highp vec2 source_size;
#endif
#if defined(FILTER_SCREEN)
uniform highp float scale;
@ -104,19 +102,10 @@ void main(void) {
// roundTexel adjusts pos by rounding it (#315, #558).
pos = roundTexel(pos);
#if defined(FILTER_NEAREST)
vec4 color = texture2D(texture, pos);
if (pos.x < varying_tex_coord_min.x ||
pos.y < varying_tex_coord_min.y ||
varying_tex_coord_max.x <= pos.x ||
varying_tex_coord_max.y <= pos.y) {
color = vec4(0, 0, 0, 0);
}
#endif
#if defined(FILTER_LINEAR)
highp vec2 texel_size = 1.0 / source_size;
#if defined(FILTER_NEAREST) || defined(FILTER_LINEAR)
// Even with the nearest filter, it is better to calculate the color with around 4 texels (#558).
highp vec2 p0 = pos - texel_size / 2.0;
highp vec2 p1 = pos + texel_size / 2.0;
vec4 c0 = texture2D(texture, p0);
@ -141,11 +130,29 @@ void main(void) {
}
vec2 rate = fract(p0 * source_size);
#if defined(FILTER_NEAREST)
vec4 color;
if (rate.x < 0.5) {
if (rate.y < 0.5) {
color = c0;
} else {
color = c2;
}
} else {
if (rate.y < 0.5) {
color = c1;
} else {
color = c3;
}
}
#elif defined(FILTER_LINEAR)
vec4 color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
#endif
#endif
#if defined(FILTER_SCREEN)
highp vec2 texel_size = 1.0 / source_size;
pos -= texel_size / 2.0 / scale;
highp vec2 p0 = pos;