Search Unity

Disable TAA Jitter for Screen-space Object Shaders

Discussion in 'Shaders' started by jolix, Jan 9, 2020.

  1. jolix

    jolix

    Joined:
    May 22, 2016
    Posts:
    70
    When using TAA (temporal anti-aliasing) and doing a screen-space effect on a surface shader using this:

    Vertex:
    o.screenPos = ComputeScreenPos(o.vertex);

    Fragment:
    float2 screenPos = i.screenPos.xy/i.screenPos.w;

    I use the screenPos as UV to a texture applied to a surface:
    tex2D(_MainTex, screenPos);

    The texture clearly jitters with TAA, which is a desired behavior.

    But my question is, how do I disable TAA just for this one texture lookup?

    I tried offsetting the screenPos with the jitter vector but that doesn't seem to work:
    tex2D(_MainTex, screenPos-_Jitter);
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    The expectation for TAA is indeed that it's being jittered, but a screen space texture won't be getting jittered. The jittered projection matrix has no impact on the screen position.

    The jittered projection matrix that TAA uses will adjust the sub pixel position of each vertex. So anything that's relying on vertex UVs will be getting jittered as expected. The screen space UVs are based on the actual screen position, which is the same regardless of the vertex positions or projection being used. That's kind of the whole reason you use a screen space UV like this is that it isn't affected by any of that.

    The solution of offsetting the UVs by the jitter is indeed the correct option, but I suspect
    _Jitter
    isn't set to anything but
    float2(0,0)
    on your object. Indeed the Post Processing Stack's TAA code only sets that property explicitly on the temporal AA "blit" directly and not as a global shader property. You'll need to get the current frame's jitter value from the active TAA script and set it on your material or as a global property.

    Also, once you do have it set properly, you may need to be adding
    _Jitter;
    rather than subtracting.
     
    jolix likes this.
  3. jolix

    jolix

    Joined:
    May 22, 2016
    Posts:
    70
    Hey thank you very very much for your response!

    It leaves me a little confused as you say the screen space texture is not jittered, yet you propose a solution to dejitter it.
    It was my first thought as well, but I can clearly see jitters when texturing using the code from above (with and without the _Jitter param).

    The _Jitter vector is passed to the material and comes from the TAA post process layer ".jitter" parameter.
    I also copied the code to generate the jitter from the official github in case the jitter parameter you get from calling temporalAntiAliasing.jitter would be from a previous frame.

    But because you say this is the solution, I now know where to look, thanks!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    My assumption is the TAA is expecting the contents of the screen to be jittered and attempting to un-jitter it. So what you're seeing as the texture is being jittered is actually the TAA over correcting something that hasn't been jittered.

    So, if the texture is actually jittered, then the TAA should be working properly and the jittering won't be noticeable.

    This is all assuming that when you say "I can clearly see the jitters" you're looking at the image with the TAA enabled, and you're not seeing it when looking at the output in the frame debugger.

    I mean, really what you want is to not apply TAA to just that texture, but that's not actually an option.
     
    jolix likes this.
  5. jolix

    jolix

    Joined:
    May 22, 2016
    Posts:
    70
    Thanks!

    When I say "I can clearly see the jitters", I mean that with TAA enabled and using your average screen space texture, it's not noticable, because TAA blends the history.
    But it clearly jitters, because when I set the blend parameter to minimum, I can see it jittering.

    I don't want it to jitter, because I want pixel precise control over my texture (it's a blue noise texture). TAA kind of blurs the pixels a little bit.
    So far without luck, but if I ever find a solution, I'll definitely post it here!

    Thank you!!!
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    When using blue noise with TAA, usually you want to change the blue noise every frame so TAA can blur it away. Accurately preserving small details is not TAA’s strong point.