Skip to content

Commit

Permalink
Merge pull request #68 from marat569/visionsofmana
Browse files Browse the repository at this point in the history
Visionsofmana
  • Loading branch information
marat569 authored Dec 22, 2024
2 parents 3df65a5 + b45f724 commit 6ede906
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 222 deletions.
12 changes: 12 additions & 0 deletions src/games/visionsofmana/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ renodx::utils::settings::Settings settings = {
.parse = [](float value) { return value * 0.02f - 1.f; },
},

new renodx::utils::settings::Setting{
.key = "colorGradeFlare",
.binding = &shader_injection.colorGradeFlare,
.default_value = 0.f,
.label = "Flare",
.section = "Color Grading",
.tooltip = "Flare/Glare",
.max = 100.f,
.is_enabled = []() { return shader_injection.toneMapType == 3; },
.parse = [](float value) { return value * 0.02f; },
},

new renodx::utils::settings::Setting{
.key = "toneMapHueCorrection",
.binding = &shader_injection.toneMapHueCorrection,
Expand Down
80 changes: 66 additions & 14 deletions src/games/visionsofmana/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,67 @@

// Common functions

void tonemap(in float3 ap1_graded_color, in float3 ap1_aces_colored, out float3 hdr_color, out float3 sdr_color, inout float3 sdr_ap1_color) {
float3 RenoDRTSmoothClamp(float3 untonemapped) {
renodx::tonemap::renodrt::Config renodrt_config =
renodx::tonemap::renodrt::config::Create();
renodrt_config.nits_peak = 100.f;
renodrt_config.mid_gray_value = 0.18f;
renodrt_config.mid_gray_nits = 18.f;
renodrt_config.exposure = 1.f;
renodrt_config.highlights = 1.f;
renodrt_config.shadows = 1.f;
renodrt_config.contrast = 1.05f;
renodrt_config.saturation = 1.05f;
renodrt_config.dechroma = 0.f;
renodrt_config.flare = 0.f;
renodrt_config.hue_correction_strength = 0.f;
renodrt_config.tone_map_method =
renodx::tonemap::renodrt::config::tone_map_method::DANIELE;
renodrt_config.working_color_space = 2u;

return renodx::tonemap::renodrt::BT709(untonemapped, renodrt_config);
}
float UpgradeToneMapRatio(float ap1_color_hdr, float ap1_color_sdr, float ap1_post_process_color) {
if (ap1_color_hdr < ap1_color_sdr) {
// If substracting (user contrast or paperwhite) scale down instead
// Should only apply on mismatched HDR
return ap1_color_hdr / ap1_color_sdr;
} else {
float ap1_delta = ap1_color_hdr - ap1_color_sdr;
ap1_delta = max(0, ap1_delta); // Cleans up NaN
const float ap1_new = ap1_post_process_color + ap1_delta;

const bool ap1_valid = (ap1_post_process_color > 0); // Cleans up NaN and ignore black
return ap1_valid ? (ap1_new / ap1_post_process_color) : 0;
}
}
float3 UpgradeToneMapPerChannel(float3 color_hdr, float3 color_sdr, float3 post_process_color, float post_process_strength) {
// float ratio = 1.f;

float3 ap1_hdr = max(0, renodx::color::ap1::from::BT709(color_hdr));
float3 ap1_sdr = max(0, renodx::color::ap1::from::BT709(color_sdr));
float3 ap1_post_process = max(0, renodx::color::ap1::from::BT709(post_process_color));

float3 ratio = float3(
UpgradeToneMapRatio(ap1_hdr.r, ap1_sdr.r, ap1_post_process.r),
UpgradeToneMapRatio(ap1_hdr.g, ap1_sdr.g, ap1_post_process.g),
UpgradeToneMapRatio(ap1_hdr.b, ap1_sdr.b, ap1_post_process.b));

float3 color_scaled = max(0, ap1_post_process * ratio);
color_scaled = renodx::color::bt709::from::AP1(color_scaled);
float peak_correction = saturate(1.f - renodx::color::y::from::AP1(ap1_post_process));
color_scaled = renodx::color::correct::Hue(color_scaled, post_process_color, peak_correction);
return lerp(color_hdr, color_scaled, post_process_strength);
}

void tonemap(in float3 ap1_graded_color, in float3 ap1_aces_colored, in float3 film_graded_color, in float3 hdr_color, in float3 sdr_color, inout float3 sdr_ap1_color, out float3 final_color) {
float3 bt709_graded_color = renodx::color::bt709::from::AP1(ap1_graded_color);
float3 bt709_aces_color = renodx::color::bt709::from::AP1(ap1_aces_colored);

float3 neutral_sdr_color = RenoDRTSmoothClamp(bt709_graded_color);

float3 color_graded = UpgradeToneMapPerChannel(bt709_graded_color, neutral_sdr_color, film_graded_color, 1);

renodx::tonemap::Config config = renodx::tonemap::config::Create();
config.type = injectedData.toneMapType;
config.peak_nits = injectedData.toneMapPeakNits;
Expand All @@ -12,18 +72,16 @@ void tonemap(in float3 ap1_graded_color, in float3 ap1_aces_colored, out float3
config.exposure = injectedData.colorGradeExposure;
config.highlights = injectedData.colorGradeHighlights;
config.shadows = injectedData.colorGradeShadows;
// if (injectedData.toneMapType == 3.f) {
config.contrast = injectedData.colorGradeContrast;
// }
config.saturation = injectedData.colorGradeSaturation;

config.reno_drt_highlights = 1.0f;
config.reno_drt_shadows = 1.0f;
config.reno_drt_contrast = 1.1f;
config.reno_drt_saturation = 1.1f;
config.reno_drt_contrast = 1.05f;
config.reno_drt_saturation = 1.05f;
config.reno_drt_dechroma = 0;
config.reno_drt_blowout = injectedData.colorGradeBlowout;
config.reno_drt_flare = 0.05f;
config.reno_drt_flare = injectedData.colorGradeFlare;
config.reno_drt_working_color_space = 2u;

if (injectedData.toneMapPerChannel) {
Expand All @@ -32,9 +90,6 @@ void tonemap(in float3 ap1_graded_color, in float3 ap1_aces_colored, out float3

config.reno_drt_hue_correction_method = (uint)injectedData.ToneMapHueProcessor;

float3 bt709_graded_color = renodx::color::bt709::from::AP1(ap1_graded_color);
float3 bt709_aces_color = renodx::color::bt709::from::AP1(ap1_aces_colored);

config.hue_correction_type =
renodx::tonemap::config::hue_correction_type::CUSTOM;
config.hue_correction_strength = injectedData.toneMapHueCorrection;
Expand All @@ -50,10 +105,7 @@ void tonemap(in float3 ap1_graded_color, in float3 ap1_aces_colored, out float3
renodx::tonemap::config::hue_correction_type::INPUT;
}

renodx::tonemap::config::DualToneMap dual_tone_map = renodx::tonemap::config::ApplyToneMaps(bt709_graded_color, config);
hdr_color = dual_tone_map.color_hdr;
sdr_color = dual_tone_map.color_sdr;
sdr_ap1_color = renodx::color::ap1::from::BT709(sdr_color);
final_color = renodx::tonemap::config::Apply(color_graded, config);
}

float3 scalePaperWhite(float3 color) {
Expand All @@ -63,4 +115,4 @@ float3 scalePaperWhite(float3 color) {
color = renodx::math::PowSafe(color, 1.f / 2.2f); //

return color;
}
}
137 changes: 66 additions & 71 deletions src/games/visionsofmana/lutbuilder1_0xC1BCC6B5.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -357,78 +357,73 @@ void main(

ap1_aces_colored = r3.xyz;

if (injectedData.toneMapType != 0.f) {
tonemap(ap1_graded_color, ap1_aces_colored, hdr_color, sdr_color, sdr_ap1_color);
r4.xy = float2(1, 0.180000007) + cb0[36].ww;
r0.w = -cb0[36].y + r4.x;
r1.w = 1 + cb0[37].x;
r2.w = -cb0[36].z + r1.w;

} else { // Added else
r4.xy = float2(1, 0.180000007) + cb0[36].ww;
r0.w = -cb0[36].y + r4.x;
r1.w = 1 + cb0[37].x;
r2.w = -cb0[36].z + r1.w;
// Film Toe > 0.8

// Film Toe > 0.8

r3.w = cmp(0.800000012 < cb0[36].y);
r4.xz = float2(0.819999993, 1) + -cb0[36].yy;
r4.xz = r4.xz / cb0[36].xx;
r4.y = r4.y / r0.w;
r4.xw = float2(-0.744727492, -1) + r4.xy;
r4.w = 1 + -r4.w;
r4.y = r4.y / r4.w;
r4.y = log2(r4.y);
r4.y = 0.346573591 * r4.y;
r4.w = r0.w / cb0[36].x;
r4.y = -r4.y * r4.w + -0.744727492;
r3.w = r3.w ? r4.x : r4.y;
r4.x = r4.z + -r3.w;
r4.y = cb0[36].z / cb0[36].x;
r4.y = r4.y + -r4.x;
r3.xyz = log2(r3.xyz);
r5.xyz = float3(0.30103001, 0.30103001, 0.30103001) * r3.xyz;
r4.xzw = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + r4.xxx;
r4.xzw = cb0[36].xxx * r4.xzw;
r5.w = r0.w + r0.w;
r6.x = -2 * cb0[36].x;
r0.w = r6.x / r0.w;
r6.xyz = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + -r3.www;
r7.xyz = r6.xyz * r0.www;
r7.xyz = float3(1.44269502, 1.44269502, 1.44269502) * r7.xyz;
r7.xyz = exp2(r7.xyz);
r7.xyz = float3(1, 1, 1) + r7.xyz;
r7.xyz = r5.www / r7.xyz;
r7.xyz = -cb0[36].www + r7.xyz;
r0.w = r2.w + r2.w;
r5.w = cb0[36].x + cb0[36].x;
r2.w = r5.w / r2.w;
r3.xyz = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + -r4.yyy;
r3.xyz = r3.xyz * r2.www;
r3.xyz = float3(1.44269502, 1.44269502, 1.44269502) * r3.xyz;
r3.xyz = exp2(r3.xyz);
r3.xyz = float3(1, 1, 1) + r3.xyz;
r3.xyz = r0.www / r3.xyz;
r3.xyz = -r3.xyz + r1.www;
r8.xyz = cmp(r5.xyz < r3.www);
r7.xyz = r8.xyz ? r7.xyz : r4.xzw;
r5.xyz = cmp(r4.yyy < r5.xyz);
r3.xyz = r5.xyz ? r3.xyz : r4.xzw;
r0.w = r4.y + -r3.w;
r4.xzw = saturate(r6.xyz / r0.www);
r0.w = cmp(r4.y < r3.w);
r5.xyz = float3(1, 1, 1) + -r4.xzw;
r4.xyz = r0.www ? r5.xyz : r4.xzw;
r5.xyz = -r4.xyz * float3(2, 2, 2) + float3(3, 3, 3);
r4.xyz = r4.xyz * r4.xyz;
r4.xyz = r4.xyz * r5.xyz;
r3.xyz = r3.xyz + -r7.xyz;
r3.xyz = r4.xyz * r3.xyz + r7.xyz;
// AP1_RGB2Y
r0.w = dot(r3.xyz, float3(0.272228718, 0.674081743, 0.0536895171));
r3.xyz = r3.xyz + -r0.www;
r3.w = cmp(0.800000012 < cb0[36].y);
r4.xz = float2(0.819999993, 1) + -cb0[36].yy;
r4.xz = r4.xz / cb0[36].xx;
r4.y = r4.y / r0.w;
r4.xw = float2(-0.744727492, -1) + r4.xy;
r4.w = 1 + -r4.w;
r4.y = r4.y / r4.w;
r4.y = log2(r4.y);
r4.y = 0.346573591 * r4.y;
r4.w = r0.w / cb0[36].x;
r4.y = -r4.y * r4.w + -0.744727492;
r3.w = r3.w ? r4.x : r4.y;
r4.x = r4.z + -r3.w;
r4.y = cb0[36].z / cb0[36].x;
r4.y = r4.y + -r4.x;
r3.xyz = log2(r3.xyz);
r5.xyz = float3(0.30103001, 0.30103001, 0.30103001) * r3.xyz;
r4.xzw = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + r4.xxx;
r4.xzw = cb0[36].xxx * r4.xzw;
r5.w = r0.w + r0.w;
r6.x = -2 * cb0[36].x;
r0.w = r6.x / r0.w;
r6.xyz = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + -r3.www;
r7.xyz = r6.xyz * r0.www;
r7.xyz = float3(1.44269502, 1.44269502, 1.44269502) * r7.xyz;
r7.xyz = exp2(r7.xyz);
r7.xyz = float3(1, 1, 1) + r7.xyz;
r7.xyz = r5.www / r7.xyz;
r7.xyz = -cb0[36].www + r7.xyz;
r0.w = r2.w + r2.w;
r5.w = cb0[36].x + cb0[36].x;
r2.w = r5.w / r2.w;
r3.xyz = r3.xyz * float3(0.30103001, 0.30103001, 0.30103001) + -r4.yyy;
r3.xyz = r3.xyz * r2.www;
r3.xyz = float3(1.44269502, 1.44269502, 1.44269502) * r3.xyz;
r3.xyz = exp2(r3.xyz);
r3.xyz = float3(1, 1, 1) + r3.xyz;
r3.xyz = r0.www / r3.xyz;
r3.xyz = -r3.xyz + r1.www;
r8.xyz = cmp(r5.xyz < r3.www);
r7.xyz = r8.xyz ? r7.xyz : r4.xzw;
r5.xyz = cmp(r4.yyy < r5.xyz);
r3.xyz = r5.xyz ? r3.xyz : r4.xzw;
r0.w = r4.y + -r3.w;
r4.xzw = saturate(r6.xyz / r0.www);
r0.w = cmp(r4.y < r3.w);
r5.xyz = float3(1, 1, 1) + -r4.xzw;
r4.xyz = r0.www ? r5.xyz : r4.xzw;
r5.xyz = -r4.xyz * float3(2, 2, 2) + float3(3, 3, 3);
r4.xyz = r4.xyz * r4.xyz;
r4.xyz = r4.xyz * r5.xyz;
r3.xyz = r3.xyz + -r7.xyz;
r3.xyz = r4.xyz * r3.xyz + r7.xyz;
// AP1_RGB2Y
r0.w = dot(r3.xyz, float3(0.272228718, 0.674081743, 0.0536895171));
r3.xyz = r3.xyz + -r0.www;

r3.xyz = r3.xyz * float3(0.930000007, 0.930000007, 0.930000007) + r0.www;
r3.xyz = max(float3(0, 0, 0), r3.xyz);
sdr_ap1_color = r3.xyz;
} // Added brace
r3.xyz = r3.xyz * float3(0.930000007, 0.930000007, 0.930000007) + r0.www;
r3.xyz = max(float3(0, 0, 0), r3.xyz);
sdr_ap1_color = r3.xyz;

r3.xyz = sdr_ap1_color;

Expand Down Expand Up @@ -464,8 +459,8 @@ void main(
float3 film_graded_color = r3.rgb;

if (injectedData.toneMapType != 0.f) {
float3 final_color = saturate(film_graded_color);
final_color = renodx::tonemap::UpgradeToneMap(hdr_color, sdr_color, final_color, 1.f);
float3 final_color;
tonemap(ap1_graded_color, ap1_aces_colored, film_graded_color, hdr_color, sdr_color, sdr_ap1_color, final_color);

final_color.rgb = scalePaperWhite(final_color.rgb);

Expand Down
Loading

0 comments on commit 6ede906

Please sign in to comment.