ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.pm
(Generate patch)

Comparing OpenCL/OpenCL.pm (file contents):
Revision 1.59 by root, Wed Apr 25 20:49:13 2012 UTC vs.
Revision 1.64 by root, Mon Apr 30 23:12:43 2012 UTC

212 $ev->wait; 212 $ev->wait;
213 213
214=head2 Use the OpenGL module to share a texture between OpenCL and OpenGL and draw some julia 214=head2 Use the OpenGL module to share a texture between OpenCL and OpenGL and draw some julia
215set tunnel effect. 215set tunnel effect.
216 216
217This is quite a long example to get you going. 217This is quite a long example to get you going - you can download it from
218L<http://cvs.schmorp.de/OpenCL/examples/juliaflight>.
218 219
219 use OpenGL ":all"; 220 use OpenGL ":all";
220 use OpenCL; 221 use OpenCL;
221 222
223 my $S = $ARGV[0] || 256; # window/texture size, smaller is faster
224
222 # open a window and create a gl texture 225 # open a window and create a gl texture
223 OpenGL::glpOpenWindow width => 256, height => 256; 226 OpenGL::glpOpenWindow width => $S, height => $S;
224 my $texid = glGenTextures_p 1; 227 my $texid = glGenTextures_p 1;
225 glBindTexture GL_TEXTURE_2D, $texid; 228 glBindTexture GL_TEXTURE_2D, $texid;
226 glTexImage2D_c GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0; 229 glTexImage2D_c GL_TEXTURE_2D, 0, GL_RGBA8, $S, $S, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0;
227 230
228 # find and use the first opencl device that let's us get a shared opengl context 231 # find and use the first opencl device that let's us get a shared opengl context
229 my $platform; 232 my $platform;
230 my $dev; 233 my $dev;
231 my $ctx; 234 my $ctx;
250 # now the boring opencl code 253 # now the boring opencl code
251 my $src = <<EOF; 254 my $src = <<EOF;
252 kernel void 255 kernel void
253 juliatunnel (write_only image2d_t img, float time) 256 juliatunnel (write_only image2d_t img, float time)
254 { 257 {
255 float2 p = (float2)(get_global_id (0), get_global_id (1)) / 256.f * 2.f - 1.f; 258 int2 xy = (int2)(get_global_id (0), get_global_id (1));
259 float2 p = convert_float2 (xy) / $S.f * 2.f - 1.f;
256 260
257 float2 m = (float2)(1.f, p.y) / fabs (p.x); 261 float2 m = (float2)(1.f, p.y) / fabs (p.x); // tunnel
258 m.x = fabs (fmod (m.x + time * 0.05f, 4.f)) - 2.f; 262 m.x = fabs (fmod (m.x + time * 0.05f, 4.f) - 2.f);
259 263
260 float2 z = m; 264 float2 z = m;
261 float2 c = (float2)(sin (time * 0.05005), cos (time * 0.06001)); 265 float2 c = (float2)(sin (time * 0.01133f), cos (time * 0.02521f));
262 266
263 for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) 267 for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) // standard julia
264 z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c; 268 z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c;
265 269
266 float3 colour = (float3)(z.x, z.y, z.x * z.y); 270 float3 colour = (float3)(z.x, z.y, atan2 (z.y, z.x));
267 write_imagef (img, (int2)(get_global_id (0), get_global_id (1)), (float4)(colour * p.x * p.x, 1.)); 271 write_imagef (img, xy, (float4)(colour * p.x * p.x, 1.));
268 } 272 }
269 EOF 273 EOF
270 274
271 my $prog = $ctx->build_program ($src); 275 my $prog = $ctx->build_program ($src);
272 my $kernel = $prog->kernel ("juliatunnel"); 276 my $kernel = $prog->kernel ("juliatunnel");
276 for (my $time; ; ++$time) { 280 for (my $time; ; ++$time) {
277 # acquire objects from opengl 281 # acquire objects from opengl
278 $queue->acquire_gl_objects ([$tex]); 282 $queue->acquire_gl_objects ([$tex]);
279 283
280 # configure and run our kernel 284 # configure and run our kernel
281 $kernel->set_image2d (0, $tex); 285 $kernel->setf ("mf", $tex, $time*2); # mf = memory object, float
282 $kernel->set_float (1, $time);
283 $queue->nd_range_kernel ($kernel, undef, [256, 256], undef); 286 $queue->nd_range_kernel ($kernel, undef, [$S, $S], undef);
284 287
285 # release objects to opengl again 288 # release objects to opengl again
286 $queue->release_gl_objects ([$tex]); 289 $queue->release_gl_objects ([$tex]);
287 290
288 # wait 291 # wait
493=cut 496=cut
494 497
495package OpenCL; 498package OpenCL;
496 499
497use common::sense; 500use common::sense;
501use Carp ();
498use Async::Interrupt (); 502use Async::Interrupt ();
499 503
500our $POLL_FUNC; # set by XS 504our $POLL_FUNC; # set by XS
501 505
502BEGIN { 506BEGIN {
503 our $VERSION = '0.97'; 507 our $VERSION = '0.98';
504 508
505 require XSLoader; 509 require XSLoader;
506 XSLoader::load (__PACKAGE__, $VERSION); 510 XSLoader::load (__PACKAGE__, $VERSION);
507 511
508 @OpenCL::Platform::ISA = 512 @OpenCL::Platform::ISA =
537=item $int = OpenCL::errno 541=item $int = OpenCL::errno
538 542
539The last error returned by a function - it's only valid after an error occured 543The last error returned by a function - it's only valid after an error occured
540and before calling another OpenCL function. 544and before calling another OpenCL function.
541 545
542=item $str = OpenCL::err2str $errval 546=item $str = OpenCL::err2str [$errval]
543 547
544Comverts an error value into a human readable string. 548Converts an error value into a human readable string. IF no error value is
549given, then the last error will be used (as returned by OpenCL::errno).
545 550
546=item $str = OpenCL::enum2str $enum 551=item $str = OpenCL::enum2str $enum
547 552
548Converts most enum values (of parameter names, image format constants, 553Converts most enum values (of parameter names, image format constants,
549object types, addressing and filter modes, command types etc.) into a 554object types, addressing and filter modes, command types etc.) into a
1002=cut 1007=cut
1003 1008
1004sub OpenCL::Context::build_program { 1009sub OpenCL::Context::build_program {
1005 my ($self, $prog, $options) = @_; 1010 my ($self, $prog, $options) = @_;
1006 1011
1007 require Carp;
1008
1009 $prog = $self->program_with_source ($prog) 1012 $prog = $self->program_with_source ($prog)
1010 unless ref $prog; 1013 unless ref $prog;
1011 1014
1012 # we build separately per device so we instantly know which one failed 1015 eval { $prog->build (undef, $options); 1 }
1016 or errno == BUILD_PROGRAM_FAILURE
1017 or errno == INVALID_BINARY # workaround nvidia bug
1018 or Carp::croak "OpenCL::Context->build_program: " . err2str;
1019
1020 # we check status for all devices
1013 for my $dev ($self->devices) { 1021 for my $dev ($self->devices) {
1014 eval { $prog->build ([$dev], $options); 1 } 1022 $prog->build_status ($dev) == BUILD_SUCCESS
1015 or Carp::croak ("Building OpenCL program for device '" . $dev->name . "' failed:\n" 1023 or Carp::croak "Building OpenCL program for device '" . $dev->name . "' failed:\n"
1016 . $prog->build_log ($dev)); 1024 . $prog->build_log ($dev);
1017 } 1025 }
1018 1026
1019 $prog 1027 $prog
1020} 1028}
1021 1029
1524If a callback is specified, then it will be called when compilation is 1532If a callback is specified, then it will be called when compilation is
1525finished. Note that many OpenCL implementations block your program while 1533finished. Note that many OpenCL implementations block your program while
1526compiling whether you use a callback or not. See C<build_async> if you 1534compiling whether you use a callback or not. See C<build_async> if you
1527want to make sure the build is done in the background. 1535want to make sure the build is done in the background.
1528 1536
1529Note that some OpenCL implementations atc up badly, and don't call the 1537Note that some OpenCL implementations act up badly, and don't call the
1530callback in some error cases (but call it in others). This implementation 1538callback in some error cases (but call it in others). This implementation
1531assumes the callback will always be called, and leaks memory if this is 1539assumes the callback will always be called, and leaks memory if this is
1532not so. So best make sure you don't pass in invalid values. 1540not so. So best make sure you don't pass in invalid values.
1533 1541
1542Some implementations fail with C<OpenCL::INVALID_BINARY> when the
1543compilation state is successful but some later stage fails.
1544
1534L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html> 1545L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html>
1535 1546
1536=item $program->build_async (\@devices = undef, $options = "", $cb->($program) = undef) 1547=item $program->build_async (\@devices = undef, $options = "", $cb->($program) = undef)
1537 1548
1538Similar to C<< ->build >>, except it starts a thread, and never fails (you 1549Similar to C<< ->build >>, except it starts a thread, and never fails (you
1685=item $ulong = $kernel->private_mem_size ($device) 1696=item $ulong = $kernel->private_mem_size ($device)
1686 1697
1687Calls C<clGetKernelWorkGroupInfo> with C<CL_KERNEL_PRIVATE_MEM_SIZE> and returns the result. 1698Calls C<clGetKernelWorkGroupInfo> with C<CL_KERNEL_PRIVATE_MEM_SIZE> and returns the result.
1688 1699
1689=for gengetinfo end kernel_work_group 1700=for gengetinfo end kernel_work_group
1701
1702=item $kernel->setf ($format, ...)
1703
1704Sets the arguments of a kernel. Since OpenCL 1.1 doesn't have a generic
1705way to set arguments (and with OpenCL 1.2 it might be rather slow), you
1706need to specify a format argument, much as with C<printf>, to tell OpenCL
1707what type of argument it is.
1708
1709The format arguments are single letters:
1710
1711 c char
1712 C unsigned char
1713 s short
1714 S unsigned short
1715 i int
1716 I unsigned int
1717 l long
1718 L unsigned long
1719
1720 h half float (0..65535)
1721 f float
1722 d double
1723
1724 z local (octet size)
1725
1726 m memory object (buffer or image)
1727 a sampler
1728 e event
1729
1730Space characters in the format string are ignored.
1731
1732Example: set the arguments for a kernel that expects an int, two floats, a buffer and an image.
1733
1734 $kernel->setf ("i ff mm", 5, 0.5, 3, $buffer, $image);
1690 1735
1691=item $kernel->set_TYPE ($index, $value) 1736=item $kernel->set_TYPE ($index, $value)
1692 1737
1693=item $kernel->set_char ($index, $value) 1738=item $kernel->set_char ($index, $value)
1694 1739

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines