ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/examples/juliaflight-nosharing
Revision: 1.4
Committed: Tue Oct 22 17:25:38 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +11 -8 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 sub get_context {
20 for (OpenCL::platforms) {
21 $platform = $_;
22 for ($platform->devices) {
23 $dev = $_;
24 $ctx = eval { $platform->context (undef, [$dev]) }
25 and return;
26 }
27 }
28
29 die "cannot find suitable OpenCL device\n";
30 }
31
32 get_context;
33
34 print "using ", $dev->name, "\n";
35
36 my $queue = $ctx->queue ($dev);
37
38 # now the boring opencl code
39 my $src = <<EOF;
40 kernel void
41 juliatunnel (write_only image2d_t img, float time)
42 {
43 int2 xy = (int2)(get_global_id (0), get_global_id (1));
44 float2 p = convert_float2 (xy) / $S.f * 2.f - 1.f;
45
46 float2 m = (float2)(1.f, p.y) / fabs (p.x); // tunnel
47 m.x = fabs (fmod (m.x + time * 0.05f, 4.f) - 2.f);
48
49 float2 z = m;
50 float2 c = (float2)(sin (time * 0.01133f), cos (time * 0.02521f));
51
52 for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) // standard julia
53 z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c;
54
55 float3 colour = (float3)(z.x, z.y, atan2 (z.y, z.x));
56 write_imagef (img, xy, (float4)(colour * p.x * p.x, 1.));
57 }
58 EOF
59
60 my $prog = $ctx->build_program ($src);
61 my $kernel = $prog->kernel ("juliatunnel");
62
63 my $tex = $ctx->image2d (OpenCL::MEM_WRITE_ONLY, OpenCL::RGBA, OpenCL::UNORM_INT8, $S, $S);
64
65 # program compiled, kernel ready, now draw and loop
66
67 for (my $time; ; ++$time) {
68 # configure and run our kernel
69 $kernel->setf ("mf", $tex, $time*2); # mf = memory object, float
70 $queue->nd_range_kernel ($kernel, undef, [$S, $S], undef);
71
72 # read image
73 $queue->read_image ($tex, 0, 0, 0, 0, $S, $S, 1, 0, 0, my $data);
74
75 # wait
76 $queue->finish;
77
78 # upload texture
79 glTexSubImage2D_s GL_TEXTURE_2D, 0, 0, 0, $S, $S, GL_RGBA, GL_UNSIGNED_BYTE, $data;
80
81 # now draw the texture, the defaults should be all right
82 glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
83
84 glEnable GL_TEXTURE_2D;
85 glBegin GL_QUADS;
86 glTexCoord2f 0, 1; glVertex3i -1, -1, -1;
87 glTexCoord2f 0, 0; glVertex3i 1, -1, -1;
88 glTexCoord2f 1, 0; glVertex3i 1, 1, -1;
89 glTexCoord2f 1, 1; glVertex3i -1, 1, -1;
90 glEnd;
91
92 # with glDrawPixels, you would simply do:
93 # glDrawPixels_s $S, $S, GL_RGBA, GL_UNSIGNED_BYTE, $data;
94 # and then start to worry a lot about the coordinate system
95 # used in the opencl kernel
96
97 glXSwapBuffers;
98
99 select undef, undef, undef, 1/60;
100 }