mirror of
https://github.com/italicsjenga/slang-shaders.git
synced 2024-11-30 03:11:31 +11:00
189 lines
6.3 KiB
Plaintext
189 lines
6.3 KiB
Plaintext
|
#version 450
|
||
|
|
||
|
/* Filename: chromaticity
|
||
|
|
||
|
Copyright (C) 2023 W. M. Martinez
|
||
|
splitted and adjusted by DariusG
|
||
|
|
||
|
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 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
layout(push_constant) uniform Push
|
||
|
{
|
||
|
vec4 SourceSize;
|
||
|
vec4 OriginalSize;
|
||
|
vec4 OutputSize;
|
||
|
uint FrameCount;
|
||
|
float CHROMA_A_X,CHROMA_A_Y,CHROMA_B_X,CHROMA_B_Y,CHROMA_C_X,CHROMA_C_Y,CHROMA_A_WEIGHT,
|
||
|
CHROMA_B_WEIGHT,CHROMA_C_WEIGHT,CRT_TR0,CRT_TR,CRT_TR2,SCALE_W,GAMMAIN,GAMMAOUT;
|
||
|
} params;
|
||
|
|
||
|
#pragma parameter Readme "READ GLSL FILE FOR VARIOUS COEFFS" 0.0 0.0 1.0 1.0
|
||
|
#pragma parameter CHROMA_A_X "Chromaticity R x" 0.670 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_A_Y "Chromaticity R y" 0.330 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_B_X "Chromaticity G x" 0.210 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_B_Y "Chromaticity G y" 0.710 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_C_X "Chromaticity B x" 0.140 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_C_Y "Chromaticity B y" 0.080 0.0 1.0 0.001
|
||
|
#pragma parameter CHROMA_A_WEIGHT "Chromaticity R luminance weight" 0.299 0.0 1.0 0.01
|
||
|
#pragma parameter CHROMA_B_WEIGHT "Chromaticity G luminance weight" 0.587 0.0 1.0 0.01
|
||
|
#pragma parameter CHROMA_C_WEIGHT "Chromaticity B luminance weight" 0.114 0.0 1.0 0.01
|
||
|
#pragma parameter CRT_TR0 "Transfer Function" 0.018 0.0 0.2 0.001
|
||
|
#pragma parameter CRT_TR "Transfer Function" 0.099 0.0 0.2 0.001
|
||
|
#pragma parameter CRT_TR2 "Transfer Function" 4.5 3.0 5.0 0.05
|
||
|
#pragma parameter SCALE_W "Scale white point" 1.0 0.0 1.0 1.0
|
||
|
#pragma parameter GAMMAIN "Gamma In" 2.4 1.0 4.0 0.05
|
||
|
#pragma parameter GAMMAOUT "Gamma Out" 2.2 1.0 4.0 0.05
|
||
|
|
||
|
|
||
|
#define CHROMA_A_X params.CHROMA_A_X
|
||
|
#define CHROMA_A_Y params.CHROMA_A_Y
|
||
|
#define CHROMA_B_X params.CHROMA_B_X
|
||
|
#define CHROMA_B_Y params.CHROMA_B_Y
|
||
|
#define CHROMA_C_X params.CHROMA_C_X
|
||
|
#define CHROMA_C_Y params.CHROMA_C_Y
|
||
|
#define CHROMA_A_WEIGHT params.CHROMA_A_WEIGHT
|
||
|
#define CHROMA_B_WEIGHT params.CHROMA_B_WEIGHT
|
||
|
#define CHROMA_C_WEIGHT params.CHROMA_C_WEIGHT
|
||
|
#define CRT_TR0 params.CRT_TR0
|
||
|
#define CRT_TR params.CRT_TR
|
||
|
#define CRT_TR2 params.CRT_TR2
|
||
|
#define SCALE_W params.SCALE_W
|
||
|
#define GAMMAIN params.GAMMAIN
|
||
|
#define GAMMAOUT params.GAMMAOUT
|
||
|
#define WHITE vec3(1.0, 1.0, 1.0)
|
||
|
|
||
|
/*
|
||
|
SMPTE-C/170M used by NTSC and PAL and by SDTV in general.
|
||
|
REC709 used by HDTV in general.
|
||
|
SRGB used by most webcams and computer graphics. ***NOTE***: Gamma 2.4
|
||
|
BT2020 used by Ultra-high definition television (UHDTV) and wide color gamut.
|
||
|
SMPTE240 used during the early days of HDTV (1988-1998).
|
||
|
NTSC1953 used by NTSC at 1953.
|
||
|
EBU used by PAL/SECAM in 1975. Identical to REC601.
|
||
|
*/
|
||
|
|
||
|
|
||
|
// RX RY GX GY BX BY RL GL BL TR0 TR TR2
|
||
|
// SMPTE C 0.630 0.340 / 0.310 0.595 / 0.155 0.070 / 0.299 0.587 0.114 / 0.018 0.099 4.5
|
||
|
// REC709 0.640 0.330 / 0.300 0.600 / 0.150 0.060 / 0.212 0.715 0.072 / 0.018 0.099 4.5
|
||
|
// SRGB 0.640 0.330 / 0.300 0.600 / 0.150 0.060 / 0.299 0.587 0.114 / 0.040 0.055 12.92
|
||
|
// BT2020 0.708 0.292 / 0.170 0.797 / 0.131 0.046 / 0.262 0.678 0.059 / 0.059 0.099 4.5
|
||
|
// SMPTE240 0.630 0.340 / 0.310 0.595 / 0.155 0.070 / 0.212 0.701 0.086 / 0.091 0.111 4.0
|
||
|
// NTSC1953 0.670 0.330 / 0.210 0.710 / 0.140 0.080 / 0.210 0.710 0.080 / 0.081 0.099 4.5
|
||
|
// EBU 0.640 0.330 / 0.290 0.600 / 0.150 0.060 / 0.299 0.587 0.114 / 0.081 0.099 4.5
|
||
|
// SECAM 0.640 0.330 / 0.290 0.600 / 0.150 0.060 / 0.334 0.585 0.081 / 0.081 0.099 4.5
|
||
|
|
||
|
|
||
|
|
||
|
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;
|
||
|
|
||
|
|
||
|
|
||
|
mat3 XYZ_TO_sRGB = mat3(
|
||
|
3.2406255, -0.9689307, 0.0557101,
|
||
|
-1.5372080, 1.8758561, -0.2040211,
|
||
|
-0.4986286, 0.0415175, 1.0569959);
|
||
|
|
||
|
mat3 colorspace_rgb()
|
||
|
{
|
||
|
return XYZ_TO_sRGB;
|
||
|
}
|
||
|
|
||
|
|
||
|
vec3 xyY_to_XYZ(const vec3 xyY)
|
||
|
{
|
||
|
float x = xyY.x;
|
||
|
float y = xyY.y;
|
||
|
float Y = xyY.z;
|
||
|
float z = 1.0 - x - y;
|
||
|
|
||
|
return vec3(Y * x / y, Y, Y * z / y);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
vec3 Yrgb_to_RGB(mat3 toRGB, vec3 W, vec3 Yrgb)
|
||
|
{
|
||
|
mat3 xyYrgb = mat3(CHROMA_A_X, CHROMA_A_Y, Yrgb.r,
|
||
|
CHROMA_B_X, CHROMA_B_Y, Yrgb.g,
|
||
|
CHROMA_C_X, CHROMA_C_Y, Yrgb.b);
|
||
|
mat3 XYZrgb = mat3(xyY_to_XYZ(xyYrgb[0]),
|
||
|
xyY_to_XYZ(xyYrgb[1]),
|
||
|
xyY_to_XYZ(xyYrgb[2]));
|
||
|
mat3 RGBrgb = mat3(toRGB * XYZrgb[0],
|
||
|
toRGB * XYZrgb[1],
|
||
|
toRGB * XYZrgb[2]);
|
||
|
return vec3(dot(W, vec3(RGBrgb[0].r, RGBrgb[1].r, RGBrgb[2].r)),
|
||
|
dot(W, vec3(RGBrgb[0].g, RGBrgb[1].g, RGBrgb[2].g)),
|
||
|
dot(W, vec3(RGBrgb[0].b, RGBrgb[1].b, RGBrgb[2].b)));
|
||
|
}
|
||
|
|
||
|
|
||
|
float sdr_linear(const float x)
|
||
|
{
|
||
|
return x < CRT_TR0 ? x / CRT_TR2 : pow((x + CRT_TR) / (1.0+ CRT_TR), GAMMAIN);
|
||
|
}
|
||
|
|
||
|
vec3 sdr_linear(const vec3 x)
|
||
|
{
|
||
|
return vec3(sdr_linear(x.r), sdr_linear(x.g), sdr_linear(x.b));
|
||
|
}
|
||
|
|
||
|
float srgb_gamma(const float x)
|
||
|
{
|
||
|
return x <= 0.0031308 ? 12.92 * x : 1.055 * pow(x, 1.0 / GAMMAOUT) - 0.055;
|
||
|
}
|
||
|
|
||
|
vec3 srgb_gamma(const vec3 x)
|
||
|
{
|
||
|
return vec3(srgb_gamma(x.r), srgb_gamma(x.g), srgb_gamma(x.b));
|
||
|
}
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
mat3 toRGB = colorspace_rgb();
|
||
|
vec3 Yrgb = texture(Source, vTexCoord).rgb;
|
||
|
Yrgb = sdr_linear(Yrgb);
|
||
|
vec3 W = vec3(CHROMA_A_WEIGHT, CHROMA_B_WEIGHT, CHROMA_C_WEIGHT);
|
||
|
vec3 RGB = Yrgb_to_RGB(toRGB, W, Yrgb);
|
||
|
if (SCALE_W > 0.0) {
|
||
|
vec3 white = Yrgb_to_RGB(toRGB, W, WHITE);
|
||
|
float G = 1.0 / max(max(white.r, white.g), white.b);
|
||
|
|
||
|
RGB *= G;
|
||
|
}
|
||
|
RGB = clamp(RGB, 0.0, 1.0);
|
||
|
RGB = srgb_gamma(RGB);
|
||
|
FragColor = vec4(RGB, 1.0);
|
||
|
}
|