Arrays as parameters to Cg functions

Hi all,

I’m struggling with the syntax for passing an array (sized or unsized, doesn’t matter) to a function in Cg. Basically, I wanna do something like this:

		inline float Foo(float testArray[])
		{
			//
			// Cg code here
			//
		}

But it won’t compile. The compilation error is unhelpful - it just says syntax error. Initially, I thought it was because the array had to be sized, e.g.:

		inline float Foo(float testArray[4])
		{
			//
			// Cg code here
			//
		}

But this is not the case. What’s up with this? Why won’t it accept arrays as parameters to functions in Cg? The Cg Reference Manual from NVidia has syntax almost identical to this in its examples. Look at page 400 in the example of cgSetArraySize, for example, they’re using a Cg stub defined like this:

float4 main(float4 myarray[])
{
/* ... */
}

Which indicates to me this is supposed to be perfectly legal… Is it because I’m on too low a shader model? I’m compiling to #pragma target 3.0 because I can’t get it to accept anything higher. It reverts to 2.0 if I try to put #pragma target 4.0 or #pragma target 5.0 instead. So perhaps Shader Model 3.0 doesn’t support arrays as parameters, or…?

Thanks a bunch in advance for any help!

Are you sure the error is at that line? I copy-pasted the code and it compiles just fine…

In which Shader Model? 3.0?

Oh yeah, sorry… I didn’t read it all through.

Hmm… arrays out of function definitions seem to be working just fine. My guess is that it’s a bug in the compiler. So either use 3.0 or flatten the function.

I’ve done some more experiments. :slight_smile:

It turns out I can get the shader to compile if the shader is a custom vertex/fragment pair, i.e. an old-school manually written one. If I add the one with the unsized array to such a shader, it’ll compile but the compiler automatically adds this directive to the top:

// Upgrade NOTE: excluded shader from DX11, Xbox360, OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers d3d11 xbox360 gles

That is, it compiles by… excluding itself from compilation. Great. If I add the function with an explicitly declared array size instead, it compiles fine.

The “syntax error” remains for all non-lowlevel shaders, that is, ShaderLab shaders or shaders that define a surface function cannot compile arrays as parameters.