Skip to content

Commit 80936e7

Browse files
committed
Deus Ex HR: Improve mod performance and stability
c++ code is fully vibe coded and untested (it wasn't my code)
1 parent 6e4f26e commit 80936e7

4 files changed

Lines changed: 335 additions & 179 deletions

File tree

‎Shaders/Deus Ex Human Revolution Director's Cut/Luma_SMAA_Linearize.hlsl‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎Shaders/Deus Ex Human Revolution Director's Cut/Luma_SMAA_impl.hlsl‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ cbuffer SceneBuffer : register(b2)
4444
#define SMAA_RT_METRICS float4(1.0 / ScreenResolution, ScreenResolution) // Should use ScreenExtents here?
4545
#define SMAA_PRESET_ULTRA
4646
#define SMAA_PREDICATION 1
47+
// The scene is gamma encoded (in an FP16 texture, so it has no sRGB views), the neighborhood blending decodes it to linear,
48+
// the same way as the FXAA and MLAA shaders that SMAA replaces, and leaves it linear, as we tonemap and encode it after anyway
49+
#define SMAA_CUSTOM_BLENDING_DECODE_ENCODE 1
50+
#define SMAA_BLENDING_DECODE(color) gamma_to_linear(color, GCT_MIRROR)
51+
#define SMAA_BLENDING_ENCODE(color) (color)
4752
#define SMAA_CUSTOM_SL
4853
SamplerState LinearSampler : register(s0);
4954
SamplerState PointSampler : register(s1);
@@ -122,7 +127,7 @@ float4 smaa_neighborhood_blending_ps(float4 position : SV_Position, float2 texco
122127
{
123128
// tex0 = colorTex
124129
// tex1 = blendTex
125-
float4 color = SMAANeighborhoodBlendingPS(texcoord, offset, tex0, tex1);
130+
float4 color = SMAANeighborhoodBlendingPS(texcoord, offset, tex0, tex1); // Inputs gamma space, outputs linear space color
126131

127132
float2 uv = position.xy * ScreenExtents.zw + ScreenExtents.xy;
128133
bool forceSDR = ShouldForceSDR(uv);
@@ -157,14 +162,10 @@ float4 smaa_neighborhood_blending_ps(float4 position : SV_Position, float2 texco
157162
color.rgb *= LumaSettings.GamePaperWhiteNits / LumaSettings.UIPaperWhiteNits;
158163
ColorGradingLUTTransferFunctionInOutCorrected(color.rgb, GAMMA_CORRECTION_TYPE, VANILLA_ENCODING_TYPE, true);
159164
#endif // UI_DRAW_TYPE == 2
160-
161-
color.rgb = linear_to_gamma(color.rgb, GCT_MIRROR);
162-
}
163-
else
164-
{
165-
color.rgb = linear_to_gamma(color.rgb, GCT_MIRROR);
166165
}
167166

167+
color.rgb = linear_to_gamma(color.rgb, GCT_MIRROR);
168+
168169
return color;
169170
}
170171

‎Shaders/Includes/SMAA.hlsl‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,33 @@
511511
#define SMAA_DECODE_VELOCITY(sample) sample.rg
512512
#endif
513513

514+
/**
515+
* Luma: set SMAA_CUSTOM_BLENDING_DECODE_ENCODE to 1 when the 'colorTex' of
516+
* SMAANeighborhoodBlendingPS is encoded (e.g. gamma) and has no sRGB view to
517+
* decode it for free (e.g. FP16 textures). Each texel it mixes is then decoded
518+
* with SMAA_BLENDING_DECODE, so the blending happens in linear space, as
519+
* intended, and the result is encoded with SMAA_BLENDING_ENCODE (e.g. back to
520+
* the input encoding, or left linear, by defining it as "(color)", if the
521+
* caller post processes it in linear anyway).
522+
* Set it to 2 instead when SMAA_BLENDING_ENCODE is the exact inverse of
523+
* SMAA_BLENDING_DECODE, so the pixels that aren't blended are returned as
524+
* they are, skipping their decode and encode. The two bilinear taps are
525+
* replaced by point loads mixed by hand, which is equivalent, given that they
526+
* only ever interpolate between the current pixel and one neighbor along the
527+
* same axis. Requires 'colorTex' to support "Load()" (HLSL 4+).
528+
*/
529+
#ifndef SMAA_CUSTOM_BLENDING_DECODE_ENCODE
530+
#define SMAA_CUSTOM_BLENDING_DECODE_ENCODE 0
531+
#endif
532+
533+
#ifndef SMAA_BLENDING_DECODE
534+
#define SMAA_BLENDING_DECODE(color) (sign(color) * pow(abs(color), 2.2))
535+
#endif
536+
537+
#ifndef SMAA_BLENDING_ENCODE
538+
#define SMAA_BLENDING_ENCODE(color) (sign(color) * pow(abs(color), 1.0 / 2.2))
539+
#endif
540+
514541
//-----------------------------------------------------------------------------
515542
// Non-Configurable Defines
516543

@@ -1249,6 +1276,16 @@ float4 SMAABlendingWeightCalculationPS(float2 texcoord,
12491276
//-----------------------------------------------------------------------------
12501277
// Neighborhood Blending Pixel Shader (Third Pass)
12511278

1279+
#if SMAA_CUSTOM_BLENDING_DECODE_ENCODE
1280+
// Luma: a texel of the encoded 'colorTex', decoded to linear (clamped to the texture edges, like a clamp sampler would)
1281+
float4 SMAALoadDecoded(SMAATexture2D(colorTex), int2 pixel) {
1282+
pixel = clamp(pixel, int2(0, 0), int2(SMAA_RT_METRICS.zw) - 1);
1283+
float4 color = colorTex.Load(int3(pixel, 0));
1284+
color.rgb = SMAA_BLENDING_DECODE(color.rgb);
1285+
return color;
1286+
}
1287+
#endif
1288+
12521289
float4 SMAANeighborhoodBlendingPS(float2 texcoord,
12531290
float4 offset,
12541291
SMAATexture2D(colorTex),
@@ -1267,6 +1304,12 @@ float4 SMAANeighborhoodBlendingPS(float2 texcoord,
12671304
SMAA_BRANCH
12681305
if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
12691306
float4 color = SMAASampleLevelZero(colorTex, texcoord);
1307+
#if SMAA_CUSTOM_BLENDING_DECODE_ENCODE == 1
1308+
// Luma: nothing is mixed here, but the output needs the same encoding as the blended pixels
1309+
// (e.g. if SMAA_BLENDING_ENCODE is left as linear, this compiles to the decode alone).
1310+
// In mode 2 the encode is the inverse of the decode, so the texel already has the output encoding.
1311+
color.rgb = SMAA_BLENDING_ENCODE(SMAA_BLENDING_DECODE(color.rgb));
1312+
#endif
12701313

12711314
#if SMAA_REPROJECTION
12721315
float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord));
@@ -1289,10 +1332,21 @@ float4 SMAANeighborhoodBlendingPS(float2 texcoord,
12891332
// Calculate the texture coordinates:
12901333
float4 blendingCoord = mad(blendingOffset, float4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texcoord.xyxy);
12911334

1335+
#if SMAA_CUSTOM_BLENDING_DECODE_ENCODE
1336+
// Luma: mix the current pixel with the chosen neighbors by hand, in linear space, then encode the result back.
1337+
// "blendingCoord.xy" moves towards the positive neighbor along the chosen axis, "blendingCoord.zw" towards the negative one.
1338+
int2 pixel = int2(texcoord * SMAA_RT_METRICS.zw);
1339+
int2 axis = h ? int2(1, 0) : int2(0, 1);
1340+
float4 centerColor = SMAALoadDecoded(colorTex, pixel);
1341+
float4 color = blendingWeight.x * lerp(centerColor, SMAALoadDecoded(colorTex, pixel + axis), blendingOffset.x + blendingOffset.y);
1342+
color += blendingWeight.y * lerp(centerColor, SMAALoadDecoded(colorTex, pixel - axis), blendingOffset.z + blendingOffset.w);
1343+
color.rgb = SMAA_BLENDING_ENCODE(color.rgb);
1344+
#else
12921345
// We exploit bilinear filtering to mix current pixel with the chosen
12931346
// neighbor:
12941347
float4 color = blendingWeight.x * SMAASampleLevelZero(colorTex, blendingCoord.xy);
12951348
color += blendingWeight.y * SMAASampleLevelZero(colorTex, blendingCoord.zw);
1349+
#endif
12961350

12971351
#if SMAA_REPROJECTION
12981352
// Antialias velocity for proper reprojection in a later stage:

0 commit comments

Comments
 (0)