# Working code doesnt work in opencl

## Recommended Posts

Hi

I have a simple ray sphere intersect function that works fine in c but when i try to run it in an opencl kernel, it doesnt work. Having a seriously hard time because i cant debug the kernel and see the problem. Am i making a simple error in my conversion to opencl?

C Code

float hitSphere(vector3 center, float radius, ray r)
{
vector3 oc = subtract_Vecs(&r.origin, &center);
float a = dot(&r.direction, &r.direction);
float b = 2 * dot(&oc, &r.direction);

float d = b*b - 4 * a*c;

if (d < 0)
return -1.0;
else
return (-b - sqrt(d)) / (2.0*a);
}


OpenCL


float hitSphere(float3 center, float radius, struct Ray r)
{
float3 oc = r.origin - center;
float a = dot(r.dir, r.dir);
float b = 2 * dot(oc, r.dir);

float d = b*b - 4 * a*c;

if(d<0)
return -1.0;
else
return (-b - sqrt(d)/(2.0*a));
}


Thanks for any help

##### Share on other sites

The only thing that helps here is to add additional code so you can output numbers and compare with cpu.

The more time you invest in this upfront, the more you save later.

I this case it would first identify a certain ray / sphere combination, output their data to make sure positions, directions etc. match

(does indexing memory work? contains memory the values i expect?),

and if so start to output values calculated inside the function...

##### Share on other sites

Thanks Joe, I thought that might be the case. I created a debug buffer that i output all my values to. In the end, it was my indexing code. I was calculating the y coordinate correctly but x was always zero.

Turns out debugging kernels isnt hard afterall.

Thanks a lot

##### Share on other sites
Last time I checked you can also use printf from CL kernels. It might have been an extension but it should be widely supported.

##### Share on other sites

intel's opencl sdk integrates nicely into visual studio (and I think in eclipse also), and allows you to debug opencl programs nearly as nicely as native c code.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627655
• Total Posts
2978460

• 10
• 12
• 22
• 13
• 33