Posts Tagged ‘hlsl’

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: , ,

XNA Planet Custom Shader

2010/12/18 2 comments

Well I’ve been working on making my planet shaders to be something a bit more colourful.

Day and Night

First of all I wanted to map two diffuse textures that are alpha blended depending on whether the planet’s side is in the Sun’s shadow or not.  When it is you’ll see the city lights twinkle on.


Next I wanted to do some sort of atmospheric effect.  This was tricky and I’m still not absolutely happy with it – I think I need to read more on subsurface scattering.  Geometry-wise, it’s just a slightly larger sphere.  To shade it the atmospheric shell only renders when the surface normal is nearly perpendicular to the eye – in other words it only shades around the edges of the planet.  The dot product is raised to the 4th power or so to be used during the calculation for the alpha value.  The atmosphere is also emissive so as to illuminate when in shadow.

2010-12-18 vanquish planet atmosphere 1

2010-12-18 vanquish planet atmosphere 2

2010-12-18 vanquish planet atmosphere 3

2010-12-18 15-35 vanquish planet atmosphere

Categories: SpaceSim Tags: , , ,

HLSL/XNA FX Shader Editing with Lumonix ShaderFX

2009/08/23 Leave a comment

NVIDIA’s FX Composer whilst useful in certain cases is perhaps a rather daunting tool to use, especially for 3D noobs such as myself.  Sometimes I just want to make a shader for the typical things like diffuse, normal and environmental maps, or even just basic phong style materials.  Say I just modelled a tank and I want to put a camouflage texture on it, maybe some specular reflections on the glass window, and perhaps some different effects on the gun and tracks. Then say when I get more experienced I want to do spiffy vertex shaders for things like water effects.  I’m sure I can do all this in FXC but I be damned if I know how.  Did I mention I wanted to use my shader in XNA?  Well there’s a problem straight away. Many of the effects I downloaded via FXC produce annoying compile effects in Visual Studio 2008.  What is a 3D apprentice to do?

Well some brilliant chaps at LumonixBen Cloward and Kees Rijnen (Ben is the author of some great tutorials on his own site which saved my sanity) have made a 3ds Max plug-in that allows you to produce shaders (including DirectX and XNA) visually without programming. The editor appears inside Max so no more dodgy external tools and mucking about exporting and importing.  In a matter of minutes you can knock up some top-notch shaders and view them directly in 3ds Max.  The best part is that you can then export them to your favourite shader format including XNA!  There’s also support for FX Composer, DXSAS, Cgxx and many more.  And don’t think this program is only for basic effects because it’s not, there are some amazing things you can do with this gem.  Check out their site.

ShaderFX UI (my humble first shader):



An advanced shader from the ShaderFX site:



Shader export:



ShaderFX is “Free for individuals and companies smaller then 2 employees”.  You can’t beat that!

Categories: Development Tags: , ,