mirror of
https://github.com/italicsjenga/slang-shaders.git
synced 2024-11-22 07:41:31 +11:00
Merge pull request #431 from gizmo98/dithering
add bayer 4x4 matrix ordered grid dithering shader
This commit is contained in:
commit
e7f143a9d3
10
dithering/bayer_4x4.slangp
Normal file
10
dithering/bayer_4x4.slangp
Normal file
|
@ -0,0 +1,10 @@
|
|||
shaders = "1"
|
||||
shader0 = "shaders/bayer_4x4.slang"
|
||||
filter_linear0 = "false"
|
||||
scale_type0 = viewport
|
||||
|
||||
parameters = "COLOR_DEPTH;DITHER_TUNE;EGA_PALETTE"
|
||||
COLOR_DEPTH = "2.0"
|
||||
DITHER_TUNE = "0.0"
|
||||
EGA_PALETTE = "0.0"
|
||||
|
214
dithering/shaders/bayer_4x4.slang
Normal file
214
dithering/shaders/bayer_4x4.slang
Normal file
|
@ -0,0 +1,214 @@
|
|||
#version 450
|
||||
/*
|
||||
* gizmo98 bayer 4x4 dithering shader
|
||||
* Copyright (C) 2023 gizmo98
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* version 0.1, 16.05.2023
|
||||
* ---------------------------------------------------------------------------------------
|
||||
* - initial commit
|
||||
*
|
||||
* https://github.com/gizmo98/gizmo-crt-shader
|
||||
*
|
||||
* uses parts of texture anti-aliasing shader from Ikaros https://www.shadertoy.com/view/ldsSRX
|
||||
*/
|
||||
|
||||
layout(push_constant) uniform Push
|
||||
{
|
||||
vec4 SourceSize;
|
||||
vec4 OriginalSize;
|
||||
vec4 OutputSize;
|
||||
uint FrameCount;
|
||||
float COLOR_DEPTH;
|
||||
float DITHER_TUNE;
|
||||
float EGA_PALETTE;
|
||||
} params;
|
||||
|
||||
layout(std140, set = 0, binding = 0) uniform UBO
|
||||
{
|
||||
mat4 MVP;
|
||||
} global;
|
||||
|
||||
#pragma parameter COLOR_DEPTH "Color depth in Bits" 1.0 1.0 8.0 1.0
|
||||
#pragma parameter DITHER_TUNE "Tune dithering" 0.0 -64.0 64.0 1.0
|
||||
#pragma parameter EGA_PALETTE "EGA palette" 0.0 0.0 1.0 1.0
|
||||
|
||||
#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;
|
||||
|
||||
vec4 DitherPattern(vec4 col, vec2 coord)
|
||||
{
|
||||
mat4 bayerMatrix;
|
||||
bayerMatrix[0] = vec4(0.0, 8.0, 2.0, 10.0);
|
||||
bayerMatrix[1] = vec4(12.0, 4.0, 14.0, 6.0);
|
||||
bayerMatrix[2] = vec4(3.0, 11.0, 1.0, 9.0);
|
||||
bayerMatrix[3] = vec4(15.0, 7.0, 13.0, 5.0);
|
||||
|
||||
ivec2 st = ivec2(fract(coord.xy / 4.0) * 4.0);
|
||||
float threshold = bayerMatrix[st.x][st.y];
|
||||
float multiplier = pow(2.0,8.0 - params.COLOR_DEPTH) - 1.0;
|
||||
|
||||
threshold = (threshold / 15.0) - 0.5;
|
||||
threshold *= ((multiplier + params.DITHER_TUNE) / 255.0);
|
||||
|
||||
col.rgb += threshold;
|
||||
return col;
|
||||
}
|
||||
|
||||
vec2 saturateA(in vec2 x)
|
||||
{
|
||||
return clamp(x, 0.0, 1.0);
|
||||
}
|
||||
|
||||
vec2 magnify(in vec2 uv, in vec2 res)
|
||||
{
|
||||
uv *= res;
|
||||
return (saturateA(fract(uv) / saturateA(fwidth(uv))) + floor(uv) - 0.5) / res.xy;
|
||||
}
|
||||
vec4 textureAA(in vec2 uv){
|
||||
vec2 texSize = vec2(textureSize(Source, 0));
|
||||
uv = magnify(uv,texSize.xy);
|
||||
vec2 uv1 = uv*texSize.xy;
|
||||
|
||||
vec2 iuv = floor(uv1);
|
||||
vec4 col = texture( Source, uv );
|
||||
|
||||
col = DitherPattern( col , iuv - 0.5);
|
||||
return col;
|
||||
}
|
||||
|
||||
vec4 ColorDepthReduction(vec4 col)
|
||||
{
|
||||
float divider = pow(2.0,params.COLOR_DEPTH) - 1.0;
|
||||
col.rgb *= divider;
|
||||
col.rgb = floor(col.rgb) + step(0.5, fract(col.rgb));
|
||||
col.rgb /= divider;
|
||||
return col;
|
||||
}
|
||||
|
||||
vec4 EGAPalette(vec4 col)
|
||||
{
|
||||
float divider = 3.0;
|
||||
vec3 c = floor(col.rgb * divider) + step(0.5, fract(col.rgb * divider));
|
||||
if (c.rgb == vec3(0.0,0.0,0.0) ||
|
||||
c.rgb == vec3(1.0,1.0,1.0) ||
|
||||
c.rgb == vec3(2.0,2.0,2.0) ||
|
||||
c.rgb == vec3(3.0,3.0,3.0) ||
|
||||
c.rgb == vec3(2.0,1.0,0.0))
|
||||
col.rgb = col.rgb;
|
||||
// bright green
|
||||
else if (c.rgb == vec3(0.0,3.0,0.0)||
|
||||
c.rgb == vec3(0.0,3.0,2.0)||
|
||||
c.rgb == vec3(2.0,3.0,0.0)||
|
||||
c.rgb == vec3(0.0,3.0,1.0)||
|
||||
c.rgb == vec3(1.0,3.0,1.0)||
|
||||
c.rgb == vec3(1.0,3.0,0.0)||
|
||||
c.rgb == vec3(2.0,3.0,1.0)||
|
||||
c.rgb == vec3(2.0,3.0,2.0))
|
||||
col.rgb = vec3(1.0,3.0,1.0) / divider;
|
||||
// green
|
||||
else if (c.rgb == vec3(0.0,2.0,0.0)||
|
||||
c.rgb == vec3(0.0,2.0,1.0)||
|
||||
c.rgb == vec3(0.0,1.0,0.0)||
|
||||
c.rgb == vec3(0.0,1.0,1.0)||
|
||||
c.rgb == vec3(1.0,2.0,1.0)||
|
||||
c.rgb == vec3(1.0,2.0,0.0)||
|
||||
c.rgb == vec3(0.0,1.0,1.0))
|
||||
col.rgb = vec3(0.0,2.0,0.0) / divider;
|
||||
// bright red
|
||||
else if (c.rgb == vec3(3.0,0.0,0.0)||
|
||||
c.rgb == vec3(3.0,0.0,1.0)||
|
||||
c.rgb == vec3(3.0,1.0,0.0)||
|
||||
c.rgb == vec3(3.0,1.0,1.0))
|
||||
col.rgb = vec3(3.0,1.0,1.0) / divider;
|
||||
// red
|
||||
else if (c.rgb == vec3(2.0,0.0,0.0)||
|
||||
c.rgb == vec3(2.0,0.0,1.0)||
|
||||
c.rgb == vec3(1.0,0.0,0.0))
|
||||
col.rgb = vec3(2.0,0.0,0.0) / divider;
|
||||
// bright cyan
|
||||
else if (c.rgb == vec3(0.0,3.0,3.0)||
|
||||
c.rgb == vec3(1.0,3.0,3.0)||
|
||||
c.rgb == vec3(2.0,3.0,3.0))
|
||||
col.rgb = vec3(1.0,3.0,3.0) / divider;
|
||||
// cyan
|
||||
else if (c.rgb == vec3(0.0,2.0,2.0)||
|
||||
c.rgb == vec3(1.0,2.0,2.0))
|
||||
col.rgb = vec3(0.0,2.0,2.0) / divider;
|
||||
// bright blue
|
||||
else if (c.rgb == vec3(0.0,2.0,3.0)||
|
||||
c.rgb == vec3(1.0,2.0,3.0)||
|
||||
c.rgb == vec3(0.0,0.0,3.0))
|
||||
col.rgb = vec3(1.0,1.0,3.0) / divider;
|
||||
// blue
|
||||
else if (c.rgb == vec3(0.0,0.0,2.0)||
|
||||
c.rgb == vec3(0.0,0.0,1.0)||
|
||||
c.rgb == vec3(0.0,1.0,2.0)||
|
||||
c.rgb == vec3(1.0,1.0,3.0)||
|
||||
c.rgb == vec3(0.0,1.0,3.0))
|
||||
col.rgb = vec3(0.0,0.0,2.0) / divider;
|
||||
// brown
|
||||
else if (c.rgb == vec3(2.0,1.0,0.0)||
|
||||
c.rgb == vec3(2.0,1.0,1.0)||
|
||||
c.rgb == vec3(1.0,1.0,0.0))
|
||||
col.rgb = vec3(2.0,1.0,0.0) / divider;
|
||||
// bright yellow
|
||||
else if (c.rgb == vec3(3.0,3.0,0.0)||
|
||||
c.rgb == vec3(3.0,3.0,1.0)||
|
||||
c.rgb == vec3(3.0,3.0,2.0)||
|
||||
c.rgb == vec3(2.0,2.0,0.0)||
|
||||
c.rgb == vec3(2.0,2.0,1.0))
|
||||
col.rgb = vec3(3.0,3.0,1.0) / divider;
|
||||
// magenta
|
||||
else if (c.rgb == vec3(2.0,0.0,2.0)||
|
||||
c.rgb == vec3(2.0,0.0,3.0)||
|
||||
c.rgb == vec3(2.0,1.0,2.0)||
|
||||
c.rgb == vec3(2.0,1.0,3.0))
|
||||
col.rgb = vec3(2.0,0.0,2.0) / divider;
|
||||
// bright magenta
|
||||
else if (c.rgb == vec3(3.0,0.0,2.0)||
|
||||
c.rgb == vec3(3.0,0.0,3.0)||
|
||||
c.rgb == vec3(3.0,2.0,3.0)||
|
||||
c.rgb == vec3(3.0,1.0,3.0)||
|
||||
c.rgb == vec3(3.0,1.0,2.0))
|
||||
col.rgb = vec3(3.0,1.0,3.0) / divider;
|
||||
else if (c.r == 0.0)
|
||||
col.gb = step(2.0,c.gb) * 2.0 / divider;
|
||||
else if (c.g == 0.0)
|
||||
col.rb = step(2.0,c.rb) * 2.0 / divider;
|
||||
else if (c.b == 0.0)
|
||||
col.rg = step(2.0,c.rg) * 2.0 / divider;
|
||||
else if (c.r == 3.0)
|
||||
col.gb = step(1.0,c.gb) / divider;
|
||||
else if (c.g == 3.0)
|
||||
col.rb = step(1.0,c.rb) / divider;
|
||||
else if (c.b == 3.0)
|
||||
col.rg = step(1.0,c.rg) / divider;
|
||||
return col;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 texcoord = vTexCoord.xy;
|
||||
FragColor = textureAA(texcoord);
|
||||
FragColor = ColorDepthReduction(FragColor);
|
||||
if (params.EGA_PALETTE == 1.0)
|
||||
FragColor = EGAPalette(FragColor);
|
||||
}
|
Loading…
Reference in a new issue