mirror of
https://github.com/italicsjenga/slang-shaders.git
synced 2024-11-22 15:51:30 +11:00
add mdapt shader and preset
This commit is contained in:
parent
fd330e5110
commit
7226419f24
26
dithering/mdapt.slangp
Normal file
26
dithering/mdapt.slangp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
shaders = 5
|
||||||
|
|
||||||
|
shader0 = shaders/mdapt/passes/mdapt-pass0.slang
|
||||||
|
filter_linear0 = false
|
||||||
|
scale_type0 = source
|
||||||
|
scale0 = 1.0
|
||||||
|
|
||||||
|
shader1 = shaders/mdapt/passes/mdapt-pass1.slang
|
||||||
|
filter_linear1 = false
|
||||||
|
scale_type1 = source
|
||||||
|
scale1 = 1.0
|
||||||
|
|
||||||
|
shader2 = shaders/mdapt/passes/mdapt-pass2.slang
|
||||||
|
filter_linear2 = false
|
||||||
|
scale_type2 = source
|
||||||
|
scale2 = 1.0
|
||||||
|
|
||||||
|
shader3 = shaders/mdapt/passes/mdapt-pass3.slang
|
||||||
|
filter_linear3 = false
|
||||||
|
scale_type3 = source
|
||||||
|
scale3 = 1.0
|
||||||
|
|
||||||
|
shader4 = shaders/mdapt/passes/mdapt-pass4.slang
|
||||||
|
filter_linear4 = false
|
||||||
|
scale_type4 = source
|
||||||
|
scale4 = 1.0
|
15
dithering/shaders/mdapt/README.md
Normal file
15
dithering/shaders/mdapt/README.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# mdapt v2.8 - merge dithering and pseudo transparency
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
In many old arcade or console games you will find dithering effects which are there to compensate for platform weaknesses like missing transparency support or small color palettes. This works well since back then the monitors (CRTs) had scanline bleeding and other certain features which merged the dithering through the display technology. But nowadays every pixel will displayed perfectly so dithering won't look like it should be.
|
||||||
|
|
||||||
|
There are already shaders out there who are trying to simulate how a CRT displays an image. mdapt though goes a different way and tries to detect dithering patterns by analyzing the relation between neighbored pixels. This way only these specific parts of the image are blended. The resulting image (still in the original resolution) is now a good base for further scaling with advanced algorithms (like xBR) which on there own usually have a hard time with dithering.
|
||||||
|
|
||||||
|
## Algorithm
|
||||||
|
|
||||||
|
mdapt can detect two basic dithering patterns. Checkerboard (CB) and vertical lines (VL). It actually doesn't matter of how many colors the pattern consists and mdapt doesn't use difference thresholds to determinie similarity at all. The algorithm just looks for regular "up and downs" between the pixels. There will always be errors though since the dithering process itself is lossy and not invertible. But mdapt tries to balance it by checking if there are enough detections in one local area.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
In RetroArch's shader options load one of the provided .cgp files. There are several configuration parameters which you can use via the parameter submenu.
|
94
dithering/shaders/mdapt/passes/mdapt-pass0.slang
Normal file
94
dithering/shaders/mdapt/passes/mdapt-pass0.slang
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
/*
|
||||||
|
Merge Dithering and Pseudo Transparency Shader v2.8 - Pass 0
|
||||||
|
by Sp00kyFox, 2014
|
||||||
|
|
||||||
|
Neighbor analysis via color metric and dot product of the difference vectors.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
layout(push_constant) uniform Push
|
||||||
|
{
|
||||||
|
vec4 SourceSize;
|
||||||
|
vec4 OriginalSize;
|
||||||
|
vec4 OutputSize;
|
||||||
|
uint FrameCount;
|
||||||
|
float MODE;
|
||||||
|
float PWR;
|
||||||
|
} params;
|
||||||
|
|
||||||
|
#pragma parameter MODE "MDAPT Monochrome Analysis" 0.0 0.0 1.0 1.0
|
||||||
|
#pragma parameter PWR "MDAPT Color Metric Exp" 2.0 0.0 10.0 0.1
|
||||||
|
|
||||||
|
layout(std140, set = 0, binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 MVP;
|
||||||
|
} global;
|
||||||
|
|
||||||
|
#define dot(x,y) clamp(dot(x,y), 0.0, 1.0) // NVIDIA Fix
|
||||||
|
#define TEX(dx,dy) texture(Source, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
|
||||||
|
// Reference: http://www.compuphase.com/cmetric.htm
|
||||||
|
float eq(vec3 A, vec3 B)
|
||||||
|
{
|
||||||
|
vec3 diff = A-B;
|
||||||
|
float ravg = (A.x + B.x) * 0.5;
|
||||||
|
|
||||||
|
diff *= diff * vec3(2.0 + ravg, 4.0, 3.0 - ravg);
|
||||||
|
|
||||||
|
return pow( smoothstep(3.0, 0.0, sqrt(diff.x + diff.y + diff.z)), params.PWR );
|
||||||
|
}
|
||||||
|
|
||||||
|
float and(float a, float b, float c, float d, float e, float f){
|
||||||
|
return min(a, min(b, min(c, min(d, min(e,f)))));
|
||||||
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
U
|
||||||
|
L C R
|
||||||
|
D
|
||||||
|
*/
|
||||||
|
|
||||||
|
vec3 C = TEX( 0., 0.).xyz;
|
||||||
|
vec3 L = TEX(-1., 0.).xyz;
|
||||||
|
vec3 R = TEX( 1., 0.).xyz;
|
||||||
|
vec3 U = TEX( 0.,-1.).xyz;
|
||||||
|
vec3 D = TEX( 0., 1.).xyz;
|
||||||
|
|
||||||
|
|
||||||
|
vec3 res = vec3(0.0);
|
||||||
|
|
||||||
|
if(params.MODE > 0.5){
|
||||||
|
res.x = float((L == R) && (C != L));
|
||||||
|
res.y = float((U == D) && (C != U));
|
||||||
|
res.z = float(bool(res.x) && bool(res.y) && (L == U));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
vec3 dCL = normalize(C-L), dCR = normalize(C-R), dCD = normalize(C-D), dCU = normalize(C-U);
|
||||||
|
|
||||||
|
res.x = dot(dCL, dCR) * eq(L,R);
|
||||||
|
res.y = dot(dCU, dCD) * eq(U,D);
|
||||||
|
res.z = and(res.x, res.y, dot(dCL, dCU) * eq(L,U), dot(dCL, dCD) * eq(L,D), dot(dCR, dCU) * eq(R,U), dot(dCR, dCD) * eq(R,D));
|
||||||
|
}
|
||||||
|
|
||||||
|
FragColor = vec4(res, 1.0);
|
||||||
|
}
|
85
dithering/shaders/mdapt/passes/mdapt-pass1.slang
Normal file
85
dithering/shaders/mdapt/passes/mdapt-pass1.slang
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
/*
|
||||||
|
Merge Dithering and Pseudo Transparency Shader v2.8 - Pass 1
|
||||||
|
by Sp00kyFox, 2014
|
||||||
|
|
||||||
|
Preparing checkerboard patterns.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
layout(push_constant) uniform Push
|
||||||
|
{
|
||||||
|
vec4 SourceSize;
|
||||||
|
vec4 OriginalSize;
|
||||||
|
vec4 OutputSize;
|
||||||
|
uint FrameCount;
|
||||||
|
} params;
|
||||||
|
|
||||||
|
#define TEX(dx,dy) texture(Source, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
|
||||||
|
|
||||||
|
float and(float a, float b){
|
||||||
|
return min(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float and(float a, float b, float c){
|
||||||
|
return min(a, min(b,c));
|
||||||
|
}
|
||||||
|
|
||||||
|
float or(float a, float b){
|
||||||
|
return max(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float or(float a, float b, float c, float d, float e){
|
||||||
|
return max(a, max(b, max(c, max(d,e))));
|
||||||
|
}
|
||||||
|
|
||||||
|
layout(std140, set = 0, binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 MVP;
|
||||||
|
} global;
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
UL U UR
|
||||||
|
L C R
|
||||||
|
DL D DR
|
||||||
|
*/
|
||||||
|
|
||||||
|
vec3 C = TEX( 0., 0.).xyz;
|
||||||
|
vec3 L = TEX(-1., 0.).xyz;
|
||||||
|
vec3 R = TEX( 1., 0.).xyz;
|
||||||
|
vec3 D = TEX( 0., 1.).xyz;
|
||||||
|
vec3 U = TEX( 0.,-1.).xyz;
|
||||||
|
|
||||||
|
float UL = TEX(-1.,-1.).z;
|
||||||
|
float UR = TEX( 1.,-1.).z;
|
||||||
|
float DL = TEX(-1., 1.).z;
|
||||||
|
float DR = TEX( 1., 1.).z;
|
||||||
|
|
||||||
|
// Checkerboard Pattern Completion
|
||||||
|
float prCB = or(C.z,
|
||||||
|
and(L.z, R.z, or(U.x, D.x)),
|
||||||
|
and(U.z, D.z, or(L.y, R.y)),
|
||||||
|
and(C.x, or(and(UL, UR), and(DL, DR))),
|
||||||
|
and(C.y, or(and(UL, DL), and(UR, DR))));
|
||||||
|
FragColor = vec4(C.x, prCB, 0.0, 0.0);
|
||||||
|
}
|
107
dithering/shaders/mdapt/passes/mdapt-pass2.slang
Normal file
107
dithering/shaders/mdapt/passes/mdapt-pass2.slang
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
/*
|
||||||
|
Merge Dithering and Pseudo Transparency Shader v2.8 - Pass 2
|
||||||
|
by Sp00kyFox, 2014
|
||||||
|
|
||||||
|
Eliminating isolated detections.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
layout(push_constant) uniform Push
|
||||||
|
{
|
||||||
|
vec4 SourceSize;
|
||||||
|
vec4 OriginalSize;
|
||||||
|
vec4 OutputSize;
|
||||||
|
uint FrameCount;
|
||||||
|
float VL_LO;
|
||||||
|
float VL_HI;
|
||||||
|
float CB_LO;
|
||||||
|
float CB_HI;
|
||||||
|
} params;
|
||||||
|
|
||||||
|
#pragma parameter VL_LO "MDAPT VL LO Thresh" 1.25 0.0 10.0 0.05
|
||||||
|
#pragma parameter VL_HI "MDAPT VL HI Thresh" 1.75 0.0 10.0 0.05
|
||||||
|
#pragma parameter CB_LO "MDAPT CB LO Thresh" 5.25 0.0 25.0 0.05
|
||||||
|
#pragma parameter CB_HI "MDAPT CB HI Thresh" 5.75 0.0 25.0 0.05
|
||||||
|
|
||||||
|
layout(std140, set = 0, binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 MVP;
|
||||||
|
} global;
|
||||||
|
|
||||||
|
#define TEX(dx,dy) texture(Source, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
#define and(x,y) min(x,y)
|
||||||
|
#define or(x,y) max(x,y)
|
||||||
|
|
||||||
|
vec2 sigmoid(vec2 signal){
|
||||||
|
return smoothstep(vec2(params.VL_LO, params.CB_LO), vec2(params.VL_HI, params.CB_HI), signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
NW UUL U2 UUR NE
|
||||||
|
ULL UL U1 UR URR
|
||||||
|
L2 L1 C R1 R2
|
||||||
|
DLL DL D1 DR DRR
|
||||||
|
SW DDL D2 DDR SE
|
||||||
|
*/
|
||||||
|
|
||||||
|
vec2 C = TEX( 0., 0.).xy;
|
||||||
|
|
||||||
|
|
||||||
|
vec2 hits = vec2(0.0);
|
||||||
|
|
||||||
|
//phase 1
|
||||||
|
vec2 L1 = TEX(-1., 0.).xy;
|
||||||
|
vec2 R1 = TEX( 1., 0.).xy;
|
||||||
|
vec2 U1 = TEX( 0.,-1.).xy;
|
||||||
|
vec2 D1 = TEX( 0., 1.).xy;
|
||||||
|
|
||||||
|
//phase 2
|
||||||
|
vec2 L2 = and(TEX(-2., 0.).xy, L1);
|
||||||
|
vec2 R2 = and(TEX( 2., 0.).xy, R1);
|
||||||
|
vec2 U2 = and(TEX( 0.,-2.).xy, U1);
|
||||||
|
vec2 D2 = and(TEX( 0., 2.).xy, D1);
|
||||||
|
vec2 UL = and(TEX(-1.,-1.).xy, or(L1, U1));
|
||||||
|
vec2 UR = and(TEX( 1.,-1.).xy, or(R1, U1));
|
||||||
|
vec2 DL = and(TEX(-1., 1.).xy, or(L1, D1));
|
||||||
|
vec2 DR = and(TEX( 1., 1.).xy, or(R1, D1));
|
||||||
|
|
||||||
|
//phase 3
|
||||||
|
vec2 ULL = and(TEX(-2.,-1.).xy, or(L2, UL));
|
||||||
|
vec2 URR = and(TEX( 2.,-1.).xy, or(R2, UR));
|
||||||
|
vec2 DRR = and(TEX( 2., 1.).xy, or(R2, DR));
|
||||||
|
vec2 DLL = and(TEX(-2., 1.).xy, or(L2, DL));
|
||||||
|
vec2 UUL = and(TEX(-1.,-2.).xy, or(U2, UL));
|
||||||
|
vec2 UUR = and(TEX( 1.,-2.).xy, or(U2, UR));
|
||||||
|
vec2 DDR = and(TEX( 1., 2.).xy, or(D2, DR));
|
||||||
|
vec2 DDL = and(TEX(-1., 2.).xy, or(D2, DL));
|
||||||
|
|
||||||
|
//phase 4
|
||||||
|
hits += and(TEX(-2.,-2.).xy, or(UUL, ULL));
|
||||||
|
hits += and(TEX( 2.,-2.).xy, or(UUR, URR));
|
||||||
|
hits += and(TEX(-2., 2.).xy, or(DDL, DLL));
|
||||||
|
hits += and(TEX( 2., 2.).xy, or(DDR, DRR));
|
||||||
|
|
||||||
|
hits += (ULL + URR + DRR + DLL + L2 + R2) + vec2(0.0, 1.0) * (C + U1 + U2 + D1 + D2 + L1 + R1 + UL + UR + DL + DR + UUL + UUR + DDR + DDL);
|
||||||
|
|
||||||
|
FragColor = vec4(C * sigmoid(hits), C);
|
||||||
|
}
|
93
dithering/shaders/mdapt/passes/mdapt-pass3.slang
Normal file
93
dithering/shaders/mdapt/passes/mdapt-pass3.slang
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
/*
|
||||||
|
Merge Dithering and Pseudo Transparency Shader v2.8 - Pass 3
|
||||||
|
by Sp00kyFox, 2014
|
||||||
|
|
||||||
|
Backpropagation and checkerboard smoothing.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
layout(push_constant) uniform Push
|
||||||
|
{
|
||||||
|
vec4 SourceSize;
|
||||||
|
vec4 OriginalSize;
|
||||||
|
vec4 OutputSize;
|
||||||
|
uint FrameCount;
|
||||||
|
} params;
|
||||||
|
|
||||||
|
layout(std140, set = 0, binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 MVP;
|
||||||
|
} global;
|
||||||
|
|
||||||
|
#define TEX(dx,dy) texture(Source, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
#define TEXt0(dx,dy) texture(Original, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
|
||||||
|
bool eq(vec3 A, vec3 B){
|
||||||
|
return (A == B);
|
||||||
|
}
|
||||||
|
|
||||||
|
float and(float a, float b){
|
||||||
|
return min(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float or(float a, float b, float c, float d, float e, float f, float g, float h, float i){
|
||||||
|
return max(a, max(b, max(c, max(d, max(e, max(f, max(g, max(h,i))))))));
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 and(vec2 a, vec2 b){
|
||||||
|
return min(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 or(vec2 a, vec2 b){
|
||||||
|
return max(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 or(vec2 a, vec2 b, vec2 c, vec2 d){
|
||||||
|
return max(a, max(b, max(c,d)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
layout(set = 0, binding = 3) uniform sampler2D Original;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
UL U UR
|
||||||
|
L C R
|
||||||
|
DL D DR
|
||||||
|
*/
|
||||||
|
|
||||||
|
vec4 C = TEX( 0., 0.); vec3 c = TEXt0( 0., 0.).xyz;
|
||||||
|
vec2 L = TEX(-1., 0.).xy; vec3 l = TEXt0(-1., 0.).xyz;
|
||||||
|
vec2 R = TEX( 1., 0.).xy; vec3 r = TEXt0( 1., 0.).xyz;
|
||||||
|
vec2 U = TEX( 0.,-1.).xy; vec3 u = TEXt0( 0.,-1.).xyz;
|
||||||
|
vec2 D = TEX( 0., 1.).xy; vec3 d = TEXt0( 0., 1.).xyz;
|
||||||
|
float UL = TEX(-1.,-1.).y; vec3 ul = TEXt0(-1.,-1.).xyz;
|
||||||
|
float UR = TEX( 1.,-1.).y; vec3 ur = TEXt0( 1.,-1.).xyz;
|
||||||
|
float DL = TEX(-1., 1.).y; vec3 dl = TEXt0(-1., 1.).xyz;
|
||||||
|
float DR = TEX( 1., 1.).y; vec3 dr = TEXt0( 1., 1.).xyz;
|
||||||
|
|
||||||
|
// Backpropagation
|
||||||
|
C.xy = or(C.xy, and(C.zw, or(L, R, U, D)));
|
||||||
|
|
||||||
|
// Checkerboard Smoothing
|
||||||
|
C.y = or(C.y, min(U.y, float(eq(c,u))), min(D.y, float(eq(c,d))), min(L.y, float(eq(c,l))), min(R.y, float(eq(c,r))), min(UL, float(eq(c,ul))), min(UR, float(eq(c,ur))), min(DL, float(eq(c,dl))), min(DR, float(eq(c,dr))));
|
||||||
|
|
||||||
|
FragColor = vec4(C);
|
||||||
|
}
|
150
dithering/shaders/mdapt/passes/mdapt-pass4.slang
Normal file
150
dithering/shaders/mdapt/passes/mdapt-pass4.slang
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
/*
|
||||||
|
Merge Dithering and Pseudo Transparency Shader v2.8 - Pass 4
|
||||||
|
by Sp00kyFox, 2014
|
||||||
|
|
||||||
|
Blends pixels based on detected dithering patterns.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
layout(push_constant) uniform Push
|
||||||
|
{
|
||||||
|
vec4 SourceSize;
|
||||||
|
vec4 OriginalSize;
|
||||||
|
vec4 OutputSize;
|
||||||
|
uint FrameCount;
|
||||||
|
float VL;
|
||||||
|
float CB;
|
||||||
|
float DEBUG;
|
||||||
|
} params;
|
||||||
|
|
||||||
|
#pragma parameter VL "MDAPT Vertical Lines" 0.0 0.0 1.0 1.0
|
||||||
|
#pragma parameter CB "MDAPT Checkerboard" 1.0 0.0 1.0 1.0
|
||||||
|
#pragma parameter DEBUG "MDAPT Adjust View" 0.0 0.0 1.0 1.0
|
||||||
|
|
||||||
|
layout(std140, set = 0, binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 MVP;
|
||||||
|
} global;
|
||||||
|
|
||||||
|
#define TEX(dx,dy) texture(Source, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
#define TEXt0(dx,dy) texture(Original, vTexCoord+vec2((dx),(dy))*params.SourceSize.zw)
|
||||||
|
|
||||||
|
bool eq(vec3 A, vec3 B){
|
||||||
|
return (A == B);
|
||||||
|
}
|
||||||
|
|
||||||
|
float and(float a, float b){
|
||||||
|
return min(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float or(float a, float b){
|
||||||
|
return max(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float or(float a, float b, float c, float d, float e, float f, float g, float h, float i){
|
||||||
|
return max(a, max(b, max(c, max(d, max(e, max(f, max(g, max(h,i))))))));
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 and(vec2 a, vec2 b){
|
||||||
|
return min(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 or(vec2 a, vec2 b){
|
||||||
|
return max(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 or(vec2 a, vec2 b, vec2 c, vec2 d){
|
||||||
|
return max(a, max(b, max(c,d)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
layout(set = 0, binding = 3) uniform sampler2D Original;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
UL U UR
|
||||||
|
L C R
|
||||||
|
DL D DR
|
||||||
|
*/
|
||||||
|
|
||||||
|
vec4 C = TEX( 0., 0.); vec3 c = TEXt0( 0., 0.).xyz;
|
||||||
|
vec2 L = TEX(-1., 0.).xy; vec3 l = TEXt0(-1., 0.).xyz;
|
||||||
|
vec2 R = TEX( 1., 0.).xy; vec3 r = TEXt0( 1., 0.).xyz;
|
||||||
|
vec2 U = TEX( 0.,-1.).xy;
|
||||||
|
vec2 D = TEX( 0., 1.).xy;
|
||||||
|
|
||||||
|
float prVL = 0.0, prCB = 0.0;
|
||||||
|
vec3 fVL = vec3(0.0), fCB = vec3(0.0);
|
||||||
|
|
||||||
|
|
||||||
|
// Backpropagation
|
||||||
|
C.xy = or(C.xy, and(C.zw, or(L.xy, R.xy, U.xy, D.xy)));
|
||||||
|
|
||||||
|
|
||||||
|
if(params.VL > 0.5){
|
||||||
|
float prSum = L.x + R.x;
|
||||||
|
|
||||||
|
prVL = max(L.x, R.x);
|
||||||
|
prVL = (prVL == 0.0) ? 1.0 : prSum/prVL;
|
||||||
|
|
||||||
|
fVL = (prVL*c + L.x*l + R.x*r)/(prVL + prSum);
|
||||||
|
prVL = C.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(params.CB > 0.5){
|
||||||
|
vec3 u = TEXt0( 0.,-1.).xyz;
|
||||||
|
vec3 d = TEXt0( 0., 1.).xyz;
|
||||||
|
|
||||||
|
float eqCL = float(eq(c,l));
|
||||||
|
float eqCR = float(eq(c,r));
|
||||||
|
float eqCU = float(eq(c,u));
|
||||||
|
float eqCD = float(eq(c,d));
|
||||||
|
|
||||||
|
float prU = or(U.y, eqCU);
|
||||||
|
float prD = or(D.y, eqCD);
|
||||||
|
float prL = or(L.y, eqCL);
|
||||||
|
float prR = or(R.y, eqCR);
|
||||||
|
|
||||||
|
|
||||||
|
float prSum = prU + prD + prL + prR;
|
||||||
|
|
||||||
|
prCB = max(prL, max(prR, max(prU,prD)));
|
||||||
|
prCB = (prCB == 0.0) ? 1.0 : prSum/prCB;
|
||||||
|
|
||||||
|
//standard formula: C/2 + (L + R + D + U)/8
|
||||||
|
fCB = (prCB*c + prU*u + prD*d + prL*l + prR*r)/(prCB + prSum);
|
||||||
|
|
||||||
|
|
||||||
|
float UL = TEX(-1.,-1.).y; vec3 ul = TEXt0(-1.,-1.).xyz;
|
||||||
|
float UR = TEX( 1.,-1.).y; vec3 ur = TEXt0( 1.,-1.).xyz;
|
||||||
|
float DL = TEX(-1., 1.).y; vec3 dl = TEXt0(-1., 1.).xyz;
|
||||||
|
float DR = TEX( 1., 1.).y; vec3 dr = TEXt0( 1., 1.).xyz;
|
||||||
|
|
||||||
|
// Checkerboard Smoothing
|
||||||
|
prCB = or(C.y, and(L.y, eqCL), and(R.y, eqCR), and(U.y, eqCU), and(D.y, eqCD), and(UL, float(eq(c,ul))), and(UR, float(eq(c,ur))), and(DL, float(eq(c,dl))), and(DR, float(eq(c,dr))));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(params.DEBUG > 0.5)
|
||||||
|
FragColor = vec4(prVL, prCB, 0.0, 0.0);
|
||||||
|
|
||||||
|
FragColor = (prCB >= prVL) ? vec4(mix(c, fCB, prCB), 1.0) : vec4(mix(c, fVL, prVL), 1.0);
|
||||||
|
}
|
Loading…
Reference in a new issue