From 7b74a8f29fb7e8c64e350123a8094db2ef3e567f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 7 Dec 2017 03:24:46 +0900 Subject: [PATCH] graphics: Improve roundTexel implementation --- internal/graphics/shader.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index cff718497..fb1b4cf3f 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -71,12 +71,14 @@ varying vec2 varying_tex_coord_min; varying vec2 varying_tex_coord_max; vec2 roundTexel(vec2 p) { - // Many devices can't use highp in fragment shaders, so use only mediump here. - // According to the spec, mediump value range is -2**14 to 2**14 (16384). - // Some machines have higher precisions, but a very slight value difference causes - // a different result of getColorAt. This function avoids such flakines by rounding values. - float max_mediump = 16384.0; - return floor(p * max_mediump + 0.5) / max_mediump; + vec2 factor = 1.0 / (source_size * 256.0); + if (factor.x > 0.0) { + p.x -= mod(p.x + factor.x * 0.5, factor.x) - factor.x * 0.5; + } + if (factor.y > 0.0) { + p.y -= mod(p.y + factor.y * 0.5, factor.y) - factor.y * 0.5; + } + return p; } vec4 getColorAt(vec2 pos) {