How to set custom structs as non-kernel args

Jun 1, 2011 at 5:19 AM

I have a function in my .cl that takes "Transform" as an argument, which contains 3 float4's. How do I do this? I thought maybe I could cast it like this: "(Transform)((float4)(0,1,2,3),(float4)(0,1,2,3),(float4)(0,1,2,3))", but that doesn't work. Is there a way to do it?

By the way, I really love luminal - it's far more C#'y that Cloo.

Coordinator
Jun 27, 2011 at 12:45 AM

Sorry for the late reply - before I switched to chrome, I had RSS bookmark feeds for all my projects. I finally got off my lazy but and found an appropriate extension.

Anyway...

If I'm interpreting your question right, this is a purely OpenCL issue? In which case, you should probably be asking on the Khronos message boards. Regardless, I'll try my best.

If you were to define your Transform struct in C# as well (with all the fields laid out exactly as you had it in your OpenCL file), you could simply pass that to SetArgument, and that would be correctly transferred to your OpenCL code.

ex.

[C#]

 

kernel.SetArgument(0, myTransform);

 

[OpenCL]

 

__kernel void myKernel(__global Transform transform, ...etc)

If you think of your structs as an array of bytes, then think of those bytes being sent to your kernel, and then casted back to your type.
Alternatively, you can set all the fields manually, ex:
[OpenCL]
Transform createTransform(float4 x, float4 y, float4 z)
{
    Transform output;
    output.x = x;
    output.y = y;
    output.z = z;
    return output;
}

 

You didn't give me the definition of your struct, so I'm just guessing here, but I think you get the point :)

In C#, the above code would be producing errors along the lines of "Uninitialized Variable" or something. This doesn't happen in OpenCL - just remember to initialize all the fields before you use it.