Thursday, August 18, 2011

Unity GPU Noise (Part 2)

Sheesh! Yet another 5 months have passed by!

When I was creating the demo of GPU Noise in Unity, I kept getting a "function 'tex2Dlod' not supported in this profile" error. I couldn't figure out why except that it was an OpenGL issue. So, I simply forced my shaders to exclude OpenGL and OpenGL ES renderers (#pragma exclude_renderers opengl gles). Obviously this is not the best solution, so I decided to dive into it again.

It turns out that the error goes away if you instead force the shader to compile to GLSL (#pragma glsl). I have no idea why this fixes the problem, since it was an OpenGL issue in the first place, but whatever. Forcing the shaders to compile to GLSL brought up two other issues that I had to fix.

1) GLSL doesn't support default parameters (Cg and HLSL do). This means that I had to go duplicate all of my shader functions that used them, which were a lot.

2) GLSL doesn't support the switch statement. I had to go manually unroll my switch statements to be if-else trees. This one really confused me because all of the GLSL documentation seems to indicate that switch is indeed in the language, so this might just be a Unity issue.

You can check out the updated demo here:

Hopefully it works on Mac now, but I haven't been able to test it out yet. Feel free to let me know!


Aras Pranckevičius said...

By default Unity compiles shaders for OpenGL into ARB vertex/fragment programs. And those indeed do not support LOD versions of the texture lookups. "#pragma glsl" tells to compile into GLSL instead. We don't default to GLSL because it tends to be much less stable (read: more driver crashes etc.) especially on older systems.

Regarding the other issues (default parameters & switch), would be good if you'd file bugs on that. Our HLSL -> GLSL translators ideally should translate any valid HLSL correctly. If they don't then it's a bug.

Patrick said...

Ah, that all makes sense. Thanks for explaining it Aras!

I'll go ahead and submit bugs for the GLSL issues. Thanks again!

Anonymous said...

Switch doesn't appear to be supported by GLSL for OpenGL ES 2.0 (the relevant specification is: )

Patrick said...

Doh! I forgot to check the ES spec. Thanks for the info.

Patapom said...

Hi, I've had the same problem (and headaches) with that darn tex2Dlod with my Nuaj' plug-in.

It turns out that, as I was about to give up, simply using "tex2D" does the trick and doesn't even pose a problem to GLSL compiler, as opposed to DirectX claiming (with reason) that it can't use ddx/ddy based texture sampling with conditional branching (my experience with DirectX was the reason why I didn't even bother to try a plain tex2D call in the first place BTW).

Anyway, I defined a convenient macro "_tex2Dlod" that's either an actual "tex2Dlod" for the HLSL compiler, or a simple "tex2D" for the GLSL one... And that seems to do the trick!