Results 1 to 7 of 7

Thread: Passing struct to kernel

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    Passing struct to kernel

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

    I have a struct defined as:
    Code :
    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:
    Code :
    			"typedef struct {\n"
    			"	Camera camera;\n"
    			"	Light *lights;\n"
    			"	Sphere *spheres;\n"
    			"} World;\n"

    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.

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Passing struct to kernel

    You cannot pass a struct that contains pointers into OpenCL (section 6.8.p).
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    Re: Passing struct to kernel

    Quote Originally Posted by david.garcia
    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?

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

  4. #4
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Passing struct to kernel

    Yes, you can do that.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    Re: Passing struct to kernel

    Quote Originally Posted by david.garcia
    Yes, you can do that.
    Thanks again, but I have a new problem
    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?

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    Re: Passing struct to kernel

    Quote Originally Posted by helloOpenCL
    Quote Originally Posted by david.garcia
    Yes, you can do that.
    Thanks again, but I have a new problem
    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?

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

  7. #7
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Passing struct to kernel

    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:

    Code :
    __kernel void myKernel(__global float* camera_matrix, __global float* light_params, uint num_lights, __global float* spheres, uint num_spheres)
    {
    ...
    }
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Similar Threads

  1. Replies: 14
    Last Post: 10-29-2013, 05:07 AM
  2. Passing pointers in a struct
    By ibbles in forum OpenCL
    Replies: 3
    Last Post: 11-10-2009, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •