Unity4.5 Shader Constant Array??

Did Unity 4.5 add in the ability to pass in Shader Constant Arrays? So I can index data passed in efficiently.

No, 4.5 does not add native support for uniform arrays.

You can index data passed in (like you always could), it’s just quite cumbersome to set it up on the CPU side. Basically, you can have “uniform float4 foo[10];” in the shader, and that will appear as 10 individual vector properties, named float0…float9 to the script side. So to set them up, you’d have to call material.SetVector 10 times.

Hi Aras, without the ability to properly index a constant array, you can’t do color pallet animations correctly in cases where a texture are not used, such as a voxel game where each block does not use a texture but rather is a solid color and only has 8 verts (Which yes I was trying to do) :slight_smile:

Would be cool to see this feature in the future. Unity4.5 shader compile is much nicer to use with the faster compile times, but fingers crossed this feature will be put in at some point (maybe Unity5.0) :stuck_out_tongue:

Like I said - you can index the constant array in the shader (and you always could). It’s just really cumbersome to set it up from scripting side.

But the shader itself is not indexing values? Thus its super slow in the Shader and slow when passing values to the GPU which is not a good idea. I don’t know how that is a solution, particularly on mobile platforms that would be a very bad idea and are not even going to have enough operations available to handle an array of any decent size…

If I understand Aras correctly, you declare your array like you always do in your shader code:

uniform float4 foo[10];

… but the shader pipeline seems to transform the array into multiple variables, like:

uniform float4 foo0;
uniform float4 foo1;
...
uniform float4 foo9;

In order to access these “array elements” from your script, specify their explicit name:

Shader.SetVector(string.Format("foo{0}", index), someVector); // set array element

Hope it makes sense.

@Peter77 I fully understand his explanation. This issue is its not an acceptable way to handle shader code on any platform, for the reasons I stated (as its slow). I have written native shader code on about every platform for years now, even old ASM stuff before HLSL even existed (no that doesn’t mean I know everything, it just means I have a pretty good understanding of how shaders should work). They all support indexing on the GPU side (if Unity supports one that doesn’t I would like to be corrected, but I highly doubt it)

I understand Unity is a big project and this is not a #1 priority issue, i’m just trying to help by pointing out a major design flaw and why it is one technically. As a long time shader/graphics dev myself, I always found this implantation in Unity to be odd.

But thank you knowing how you index something via ““foo{0}”” does help me for future stuff.

What makes you think so? He didn’t say that. He said, they work on the gpu/shader side as you’d expect them too — only issue is if you wanna populate your constant array from a script, you have to use the slightly “inconvenient” string names exposed to the script side(even though the array exists in indexed form). Unless you have verified that the compiled shader code does indeed remove your indexed constants, I don’t see what the fuss is about.

Wrong, here is a quote: “No, 4.5 does not add native support for uniform arrays.”

No they don’t. Just look at the compiled shader code. It breaks it up into tons of separate objects. Why would you assert something before checking yourself?

Whoops you’re right, my bad… one of the very few instances where I did INDEED not pay 100% proper attention … :o

Yeah it’s annoying how Unity restricts certain “bog-standard shading standard things” quite arbitrarily. I’ve said it before and I’ll say it again — Aras, give us the #pragma im-a-shader-hacker-and-want-full-access-at-my-own-risk switch!

Although maybe that’s only in Cg? Maybe anything is possible in direct hardcoded GLSL? Not sure, have you tried?

No I wouldn’t really want to do that as thats not portable and would have to be done differently for OpenGL vs D3D9 vs D3D11. It needs to be done in GC and then converted to each native platform or its just a waste of time.