Difference between tex2D and SAMPLE_TEXTURE2D (Post Processing effect)

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

        float4 Frag(VaryingsDefault i) : SV_Target
        {
            float2 uv = i.texcoord;
            uv.x *= 1 / (uv.y*_URx + 1);
            return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv); //Compiles, but produces curved lines instead of the desired straight ones
            //return tex2D(_MainTex, uv); //Doesnt compile here, but produces good straight lines
        }

I built the basic Post Processing effect based on this article: Writing Custom Effects · Unity-Technologies/PostProcessing Wiki · GitHub

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

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-Technologies/ScriptableRenderPipeline/tree/master/com.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.

4 Likes

Hmm. The plot thickens. Are there any variables for sampling techniques. Something which would effect how it interpolates samples over the face?

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.

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.

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.

Yeah, still not really sure why they killed the original repo. It’s here now:
https://github.com/Unity-Technologies/Graphics/blob/master/com.unity.render-pipelines.core/ShaderLibrary/API/D3D11.hlsl

3 Likes

Maybe you know why they killed this one?

Nope, but now it’s here:

2 Likes