2014-12-24 03:04:10 +01:00
|
|
|
// 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
|
|
|
|
2015-01-26 15:32:50 +01:00
|
|
|
package graphics
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2017-12-13 15:45:05 +01:00
|
|
|
type shaderID int
|
2013-12-12 16:31:00 +01:00
|
|
|
|
|
|
|
const (
|
2017-12-13 15:45:05 +01:00
|
|
|
shaderVertexModelview shaderID = iota
|
2015-01-12 10:34:02 +01:00
|
|
|
shaderFragmentTexture
|
2013-12-12 16:31:00 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 15:45:05 +01:00
|
|
|
func shader(id shaderID) string {
|
|
|
|
return shaders[id]
|
2015-01-02 15:30:22 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:45:05 +01:00
|
|
|
var shaders = map[shaderID]string{
|
2015-01-17 08:09:09 +01:00
|
|
|
shaderVertexModelview: `
|
2017-07-28 19:57:56 +02:00
|
|
|
uniform mat4 projection_matrix;
|
|
|
|
attribute vec2 vertex;
|
2017-12-02 12:51:18 +01:00
|
|
|
attribute vec4 tex_coord;
|
2017-07-28 19:57:56 +02:00
|
|
|
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 =
|
2017-12-02 12:51:18 +01:00
|
|
|
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 =
|
2017-12-02 12:51:18 +01:00
|
|
|
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),
|
2016-10-24 18:56:59 +02:00
|
|
|
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);
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2013-06-15 10:07:14 +02:00
|
|
|
`,
|
2015-01-12 10:34:02 +01:00
|
|
|
shaderFragmentTexture: `
|
2017-07-24 15:59:25 +02:00
|
|
|
#if defined(GL_ES)
|
2017-07-24 14:56:40 +02:00
|
|
|
precision mediump float;
|
2017-07-24 15:59:25 +02:00
|
|
|
#else
|
|
|
|
#define lowp
|
|
|
|
#define mediump
|
|
|
|
#define highp
|
2017-07-24 14:56:40 +02:00
|
|
|
#endif
|
|
|
|
|
2017-07-28 21:07:47 +02:00
|
|
|
uniform sampler2D texture;
|
|
|
|
uniform mat4 color_matrix;
|
|
|
|
uniform vec4 color_matrix_translation;
|
2017-12-15 19:28:48 +01:00
|
|
|
uniform highp vec2 source_size;
|
2017-12-11 19:27:56 +01:00
|
|
|
uniform int filter_type;
|
2017-12-11 15:07:01 +01:00
|
|
|
|
2017-12-13 15:45:05 +01:00
|
|
|
varying highp vec2 varying_tex_coord;
|
|
|
|
varying highp vec2 varying_tex_coord_min;
|
|
|
|
varying highp vec2 varying_tex_coord_max;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2017-12-13 15:45:05 +01:00
|
|
|
highp vec2 roundTexel(highp vec2 p) {
|
|
|
|
// highp (relative) precision is 2^(-16) in the spec.
|
2017-12-15 17:42:36 +01:00
|
|
|
// The minimum value for a denominator is half of 65536.
|
|
|
|
highp float factor = 1.0 / 32768.0;
|
|
|
|
p.x -= mod(p.x + factor * 0.5, factor) - factor * 0.5;
|
|
|
|
p.y -= mod(p.y + factor * 0.5, factor) - factor * 0.5;
|
2017-12-11 15:07:01 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:07:14 +02:00
|
|
|
void main(void) {
|
2017-12-02 12:51:18 +01:00
|
|
|
vec4 color = vec4(0, 0, 0, 0);
|
2017-12-11 15:07:01 +01:00
|
|
|
|
2017-12-15 19:28:48 +01:00
|
|
|
highp vec2 pos = roundTexel(varying_tex_coord);
|
2017-12-11 15:07:01 +01:00
|
|
|
if (filter_type == 1) {
|
|
|
|
// Nearest neighbor
|
2017-12-16 10:35:45 +01:00
|
|
|
if (varying_tex_coord_min.x <= pos.x &&
|
|
|
|
varying_tex_coord_min.y <= pos.y &&
|
|
|
|
pos.x < varying_tex_coord_max.x &&
|
|
|
|
pos.y < varying_tex_coord_max.y) {
|
|
|
|
color = texture2D(texture, pos);
|
|
|
|
}
|
2017-12-11 15:07:01 +01:00
|
|
|
} else if (filter_type == 2) {
|
|
|
|
// Bi-linear
|
2017-12-13 15:45:05 +01:00
|
|
|
highp vec2 texel_size = 1.0 / source_size;
|
2017-12-11 15:07:01 +01:00
|
|
|
pos -= texel_size * 0.5;
|
2017-12-15 20:09:04 +01:00
|
|
|
|
2017-12-17 19:10:04 +01:00
|
|
|
highp vec2 p0 = pos;
|
|
|
|
highp vec2 p1 = pos + texel_size;
|
|
|
|
vec4 c0 = texture2D(texture, p0);
|
|
|
|
vec4 c1 = texture2D(texture, vec2(p1.x, p0.y));
|
|
|
|
vec4 c2 = texture2D(texture, vec2(p0.x, p1.y));
|
|
|
|
vec4 c3 = texture2D(texture, p1);
|
|
|
|
if (p0.x < varying_tex_coord_min.x) {
|
2017-12-15 20:09:04 +01:00
|
|
|
c0 = vec4(0, 0, 0, 0);
|
|
|
|
c2 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
2017-12-17 19:10:04 +01:00
|
|
|
if (p0.y < varying_tex_coord_min.y) {
|
2017-12-15 20:09:04 +01:00
|
|
|
c0 = vec4(0, 0, 0, 0);
|
|
|
|
c1 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
2017-12-17 19:10:04 +01:00
|
|
|
if (varying_tex_coord_max.x <= p1.x) {
|
2017-12-15 20:09:04 +01:00
|
|
|
c1 = vec4(0, 0, 0, 0);
|
|
|
|
c3 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
2017-12-17 19:10:04 +01:00
|
|
|
if (varying_tex_coord_max.y <= p1.y) {
|
2017-12-15 20:09:04 +01:00
|
|
|
c2 = vec4(0, 0, 0, 0);
|
|
|
|
c3 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2017-12-17 19:10:04 +01:00
|
|
|
vec2 rate = fract(pos * source_size);
|
|
|
|
color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
2017-12-11 15:07:01 +01:00
|
|
|
} else {
|
2017-12-15 20:09:04 +01:00
|
|
|
// Error
|
2017-12-11 15:07:01 +01:00
|
|
|
color = vec4(1, 0, 0, 1);
|
2017-12-02 12:51:18 +01:00
|
|
|
}
|
2014-12-19 21:22:10 +01:00
|
|
|
|
2016-06-22 17:25:31 +02:00
|
|
|
// Un-premultiply alpha
|
2016-06-26 08:57:45 +02:00
|
|
|
if (0.0 < color.a) {
|
|
|
|
color.rgb /= color.a;
|
|
|
|
}
|
2016-06-22 17:25:31 +02:00
|
|
|
// 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-20 10:07:25 +01:00
|
|
|
|
2014-12-22 14:49:14 +01:00
|
|
|
gl_FragColor = color;
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
`,
|
|
|
|
}
|