0
votes

I'm trying to write a function within a compute shader (HLSL) that accept an argument being an array on different size. The compiler always reject it.

Example (not working!):

void TestFunc(in uint SA[])
{
   int K;
   for (K = 0; SA[K] != 0; K++) {
       // Some code using SA array
   }
}

[numthreads(1, 1, 1)]
void CSMain(
    uint S1[] = {1, 2, 3, 4 };  // Compiler happy and discover the array size
    uint S2[] = {10, 20};  // Compiler happy and discover the array size

    TestFunc(S1);
    TestFunc(S2);
}

If I give an array size in TestFunc(), then the compiler is happy when calling TestFunc() passing that specific array size but refuse the call for another size.

1
I don’t understand. In your code you say the compiler is happy all the times, but you state that the code doesn't work. Can you please clarify better what the problem is?kefren
The compiler is happy with the two declaration of array variables S1 and S2 without dimension. The compiler doesn't like the array withut dimension declared as TestFunc() argument. The code I present is the code I would like to compile: having a function taking an undimensionned array and called with various array sizes.fpiette

1 Answers

0
votes

You cannot have function parameters of indeterminate size. You need to initialize an array of know length, and an int variable that holds the array length.

void TestFunc(in uint SA[4], in uint saCount) 
{  int K; 
   for (K = 0; SA[K] != 0; K++)
     { 
        // Some code using SA array, saCount is your array length;
      }
 }

[numthreads(1, 1, 1)] 
 void CSMain()
{
 uint S1count = 4;
 uint S1[] = {1, 2, 3, 4 };
 uint S2count = 2;
 uint S2[] = {10, 20,0,0}; 
 TestFunc(S1, S1count); 
 TestFunc(S2, S2count);
 }

In my example I have set your array max size as 4, but you can set it bigger if needed. You can also set multiple functions for different array lengths, of set up multiple passes if your data overflows your array max size.

Edit to answer comment The issue is that array dimensions of function parameters must be explicit as the compiler error states. This cannot be avoided. What you can do however, is avoid passing the array at all. If you in-line your TestFunc in your CSMain, you avoid passing the array and your routine compiles and runs. I know it can make your code longer and harder to maintain, but it's the only way to do what you want with an array of unspecified length. The advantage is that this way you have access to array.Length that might make your code simpler.