ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/examples/juliaflight
Revision: 1.3
Committed: Mon Apr 30 23:36:41 2012 UTC (12 years, 5 months ago) by root
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/usr/bin/perl
2    
3     use OpenGL ":all";
4     use OpenCL;
5    
6     my $S = $ARGV[0] || 256; # window/texture size, smaller is faster
7    
8     # open a window and create a gl texture
9     OpenGL::glpOpenWindow width => $S, height => $S;
10     my $texid = glGenTextures_p 1;
11     glBindTexture GL_TEXTURE_2D, $texid;
12     glTexImage2D_c GL_TEXTURE_2D, 0, GL_RGBA8, $S, $S, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0;
13    
14     # find and use the first opencl device that let's us get a shared opengl context
15     my $platform;
16     my $dev;
17     my $ctx;
18    
19     for (OpenCL::platforms) {
20     $platform = $_;
21     for ($platform->devices) {
22     $dev = $_;
23 root 1.3 $ctx = $platform->context ([OpenCL::GLX_DISPLAY_KHR, undef, OpenCL::GL_CONTEXT_KHR, undef], [$dev])
24 root 1.1 and last;
25     }
26     }
27    
28     $ctx
29     or die "cannot find suitable OpenCL device\n";
30    
31     my $queue = $ctx->queue ($dev);
32    
33     # now attach an opencl image2d object to the opengl texture
34     my $tex = $ctx->gl_texture2d (OpenCL::MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, $texid);
35    
36     # now the boring opencl code
37     my $src = <<EOF;
38     kernel void
39     juliatunnel (write_only image2d_t img, float time)
40     {
41     int2 xy = (int2)(get_global_id (0), get_global_id (1));
42     float2 p = convert_float2 (xy) / $S.f * 2.f - 1.f;
43    
44     float2 m = (float2)(1.f, p.y) / fabs (p.x); // tunnel
45     m.x = fabs (fmod (m.x + time * 0.05f, 4.f) - 2.f);
46    
47     float2 z = m;
48     float2 c = (float2)(sin (time * 0.01133f), cos (time * 0.02521f));
49    
50     for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) // standard julia
51     z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c;
52    
53     float3 colour = (float3)(z.x, z.y, atan2 (z.y, z.x));
54     write_imagef (img, xy, (float4)(colour * p.x * p.x, 1.));
55     }
56     EOF
57    
58     my $prog = $ctx->build_program ($src);
59     my $kernel = $prog->kernel ("juliatunnel");
60    
61     # program compiled, kernel ready, now draw and loop
62    
63     for (my $time; ; ++$time) {
64     # acquire objects from opengl
65     $queue->acquire_gl_objects ([$tex]);
66    
67     # configure and run our kernel
68     $kernel->setf ("mf", $tex, $time*2); # mf = memory object, float
69     $queue->nd_range_kernel ($kernel, undef, [$S, $S], undef);
70    
71     # release objects to opengl again
72     $queue->release_gl_objects ([$tex]);
73    
74     # wait
75     $queue->finish;
76    
77     # now draw the texture, the defaults should be all right
78     glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
79    
80     glEnable GL_TEXTURE_2D;
81     glBegin GL_QUADS;
82     glTexCoord2f 0, 1; glVertex3i -1, -1, -1;
83     glTexCoord2f 0, 0; glVertex3i 1, -1, -1;
84     glTexCoord2f 1, 0; glVertex3i 1, 1, -1;
85     glTexCoord2f 1, 1; glVertex3i -1, 1, -1;
86     glEnd;
87    
88     glXSwapBuffers;
89    
90     select undef, undef, undef, 1/60;
91     }