Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Difference between tex2D and SAMPLE_TEXTURE2D (Post Processing effect)

Discussion in 'Shaders' started by intouchmatt, Mar 27, 2019.

  1. intouchmatt

    intouchmatt

    Joined:
    Dec 4, 2018
    Posts:
    16
    What is the difference between tex2D() and SAMPLE_TEXTURE2D? I am trying to create a four corner pin post processing effect, and my lines come out curved with SAMPLE_TEXTURE2D, but are good and straight when I use tex2D() in an ImageEffect.

    Motivation for using a custum post processing effect instead of an ImageEffect is because ImageEffects dont seem to work when using HDRP (OnRenderImage() is never called), and I want to use ShaderGraph for other parts of the app. I should also mention my shader knowledge is pretty limited so feel free to point out any newb mistakes

    Here is my simple fragment shader

    Code (CSharp):
    1.  
    2.         float4 Frag(VaryingsDefault i) : SV_Target
    3.         {
    4.             float2 uv = i.texcoord;
    5.             uv.x *= 1 / (uv.y*_URx + 1);
    6.             return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv); //Compiles, but produces curved lines instead of the desired straight ones
    7.             //return tex2D(_MainTex, uv); //Doesnt compile here, but produces good straight lines
    8.         }
    9.  
    I built the basic Post Processing effect based on this article: https://github.com/Unity-Technologies/PostProcessing/wiki/Writing-Custom-Effects

    and the specifics of the fragment shader are from this post:

    https://answers.unity.com/questions/835540/4-corner-pin-distrotion-shader.html
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,979
    The difference is ... there isn’t one really.

    Older Unity shaders are almost uniformly written in D3D9 style HLSL, which uses:

    sampler2D _Tex;
    tex2D(_Tex, uv);

    All the new post processing stuff, and the SRPs are written with macros that generally use D3D11 style HLSL, which uses:

    texture2D _Tex;
    samplerState sampler_Tex;
    _Tex.Sample(sampler_Tex, uv);

    It just wraps those in macros so it can switch to other styles depending on the target rather than rely purely on cross compilation, as some features aren’t available on all platforms.
    https://github.com/Unity-Technologi...unity.render-pipelines.core/ShaderLibrary/API


    So, in short, whatever the problem is, it has nothing to do with the use of that macro vs tex2D as the behavior should be identical.
     
    chrismarch likes this.
  3. intouchmatt

    intouchmatt

    Joined:
    Dec 4, 2018
    Posts:
    16
    Hmm. The plot thickens. Are there any variables for sampling techniques. Something which would effect how it interpolates samples over the face?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,979
    There are, but none are going to be used unless you're explicity setting them on the interpolated value definitions, and even then they wouldn't have much effect during a blit() or similar full screen screen aligned geomerty.

    However the math you're using confuses me a bit. Specifically I don't think the + 1 is correct.
     
  5. Mutimir

    Mutimir

    Joined:
    Dec 6, 2018
    Posts:
    23
    My guess is that the + 1 is so that he dosnt get a 0 in the denominator. He is extending the ux.x but i dont see the purpose of this without some images.
     
  6. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    392
    Where did that ShaderLibrary API repo go? I can't find it. I'm trying to find the definition of
    SAMPLE_TEXTURE2D, and then hopefully something similar that wraps
    tex2Dlod.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,979
    chrismarch likes this.
unityunity