PDA

View Full Version : opencl problems



nomad28
11-06-2012, 04:29 PM
I want to use the opencl framework in my project but i'm stuck on the parts that I need. I think I need the Compute, GL, D3D, Core, Display, and SWRender libraries, but I know that I don't need all of them to create a single OpenCL library that I can use. I'm trying to totally integrate it into my project, so I don't want have to handle a lot of extra overhead. Can anyone point me in the right direction?

Judas
11-06-2012, 09:29 PM
I assume you mean the clanCompute project in the ClanLib 3.0 SVN branch. It was designed around the idea that you create your OpenCL compute context from a GraphicContext. It doesn't have a way of initializing OpenCL without, although itself OpenCL does support such a thing.

A code example for the clanCompute library:


using namespace clan;

SetupCore setup_core; // Initialize clanCore
SetupDisplay setup_display; // Initialize clanDisplay
SetupD3D setup_d3d; // Initialize clanD3D (could also be clanGL)
SetupCompute setup_compute; // Initialize clanCompute

DisplayWindow window("Hello", 640, 480); // create window with either OpenGL or Direct3D target
GraphicContext gc = window.get_gc(); // fetch graphics command queue object

ComputeContext cc(gc); // create OpenCL compute context object
ComputeCommandQueue cq(cc); // create OpenCL command queue for the context

// Create silly compute program that fills a buffer with incrementing values
ComputeProgram program(cc, "kernel void increment(global unsigned char *buffer) { buffer[get_global_id(0)] = get_global_id(0); }");
program.build();

ComputeBuffer buffer(cc, 16*1024); // create GPU buffer for output values

ComputeKernel kernel(program, "increment"); // create kernel object for the 'increment' function in our program
kernel.set_arg_buffer(0, buffer); // bind buffer to first function parameter

ComputeEvent e_run = cq.run_1d(kernel, 16*1024); // dispatch function to be run 16*1024 times

char result[16*1024];
ComputeEvent e_download = cq.read_buffer(buffer, false, 0, 16*1024, result, e_run); // copy from buffer to a CPU memory buffer (after e_run finished)

e_download.wait(); // wait for transfer to complete

// result should now contain a buffer with values: 0 1 2 3 4 5 .. 253 254 255 0 1 2 ..


Personally I am no longer using clanCompute for my graphical needs anymore since both OpenGL and Direct3D has compute shaders now. Theoretically OpenCL is more powerful but unfortuantely my own tests revealed that Nvidia's OpenCL/OpenGL interop is so crap that it effectively became useless. You can create compute shaders using ProgramObject in clanDisplay.

Out of curiosity what do you intend to use it for?