According to the Unity documentation, in shader code we can get the size of a texture with variables named in {TextureName}_TexelSize
. I tried but failed when writing compute shader. So I wonder if it's because the _TexelSize
suffix doesn't work in compute shader?
What I've tried is as follows.
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);
I've also tried to change the type of _MyTexture
to sample2D
but it doesn't work.
My goal is to get the size of _MyTexture
with _MyTexture_TexelSize
in shader code without setting it in C# code.
CodePudding user response:
Set the value manually:
myComputeShader.SetVector("_MyTexture_TexelSize", new Vector4(1.0f / myTexture.width, 1.0f / myTexture.height, myTexture.width, myTexture.height));