- Home /
Fragment shader with "return" in for loop crashes the game on Android
Maybe someone can explain this phenomena?
This fragment shader makes the game hang and then crash on Anrdoid device.
fixed4 frag(v2f_grab IN) : SV_Target
{
fixed4 color = tex2Dproj(_GrabTexture, IN.uvgrab);
if (_HasNewColor == 1 && ColorsAreEqual(color, _NewColor))
{
return _NewColorState;
}
for (uint i = 0; i < _ColorsToShowLength; i++)
{
if (ColorsAreEqual(color, _ColorsToShow[i]))
{
return color;
}
}
color = 1;
return fixed4(1,1,1,1);
}
The issue disappears if I replace "return" with "break".
fixed4 frag(v2f_grab IN) : SV_Target
{
fixed4 color = tex2Dproj(_GrabTexture, IN.uvgrab);
if (_HasNewColor == 1 && ColorsAreEqual(color, _NewColor))
{
return _NewColorState;
}
bool shouldShow = false;
for (uint i = 0; i < _ColorsToShowLength; i++)
{
if (ColorsAreEqual(color, _ColorsToShow[i]))
{
shouldShow = true;
break;
}
}
if (!shouldShow)
{
color = 1;
}
return color;
}
$$anonymous$$y shader does a return in the loop, although not crashing it causes artifacts.
In $$anonymous$$e I fixed it by exiting early setting 'i' to the 'length' (so like a break).
Seems like returning early in shaders is not a great idea, and definitely not in loops. I'm no expert with shaders, but I seem to remember there are complications with early exits, especially in older versions.
For example the clip function is known to have complications, or still continue execution anyway.
Your answer
Follow this Question
Related Questions
How to convert surface shader to Vertex/Fragment code? 0 Answers
What is the proper way of calculating attentuation in Unity3D non-surface shaders? 0 Answers
Need help with this Shader; camera Solid color is being rendered on skybox. 0 Answers
Dissolve Shader Problem 1 Answer
General question about shading in unity 2 Answers