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
|
|
|
|
2018-10-29 17:27:31 +01:00
|
|
|
package opengl
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2017-12-20 16:24:27 +01:00
|
|
|
import (
|
2018-12-24 08:02:08 +01:00
|
|
|
"fmt"
|
2019-01-07 16:07:47 +01:00
|
|
|
"regexp"
|
2017-12-20 16:24:27 +01:00
|
|
|
"strings"
|
2018-12-24 08:02:08 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2017-12-20 16:24:27 +01:00
|
|
|
)
|
|
|
|
|
2019-01-07 16:07:47 +01:00
|
|
|
// glslReservedKeywords is a set of reserved keywords that cannot be used as an indentifier on some environments.
|
|
|
|
// See https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf.
|
|
|
|
var glslReservedKeywords = map[string]struct{}{
|
|
|
|
"common": {}, "partition": {}, "active": {},
|
|
|
|
"asm": {},
|
|
|
|
"class": {}, "union": {}, "enum": {}, "typedef": {}, "template": {}, "this": {},
|
|
|
|
"resource": {},
|
|
|
|
"goto": {},
|
|
|
|
"inline": {}, "noinline": {}, "public": {}, "static": {}, "extern": {}, "external": {}, "interface": {},
|
|
|
|
"long": {}, "short": {}, "half": {}, "fixed": {}, "unsigned": {}, "superp": {},
|
|
|
|
"input": {}, "output": {},
|
|
|
|
"hvec2": {}, "hvec3": {}, "hvec4": {}, "fvec2": {}, "fvec3": {}, "fvec4": {},
|
|
|
|
"filter": {},
|
|
|
|
"sizeof": {}, "cast": {},
|
|
|
|
"namespace": {}, "using": {},
|
|
|
|
"sampler3DRect": {},
|
|
|
|
}
|
|
|
|
|
|
|
|
var glslIdentifier = regexp.MustCompile(`[_a-zA-Z][_a-zA-Z0-9]*`)
|
|
|
|
|
|
|
|
func checkGLSL(src string) {
|
|
|
|
for _, l := range strings.Split(src, "\n") {
|
|
|
|
if strings.Contains(l, "//") {
|
|
|
|
l = l[:strings.Index(l, "//")]
|
|
|
|
}
|
|
|
|
for _, token := range glslIdentifier.FindAllString(l, -1) {
|
|
|
|
if _, ok := glslReservedKeywords[token]; ok {
|
|
|
|
panic(fmt.Sprintf("opengl: %q is a reserved keyword", token))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 03:00:18 +01:00
|
|
|
func vertexShaderStr() string {
|
|
|
|
src := shaderStrVertex
|
|
|
|
checkGLSL(src)
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
|
|
|
|
func fragmentShaderStr(filter graphics.Filter, address graphics.Address) string {
|
|
|
|
replaces := map[string]string{
|
|
|
|
"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero),
|
|
|
|
"{{.AddressRepeat}}": fmt.Sprintf("%d", graphics.AddressRepeat),
|
|
|
|
}
|
|
|
|
src := shaderStrFragment
|
|
|
|
for k, v := range replaces {
|
|
|
|
src = strings.Replace(src, k, v, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var defs []string
|
|
|
|
switch filter {
|
|
|
|
case graphics.FilterNearest:
|
|
|
|
defs = append(defs, "#define FILTER_NEAREST")
|
|
|
|
case graphics.FilterLinear:
|
|
|
|
defs = append(defs, "#define FILTER_LINEAR")
|
|
|
|
case graphics.FilterScreen:
|
|
|
|
defs = append(defs, "#define FILTER_SCREEN")
|
2017-12-20 16:57:36 +01:00
|
|
|
default:
|
2019-02-12 03:00:18 +01:00
|
|
|
panic(fmt.Sprintf("opengl: invalid filter: %d", filter))
|
2017-12-20 16:24:27 +01:00
|
|
|
}
|
2019-02-12 03:00:18 +01:00
|
|
|
switch address {
|
|
|
|
case graphics.AddressClampToZero:
|
|
|
|
defs = append(defs, "#define ADDRESS_CLAMP_TO_ZERO")
|
|
|
|
case graphics.AddressRepeat:
|
|
|
|
defs = append(defs, "#define ADDRESS_REPEAT")
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("opengl: invalid address: %d", address))
|
|
|
|
}
|
|
|
|
|
|
|
|
src = strings.Replace(src, "{{.Definitions}}", strings.Join(defs, "\n"), -1)
|
2019-01-07 16:07:47 +01:00
|
|
|
|
|
|
|
checkGLSL(src)
|
|
|
|
return src
|
2015-01-02 15:30:22 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 16:24:27 +01:00
|
|
|
const (
|
|
|
|
shaderStrVertex = `
|
2018-11-17 14:29:04 +01:00
|
|
|
uniform vec2 viewport_size;
|
2018-11-17 15:23:47 +01:00
|
|
|
attribute vec2 vertex;
|
2018-12-22 17:40:43 +01:00
|
|
|
attribute vec2 tex;
|
|
|
|
attribute vec4 tex_region;
|
2018-08-09 20:33:28 +02:00
|
|
|
attribute vec4 color_scale;
|
2018-12-22 17:40:43 +01:00
|
|
|
varying vec2 varying_tex;
|
|
|
|
varying vec4 varying_tex_region;
|
2018-08-09 20:33:28 +02:00
|
|
|
varying vec4 varying_color_scale;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2018-12-22 17:40:43 +01:00
|
|
|
varying_tex = tex;
|
|
|
|
varying_tex_region = tex_region;
|
2018-08-09 20:33:28 +02:00
|
|
|
varying_color_scale = color_scale;
|
2018-11-17 14:29:04 +01:00
|
|
|
|
|
|
|
mat4 projection_matrix = mat4(
|
|
|
|
vec4(2.0 / viewport_size.x, 0, 0, 0),
|
|
|
|
vec4(0, 2.0 / viewport_size.y, 0, 0),
|
|
|
|
vec4(0, 0, 1, 0),
|
|
|
|
vec4(-1, -1, 0, 1)
|
|
|
|
);
|
2018-11-17 15:23:47 +01:00
|
|
|
gl_Position = projection_matrix * vec4(vertex, 0, 1);
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2017-12-20 16:24:27 +01:00
|
|
|
`
|
|
|
|
shaderStrFragment = `
|
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
|
|
|
|
|
2019-02-12 03:00:18 +01:00
|
|
|
{{.Definitions}}
|
2017-12-20 16:24:27 +01:00
|
|
|
|
2017-07-28 21:07:47 +02:00
|
|
|
uniform sampler2D texture;
|
2018-08-05 14:30:06 +02:00
|
|
|
uniform mat4 color_matrix_body;
|
|
|
|
uniform vec4 color_matrix_translation;
|
2017-12-20 16:24:27 +01:00
|
|
|
|
2017-12-15 19:28:48 +01:00
|
|
|
uniform highp vec2 source_size;
|
2017-12-11 15:07:01 +01:00
|
|
|
|
2018-02-22 03:46:46 +01:00
|
|
|
#if defined(FILTER_SCREEN)
|
|
|
|
uniform highp float scale;
|
|
|
|
#endif
|
|
|
|
|
2018-12-22 17:40:43 +01:00
|
|
|
varying highp vec2 varying_tex;
|
|
|
|
varying highp vec4 varying_tex_region;
|
2018-08-09 20:33:28 +02:00
|
|
|
varying highp vec4 varying_color_scale;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2018-08-29 17:17:03 +02:00
|
|
|
// adjustTexel adjusts the two texels and returns the adjusted second texel.
|
|
|
|
// When p1 - p0 is exactly equal to the texel size, jaggy can happen on macOS (#669).
|
|
|
|
// In order to avoid this jaggy, subtract a little bit from the second texel.
|
|
|
|
highp vec2 adjustTexel(highp vec2 p0, highp vec2 p1) {
|
|
|
|
highp vec2 texel_size = 1.0 / source_size;
|
|
|
|
if (fract((p1.x-p0.x)*source_size.x) == 0.0) {
|
|
|
|
p1.x -= texel_size.x / 512.0;
|
|
|
|
}
|
|
|
|
if (fract((p1.y-p0.y)*source_size.y) == 0.0) {
|
|
|
|
p1.y -= texel_size.y / 512.0;
|
|
|
|
}
|
|
|
|
return p1;
|
|
|
|
}
|
|
|
|
|
2018-12-24 09:45:16 +01:00
|
|
|
highp float floorMod(highp float x, highp float y) {
|
2018-12-23 19:00:00 +01:00
|
|
|
if (x < 0.0) {
|
|
|
|
return y - (-x - y * floor(-x/y));
|
|
|
|
}
|
|
|
|
return x - y * floor(x/y);
|
|
|
|
}
|
|
|
|
|
2019-02-12 03:00:18 +01:00
|
|
|
highp vec2 adjustTexelByAddress(highp vec2 p, highp vec4 tex_region) {
|
|
|
|
#if defined(ADDRESS_CLAMP_TO_ZERO)
|
|
|
|
return p;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ADDRESS_REPEAT)
|
|
|
|
highp vec2 o = vec2(tex_region[0], tex_region[1]);
|
|
|
|
highp vec2 size = vec2(tex_region[2] - tex_region[0], tex_region[3] - tex_region[1]);
|
|
|
|
return vec2(floorMod((p.x - o.x), size.x) + o.x, floorMod((p.y - o.y), size.y) + o.y);
|
|
|
|
#endif
|
2018-12-23 19:00:00 +01:00
|
|
|
}
|
|
|
|
|
2013-06-15 10:07:14 +02:00
|
|
|
void main(void) {
|
2018-12-22 17:40:43 +01:00
|
|
|
highp vec2 pos = varying_tex;
|
2017-12-20 16:24:27 +01:00
|
|
|
highp vec2 texel_size = 1.0 / source_size;
|
|
|
|
|
2018-12-24 08:02:08 +01:00
|
|
|
vec4 color;
|
|
|
|
|
2019-02-12 03:00:18 +01:00
|
|
|
#if defined(FILTER_NEAREST)
|
|
|
|
pos = adjustTexelByAddress(pos, varying_tex_region);
|
|
|
|
color = texture2D(texture, pos);
|
|
|
|
if (pos.x < varying_tex_region[0] ||
|
|
|
|
pos.y < varying_tex_region[1] ||
|
|
|
|
(varying_tex_region[2] - texel_size.x / 512.0) <= pos.x ||
|
|
|
|
(varying_tex_region[3] - texel_size.y / 512.0) <= pos.y) {
|
|
|
|
color = vec4(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(FILTER_LINEAR)
|
|
|
|
highp vec2 p0 = pos - texel_size / 2.0;
|
|
|
|
highp vec2 p1 = pos + texel_size / 2.0;
|
|
|
|
|
|
|
|
p1 = adjustTexel(p0, p1);
|
|
|
|
p0 = adjustTexelByAddress(p0, varying_tex_region);
|
|
|
|
p1 = adjustTexelByAddress(p1, varying_tex_region);
|
|
|
|
|
|
|
|
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_region[0]) {
|
|
|
|
c0 = vec4(0, 0, 0, 0);
|
|
|
|
c2 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if (p0.y < varying_tex_region[1]) {
|
|
|
|
c0 = vec4(0, 0, 0, 0);
|
|
|
|
c1 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if ((varying_tex_region[2] - texel_size.x / 512.0) <= p1.x) {
|
|
|
|
c1 = vec4(0, 0, 0, 0);
|
|
|
|
c3 = vec4(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if ((varying_tex_region[3] - texel_size.y / 512.0) <= p1.y) {
|
|
|
|
c2 = vec4(0, 0, 0, 0);
|
|
|
|
c3 = vec4(0, 0, 0, 0);
|
2017-12-20 16:24:27 +01:00
|
|
|
}
|
2018-02-22 03:46:46 +01:00
|
|
|
|
2019-02-12 03:00:18 +01:00
|
|
|
vec2 rate = fract(p0 * source_size);
|
|
|
|
color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(FILTER_SCREEN)
|
|
|
|
highp vec2 p0 = pos - texel_size / 2.0 / scale;
|
|
|
|
highp vec2 p1 = pos + texel_size / 2.0 / scale;
|
|
|
|
|
|
|
|
// Prevent this variable from being optimized out.
|
|
|
|
p0 += varying_tex_region.xy - varying_tex_region.xy;
|
|
|
|
|
|
|
|
p1 = adjustTexel(p0, p1);
|
|
|
|
|
|
|
|
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);
|
|
|
|
// Texels must be in the source rect, so it is not necessary to check that like linear filter.
|
|
|
|
|
|
|
|
vec2 rateCenter = vec2(1.0, 1.0) - texel_size / 2.0 / scale;
|
|
|
|
vec2 rate = clamp(((fract(p0 * source_size) - rateCenter) * scale) + rateCenter, 0.0, 1.0);
|
|
|
|
color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
2018-08-09 20:33:28 +02:00
|
|
|
// Apply the color matrix or scale.
|
2018-08-05 14:30:06 +02:00
|
|
|
color = (color_matrix_body * color) + color_matrix_translation;
|
2018-08-09 20:33:28 +02:00
|
|
|
color *= varying_color_scale;
|
2016-06-22 17:25:31 +02:00
|
|
|
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
|
|
|
}
|
2017-12-20 16:24:27 +01:00
|
|
|
`
|
|
|
|
)
|