Bizzare error with for loop and tex2D in HLSL shader
I'm trying to make two for loops within a post-processing shader for Unity in HLSL, however I'm having a weird problem. When I try and use tex2D
on _MainTex
, if I try to do so before the for loop everything works fine. However trying to use it within or after the for loop results in an error: Shader error in 'Custom/Pixelate': 'tex2D': no matching 2 parameter intrinsic function;
This error seems to indicate something has changed about _MainTex
or i.uv
, but I can't figure out why it should. I should note that if I remove this line of code or place it before the for loop, I get no errors, and I can't see anything wrong with the for loops themselves. Is there a technicality I'm missing?
Relevant code:
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(0,0,0,0);
[loop]
for (int i = (int)round(-(_PixelSize / 2)); i < _PixelSize / 2; i ++)
{
[loop]
for (int j = (int)round(-(_PixelSize / 2)); j < _PixelSize / 2; j ++)
{
fixed4 sampleCol = tex2D(_MainTex, i.uv);
}
}
// just invert the colors
col = 1 - col;
return col;
}
Answer by ifurkend · Apr 01, 2019 at 01:05 AM
You have redefined the variable “i” as an integer in the declaration of the first for loop, which overwrites the struct “i”. Just change the name of the iteration variable to something else like “k”.