ebiten/internal/graphics/shader.go

156 lines
4.1 KiB
Go
Raw Normal View History

// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-09 15:16:04 +01:00
package graphics
2013-06-15 10:07:14 +02:00
import (
"strings"
"github.com/hajimehoshi/ebiten/internal/web"
)
2013-12-12 16:31:00 +01:00
type shaderId int
const (
shaderVertexModelview shaderId = iota
shaderFragmentTexture
2013-12-12 16:31:00 +01:00
)
func shader(id shaderId) string {
s := 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
2015-01-02 15:30:22 +01:00
}
var shaders = map[shaderId]string{
shaderVertexModelview: `
{{Definitions}}
uniform mat4 projection_matrix;
attribute vec2 vertex;
attribute vec4 tex_coord;
attribute vec4 geo_matrix_body;
attribute vec2 geo_matrix_translation;
2017-12-03 11:07:12 +01:00
varying vec2 varying_tex_coord;
varying vec2 varying_tex_coord_min;
varying vec2 varying_tex_coord_max;
2013-06-15 10:07:14 +02:00
void main(void) {
2017-12-03 11:07:12 +01:00
varying_tex_coord = vec2(tex_coord[0], tex_coord[1]);
varying_tex_coord_min =
vec2(min(tex_coord[0], tex_coord[2]), min(tex_coord[1], tex_coord[3]));
2017-12-03 11:07:12 +01:00
varying_tex_coord_max =
vec2(max(tex_coord[0], tex_coord[2]), max(tex_coord[1], tex_coord[3]));
2016-10-24 20:04:06 +02:00
mat4 geo_matrix = mat4(
vec4(geo_matrix_body[0], geo_matrix_body[2], 0, 0),
vec4(geo_matrix_body[1], geo_matrix_body[3], 0, 0),
vec4(0, 0, 1, 0),
vec4(geo_matrix_translation, 0, 1)
);
2016-10-24 20:04:06 +02:00
gl_Position = projection_matrix * geo_matrix * vec4(vertex, 0, 1);
}
2013-06-15 10:07:14 +02:00
`,
shaderFragmentTexture: `
2017-07-24 15:59:25 +02:00
#if defined(GL_ES)
precision mediump float;
2017-07-24 15:59:25 +02:00
#else
#define lowp
#define mediump
#define highp
#endif
{{Definitions}}
uniform sampler2D texture;
uniform mat4 color_matrix;
uniform vec4 color_matrix_translation;
uniform vec2 source_size;
uniform int filter_type;
2017-12-03 11:07:12 +01:00
varying vec2 varying_tex_coord;
varying vec2 varying_tex_coord_min;
varying vec2 varying_tex_coord_max;
2013-06-15 10:07:14 +02:00
vec2 roundTexel(vec2 p) {
#if defined(IS_MOBILE_BROWSER)
// This is a temporary hack: on mobile browsers like Android Chrome,
// the code to round a float might cause unexpected behavior.
// This is confirmed with rendering a relatively large (e.g. 4096x512) image
// with specifying a source rectangle near its edges.
// 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;
}
if (factor.y > 0.0) {
p.y -= mod(p.y + factor.y * 0.5, factor.y) - factor.y * 0.5;
}
return p;
#endif
}
vec4 getColorAt(vec2 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) {
return vec4(0, 0, 0, 0);
}
return texture2D(texture, pos);
}
2013-06-15 10:07:14 +02:00
void main(void) {
vec4 color = vec4(0, 0, 0, 0);
vec2 pos = roundTexel(varying_tex_coord);
if (filter_type == 1) {
// Nearest neighbor
color = getColorAt(pos);
} else if (filter_type == 2) {
// Bi-linear
vec2 texel_size = 1.0 / source_size;
pos -= texel_size * 0.5;
vec4 c0 = getColorAt(pos);
vec4 c1 = getColorAt(pos + vec2(texel_size.x, 0));
vec4 c2 = getColorAt(pos + vec2(0, texel_size.y));
vec4 c3 = getColorAt(pos + texel_size);
float rateX = fract(pos.x * source_size.x);
float rateY = fract(pos.y * source_size.y);
color = mix(mix(c0, c1, rateX), mix(c2, c3, rateX), rateY);
} else {
color = vec4(1, 0, 0, 1);
}
// Un-premultiply alpha
2016-06-26 08:57:45 +02:00
if (0.0 < color.a) {
color.rgb /= color.a;
}
// Apply the color matrix
color = (color_matrix * color) + color_matrix_translation;
color = clamp(color, 0.0, 1.0);
// Premultiply alpha
color.rgb *= color.a;
2014-12-22 14:49:14 +01:00
gl_FragColor = color;
}
2013-12-12 16:31:00 +01:00
`,
}