slang-shaders/crt/shaders/guest/color-profiles.slang
2019-05-03 12:31:31 -05:00

160 lines
3.5 KiB
Plaintext

#version 450
/*
CRT Color Profiles
Copyright (C) 2019 guest(r) and Dr. Venom
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.
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
layout(push_constant) uniform Push
{
float CP, CS;
} params;
#pragma parameter CP "CRT Color Profile" 0.0 -1.0 5.0 1.0
#pragma parameter CS "Color Space: sRGB, DCI, Adobe, Rec.2020" 0.0 0.0 3.0 1.0
#define CP params.CP
#define CS params.CS
#define COMPAT_TEXTURE(c,d) texture(c,d)
#define TEX0 vTexCoord
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;
const mat3 Profile0 =
mat3(
0.412391, 0.212639, 0.019331,
0.357584, 0.715169, 0.119195,
0.180481, 0.072192, 0.950532
);
const mat3 Profile1 =
mat3(
0.430554, 0.222004, 0.020182,
0.341550, 0.706655, 0.129553,
0.178352, 0.071341, 0.939322
);
const mat3 Profile2 =
mat3(
0.396686, 0.210299, 0.006131,
0.372504, 0.713766, 0.115356,
0.181266, 0.075936, 0.967571
);
const mat3 Profile3 =
mat3(
0.393521, 0.212376, 0.018739,
0.365258, 0.701060, 0.111934,
0.191677, 0.086564, 0.958385
);
const mat3 Profile4 =
mat3(
0.392258, 0.209410, 0.016061,
0.351135, 0.725680, 0.093636,
0.166603, 0.064910, 0.850324
);
const mat3 Profile5 =
mat3(
0.377923, 0.195679, 0.010514,
0.317366, 0.722319, 0.097826,
0.207738, 0.082002, 1.076960
);
const mat3 ToSRGB =
mat3(
3.240970, -0.969244, 0.055630,
-1.537383, 1.875968, -0.203977,
-0.498611, 0.041555, 1.056972
);
const mat3 ToDCI =
mat3(
2.725394, -0.795168, 0.041242,
-1.018003, 1.689732, 0.022647,
-0.440163, 0.022647, 1.100929
);
const mat3 ToAdobe =
mat3(
2.041588, -0.969244, 0.013444,
-0.565007, 1.875968, -0.11836,
-0.344731, 0.041555, 1.015175
);
const mat3 ToREC =
mat3(
1.716651, -0.666684, 0.017640,
-0.355671, 1.616481, -0.042771,
-0.253366, 0.015769, 0.942103
);
void main()
{
vec3 c = COMPAT_TEXTURE(Source, TEX0.xy).rgb;
float p;
mat3 m_out;
if (CS == 0.0) { p = 2.4; m_out = ToSRGB; } else
if (CS == 1.0) { p = 2.6; m_out = ToDCI; } else
if (CS == 2.0) { p = 2.2; m_out = ToAdobe;} else
if (CS == 3.0) { p = 2.4; m_out = ToREC; }
vec3 color = pow(c, vec3(p));
mat3 m_in = Profile0;
if (CP == 0.0) { m_in = Profile0; } else
if (CP == 1.0) { m_in = Profile1; } else
if (CP == 2.0) { m_in = Profile2; } else
if (CP == 3.0) { m_in = Profile3; } else
if (CP == 4.0) { m_in = Profile4; } else
if (CP == 5.0) { m_in = Profile5; }
color = m_in*color;
color = m_out*color;
color = pow(color, vec3(1.0/p));
if (CP == -1.0) color = c;
FragColor = vec4(color,1.0);
}