Grade - Update + Cosmetics (#451)

* Grade - Update + Cosmetics

- P3-D65 now uses 1886 EOTF
- Mask for GamutCompression is now CRT Gamma adjusted instead of 2.4 constant
- Fix uninitialized 'm_in' variable
- Cosmetics

* Update grade-no-LUT.slang

* Update grade.slang
This commit is contained in:
Dogway 2023-06-17 22:28:47 +01:00 committed by GitHub
parent 91a5ff8445
commit 5f64d80b7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 122 deletions

View file

@ -82,7 +82,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
} global;
/*
Grade (03-06-2023)
Grade (16-06-2023)
> See settings decriptions at: https://forums.libretro.com/t/dogways-grading-shader-slang/27148/442
> Ubershader grouping some monolithic color related shaders:
@ -94,7 +94,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
**Syh, Nesguy, hunterk, and the libretro forum members.
######################################...PRESETS...#######################################
#####################################...STANDARDS...######################################
##########################################################################################
### ###
### PAL ###
@ -177,7 +177,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
#define V_SHIFT params.g_V_SHIFT
#define U_MUL params.g_U_MUL
#define V_MUL params.g_V_MUL
#define g_CRT_l -(100000.*log((72981.-500000./(3.*max(2.3,params.g_CRT_l)))/9058.))/945461.
#define CRT_l -(100000.*log((72981.-500000./(3.*max(2.3,params.g_CRT_l)))/9058.))/945461.
#define lum_fix params.g_lum_fix
#define vignette global.g_vignette
#define GCompress global.g_GCompress
@ -256,7 +256,6 @@ vec3 XYZ_to_RGB(vec3 XYZ, mat3 primaries) {
}
vec3 XYZtoYxy(vec3 XYZ) {
float XYZrgb = XYZ.r+XYZ.g+XYZ.b;
@ -277,7 +276,7 @@ vec3 YxytoXYZ(vec3 Yxy) {
///////////////////////// White Point Mapping /////////////////////////
//
//
// PAL: D65 NTSC-U: D65 NTSC-J: CCT NTSC-J
// PAL: D65 NTSC-U: D65 NTSC-J: CCT 9300K+27MPCD
// PAL: 6503.512K NTSC-U: 6503.512K NTSC-J: ~8945.436K
// [x:0.31266142 y:0.3289589] [x:0.281 y:0.311]
@ -366,8 +365,8 @@ vec3 EOTF_1886a_f3( vec3 color, float BlackLevel, float brightness, float contra
//----------------------------------------------------------------------
float moncurve_f( float color, float gamma, float offs)
{
float moncurve_f( float color, float gamma, float offs) {
// Forward monitor curve
color = clamp(color, 0.0, 1.0);
float fs = (( gamma - 1.0) / offs) * pow( offs * gamma / ( ( gamma - 1.0) * ( 1.0 + offs)), gamma);
@ -378,8 +377,8 @@ float moncurve_f( float color, float gamma, float offs)
}
vec3 moncurve_f_f3( vec3 color, float gamma, float offs)
{
vec3 moncurve_f_f3( vec3 color, float gamma, float offs) {
color.r = moncurve_f( color.r, gamma, offs);
color.g = moncurve_f( color.g, gamma, offs);
color.b = moncurve_f( color.b, gamma, offs);
@ -387,8 +386,8 @@ vec3 moncurve_f_f3( vec3 color, float gamma, float offs)
}
float moncurve_r( float color, float gamma, float offs)
{
float moncurve_r( float color, float gamma, float offs) {
// Reverse monitor curve
color = clamp(color, 0.0, 1.0);
float yb = pow( offs * gamma / ( ( gamma - 1.0) * ( 1.0 + offs)), gamma);
@ -399,8 +398,8 @@ float moncurve_r( float color, float gamma, float offs)
}
vec3 moncurve_r_f3( vec3 color, float gamma, float offs)
{
vec3 moncurve_r_f3( vec3 color, float gamma, float offs) {
color.r = moncurve_r( color.r, gamma, offs);
color.g = moncurve_r( color.g, gamma, offs);
color.b = moncurve_r( color.b, gamma, offs);
@ -459,8 +458,8 @@ vec3 rolled_gain_v3(vec3 color, float gain){
}
float SatMask(float color_r, float color_g, float color_b)
{
float SatMask(float color_r, float color_g, float color_b) {
float max_rgb = max(color_r, max(color_g, color_b));
float min_rgb = min(color_r, min(color_g, color_b));
float msk = clamp((max_rgb - min_rgb) / (max_rgb + min_rgb), 0.0, 1.0);
@ -482,8 +481,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
vec3 WPD = global.wp_temperature < 7000 ? vec3(1,temp,(temp-1)/2+1) : vec3((temp-1)/2+1,temp,1);
sat = max(0.0,g_sat+1)*(sat*beam) * WPD;
mat2x3 LimThres = \
mat2x3( 0.100000,0.100000,0.100000,
mat2x3 LimThres = mat2x3( 0.100000,0.100000,0.100000,
0.125000,0.125000,0.125000);
if (SPC < 1.0) {
@ -541,7 +539,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
// Inverse RGB Ratios to RGB
// and Mask with "luma"
return mix(rgb, ac-cd.xyz*abs(ac), pow(grey,1/2.4));
return mix(rgb, ac-cd.xyz*abs(ac), pow(grey,1/params.g_CRT_l));
}
@ -550,7 +548,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
// Matrices in OpenGL column-major
// Matrices in column-major
//----------------------- Y'UV color model -----------------------
@ -670,7 +668,7 @@ const mat3 SMPTE470BG_ph =
// NTSC-J P22
// Mix between averaging KV-20M20, KDS VS19, Dell D93, 4-TR-B09v1_0.pdf and Phosphor Handbook 'P22'
// ILLUMINANT: D93->[0.281000,0.311000] (CCT of 8945.436K)
// ILLUMINANT: D97->[0.285000,0.285000] (CCT of 9696K) for Nanao MS-2930s series (in practice prolly more like ~9177.98K)
// ILLUMINANT: D97->[0.285000,0.285000] (CCT of 9696K) for Nanao MS-2930s series (around 10000.0K for wp_adjust() daylight fit)
const mat3 P22_J_ph =
mat3(
0.625, 0.280, 0.152,
@ -696,8 +694,8 @@ const mat3 P22_90s_ph =
0.3329, 0.6310, 0.0642,
0.0010, 0.0556, 0.7886);
// CRT for Projection Tubes for NTSC-U late 90s, early 00s
const mat3 CRT_95s_ph =
// RPTV (Rear Projection TV) for NTSC-U late 90s, early 00s
const mat3 RPTV_95s_ph =
mat3(
0.640, 0.341, 0.150,
0.335, 0.586, 0.070,
@ -777,7 +775,7 @@ void main()
col = signal > 0.0 ? max(Quantize8_f3(YUV_r601(col.xyz, NTSC_U ? 1.0 : 0.0))/255.0, 0.0) : src;
// CRT EOTF. To Display Referred Linear: Undo developer baked CRT gamma (from 2.40 at default 0.1 CRT black level, to 2.60 at 0.0 CRT black level)
col = EOTF_1886a_f3(col, g_CRT_l, params.g_CRT_b, params.g_CRT_c);
col = EOTF_1886a_f3(col, CRT_l, params.g_CRT_b, params.g_CRT_c);
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
@ -797,16 +795,16 @@ void main()
screen *= transpose(color);
// CRT Phosphor Gamut (0.0 is noop)
// CRT Phosphor Gamut (0.0 is sRGB/noop)
mat3 m_in;
if (crtgamut == -3.0) { m_in = SMPTE170M_ph; } else
if (crtgamut == -2.0) { m_in = CRT_95s_ph; } else
if (crtgamut == -2.0) { m_in = RPTV_95s_ph; } else
if (crtgamut == -1.0) { m_in = P22_80s_ph; } else
if (crtgamut == 0.0) { m_in = sRGB_prims; } else
if (crtgamut == 1.0) { m_in = P22_90s_ph; } else
if (crtgamut == 2.0) { m_in = P22_J_ph; } else
if (crtgamut == 3.0) { m_in = SMPTE470BG_ph; }
if (crtgamut == 3.0) { m_in = SMPTE470BG_ph; } else
{ m_in = sRGB_prims; }
// Display color space
@ -897,15 +895,13 @@ void main()
src_h *= (vignette == 1.0) ? vig : 1.0;
// Dark to Dim adaptation OOTF; only for 709 and 2020
vec3 src_D = global.g_Dark_to_Dim > 0.0 ? pow(src_h,vec3(0.9811)) : src_h;
// Dark to Dim adaptation OOTF; for 709, P3-D65 and 2020
float DtD = global.g_Dark_to_Dim > 0.0 ? 1/0.9811 : 1.0;
// EOTF^-1 - Inverted Electro-Optical Transfer Function
vec3 TRC = (SPC == 3.0) ? clamp(pow(src_h, vec3(1./ (563./256.))), 0., 1.) : \
(SPC == 2.0) ? moncurve_r_f3(src_D, 2.20 + 0.022222, 0.0993) : \
(SPC == 1.0) ? clamp(pow(src_h, vec3(1./(2.20 + 0.40))), 0., 1.) : \
(SPC == 0.0) ? moncurve_r_f3(src_h, 2.20 + 0.20, 0.0550) : \
clamp(pow( src_D, vec3(1./(2.20 + 0.20))), 0., 1.) ;
clamp(pow( src_h, vec3(1./((2.20 + 0.20)*DtD))), 0., 1.) ;
// External Flare for Surround Illuminant 2700K (Soft White) at F0 (Lambertian reflectance); defines offset thus also black lift

View file

@ -86,7 +86,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
} global;
/*
Grade (03-06-2023)
Grade (16-06-2023)
> See settings decriptions at: https://forums.libretro.com/t/dogways-grading-shader-slang/27148/442
> Ubershader grouping some monolithic color related shaders:
@ -98,7 +98,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
**Syh, Nesguy, hunterk, and the libretro forum members.
######################################...PRESETS...#######################################
#####################################...STANDARDS...######################################
##########################################################################################
### ###
### PAL ###
@ -185,7 +185,7 @@ layout(std140, set = 0, binding = 0) uniform UBO
#define V_SHIFT params.g_V_SHIFT
#define U_MUL params.g_U_MUL
#define V_MUL params.g_V_MUL
#define g_CRT_l -(100000.*log((72981.-500000./(3.*max(2.3,params.g_CRT_l)))/9058.))/945461.
#define CRT_l -(100000.*log((72981.-500000./(3.*max(2.3,params.g_CRT_l)))/9058.))/945461.
#define lum_fix params.g_lum_fix
#define vignette global.g_vignette
#define GCompress global.g_GCompress
@ -266,7 +266,6 @@ vec3 XYZ_to_RGB(vec3 XYZ, mat3 primaries) {
}
vec3 XYZtoYxy(vec3 XYZ) {
float XYZrgb = XYZ.r+XYZ.g+XYZ.b;
@ -287,7 +286,7 @@ vec3 YxytoXYZ(vec3 Yxy) {
///////////////////////// White Point Mapping /////////////////////////
//
//
// PAL: D65 NTSC-U: D65 NTSC-J: CCT NTSC-J
// PAL: D65 NTSC-U: D65 NTSC-J: CCT 9300K+27MPCD
// PAL: 6503.512K NTSC-U: 6503.512K NTSC-J: ~8945.436K
// [x:0.31266142 y:0.3289589] [x:0.281 y:0.311]
@ -376,8 +375,8 @@ vec3 EOTF_1886a_f3( vec3 color, float BlackLevel, float brightness, float contra
//----------------------------------------------------------------------
float moncurve_f( float color, float gamma, float offs)
{
float moncurve_f( float color, float gamma, float offs) {
// Forward monitor curve
color = clamp(color, 0.0, 1.0);
float fs = (( gamma - 1.0) / offs) * pow( offs * gamma / ( ( gamma - 1.0) * ( 1.0 + offs)), gamma);
@ -388,8 +387,8 @@ float moncurve_f( float color, float gamma, float offs)
}
vec3 moncurve_f_f3( vec3 color, float gamma, float offs)
{
vec3 moncurve_f_f3( vec3 color, float gamma, float offs) {
color.r = moncurve_f( color.r, gamma, offs);
color.g = moncurve_f( color.g, gamma, offs);
color.b = moncurve_f( color.b, gamma, offs);
@ -397,8 +396,8 @@ vec3 moncurve_f_f3( vec3 color, float gamma, float offs)
}
float moncurve_r( float color, float gamma, float offs)
{
float moncurve_r( float color, float gamma, float offs) {
// Reverse monitor curve
color = clamp(color, 0.0, 1.0);
float yb = pow( offs * gamma / ( ( gamma - 1.0) * ( 1.0 + offs)), gamma);
@ -409,8 +408,8 @@ float moncurve_r( float color, float gamma, float offs)
}
vec3 moncurve_r_f3( vec3 color, float gamma, float offs)
{
vec3 moncurve_r_f3( vec3 color, float gamma, float offs) {
color.r = moncurve_r( color.r, gamma, offs);
color.g = moncurve_r( color.g, gamma, offs);
color.b = moncurve_r( color.b, gamma, offs);
@ -469,8 +468,8 @@ vec3 rolled_gain_v3(vec3 color, float gain){
}
float SatMask(float color_r, float color_g, float color_b)
{
float SatMask(float color_r, float color_g, float color_b) {
float max_rgb = max(color_r, max(color_g, color_b));
float min_rgb = min(color_r, min(color_g, color_b));
float msk = clamp((max_rgb - min_rgb) / (max_rgb + min_rgb), 0.0, 1.0);
@ -481,14 +480,12 @@ float SatMask(float color_r, float color_g, float color_b)
// This shouldn't be necessary but it seems some undefined values can
// creep in and each GPU vendor handles that differently. This keeps
// all values within a safe range
vec3 mixfix(vec3 a, vec3 b, float c)
{
vec3 mixfix(vec3 a, vec3 b, float c) {
return (a.z < 1.0) ? mix(a, b, c) : a;
}
vec4 mixfix_v4(vec4 a, vec4 b, float c)
{
vec4 mixfix_v4(vec4 a, vec4 b, float c) {
return (a.z < 1.0) ? mix(a, b, c) : a;
}
@ -507,8 +504,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
vec3 WPD = global.wp_temperature < 7000 ? vec3(1,temp,(temp-1)/2+1) : vec3((temp-1)/2+1,temp,1);
sat = max(0.0,g_sat+1)*(sat*beam) * WPD;
mat2x3 LimThres = \
mat2x3( 0.100000,0.100000,0.100000,
mat2x3 LimThres = mat2x3( 0.100000,0.100000,0.100000,
0.125000,0.125000,0.125000);
if (SPC < 1.0) {
@ -566,7 +562,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
// Inverse RGB Ratios to RGB
// and Mask with "luma"
return mix(rgb, ac-cd.xyz*abs(ac), pow(grey,1/2.4));
return mix(rgb, ac-cd.xyz*abs(ac), pow(grey,1/params.g_CRT_l));
}
@ -575,7 +571,7 @@ vec3 GamutCompression (vec3 rgb, float grey) {
// Matrices in OpenGL column-major
// Matrices in column-major
//----------------------- Y'UV color model -----------------------
@ -695,7 +691,7 @@ const mat3 SMPTE470BG_ph =
// NTSC-J P22
// Mix between averaging KV-20M20, KDS VS19, Dell D93, 4-TR-B09v1_0.pdf and Phosphor Handbook 'P22'
// ILLUMINANT: D93->[0.281000,0.311000] (CCT of 8945.436K)
// ILLUMINANT: D97->[0.285000,0.285000] (CCT of 9696K) for Nanao MS-2930s series (in practice prolly more like ~9177.98K)
// ILLUMINANT: D97->[0.285000,0.285000] (CCT of 9696K) for Nanao MS-2930s series (around 10000.0K for wp_adjust() daylight fit)
const mat3 P22_J_ph =
mat3(
0.625, 0.280, 0.152,
@ -721,8 +717,8 @@ const mat3 P22_90s_ph =
0.3329, 0.6310, 0.0642,
0.0010, 0.0556, 0.7886);
// CRT for Projection Tubes for NTSC-U late 90s, early 00s
const mat3 CRT_95s_ph =
// RPTV (Rear Projection TV) for NTSC-U late 90s, early 00s
const mat3 RPTV_95s_ph =
mat3(
0.640, 0.341, 0.150,
0.335, 0.586, 0.070,
@ -813,7 +809,7 @@ void main()
// CRT EOTF. To Display Referred Linear: Undo developer baked CRT gamma (from 2.40 at default 0.1 CRT black level, to 2.60 at 0.0 CRT black level)
col = EOTF_1886a_f3(vcolor, g_CRT_l, params.g_CRT_b, params.g_CRT_c);
col = EOTF_1886a_f3(vcolor, CRT_l, params.g_CRT_b, params.g_CRT_c);
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
@ -833,16 +829,16 @@ void main()
screen *= transpose(color);
// CRT Phosphor Gamut (0.0 is noop)
// CRT Phosphor Gamut (0.0 is sRGB/noop)
mat3 m_in;
if (crtgamut == -3.0) { m_in = SMPTE170M_ph; } else
if (crtgamut == -2.0) { m_in = CRT_95s_ph; } else
if (crtgamut == -2.0) { m_in = RPTV_95s_ph; } else
if (crtgamut == -1.0) { m_in = P22_80s_ph; } else
if (crtgamut == 0.0) { m_in = sRGB_prims; } else
if (crtgamut == 1.0) { m_in = P22_90s_ph; } else
if (crtgamut == 2.0) { m_in = P22_J_ph; } else
if (crtgamut == 3.0) { m_in = SMPTE470BG_ph; }
if (crtgamut == 3.0) { m_in = SMPTE470BG_ph; } else
{ m_in = sRGB_prims; }
m_in = (global.LUT1_toggle == 0.0) ? m_in : sRGB_prims;
@ -934,15 +930,13 @@ void main()
src_h *= (vignette == 1.0) ? vig : 1.0;
// Dark to Dim adaptation OOTF; only for 709 and 2020
vec3 src_D = global.g_Dark_to_Dim > 0.0 ? pow(src_h,vec3(0.9811)) : src_h;
// Dark to Dim adaptation OOTF; for 709, P3-D65 and 2020
float DtD = global.g_Dark_to_Dim > 0.0 ? 1/0.9811 : 1.0;
// EOTF^-1 - Inverted Electro-Optical Transfer Function
vec3 TRC = (SPC == 3.0) ? clamp(pow(src_h, vec3(1./ (563./256.))), 0., 1.) : \
(SPC == 2.0) ? moncurve_r_f3(src_D, 2.20 + 0.022222, 0.0993) : \
(SPC == 1.0) ? clamp(pow(src_h, vec3(1./(2.20 + 0.40))), 0., 1.) : \
(SPC == 0.0) ? moncurve_r_f3(src_h, 2.20 + 0.20, 0.0550) : \
clamp(pow( src_D, vec3(1./(2.20 + 0.20))), 0., 1.) ;
clamp(pow( src_h, vec3(1./((2.20 + 0.20)*DtD))), 0., 1.) ;
// External Flare for Surround Illuminant 2700K (Soft White) at F0 (Lambertian reflectance); defines offset thus also black lift