tex2Dfetch

How do you use this?

sampler2D _MainTex;
tex2Dfetch(_MainTex, int2(0, 0));

error:
Shader error in ‘Custom/MyShader’: ‘tex2Dfetch’ : no matching overloaded function found at line 52 (on opengl)
Compiling Vertex program

So… no matching overload, or no function at all?

http://http.developer.nvidia.com/Cg/tex2Dfetch.html

Looks like it takes an int4 or float4, not an int2.

Already tried it.

Seems like it’s not supported then, in Unity’s compiler. What are you trying to do exactly? Get point filtering on a bilinear filtered texture?

If you have a 256x256 texture you can do:-

tex2Dlod(_MainTex, float4((uv+.5) / 256.0, 0.0, 0.0));
Which will find the centre of the texel at the location specified by uv (in whole number pixel values).

It uses tex 2d LOD to make sure you’re getting the highest level mip map. It was added a few versions back, along with tex2Dgrad

tex2Dfetch is a Cg only thing… Unity actually does not use the Cg shader language anymore. It has ceased development and has been deprecated since 2012. It does not support SM4.0+ constructs, so for those it’s not even possible to use Cg. Instead, Unity compiles the shaders as HLSL - back in the days almost identical language to Cg. So, if you want a HLSL equivalent of tex2Dfetch, use this: Load (DirectX HLSL Texture Object) - Win32 apps | Microsoft Learn
Like so:

Texture2D _MainTex;
_MainTex.Load(int3(0, 0, 0));

Shader error in ‘Custom/MyShader’: ‘’ : methods are not supported; ‘Load’ : no matching overloaded function found

It would be nice to have an answer to this, but for now, I’ve just switched texture filtering to Point and gone with this:

float4 data = tex2Dlod(_MainTex, float4(_MainTexPixelWidth * 0.5 + i * _MainTexPixelWidth, 0.5, 0.0, 0.0));

@DaveHoskins I had turned off mip-mapping for the texture I’m using since it’s basically being used as a large uniform buffer, but I suppose this is a good tip anyways, just to make sure nothing goes wrong.

Thanks

What shader model are you targeting? Load works and compiles just fine on my platform, but I am targeting 5.0. You might try pushing up the profile to a higher target.