Is there any way to set length of integer array with variable in hlsl?

983 views Asked by At

firstly, I wish you guys can understand awkward grammar skills of next writing. English is not my first language.

Im currently using UnityEngine Now. what i wanna do is sending a number of rows to a shader, so that i can set a count of rows of stripes in Gameobject mesh using the shader which got the number of stripes.

And I made to send a number variable to a shader, but when i try to create int array in CG program part(which is HLSL) with size of the rows that i want using the number, unity Engine gives me this error message - "array dimensions must be literal scalar expressions".

This is the integer variable that i set in my unity shader script. This gets integer value from c# script function(this part doenst have any issue)

_LowCount ("LowCount", int) = 0

And this is CG Program part which im struggling with. The variable below is declared in global field. It receives number value from the properties.

int _LowCount;

And this is fragment shader function part and it declares integer array in its local field setting the array size on integer variable - "_LowCount"

fixed4 frag(v2f i):COLOR{
                fixed4 c=0;
                int ColorsArray[_LowCount];
                for(int aa=0;aa<_LowCount;aa++){
                    ColorsArray[aa]=0;
                }
return c;

And below part from fragment shader function gives me the error that i mentioned in above.

int ColorsArray[_LowCount];

I searched this issue in google, then i realized i have to set array size with number value( not a variable). But I need an integer array with size of number variable that i can give any integer value anytime i want. Is there any solution?

*ps. I started to learn CG graphics from just 2 weeks ago. So I might be wrong in my understading and my knowledge. Thank you.

1

There are 1 answers

1
mcky On

There is no way to define an hlsl array with variable size. From the docs:

Literal scalar expressions declared containing most other types

Either preallocate with the maximum array size int ColorsArray[maximum possible _LowCount];

It's not super clear what your end goal is, but another solution may be to execute a different shader for each object instead. See if you can update your question a little and I'll update the answer.