From 797716b17a10085b73c305cb008a7ab426d31833 Mon Sep 17 00:00:00 2001 From: Hyllian Date: Thu, 15 Sep 2022 09:41:16 -0300 Subject: [PATCH] Add gamma slider on checkerboard-dedither (#295) * Add Retro-Tiles shaders A shader designed for handheld and low res systems. It turns pixels into crisp tiles. * Add box scaling - Turn mandatory integer scaling; - Add Overscan option. * Fix gamma on retro-tiles shader - Increased gamma from 2.2 to 2.7 for a better white balance. * Add Gamma Slider to checkerboard-dedither - Add Gamma Slider to checkerboard-dedither. * Update checkerboard-dedither shaders - Add new pattern recognitions; - Add new option to mitigate false positives. --- .../checkerboard-dedither-pass1.slang | 71 +++++----- .../checkerboard-dedither-pass2.slang | 114 +++++++++------- .../checkerboard-dedither-pass3.slang | 125 +++++++++++------- .../checkerboard-dedither/linearize.slang | 8 +- 4 files changed, 184 insertions(+), 134 deletions(-) diff --git a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang index 18fbd0a..7fb6ac2 100644 --- a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang @@ -28,17 +28,16 @@ layout(push_constant) uniform Push { - vec4 SourceSize; - vec4 OriginalSize; - vec4 OutputSize; - uint FrameCount; - float CD_BLEND_OPTION; - float CD_BLEND_LEVEL; + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float CD_BLEND_OPTION; + float CD_BLEND_LEVEL; } params; -#pragma parameter CB-DEDITHER "[CHECKERBOARD-DEDITHER PARAMS]" 0.0 0.0 0.0 0.0 -#pragma parameter CD_BLEND_OPTION " [ OFF | ON ]" 1.0 0.0 1.0 1.0 -#pragma parameter CD_BLEND_LEVEL " Blend Level" 1.0 0.0 1.0 0.1 +#pragma parameter CD_BLEND_OPTION "CHECKERBOARD-DEDITHER: [ OFF | ON ]" 1.0 0.0 1.0 1.0 +#pragma parameter CD_BLEND_LEVEL " Blend Level" 1.0 0.0 1.0 0.1 #define CD_BLEND_OPTION params.CD_BLEND_OPTION #define CD_BLEND_LEVEL params.CD_BLEND_LEVEL @@ -50,7 +49,7 @@ vec3 max_s(vec3 central, vec3 adj1, vec3 adj2) {return max(central, min(adj1, ad layout(std140, set = 0, binding = 0) uniform UBO { - mat4 MVP; + mat4 MVP; } global; @@ -72,41 +71,37 @@ layout(set = 0, binding = 2) uniform sampler2D Source; void main() { - vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; - vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; + vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; + vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; - // Reading the texels. - vec3 C = texture(Source, vTexCoord ).xyz; - vec3 L = texture(Source, vTexCoord -dx).xyz; - vec3 R = texture(Source, vTexCoord +dx).xyz; - vec3 U = texture(Source, vTexCoord -dy).xyz; - vec3 D = texture(Source, vTexCoord +dy).xyz; - vec3 UL = texture(Source, vTexCoord -dx -dy).xyz; - vec3 UR = texture(Source, vTexCoord +dx -dy).xyz; - vec3 DL = texture(Source, vTexCoord -dx +dy).xyz; - vec3 DR = texture(Source, vTexCoord +dx +dy).xyz; + // Reading the texels. + vec3 C = texture(Source, vTexCoord ).xyz; + vec3 L = texture(Source, vTexCoord -dx).xyz; + vec3 R = texture(Source, vTexCoord +dx).xyz; + vec3 U = texture(Source, vTexCoord -dy).xyz; + vec3 D = texture(Source, vTexCoord +dy).xyz; + vec3 UL = texture(Source, vTexCoord -dx -dy).xyz; + vec3 UR = texture(Source, vTexCoord +dx -dy).xyz; + vec3 DL = texture(Source, vTexCoord -dx +dy).xyz; + vec3 DR = texture(Source, vTexCoord +dx +dy).xyz; - vec3 color = C; + vec3 color = C; - // Get min/max samples - vec3 min_sample = min_s(C, L, R); - vec3 max_sample = max_s(C, L, R); + if (CD_BLEND_OPTION == 1) + { + float diff = dot(max(C, max(L, R)) - min(C, min(L, R)), Y); - float diff = dot(max(max(C, L), max(C, R)) - min(min(C, L), min(C, R)), Y); + diff *= (1.0 - CD_BLEND_LEVEL); - diff *= (1.0 - CD_BLEND_LEVEL); + vec3 min_sample = max(min_s(C, L, R), min_s(C, U, D)); + vec3 max_sample = min(max_s(C, L, R), max_s(C, U, D)); - if (CD_BLEND_OPTION == 1) - { - min_sample = max(min_sample, min_s(C, U, D)); - max_sample = min(max_sample, max_s(C, U, D)); + color = 0.5*( 1.0 + diff )*C + 0.125*( 1.0 - diff )*(L + R + U + D); - color = 0.5*( 1.0 + diff )*C + 0.125*( 1.0 - diff )*(L + R + U + D); - } + color = clamp(color, min_sample, max_sample); + } - color = clamp(color, min_sample, max_sample); + float luma_diff = abs(dot(C, Y)-dot(color, Y)); - float luma_diff = abs(dot(C, Y)-dot(color, Y)); - - FragColor = vec4(color, luma_diff); + FragColor = vec4(color, luma_diff); } diff --git a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang index 5118023..0b5d452 100644 --- a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang @@ -28,26 +28,25 @@ layout(push_constant) uniform Push { - vec4 SourceSize; - vec4 OriginalSize; - vec4 OutputSize; - uint FrameCount; - float CD_BLEND_LEVEL; + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float CD_BLEND_LEVEL; } params; #pragma parameter CD_BLEND_LEVEL " Blend Level" 1.0 0.0 1.0 0.1 #define CD_BLEND_LEVEL params.CD_BLEND_LEVEL +#define PATTERN(A) step(Delta,A) + const float Delta = 0.000000001; const vec3 Y = vec3( 0.299, 0.587, 0.114); -vec3 min_s(vec3 central, vec3 adj1, vec3 adj2) {return min(central, max(adj1, adj2));} -vec3 max_s(vec3 central, vec3 adj1, vec3 adj2) {return max(central, min(adj1, adj2));} - layout(std140, set = 0, binding = 0) uniform UBO { - mat4 MVP; + mat4 MVP; } global; @@ -70,53 +69,74 @@ layout(set = 0, binding = 3) uniform sampler2D LinearGamma; void main() { - vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; - vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; + vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; + vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; - // Reading the texels. - vec4 C = texture(Source, vTexCoord ); - vec4 L = texture(Source, vTexCoord -dx ); - vec4 R = texture(Source, vTexCoord +dx ); - vec4 U = texture(Source, vTexCoord -dy); - vec4 D = texture(Source, vTexCoord +dy); - vec4 UL = texture(Source, vTexCoord -dx -dy); - vec4 UR = texture(Source, vTexCoord +dx -dy); - vec4 DL = texture(Source, vTexCoord -dx +dy); - vec4 DR = texture(Source, vTexCoord +dx +dy); + // Reading the texels. + vec4 C = texture(Source, vTexCoord ); + vec4 L = texture(Source, vTexCoord -dx ); + vec4 R = texture(Source, vTexCoord +dx ); + vec4 U = texture(Source, vTexCoord -dy); + vec4 D = texture(Source, vTexCoord +dy); + vec4 UL = texture(Source, vTexCoord -dx -dy); + vec4 UR = texture(Source, vTexCoord +dx -dy); + vec4 DL = texture(Source, vTexCoord -dx +dy); + vec4 DR = texture(Source, vTexCoord +dx +dy); - vec3 color = C.rgb; + vec4 L2 = texture(Source, vTexCoord -2.0*dx ); + vec4 R2 = texture(Source, vTexCoord +2.0*dx ); + vec4 U2 = texture(Source, vTexCoord -2.0*dy); + vec4 D2 = texture(Source, vTexCoord +2.0*dy); - vec3 oriC = texture(LinearGamma, vTexCoord ).rgb; - vec3 oriL = texture(LinearGamma, vTexCoord -dx ).rgb; - vec3 oriR = texture(LinearGamma, vTexCoord +dx ).rgb; - vec3 oriU = texture(LinearGamma, vTexCoord -dy).rgb; - vec3 oriD = texture(LinearGamma, vTexCoord +dy).rgb; + vec4 UL2 = texture(Source, vTexCoord -2.0*dx -dy); + vec4 UR2 = texture(Source, vTexCoord +2.0*dx -dy); + vec4 DL2 = texture(Source, vTexCoord -2.0*dx +dy); + vec4 DR2 = texture(Source, vTexCoord +2.0*dx +dy); - float count1 = 0.0; - float count2 = 0.0; + vec4 LU2 = texture(Source, vTexCoord -dx -2.0*dy); + vec4 RU2 = texture(Source, vTexCoord +dx -2.0*dy); + vec4 LD2 = texture(Source, vTexCoord -dx +2.0*dy); + vec4 RD2 = texture(Source, vTexCoord +dx -2.0*dy); - float diff = (1.0 - CD_BLEND_LEVEL); + vec3 color = C.rgb; -// UL U UR -// L C R -// DL D DR + vec3 oriC = texture(LinearGamma, vTexCoord ).rgb; + vec3 oriL = texture(LinearGamma, vTexCoord -dx ).rgb; + vec3 oriR = texture(LinearGamma, vTexCoord +dx ).rgb; + vec3 oriU = texture(LinearGamma, vTexCoord -dy).rgb; + vec3 oriD = texture(LinearGamma, vTexCoord +dy).rgb; - count1 += (step(Delta, L.a*D.a*R.a)); - count1 += (step(Delta, L.a*U.a*R.a)); - count1 += (step(Delta, L.a*UL.a*R.a*UR.a + L.a*DL.a*R.a*DR.a)); + float count1 = 0.0; + float count2 = 0.0; - count2 += (step(Delta, U.a*L.a*D.a)); - count2 += (step(Delta, U.a*R.a*D.a)); - count2 += (step(Delta, U.a*UR.a*D.a*DR.a + D.a*DL.a*U.a*UL.a)); + float diff = (1.0 - CD_BLEND_LEVEL); - if ((count1 * count2) > 0.0) - color = 0.5*(1.0+diff)*oriC + 0.125*(1.0-diff)*(oriL + oriR + oriU + oriD); - else if (count1 > 0.0) - color = 0.5*(1.0+diff)*oriC + 0.25*(1.0-diff)*(oriL + oriR); - else if (count2 > 0.0) - color = 0.5*(1.0+diff)*oriC + 0.25*(1.0-diff)*(oriU + oriD); +// LU2 U2 RU2 +// UL2 UL U UR UR2 +// L2 L C R R2 +// DL2 DL D DR DR2 +// LD2 D2 RD2 - float luma_diff = abs(dot(oriC, Y)-dot(color, Y)); + count1 += PATTERN(L.a*D.a*R.a); + count1 += PATTERN(L.a*U.a*R.a); + count1 += PATTERN(L.a*UL.a*R.a*UR.a + L.a*DL.a*R.a*DR.a); - FragColor = vec4(color, luma_diff); + count1 += PATTERN(U.a*D2.a*(UL.a*LD2.a + UR.a*RD2.a) + U2.a*D.a*(LU2.a*DL.a + RU2.a*DR.a)); + + count2 += PATTERN(U.a*L.a*D.a); + count2 += PATTERN(U.a*R.a*D.a); + count2 += PATTERN(U.a*UR.a*D.a*DR.a + D.a*DL.a*U.a*UL.a); + + count2 += PATTERN(L.a*R2.a*(DL.a*DR2.a + UL.a*UR2.a) + L2.a*R.a*(DL2.a*DR.a + UL2.a*UR.a)); + + if ((count1 * count2) > 0.0 && count1 == count2) + color = 0.5*(1.0+diff)*oriC + 0.125*(1.0-diff)*(oriL + oriR + oriU + oriD); + else if (count1 > 0.0 && count1 > count2) + color = 0.5*(1.0+diff)*oriC + 0.25*(1.0-diff)*(oriL + oriR); + else if (count2 > 0.0 && count2 > count1) + color = 0.5*(1.0+diff)*oriC + 0.25*(1.0-diff)*(oriU + oriD); + + float luma_diff = abs(dot(oriC, Y)-dot(color, Y)); + + FragColor = vec4(color, luma_diff); } diff --git a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang index 3e3f035..3f8d695 100644 --- a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang @@ -28,33 +28,37 @@ layout(push_constant) uniform Push { - vec4 SourceSize; - vec4 OriginalSize; - vec4 OutputSize; - uint FrameCount; - float CD_HUD_DETAILS; - float CD_ADJUST_VIEW; + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float CD_MITIG_NEIGHBRS; + float CD_MITIG_LINES; + float CD_ADJUST_VIEW; + float UseGamma; } params; -#pragma parameter CD_HUD_DETAILS " Details Threshold" 1.0 0.0 5.0 1.0 -#pragma parameter CD_ADJUST_VIEW " Adjust View" 0.0 0.0 1.0 1.0 +#pragma parameter CD_MITIG_NEIGHBRS " Mitigate Errors (neighbors)" 1.0 0.0 4.0 1.0 +#pragma parameter CD_MITIG_LINES " Mitigate Errors (regions)" 0.0 0.0 1.0 1.0 +#pragma parameter CD_ADJUST_VIEW " Adjust View" 0.0 0.0 1.0 1.0 +#pragma parameter UseGamma " Gamma Slider" 1.0 0.0 1.0 0.1 -#define CD_HUD_DETAILS params.CD_HUD_DETAILS -#define CD_ADJUST_VIEW params.CD_ADJUST_VIEW +#define CD_MITIG_NEIGHBRS params.CD_MITIG_NEIGHBRS +#define CD_MITIG_LINES params.CD_MITIG_LINES +#define CD_ADJUST_VIEW params.CD_ADJUST_VIEW +#define UseGamma params.UseGamma +#define OuputGamma (UseGamma+1.0) -#define GAMMA_EXP 2.0 -#define GAMMA_IN(color) pow(color, vec3(GAMMA_EXP, GAMMA_EXP, GAMMA_EXP)) -#define GAMMA_OUT(color) pow(color, vec3(1.0 / GAMMA_EXP, 1.0 / GAMMA_EXP, 1.0 / GAMMA_EXP)) +#define GAMMA_OUT(color) pow(color, vec3(1.0 / OuputGamma, 1.0 / OuputGamma, 1.0 / OuputGamma)) + +#define PATTERN(A) step(Delta,A) const float Delta = 0.000000001; const vec3 Y = vec3( 0.299, 0.587, 0.114); -vec3 min_s(vec3 central, vec3 adj1, vec3 adj2) {return min(central, max(adj1, adj2));} -vec3 max_s(vec3 central, vec3 adj1, vec3 adj2) {return max(central, min(adj1, adj2));} - layout(std140, set = 0, binding = 0) uniform UBO { - mat4 MVP; + mat4 MVP; } global; @@ -77,45 +81,72 @@ layout(set = 0, binding = 3) uniform sampler2D LinearGamma; void main() { - vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; - vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; + vec2 dx = vec2(1.0, 0.0)*params.SourceSize.zw; + vec2 dy = vec2(0.0, 1.0)*params.SourceSize.zw; - // Reading the texels. - vec4 C = texture(Source, vTexCoord ); - vec4 L = texture(Source, vTexCoord -dx ); - vec4 R = texture(Source, vTexCoord +dx ); - vec4 U = texture(Source, vTexCoord -dy); - vec4 D = texture(Source, vTexCoord +dy); - vec4 UL = texture(Source, vTexCoord -dx -dy); - vec4 UR = texture(Source, vTexCoord +dx -dy); - vec4 DL = texture(Source, vTexCoord -dx +dy); - vec4 DR = texture(Source, vTexCoord +dx +dy); + // Reading the texels. + vec4 C = texture(Source, vTexCoord ); + vec4 L = texture(Source, vTexCoord -dx ); + vec4 R = texture(Source, vTexCoord +dx ); + vec4 U = texture(Source, vTexCoord -dy); + vec4 D = texture(Source, vTexCoord +dy); + vec4 UL = texture(Source, vTexCoord -dx -dy); + vec4 UR = texture(Source, vTexCoord +dx -dy); + vec4 DL = texture(Source, vTexCoord -dx +dy); + vec4 DR = texture(Source, vTexCoord +dx +dy); - vec3 color = C.rgb; + vec4 L2 = texture(Source, vTexCoord -2.0*dx ); + vec4 R2 = texture(Source, vTexCoord +2.0*dx ); + vec4 U2 = texture(Source, vTexCoord -2.0*dy); + vec4 D2 = texture(Source, vTexCoord +2.0*dy); - vec3 oriC = texture(LinearGamma, vTexCoord ).rgb; + vec4 L3 = texture(Source, vTexCoord -3.0*dx ); + vec4 R3 = texture(Source, vTexCoord +3.0*dx ); + vec4 U3 = texture(Source, vTexCoord -3.0*dy); + vec4 D3 = texture(Source, vTexCoord +3.0*dy); - float count = 0.0; - float count2 = 0.0; + vec3 color = C.rgb; -// UL U UR -// L C R -// DL D DR + vec3 oriC = texture(LinearGamma, vTexCoord ).rgb; - count += step(Delta, L.a); - count += step(Delta, R.a); - count += step(Delta, U.a); - count += step(Delta, D.a); - count += step(Delta, UL.a*UR.a*DL.a*DR.a); + float count = 0.0; + float count2 = 0.0; + float count3 = 0.0; - count2 += (step(Delta, L.a*UL.a*U.a + U.a*UR.a*R.a + R.a*DR.a*D.a + D.a*DL.a*L.a)); +// U3 +// U2 +// UL U UR +// L3 L2 L C R R2 R3 +// DL D DR +// D2 +// D3 - if ((count < CD_HUD_DETAILS) && (count2 < 1.0)) - color = oriC; + count += PATTERN(L.a); + count += PATTERN(R.a); + count += PATTERN(U.a); + count += PATTERN(D.a); + count += PATTERN(UL.a*UR.a*DL.a*DR.a); - float luma_diff = abs(dot(oriC, Y)-dot(color, Y)); + count2 += PATTERN(L.a*UL.a*U.a + U.a*UR.a*R.a + R.a*DR.a*D.a + D.a*DL.a*L.a); - color = mix(color, vec3(luma_diff), CD_ADJUST_VIEW); + count3 += PATTERN(L3.a*L2.a*L.a); + count3 += PATTERN(L2.a*L.a*R.a); + count3 += PATTERN(L.a*R.a*R2.a); + count3 += PATTERN(R.a*R2.a*R3.a); - FragColor = vec4(GAMMA_OUT(color), 1.0); + count3 += PATTERN(U3.a*U2.a*U.a); + count3 += PATTERN(U2.a*U.a*D.a); + count3 += PATTERN(U.a*D.a*D2.a); + count3 += PATTERN(D.a*D2.a*D3.a); + + if ((count < CD_MITIG_NEIGHBRS) && (count2 < 1.0)) + color = oriC; + else if ((CD_MITIG_LINES == 1.0) && (count3 < 1.0)) + color = oriC; + + float luma_diff = abs(dot(oriC, Y)-dot(color, Y)); + + color = mix(color, vec3(luma_diff), CD_ADJUST_VIEW); + + FragColor = vec4(GAMMA_OUT(color), 1.0); } diff --git a/dithering/shaders/checkerboard-dedither/linearize.slang b/dithering/shaders/checkerboard-dedither/linearize.slang index 03fe5fc..60cff02 100644 --- a/dithering/shaders/checkerboard-dedither/linearize.slang +++ b/dithering/shaders/checkerboard-dedither/linearize.slang @@ -6,11 +6,15 @@ layout(push_constant) uniform Push vec4 OriginalSize; vec4 OutputSize; uint FrameCount; - float InputGamma; + float CD_BLEND_OPTION; + float UseGamma; } params; +#pragma parameter CD_BLEND_OPTION "CHECKERBOARD-DEDITHER: [ OFF | ON ]" 1.0 0.0 1.0 1.0 +#pragma parameter UseGamma " Gamma Slider" 1.0 0.0 1.0 0.1 -#define InputGamma 2.0 +#define UseGamma params.UseGamma +#define InputGamma (UseGamma+1.0) #define GAMMA_IN(color) pow(color, vec3(InputGamma, InputGamma, InputGamma))