#!/usr/bin/perl use OpenGL ":all"; use OpenCL; my $S = $ARGV[0] || 256; # window/texture size, smaller is faster # open a window and create a gl texture OpenGL::glpOpenWindow width => $S, height => $S; my $texid = glGenTextures_p 1; glBindTexture GL_TEXTURE_2D, $texid; glTexImage2D_c GL_TEXTURE_2D, 0, GL_RGBA8, $S, $S, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0; # find and use the first opencl device that let's us get a shared opengl context my $platform; my $dev; my $ctx; sub get_context { for (OpenCL::platforms) { $platform = $_; for ($platform->devices) { $dev = $_; $ctx = eval { $platform->context (undef, [$dev]) } and return; } } die "cannot find suitable OpenCL device\n"; } get_context; print "using ", $dev->name, "\n"; my $queue = $ctx->queue ($dev); # now the boring opencl code my $src = <build_program ($src); my $kernel = $prog->kernel ("juliatunnel"); my $tex = $ctx->image2d (OpenCL::MEM_WRITE_ONLY, OpenCL::RGBA, OpenCL::UNORM_INT8, $S, $S); # program compiled, kernel ready, now draw and loop for (my $time; ; ++$time) { # configure and run our kernel $kernel->setf ("mf", $tex, $time*2); # mf = memory object, float $queue->nd_range_kernel ($kernel, undef, [$S, $S], undef); # read image $queue->read_image ($tex, 0, 0, 0, 0, $S, $S, 1, 0, 0, my $data); # wait $queue->finish; # upload texture glTexSubImage2D_s GL_TEXTURE_2D, 0, 0, 0, $S, $S, GL_RGBA, GL_UNSIGNED_BYTE, $data; # now draw the texture, the defaults should be all right glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; glEnable GL_TEXTURE_2D; glBegin GL_QUADS; glTexCoord2f 0, 1; glVertex3i -1, -1, -1; glTexCoord2f 0, 0; glVertex3i 1, -1, -1; glTexCoord2f 1, 0; glVertex3i 1, 1, -1; glTexCoord2f 1, 1; glVertex3i -1, 1, -1; glEnd; # with glDrawPixels, you would simply do: # glDrawPixels_s $S, $S, GL_RGBA, GL_UNSIGNED_BYTE, $data; # and then start to worry a lot about the coordinate system # used in the opencl kernel glXSwapBuffers; select undef, undef, undef, 1/60; }