Skip to content

Commit

Permalink
refactor!(shaders): add pq::EncodeSafe and pq:DecodeSafe
Browse files Browse the repository at this point in the history
old pq::Decode was safe but was never noted.
  • Loading branch information
clshortfuse committed Jan 4, 2025
1 parent 9d1d6eb commit 13f8311
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/games/1000xresist/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ float3 FinalizeOutput(float3 color) {
}

float3 lutShaper(float3 color, bool builder = false) {
color = builder ? renodx::color::pq::Decode(color, 100.f)
color = builder ? renodx::color::pq::DecodeSafe(color, 100.f)
: renodx::color::pq::Encode(color, 100.f);

return color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void main(

float3 lut_input = renodx::color::pq::Encode(untonemapped, 100.f);
float3 sampled = renodx::lut::Sample(t3, s3_s, lut_input);
float3 post_lut = renodx::color::pq::Decode(sampled, 100.f);
float3 post_lut = renodx::color::pq::DecodeSafe(sampled, 100.f);
r1.xyz = post_lut;

o0.w = saturate(dot(r1.xyz, float3(0.298999995,0.587000012,0.114)));
Expand Down
4 changes: 2 additions & 2 deletions src/games/dbzspark/final00_0x4156562D.ps_5_1.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main(
r0.xyz = cb0[7].www * r1.xyz;

r1.xyz = t1.Sample(s1_s, v0.xy).xyz; // Game in PQ
r1.rgb = renodx::color::pq::Decode(r1.rgb, injectedData.toneMapGameNits);
r1.rgb = renodx::color::pq::DecodeSafe(r1.rgb, injectedData.toneMapGameNits);

r1.rgb = renodx::color::bt709::from::BT2020(r1.rgb);
r1.rgb = renodx::color::grade::UserColorGrading(
Expand Down Expand Up @@ -76,7 +76,7 @@ void main(
r1.rgb = renodx::color::bt2020::from::BT709(r1.rgb);
r1.rgb = renodx::color::pq::Encode(r1.rgb, injectedData.toneMapGameNits);

r1.rgb = renodx::color::pq::Decode(r1.rgb, 1.f); // We need it to merge with UI
r1.rgb = renodx::color::pq::DecodeSafe(r1.rgb, 1.f); // We need it to merge with UI

/* // pow(in_color, 1.f / M2)
r1.xyz = log2(r1.xyz);
Expand Down
4 changes: 2 additions & 2 deletions src/games/dbzspark/tonemapper.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ float3 pqTosRGB(float3 input, float customDecode = 0.f, bool clamp = false) {
if (injectedData.toneMapType == 1.f) {
output = input;
} else {
output = renodx::color::pq::Decode(input, customDecode != 0.f ? customDecode : injectedData.toneMapGameNits);
output = renodx::color::pq::DecodeSafe(input, customDecode != 0.f ? customDecode : injectedData.toneMapGameNits);
if (clamp) {
output = clampForSRGB(output);
}
Expand All @@ -95,7 +95,7 @@ float3 upgradeSRGBtoPQ(float3 tonemapped, float3 post_srgb, float customDecode =
if (injectedData.toneMapType == 1.f) {
output = tonemapped;
} else {
hdr = renodx::color::pq::Decode(tonemapped, customDecode != 0.f ? customDecode : injectedData.toneMapGameNits);
hdr = renodx::color::pq::DecodeSafe(tonemapped, customDecode != 0.f ? customDecode : injectedData.toneMapGameNits);
hdr = renodx::color::bt709::from::BT2020(hdr);

post = post_srgb;
Expand Down
2 changes: 1 addition & 1 deletion src/games/honkai-starrail/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ float3 FinalizeOutput(float3 color) {
}

float3 lutShaper(float3 color, bool builder = false) {
color = builder ? renodx::color::pq::Decode(color, 100.f)
color = builder ? renodx::color::pq::DecodeSafe(color, 100.f)
: renodx::color::pq::Encode(color, 100.f);

return color;
Expand Down
2 changes: 1 addition & 1 deletion src/games/outerwilds/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ float3 FinalizeOutput(float3 color) {
}

float3 lutShaper(float3 color, bool builder = false) {
color = builder ? renodx::color::pq::Decode(color, 100.f)
color = builder ? renodx::color::pq::DecodeSafe(color, 100.f)
: renodx::color::pq::Encode(color, 100.f);

return color;
Expand Down
2 changes: 1 addition & 1 deletion src/games/seaofstars/lutbuilder_new_0x77850945.ps_4_0.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main(float4 v0: SV_POSITION0, float2 v1: TEXCOORD0, out float4 o0: SV_Targe
r0.w = -r1.x + r0.y;

if (injectedData.processingInternalSampling == 1.f) {
r0.xyz = renodx::color::pq::Decode(r0.xzw, 100.f);
r0.xyz = renodx::color::pq::DecodeSafe(r0.xzw, 100.f);
} else {
r0.xyz = r0.xzw * cb0[129].www;
r0.xyz += float3(-0.386036009, -0.386036009, -0.386036009);
Expand Down
2 changes: 1 addition & 1 deletion src/games/stalker2/final00_0xA7EFB8C2.ps_6_6.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ float4 main(
// Scene will always be PQ sRGB bt709
float4 _59 = SceneTexture.Sample(SceneSampler, float2(_7, _8));
float3 scene = _59.rgb;
scene = renodx::color::pq::Decode(scene.rgb, injectedData.toneMapGameNits);
scene = renodx::color::pq::DecodeSafe(scene.rgb, injectedData.toneMapGameNits);
uiTexture.rgb = correctGamma(uiTexture.rgb);

// Multiple by games because we're encoding PQ later
Expand Down
2 changes: 1 addition & 1 deletion src/games/stalker2/tonemapper.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ float3 decodedTosRGB(float3 input_linear) {
float3 pqToDecoded(float3 input_pq) {
float3 output = input_pq;
if (injectedData.toneMapType > 1.f) {
output = renodx::color::pq::Decode(input_pq, injectedData.toneMapGameNits);
output = renodx::color::pq::DecodeSafe(input_pq, injectedData.toneMapGameNits);
}

return output;
Expand Down
20 changes: 17 additions & 3 deletions src/shaders/color.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,21 @@ float3 Encode(float3 color, float scaling = 10000.f) {

float3 Decode(float3 in_color, float scaling = 10000.f) {
float3 e_m12 = pow(in_color, 1.f / M2);
float3 out_color = pow(max(e_m12 - C1, 0) / (C2 - C3 * e_m12), 1.f / M1);
float3 out_color = pow(e_m12 - C1 / (C2 - C3 * e_m12), 1.f / M1);
return out_color * (10000.f / scaling);
}

float3 EncodeSafe(float3 color, float scaling = 10000.f) {
color *= (scaling / 10000.f);
color = max(0, color);
float3 y_m1 = pow(color, M1);
return pow((C1 + C2 * y_m1) / (1.f + C3 * y_m1), M2);
}

float3 DecodeSafe(float3 in_color, float scaling = 10000.f) {
in_color = max(0, in_color);
float3 e_m12 = pow(in_color, 1.f / M2);
float3 out_color = pow((e_m12 - C1) / (C2 - C3 * e_m12), 1.f / M1);
return out_color * (10000.f / scaling);
}

Expand Down Expand Up @@ -584,7 +598,7 @@ namespace bt2020 {
namespace from {
/// @deprecated - Use pq::Decode
float3 PQ(float3 pq_color, float scaling = 10000.f) {
return pq::Decode(pq_color, scaling);
return pq::DecodeSafe(pq_color, scaling);
}
} // namespace from
} // namespace bt2020
Expand Down Expand Up @@ -815,7 +829,7 @@ float3 ICtCp(float3 col) {
col = mul(ictcp_to_lms, col);

// 1.0f = 100 nits, 100.0f = 10k nits
col = pq::Decode(max(0, col), 100.f);
col = pq::DecodeSafe(col, 100.f);
return mul(ICTCP_LMS_TO_BT709_MAT, col);
}

Expand Down

0 comments on commit 13f8311

Please sign in to comment.