<p>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 <code>cl_*</code> datatypes. OpenCL provides typedefs for all primitive data types. Then create these structures on the GPU as well.</p>

<p>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.</p>

<p>After you create your structures on the CPU, you encode them in a OpenCL buffer, and pass this buffer to the kernel.</p>

CC BY-SA 4.0
2018-08-31T07:36:55.160Z
2018-08-31T07:36:55.160Z
gallickgunner
<p>I am writing a ray tracer using openCL, and following this tutorial <a href="http://raytracey.blogspot.com/2016/11/opencl-path-tracing-tutorial-2-path.html" rel="nofollow noreferrer">http://raytracey.blogspot.com/2016/11/opencl-path-tracing-tutorial-2-path.html</a>
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?</p>

<raytracing><opencl>
OpenCL ray tracer: which is better, geometry code on device side or on host side?
Manh Nguyen
# OpenCL ray tracer: which is better, geometry code on device side or on host side?

(Asked by: username_0 on 2018-08-30)

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?

**Tags:** raytracing, opencl


---

## 1 answers

### Answer by username_1 on 2018-08-31. Score: 1

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.


---

