Skip to content

Commit

Permalink
Merge branch 'clshortfuse:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
marat569 authored Nov 8, 2024
2 parents 3e95b60 + 7b387b9 commit 0b7df11
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 64 deletions.
15 changes: 13 additions & 2 deletions src/games/cp2077/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ renodx::utils::settings::Settings settings = {
.key = "toneMapGammaCorrection",
.binding = &shader_injection.toneMapGammaCorrection,
.value_type = renodx::utils::settings::SettingValueType::INTEGER,
.default_value = 0.f,
.default_value = 2.f,
.can_reset = false,
.label = "Gamma Correction",
.section = "Tone Mapping",
Expand All @@ -127,6 +127,17 @@ renodx::utils::settings::Settings settings = {
.tooltip = "Applies hue shift emulation before tonemapping",
.labels = {"None", "Reinhard", "ACES BT709", "ACES AP1", "Filmic"},
},
new renodx::utils::settings::Setting{
.key = "toneMapHueProcessor",
.binding = &shader_injection.toneMapHueProcessor,
.value_type = renodx::utils::settings::SettingValueType::INTEGER,
.default_value = 1.f,
.can_reset = false,
.label = "Hue Processor",
.section = "Tone Mapping",
.tooltip = "Selects hue processor",
.labels = {"OKLab", "ICtCp" },
},
new renodx::utils::settings::Setting{
.key = "colorGradeExposure",
.binding = &shader_injection.colorGradeExposure,
Expand Down Expand Up @@ -189,7 +200,7 @@ renodx::utils::settings::Settings settings = {
.default_value = 50.f,
.label = "Flare",
.section = "Color Grading",
.tooltip = "Flare/Glare",
.tooltip = "Flare/Glare Compensation",
.max = 100.f,
.is_enabled = []() { return shader_injection.toneMapType == 3; },
.parse = [](float value) { return value * 0.02f; },
Expand Down
2 changes: 1 addition & 1 deletion src/games/cp2077/colormath.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ float3 convertColor(float3 inputColor, ConvertColorParams params) {
default:
// Unknown default case
outputColor *= params.paperWhiteScaling;
outputColor += params.blackFloorAdjust;
// outputColor += params.blackFloorAdjust;
}
return outputColor.rgb;
}
3 changes: 2 additions & 1 deletion src/games/cp2077/cp2077.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
// Must be 32bit aligned
// Should be 4x32
// 27 max for pixel/vertex
// 49 max for 49
// 49 max for compute

struct ShaderInjectData {
float toneMapType;
float toneMapPeakNits;
float toneMapGameNits;
float toneMapGammaCorrection;
float toneMapHueCorrection;
float toneMapHueProcessor;

float fxBloom;
float fxVignette;
Expand Down
53 changes: 27 additions & 26 deletions src/games/cp2077/tonemapper.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include "./injectedBuffer.hlsl"

static const float HEATMAP_COLORS[27] = {
0.0f, 0.0f, 0.0f, // Black
0.0f, 0.0f, 1.0f, // Blue
0.0f, 1.0f, 1.0f, // Cyan
0.0f, 1.0f, 0.0f, // Green
1.0f, 1.0f, 0.0f, // Yellow
1.0f, 0.0f, 0.0f, // Red
1.0f, 0.0f, 1.0f, // Magenta
1.0f, 1.0f, 1.0f, // White
1.0f, 1.0f, 1.0f, // White
0.0f, 0.0f, 0.0f, // Black
0.0f, 0.0f, 1.0f, // Blue
0.0f, 1.0f, 1.0f, // Cyan
0.0f, 1.0f, 0.0f, // Green
1.0f, 1.0f, 0.0f, // Yellow
1.0f, 0.0f, 0.0f, // Red
1.0f, 0.0f, 1.0f, // Magenta
1.0f, 1.0f, 1.0f, // White
1.0f, 1.0f, 1.0f, // White
};

static const float SQRT_HALF = sqrt(1.f / 2.f); // 0.7071067811865476
Expand Down Expand Up @@ -220,8 +220,8 @@ float3 SampleLUT(float4 lutSettings, const float3 inputColor, uint textureIndex)
renodx::color::srgb::Decode(unclamped));
color = lerp(color, recolored, min(injectedData.processingLUTCorrection * 2.f, 1.f));
} else {
const float lutMinY = renodx::color::y::from::BT709(minBlack);
const float lutPeakY = renodx::color::y::from::BT709(peakWhite);
const float lutMinY = renodx::color::y::from::BT709(abs(minBlack));
const float lutPeakY = renodx::color::y::from::BT709(abs(peakWhite));
const float targetPeakY = 100.f / max(lutSettings.z, 1.f);

// Only applies on LUTs that are clamped (all?)
Expand Down Expand Up @@ -325,7 +325,7 @@ float4 tonemap(bool isACESMode = false) {

const float hueShift = cb6[7u].z;
// Add branch to skip if not hue shifting
if (hueShift) {
if (hueShift != 0.f) {
float _260 = sin(hueShift); // sin(0)
float _261 = cos(hueShift); // cos(0)
float _262 = (-0.0f) - _260;
Expand Down Expand Up @@ -400,13 +400,13 @@ float4 tonemap(bool isACESMode = false) {
}

if (injectedData.toneMapHueCorrection == 1.f) {
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::Reinhard(outputRGB));
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::Reinhard(outputRGB), 1.f, (uint)injectedData.toneMapHueProcessor);
} else if (injectedData.toneMapHueCorrection == 2.f) {
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::ACESFittedBT709(outputRGB));
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::ACESFittedBT709(outputRGB), 1.f, (uint)injectedData.toneMapHueProcessor);
} else if (injectedData.toneMapHueCorrection == 3.f) {
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::ACESFittedAP1(outputRGB));
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::ACESFittedAP1(outputRGB), 1.f, (uint)injectedData.toneMapHueProcessor);
} else if (injectedData.toneMapHueCorrection == 4.f) {
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::uncharted2::BT709(outputRGB));
outputRGB = renodx::color::correct::Hue(outputRGB, renodx::tonemap::uncharted2::BT709(outputRGB), 1.f, (uint)injectedData.toneMapHueProcessor);
}

if (toneMapperType == TONE_MAPPER_TYPE__VANILLA) {
Expand Down Expand Up @@ -439,13 +439,13 @@ float4 tonemap(bool isACESMode = false) {
// Scales with paperwhite, which can be reversed

const SegmentedSplineParams_c9 ODT_CONFIG = {
{cb6[8u].x, cb6[9u].x, cb6[10u].x, cb6[11u].x, cb6[12u].x, cb6[13u].x, cb6[14u].x, cb6[15u].x, cb6[16u].x, cb6[17u].x}, // coefsLow[10]
{cb6[8u].y, cb6[9u].y, cb6[10u].y, cb6[11u].y, cb6[12u].y, cb6[13u].y, cb6[14u].y, cb6[15u].y, cb6[16u].y, cb6[17u].y}, // coefsHigh[10]
{cb6[18u].x, cb6[18u].y}, // minPoint
{cb6[18u].z, midGrayNits}, // midPoint
{cb6[19u].x, cb6[19u].y}, // maxPoint - doesn't always match peak nits?
cb6[19u].z, // slopeLow
cb6[19u].w // slopeHigh
{ cb6[8u].x, cb6[9u].x, cb6[10u].x, cb6[11u].x, cb6[12u].x, cb6[13u].x, cb6[14u].x, cb6[15u].x, cb6[16u].x, cb6[17u].x }, // coefsLow[10]
{ cb6[8u].y, cb6[9u].y, cb6[10u].y, cb6[11u].y, cb6[12u].y, cb6[13u].y, cb6[14u].y, cb6[15u].y, cb6[16u].y, cb6[17u].y }, // coefsHigh[10]
{ cb6[18u].x, cb6[18u].y }, // minPoint
{ cb6[18u].z, midGrayNits }, // midPoint
{ cb6[19u].x, cb6[19u].y }, // maxPoint - doesn't always match peak nits?
cb6[19u].z, // slopeLow
cb6[19u].w // slopeHigh
};

float yRange = peakNits - minNits;
Expand Down Expand Up @@ -550,7 +550,7 @@ float4 tonemap(bool isACESMode = false) {
config.type = injectedData.toneMapType;
config.peak_nits = injectedData.toneMapPeakNits;
config.game_nits = (injectedData.toneMapType == 2.f ? (100.f / 203.f) : 1.f) * injectedData.toneMapGameNits;
config.gamma_correction = injectedData.toneMapGammaCorrection == 2.f;
config.gamma_correction = (injectedData.toneMapGammaCorrection == 2.f) ? 1.f : 0.f;
config.exposure = injectedData.colorGradeExposure;
config.highlights = injectedData.colorGradeHighlights;
config.shadows = injectedData.colorGradeShadows;
Expand All @@ -560,10 +560,11 @@ float4 tonemap(bool isACESMode = false) {
config.mid_gray_nits = midGrayNits;
config.reno_drt_highlights = 1.20f;
config.reno_drt_shadows = 1.20f;
config.reno_drt_contrast = 1.20f;
config.reno_drt_contrast = 1.3f;
config.reno_drt_saturation = 1.20f;
config.reno_drt_dechroma = injectedData.colorGradeBlowout;
config.reno_drt_flare = 0.10f * pow(injectedData.colorGradeFlare, 10.f);
config.reno_drt_flare = 0.005 * injectedData.colorGradeFlare;
config.reno_drt_hue_correction_method = (uint)injectedData.toneMapHueProcessor;

outputRGB = renodx::tonemap::config::Apply(outputRGB, config);
bool useD60 = (injectedData.colorGradeWhitePoint == -1.0f || (injectedData.colorGradeWhitePoint == 0.f && cb6[28u].z == 0.f));
Expand Down
29 changes: 19 additions & 10 deletions src/shaders/RenoDRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ struct Config {
float3 hue_correction_source;
uint hue_correction_method;
uint tone_map_method;
uint hue_correction_type;
};
namespace config {

namespace hue_correction_type {
static const uint INPUT = 0u;
static const uint CUSTOM = 1u;
}

namespace hue_correction_method {
static const uint OKLAB = 0u;
static const uint ICTCP = 1u;
Expand All @@ -49,7 +56,8 @@ Config Create(
float hue_correction_strength = 1.f,
float3 hue_correction_source = 0,
uint hue_correction_method = config::hue_correction_method::OKLAB,
uint tone_map_method = config::tone_map_method::DANIELE) {
uint tone_map_method = config::tone_map_method::DANIELE,
uint hue_correction_type = config::hue_correction_type::INPUT) {
const Config renodrt_config = {
nits_peak,
mid_gray_value,
Expand All @@ -64,7 +72,8 @@ Config Create(
hue_correction_strength,
hue_correction_source,
hue_correction_method,
tone_map_method
tone_map_method,
hue_correction_type
};
return renodrt_config;
}
Expand Down Expand Up @@ -98,18 +107,17 @@ float3 BT709(float3 bt709, Config current_config) {
n = current_config.nits_peak;
t_1 = current_config.flare;

float3 signs = renodx::math::Sign(bt709);

bt709 = abs(bt709);

float y_original = renodx::color::y::from::BT709(bt709);
float y_original = renodx::color::y::from::BT709(abs(bt709));

float3 perceptual_old;
if (current_config.hue_correction_strength != 0) {
float3 source = (current_config.hue_correction_type == config::hue_correction_type::INPUT)
? bt709
: current_config.hue_correction_source;
if (current_config.hue_correction_method == config::hue_correction_method::OKLAB) {
perceptual_old = renodx::color::oklab::from::BT709(current_config.hue_correction_source);
perceptual_old = renodx::color::oklab::from::BT709(source);
} else if (current_config.hue_correction_method == config::hue_correction_method::ICTCP) {
perceptual_old = renodx::color::ictcp::from::BT709(current_config.hue_correction_source);
perceptual_old = renodx::color::ictcp::from::BT709(source);
}
}

Expand Down Expand Up @@ -162,7 +170,7 @@ float3 BT709(float3 bt709, Config current_config) {

float y_new = clamp(flared, 0, m_0);

float3 color_output = signs * bt709 * (y_original > 0 ? (y_new / y_original) : 0);
float3 color_output = bt709 * (y_original > 0 ? (y_new / y_original) : 0);
float3 color = color_output;

if (current_config.dechroma != 0.f || current_config.saturation != 1.f || current_config.hue_correction_strength != 0.f) {
Expand Down Expand Up @@ -232,6 +240,7 @@ float3 BT709(
config.flare = flare;
config.hue_correction_strength = hue_correction_strength;
config.hue_correction_source = hue_correction_source;
config.hue_correction_type = renodrt::config::hue_correction_type::CUSTOM;
return BT709(bt709, config);
}
float3 BT709(
Expand Down
31 changes: 30 additions & 1 deletion src/shaders/colorcorrect.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ float4 GammaSafe(float4 color, bool pow2srgb = false) {
return float4(GammaSafe(color.rgb, pow2srgb), color.a);
}

float3 Hue(float3 incorrect_color, float3 correct_color, float strength = 1.f) {
float3 HueOKLab(float3 incorrect_color, float3 correct_color, float strength = 1.f) {
if (strength == 0.f) return incorrect_color;

float3 correct_lab = renodx::color::oklab::from::BT709(correct_color);
Expand All @@ -71,6 +71,35 @@ float3 Hue(float3 incorrect_color, float3 correct_color, float strength = 1.f) {
return color;
}

float3 HueICtCp(float3 incorrect_color, float3 correct_color, float strength = 1.f) {
if (strength == 0.f) return incorrect_color;

float3 correct_perceptual = renodx::color::ictcp::from::BT709(correct_color);

float3 incorrect_perceptual = renodx::color::ictcp::from::BT709(incorrect_color);

float chrominance_pre_adjust = distance(incorrect_perceptual.yz, 0);

incorrect_perceptual.yz = lerp(incorrect_perceptual.yz, correct_perceptual.yz, strength);

float chrominance_post_adjust = distance(incorrect_perceptual.yz, 0);

if (chrominance_post_adjust != 0.f) {
incorrect_perceptual.yz *= chrominance_pre_adjust / chrominance_post_adjust;
}

float3 color = renodx::color::bt709::from::ICtCp(incorrect_perceptual);
color = renodx::color::bt709::clamp::AP1(color);
return color;
}

float3 Hue(float3 incorrect_color, float3 correct_color, float strength = 1.f, uint method = 0u) {
if (method == 1u) {
return HueICtCp(incorrect_color, correct_color, strength);
}
return HueOKLab(incorrect_color, correct_color, strength);
}

} // namespace correct
} // namespace color
} // namespace renodx
Expand Down
8 changes: 4 additions & 4 deletions src/shaders/lut.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ float3 SampleUnreal(Texture2D lut, SamplerState state, float3 color, float size
}

float3 CorrectBlack(float3 color_input, float3 lut_color, float lut_black_y, float strength) {
const float input_y = renodx::color::y::from::BT709(color_input);
const float color_y = renodx::color::y::from::BT709(lut_color);
const float input_y = renodx::color::y::from::BT709(abs(color_input));
const float color_y = renodx::color::y::from::BT709(abs(lut_color));
const float a = lut_black_y;
const float b = lerp(0, lut_black_y, strength);
const float g = input_y;
Expand All @@ -169,8 +169,8 @@ float3 CorrectBlack(float3 color_input, float3 lut_color, float lut_black_y, flo
}

float3 CorrectWhite(float3 color_input, float3 lut_color, float lut_white_y, float target_white_y, float strength) {
const float input_y = min(target_white_y, renodx::color::y::from::BT709(color_input));
const float color_y = renodx::color::y::from::BT709(lut_color);
const float input_y = min(target_white_y, renodx::color::y::from::BT709(abs(color_input)));
const float color_y = renodx::color::y::from::BT709(abs(lut_color));
const float a = lut_white_y / target_white_y;
const float b = lerp(1.f, 0.f, strength);
const float g = input_y;
Expand Down
Loading

0 comments on commit 0b7df11

Please sign in to comment.