Can anyone help solve this console error please

i am getting this error in the console:in regard to a shader

integer modulus may be much slower, try using uints if possible. at line 55 (on d3d11)

the following line is causing the error

return ((int)(ps / floor(_LineWidth*lineSize)) % 2 == 0) ? color : color *float4(_Hardness,_Hardness,_Hardness,1);

i take it it is to do with int but i dont know how to replace with uints

thanks if you can help

i have just found some info saying that i should also not use uint as sm3.0 has no integers, so how can i convert the problem code into a float

If you’re aiming for desktop or console, you can kind of ignore that warning, or you can replace the (int) with (uint) and be done.

return ((uint)(ps / floor(_LineWidth*lineSize)) % 2 == 0) ? color : color *float4(_Hardness,_Hardness,_Hardness,1);

Though you can avoid the cast to uint or int completely if you want, like this:

return fmod(ps / floor(_LineWidth*lineSize), 2) < 1.0) ? color : color *float4(_Hardness,_Hardness,_Hardness,1);

That returns the exact same result, but without any casts to int. What you’re gaining by casting to an int is it floors the value to a whole number. We could replace the (int) with another floor(), but just testing if the remainder of the modulo is less than 1.0 gets the same result without needing to do that, and == and < are identical in cost. Now, you’ll also notice that there’s no % (modulo operator) in that int-less example. In many programming languages, % can only be used on integer values, but that’s not true for HLSL, so you could actually use it still. However fmod() is slightly faster and for this case produces identical results.

But, I would suggest not using that second example and stick with your original line, just replacing (int) with (uint).

Why? Because your original code using uint is potentially about 40% faster than on hardware that does support uint values than the cost of the second example (which is itself 10% faster than the int version), and on everything that doesn’t it’ll be automatically converted to something nearly identical to that second example. There’s not really a need for you to rewrite your code to account for the lack of uint support because Unity’s cross compiler does that for you.

1 Like

thank you very much for your quick reply