graphics: texel should be represented as highp for precision

Fixes #460
This commit is contained in:
Hajime Hoshi 2017-12-13 23:45:05 +09:00
parent 4928899566
commit 704d4cf464

View File

@ -14,33 +14,19 @@
package graphics package graphics
import ( type shaderID int
"strings"
"github.com/hajimehoshi/ebiten/internal/web"
)
type shaderId int
const ( const (
shaderVertexModelview shaderId = iota shaderVertexModelview shaderID = iota
shaderFragmentTexture shaderFragmentTexture
) )
func shader(id shaderId) string { func shader(id shaderID) string {
s := shaders[id] return shaders[id]
defs := []string{}
if web.IsMobileBrowser() {
defs = append(defs, "#define IS_MOBILE_BROWSER")
}
s = strings.Replace(s, "{{Definitions}}", strings.Join(defs, "\n"), -1)
return s
} }
var shaders = map[shaderId]string{ var shaders = map[shaderID]string{
shaderVertexModelview: ` shaderVertexModelview: `
{{Definitions}}
uniform mat4 projection_matrix; uniform mat4 projection_matrix;
attribute vec2 vertex; attribute vec2 vertex;
attribute vec4 tex_coord; attribute vec4 tex_coord;
@ -74,40 +60,29 @@ precision mediump float;
#define highp #define highp
#endif #endif
{{Definitions}}
uniform sampler2D texture; uniform sampler2D texture;
uniform mat4 color_matrix; uniform mat4 color_matrix;
uniform vec4 color_matrix_translation; uniform vec4 color_matrix_translation;
uniform vec2 source_size; uniform vec2 source_size;
uniform int filter_type; uniform int filter_type;
varying vec2 varying_tex_coord; varying highp vec2 varying_tex_coord;
varying vec2 varying_tex_coord_min; varying highp vec2 varying_tex_coord_min;
varying vec2 varying_tex_coord_max; varying highp vec2 varying_tex_coord_max;
vec2 roundTexel(vec2 p) { highp vec2 roundTexel(highp vec2 p) {
#if defined(IS_MOBILE_BROWSER) // highp (relative) precision is 2^(-16) in the spec.
// This is a temporary hack: on mobile browsers like Android Chrome, // As the maximum source size is 4096, the minimum value for a denominator is
// the code to round a float might cause unexpected behavior. // 65536 (= 4096 * 16).
// This is confirmed with rendering a relatively large (e.g. 4096x512) image highp vec2 factor = 1.0 / (source_size * 16.0);
// with specifying a source rectangle near its edges. if (factor.x * 0.5 > 0.0 && factor.y * 0.5 > 0.0) {
// TODO: examples/moire doesn't work anyway.
// TODO: Does this function work on an Android native app?
return p;
#else
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; 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; p.y -= mod(p.y + factor.y * 0.5, factor.y) - factor.y * 0.5;
} }
return p; return p;
#endif
} }
vec4 getColorAt(vec2 pos) { vec4 getColorAt(highp vec2 pos) {
if (pos.x < varying_tex_coord_min.x || if (pos.x < varying_tex_coord_min.x ||
pos.y < varying_tex_coord_min.y || pos.y < varying_tex_coord_min.y ||
varying_tex_coord_max.x <= pos.x || varying_tex_coord_max.x <= pos.x ||
@ -126,7 +101,7 @@ void main(void) {
color = getColorAt(pos); color = getColorAt(pos);
} else if (filter_type == 2) { } else if (filter_type == 2) {
// Bi-linear // Bi-linear
vec2 texel_size = 1.0 / source_size; highp vec2 texel_size = 1.0 / source_size;
pos -= texel_size * 0.5; pos -= texel_size * 0.5;
vec4 c0 = getColorAt(pos); vec4 c0 = getColorAt(pos);
vec4 c1 = getColorAt(pos + vec2(texel_size.x, 0)); vec4 c1 = getColorAt(pos + vec2(texel_size.x, 0));