Archive for October 7th, 2009

07
Oct
09

OpenCL Radial Displace Code

Here’s the code for the below.

const float PI = 3.14159265359;
const float TWOPI = 6.28318530718;

float4 makeSphere(float4 pos, float radius) {
	
	float u = pos.x * TWOPI * 1.01;
	float v = pos.y * PI;
	
	float4 sphere;
	sphere.x = radius * cos(u) * sin(v);
	sphere.y = radius * sin(u) * sin(v);
	sphere.z = radius * cos(v);
	sphere.w = 1.0;
	
	return sphere;
}


__kernel void tb_vdm_sphere(__rd image2d_t BaseImg, float Width, float Height, float Displacement,
						__global float4 *Mesh, __global float4 *Colors)
{
	// Get values for current iteration
	int		tid_x = get_global_id(0),
			tid_y = get_global_id(1),
			indx = tid_y * get_global_size(0) + tid_x;
	
	// Init variables		
	float4 color, vertex;
	//int2   pos = (int2)(tid_x, tid_y);
	// Floating-point position in 0 > 1 range
	float4 posFloat = (float4)((float)tid_x / Width, (float)tid_y / Height, 0.0, 1.0); 
	
	// Sample colour from base image
	sampler_t smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
	color = read_imagef(BaseImg, smp, (int2)(tid_x, tid_y));
	
	// Write color at index in Colors output array
	Colors[indx] = (float4)(1.0,1.0,1.0,1.0);
	
	// Displacement is determined by the displacement amount * the "length" of the color vector (ie approximate brightness)
	/*vertex =	(float4)
				((float)tid_x / get_global_size(0) - 0.5,
				1.0 - (float)tid_y / get_global_size(1) - 0.5,
				Displacement * length(color.xyzw), 1.0);*/

	
	// Write mesh value at index of Mesh output array
	Mesh[indx] = makeSphere(posFloat, Displacement * length(color.xyzw));
}

It’s just a doctored version of the OpenCL Extruded Text developer example (which is very nice, incidentally).

OpenCL is another ‘C-like’ language, very like GLSL, in a lot of ways. There are some slight syntax differences which have been tripping me up just while working on this little project. I’m sure I’ll get used to it though. There’s so much you can do with it, I’m sure I’ll be using it a lot. It’s just a shame relatively few Mac models/GPUs currently support it. OpenCL is actually one of the reasons I decided to upgrade my ageing laptop this Summer. I’m glad I did, now.

07
Oct
09

OpenCL

I finally had a mess around with OpenCL in Quartz Composer 4 today, and managed to achieve something I’d been attempting to do unsuccessfully using GLSL shaders for a while.

My joy was somewhat punctured when my machine locked-up completely, about a minute later, and I had to do a force-shutdown.

So, still flaky, but powerful, too.

Incidentally, this ran at 60fps at 512 x 384px, with a 200×200 vertex grid (40,000 verts).




Twitter

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

October 2009
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Links

Blog Stats

  • 406,500 hits

Follow

Get every new post delivered to your Inbox.

Join 37 other followers