_MainTex_TexelSize what's the meaning?

uniform float4 _MainTex_TexelSize
where is the value of the float4 _MainTexelSize from?

It’s set by the application if it’s present in the shader - it is the size of a texel of the texture in question, in other words, if it’s a 1k x 1k texture, both x and y will be 1.0/1024.0

1 Like

it’s the easy way you get your 0…1 uv range from the texture size. It should go up to1/ 2048 or 1/4096 in unity though?

_MainTex_TexelSize is set by magic sauce in the darkness of Unity’s source code. It follows the dimensions of the _MainTex sampler and has these contents:

Vector4(1 / width, 1 / height, width, height)

16 Likes

Oh wow, that’s awesome. I didn’t know it did that :smile:

There’s more to it than that: _TexelSize.y is negative when it belongs to a RenderTexture that has been flipped vertically by D3D anti-aliasing. Unfortunately, even after years of documentation complaints and forum posts, this is the only thing that is documented about _TexelSize.

13 Likes

I’m just trying to understand the meaning of this “TexelSize” variable,thanks guys

Is there something more specific about the first three responses in this thread that doesn’t make sense?

I can’t find the description in documents. is there url ?

_TexelSize is documented here.
http://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html

4 Likes

unfortunately the way _TexelSize was defined, it doesn’t scale up to 3D textures

I cannot seem to use _MainTex_TexelSize. When I use it, it just says that it doesn’t exist. How do I use it? In for example Unity’s default Sprites/Default shader (which I have copied from latest beta)?

I think it seems to work. You’d need to declare the variable to access the associated property value. Put this above the frag function if that’s where you refer to it:
float4 _MainTex_TexelSize;

How do we get the size of the volume depth though (for a 3D texture)?
We have to manually feed it?

Feed it in manually.

2 Likes

If you are using HLSL you can do this:

uint3 textureDimensions = 0;
_MainTex.GetDimensions(0,textureDimensions.x,textureDimensions.y,textureDimensions.z);

I don’t think it is possible in CG.

There is no more CG in Unity, hasn’t been for maaany years. It is all treated as HLSL despite the .cginc and CGINCLUDE naming. What matters though is the shader model you’re targeting your shader code, as GetDimensions() did not exist in HLSL before SM4.0 (or before DX10 to be more specific). So you’d want a #pragma target 4.0 at least where Texture2D exists, prior to that it was just the sampler bound sampler2D types.

DX9 was removed from Unity in 2017.3 though, so there isn’t much worry about that function being unsupported in your shader code now.

  • Unless you’re targeting OpenGLES 2.0 or 3.0 in which case there’s no equivalent function.
1 Like

Is the _TexelSize suffix also available in compute shader?

I defined the following two variables in my compute shader

Texture<float4> _MyTexture;
float4 _MyTexture_TexelSize;

and set _MyTexture in C# code

ComputeShader myComputeShader;
Texture2D myTexture;
// ...
myComputeShader = myComputeShader.SetTexture(0, "_MyTexture", myTexture);

But _MyTexture_TexelSize seems to not containing the size information of my texture.