Welcome, Guest. Please login or register.
February 05, 2012, 10:31:36 PM
Home Help Search Login Register
News: A significant upgrade to OpenCL Studio 1.1 has been released including new OpenCL library elements, addtions to the scripting interface and various bug fixes.

+  opencldev.com
|-+  OpenCL Studio
| |-+  Development Environment
| | |-+  Passing custom structure from script to kernel
« previous next »
Pages: [1] Print
Author Topic: Passing custom structure from script to kernel  (Read 570 times)
raistlin969
Newbie
*
Posts: 13


« on: March 02, 2011, 04:01:43 PM »

I am trying to pass a custom structure to my kernel call but seem to have trouble when one of the members is a vector.  I have done the same thing that the Particles demo does with the System structure, but if I read the value of the vector it is not the same as what I set it to in the script.  Here is some code to show what I am trying.

In Lua:
(Global section)
struct.create("Sphere")
struct.field("Center", cl.cl_float4)
struct.field("Color", cl.cl_uchar4)
struct.field("Radius", cl.cl_float)

(EventHandler section)
event:setSize(struct.size("Sphere"))

buffer = event:getBuffer()
struct.interpret(buffer, "Sphere")

mem.map(buffer, mem.WRITE)

struct.map(buffer, 0)
struct.set("Center", {0.0, 0.0, 0.0, 0.0})
struct.set("Color", {255, 255, 0, 255})
struct.set("Radius", 100.0)

mem.write(buffer)

s = OpenCL.sphere:getBuffer()
cl.clSetKernelArg(kernel, 1, s)

The kernel parameter in the code section is __global const Sphere* sphere
If I access a single member variable like, sphere->Radius == 100 I have no problem.  But when I try to test a vector value, like sphere->Color.x == 255, it does not seem to work.  I do not know what value is in there, but it should be 255, but it isn't since that comparison fails.
Any one have any ideas?  I must be missing some small step somewhere.
Logged
opencldev
Administrator
Jr. Member
*****
Posts: 59


« Reply #1 on: March 02, 2011, 05:18:56 PM »

I don't think you can use table notation for vector parameters in the struct package. i.e you should use

Code:
struct.set("Color", 255, 255, 0, 255)
struct.set("Center", 0.0, 0.0, 0.0, 0.0)


Without the "{" and "}". I am surprised there was no error message. Thx for pointing this out.

There are sometimes also alignment problems between the OpenCL structs and the ones created by the struct package. It would be best to use the "aligned" directive when defining OpenCL structs like so:

Code:
typedef struct  __attribute__ ((aligned (16)))
{
        float4 Center;
        uchar4 Color;
        float Radius;
} Sphere;

Right now the struct package implicity aligns to 16 bytes but in the future this parameter will be variable.


I hope that fixes the problem. Otherwise your code looks fine.

« Last Edit: March 02, 2011, 09:08:37 PM by opencldev » Logged
raistlin969
Newbie
*
Posts: 13


« Reply #2 on: March 03, 2011, 12:01:06 PM »

Yes that fixed it.  I did not get an error message by using the table notation, but that is what the problem was.
Thanks.

I also just found out the hard way that if you initialize a float4 in your kernel code to make sure to write
Code:
float4 color = (float4)(1.0, 0.0, 0.0, 1.0)

instead of
Code:
float4 color = (1.0, 0.0, 0.0, 1.0)

For some reason I did not think I needed the cast since I was setting the components to floats to begin with.
Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!