Skip to content

Commit

Permalink
Merge pull request #7 from papadanku/dev
Browse files Browse the repository at this point in the history
RaySchism: Do OKLab-based saturation
  • Loading branch information
papadanku authored Oct 9, 2024
2 parents e00a11e + 404c5d6 commit 1fdeab7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
11 changes: 7 additions & 4 deletions shaders/cChromaticity.fx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
uniform int _Select <
ui_label = "Chromaticity Method";
ui_type = "combo";
ui_items = "Length (XY)\0Length (XYZ)\0Average (XY)\0Average (XYZ)\0Sum (XY)\0Sum (XYZ)\0Max (XY)\0Max (XYZ)\0Ratio (XY)\0Spherical (XY)\0Hue-Saturation (HSI)\0Hue-Saturation (HSL)\0Hue-Saturation (HSV)\0CoCg (XY)\0CrCb (XY)\0";
ui_items = "Length (XY)\0Length (XYZ)\0Average (XY)\0Average (XYZ)\0Sum (XY)\0Sum (XYZ)\0Max (XY)\0Max (XYZ)\0Ratio (XY)\0Spherical (XY)\0Hue-Saturation (HSI)\0Hue-Saturation (HSL)\0Hue-Saturation (HSV)\0YCoCg (XY)\0OKLab (AB)\0OKLch (CH)\0";
> = 0;

#include "shared/cShadeHDR.fxh"
Expand Down Expand Up @@ -67,10 +67,13 @@ float4 PS_Chromaticity(CShade_VS2PS_Quad Input) : SV_TARGET0
Chromaticity.rg = CColor_GetHSVfromRGB(Color).rg;
break;
case 13: // CoCg (XY)
Chromaticity.rg = CColor_GetCoCg(Gamma);
Chromaticity.rg = CColor_GetYCOCGfromRGB(Gamma, true).yz;
break;
case 14: // CrCb (XY)
Chromaticity.rg = CColor_GetCrCb(Gamma);
case 14: // OKLab (AB)
Chromaticity.rg = CColor_GetOKLABfromRGB(Color).yz;
break;
case 15: // OKLch (CH)
Chromaticity.rg = CColor_GetOKLCHfromRGB(Color, true).yz;
break;
default: // No Chromaticity
Chromaticity.rgb = 0.0;
Expand Down
15 changes: 10 additions & 5 deletions shaders/cRaySchism.fx
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,22 @@ void ApplyColorGrading(inout float3 Color)
Color = (Color - ACEScc_MIDGRAY) * Contrast + ACEScc_MIDGRAY;
Color = CColor_DecodeLogC(Color);

Color = max(Color, 0.0);

// Apply color filter
Color *= _GradeColorFilter;

// Apply hue shifting
Color = CColor_GetOKLCHfromRGB(Color);
// Convert RGB to OKLch
Color = CColor_GetOKLCHfromRGB(Color, false);

// Apply hue shift
Color.z += HueShift;
Color = CColor_GetRGBfromOKLCH(Color);

// Apply saturation
float Luminance = CColor_GetLuma(Color, 3);
Color = lerp(Luminance, Color, Saturation);
Color.y *= Saturation;

// Convert OKLch to RGB
Color = CColor_GetRGBfromOKLCH(Color);

Color = max(Color, 0.0);
}
Expand Down
70 changes: 30 additions & 40 deletions shaders/shared/cColor.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,6 @@
return Ratio / (Ratio + 1.0);
}

/*
https://www.microsoft.com/en-us/research/publication/ycocg-r-a-color-space-with-rgb-reversibility-and-low-dynamic-range/
---
YCoCg-R: A Color Space with RGB Reversibility and Low Dynamic Range
Henrique S. Malvar, Gary Sullivan
MSR-TR-2003-103 | July 2003
---
Technical contribution to the H.264 Video Coding Standard. Joint Video Team (JVT) of ISO/IEC MPEG & ITU-T VCEG (ISO/IEC JTC1/SC29/WG11 and ITU-T SG16 Q.6) Document JVT-I014r3.
*/

float2 CColor_GetCoCg(float3 Color)
{
float2 CoCg = 0.0;
float2x3 MatCoCg = float2x3
(
float3(1.0, 0.0, -1.0),
float3(-0.5, 1.0, -0.5)
);

CoCg.x = dot(Color, MatCoCg[0]);
CoCg.y = dot(Color, MatCoCg[1]);

return (CoCg * 0.5) + 0.5;
}

/*
RGB to CrCb
---
https://docs.opencv.org/4.7.0/de/d25/imgproc_color_conversions.html
*/

float2 CColor_GetCrCb(float3 Color)
{
float Y = dot(Color, float3(0.299, 0.587, 0.114));
return ((Color.br - Y) * float2(0.564, 0.713)) + 0.5;
}

/*
Color-space conversion
---
Expand All @@ -101,6 +64,31 @@
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/

float3 CColor_GetYCOCGfromRGB(float3 RGB, bool Normalize)
{
float3x3 M = float3x3
(
1.0 / 4.0, 1.0 / 2.0, 1.0 / 4.0,
1.0 / 2.0, 0.0, -1.0 / 2.0,
-1.0 / 4.0, 1.0 / 2.0, -1.0 / 4.0
);

RGB = (Normalize) ? mul(M, RGB) + 0.5 : mul(M, RGB);
return RGB;
}

float3 CColor_GetRGBfromYCOCG(float3 YCoCg)
{
float3x3 M = float3x3
(
1.0, 1.0, -1.0,
1.0, 0.0, 1.0,
1.0, -1.0, -1.0
);

return mul(M, YCoCg);
}

float3 CColor_GetHSVfromRGB(float3 Color)
{
float MinRGB = min(min(Color.r, Color.g), Color.b);
Expand Down Expand Up @@ -298,12 +286,14 @@
return LMS;
}

float3 CColor_GetOKLCHfromOKLAB(float3 OKLab)
float3 CColor_GetOKLCHfromOKLAB(float3 OKLab, bool Normalize)
{
const float Pi2 = CMath_GetPi() * 2.0;
float3 OKLch = 0.0;
OKLch.x = OKLab.x;
OKLch.y = length(OKLab.yz);
OKLch.z = atan2(OKLab.z, OKLab.y);
OKLch.z = (Normalize) ? OKLch.z / Pi2 : OKLch.z;
return OKLch;
}

Expand All @@ -316,9 +306,9 @@
return OKLab;
}

float3 CColor_GetOKLCHfromRGB(float3 Color)
float3 CColor_GetOKLCHfromRGB(float3 Color, bool Normalize)
{
return CColor_GetOKLCHfromOKLAB(CColor_GetOKLABfromRGB(Color));
return CColor_GetOKLCHfromOKLAB(CColor_GetOKLABfromRGB(Color), Normalize);
}

float3 CColor_GetRGBfromOKLCH(float3 OKLch)
Expand Down

0 comments on commit 1fdeab7

Please sign in to comment.