--- OpenCL/OpenCL.pm 2012/04/19 14:36:46 1.37 +++ OpenCL/OpenCL.pm 2012/04/24 13:30:49 1.50 @@ -162,7 +162,7 @@ my $prog = $ctx->program_with_source ($src); # build croaks on compile errors, so catch it and print the compile errors - eval { $prog->build ($dev); 1 } + eval { $prog->build ($dev, "-cl-fast-relaxed-math"); 1 } or die $prog->build_log; my $kernel = $prog->kernel ("squareit"); @@ -213,6 +213,101 @@ # wait for the last event to complete $ev->wait; +=head2 Use the OpenGL module to share a texture between OpenCL and OpenGL and draw some julia +set tunnel effect. + +This is quite a long example to get you going. + + use OpenGL ":all"; + use OpenCL; + + # open a window and create a gl texture + OpenGL::glpOpenWindow width => 256, height => 256; + my $texid = glGenTextures_p 1; + glBindTexture GL_TEXTURE_2D, $texid; + glTexImage2D_c GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 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; + + for (OpenCL::platforms) { + $platform = $_; + for ($platform->devices) { + $dev = $_; + $ctx = $platform->context ([OpenCL::GLX_DISPLAY_KHR, undef, OpenCL::GL_CONTEXT_KHR, undef], [$dev]) + and last; + } + } + + $ctx + or die "cannot find suitable OpenCL device\n"; + + my $queue = $ctx->queue ($dev); + + # now attach an opencl image2d object to the opengl texture + my $tex = $ctx->gl_texture2d (OpenCL::MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, $texid); + + # now the boring opencl code + my $src = <program_with_source ($src); + eval { $prog->build ($dev); 1 } + or die $prog->build_log ($dev); + + my $kernel = $prog->kernel ("juliatunnel"); + + # program compiled, kernel ready, now draw and loop + + for (my $time; ; ++$time) { + # acquire objects from opengl + $queue->enqueue_acquire_gl_objects ([$tex]); + + # configure and run our kernel + $kernel->set_image2d (0, $tex); + $kernel->set_float (1, $time); + $queue->enqueue_nd_range_kernel ($kernel, undef, [256, 256], undef); + + # release objects to opengl again + $queue->enqueue_release_gl_objects ([$tex]); + + # wait + $queue->finish; + + # 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; + + glXSwapBuffers; + + select undef, undef, undef, 1/60; + } + =head1 DOCUMENTATION =head2 BASIC CONVENTIONS @@ -243,7 +338,9 @@ =item * When enqueuing commands, the wait list is specified by adding extra arguments to the function - anywhere a C<$wait_events...> argument -is documented this can be any number of event objects. +is documented this can be any number of event objects. As an extsnion +implemented by this module, C values will be ignored in the event +list. =item * When enqueuing commands, if the enqueue method is called in void context, no event is created. In all other contexts an event is returned @@ -277,11 +374,11 @@ Due to the sad state that OpenGL support is in in Perl (mostly the OpenGL module, which has little to no documentation and has little to no support -for glx), this module, as a special extension, treats context creation +for glX), this module, as a special extension, treats context creation properties C and C specially: If either or both of these are C, then the OpenCL -module tries to dynamically resolve C and -C, call these functions and use their return values +module tries to dynamically resolve C and +C, call these functions and use their return values instead. For this to work, the OpenGL library must be loaded, a GLX context must @@ -361,6 +458,13 @@ L +=item $platform->unload_compiler + +Attempts to unload the compiler for this platform, for endless +profit. Does nothing on OpenCL 1.1. + +L + =for gengetinfo begin platform =item $string = $platform->profile @@ -655,9 +759,9 @@ Calls C with C and returns the result. -=item $uint = $device->reference_count_ext +=item $uint = $device->reference_count_ext -Calls C with C and returns the result. +Calls C with C and returns the result. =item @device_partition_property_exts = $device->partition_style_ext @@ -677,6 +781,10 @@ L +Example: create an out-of-order queue. + + $queue = $ctx->queue ($device, OpenCL::QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE); + =item $ev = $ctx->user_event Creates a new OpenCL::UserEvent object. @@ -695,6 +803,13 @@ Creates a new OpenCL::Buffer (actually OpenCL::BufferObj) object and initialise it with the given data values. +=item $img = $ctx->image ($self, $flags, $channel_order, $channel_type, $type, $width, $height, $depth, $array_size = 0, $row_pitch = 0, $slice_pitch = 0, $num_mip_level = 0, $num_samples = 0, $*data = &PL_sv_undef) + +Creates a new OpenCL::Image object and optionally initialises it with +the given data values. + +L + =item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $row_pitch = 0, $data = undef) Creates a new OpenCL::Image2D object and optionally initialises it with @@ -716,14 +831,21 @@ http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateFromGLBuffer.html -=item $ctx->gl_texture2d ($flags, $target, $miplevel, $texture) +=item $img = $ctx->gl_texture ($flags, $target, $miplevel, $texture) + +Creates a new OpenCL::Image object that refers to the given OpenGL +texture object or buffer. + +http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clCreateFromGLTexture.html + +=item $img = $ctx->gl_texture2d ($flags, $target, $miplevel, $texture) Creates a new OpenCL::Image2D object that refers to the given OpenGL 2D texture object. http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateFromGLTexture2D.html -=item $ctx->gl_texture3d ($flags, $target, $miplevel, $texture) +=item $img = $ctx->gl_texture3d ($flags, $target, $miplevel, $texture) Creates a new OpenCL::Image3D object that refers to the given OpenGL 3D texture object. @@ -796,11 +918,17 @@ They also allow you to specify any number of other event objects that this request has to wait for before it starts executing, by simply passing the -event objects as extra parameters to the enqueue methods. +event objects as extra parameters to the enqueue methods. To simplify +program design, this module ignores any C values in the list of +events. This makes it possible to code operations such as this, without +having to put a valid event object into C<$event> first: + + $event = $queue->enqueue_xxx (..., $event); Queues execute in-order by default, without any parallelism, so in most cases (i.e. you use only one queue) it's not necessary to wait for or -create event objects. +create event objects, althoguh an our of order queue is often a bit +faster. =over 4 @@ -876,10 +1004,6 @@ L -=item $ev = $queue->enqueue_marker ($wait_events...) - -L - =item $ev = $queue->enqueue_acquire_gl_objects ([object, ...], $wait_events...) Enqueues a list (an array-ref of OpenCL::Memory objects) to be acquired @@ -898,9 +1022,13 @@ L -=item $queue->enqueue_barrier +=item $ev = $queue->enqueue_marker ($wait_events...) + +L -L +=item $ev = $queue->enqueue_barrier ($wait_events...) + +L =item $queue->flush @@ -1025,7 +1153,9 @@ =head2 THE OpenCL::Image CLASS -This is the superclass of all image objects - OpenCL::Image2D and OpenCL::Image3D. +This is the superclass of all image objects - OpenCL::Image1D, +OpenCL::Image1DArray, OpenCL::Image1DBuffer, OpenCL::Image2D, +OpenCL::Image2DArray and OpenCL::Image3D. =over 4 @@ -1140,6 +1270,12 @@ L +=item @kernels = $program->kernels_in_program + +Returns all kernels successfully compiled for all devices in program. + +http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateKernelsInProgram.html + =for gengetinfo begin program_build =item $build_status = $program->build_status ($device) @@ -1276,12 +1412,17 @@ TYPE is one of C, C, C, C, C, C, C, C, C, C, C, C, C, -C, C, C or C. +C, C, C, C or C. Chars and integers (including the half type) are specified as integers, float and double as floating point values, memory/buffer/image2d/image3d -must be an object of that type or C, and sampler and event must be -objects of that type. +must be an object of that type or C, local-memory arguments are +set by specifying the size, and sampler and event must be objects of that +type. + +Setting an argument for a kernel does NOT keep a reference to the object - +for example, if you set an argument to some image object, free the image, +and call the kernel, you will run into undefined behaviour. L @@ -1380,20 +1521,24 @@ use common::sense; BEGIN { - our $VERSION = '0.92'; + our $VERSION = '0.96'; require XSLoader; XSLoader::load (__PACKAGE__, $VERSION); - @OpenCL::Buffer::ISA = - @OpenCL::Image::ISA = OpenCL::Memory::; + @OpenCL::Buffer::ISA = + @OpenCL::Image::ISA = OpenCL::Memory::; - @OpenCL::BufferObj::ISA = OpenCL::Buffer::; + @OpenCL::BufferObj::ISA = OpenCL::Buffer::; - @OpenCL::Image2D::ISA = - @OpenCL::Image3D::ISA = OpenCL::Image::; + @OpenCL::Image2D::ISA = + @OpenCL::Image3D::ISA = + @OpenCL::Image2DArray::ISA = + @OpenCL::Image1D::ISA = + @OpenCL::Image1DArray::ISA = + @OpenCL::Image1DBuffer::ISA = OpenCL::Image::; - @OpenCL::UserEvent::ISA = OpenCL::Event::; + @OpenCL::UserEvent::ISA = OpenCL::Event::; } 1;