Blackbody Rendering

December 29, 2010

In between bouts of festive over-eating I added support for blackbody emission to my fluid simulator and thought I’d describe what was involved.

Briefly, a blackbody is an idealised substance that gives off light when heated. Planck’s formula describes the intensity of light per-wavelength with units W·sr-1·m-2·m-1.

Radiance has units W·sr-1·m-2 so we need a way to convert the wavelength dependent power distribution given by Planck’s formula to a radiance value in RGB that we can use in our shader / ray-tracer.

The typical way to do this is as follows:

  1. Integrate Planck’s formula against the CIE XYZ colour matching functions (available as part of PBRT in 1nm increments)
  2. Convert from XYZ to linear sRGB (do not perform gamma correction yet)
  3. Render as normal
  4. Perform tone-mapping / gamma correction

We are throwing away spectral information by projecting into XYZ but a quick dimensional analysis shows that now we at least have the correct units (because the integration is with respect to measured in meters the extra m-1 is removed).

I was going to write more about the colour conversion process, but I didn’t want to add to the confusion out there by accidentally misusing terminology. Instead here are a couple of papers describing the conversion from Spectrum->RGB and RGB->Spectrum, questions about these come up all the time on various forums and I think these two papers do a good job of providing background and clarifying the process:

And some more general colour space links:

Here is a small sample of linear sRGB radiance values for different Blackbody temperatures:

1000K: 1.81e-02, 1.56e-04, 1.56e-04
2000K: 1.71e+03, 4.39e+02, 4.39e+02
4000K: 5.23e+05, 3.42e+05, 3.42e+05
8000K: 9.22e+06, 9.65e+06, 9.65e+06

It’s clear from the range of values that we need some sort of exposure control and tone-mapping. I simply picked a temperature in the upper end of my range (around 3000K) and scaled intensities around it before applying Reinhard tone mapping and gamma correction. You can also perform more advanced mapping by taking into account the human visual system adaptation as described in Physically Based Modeling and Animation of Fire.

Again the hardest part was setting up the simulation parameters to get the look you want, here’s one I spent at least 4 days tweaking:

Simulation time is ~30s a frame (10 substeps) on a 128^3 grid tracking temperature, fuel, smoke and velocity. Most of that time is spent in the tri-cubic interpolation during advection, I’ve been meaning to try MacCormack advection to see if it’s a net win.

There are some pretty obvious artifacts due to the tri-linear interpolation on the GPU, that would be helped by a higher resolution grid or manually performing tri-cubic in the shader.

Inspired by Kevin Beason’s work in progress videos I put together a collection of my own failed tests which I think are quite amusing:

I have to admit to being simultaneously fascinated and slightly intimidated by the fluid simulation crowd. I’ve been watching the videos on Ron Fedkiw’s page for years and am still in awe of his results, which sometimes seem little short of magic.

Recently I resolved to write my first fluid simulator and purchased a copy of Fluid Simulation for Computer Graphics by Robert Bridson.

Like a lot of developers my first exposure to the subject was Jos Stam’s stable fluids paper and his more accessible Fluid Dynamics for Games presentation, while the ideas are undeniable great I never came away feeling like I truly understood the concepts or the mathematics behind it.

I’m happy to report that Bridson’s book has helped change that. It includes a review of vector calculus in the appendix that is given in a wonderfully straight-foward and concise manner, Bridson takes almost nothing for granted and gives lots of real-world examples which helps for some of the less intuitive concepts.

I’m planning a bigger post on the subject but I thought I’d write a quick update with my progress so far.

I started out with a 2D simulation similar to Stam’s demos, having a 2D implementation that you’re confident in is really useful when you want to quickly try out different techniques and to sanity check results when things go wrong in 3D (and they will).

Before you write the 3D sim though, you need a way of visualising the data. I spent quite a while on this and implemented a single-scattering model using brute force ray-marching on the GPU.

I did some tests with a procedural pyroclastic cloud model which you can see below, this runs at around 25ms on my MacBook Pro (NVIDIA 320M) but you can dial the sample counts up and down to suit:

(videos don’t show in RSS feeds)

Here’s a simplified GLSL snippet of the volume rendering shader, it’s not at all optimised apart from some branches to skip over empty space and an assumption that absorption varies linearly with density:

uniform sampler3D g_densityTex;
uniform vec3 g_lightPos;
uniform vec3 g_lightIntensity;
uniform vec3 g_eyePos;
uniform float g_absorption;

void main()
{
	// diagonal of the cube
	const float maxDist = sqrt(3.0);

	const int numSamples = 128;
	const float scale = maxDist/float(numSamples);

	const int numLightSamples = 32;
	const float lscale = maxDist / float(numLightSamples);

	// assume all coordinates are in texture space
	vec3 pos = gl_TexCoord[0].xyz;
	vec3 eyeDir = normalize(pos-g_eyePos)*scale;

	// transmittance
	float T = 1.0;
	// in-scattered radiance
	vec3 Lo = vec3(0.0);

	for (int i=0; i < numSamples; ++i)
	{
		// sample density
		float density = texture3D(g_densityTex, pos).x;

		// skip empty space
		if (density > 0.0)
		{
			// attenuate ray-throughput
			T *= 1.0-density*scale*g_absorption;
			if (T <= 0.01)
				break;

			// point light dir in texture space
			vec3 lightDir = normalize(g_lightPos-pos)*lscale;

			// sample light
			float Tl = 1.0;	// transmittance along light ray
			vec3 lpos = pos + lightDir;

			for (int s=0; s < numLightSamples; ++s)
			{
				float ld = texture3D(g_densityTex, lpos).x;
				Tl *= 1.0-g_absorption*lscale*ld;

				if (Tl <= 0.01)
					break;

				lpos += lightDir;
			}

			vec3 Li = g_lightIntensity*Tl;

			Lo += Li*T*density*scale;
		}

		pos += eyeDir;
	}

	gl_FragColor.xyz = Lo;
	gl_FragColor.w = 1.0-T;
}

I’m pretty sure there’s a whole post on the ways this could be optimised but I’ll save that for next time. Also this example shader doesn’t have any wavelength dependent variation. Making your absorption coefficient different for each channel looks much more interesting and having a different coefficient for your primary and shadow rays also helps, you can see this effect in the videos.

To create the cloud like volume texture in OpenGL I use a displaced distance field like this (see the SIGGRAPH course for more details):

// create a volume texture with n^3 texels and base radius r
GLuint CreatePyroclasticVolume(int n, float r)
{
	GLuint texid;
	glGenTextures(1, &texid);

	GLenum target = GL_TEXTURE_3D;
	GLenum filter = GL_LINEAR;
	GLenum address = GL_CLAMP_TO_BORDER;

	glBindTexture(target, texid);

	glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
	glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);

	glTexParameteri(target, GL_TEXTURE_WRAP_S, address);
	glTexParameteri(target, GL_TEXTURE_WRAP_T, address);
	glTexParameteri(target, GL_TEXTURE_WRAP_R, address);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	byte *data = new byte[n*n*n];
	byte *ptr = data;

	float frequency = 3.0f / n;
	float center = n / 2.0f + 0.5f;

	for(int x=0; x < n; x++)
	{
		for (int y=0; y < n; ++y)
		{
			for (int z=0; z < n; ++z)
			{
				float dx = center-x;
				float dy = center-y;
				float dz = center-z;

				float off = fabsf(Perlin3D(x*frequency,
										   y*frequency,
										   z*frequency,
										   5,
										   0.5f));

				float d = sqrtf(dx*dx+dy*dy+dz*dz)/(n);

				*ptr++ = ((d-off) < r)?255:0;
			}
		}
	}

	// upload
	glTexImage3D(target,
				 0,
				 GL_LUMINANCE,
				 n,
				 n,
				 n,
				 0,
				 GL_LUMINANCE,
				 GL_UNSIGNED_BYTE,
				 data);

	delete[] data;

	return texid;
}

An excellent introduction to volume rendering is the SIGGRAPH 2010 course, Volumetric Methods in Visual Effects and Kyle Hayward’s Volume Rendering 101 for some GPU specifics.

Once I had the visualisation in place, porting the fluid simulation to 3D was actually not too difficult. I spent most of my time tweaking the initial conditions to get the smoke to behave in a way that looks interesting, you can see one of my more successful simulations below:

Currently the simulation runs entirely on the CPU using a 128^3 grid with monotonic tri-cubic interpolation and vorticity confinement as described in Visual Simulation of Smoke by Fedkiw. I’m fairly happy with the result but perhaps I have the vorticity confinement cranked a little high.

Nothing is optimised so its running at about 1.2s a frame on my 2.66ghz Core 2 MacBook.

Future work is to port the simulation to OpenCL and implement some more advanced features. Specifically I’m interested in A Vortex Particle Method for Smoke, Water and Explosions which Kevin Beason describes on his fluid page (with some great videos).

On a personal note, I resigned from LucasArts a couple of weeks ago and am looking forward to some time off back in New Zealand with my family and friends. Just in time for the Kiwi summer!

Links

GPU Gems – Fluid Simulation on the GPU
GPU Gems 3 – Real-Time Rendering and Simulation of 3D Fluids
Fluid Simulation For Computer Graphics: A Tutorial in Grid Based and Particle Based Methods

Tracing

October 3, 2010

Gregory Pakosz reminded me to write a follow up on my path tracing efforts since my last post on the subject.

It’s good timing because the friendly work-place competition between Tom and me has been in full swing. For about two weeks each of us would arrive in the morning and announce our supremacy in the speed race, only to be dethroned the next day (or a few hours later).

The great thing about ray tracing is that there are many opportunities for optimisation at all levels of computation. This keeps you “hooked” by constantly offering decent speed increases for relatively little effort.

My competitor had an existing BIH (bounding interval hierarchy) implementation that was doing a pretty good job, so I had some catching up to do. Previously I had a positive experience using a BVH (AABB tree) in a games context so decided to go that route.

Our benchmark scene was Crytek’s Sponza with the camera positioned in the center of the model looking down the z-axis. This might not be the most representative case but was good enough for comparing primary ray speeds.

Here’s a rough timeline of the performance progress (all timings were taken from my 2.6ghz i7 running 8 worker threads):

Optimisation Rays/second
Baseline (median split) 91246
Tweak compiler settings (/fp:fast /sse2 /Ot) 137486
Non-recursive traversal 145847
Traverse closest branch first 146822
Surface area heuristic 1.27589e+006
Surface area heuristic (exhaustive) 1.9375e+006
Optimized ray-AABB 2.14232e+006
VS2008 to VS2010 2.47746e+006

You can see the massive difference tree quality has on performance. What I found surprising though was the effect switching to VS2010 had, 15% faster is impressive for a single compiler revision.

I played around with a quantized BVH which reduced node size from 32 bytes to 11 but I couldn’t get the decrease in cache traffic to outweigh the cost in decoding the nodes. If anyone has had success with this I’d be interested in the details.

Algorithmically it is a uni-directional path tracer with multiple importance sampling. Of course importance sampling doesn’t make individual samples faster but allows you to take less total samples than you would have to otherwise.

So, time for some pictures:

Despite being the lowest poly models, Sponza (200k triangles) and the classroom (250k triangles) were by far the most difficult for the renderer; they both took 10+ hours and still have visible noise. In contrast the gold statuette (10 million triangles) took only 20 mins to converge!

This is mainly because the architectural models have a mixture of very large and very small polygons which creates deep trees with large nodes near the root. I think a kd-tree which splits or duplicates primitives might be more effective in this case.

A fun way to break your spatial hierarchy is simply to add a ground plane. Until I performed an exhaustive split search adding a large two triangle ground plane could slow down tracing by as much as 50%.

Of course these numbers are peanuts compared to what people are getting with GPU or SIMD packet tracers, Timo Aila reports speeds of 142 million rays/second on similar scenes using a GPU tracer in this paper.

Writing a path tracer has been a great education for me and I would encourage anyone interested in getting a better grasp on computer graphics to get a copy of PBRT and have a go at it. It’s easy to get started and seeing the finished product is hugely rewarding.

Links:

John Carmack tweeting about his experience optimising the offline global illumination calculations in RAGE.

I was surprised to learn at SIGGRAPH that Arnold (as used by Sony Pictures Imageworks) is at it’s core a uni-directional path tracer. Marcos Fajardo described some details in the Global Illumination Across Industries talk.

Mental Images iRay (their GPU based cloud renderer) looks impressive and apparently uses a single BSSRDF on all their surfaces which I guess helps simplify their GPU implementation.

Ompf.org

Model credits:

Sponza – Crytek
Classroom – LuxRender
Thai Statuette, Dragon, Bunny, Lucy – Stanford scanning repository

Faster Fog

June 10, 2010

Cedrick at Lucas suggested some nice optimisations for the in-scattering equation I posted last time.

I had left off at:

L_{s} = \frac{\sigma_{s}I}{v}( \tan^{-1}\frac{d+b}{v} - \tan^{-1}\frac{b}{v} ) \nonumber

But we can remove one of the two inverse trigonometric functions by using the following identity:

\tan^{-1}x -\tan^{-1}y = \tan^{-1}\frac{x-y}{1+xy} \nonumber

Giving us:

L_{s} = \frac{\sigma_{s}I}{v}( \tan^{-1}\frac{x-y}{1+xy} ) \nonumber

With:

x = \frac{d+b}{v}, y = \frac{b}{v}

So the updated GLSL snippet looks like:

float InScatter(vec3 start, vec3 dir, vec3 lightPos, float d)
{
   vec3 q = start - lightPos;

   // calculate coefficients
   float b = dot(dir, q);
   float c = dot(q, q);
   float s = 1.0f / sqrt(c - b*b);

   // after a little algebraic re-arrangement
   float x = d*s;
   float y = b*s;
   float l = s * atan( (x) / (1.0+(x+y)*y));

   return l;
}

Of course it’s always good to verify your ‘optimisations’, ideally I would take GPU timings but next best is to run it through NVShaderPerf and check the cycle counts:

Original (2x atan()):

Fragment Performance Setup: Driver 174.74, GPU G80, Flags 0x1000
Results 76 cycles, 10 r regs, 2,488,320,064 pixels/s

Updated (1x atan())

Fragment Performance Setup: Driver 174.74, GPU G80, Flags 0x1000
Results 55 cycles, 8 r regs, 3,251,200,103 pixels/s

A tasty 25% reduction in cycle count!

Another idea is to use an approximation of atan(), Robin Green has some great articles about faster math functions where he discusses how you can range reduce to 0-1 and approximate using minimax polynomials.

My first attempt was much simpler, looking at it’s graph we can see that atan() is almost linear near 0 and asymptotically approaches \frac{\pi}{2} .

Perhaps the simplest approximation we could try would be something like:

\tan^{-1}(x) \approx \min(x, \frac{\pi}{2})

Which looks like:

float atanLinear(float x)
{
   return clamp(x, -0.5*kPi, 0.5*kPi);
}

Fragment Performance Setup: Driver 174.74, GPU G80, Flags 0x1000
Results 34 cycles, 8 r regs, 4,991,999,816 pixels/s

Pretty ugly, but even though the maximum error here is huge (~0.43 relative), visually the difference is surprisingly small.

Still I thought I’d try for something more accurate, I used a 3rd degree minimax polynomial to approximate the range 0-1 which gave something practically identical to atan() for my purposes (~0.0052 max relative error):

float MiniMax3(float x)
{
   return ((-0.130234*x - 0.0954105)*x + 1.00712)*x - 0.00001203333;
}

float atanMiniMax3(float x)
{
   // range reduction
   if (x < 1)
      return MiniMax3(x);
   else
      return kPi*0.5 - MiniMax3(1.0/x);
}

Fragment Performance Setup: Driver 174.74, GPU G80, Flags 0x1000
Results 40 cycles, 8 r regs, 4,239,359,951 pixels/s

Disclaimer: This isn’t designed as a general replacement for atan(), for a start it doesn’t handle values of x < 0 and it hasn’t had anywhere near the love put into other approximations you can find online (optimising for floating point representations for example).

As a bonus I found that putting the polynomial evaluation into Horner form shaved 4 cycles from the shader.

Cedrick also had an idea to use something a little different:

\tan^{-1}(x) \approx \frac{\pi}{2}\frac{kx}{1+kx} .

This might look familiar to some as the basic Reinhard tone mapping curve!  We eyeballed values for k until we had one that looked close (you can tell I’m being very rigorous here), in the end k=1 was close enough and is one cycle faster :)

float atanRational(float x)
{
   return kPi*0.5*x / (1.0+x);
}

Fragment Performance Setup: Driver 174.74, GPU G80, Flags 0x1000
Results 34 cycles, 8 r regs, 4,869,120,025 pixels/s

To get it down to 34 cycles we had to expand out the expression for x and perform some more grouping of terms which shaved another cycle and a register off it.  I was surprised to see the rational approximation be so close in terms of performance to the linear one, I guess the scheduler is doing a good job at hiding some work there.

In the end all three approximations gave pretty good visual results:

Original (cycle count 76):

MiniMax3, Error 8x (cycle count 40):

Rational, Error 8x (cycle count 34):

Linear, Error 8x (cycle count 34):

Links:

http://realtimecollisiondetection.net/blog/?p=9

http://www.research.scea.com/gdc2003/fast-math-functions.html

In-Scattering Demo

May 29, 2010

This demo shows an analytic solution to the differential in-scattering equation for light in participating media. It’s a similar but simplified version of equations found in [1], [2] and as I recently discovered [3]. However I thought showing the derivation might be interesting for some out there, plus it was a good excuse for me to brush up on my \LaTeX.

You might notice I also updated the site’s theme, unfortunately you need a white background to make wordpress.com LaTeX rendering play nice with RSS feeds (other than that it’s very convenient).

Download the demo here

The demo uses GLSL and shows point and spot lights in a basic scene with some tweakable parameters:

Background

Given a view ray defined as:

\mathbf{x}(t) = \mathbf{p} + t\mathbf{d} \nonumber

We would like to know the total amount of light scattered towards the viewer (in-scattered) due to a point light source. For the purposes of this post I will only consider single scattering within isotropic media.

The differential equation that describes the change in radiance due to light scattered into the view direction inside a differential volume is given in PBRT (p578), if we assume equal scattering in all directions we can write it as:

dL_{s}(t) = \sigma_{s}L_{i}(t) dt (1)

Where \sigma_{s}   is the scattering probability which I will assume includes the normalization term for an isotropic phase funtion of \frac{1}{4\pi} . For a point light source at distance d with intensity I we can calculate the radiant intensity at a receiving point as:

L_{i} = \frac{I}{d^2}

Plugging in the equation for a point along the view ray we have:

L_{i}(t) = \frac{I}{|\mathbf{x}(t)-\mathbf{s}|^2} \nonumber

Where s is the light source position. The solution to (1) is then given by:

L_{s} = \int_{0}^{d} \sigma_{s}L_{i}(t)dt \nonumber \\

L_{s} = \int_{0}^{d} \frac{\sigma_{s}I}{|\mathbf{x}(t)-\mathbf{s}|^2}dt \nonumber

To find this integral in closed form we need to expand the distance calculation in the denominator into something we can deal with more easily:

L_{s} = \sigma_{s}I\int_{0}^{d} \frac{dt}{(\mathbf{p} + t\mathbf{d} - \mathbf{s})\cdot(\mathbf{p} + t\mathbf{d} - \mathbf{s})} \nonumber

Expanding the dot product and gathering terms, we have:

L_{s} = \sigma_{s}I\int_{0}^{d}\frac{dt}{(\mathbf{d}\cdot\mathbf{d})t^2 + 2(\mathbf{m}\cdot\mathbf{d})t + \mathbf{m}\cdot\mathbf{m} } \nonumber

Where:

\mathbf{m} = (\mathbf{p}-\mathbf{s}) \nonumber

Now we have something a bit more familiar, because the direction vector is unit length we can remove the coefficient from the quadratic term and we have:

L_{s} = \sigma_{s}I\int_{0}^{d}\frac{dt}{t^2 + 2bt + c} \nonumber

At this point you could look up the integral in standard tables but I’ll continue to simplify it for completeness.  Completing the square we obtain:

L_{s} = \sigma_{s}I\int_{0}^{d}\frac{dt}{ (t^2 + 2bt + b^2) + (c-b^2)} \nonumber

Making the substitution:

u = (t + b)     \nonumber

v = \sqrt{(c-b^2)} \nonumber

And updating our limits of integration, we have:

L_{s} = \sigma_{s}I\int_{b}^{b+d}\frac{du}{ u^2 + v^2} \nonumber

L_{s} = \sigma_{s}I \left[ \frac{1}{v}tan^{-1}\frac{u}{v} \right]_b^{b+d}\nonumber

Finally giving:

L_{s} = \frac{\sigma_{s}I}{v}( tan^{-1}\frac{d+b}{v} - tan^{-1}\frac{b}{v} ) \nonumber

This is what we will evaluate in the pixel shader, here’s the GLSL snippet for the integral evaluation (direct translation of the equation above):

float InScatter(vec3 start, vec3 dir, vec3 lightPos, float d)

// light to ray origin
vec3 q = start - lightPos;

// coefficients
float b = dot(dir, q);
float c = dot(q, q);

// evaluate integral
float s = 1.0f / sqrt(c - b*b);
float l = s * (atan( (d + b) * s) - atan( b*s ));

return l;
}

Where d is the distance traveled, computed by finding the entry / exit points of the ray with the volume.

To make the effect more interesting it is possible to incorporate a particle system, I apply the same scattering shader to each particle and treat it as a thin slab to obtain an approximate depth, then simply multiply by a noise texture at the end.

Optimisations

  • As it is above the code only supports lights with infinite extent, this implies drawing the entire frame for each light.  It would be possible to limit it to a volume but you’d want to add a falloff to the effect to avoid a sharp transition at the boundary.
  • Performing the full evaluation per-pixel for the particles is probably unnecessary, doing it at a lower frequency, per-vertex or even per-particle would probably look acceptable.

Notes

  • Generally objects appear to have wider specular highlights and more ambient lighting in the presence of particpating media.  [1] Discusses this in detail but you can fudge it by lowering the specular power in your materials as the scattering coefficient increases.
  • According to Rayliegh scattering blue light at the lower end of the spectrum is scattered considerably more than red light.  It’s simple to account for this wavelength dependence by making the scattering coefficient a constant vector weighted towards the blue component.  I found this helps add to the realism of the effect.
  • I’m curious to know how the torch light was done in Alan Wake as it seems to be high quality (not just billboards) with multiple light shafts.. maybe someone out there knows?

References

[1] Sun, B., Ramamoorthi, R., Narasimhan, S. G., and Nayar, S. K. 2005. A practical analytic single scattering model for real time rendering.

[2] Wenzel, C. 2006. Real-time atmospheric effects in games.

[3] Zhou, K., Hou, Q., Gong, M., Snyder, J., Guo, B., and Shum, H. 2007. Fogshop: Real-Time Design and Rendering of Inhomogeneous, Single-Scattering Media.

Related

[4] Engelhardt, T. and Dachsbacher, C. 2010. Epipolar sampling for shadows and crepuscular rays in participating media with single scattering.

[5] Volumetric Shadows using Polygonal Light Volumes (upcoming HPG2010)

Threading Fun

May 24, 2010

So we had an interesting threading bug at work today which I thought I’d write up here as I hadn’t seen this specific problem before (note I didn’t write this code, I merely helped debug it).  The set up was a basic single producer single consumer arrangement something like this:

#include <Windows.h>
#include <cassert>

volatile LONG gAvailable = 0;

// thread 1
DWORD WINAPI Producer(LPVOID)
{
	while (1)
	{
		InterlockedIncrement(&gAvailable);
	}
}

// thread 2
DWORD WINAPI Consumer(LPVOID)
{
	while (1)
	{
		// pull available work with a limit of 5 items per iteration
		LONG work = min(gAvailable, 5);

		// this should never fire.. right?
		assert(work <= 5);

		// update available work
		InterlockedExchangeAdd(&gAvailable, -work);
	}
}

int main(int argc, char* argv[])
{
	HANDLE h[2];

	h[0] = CreateThread(0, 0, Consumer, NULL, 0, 0);
	h[1] = CreateThread(0, 0, Producer, NULL, 0, 0);

	WaitForMultipleObjects(2, h, TRUE, INFINITE);

	return 0;
}

So where’s the problem? What would make the assert fire?

We triple-checked the logic and couldn’t see anything wrong (it was more complicated than the example above so there were a number of possible culprits) and unlike the example above there were no asserts, just a hung thread at some later stage of execution.

Unfortunately the bug reproduced only once every other week so we knew we had to fix it while I had it in a debugger. We checked all the relevant in-memory data and couldn’t see any that had obviously been overwritten (“memory stomp” is usually the first thing called out when these kinds of bugs show up).

It took us a while but eventually we checked the disassembly for the call to min(). Much to our surprise it was performing two loads of gAvailable instead of the one we had expected!

This happened to be on X360 but the same problem occurs on Win32, here’s the disassembly for the code above (VS2010 Debug):

// calculate available work with a limit of 5 items per iteration
LONG work = min(gAvailable, 5);

// (1) read gAvailable, compare against 5
002D1457  cmp         dword ptr [gAvailable (2D7140h)],5
002D145E  jge         Consumer+3Dh (2D146Dh)

// (2) read gAvailable again, store on stack
002D1460  mov         eax,dword ptr [gAvailable (2D7140h)]
002D1465  mov         dword ptr [ebp-0D0h],eax
002D146B  jmp         Consumer+47h (2D1477h)
002D146D  mov         dword ptr [ebp-0D0h],5

// (3) store gAvailable from (2) in 'work'
002D1477  mov         ecx,dword ptr [ebp-0D0h]
002D147D  mov         dword ptr [work],ecx

The question is what happens between (1) and (2)? Well the answer is that any other thread can add to gAvailable, meaning that the stored value at (3) is now > 5.

In this case the simple solution was to read gAvailable outside of the call to min():

// pull available work with a limit of 5 items per iteration
LONG available = gAvailable;
LONG work = min(available, 5);

Maybe this is obvious to some people but it sure caused me and some smart people a headache for a few hours :)

Note that you may not see the problem in some build configurations depending on whether or not the compiler generates code to perform the second read of the variable after the comparison. As far as I know there are no guarantees about what it may or may not do in this case, FWIW we had the problem in a release build with optimisations enabled.

Big props to Tom and Ruslan at Lucas for helping track this one down.

GOW III: Shadows

March 11, 2010

I checked out this session at GDC today – I’ll try and sum up the main takeaways (at least for me):

  • Artist controlled cascaded shadow maps, each cascade is accumulated into a ‘white buffer’ (new term coined?) in deferred style passes using standard PCF filtering
  • Shadow accumulation pass re-projects world space position from an FP32 depth buffer (separate from the main depth buffer). The motivation for the separate depth buffer is performance so I assume they store linear depth which means they can reconstruct the world position using just a single multiply-add (saving a reciprocal).
  • They have the ability to tile individual cascades to achieve arbitrary levels of sampling within a fixed size memory (render cascade tile, apply into white buffer, repeat)
  • Often up to 9 mega-texel resolution used for in game scenes
  • White buffer is blended to using MIN blend mode to avoid double darkening (old school)
  • Invisible ‘caster only’ geometry to make baked shadows match on dynamic objects
  • Stencil bits used to mask off baked geometry, fore-ground, back-ground characters

 
The most interesting part (in my opinion) was the optimisation work, Ben creates a light direction aligned 8x8x4 grid that he renders extruded bounding spheres into (on the SPUs). Each cell records whether or not it is in shadow and the rough bounds of that shadow. To take advantage of this information the accumulation pass (where the expensive filtering is done) breaks the screen up into tiles, checks the tile against the volume and adjusts it’s depth and 2D bounds accordingly, potentially rejecting entire tiles.

Looking forward to the the rest of the talks, this is my first year at GDC and it’s pretty great :)

Stochastic Pruning (2)

February 7, 2010

A quick update for anyone who was having problems running my stochastic pruning demo on NVIDIA cards, I’ve updated the demo with a fix (I had forgotten to disable a vertex array).

While I was at it I added some grass:

The grass uses stochastic pruning but still generates a lot of geometry, it’s just one grass tile flipped around and rendered multiple times. I wanted to see if it would be practical for games to render grass using pure geometry but really you’d need to be much more aggressive with the LOD (Update: apparently the same technique was used in Flower, see comments).

Kevin Boulanger has done some impressive real time grass rendering using 3 levels of detail with transitions. Cool stuff and quite practical by the looks of it.

Rendering plants efficiently has always been a challenge in computer graphics, a relatively new technique to address this is Pixar’s stochastic pruning algorithm. Originally developed for rendering the desert scenes in Cars, Weta also claim to have used the same technique on Avatar.

Although designed with offline rendering in mind it maps very naturally to the GPU and real-time rendering. The basic algorithm is this:

  1. Build your mesh of N elements (in the case of a tree the elements would be leaves, usually represented by quads)
  2. Sort the elements in random order (a robust way of doing this is to use the Fisher-Yates shuffle)
  3. Calculate the proportion U of elements to render based on distance to the object.
  4. Draw N*U unpruned elements with area scaled by 1/U

So putting this onto the GPU is straightforward, pre-shuffle your index buffer (element wise), when you come to draw you can calculate the unpruned element count using something like:

// calculate scaled distance to viewer
float z = max(1.0f, Length(viewerPos-objectPos)/pruneStartDistance);
// distance at which half the leaves will be pruned
float h = 2.0f;
// proportion of elements unpruned
float u = powf(z, -Log(h, 2));
// actual element count
int m = ceil(numElements * u);
// scale factor
float s = 1.0f / u;

Then just submit a modified draw call for m quads:

glDrawElements(GL_QUADS, m*4, GL_UNSIGNED_SHORT, 0);

The scale factor computed above preserves the total global surface area of all elements, this ensures consistent pixel coverage at any distance. The scaling by area can be performed efficiently in the vertex shader meaning no CPU involvement is necessary (aside from setting up the parameters of course). In a basic implementation you would see elements pop in and out as you change distance but this can be helped by having a transition window that scales elements down before they become pruned (discussed in the original paper).

Tree unpruned

Tree pruned to 10% of original

Billboards still have their place but it seems like this kind of technique could have applications for many effects, grass and particle systems being obvious ones.

I’ve updated my previous tree demo with an implementation of stochastic pruning and a few other changes:

  • Fixed some bugs with ATI driver compatability
  • Preetham based sky-dome
  • Optimised shadow map generation
  • Some new example plants
  • Tweaked leaf and branch shaders

You can download the demo here

I use the Self-organising tree models for image synthesis algorithm (from SIGGRAPH09) to generate the trees which I have posted about previously.

While I was researching I also came across Physically Guided Animation of Trees from Eurographics 2009, they have some great videos of real-time animated trees.

I’ve also posted my collection of plant modelling papers onto Mendeley (great tool for organising pdfs!).

Tree pruned to 70% of original

Sky

December 31, 2009

I had been meaning to implement Preetham’s analytic sky model ever since I first came across it years ago. Well I finally got around to it and was pleased to find it’s one of those few papers that gives you pretty much everything you need to put together an implementation (although with over 50 unique constants you need to be careful with your typing).

I integrated it into my path tracer which made for some nice images:

Also a small video.

It looks like the technique has been surpassed now by Precomputed Atmospheric Scattering but it’s still useful for generating environment maps / SH lights.

I also fixed a load of bugs in my path tracer, I was surprised to find that on my new i7 quad-core (8 logical threads) renders with 8 worker threads were only twice as fast as with a single worker, given the embarrassingly parallel nature of path-tracing you would expect at least a factor of 4 decrease in render time.

It turns out the problem was contention in the OS allocator, as I allocate BRDF objects per-intersection there was a lot of overhead there (more than I had expected). I added a per-thread memory arena where each worker thread has a pool of memory to allocate from linearly during a trace, allocations are never freed and the pool is just reset per-path.

This had the following effect on render times:

1 thread: 128709ms->35553ms (3.6x faster)
8 threads: 54071ms->8235ms (6.5x faster!)

You might also notice that the total speed up is not linear with the number of workers. It tails off as the 4 ‘real’ execution units are used up, so hyper-threading doesn’t seem to be too effective here, I suspect this is due to such simple scenes not providing enough opportunity for swapping the thread states.

The HT numbers seems to roughly agree with what people are reporting on the Ompf forums (~20% improvement).

Path Tracing

December 2, 2009

A few of us at work have been having a friendly path-tracing competition (greets to Tom & Dom). It’s been a lot of fun and comparing images in the office each Monday morning is a great motivation to get features in and renders out. I thought I’d write a post about it to record my progress and gather links to some reference material.

Here’s a list of features I’ve implemented so far and some pics below:

  • Monte-Carlo path tracing with explicit area light sampling at each step
  • Stratified image sampling
  • Importance sampled Lambert and Blinn BRDFs
  • Sphere, Plane, Disc, Metaball and Distance Field primitives (no triangles yet)
  • Multi-threaded tile renderer
  • Cross-compiles for PS3 on Linux (runs on SPUs)
  • Quite general shade-trees with Perlin noise etc




Sphere-tracing the distance fields produced some cool effects (the blobby sphere above). I first heard about the technique from Inigo Quilez who used it to generate an amazing image in his slisesix demo, he has some good descriptions on his page but for the details I would check out these papers:


And for global illumination and path-tracing in general:


Also, this is what happens when you push Perlin too far:

Trees (the green kind)

September 28, 2009

I’ve always had an interest in computer generated plants so I was pleased to read Self-organising tree models for image synthesis from Siggraph this year.

The paper basically pulls together a bunch of techniques that have been around for a while and uses them to generate some really good looking tree models.

Seeing as I’ve had a bit of time on my hands between Batman and before I start at LucasArts I decided to put together an implementation in OpenGL (being a games programmer I want realtime feedback).

Some screenshots below and a Win32 executable available – Plant.zip

tree_1
tree_2_bare
tree_2_leaves

Some Notes:

I implemented both the space colonisation and shadow propagation methods. The space colonisation is nice in that you can draw where the plant should grow by placing space samples with the mouse, this allows some pretty funky topiary but I found it difficult to grow convincing real-world plants with this method. The demo only uses the shadow propagation method.

Creating the branch geometry from generalised cylinders requires generating a continuous coordinate frame along a curve without any twists or knots. I used a parallel transport frame for this which worked out really nicely, these two papers describe the technique and the problem:

Parallel Transport Approach to Curve Framing
Quaternion Gauss Maps and Optimal Framings of Curves and Surfaces (1998)

Getting the lighting and leaf materials to look vaguely realistic took quite a lot of tweaking and I’m not totally happy with it. Until I implemented self-shadowing on the trunk and leaves it looked very weird. Also you need to account for the transmission you get through the leaves when looking toward the light:

tree_inside

There is a nice article in GPU Gems 3 on how SpeedTree do this.

The leaves are normal mapped with a simple Phong specular, I messed about with various modified diffuse models like half-Lambert but eventually just went with standard Lambert. It would be interesting to use a more sophisticated ambient term.

Still a lot of scope for performance optimisation, the leaves are alpha-tested right now so it’s doing loads of redundant fragment shader work (something like Emil Persson’s particle trimmer would be useful here).

If you want to take a look at the source code drop me an email.

Known issues:

On my NVIDIA card when the vert count is > 10^6 it runs like a dog, I need to break it up into smaller vertex buffers.

Some ATI mobile drivers don’t like the variable number of shadow mapping samples. If that’s your card then I recommend hacking the shaders to disable it.

Atomic float+

August 19, 2009

No hardware I know of has atomic floating point operations but here’s a handy little code snippet from Matt Pharr over on the PBRT mailing list which emulates the same functionality using an atomic compare and swap:

inline float AtomicAdd(volatile float *val, float delta) {
     union bits { float f; int32_t i; };
     bits oldVal, newVal;
     do {
         oldVal.f = *val;
         newVal.f = oldVal.f + delta;
     } while (AtomicCompareAndSwap(*((AtomicInt32 *)val),
         newVal.i, oldVal.i) != oldVal.i);
     return newVal.f;
}

In unrelated news, I’ve taken a job at LucasArts which I’ll be starting soon, sad to say goodbye to Rocksteady they’re a great company to work for and I’ll miss the team there.

Looking forward to San Francisco though, 12 hours closer to my home town (Auckland, New Zealand) and maybe now I can finally get along to Siggraph or GDC. If anyone has some advice on where to live there please let me know!

Also a few weeks in between jobs so hopefully time to write some code and finish off all the tourist activities we never got around to in London.

Fin!

August 14, 2009

Batman: Arkham Asylum is finished and the demo is up on PSN and Xbox Live. I was pretty much responsible for the PS3 version on the engineering side so anything wrong with it is ultimately my fault. I think most PS3 engineers working on a cross platform title will tell you that there is always some apprehension of the ‘side by side comparisons’ which are so popular these days. This one popped up pretty quickly after the demo was released:

http://www.eurogamer.net/articles/digitalfoundry-batman-demo-showdown-blog-entry

The article is quite accurate (unlike some of the comments) and it was generally very positive which is great to see as we put a lot of effort into getting parity between the two console versions.

The game has been getting a good reception which is especially nice given that Batman games have a long tradition of being terrible.

Tim Sweeney’s HPG talk

August 14, 2009

This link was going round our office, a discussion over at Lambda the Ultimate regarding Tim Sweeney’s HPG talk.

http://lambda-the-ultimate.org/node/3560

Tim chimes in a bit further down in the comments.

Code517E recently reminded me of a site I’ve used before when looking up form factors for various geometric configurations.

One I had missed the first time though is the differential element on ceiling, floor or wall to cow.

http://www.me.utexas.edu/~howell/sectionb/B-68.html

Very handy if you’re writing a farmyard simulator I’m sure.

Particle lighting

June 15, 2009

I put together an implementation of the particle shadowing technique NVIDIA showed off a while ago. My original intention was to do a survey of particle lighting techniques, in the end I just tried out two different methods that I thought sounded promising.

The first was the one ATI used in the Ruby White Out demo, the best take away from it is that they write out the min distance, max distance and density in one pass. You can do this by setting your RGB blend mode to GL_MIN, your alpha blend mode to GL_ADD and writing out r=z, g=1-z, b=0, a=density for each particle (you can reconstruct the max depth from min(1-z), think of it as the minimum distance from an end point). Here’s a screen:

smoke2

The technique needs a bit of fudging to look OK. Blur the depths, add some smoothing functions, it only works for mostly convex objects, good for amorphous blobs (clouds maybe). Performance wise it is probably the best candidate for current-gen consoles.

http://ati.amd.com/developer/gdc/2007/ArtAndTechnologyOfWhiteout(Siggraph07).pdf

IMO the NVIDIA technique is much nicer visually, it gives you fairly accurate self shadowing which looks great but is considerably more expensive. I won’t go into the implementation details too much as the paper does a pretty good job at describing it.
http://developer.download.nvidia.com/compute/cuda/sdk/website/projects/smokeParticles/doc/smokeParticles.pdf

The Nvidia demo uses 32k particles and 32 slices but you can get pretty decent results with much less. Here’s a pic of my implementation, this is running on my trusty 7600 with 1000 particles and 10 slices through the volume:

smoke1

Unfortunately you need quite a lot of quite transparent particles otherwise there are noticeable artifacts as particles change order and end up in different slices. You can improve this by using a non-linear distribution of slices so that you use more slices up front (which works nicely because the extinction for light in participating media is exponential).

Looking forward to tackling some surface shaders next.

Code charity

April 1, 2009

A friend just sent me this:

http://playpower.org/

It’s a non-profit organisation with the goal of developing educational games for developing countries that run on 8bit NES hardware. The old Nintedo chips are now patent-free and clones are very common:

nes

They’re trying to recruit programmers with a social conscience, I’m not old-school enough to know 8bit assembly but then I wouldn’t mind learning.. who needs GPUs anyway!

Red balls

April 1, 2009

A small update on my global illumination renderer, I’ve ported the radiance transfer to the GPU. It was fairly straight forward as my CPU tree structure was already set up for easy GPU traversal, basically just a matter of converting array offsets into texture coordinates and packing into an indices texture.

The hardest part is of course wrangling OpenGL to do what you want and give you a proper error message. This site is easily the best starting point I found for GPGPU stuff:

http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/tutorial.html

So here’s an image, there are 7850 surfels, it runs about 20ms on my old school NVidia 7600, so it’s still at least an order of magnitude or two slower than you would need for typical game scenes. But besides that it’s fun to pull area lights around in real time.

balls2

Not as much colour bleeding as you might expect, there is some but it is subtle.

Tree traversals

February 22, 2009

I changed my surfel renderer over to use a pre-order traversal layout for the nodes, this generally gives better cache utilisation and I did see a small speed up from using it. The layout is quite nice because to traverse your tree you just linearly iterate over your array and whenever you find a subtree you want to skip you just increment your node pointer by the size of that subtree (which is precomputed, see Real Time Collision Detection 6.6.2).

The best optimisation though comes from compacting the size of the surfel data, which again improves the cache performance. As some parts of the traversal don’t need all of the surfel data it seems to make sense to split things out, for instance to store the hierarchy information and the area seperately from the colour/irradiance information.

In fact it seems like when generalised, this idea leads you to the structure of arrays (SOA) layout, which essentially provides the finest grained breakdown where you only pull into the cache what you use and for all the nodes that you skip over there is no added cost.

I haven’t done any timings to see how much of a win this would actually be, mainly because dealing with SOA data is so damn cumbersome.

It definitely seems like something you should do after you’ve done all your hierarchy building and node shuffling which is just so much more intuitive with structures. Then you can just ‘bake’ it down to SOA format and throw it at the GPU/SIMD.

PBRT

February 22, 2009

I just bought a copy of Physically Based Rendering, I’ve been meaning to get one for ages as it’s often recommended and I thought it might be useful given my recent interest in global illumination. I’m also hoping to get a more formal background in rendering rather than the hacktastic world of real time.

The subjects covered are broad and it’s very readable. It’s my first exposure to literate programming, where essentially the book describes and contains the full implementation of a program. In fact the source code for their renderer is generated (tangled) from the definition of the book before compilation.

The only problem is the amount it weighs, I like to read on the tube but 1000 page hard backs aren’t exactly light reading.

(Almost) realtime GI

January 21, 2009

After my initial implementation of surfel based illumination I’ve extended it to do hierarchical clustering of surfels based on a similar idea to the one presented in GPU Gems 2.

A few differences:

I’m using a k-means clustering to build the approximation hierarchy bottom up. A couple of iterations of Lloyd’s algorithm provides pretty good results. Really you could get away with one iteration.

To seed the clustering I’m simply selecting every n’th surfel from the source input. At first I thought I should be choosing a random spatial distribution but it turns out clustering based on regular intervals in the mesh works well. This is because you will end up with more detail in highly tesselated places, which is what you want (assuming your input has been cleaned).

For example, a two tri wall will be clustered into one larger surfel where as a highly tesselated sphere will be broken into more clusters.

The error metric I used to do the clustering is this:

Error = (1.0f + (1.0f-Dot(surfel.normal, cluster.normal)) * Length(surfel.pos-cluster.pos)

So it’s a combination of how aligned the surfel and cluster are and how far away they are from each other. You can experiment with weighting each of those metrics individually but just summing seems to give good results.

When summing up the surfels to form your representative cluster surfel you want to:

a) sum the area
b) average the position, normal, emission and albedo weighted by area

Weighting by area is quite important and necessary for the emissive value or you’ll basically be adding energy into the simulation.

Then you get to the traversal, Bunnel recommended skipping a sub-tree of surfels if the distance to the query point is > 4*radius of the cluster surfel. That gives results practically identical to the brute force algorithm and I think you can be more agressive without losing much quality at all.

I get between a 4-6x speed up using the hierarchy over brute force. Not quite realtime yet but I haven’t optimised the tree structure at all, I’m also not running it on the GPU :)

It seems like every post I make here has to reference Christer Ericson somehow but I really recommend his book for ideas about optimising bounding volumes. Loads of good stuff in there that I have yet to implement.

Links:

Real-time Soft Shadows in Dynamic Scenes using Spherical Harmonic Exponentiation
Lightcuts: a scalable approach to illumination

Indirect illumination

January 11, 2009

It’s been a while since I checked in on the state of the art in global illumination but there is some seriously cool research happening at the moment.

I liked the basic idea Dreamworks used on Shrek2 (An Approximate Global Illumination System for Computer Generated Films) which stores direct illumination in light maps and then runs a final gather pass on that to calculate one bounce of indirect. It might be possible to adapt this to real-time if you could pre-compute and store the sample coordinates for each point..

However the current state of the art seems to be the point based approach that was first presented by Michael Bunnell with his GPU Gems 2 article Dynamic Ambient Occlusion and Indirect Lighting, he approximates the mesh as a set of oriented discs and computes the radiance transfer dynamically on the GPU.

Turns out Pixar took this idea and now use it on all their movies, Pirates of Carribean, Wall-E, etc. The technique is described here in Point Based Approximate Color Bleeding. Bunnell’s original algorithm was O(N^2) in the number of surfels but he used an clustered hierarchy to get that down to O(N.log(N)), Pixar use an Octree which stores a more accurate spherical harmonic approximation at each node.

What’s really interesting is how far Bunnell has pushed this idea, if you read through Fantasy Lab’s recent patent (August 2008), there are some really nice ideas in there that I haven’t seen published anywhere.

Here’s a summary of what’s new since the GPU Gems 2 article:

- Fixed the slightly cumbersome multiple shadow passes by summing ‘negative illumination’ from back-facing surfels
- Takes advantage of temporal coherence by simply iterating the illumination map each frame
- Added some directional information by subdividing the hemisphere into quadrants
- Threw in some nice subdivision surfaces stuff in at the end

Anyway, I knocked up a prototype of the indirect illumination technique and it seems to work quite well. The patent leaves out loads of information (and spends two pages describing what a GPU is), but it’s not too difficult to work out the details (note the form factor calculation is particularly simplified).

Here are the results from a *very* low resolution mesh, in reality you would prime your surfels with direct illumination calculated in the traditional way with shadow mapping / shaders then let the sim bounce it round but in this case I’ve done the direct lighting using his method as well.

gi_shot1

gi_shot2

Disclaimer: some artifacts are visible here due to the way illumination is baked back to the mesh and there aren’t really enough surfels to capture all the fine detail but it’s quite promising.

This seems like a perfect job for CUDA or Larrabee as the whole algorithm can be run in parallel. You can do it purely through DirectX or OpenGL but it’s kind’ve nasty.

Branch free Clamp()

January 9, 2009

One of my work mates had some code with a lot of floating point clamps in it the other day so I wrote this little branch free version using the PS3′s floating point select intrinsic:

float Clamp(float x, float lower, float upper)
{
	float t = __fsels(x-lower, x, lower);
	return __fsels(t-upper, upper, t);
}

__fsels basically does this:

float __fsels(float x, float a, float b)
{
	return (x >= 0.0f) ? a : b
}

I measured it to be 8% faster than a standard implementation, not a whole lot but quite fun to write. The SPUs have quite general selection functionality which is more useful, some stuff about it here:

http://realtimecollisiondetection.net/blog/?p=90

(Not sure about this free WordPress code formatting, I may have to move it to my own host soon)

An interesting thread going around the GDA mailing list at the moment about multithreaded programming reminded me of a little test app I wrote a while back to measure the cost of two threads accessing memory on the same cache line.

The program basically creates two threads which increment a variable a large number of times measuring the time it takes to complete with different distances between the write addresses. Something like this:

__declspec (align (512)) volatile int B[128]; 


DWORD WINAPI ThreadProc(LPVOID param)
{
	// read/write the address a whole lot
	for (int i=0; i < 10000000; ++i)
	{
		(*(volatile int*)param)++;
	}

	return 0;
}


int main()
{
	volatile int* d1 = &B[0];
	volatile int* d2 = &B[127];

	while (d1 != d2)
	{
		HANDLE threads[2];

		// QPC wrapper
		double start = GetSeconds();

		threads[0] = CreateThread(NULL, 0, ThreadProc, (void*)d1, 0, NULL);
		threads[1] = CreateThread(NULL, 0, ThreadProc, (void*)d2, 0, NULL);

		WaitForMultipleObjects(2, threads, TRUE, INFINITE);

		double end = GetSeconds();

		--d2;

		cout << (d2-d1) * sizeof(int) << "bytes apart: " << (end-start)*1000.0f << "ms" << endl;
	}
	
	int i;
	cin >> i;
	return 0;
}

On my old P4 with a 64 byte cache line these are the results:

128bytes apart: 17.4153ms
124bytes apart: 17.878ms
120bytes apart: 17.4028ms
116bytes apart: 17.3625ms
112bytes apart: 17.959ms
108bytes apart: 18.0241ms
104bytes apart: 17.2938ms
100bytes apart: 17.6643ms
96bytes apart: 17.5377ms
92bytes apart: 19.3156ms
88bytes apart: 17.2013ms
84bytes apart: 17.9361ms
80bytes apart: 17.1321ms
76bytes apart: 17.5997ms
72bytes apart: 17.4634ms
68bytes apart: 17.6562ms
64bytes apart: 17.4704ms
60bytes apart: 17.9947ms
56bytes apart: 149.759ms ***
52bytes apart: 151.64ms
48bytes apart: 150.132ms
44bytes apart: 125.318ms
40bytes apart: 160.33ms
36bytes apart: 147.889ms
32bytes apart: 152.42ms
28bytes apart: 157.003ms
24bytes apart: 149.552ms
20bytes apart: 142.372ms
16bytes apart: 136.908ms
12bytes apart: 145.691ms
8bytes apart: 146.768ms
4bytes apart: 128.408ms
0bytes apart: 125.655ms

You can see when it gets to 56 bytes there is a large penalty (9x slower!) as it brings the cache-coherency protocol into play which forces the processor to reload from main memory.

Actually it turns out this is called “false sharing” and it’s quite well known, the common solution is to pad your shared data to be at least one cache line apart.

Refs:

http://software.intel.com/en-us/articles/reduce-false-sharing-in-net/
http://www.ddj.com/embedded/196902836

More Metaballs

December 28, 2008

So after running my Metaballs demo on my girlfriends laptop it appears to be GPU limited due to fillrate. This is mainly due to the overkill number of particles in my test setup and the fact they hang round for so long, but the technique is fillrate heavy so it might be a problem.

It’d be nice to do a multithreaded CPU implementation to see how that compares, but the advantage of the current method is that it keeps the CPU free to do other things.

You could probably get more performance in some cases by uploading all the metaballs as shader parameters (or as a texture) and evaluating them directly in the pixel shader. Also I just realised I could probably render the ‘density’ texture at a lower resolution for a cheap speedup.

GPU Metaballs

December 20, 2008

I’ve been meaning to implement this idea for ages, it’s a GPU implementation of 2D metaballs. It’s very simple, very fast and doesn’t even require hardware shader support. Seems like the kind of effect that could be useful in some little game..

metaballs

It’s quite fun to play with (use left drag to rotate the emitter), so I might try and fancy it up with some better graphics and collision.

The demo is below, I’ll probably release source at some stage but at the moment it’s all tied up in my dev framework which needs to be cleaned up (the exe is also larger than it should be as I have all sorts of crap linked in).

Demo

The simulation is just done using my simple particle system but it would be fun to implement smoothed particle hydrodynamics..

Sprite mipmaps

November 3, 2008

Information on how to correctly make sprite textures is just not on the web anywhere. There are so many subtle problems with making transparent textures for use in a 3D engine, here are some of the things I learned recently from my hobby project:

Generally the easiest way is to paint on a transparent background, that is, don’t try and paint the alpha channel yourself. That’s because it’s a complete nightmare trying to match up your rgb and alpha channels correctly. This is one of the reasons why you see loads of games with dark ‘halo’s around their particle textures (although it’s not the only reason). Unless you want to get really tricky and try and paint your own pre-multiplied texture it’s not worth it, just create a new image in Photoshop with a transparent background and export as PNG.

Note: the Photoshop Targa exporter won’t export the alpha channel properly from a transparent image, it only supports alpha if you explicitly add the alpha channel to an RGB image and paint it yourself. This is a slight pain because I’ve always favoured Targa as an image file format that’s easy to read / write but in this case Photoshop drops the ball once again.

OK now you have your image exported, the image is currently NOT pre-multiplied, it may look like it in Photoshop and most image viewers but on disc it is not pre-multiplied.

Read the file into ram using libPNG or your PNG library of choice.

Generate mip maps… this is where it gets interesting. If you just call something like gluBuild2DMipmaps() your texture will be wrong, the lower level mipmaps will have a dark halo around the edges. Now what most artists do in this case is convert it to a non-transparent image and start painting in some bright colour around the edges of their sprite in the RGB channels, this is the equivalent of a really nasty filthy hack from which no good can come. You can never paint just the right colour in there and can never get it in just the right place, if you’re having this problem go talk to your coders and get them to implement a better texture import pipeline (see below):

The fix is really quite simple and you should probably have already guessed that it is to use pre-multiplied alpha. If you haven’t heard of it before read here:

Tom Forsyth’s blog

http://keithp.com/~keithp/porterduff/

Although it’s not mentioned on those sites pre-multiplication is also the solution to building mipmaps correctly. Let’s imagine you don’t pre-multiply before you downscale to build your mipmap, you have four source RGBA texels from the higher level:

t1, t2, t3, t4

In some kind of box filter arrangement, then your destination pixel in the next lower mip is computed like this:

lowermip.rgb = (t1.rgb + t2.rgb + t3.rgb + t4.rgb) / 4
lowermip.a = (t1.a + t2.a + t3.a + t4.a) /4

And when you come to render with your blend function set to GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA you get:

framebuffer.rgb = lowermip.rgb * lowermip.a + framebuffer.rgb * (1.0-lowermip.a)

It should be farily obvious why this doesn’t work, the filtered RGB channel will contain information from neighbouring pixels even if those pixels have a zero alpha. As the background in most exported source textures is black it’s the equivalent of ‘pulling in’ black to your lower mip rgb channel.

The very simple solution is to use pre-multiplication which makes your lower mip calculation like this:

lowermip.rgb = (t1.rgb*t1.a + t2.rgb*t2.a + t3.rgb*t3.a + t4.rgb*t4.a) / 4;
lowermip.a = (t1.a + t2.a + t3.a + t4.a) / 4;

So now you’re weighting each pixels contribution to your lower mip by the appropriate alpha value. And when you render you set your blend modes to GL_ONE, GL_INV_SRC_ALPHA. Not only do your mips work correctly you can now ‘mix’ additive and subtractive blending which is great for fire turning into smoke for instance (see references below).

You don’t need to export your image pre-multiplied, I fix mine up at import / asset cooking time which is probably preferable so you can keep working on the PNG source asset in a convenient way.

This is really just the basics and mip-map filtering has had a lot of other good stuff written about it:

http://number-none.com/product/Mipmapping,%20Part%201/index.html
http://number-none.com/product/Mipmapping,%20Part%202/index.html

Also see http://code.google.com/p/nvidia-texture-tools/ for some image manipulation source code (the Nvidia Photoshop DDS plugin also has an option to modulate your image by alpha when generating mip-maps).

This is a great little paper about soft-edged particles and pre-multiplied alpha (even though it’s not referred too as such)
Soft Edges and Burning Things: Enhanced Real-Time Rendering of Particle Systems

iPhone GL specs

September 6, 2008

So I’ve been trying to find out a bit more about the iPhone’s capabilities, this site has some good details. No shaders, roughly enough power for 15000 lit triangles @ 30hz. Not too shabby, I wonder about the power consumption and if certain hardware paths drain the battery faster than others. It would be nice to make something that people can play for more than 3 hours.

So the word on the street is that O2 are bringing out a pre-pay iPhone in December, I’m currently debating whether to wait for that or just get an iPod touch which you can still develop for using pretty much the same feature set.

Constraining

July 20, 2008

I am no expert when it comes to physics programming (for that see Christer Ericson’s blog) but like lots of games programmers I have written a few physics demos using Verlet integration and point masses.

So that’s nothing new, but a while back I came across a really nice approach for iterative constraint solving (which is the common approach in Verlet engines).  Usually you would formulate your constraint function and solve it directly, for example the classic distance constraint would look like:

error = restlength - |A-B|

In the case of the distance constraint, fixing this error is straight forward, you simply adjust A and B along their axis by error/2 and -error/2 respectively.

For more complicated constraints things can become difficult, because although it can be easy to calculate the error it is not always obvious how to correct for it.

Generally what you want to do is move along the derivative of your constraint function, ie: minimize the error using something like Newton’s method. This works pretty fine and provided you do enough iterations will converge quite quickly. However finding the derivative itself can be problematic, let’s take the example of an angular constraint function:

Given three points and a rest angle (the angle between the two line segments) this function will the return the current error:

Error(A, B, C) = restangle - acos(dot(B-A/|B-A|, C-B/|C-B|))

Now you have the error, how do you move the three points to correct for it? Well you need to calculate the partial derivative of the error function with respect to each component of A, B, C.  To do this symbolically is a pain, I tried plugging it into Mathematica and got a huge mess before eventually doing it by hand which was very tedious given my rusty calculus.

But here’s another way, if you can’t calculate the derivative directly you can at least approximate it using finite differences. This turns out to be sweet because all of a sudden you only have to write your error function and you’re done.  Here’s an example in case that’s not clear:

First of all, calculate the current error, I’ll stick with the angular constraint example which takes three points.

currentError = Error(A, B, C)

Now calculate the finite difference for each component of the three points by evaluating the error at some small step away from the current position, here h is our step size and A, B and C are two-vectors but the same thing applies to other dimensions:

X = {h, 0}
Y = {0, h}


derivA.x = (Error(A + X, B, C) - currentError)/h
derivA.y = (Error(A + Y, B, C) - currentError)/h


derivB.x = (Error(A, B + X, C) - currentError)/h
derivB.y = (Error(A, B + Y, C) - currentError)/h


derivC.x = (Error(A, B, C + X) - currentError)/h
derivC.y = (Error(A, B, C + Y) - currentError)/h

Now you just need to scale according to the current error and relative masses of the points and apply each derivative to each point respectively.

So there you have it, you’ve solved the constraint with no more information than a way of measuring the current error. Because this is less direct it is also generally more costly but can be a great for prototyping new or more complex constraints.

For more information see the following:

http://www.beosil.com/download/PositionBasedDynamics_VRIPHYS06.pdf
http://www.bulletphysics.com/Bullet/wordpress/bullet
http://www.gamasutra.com/resource_guide/20030121/jacobson_01.shtml

While I was at Sony I spent a lot of time thinking about making tasks run concurrently and how they should be scheduled to maximise performance.

Recently this kind of analysis has been spilling over into my everyday life and you start seeing ways you could parallelise all sorts of tasks.  Of course this is just common sense and something we all do naturally do different degrees.

A simple example is making a cup of tea, you don’t want to get the tea bags and the cups first, no, that would be a waste of precious milliseconds.  First you switch on the jug and then get to work on the rest of the process in parallel.  I imagine anyone who has worked in a kitchen before would be experts at this kind of scheduling and could probably do my job better than I can.

New job

July 17, 2008

So I took a new job today, working for Rocksteady in London.  Quite exciting, they use Unreal3 tech which I’ve always respected (and stolen ideas from).  It’s a big game title and technically I don’t know what it is.. but it’s a big franchise and should do well.

Plus working with friends in Kentish Town, s’all good.

Follow

Get every new post delivered to your Inbox.