Sign in to follow this  
BadEggGames

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

Edited by BadEggGames

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this