I am writing a ray tracer using openCL, and following this tutorial http://raytracey.blogspot.com/2016/11/opencl-path-tracing-tutorial-2-path.html However, I find that the author has two copies of the Sphere code, one in host in c++ and one in device in cl. This causes duplication, which I would like to avoid if possible. Is there any way to avoid this? If there is, should the code for, say a sphere struct be in host or in device?
In order to pass data to the device side, in OpenCL you must create the sphere structure on the device as well (that is in the kernel). So you first create your structures as classes or structs on the CPU. The variables in the structs should be of
cl_* datatypes. OpenCL provides typedefs for all primitive data types. Then create these structures on the GPU as well.
Care should be taken about data alignment when passing structures from CPU to GPU. If you have passed data in OpenGL, you'll know what this is about.
After you create your structures on the CPU, you encode them in a OpenCL buffer, and pass this buffer to the kernel.