Working code doesnt work in opencl

Started by
3 comments, last by Krypt0n 7 years, 7 months ago

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 c = dot(&oc, &oc) - radius*radius;

	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 c = dot(oc, oc) - (radius*radius);

	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

Advertisement

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

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

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

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.

This topic is closed to new replies.

Advertisement