Skip to content

Commit

Permalink
feat(dbzspark): remove blowout, fix NaNs and UI interference, and swi…
Browse files Browse the repository at this point in the history
…tch srgb clamp to smoothRenoDRT
  • Loading branch information
MohannedElfatih authored and clshortfuse committed Dec 5, 2024
1 parent d9b7448 commit e75547d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 46 deletions.
20 changes: 4 additions & 16 deletions src/games/dbzspark/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ renodx::utils::settings::Settings settings = {
.key = "toneMapType",
.binding = &shader_injection.toneMapType,
.value_type = renodx::utils::settings::SettingValueType::INTEGER,
.default_value = 0.f,
.default_value = 3.f,
.can_reset = true,
.label = "Tone Mapper",
.section = "Tone Mapping",
Expand Down Expand Up @@ -95,7 +95,7 @@ renodx::utils::settings::Settings settings = {
new renodx::utils::settings::Setting{
.key = "toneMapGameNits",
.binding = &shader_injection.toneMapGameNits,
.default_value = 60.f,
.default_value = 150.f,
.can_reset = true,
.label = "Game Brightness",
.section = "Tone Mapping",
Expand Down Expand Up @@ -173,17 +173,6 @@ renodx::utils::settings::Settings settings = {
.is_enabled = []() { return shader_injection.toneMapType != 1; },
.parse = [](float value) { return value * 0.02f; },
},
new renodx::utils::settings::Setting{
.key = "colorGradeBlowout",
.binding = &shader_injection.colorGradeBlowout,
.default_value = 0.f,
.label = "Blowout",
.section = "Color Grading",
.tooltip = "Controls highlight desaturation due to overexposure.",
.max = 100.f,
.is_enabled = []() { return shader_injection.toneMapType == 3; },
.parse = [](float value) { return value * 0.01f; },
},
new renodx::utils::settings::Setting{
.value_type = renodx::utils::settings::SettingValueType::TEXT,
.label = " - Please enable Native HDR in game! \r\n - Use a low paper white with Vanilla+, and high with others! \r\n - Consider lowering peak to reduce sharp highlights",
Expand Down Expand Up @@ -244,17 +233,16 @@ renodx::utils::settings::Settings settings = {

void OnPresetOff() {
renodx::utils::settings::UpdateSetting("toneMapType", 0.f);
renodx::utils::settings::UpdateSetting("toneMapDisplay", 1.f);
renodx::utils::settings::UpdateSetting("toneMapDisplay", 0.f);
renodx::utils::settings::UpdateSetting("toneMapPeakNits", 400.f);
renodx::utils::settings::UpdateSetting("toneMapGameNits", 60.f);
renodx::utils::settings::UpdateSetting("toneMapUINits", 120.f);
renodx::utils::settings::UpdateSetting("toneMapGammaCorrection", 1.f);
renodx::utils::settings::UpdateSetting("toneMapGammaCorrection", 0.f);
renodx::utils::settings::UpdateSetting("colorGradeExposure", 1.f);
renodx::utils::settings::UpdateSetting("colorGradeHighlights", 50.f);
renodx::utils::settings::UpdateSetting("colorGradeShadows", 50.f);
renodx::utils::settings::UpdateSetting("colorGradeContrast", 50.f);
renodx::utils::settings::UpdateSetting("colorGradeSaturation", 50.f);
renodx::utils::settings::UpdateSetting("colorGradeBlowout", 0.f);
}

} // namespace
Expand Down
4 changes: 2 additions & 2 deletions src/games/dbzspark/colorcorrect_0x5975CAFA.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main(

tonemappedPQ = r1.rgb;
// Hacky fix for shop
float customDecode = 203.f;
float customDecode = 0.f;
r1.rgb = pqTosRGB(tonemappedPQ, customDecode, true);

r0.zw = r0.xy * cb1[129].xy + cb1[128].xy;
Expand Down Expand Up @@ -249,7 +249,7 @@ void main(
post_srgb = o0.rgb;
o0.w = 0;

o0.rgb = upgradeSRGBtoPQ(tonemappedPQ, post_srgb, customDecode, 1.f);
o0.rgb = upgradeSRGBtoPQ(tonemappedPQ, post_srgb, customDecode);

return;
}
16 changes: 10 additions & 6 deletions src/games/dbzspark/final00_0x4156562D.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ void main(
injectedData.colorGradeContrast,
1.f); // We'll do saturation post tonemap

r1.rgb = displayTonemap(r1.rgb);
if (injectedData.toneMapGammaCorrection == 1.f) {
r0.rgb = renodx::color::correct::GammaSafe(r0.rgb);
r1.rgb = renodx::color::correct::GammaSafe(r1.rgb);
}

r0.rgb *= injectedData.toneMapUINits / 203.f; // Value found so it matches tonemapUINits

r1.rgb = renodx::color::grade::UserColorGrading(
r1.rgb,
Expand All @@ -62,12 +67,11 @@ void main(
1.f,
injectedData.colorGradeSaturation);

if (injectedData.toneMapGammaCorrection == 1.f) {
r0.rgb = renodx::color::correct::GammaSafe(r0.rgb);
r1.rgb = renodx::color::correct::GammaSafe(r1.rgb);
}
// Fix NaN
r1.rgb = renodx::color::bt709::clamp::BT709(r1.rgb);
r0.rgb = renodx::color::bt709::clamp::BT709(r0.rgb);

r0.rgb *= injectedData.toneMapUINits / 203.f; // Value found so it matches tonemapUINits
r1.rgb = displayTonemap(r1.rgb);

r1.rgb = renodx::color::bt2020::from::BT709(r1.rgb);
r1.rgb = renodx::color::pq::Encode(r1.rgb, injectedData.toneMapGameNits);
Expand Down
9 changes: 6 additions & 3 deletions src/games/dbzspark/lutbuilder_0x31FE4421.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ void main(
r5.rgb = max(0, r5.rgb);

float3 lut_input_color = r5.rgb;
float3 lut_input_color_sdr = saturate(renoDRTSmoothClamp(lut_input_color));

float3 post_lut_color;

if (false) {
Expand All @@ -522,9 +524,10 @@ void main(
renodx::lut::config::type::SRGB,
16.f);

post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color);
post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color_sdr);
} else {
r5.rgb = saturate(r5.rgb); // Better picture
r5.rgb = lut_input_color_sdr;
// r5.rgb = saturate(r5.rgb); // Better picture
r1.xyz = float3(12.9200001, 12.9200001, 12.9200001) * r5.xyz;
r6.xyz = cmp(r5.xyz >= float3(0.00313066994, 0.00313066994, 0.00313066994));
r5.xyz = log2(r5.xyz);
Expand Down Expand Up @@ -557,7 +560,7 @@ void main(
post_lut_color = r1.rgb;
} // CustomEdit
r1.rgb = post_lut_color;
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, saturate(lut_input_color), r1.rgb, 1.f);
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, lut_input_color_sdr, r1.rgb, 1.f);

r5.xyz = r1.xyz * r1.xyz;
r1.xyz = cb0[39].yyy * r1.xyz;
Expand Down
8 changes: 5 additions & 3 deletions src/games/dbzspark/lutbuilder_0x36E3A438.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ void main(
r5.rgb = max(0, r5.rgb);

float3 lut_input_color = r5.rgb;
float3 lut_input_color_sdr = saturate(renoDRTSmoothClamp(lut_input_color));
float3 post_lut_color;

// It's sampling two LUTs and lerping, so we just use vanilla
Expand All @@ -414,9 +415,10 @@ void main(
renodx::lut::config::type::SRGB,
16.f);

post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color);
post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color_sdr);
} else {
r5.rgb = saturate(r5.rgb); // Better picture
r5.rgb = lut_input_color_sdr;
// r5.rgb = saturate(r5.rgb); // Better picture
r1.xyz = float3(12.9200001, 12.9200001, 12.9200001) * r5.xyz;
r6.xyz = cmp(r5.xyz >= float3(0.00313066994, 0.00313066994, 0.00313066994));
r5.xyz = log2(r5.xyz);
Expand Down Expand Up @@ -453,7 +455,7 @@ void main(
post_lut_color = r1.rgb;
} // CustomEdit
r1.rgb = post_lut_color;
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, saturate(lut_input_color), r1.rgb, 1.f);
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, lut_input_color_sdr, r1.rgb, 1.f);

r5.xyz = r1.xyz * r1.xyz;
r1.xyz = cb0[39].yyy * r1.xyz;
Expand Down
9 changes: 6 additions & 3 deletions src/games/dbzspark/lutbuilder_0xC2A711CC.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ void main(
r5.rgb = max(0, r5.rgb);

float3 lut_input_color = r5.rgb;
float3 lut_input_color_sdr = saturate(renoDRTSmoothClamp(r5.rgb));

float3 post_lut_color;

// Vanilla sampler looks better
Expand All @@ -409,9 +411,10 @@ void main(
renodx::lut::config::type::SRGB,
16.f);

post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color);
post_lut_color = renodx::lut::Sample(t0, lut_config, lut_input_color_sdr);
} else {
r5.rgb = saturate(r5.rgb); // Better picture
r5.rgb = lut_input_color_sdr;
// r5.rgb = saturate(r5.rgb); // Better picture
r1.xyz = float3(12.9200001, 12.9200001, 12.9200001) * r5.xyz;
r6.xyz = cmp(r5.xyz >= float3(0.00313066994, 0.00313066994, 0.00313066994));
r5.xyz = log2(r5.xyz);
Expand Down Expand Up @@ -443,7 +446,7 @@ void main(
post_lut_color = r1.rgb;
} // CustomEdit
r1.rgb = post_lut_color;
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, saturate(lut_input_color), r1.rgb, 1.f);
r1.rgb = renodx::tonemap::UpgradeToneMap(lut_input_color, lut_input_color_sdr, r1.rgb, 1.f);

r5.xyz = r1.xyz * r1.xyz;
r1.xyz = cb0[39].yyy * r1.xyz;
Expand Down
8 changes: 3 additions & 5 deletions src/games/dbzspark/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,24 @@ struct ShaderInjectData {
float colorGradeShadows;
float colorGradeContrast;
float colorGradeSaturation;
float colorGradeBlowout;
};

#ifndef __cplusplus
cbuffer injectedBuffer : register(b0, space50) {
ShaderInjectData injectedData : packoffset(c0);
}
/* static const ShaderInjectData injectedData = {
0.f, // toneMapType
3.f, // toneMapType
1.f, // toneMapDisplay
300.f, // toneMapPeakNits
60.f, // toneMapGameNits
500.f, // toneMapPeakNits
150.f, // toneMapGameNits
120.f, // toneMapUINits
1.f, // toneMapGammaCorrection
1.f, // colorGradeExposure
1.f, // colorGradeHighlights
1.f, // colorGradeShadows
1.f, // colorGradeContrast
1.f, // colorGradeSaturation
0.f, // colorGradeBlowout
}; */
#endif

Expand Down
47 changes: 39 additions & 8 deletions src/games/dbzspark/tonemapper.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ renodx::tonemap::Config getCommonConfig() {
float renoDRTContrast = 1.f;
float renoDRTFlare = 0.f;
float renoDRTShadows = 1.f;
float renoDRTDechroma = injectedData.colorGradeBlowout;
float renoDRTSaturation = 1.f;
float renoDRTHighlights = 1.f;

Expand All @@ -22,24 +21,56 @@ renodx::tonemap::Config getCommonConfig() {
config.peak_nits = injectedData.toneMapPeakNits;
config.game_nits = injectedData.toneMapGameNits;
config.gamma_correction = injectedData.toneMapGammaCorrection;
config.saturation = renoDRTSaturation;
config.highlights = renoDRTHighlights;
config.shadows = renoDRTShadows;
config.contrast = renoDRTContrast;

config.reno_drt_highlights = renoDRTHighlights;
config.reno_drt_shadows = renoDRTShadows;
config.reno_drt_contrast = renoDRTContrast;
config.reno_drt_saturation = renoDRTSaturation;
config.reno_drt_dechroma = renoDRTDechroma;
config.mid_gray_value = vanillaMidGray;
config.mid_gray_nits = vanillaMidGray * 100.f;
config.reno_drt_flare = renoDRTFlare;

return config;
}

/// Applies a customized version of RenoDRT tonemapper that tonemaps down to 1.0.
/// This function is used to compress HDR color to SDR range for use alongside `UpgradeToneMap`.
///
/// @param lutInputColor The color input that needs to be tonemapped.
/// @return The tonemapped color compressed to the SDR range, ensuring that it can be applied to SDR color grading with `UpgradeToneMap`.
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.f;
renodrt_config.saturation = 1.04f;
renodrt_config.dechroma = 0.f;
renodrt_config.flare = 0.f;
// renodrt_config.hue_correction_strength = 1.f;
renodrt_config.hue_correction_source = renodx::tonemap::uncharted2::BT709(untonemapped);
renodrt_config.hue_correction_method = renodx::tonemap::renodrt::config::hue_correction_method::OKLAB;
renodrt_config.tone_map_method = renodx::tonemap::renodrt::config::tone_map_method::DANIELE;
renodrt_config.hue_correction_type = renodx::tonemap::renodrt::config::hue_correction_type::INPUT;
renodrt_config.per_channel = false;

float3 renoDRTColor = renodx::tonemap::renodrt::BT709(untonemapped, renodrt_config);
renoDRTColor = lerp(untonemapped, renoDRTColor, saturate(renodx::color::y::from::BT709(untonemapped) / renodrt_config.mid_gray_value));
// renoDRTColor = renodx::tonemap::UpgradeToneMap(untonemapped, saturate(untonemapped), renoDRTColor, saturate(renodx::color::y::from::BT709(untonemapped) / renodrt_config.mid_gray_value));
// renoDRTColor = renodx::tonemap::UpgradeToneMap(untonemapped, saturate(untonemapped), renoDRTColor, 1.f);

return renoDRTColor;
}

// Here so we have a central function once we figure out a better way
float3 clampForSRGB(float3 color) {
// clamp so colors don't go NaN, and didn't want to clamp to 1
// value derived from testing main menu
return min(float3(1.f, 1.f, 1.f) * 10.f, color);
// return min(float3(1.f, 1.f, 1.f) * 10.f, color);
return renoDRTSmoothClamp(color);
}

// input is always PQ, even for vanilla+
Expand Down Expand Up @@ -73,7 +104,7 @@ float3 upgradeSRGBtoPQ(float3 tonemapped, float3 post_srgb, float customDecode =
post = renodx::color::srgb::Decode(post);

output = renodx::tonemap::UpgradeToneMap(hdr, saturate(hdr), post, strength);
output = renodx::color::bt2020::from::BT709(output); // Maybe change this to post
output = renodx::color::bt2020::from::BT709(output);
output = renodx::color::pq::Encode(output, customDecode != 0.f ? customDecode : injectedData.toneMapGameNits);
}

Expand Down

0 comments on commit e75547d

Please sign in to comment.