Archive for the ‘Development’ Category

n-Body Galaxy Simulation using Compute Shaders on GPGPU via Unity 3D

2014/02/01 12 comments


Following on from my prior article about GPGPU, I thought I try my hand at n-Body simulation.

Here’s my first attempt at n-Body simulation of a galaxy where n equals 10,000 stars with the algorithm running as a compute shader on the GPU.  Compute shaders are small HLSL-syntax-like kernels but can run in massive parallel compared to a regular CPU equivalent.

It’s important to remember that conventional graphics including those in games, do not use GPGPU for star fields.  Generally this is accomplished by either manipulating the star coordinates CPU-side and uploading to the GPU every frame or so; or utilise a small shader that renders a pre-uploaded buffer of stars that may or may not move over time.  In any event, it would be unlikely to perform n-Body simulation using conventional non-GPGPU means due to the slow performance.

My simulation entails n2 gravity calculations per frame or 10,0002 = 100,000,000 calculations per frame at 60 frames per second – something that is quite impossible if I attempted to do it from the CPU-side even if the GPU was still rendering the stars.

Here’s the compute kernel (note I’m hosting the Gist on GitHub as a “c” file in order to display syntax hi-lighting.  Readers should be aware the correct file extension is .compute):

Here’s my Unity MonoBehaviour controller that initialises and manages the simulation:


GPU General Purpose Computing with Unity Compute Shaders

2014/01/16 3 comments

Compute Shaders in Unity3D are a way to perform general purpose computing on a GPU card.  This is known as General Purpose computing on Graphics Processing Units or GPGPU.  This means you can use the GPU to perform operations that may not necessarily have anything to do with graphics.  e.g. protein folding simulations (ya I don’t know what that is either).  Not everything is suitable, only those operations that can run concurrently and make use of the ridiculous amount of parallelism on a GPU.

Unity’s Compute Shaders are currently limited to supporting DirectX 11 only but I suspect this will change in the future when you consider that Unity’s aim is to be cross-platform.  DX11 compute shaders is also known as DirectCompute.

Compute shaders syntax is just as per HLSL so you should feel right at home.

Compute shaders are great.  Normally from what I understand, if you had a ton of stuff to manipulate well you had to do it CPU side then upload it to the GPU – a very expensive operation not to be done frequently.

With compute shaders you merely allocate the data CPU side and send it once to the GPU.  From then on a little program (or kernel) updates the data for you and the output is passed to a shader for rendering.  All without being touched by the CPU.

Here’s my first attempt at making a GPGPU galaxy modelled with 1,000,000 stars where the stars are moved on the GPU side of things.  It’s looks more like a pizza at the moment, oh well.  I can manage 60 FPS on 1920×1080.




I’ll follow up with a non-pizza looking galaxy soon and some source code as documentation on anything GPGPU is quite rare.

Maybe if we get enough of these GPUs we can find out what “42” means?

Categories: Development Tags: , ,

The Programmer and the Paper Plane

2013/03/23 Leave a comment

This week at work we were visited by an agile expert who presented a workshop on agile task estimation techniques such as:

During the Planning Poker segment we were to use Fibonacci sequence poker cards to estimate a hypothetical project were the aim was to create a series of items made from folded paper including:

  • Simple helicopter
  • Paper plane
  • Decorated plane
  • bird
  • decorated bird
  • and so on…

Having made many Origami flapping birds, cranes, frogs, flowers when I was in my teens, I eagerly raised my hand when the question was asked:

“Has anyone ever made paper items before?”

Before I realised what was going on I was being passed a sheet of paper and told to make a paper airplane in front of everyone whilst being timed.  Normally I don’t like this level of attention but it was all in good fun.

I started off well, remembering to fold the paper in half but unfortunately the only design that came to mind was probably the most obvious from my early youth.  I continued to fold and crease, trying my best to keep it neat all awhile the imaginary tick-tick-tick from a stopwatch from afar encouraging me to go faster.

I kept at it till it resembled something that could fly at which point it was ready to give it its maiden flight.  I picked it up, raised it high above my head and carefully tossed it with all the grace I could muster into the boardroom skyline, only to watch it gently fall to the table in exactly the same way that feathers don’t. 😉

At which point someone said:

…and this is why developers should not test their own code!

To which we all including myself broke out in hysterics. Smile

Categories: Development Tags: , ,

Well No, Macs No Longer “Just Work”

2012/09/16 Leave a comment

Last year I wrote about how things changed once I bought a Mac, well it has been over a year now and I wonder how things are fairing?

Things have changed drastically during the past year and it’s quite worrisome. Let’s review that post in chunks:

  • Apple TV and iTunes on iMac just work; do not forget each other

Well no, ever since OS X Mountain Lion (ML) if my Mac goes to sleep to conserve power, it’s too dumb to restore network connections properly once the Mac comes out of sleep.  Maybe it’s my router?  Did I mention I have an Apple Airport too? So now I have to tell it not to sleep which is sort of naughty in this green day and age.

  • Canon MX850 was automatically detected over wi-fi when plugged into Airport Extreme Base Station USB socket

This still works, yay!

  • Lacie 4big Quadra 4TB RAID was automatically detected and iMac kindly asked if I wished to use it for TimeMachine
  • Pressing big blue button on Lacie launched the brilliant TimeMachine app
  • TimeMachine is fast, intuitive, reliable
    Sadly this is screwed if my Mac does to sleep; the Mac does not wait for the drive to be ready before it stupidly tries to open iTunes.  Some times I have to reboot the iMac and/or Quadra.  Did I mention Windows has no problem?
  • Apple’s iMail automatically configured e-mail server settings just by me typing in my address

I have had no reason to redo this but that’s all pretty academic since iMail/Mail is too demented to restore connectivity after a sleep.  I have to rerun Mail.  At least prior to ML you could click the little alert symbol to fix it.

  • MS Messenger for iMac (via Office Mac) is much like pre-Windows Live Essentials meaning it doesn’t have the annoying ads but more importantly, uses less computer resources like memory and CPU in wlcomm.exe

Well sure, but sadly it does not support pasting images into it plus it’s missing all the features in the Windows Live Essentials version on Windows.

  • Apple’s Xcode IDE is only $5.00 and uses minimal CPU and memory

Xcode tends to crash – a lot!  However, Xcode is continuing to grow on me and they are adding new features all the time, sadly the debugger is rather featureless compared to Microsoft Visual Studio.

  • Though Apple may make use of seemingly vertical standards such as Bonjour, historically it can be shown that Apple is more likely to adopt more popular standards with more realized use cases than say MS.  Calendering is a good example

Mail, Calenders and Chat continue to support other non-Apple tech which is nice.

I’m not sure why I said Visual Studio is bloated, particularly when such criticisms are silly when no one is stopping you to install any version whether it be Express or Ultimate or any in between.

Categories: Development, News Tags:

Check Out our New Work Office!

2012/08/28 2 comments

Our work – Primary Health Care (formerly DDI Health), moved to it’s new office in Perth, Australia and it’s fantastic!  We think it’s the best place to work at in Australia.


  • A TARDIS that opens so that you can make a phone call in privacy and comfort
  • Groovy retro meeting room complete with “Dr Evil”-looking chairs
  • Heaps of meeting rooms each with unique theme; smartboards through-out
  • A cottage/kitchen right smack in the middle
  • Homely kitchen
  • Huge outdoor veranda; remote-controlled retractable roof
  • Ergonomic tables and chairs for all
  • Forest theme
  • Ergo-smart building
  • Converts to a “nightclub” at a flick of a switch!

Retro meeting roomIMG_0608

TARDIS – yes you can go in and make phone calls in comfort and privacy!






North wing


Meeting roomIMG_0609

Arched EntranceIMG_0610

Lounge Area #1


North Wing phone booth


North wing looking back to kitchen


Balcony with electric awning retracted


South wing


South wing looking towards kitchen/cottage


outside kitchen


The kitchen


Kitchen looking towards north wing


Categories: Development Tags:

Enterprise SCM Techniques with Xcode and Subversion

2012/07/22 6 comments

Actually that title is a bit of an oxymoron, whilst Xcode can be used in the enterprise with SCM systems like Subversion (SVN), there are some things that Xcode refuses to do properly one of them being honouring SVN file locks.

SVN file locks allows you to mark files that should not be edited concurrently via the svn:needs-lock attribute.  Examples of files that you should really apply this to are:

  • *.XIB files
  • *.StoryBoard files

Now Xcode will see the SVN attribute and prompt you if you wish to “unlock” it.  It appears however that whatever Xcode is doing, it certainly is not informing the SVN server about SVN locks as other members in my team can perform the same action, even on Windows leading to false assumptions over file integrity.  Regardless of the OS, this passive lock breaks all user workflows if they attempt to check in the file due to a SVN error.


So what can be done about it?  We have a few options:

  1. Use SVN command-line tools in Terminal
  2. JetBrains AppCode – full alternative to Xcode (minus IB) + SVN client
  3. Zennaware Cornerstone
  4. Black Pixel Versions
    Any of the above tools fully honour SVN needs:lock for concurrent users on multiple machines and platforms.
    Categories: Development Tags: , ,

    Creating Render-to-Texture Secondary Framebuffer Objects on iOS Using OpenGL ES 2

    2012/05/20 8 comments

    I’ve come to greatly respect OpenGL and for which it stands as I discussed in my prior post.  OpenGL was recently added to our awareness for a project for work at a time I was dabbling with it at home.  These parallel lines of development brought excitement and mutual benefits for each locale.   The project I am working on at work depends on what is inherently gaming technology even though you would think so to look at it.

    I needed the ability to render the scene to a secondary framebuffer object (or FBO) due to the way the application incrementally draws a picture over time.  Successive scenes merely draw the remaining portions whilst being merged with previous fragments rendered in the framebuffer.   Think of the way Google Earth draws the image.

    Anyway, the code worked fine on OpenGL 3.3 but for some reason the FBO failed the completeness test on OpenGL ES 2 under iOS.  After much banging of head-to-wall I found the reason was due to the fact I was creating the secondary FBO ultimately during GLKViewController’s viewDidLoad call.  This puzzled me as I already have a OpenGL context at this time due to a:

    // We want OpenGL ES 2 thank-you very much
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    It seems creating a secondary framebuffer will fail if the size differs to the main framebuffer size.  The best way to create secondary framebuffers is to wait for a viewWillLayoutSubviews notification.  By this time your GLKit view is already resized so all you need to do is query the new size and create an appropriate FBO.

    The above controller calls into the following view for creating the framebuffer

    To draw, you must:

    1. Have the controller call into your view
    2. Bind to your secondary FBO
    3. Draw
    4. Return to the controller
    5. Have the controller rebind to the main FBO
    6. Call into your view again to draw the offscreen texture
      The final step is to draw the screen quad (steps 5-6 above)

    You might be wondering what all the bindDrawable is about.  Well normally in OpenGL you would glBindFrameBuffer(…) to your framebuffer then perform a glBindFramebuffer (…, 0) to reset to the default framebuffer – well that won’t work on OpenGL ES (well you could perhaps if you knew the actual handle).  You must use bindDrawable() instead.

    Here are some books that helped me greatly

    OpenGL® SuperBible: Comprehensive Tutorial and Reference (4th Edition)

    OpenGL® ES 2.0 Programming Guide

    Categories: Development Tags: , ,