<?xml version="1.0" encoding="utf-8"?>
<!-- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
-->
<?xml-stylesheet type="text/xsl" href="https://mbien.dev/roller-ui/styles/rss.xsl" media="screen"?><rss version="2.0" 
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
  <title>Michael Bien&apos;s Weblog</title>
  <link>https://mbien.dev/blog/</link>
    <atom:link rel="self" type="application/rss+xml" href="https://mbien.dev/blog/feed/entries/rss?tags=jogamp" />
  <description>don&apos;t panic</description>
  <language>en-us</language>
  <copyright>Copyright 2024</copyright>
  <lastBuildDate>Sat, 24 Aug 2024 07:57:58 +0000</lastBuildDate>
  <generator>Apache Roller 6.1.4</generator>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/java_binding_for_the_opencl</guid>
    <title>Java Binding for the OpenCL API </title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/java_binding_for_the_opencl</link>
    <pubDate>Fri, 10 Sep 2010 22:49:42 +0000</pubDate>
    <category>Java</category>
    <category>java</category>
    <category>jocl</category>
    <category>jogamp</category>
    <category>jogl</category>
    <category>opencl</category>
<description>&lt;p&gt;
I am currently working on Java Binding for the &lt;a href=&quot;http://en.wikipedia.org/wiki/Opencl&quot;&gt;OpenCL&lt;/a&gt; API using &lt;a href=&quot;https://jogamp.org/gluegen/www/&quot;&gt;GlueGen&lt;/a&gt; (as used in &lt;a href=&quot;https://jogl.jogamp.org&quot;&gt;JOGL&lt;/a&gt;, &lt;a href=&quot;https://joal.jogamp.org&quot;&gt;JOAL&lt;/a&gt;). The project started as part of my bachelor of CS thesis short after the release of the first OpenCL specification draft and is now fully feature complete with OpenCL 1.1. &lt;a href=&quot;https://jogamp.org/jocl/www/&quot;&gt;JOCL&lt;/a&gt; is currently in the stabilization phase, a beta release shouldn&apos;t be far away.
&lt;/p&gt;

&lt;h3&gt;Overview - How does it work?&lt;/h3&gt;
JOCL enables applications running on the JVM to use OpenCL for massively parallel, high performance computing tasks, executed on heterogeneous hardware (GPUs, CPUs, FPGAs etc) in a platform independent manner. JOCL consists of two parts, the low level and the high level binding.
&lt;p&gt;
The &lt;b&gt;low level bindings (LLB)&lt;/b&gt; are automatically generated using the official OpenCL &lt;a href=&quot;http://www.khronos.org/registry/cl/&quot;&gt;headers&lt;/a&gt; as input and provide a high performance, JNI based, 1:1 mapping to the C functions.
&lt;/p&gt;
&lt;p&gt;This has the following advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;reduces maintenance overhead and ensures spec conformance&lt;/li&gt;
&lt;li&gt;compiletime JNI bindings are the fastest way to access native libs from the JVM&lt;/li&gt;
&lt;li&gt;makes translating OpenCL C code into Java + JOCL very easy (e.g. from books or tutorials)&lt;/li&gt;
&lt;li&gt;flexibility and stability: OpenCL libs are loaded dynamically and accessed via function pointers&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
The hand written &lt;b&gt;high level bindings (HLB)&lt;/b&gt; is build on top of LLB and hides most boilerplate code (like object IDs, pointers and resource management) behind easy to use java objects.
HLB use direct NIO buffers internally for fast memory transfers between the JVM and the OpenCL implementation and is very GC friendly. Most of the API is designed for method chaining but of course you don&apos;t have to use it this way if you don&apos;t want to. JOCL also seamlessly integrates with JOGL 2 (both are built and tested together). Just pass the JOGL context as parameter to the JOCL context factory and you will receive a shared context. If you already know OpenCL and Java, HLB should be very intuitive for you.
&lt;/p&gt;
&lt;p&gt;
The project is available on &lt;a href=&quot;https://jocl.jogamp.org&quot;&gt;jogamp.org&lt;/a&gt;. Please use the &lt;a href=&quot;https://jogamp.org/forum.html&quot;&gt;mailinglist / forum&lt;/a&gt; for feedback or questions and the &lt;a href=&quot;https://jogamp.org/bugzilla/&quot;&gt;bugtracker&lt;/a&gt; if you experience any issues.
The JOCL &lt;a href=&quot;http://github.com/mbien/jocl&quot;&gt;root repository&lt;/a&gt; is located on github, you may also want to take a look at the &lt;a href=&quot;http://github.com/mbien/jocl-demos&quot;&gt;jocl-demos&lt;/a&gt; project. (If the demos are not enough you might also want to take a look at the junit tests)
&lt;/p&gt;

&lt;h3&gt;Screenshots (sourcecode in jocl-demos project):&lt;/h3&gt;
&lt;a href=&quot;https://jogamp.org/jocl/www/Julia3d.png&quot;&gt;
&lt;img src=&quot;https://jogamp.org/jocl/www/Julia3d_sm.png&quot; width=&quot;400&quot; height=&quot;300&quot; alt=&quot;JOCL Julia Set&quot;/&gt;
&lt;/a&gt;
&lt;img src=&quot;https://jogamp.org/jocl/www/mandelbrot64_sm.png&quot; width=&quot;256&quot; height=&quot;256&quot; alt=&quot;high precision&quot;/&gt;


&lt;p&gt;
More regarding OpenGL interoperability and other features in upcoming blog entries.
&lt;/p&gt;
&lt;p&gt;
The following sample shows basic setup, computation and cleanup using the high level APIs.
&lt;/p&gt;
&lt;h3&gt;Hello World or parallel a+b=c&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
/**
 * Hello Java OpenCL example. Adds all elements of buffer A to buffer B
 * and stores the result in buffer C.
 * Sample was inspired by the Nvidia VectorAdd example written in C/C++
 * which is bundled in the Nvidia OpenCL SDK.
 * @author Michael Bien
 */
public class HelloJOCL {

    public static void main(String[] args) throws IOException {
        // Length of arrays to process (arbitrary number)
        int elementCount = 11444777;
        // Local work size dimensions
        int localWorkSize = 256;
        // rounded up to the nearest multiple of the localWorkSize
        int globalWorkSize = roundUp(localWorkSize, elementCount);

        // setup
        CLContext context = CLContext.create();

        CLProgram program = context.createProgram(
                       HelloJOCL.class.getResourceAsStream(&quot;VectorAdd.cl&quot;)
                                 ).build();

        CLBuffer&amp;lt;FloatBuffer&amp;gt; clBufferA =
                       context.createFloatBuffer(globalWorkSize, READ_ONLY);
        CLBuffer&amp;lt;FloatBuffer&amp;gt; clBufferB =
                       context.createFloatBuffer(globalWorkSize, READ_ONLY);
        CLBuffer&amp;lt;FloatBuffer&amp;gt; clBufferC =
                       context.createFloatBuffer(globalWorkSize, WRITE_ONLY);

        out.println(&quot;used device memory: &quot;
            + (clBufferA.getSize()+clBufferB.getSize()+clBufferC.getSize())/1000000 +&quot;MB&quot;);

        // fill read buffers with random numbers (just to have test data).
        fillBuffer(clBufferA.getBuffer(), 12345);
        fillBuffer(clBufferB.getBuffer(), 67890);

        // get a reference to the kernel functon with the name &apos;VectorAdd&apos;
        // and map the buffers to its input parameters.
        CLKernel kernel = program.createCLKernel(&quot;VectorAdd&quot;);
        kernel.putArgs(clBufferA, clBufferB, clBufferC).putArg(elementCount);

        // create command queue on fastest device.
        CLCommandQueue queue = context.getMaxFlopsDevice().createCommandQueue();

        // asynchronous write to GPU device,
        // blocking read later to get the computed results back.
        long time = nanoTime();
        queue.putWriteBuffer(clBufferA, false)
             .putWriteBuffer(clBufferB, false)
             .put1DRangeKernel(kernel, 0, globalWorkSize, localWorkSize)
             .putReadBuffer(clBufferC, true);
        time = nanoTime() - time;

        // cleanup all resources associated with this context.
        context.release();

        // print first few elements of the resulting buffer to the console.
        out.println(&quot;a+b=c results snapshot: &quot;);
        for(int i = 0; i &amp;lt; 10; i++)
            out.print(clBufferC.getBuffer().get() + &quot;, &quot;);
        out.println(&quot;...; &quot; + clBufferC.getBuffer().remaining() + &quot; more&quot;);

        out.println(&quot;computation took: &quot;+(time/1000000)+&quot;ms&quot;);

    }

    private static final void fillBuffer(FloatBuffer buffer, int seed) {
        Random rnd = new Random(seed);
        while(buffer.remaining() != 0)
            buffer.put(rnd.nextFloat()*100);
        buffer.rewind();
    }

    private static final int roundUp(int groupSize, int globalSize) {
        int r = globalSize % groupSize;
        if (r == 0) {
            return globalSize;
        } else {
            return globalSize + groupSize - r;
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;VectorAdd.cl&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-opencl&quot;&gt;
    // OpenCL Kernel Function for element by element vector addition
    kernel void VectorAdd(global const float* a,
                          global const float* b,
                          global float* c, int numElements) {

        // get index into global data array
        int iGID = get_global_id(0);

        // bound check (equivalent to the limit on a &apos;for&apos; loop)
        if (iGID &amp;gt;= numElements)  {
            return;
        }

        // add the vector elements
        c[iGID] = a[iGID] + b[iGID];
    }
&lt;/code&gt;&lt;/pre&gt;
</description>  </item>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/new_getting_started_with_jogl</guid>
    <title>New Getting Started with JOGL 2 tutorials</title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/new_getting_started_with_jogl</link>
    <pubDate>Sat, 4 Sep 2010 00:22:38 +0000</pubDate>
    <category>Java</category>
    <category>java</category>
    <category>jogamp</category>
    <category>jogl</category>
    <category>opengl</category>
<description>&lt;p&gt;
Thanks to &lt;a href=&quot;https://sites.google.com/site/justinscsstuff/Home&quot;&gt;Justin Stoecker&lt;/a&gt;, computer science graduate student at the University of Miami, JOGL gets a new set of getting started tutorials:
&lt;/p&gt;
&lt;i&gt;
&lt;p&gt;
JOGL, or Java Bindings for OpenGL, allows Java programs to access the OpenGL API for graphics programming. The graphics code in JOGL programs will look almost identical to that found in C or C++ OpenGL programs, as the API is automatically generated from C header files. This is one of the greatest strengths of JOGL, as it is quite easy to port OpenGL programs written in C or C++ to JOGL; learning JOGL is essentially learning OpenGL[...]
&lt;/p&gt;
&lt;/i&gt;
&lt;h3&gt;Tutorials:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://sites.google.com/site/justinscsstuff/jogl-tutorials&quot;&gt;index&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://sites.google.com/site/justinscsstuff/jogl-tutorial-1&quot;&gt;Tutorial 1 - Environment Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://sites.google.com/site/justinscsstuff/jogl-tutorial-2&quot;&gt;Tutorial 2 - Creating a Window&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://sites.google.com/site/justinscsstuff/jogl-tutorial-3&quot;&gt;Tutorial 3 - Creating a Render Loop&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
Thanks Justin!&lt;br/&gt;</description>  </item>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/jogl_2_composeable_pipline</guid>
    <title>JOGL 2 - Composeable Pipline</title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/jogl_2_composeable_pipline</link>
    <pubDate>Wed, 25 Aug 2010 15:27:31 +0000</pubDate>
    <category>Java</category>
    <category>java</category>
    <category>jogamp</category>
    <category>jogl</category>
    <category>opengl</category>
<description>JOGL provides a feature called &apos;composeable pipeline&apos; which can be quite useful in some situations. It enables you to put additional delegating layers between your java application and the OpenGL driver.

A few usecases could be:
&lt;ul&gt;
&lt;li&gt;performance metrics&lt;/li&gt;
&lt;li&gt;logging, debugging or diagnostics&lt;/li&gt;
&lt;li&gt;to ignore specific function calls&lt;/li&gt;
&lt;/ul&gt;

It is very easy to set up. Just put this line into your code and the DebugGL layer will throw a GLException as soon an error occurs (you want this usually when you are developing the software).

&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
    public void init(GLAutoDrawable drawable) {
        // wrap composeable pipeline in a Debug utility, all OpenGL error codes are automatically
        // converted to GLExceptions as soon as they appear
        drawable.setGL(new DebugGL3(drawable.getGL().getGL3()));
        //..
    }
&lt;/code&gt;&lt;/pre&gt;

Another predefined layer is TraceGL which intercepts all OpenGL calls and prints them to an output stream.
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
        drawable.setGL(new TraceGL3(drawable.getGL().getGL3(), System.out));
&lt;/code&gt;&lt;/pre&gt;
see also &lt;a href=&quot;//mbien.dev/blog/entry/jogl_2_opengl_profiles_explained&quot;&gt;GL Profiles&lt;/a&gt;
&lt;br/&gt;</description>  </item>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/you_have_won_the_jackpot</guid>
    <title>You have won the Jackpot (3.0)</title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/you_have_won_the_jackpot</link>
    <pubDate>Thu, 3 Jun 2010 23:16:27 +0000</pubDate>
    <category>Java</category>
    <category>jackpot</category>
    <category>jogamp</category>
    <category>jogl</category>
    <category>netbeans</category>
    <category>tools</category>
<description>&lt;p&gt;
You might remember the project called &lt;b&gt;Jackpot&lt;/b&gt; which came out of SunLabs and had &lt;a href=&quot;http://nighthacks.com/roller/jag/&quot;&gt;James Gosling&lt;/a&gt; involved with. It was basically a way to declaratively define refactoring rules, allowing for example, to migrate client code between incompatible third party libraries. The project has been integrated into NetBeans as the IDE&apos;s refactoring engine since then. NetBeans 6.9 uses Jackpot for most of the in-code hints and code inspections for instance.
&lt;/p&gt;

&lt;p&gt;
There were various ways to specify the transformation rules, e.g. via a special &lt;a href=&quot;http://bitbucket.org/jlahoda/jackpot30/wiki/RulesLanguage&quot;&gt;declarative language&lt;/a&gt; or even in &lt;a href=&quot;https://lang.dev.java.net/&quot;&gt;Annotations&lt;/a&gt; directly in the library-code which would cause incompatibilities (or e.g in conjunction with @Deprecated).
&lt;/p&gt;
&lt;p&gt;
Jan Lahoda recently started with the efforts to make the project usable as standalone tool again. Jackpot 3.0 is available via 
&lt;a href=&quot;http://bitbucket.org/jlahoda/jackpot30/wiki/Home&quot;&gt;bitbucket&lt;/a&gt; for early adopters.
&lt;/p&gt;

&lt;h3&gt;Back to the Future&lt;/h3&gt;
&lt;p&gt;
I used this opportunity to test jackpotc (the jackpot compiler) with &lt;a href=&quot;http://jogamp.org&quot;&gt;JOGL&lt;/a&gt;. What I tired is to provide transformations which transform old JOGL 1.1.1 code into latest JOGL 2 compatible client code. So firstly thanks to Jan for fixing all the bugs we ran into while testing the experimental command line compiler.
&lt;/p&gt;

&lt;p&gt;
The first thing I did was to transform the code to properly use OpenGL &lt;a href=&quot;//mbien.dev/blog/entry/jogl_2_opengl_profiles_explained&quot;&gt;profiles&lt;/a&gt;. As testcode i will use the famous Gears OpenGL demo as example (but those kind of automatic transformations will only pay of if you use them on large codebases). Since it was written against JOGL 1.1.1 it can only use OpenGL up to version 2.x, which means we can simply use the GL2 profile.
&lt;/p&gt;

&lt;h3&gt;Transformation Rules&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
&apos;JOGL2 API change: javax.media.opengl.GL -&gt; javax.media.opengl.GL2&apos;:
javax.media.opengl.GL=&gt;javax.media.opengl.GL2;;

&apos;JOGL2 API change: new javax.media.opengl.GLCapabilities(javax.media.opengl.GLProfile)&apos;:
new javax.media.opengl.GLCapabilities()=&gt;
new javax.media.opengl.GLCapabilities(javax.media.opengl.GLProfile.get(javax.media.opengl.GLProfile.GL2));;

&apos;JOGL2 API change: GL gl = drawable.getGL() -&gt; GL2 gl = drawable.getGL().getGL2()&apos;:
$d.getGL() :: $d instanceof javax.media.opengl.GLAutoDrawable=&gt;
$d.getGL().getGL2();; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Just by looking at the transformation rules you can easily see that it is far more powerful than any simple text replacement could be. Jackpot uses javac and can therefore work with full qualified names, &lt;i&gt;instanceof&lt;/i&gt; and more. It will also correctly fix imports for you (there is currently a open bug in this area). The quotes are used as description string which will be printed when jackpotc runs on every code occurrence which applies to that rule.
&lt;/p&gt;

&lt;h3&gt;Invoking Jackpot&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
jackpotc -sourcepath $SRC -cp $LIBS -d $OUTPUT\
         -Ajackpot30_extra_hints=./jogl1Tojogl2.hint $FILESET
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;b&gt;$LIBS&lt;/b&gt; must contain both library versions, JOGL 1.1.1 and JOGL 2. This is not optimal but it will probably work in most situations to just use both without thinking about an particular ordering or the need to do multiple iterations.
&lt;/p&gt;

&lt;h3&gt;Results&lt;/h3&gt;
If everything runs fine the console output should look like the sample below for each transformation which applies for the given &lt;b&gt;$FILESET&lt;/b&gt;:
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
./test/oldgears/src/jogl111/gears/Gears.java:54: warning: JOGL2 API change: GL gl = drawable.getGL() -&gt; GL2 gl = drawable.getGL().getGL2()
    GL gl = drawable.getGL();
...
&lt;/code&gt;&lt;/pre&gt;
The final result is a diff patch located in &lt;b&gt;$OUTPUT/META_INF/upgrade&lt;/b&gt; called &lt;b&gt;upgrade.diff&lt;/b&gt; containing the complete changeset for the transformation. Now the only thing you have to do is to review the changes and apply them.
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
@@ -51,7 +51,7 @@
     // Use debug pipeline
     // drawable.setGL(new DebugGL(drawable.getGL()));
 
-    GL gl = drawable.getGL();
+    GL2 gl = drawable.getGL().getGL2();
...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
You can find the complete demo and all ready-to-run shellscripts in the tools/jackpotc folder inside JOGL&apos;s git &lt;a href=&quot;http://github.com/mbien/jogl&quot;&gt;repository&lt;/a&gt;. The classic JOGL 2 Gears demo can be found in form of an applet &lt;a href=&quot;http://jogamp.org/jogl-demos/www/applettest-jnlp.html&quot;&gt;here&lt;/a&gt; (uses latest hudson builds... can be unstable).
&lt;/p&gt;
&lt;p&gt;
happy coding!
&lt;/p&gt;
&lt;br/&gt;
- - - -&lt;br/&gt;
The JOGL repositories are open for contributions. If you would like to add some rules or fix other things... feel free to fork the repos on github and commit to them. (same rule applies for all &lt;a href=&quot;http://jogamp.org/&quot;&gt;JogAmp&lt;/a&gt; Projects like JOCL, JOAL, GlueGen... etc)
&lt;br/&gt;</description>  </item>
  <item>
    <guid isPermaLink="true">https://mbien.dev/blog/entry/jogamp_at_siggraph_2010</guid>
    <title>JogAmp at SIGGRAPH 2010</title>
    <dc:creator>mbien</dc:creator>
    <link>https://mbien.dev/blog/entry/jogamp_at_siggraph_2010</link>
    <pubDate>Sun, 23 May 2010 21:17:07 +0000</pubDate>
    <category>Java</category>
    <category>jocl</category>
    <category>jogamp</category>
    <category>jogl</category>
    <category>opencl</category>
    <category>opengl</category>
<description>&lt;b&gt;The &lt;a href=&quot;http://jogamp.org/&quot;&gt;JogAmp&lt;/a&gt; team will be present at &lt;a href=&quot;http://www.siggraph.org/s2010&quot;&gt;SIGGRAPH&lt;/a&gt; this year:&lt;/b&gt;
&lt;pre&gt;
3D &amp; Multimedia Across Platforms and Devices Using JOGL
Tuesday, 27 July | 4:00 PM - 6:00 PM

This session discusses the features, contributions, and future of OpenGL, OpenCL, and OpenMax
across devices and OS exposed on top of Java using the JogAmp open-source libraries.
&lt;/pre&gt;
&lt;a href=&quot;http://www.siggraph.org/s2010/for_attendees/birds_feather&quot;&gt;link to Session&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;

hope to meet you there.&lt;br/&gt;&lt;br/&gt;

&lt;b&gt;about &lt;a href=&quot;http://jogamp.org/&quot;&gt;JogAmp&lt;/a&gt;.&lt;/b&gt;&lt;br/&gt;
&lt;i&gt;JogAmp is the home of high performance Java libraries for 3D Graphics, Multimedia and Processing.
JogAmp consists currently of the projects JOGL, JOCL and JOAL which provide cross platform language bindings to the OpenGL, OpenCL, OpenAL and OpenMAX APIs.&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;

&lt;iframe width=&quot;620&quot; height=&quot;348&quot; src=&quot;https://www.youtube-nocookie.com/embed/hTNfGZTnp3c&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;br/&gt;
- - - -
&lt;br/&gt;
(yes i know i should start bogging again :))&lt;br/&gt;&lt;br/&gt;</description>  </item>
</channel>
</rss>