0

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.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • 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 Aug 22 '19 at 17:46
  • 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 Aug 23 '19 at 05:33

1 Answers1

1

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.

kefren
  • 1,042
  • 7
  • 12
  • That is of course trivial. This is what I'm doing right now and is less than optimal and poor performance when the array size may be big. To somewhat overcome the issue, I made a macro generating the function for given size. That is not optimal either but much better. I'm still looking for a way to handle unspecified array size, the kind of thing very common in C/CPP using a pointer and a size argument or a pointer and aterminating value (Such as C strings). – fpiette Aug 24 '19 at 13:28
  • HLSL has no pointers, and it seems it's not going to, at least in the close future (https://github.com/microsoft/DirectXShaderCompiler/issues/2290). Other languages have them (like OpenCL), but HLSL does not. – kefren Aug 24 '19 at 16:10