# Temporal AA sharpening

**URL:** <https://forums.unrealengine.com/t/temporal-aa-sharpening/98676>\
**Category:** Rendering\
**Tags:** unreal-engine\
**Created:** [August 17, 2017, 10:06pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676 "2017-08-17T22:06:25Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![John\_Alcatraz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@John\_Alcatraz](https://forums.unrealengine.com/u/John_Alcatraz)\
**Post date:** [August 18, 2017, 12:08pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/22 "2017-08-18T12:08:41Z")

</div>

> [@hallatore;751608](#):
>
> I think it should work. Just turn on r.ShaderDevelopmentMode=1 in Engine/Config/ConsoleVariables.ini so you see what it’s complaining about.
> 
> The code is pretty basic, so I don’t see what would have changed between 4.15 and now.

Its more than 200 lines of changes, so a lot of stuff can have changed there between 4.15 and 4.18.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 12:11pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/23 "2017-08-18T12:11:51Z")

</div>

> [@John Alcatraz;751609](#):
>
> Its more than 200 lines of changes, so a lot of stuff can have changed there between 4.15 and 4.18.

That’s true. But If it’s just a shader compiler error than it’s most likely easy to fix. Probably duplicate or unknown function declaration.  
If the editor just crashes on the other hand… 😛

---

<div class="post-metadata">

**Author:** ![Tom\_Looman](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tom_looman/32/1215549_2.png) [@Tom\_Looman](https://forums.unrealengine.com/u/Tom_Looman)\
**Post date:** [August 18, 2017, 2:33pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/24 "2017-08-18T14:33:22Z")

</div>

This looks very interesting! The bluriness of TXAA really ruins the feel of a scene. I hope this or a variation of it makes it into the engine.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 4:07pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/25 "2017-08-18T16:07:37Z")

</div>

> [@mittense;751584](#):
>
> I’m hoping to give it a go later today; I’ll report back with any feedback/screens.

I’ve squashed the commits so it’s easier to see what you need to copy etc.

---

<div class="post-metadata">

**Author:** ![trentpolack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/trentpolack/32/1173069_2.png) [@trentpolack](https://forums.unrealengine.com/u/trentpolack)\
**Post date:** [August 18, 2017, 4:09pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/26 "2017-08-18T16:09:42Z")

</div>

I may have stumbled onto a bug after my integration, but I’m trying to figure out if it was just a really bizarre random thing that’s occurring or a legit bug (deleted my DDC, so waiting on that to be sure).

I have a fairly non-standard setup, but it looked like it was suddenly obscuring my sky entirely (trueSKY)? Anyway, I’m working on verifying.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 4:36pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/27 "2017-08-18T16:36:57Z")

</div>

I have a question! 🆒  
Which one is default TAA and screen percentage set to 150, and which is Dynamic with only screen percentage 100. A or B?

> **[Imgsli](https://imgsli.com/MDYyNQ)**

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 4:38pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/28 "2017-08-18T16:38:51Z")

</div>

> [@mittense;751706](#):
>
> I may have stumbled onto a bug after my integration, but I’m trying to figure out if it was just a really bizarre random thing that’s occurring or a legit bug (deleted my DDC, so waiting on that to be sure).
> 
> I have a fairly non-standard setup, but it looked like it was suddenly obscuring my sky entirely (trueSKY)? Anyway, I’m working on verifying.

Please send me some screenshots if you can.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 4:59pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/29 "2017-08-18T16:59:08Z")

</div>

> [@mittense;751706](#):
>
> I may have stumbled onto a bug after my integration, but I’m trying to figure out if it was just a really bizarre random thing that’s occurring or a legit bug (deleted my DDC, so waiting on that to be sure).

This code should ignore the sky. Let me know if it helps, or if it was just a random thing.

```auto

void MainDynamicSharpenTemporalAAPS(
	in noperspective float2 UV: TEXCOORD0,
	float4 SvPosition : SV_POSITION,
	out float4 OutColor : SV_Target0 )
{
	float2 FullResUV = SvPosition.xy * PostprocessInput0Size.zw;
	float4 CenterColor = PostprocessInput0.SampleLevel( PostprocessInput0Sampler, UV, 0 );
	half CenterDepth = CalcSceneDepth(FullResUV);
	half DepthLimit = saturate(1 - CenterDepth / 1000000);

	float4 DepthQuad = GatherSceneDepth(UV, PostprocessInput1Size.zw);
	half D1 = abs(CenterDepth - DepthQuad.x);
	half D2 = abs(CenterDepth - DepthQuad.y);
	half D3 = abs(CenterDepth - DepthQuad.z);
	half D4 = abs(CenterDepth - DepthQuad.w);
	half NeighborDepth = max(max(D1, D2), max(D3, D4));

	half3 C1 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, 0)).rgb;
	half3 C2 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2( 1, 0)).rgb;
	half3 C3 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, -1)).rgb;
	half3 C4 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, 1)).rgb;

	half L1 = LuminanceDifference(C1.rgb, CenterColor.rgb);
	half L2 = LuminanceDifference(C2.rgb, CenterColor.rgb);
	half L3 = LuminanceDifference(C3.rgb, CenterColor.rgb);
	half L4 = LuminanceDifference(C4.rgb, CenterColor.rgb);
	half NeighborDifference = max(max(L1, L2), max(L3, L4));
	
	// Fade sharpen based on depth difference in world units. (0...5)
	half DepthFactor = saturate(1 - NeighborDepth / 5.0);

	// Fade sharpen based on luminance difference in percentage. (0...5)
	half LuminanceDifferenceFactor = saturate(1 - NeighborDifference * 20.0);

	half LerpFactor = -DynamicSharpen * DepthFactor * LuminanceDifferenceFactor;
	half3 DeltaColor = (C1 + C2 + C3 + C4) - CenterColor.rgb * 4;
	CenterColor.rgb += -DeltaColor * DynamicSharpen * DepthFactor * LuminanceDifferenceFactor * DepthLimit;
	OutColor = CenterColor;
}

```

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 6:25pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/30 "2017-08-18T18:25:16Z")

</div>

Here is an image visualizing the dynamic sharpening strength. It goes from white (no sharpen) to red (max sharpen).  
Comparison: [Imgsli](https://imgsli.com/MDYyOQ)

![36ba6d14ec3bd7ba4f9801d0cb0c32ef72f8e96e.jpeg](https://d1ap1mz92jnks1.cloudfront.net/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![Chosker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chosker/32/710746_2.png) [@Chosker](https://forums.unrealengine.com/u/Chosker)\
**Post date:** [August 18, 2017, 6:31pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/31 "2017-08-18T18:31:01Z")

</div>

> [@hallatore;751722](#):
>
> I have a question! 🆒  
> Which one is default TAA and screen percentage set to 150, and which is Dynamic with only screen percentage 100. A or B?
> 
> [Imgsli](https://imgsli.com/MDYyNQ)

I’d say A is screen percentage set to 150 😉

---

<div class="post-metadata">

**Author:** ![John\_Alcatraz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@John\_Alcatraz](https://forums.unrealengine.com/u/John_Alcatraz)\
**Post date:** [August 18, 2017, 6:43pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/32 "2017-08-18T18:43:02Z")

</div>

> [@hallatore;751722](#):
>
> I have a question! 🆒  
> Which one is default TAA and screen percentage set to 150, and which is Dynamic with only screen percentage 100. A or B?
> 
> [Imgsli](https://imgsli.com/MDYyNQ)

On the left the grass looks better, but on the right the rock in the background looks better.

---

<div class="post-metadata">

**Author:** ![trentpolack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/trentpolack/32/1173069_2.png) [@trentpolack](https://forums.unrealengine.com/u/trentpolack)\
**Post date:** [August 18, 2017, 6:45pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/33 "2017-08-18T18:45:45Z")

</div>

Boosh, here’s my scene with r.TemporalAADynamicSharpen set to 0.75. Tried to find a section with some more prominent lines (the grates on the gas tank); I dig it. It looks a fair bit better than my prior solution (some sharpening + a dedicated post pass pre-tonemapper that just sharpened based on luminosity), so yay.

Thanks for this. \<3

![f61c507fad7af75e16ac03cc4971c0db3954f106.jpeg](https://d1ap1mz92jnks1.cloudfront.net/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 6:49pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/34 "2017-08-18T18:49:55Z")

</div>

> [@mittense;751782](#):
>
> Thanks for this. \<3

Glad you like it! 🙂  
PS: Can’t see any image though.

Here is the latest code btw. Just some minor tweaks.  
Personally I found values between 0.2 and 0.4 to be closest to the reference. But it’s all about personal preference, and the code seems to scale pretty well to extreme values.

```auto

void MainDynamicSharpenTemporalAAPS(
	in noperspective float2 UV: TEXCOORD0,
	float4 SvPosition : SV_POSITION,
	out float4 OutColor : SV_Target0 )
{
	float4 CenterColor = PostprocessInput0.SampleLevel( PostprocessInput0Sampler, UV, 0 );

	if (DynamicSharpen <= 0)
	{
		OutColor = CenterColor;
		return;
	}

	float2 FullResUV = SvPosition.xy * PostprocessInput0Size.zw;
	half CenterDepth = CalcSceneDepth(FullResUV);

	float4 DepthQuad = GatherSceneDepth(UV, PostprocessInput1Size.zw);
	half D1 = abs(CenterDepth - DepthQuad.x);
	half D2 = abs(CenterDepth - DepthQuad.y);
	half D3 = abs(CenterDepth - DepthQuad.z);
	half D4 = abs(CenterDepth - DepthQuad.w);
	half NeighborDepth = max(max(D1, D2), max(D3, D4));

	half3 C1 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, 0)).rgb;
	half3 C2 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2( 1, 0)).rgb;
	half3 C3 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, -1)).rgb;
	half3 C4 = PostprocessInput0.SampleLevel(PostprocessInput0Sampler, UV, 0, int2(-1, 1)).rgb;

	half L1 = LuminanceDifference(C1.rgb, CenterColor.rgb);
	half L2 = LuminanceDifference(C2.rgb, CenterColor.rgb);
	half L3 = LuminanceDifference(C3.rgb, CenterColor.rgb);
	half L4 = LuminanceDifference(C4.rgb, CenterColor.rgb);
	half NeighborDifference = max(max(L1, L2), max(L3, L4));
	
	// Fade sharpen based on depth difference in world units. (0...15)
	half DepthFactor = saturate(1 - NeighborDepth / 15.0);

	// Fade sharpen based on luminance difference in percentage. (0...5)
	half LuminanceDifferenceFactor = saturate(1 - NeighborDifference * 20.0);

	// Ignore the sky since depth info will be wrong.
	half DepthLimit = saturate(1 - CenterDepth / 1000000);

	half LerpFactor = -DynamicSharpen * DepthFactor * LuminanceDifferenceFactor;
	half3 DeltaColor = (C1 + C2 + C3 + C4) - CenterColor.rgb * 4;	
	CenterColor.rgb += -DeltaColor * DynamicSharpen * DepthFactor * LuminanceDifferenceFactor * DepthLimit;
	OutColor = CenterColor;
}

```

---

<div class="post-metadata">

**Author:** ![trentpolack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/trentpolack/32/1173069_2.png) [@trentpolack](https://forums.unrealengine.com/u/trentpolack)\
**Post date:** [August 18, 2017, 6:59pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/35 "2017-08-18T18:59:13Z")

</div>

Fixed the image. 🙂

---

<div class="post-metadata">

**Author:** ![Zeblote](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeblote](https://forums.unrealengine.com/u/Zeblote)\
**Post date:** [August 18, 2017, 7:00pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/36 "2017-08-18T19:00:55Z")

</div>

That image is extremely noisy, is that caused by this change or something else?

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 7:02pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/37 "2017-08-18T19:02:23Z")

</div>

> [@mittense;751790](#):
>
> Fixed the image. 🙂

Nice! 🙂  
I see you like quite a bit of sharpen. I bet the tonemap.sharpen creates a few artifacts with the same strength.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 7:03pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/38 "2017-08-18T19:03:49Z")

</div>

> [@Zeblote;751791](#):
>
> That image is extremely noisy, is that caused by this change or something else?

0.75 is a huge value. I use 0.25 - 0.30 to get close to my reference image.  
0.25 - 0.30 is just to combat the slight blur from TAA, not make it sharper than original. But I bet some feel it looks nice even higher.

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 7:05pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/39 "2017-08-18T19:05:14Z")

</div>

Made a video visualizing the strength value from white to red. More red means sharper.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/b/f/ebf58e19a7ed1b85076c801d8ea59e617e1290b3.jpeg "Unreal Engine - Dynamic TAA sharpening visualized") ](https://www.youtube.com/watch?v=6aGasGgrvoI)

---

<div class="post-metadata">

**Author:** ![trentpolack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/trentpolack/32/1173069_2.png) [@trentpolack](https://forums.unrealengine.com/u/trentpolack)\
**Post date:** [August 18, 2017, 7:06pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/40 "2017-08-18T19:06:26Z")

</div>

> [@Zeblote;751791](#):
>
> That image is extremely noisy, is that caused by this change or something else?

Noisy is largely due to the noise post settings. It’s still being tuned off and on.

And, yeah, just realized I have the tonemap sharpening still set as well (and my TAA samples were too high). Also just reduced the r.TemporalAADynamicSharpen to 0.3. Reduced the tonemap sharpening and the TAA sample count and this is that:

![b69704f683fb490fe73012e2cf827ec4b1b0d894.jpeg](https://d1ap1mz92jnks1.cloudfront.net/images/transparent.png)

(I have a pretty intense post stack and custom changes/integrations, so getting the whole composition right takes… iteration)

---

<div class="post-metadata">

**Author:** ![hallatore](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hallatore/32/1109226_2.png) [@hallatore](https://forums.unrealengine.com/u/hallatore)\
**Post date:** [August 18, 2017, 7:46pm UTC](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676/42 "2017-08-18T19:46:54Z")

</div>

> [@dvd.kh;751805](#):
>
> thanks for this method …
> 
> but this update code does not exists on the latest commit [22 hours ago!]  
> can you update commit please?

Was still looking at it. Pushed it now. 🙂

[https://github.com/hallatore/UnrealEngine/tree/TAA\_Sharpen](https://github.com/hallatore/UnrealEngine/tree/TAA_Sharpen)

[Previous page](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676.md?page=1)

[Next page](https://forums.unrealengine.com/t/temporal-aa-sharpening/98676.md?page=3)
