tex2D method expects sampler2D, not texture2D, as first argument

Hi guys. Trying to feed a texture2D into a .hlsl pixel shader. In my pixel shader I have a tex2D method which expects a sampler2D, NOT texture2D, as the first argument.

How do I convert a texture2D into a sampler2D?

My .hlsl pixel shader is being imported as a Custom Function in ShaderGraph.

The SRP’s are all written with macros that assume you’re using DX11 on desktop, and DX11 style HLSL has some significant differences to the DX9 HLSL that most of Unity’s built in shader code and tutorials you’ll find are written using.

The short version is you can’t convert a Texture2D to a sampler2D, not directly, because they’re different things. (Also, remember my comment from my other response to you … case matters!)

A DX9 style sampler2D _myTexture; is a combined texture and sampler state, and you use tex2D(_myTexture, uv) to sample it.

DX11 style Texture2D _myTexture; usually has a paired SamplerState sampler_myTexture; and you use _myTexture.Sample(sampler_myTexture, uv); to sample it. For the SRPs, both the declaration and the sampling are wrapped up in macros that may actually swap back to DX9 style code, like for OpenGLES devices that don’t support using a separate texture and sampler state. You’ll likely want to use those macros, like most of Unity’s own shaders for the SRPs do.

Here’s an example of the macros you should use when writing for Shader Graph Custom Functions, with the familiar D3D9 style syntax.

And here’s what those macros do when you’re using DX11, and why you’re probably seeing the errors you are about tex2D not taking a Texture2D.

Thanks. Slowly making sense. The DX9 vs DX11 requirements were at the crux of my confusion.

Finally have something working.

My assumption now is that if I am feeding a texture to a Custom Function node the tex2D method should NOT be used for sampling because Shadergraph assumes DX11 style HLSL.