Posts

Showing posts from May, 2011

Gymboree Starship

The prototype can be seen on Caspian's T-Shirt from Gymboree. _ ,'|`. / | \ / | \ /. / \ .\ /. / \ .\ /. | | .\ /| /_____|___|_____\ |\ || | | -= =- | | || .|| ,-'| | | |`-. ||. | `-'___| | | |___`-' | |________| | =---= | |________| | | | | | |___H___| | | [| | |] | |__|___|___|__| \ / \ / ...

C++ Optimization Trick

Maybe this is old hat, but when I was writing a software rasterizer several years ago I came up with a trick for getting optimized variations for different render states (textured, flat shaded, Gouraud shaded, wireframe, z-write, z-test, cull-ccw, etc) without hand-writing multiple permutations of the rasterizer as is recommended by LaMothe and others. It requires an optimizing C++ compiler that supports template functions. The trick is to pack as much state as possible into a single POD value like a 32-bit integer, write your function so that all of the if() tests for render state look at the integer for branching decisions, and call a template function that takes this value as a template parameter in a constant context like a switch statement. Here's the base function full of nasty branches: result_t func(int flags, const data_t& data) { if (flags & OPT_A) { // ... } if (flags & OPT_B) { if (flags & OPT_C) { // ... } else { // ... }...