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+
12521289float4 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