From 762dfe1cb5c80ee922ea04fbfc38e29e40bd178a Mon Sep 17 00:00:00 2001 From: hunterk Date: Thu, 21 Jul 2016 08:39:08 -0500 Subject: [PATCH] add lcd-cgwg and dot shaders to handheld --- handheld/dot.slang | 98 ++++++++++++++++++++++++ handheld/lcd-cgwg/lcd-grid-v2.slang | 114 ++++++++++++++++++++++++++++ handheld/lcd-cgwg/lcd-grid.slang | 91 ++++++++++++++++++++++ 3 files changed, 303 insertions(+) create mode 100644 handheld/dot.slang create mode 100644 handheld/lcd-cgwg/lcd-grid-v2.slang create mode 100644 handheld/lcd-cgwg/lcd-grid.slang diff --git a/handheld/dot.slang b/handheld/dot.slang new file mode 100644 index 0000000..e9379e2 --- /dev/null +++ b/handheld/dot.slang @@ -0,0 +1,98 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#define gamma 2.4 +#define shine 0.05 +#define blend 0.65 + +/* + Author: Themaister + License: Public domain +*/ + +#define TEX(coord) texture(Source, vTexCoord).rgb + +float dist(vec2 coord, vec2 source) +{ + vec2 delta = coord - source; + return sqrt(dot(delta, delta)); +} + +float color_bloom(vec3 color) +{ + const vec3 gray_coeff = vec3(0.30, 0.59, 0.11); + float bright = dot(color, gray_coeff); + return mix(1.0 + shine, 1.0 - shine, bright); +} + +vec3 lookup(vec2 pixel_no, float offset_x, float offset_y, vec3 color) +{ + vec2 offset = vec2(offset_x, offset_y); + float delta = dist(fract(pixel_no), offset + vec2(0.5, 0.5)); + return color * exp(-gamma * delta * color_bloom(color)); +} + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; +layout(location = 1) out vec4 c00_10; +layout(location = 2) out vec4 c00_01; +layout(location = 3) out vec4 c20_01; +layout(location = 4) out vec4 c21_02; +layout(location = 5) out vec4 c12_22; +layout(location = 6) out vec2 c11; +layout(location = 7) out vec2 pixel_no; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; + + float dx = global.SourceSize.z; + float dy = global.SourceSize.w; + + c00_10 = vec4(vTexCoord + vec2(-dx, -dy), vTexCoord + vec2(0, -dy)); + c20_01 = vec4(vTexCoord + vec2(dx, -dy), vTexCoord + vec2(-dx, 0)); + c21_02 = vec4(vTexCoord + vec2(dx, 0), vTexCoord + vec2(-dx, dy)); + c12_22 = vec4(vTexCoord + vec2(0, dy), vTexCoord + vec2(dx, dy)); + c11 = vTexCoord; + pixel_no = vTexCoord * global.SourceSize.xy; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in vec4 c00_10; +layout(location = 2) in vec4 c00_01; +layout(location = 3) in vec4 c20_01; +layout(location = 4) in vec4 c21_02; +layout(location = 5) in vec4 c12_22; +layout(location = 6) in vec2 c11; +layout(location = 7) in vec2 pixel_no; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +void main() +{ + vec3 mid_color = lookup(pixel_no, 0.0, 0.0, TEX(c11)); + vec3 color = vec3(0.0, 0.0, 0.0); + color += lookup(pixel_no, -1.0, -1.0, TEX(c00_10.xy)); + color += lookup(pixel_no, 0.0, -1.0, TEX(c00_10.zw)); + color += lookup(pixel_no, 1.0, -1.0, TEX(c20_01.xy)); + color += lookup(pixel_no, -1.0, 0.0, TEX(c20_01.zw)); + color += mid_color; + color += lookup(pixel_no, 1.0, 0.0, TEX(c21_02.xy)); + color += lookup(pixel_no, -1.0, 1.0, TEX(c21_02.zw)); + color += lookup(pixel_no, 0.0, 1.0, TEX(c12_22.xy)); + color += lookup(pixel_no, 1.0, 1.0, TEX(c12_22.zw)); + vec3 out_color = mix(1.2 * mid_color, color, blend); + + FragColor = vec4(out_color, 1.0); +} \ No newline at end of file diff --git a/handheld/lcd-cgwg/lcd-grid-v2.slang b/handheld/lcd-cgwg/lcd-grid-v2.slang new file mode 100644 index 0000000..7593d86 --- /dev/null +++ b/handheld/lcd-cgwg/lcd-grid-v2.slang @@ -0,0 +1,114 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#define RSUBPIX_R 1.0 +#define RSUBPIX_G 0.0 +#define RSUBPIX_B 0.0 +#define GSUBPIX_R 0.0 +#define GSUBPIX_G 1.0 +#define GSUBPIX_B 0.0 +#define BSUBPIX_R 0.0 +#define BSUBPIX_G 0.0 +#define BSUBPIX_B 1.0 +#define gain 1.0 +#define gamma 3.0 +#define blacklevel 0.05 +#define ambient 0.0 +#define BGR 0.0 + +#define outgamma 2.2 + +#define fetch_offset(coord,offset) (pow(vec3(gain) * texelFetchOffset(Source, (coord), 0, (offset)).rgb + vec3(blacklevel), vec3(gamma)) + vec3(ambient)) + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +// integral of (1 - x^2 - x^4 + x^6)^2 +float coeffs_x[] = float[](1.0, -2.0/3.0, -1.0/5.0, 4.0/7.0, -1.0/9.0, -2.0/11.0, 1.0/13.0); +// integral of (1 - 2x^4 + x^6)^2 +float coeffs_y[] = float[](1.0, 0.0, -4.0/5.0, 2.0/7.0, 4.0/9.0, -4.0/11.0, 1.0/13.0); + +float intsmear_func(float z, float coeffs[7]) +{ + float z2 = z*z; + float zn = z; + float ret = 0.0; + for (int i = 0; i < 7; i++) { + ret += zn*coeffs[i]; + zn *= z2; + } + return ret; +} + +float intsmear(float x, float dx, float d, float coeffs[7]) +{ + float zl = clamp((x-dx*0.5)/d,-1.0,1.0); + float zh = clamp((x+dx*0.5)/d,-1.0,1.0); + return d * ( intsmear_func(zh,coeffs) - intsmear_func(zl,coeffs) )/dx; +} + +void main() +{ + vec2 texelSize = global.SourceSize.zw; + vec2 range; + range = global.SourceSize.xy / (global.OutputSize.xy * global.SourceSize.xy); + + vec3 cred = pow(vec3(RSUBPIX_R, RSUBPIX_G, RSUBPIX_B), vec3(outgamma)); + vec3 cgreen = pow(vec3(GSUBPIX_R, GSUBPIX_G, GSUBPIX_B), vec3(outgamma)); + vec3 cblue = pow(vec3(BSUBPIX_R, BSUBPIX_G, BSUBPIX_B), vec3(outgamma)); + + ivec2 tli = ivec2(floor(vTexCoord/texelSize-vec2(0.4999))); + + vec3 lcol, rcol; + float subpix = (vTexCoord.x/texelSize.x - 0.4999 - float(tli.x))*3.0; + float rsubpix = range.x/texelSize.x * 3.0; + + lcol = vec3(intsmear(subpix+1.0,rsubpix, 1.5, coeffs_x), + intsmear(subpix ,rsubpix, 1.5, coeffs_x), + intsmear(subpix-1.0,rsubpix, 1.5, coeffs_x)); + rcol = vec3(intsmear(subpix-2.0,rsubpix, 1.5, coeffs_x), + intsmear(subpix-3.0,rsubpix, 1.5, coeffs_x), + intsmear(subpix-4.0,rsubpix, 1.5, coeffs_x)); + + if (BGR > 0.5) { + lcol.rgb = lcol.bgr; + rcol.rgb = rcol.bgr; + } + + float tcol, bcol; + subpix = vTexCoord.y/texelSize.y - 0.4999 - float(tli.y); + rsubpix = range.y/texelSize.y; + tcol = intsmear(subpix ,rsubpix, 0.63, coeffs_y); + bcol = intsmear(subpix-1.0,rsubpix, 0.63, coeffs_y); + + vec3 topLeftColor = fetch_offset(tli, ivec2(0,0)) * lcol * vec3(tcol); + vec3 bottomRightColor = fetch_offset(tli, ivec2(1,1)) * rcol * vec3(bcol); + vec3 bottomLeftColor = fetch_offset(tli, ivec2(0,1)) * lcol * vec3(bcol); + vec3 topRightColor = fetch_offset(tli, ivec2(1,0)) * rcol * vec3(tcol); + + vec3 averageColor = topLeftColor + bottomRightColor + bottomLeftColor + topRightColor; + + averageColor = averageColor * mat3(cred, cgreen, cblue); + + FragColor = vec4(pow(averageColor,vec3(1.0/outgamma)),0.0); +} \ No newline at end of file diff --git a/handheld/lcd-cgwg/lcd-grid.slang b/handheld/lcd-cgwg/lcd-grid.slang new file mode 100644 index 0000000..54f3603 --- /dev/null +++ b/handheld/lcd-cgwg/lcd-grid.slang @@ -0,0 +1,91 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#define GRID_STRENGTH 0.05 + +#define outgamma 2.2 + +#define round(x) floor( (x) + 0.5 ) +#define gamma 2.2 +#define TEX2D(c) pow(texture(Source, (c)), vec4(gamma)) + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +float intsmear_func(float z) +{ + float z2 = z*z; + float z4 = z2*z2; + float z8 = z4*z4; + return z - 2.0/3.0*z*z2 - 1.0/5.0*z*z4 + 4.0/7.0*z*z2*z4 - 1.0/9.0*z*z8 + - 2.0/11.0*z*z2*z8 + 1.0/13.0*z*z4*z8; +} + +float intsmear(float x, float dx) +{ + const float d = 1.5; + float zl = clamp((x-dx)/d,-1.0,1.0); + float zh = clamp((x+dx)/d,-1.0,1.0); + return d * ( intsmear_func(zh) - intsmear_func(zl) )/(2.0*dx); +} + +void main() +{ + vec2 texelSize = global.SourceSize.zw; + vec2 subtexelSize = texelSize / vec2(3.0,1.0); + vec2 range; + range = global.SourceSize.xy / (global.OutputSize.xy * global.SourceSize.xy); + + float left = vTexCoord.x - texelSize.x*0.5; + float top = vTexCoord.y + range.y; + float right = vTexCoord.x + texelSize.x*0.5; + float bottom = vTexCoord.y - range.y; + + vec4 lcol, rcol; + float subpix = mod(vTexCoord.x/subtexelSize.x+1.5,3.0); + float rsubpix = range.x/subtexelSize.x; + lcol = vec4(intsmear(subpix+1.0,rsubpix),intsmear(subpix ,rsubpix), + intsmear(subpix-1.0,rsubpix),0.0); + rcol = vec4(intsmear(subpix-2.0,rsubpix),intsmear(subpix-3.0,rsubpix), + intsmear(subpix-4.0,rsubpix),0.0); + + vec4 topLeftColor = TEX2D((floor(vec2(left, top) / texelSize) + 0.5) * texelSize) * lcol; + vec4 bottomRightColor = TEX2D((floor(vec2(right, bottom) / texelSize) + 0.5) * texelSize) * rcol; + vec4 bottomLeftColor = TEX2D((floor(vec2(left, bottom) / texelSize) + 0.5) * texelSize) * lcol; + vec4 topRightColor = TEX2D((floor(vec2(right, top) / texelSize) + 0.5) * texelSize) * rcol; + + vec2 border = round(vTexCoord.st/subtexelSize); + vec2 bordert = clamp((border+vec2(0.0,+GRID_STRENGTH)) * subtexelSize, + vec2(left, bottom), vec2(right, top)); + vec2 borderb = clamp((border+vec2(0.0,-GRID_STRENGTH)) * subtexelSize, + vec2(left, bottom), vec2(right, top)); + float totalArea = 2.0 * range.y; + + vec4 averageColor; + averageColor = ((top - bordert.y) / totalArea) * topLeftColor; + averageColor += ((borderb.y - bottom) / totalArea) * bottomRightColor; + averageColor += ((borderb.y - bottom) / totalArea) * bottomLeftColor; + averageColor += ((top - bordert.y) / totalArea) * topRightColor; + + FragColor = pow(averageColor,vec4(1.0/gamma)); +} \ No newline at end of file