mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-09 21:31:51 +09:00
option for screen filtering mode
This commit is contained in:
87
src/shaders/hq2x.frag
Normal file
87
src/shaders/hq2x.frag
Normal file
@@ -0,0 +1,87 @@
|
||||
// This float value should be defined from the compiling code.
|
||||
// #define SCALE [2, 3, 4].0
|
||||
|
||||
#version 150
|
||||
#ifdef GL_ES
|
||||
#define PRECISION mediump
|
||||
precision PRECISION float;
|
||||
precision PRECISION int;
|
||||
#else
|
||||
#define PRECISION
|
||||
#endif
|
||||
#define SCALE 2.0
|
||||
|
||||
uniform sampler2D u_texture;
|
||||
uniform sampler2D u_lut;
|
||||
uniform vec2 u_textureSize;
|
||||
|
||||
in vec4 v_texCoord[4];
|
||||
|
||||
const mat3 YUV_MATRIX = mat3(0.299, 0.587, 0.114, -0.169, -0.331, 0.5, 0.5, -0.419, -0.081);
|
||||
const vec3 YUV_THRESHOLD = vec3(48.0/255.0, 7.0/255.0, 6.0/255.0);
|
||||
const vec3 YUV_OFFSET = vec3(0, 0.5, 0.5);
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
bool diff(vec3 yuv1, vec3 yuv2) {
|
||||
return any(greaterThan(abs((yuv1 + YUV_OFFSET) - (yuv2 + YUV_OFFSET)), YUV_THRESHOLD));
|
||||
}
|
||||
|
||||
mat3 transpose(mat3 val) {
|
||||
mat3 result;
|
||||
result[0][1] = val[1][0];
|
||||
result[0][2] = val[2][0];
|
||||
result[1][0] = val[0][1];
|
||||
result[1][2] = val[2][1];
|
||||
result[2][0] = val[0][2];
|
||||
result[2][1] = val[1][2];
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 fp = fract(v_texCoord[0].xy * u_textureSize);
|
||||
vec2 quad = sign(-0.5 + fp);
|
||||
mat3 yuv = transpose(YUV_MATRIX);
|
||||
|
||||
float dx = v_texCoord[0].z;
|
||||
float dy = v_texCoord[0].w;
|
||||
vec3 p1 = texture(u_texture, v_texCoord[0].xy).rgb;
|
||||
vec3 p2 = texture(u_texture, v_texCoord[0].xy + vec2(dx, dy) * quad).rgb;
|
||||
vec3 p3 = texture(u_texture, v_texCoord[0].xy + vec2(dx, 0) * quad).rgb;
|
||||
vec3 p4 = texture(u_texture, v_texCoord[0].xy + vec2(0, dy) * quad).rgb;
|
||||
// Use mat4 instead of mat4x3 here to support GLES.
|
||||
mat4 pixels = mat4(vec4(p1, 0.0), vec4(p2, 0.0), vec4(p3, 0.0), vec4(p4, 0.0));
|
||||
|
||||
vec3 w1 = yuv * texture(u_texture, v_texCoord[1].xw).rgb;
|
||||
vec3 w2 = yuv * texture(u_texture, v_texCoord[1].yw).rgb;
|
||||
vec3 w3 = yuv * texture(u_texture, v_texCoord[1].zw).rgb;
|
||||
|
||||
vec3 w4 = yuv * texture(u_texture, v_texCoord[2].xw).rgb;
|
||||
vec3 w5 = yuv * p1;
|
||||
vec3 w6 = yuv * texture(u_texture, v_texCoord[2].zw).rgb;
|
||||
|
||||
vec3 w7 = yuv * texture(u_texture, v_texCoord[3].xw).rgb;
|
||||
vec3 w8 = yuv * texture(u_texture, v_texCoord[3].yw).rgb;
|
||||
vec3 w9 = yuv * texture(u_texture, v_texCoord[3].zw).rgb;
|
||||
|
||||
bvec3 pattern[3];
|
||||
pattern[0] = bvec3(diff(w5, w1), diff(w5, w2), diff(w5, w3));
|
||||
pattern[1] = bvec3(diff(w5, w4), false , diff(w5, w6));
|
||||
pattern[2] = bvec3(diff(w5, w7), diff(w5, w8), diff(w5, w9));
|
||||
bvec4 cross = bvec4(diff(w4, w2), diff(w2, w6), diff(w8, w4), diff(w6, w8));
|
||||
|
||||
vec2 index;
|
||||
index.x = dot(vec3(pattern[0]), vec3(1, 2, 4)) +
|
||||
dot(vec3(pattern[1]), vec3(8, 0, 16)) +
|
||||
dot(vec3(pattern[2]), vec3(32, 64, 128));
|
||||
index.y = dot(vec4(cross), vec4(1, 2, 4, 8)) * (SCALE * SCALE) +
|
||||
dot(floor(fp * SCALE), vec2(1.0, SCALE));
|
||||
|
||||
vec2 step = vec2(1.0) / vec2(256.0, 16.0 * (SCALE * SCALE));
|
||||
vec2 offset = step / vec2(2.0);
|
||||
vec4 weights = texture(u_lut, index * step + offset);
|
||||
float sum = dot(weights, vec4(1));
|
||||
vec3 res = (pixels * (weights / sum)).rgb;
|
||||
|
||||
fragColor.rgb = res;
|
||||
}
|
||||
BIN
src/shaders/hq2x.tga
LFS
Normal file
BIN
src/shaders/hq2x.tga
LFS
Normal file
Binary file not shown.
41
src/shaders/hq2x.vert
Normal file
41
src/shaders/hq2x.vert
Normal file
@@ -0,0 +1,41 @@
|
||||
#version 150
|
||||
#ifdef GL_ES
|
||||
#define PRECISION mediump
|
||||
precision PRECISION float;
|
||||
precision PRECISION int;
|
||||
#else
|
||||
#define PRECISION
|
||||
#endif
|
||||
|
||||
in vec4 a_position;
|
||||
in vec2 a_texCoord0;
|
||||
|
||||
uniform mat4 u_projTrans;
|
||||
|
||||
out vec2 u_textureSize;
|
||||
out vec4 v_texCoord[4];
|
||||
|
||||
void main() {
|
||||
gl_Position = u_projTrans * a_position;
|
||||
|
||||
vec2 ps = 1.0/u_textureSize;
|
||||
float dx = ps.x;
|
||||
float dy = ps.y;
|
||||
|
||||
// +----+----+----+
|
||||
// | | | |
|
||||
// | w1 | w2 | w3 |
|
||||
// +----+----+----+
|
||||
// | | | |
|
||||
// | w4 | w5 | w6 |
|
||||
// +----+----+----+
|
||||
// | | | |
|
||||
// | w7 | w8 | w9 |
|
||||
// +----+----+----+
|
||||
|
||||
v_texCoord[0].zw = ps;
|
||||
v_texCoord[0].xy = a_texCoord0.xy;
|
||||
v_texCoord[1] = a_texCoord0.xxxy + vec4(-dx, 0, dx, -dy); // w1 | w2 | w3
|
||||
v_texCoord[2] = a_texCoord0.xxxy + vec4(-dx, 0, dx, 0); // w4 | w5 | w6
|
||||
v_texCoord[3] = a_texCoord0.xxxy + vec4(-dx, 0, dx, dy); // w7 | w8 | w9
|
||||
}
|
||||
Reference in New Issue
Block a user