Passing struct to kernel

Hi I am new to OpenCL and have many problems writing OpenCL kernels.

I have a struct defined as:


typedef struct World {
	Camera camera;
	Light *lights;
	Sphere *spheres;
} World;

where Light and Sphere are another 2 structs which contains cl_float4 type, i tried to define a struct in the kernel as:


			"typedef struct {
"
			"	Camera camera;
"
			"	Light *lights;
"
			"	Sphere *spheres;
"
			"} World;
"

However, when I pass my memory object to the kernel, all the values in the lights and spheres are 0, the value in camera is correct.

You cannot pass a struct that contains pointers into OpenCL (section 6.8.p).

Hi david, thank you for your reply, can I change the struct to the following to pass to the kernel since i know the size?


typedef struct {
	Camera camera;
	Light lights[2];
	Sphere spheres[121];
} World;

Yes, you can do that.

Thanks again, but I have a new problem :frowning:
when I pass the world struct to the kernel which contains some value, i cannot get the value stored in the kernel by: float a = world->lights[1].position.x, because when I passed the value of ‘float a’ back to the host it is 0, do you know why?

Thanks again, but I have a new problem :frowning:
when I pass the world struct to the kernel which contains some value, i cannot get the value stored in the kernel by: float a = world->lights[1].position.x, because when I passed the value of ‘float a’ back to the host it is 0, do you know why?[/quote]

Just realised there is a difference between the struct in the kernel and outside :cry:

Just realised there is a difference between the struct in the kernel and outside

Yeah, that can happen. It would be easier if you didn’t use structs. For example, you can pass each camera parameter and light locations using a new kernel argument. Something like this:


__kernel void myKernel(__global float* camera_matrix, __global float* light_params, uint num_lights, __global float* spheres, uint num_spheres)
{
...
}