From 22c21cc56e14992b1c3e0198579113b67515a5e9 Mon Sep 17 00:00:00 2001 From: Hyllian Date: Fri, 2 Sep 2022 16:41:05 -0300 Subject: [PATCH] Add Chckerboard-Dedither shader - Smooths checkerboard dithering. --- dithering/checkerboard-dedither.slangp | 20 +++ .../checkerboard-dedither-pass1.slang | 112 ++++++++++++++++ .../checkerboard-dedither-pass2.slang | 122 ++++++++++++++++++ .../checkerboard-dedither-pass3.slang | 121 +++++++++++++++++ .../checkerboard-dedither/linearize.slang | 41 ++++++ 5 files changed, 416 insertions(+) create mode 100644 dithering/checkerboard-dedither.slangp create mode 100644 dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang create mode 100644 dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang create mode 100644 dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang create mode 100644 dithering/shaders/checkerboard-dedither/linearize.slang diff --git a/dithering/checkerboard-dedither.slangp b/dithering/checkerboard-dedither.slangp new file mode 100644 index 0000000..9f0d2bd --- /dev/null +++ b/dithering/checkerboard-dedither.slangp @@ -0,0 +1,20 @@ +shaders = 4 + +shader0 = shaders/checkerboard-dedither/linearize.slang +filter_linear0 = false +scale_type0 = source +scale0 = 1.0 +alias0 = LinearGamma + +shader1 = shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang +filter_linear1 = false +scale_type1 = source +scale1 = 1.0 + +shader2 = shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang +filter_linear2 = false +scale_type2 = source +scale2 = 1.0 + +shader3 = shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang +filter_linear3 = false diff --git a/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang new file mode 100644 index 0000000..18fbd0a --- /dev/null +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass1.slang @@ -0,0 +1,112 @@ +#version 450 + +/* + Checkerboard Dedither - pass1 + + 2011-2022 Hyllian - sergiogdb@gmail.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + + +layout(push_constant) uniform Push +{ + 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 + +#define CD_BLEND_OPTION params.CD_BLEND_OPTION +#define CD_BLEND_LEVEL params.CD_BLEND_LEVEL + +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; +} 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() +{ + 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; + + vec3 color = C; + + // Get min/max samples + vec3 min_sample = min_s(C, L, R); + vec3 max_sample = max_s(C, L, R); + + float diff = dot(max(max(C, L), max(C, R)) - min(min(C, L), min(C, R)), Y); + + diff *= (1.0 - CD_BLEND_LEVEL); + + 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 = clamp(color, min_sample, max_sample); + + float luma_diff = abs(dot(C, Y)-dot(color, Y)); + + 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 new file mode 100644 index 0000000..5118023 --- /dev/null +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass2.slang @@ -0,0 +1,122 @@ +#version 450 + +/* + Checkerboard Dedither - pass2 + + 2011-2022 Hyllian - sergiogdb@gmail.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + + +layout(push_constant) uniform Push +{ + 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 + +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; +} 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; +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; + + // 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; + + 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; + + float count1 = 0.0; + float count2 = 0.0; + + float diff = (1.0 - CD_BLEND_LEVEL); + +// UL U UR +// L C R +// DL D DR + + 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)); + + 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)); + + 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); + + 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 new file mode 100644 index 0000000..3e3f035 --- /dev/null +++ b/dithering/shaders/checkerboard-dedither/checkerboard-dedither-pass3.slang @@ -0,0 +1,121 @@ +#version 450 + +/* + Checkerboard Dedither - pass3 + + 2011-2022 Hyllian - sergiogdb@gmail.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float CD_HUD_DETAILS; + float CD_ADJUST_VIEW; +} 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 + +#define CD_HUD_DETAILS params.CD_HUD_DETAILS +#define CD_ADJUST_VIEW params.CD_ADJUST_VIEW + +#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)) + +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; +} 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; +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; + + // 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; + + vec3 oriC = texture(LinearGamma, vTexCoord ).rgb; + + float count = 0.0; + float count2 = 0.0; + +// UL U UR +// L C R +// DL D DR + + 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); + + 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)); + + if ((count < CD_HUD_DETAILS) && (count2 < 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 new file mode 100644 index 0000000..03fe5fc --- /dev/null +++ b/dithering/shaders/checkerboard-dedither/linearize.slang @@ -0,0 +1,41 @@ +#version 450 + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float InputGamma; +} params; + + +#define InputGamma 2.0 + +#define GAMMA_IN(color) pow(color, vec3(InputGamma, InputGamma, InputGamma)) + +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() +{ + FragColor = vec4(GAMMA_IN(texture(Source, vTexCoord).rgb), 1.0); +}