ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/examples/juliaflight
Revision: 1.5
Committed: Tue Oct 22 17:25:38 2013 UTC (10 years, 9 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +11 -8 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 root 1.5 sub get_context {
20     for (OpenCL::platforms) {
21     $platform = $_;
22     for ($platform->devices) {
23     $dev = $_;
24     $ctx = eval { $platform->context ([OpenCL::GLX_DISPLAY_KHR, undef, OpenCL::GL_CONTEXT_KHR, undef], [$dev]) }
25     and return;
26     }
27 root 1.1 }
28 root 1.5
29     die "cannot find suitable OpenCL device\n";
30 root 1.1 }
31    
32 root 1.5 get_context;
33 root 1.1
34 root 1.4 print "using ", $dev->name, "\n";
35    
36 root 1.1 my $queue = $ctx->queue ($dev);
37    
38     # now attach an opencl image2d object to the opengl texture
39     my $tex = $ctx->gl_texture2d (OpenCL::MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, $texid);
40    
41     # now the boring opencl code
42     my $src = <<EOF;
43     kernel void
44     juliatunnel (write_only image2d_t img, float time)
45     {
46     int2 xy = (int2)(get_global_id (0), get_global_id (1));
47     float2 p = convert_float2 (xy) / $S.f * 2.f - 1.f;
48    
49     float2 m = (float2)(1.f, p.y) / fabs (p.x); // tunnel
50     m.x = fabs (fmod (m.x + time * 0.05f, 4.f) - 2.f);
51    
52     float2 z = m;
53     float2 c = (float2)(sin (time * 0.01133f), cos (time * 0.02521f));
54    
55     for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) // standard julia
56     z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c;
57    
58     float3 colour = (float3)(z.x, z.y, atan2 (z.y, z.x));
59     write_imagef (img, xy, (float4)(colour * p.x * p.x, 1.));
60     }
61     EOF
62    
63     my $prog = $ctx->build_program ($src);
64     my $kernel = $prog->kernel ("juliatunnel");
65    
66     # program compiled, kernel ready, now draw and loop
67    
68     for (my $time; ; ++$time) {
69     # acquire objects from opengl
70     $queue->acquire_gl_objects ([$tex]);
71    
72     # configure and run our kernel
73     $kernel->setf ("mf", $tex, $time*2); # mf = memory object, float
74     $queue->nd_range_kernel ($kernel, undef, [$S, $S], undef);
75    
76     # release objects to opengl again
77     $queue->release_gl_objects ([$tex]);
78    
79     # wait
80     $queue->finish;
81    
82     # now draw the texture, the defaults should be all right
83     glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
84    
85     glEnable GL_TEXTURE_2D;
86     glBegin GL_QUADS;
87     glTexCoord2f 0, 1; glVertex3i -1, -1, -1;
88     glTexCoord2f 0, 0; glVertex3i 1, -1, -1;
89     glTexCoord2f 1, 0; glVertex3i 1, 1, -1;
90     glTexCoord2f 1, 1; glVertex3i -1, 1, -1;
91     glEnd;
92    
93     glXSwapBuffers;
94    
95     select undef, undef, undef, 1/60;
96     }