PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Gapi3D / Optimization Trick / Template Help?

InexorableTash · 02 Aug 2004, 01:08 UTC
Hey everyone. I thought I'd kill several birds with one stone - give an update on a project I doubt anyone cares about, share a handy trick, and ask for help on the same trick. :)

Gapi3D

About 2 years ago I had some spare time and was dorking around with Gapi3D - a Direct3D-like layer on top of GapiDraw (which is itself a DirectDraw-like layer on top of GAPI). After going idle on it for almost as long, I've recently had some cycles to poke at the project and make some progress on the list of TODO items like alpha blending.

That gets to the next point: any 3D core really needs to be heavily optimized. Just like GapiDraw, any extraneous per-pixel computations really eat up the cycles. And worse than GapiDraw, you're rarely doing simple linear walks over a source and destination buffers, so best case you're hurting at the get-go. Since it's a hobby project, I'm too lazy to code anything in ARM asm or the optimal route - self-modifying asm code. (The Vincent OpenGL-ES project uses this.)

The other common approach (used in LaMothe's "Tricks" book) is to just hand-code N different versions of all of your functions where N is the number of different render pipelines you want. (E.g. flat shaded, smooth shaded, decal textures, flat shaded textured, flat smooth shaded textures, etc.) That makes the code just hideously unmaintainable, though.

Optimization Trick

I came up with a handy trick that combines the benefit of hand-coded pipelines without the maintenance nightmare. I'm sure has been published before. Here's what the code looks like:

Code:

HRESULT OuterFunction( args )
{
DWORD dwFlags = ComputeCondensedState();

switch( dwFlags )
{
case 1: return function( 1, args ... );
case 2: return function( 2, args ... );
...
default:
Assert( "Unoptimized!" );
return function( dwFlags, args ... );
}
}

__forceinline
HRESULT function( DWORD dwFlags, args ... )
{
...
if( dwFlags & FLAG_VALUE )
{
...
}
...
}


The trick is to condense as many different pipeline paths (render states) into a single value (dwFlags). Then you pick a set of states to produce optimized compiles for, and list them as case statements.

When the compiler hits this, it will inline (thanks to __forceinline) the function which does all of the work. Since it's inlined, the dwFlags value passed in is a constant. This means that any if() statements that refer to the dwFlags value will get removed or inlined as well.

Template Help?

The problem: when too many cases are added the function containing the switch() statement gets too large (everything is inlined!) so the compiler chokes. I need a semi-clean way to have this generate a bunch of non-inlined functions. Here's what I tried using templates:

Code:

template< int dwFlagsConst > HRESULT function( int dwFlagsArg, args ... )
{
int dwFlags = dwFlagsConst == -1 ? dwFlagsArg : dwFlagsConst;

if( dwFlags == 0 )
{
OutputDebugString(TEXT(" path c \n"));
return S_OK;
}
else
{
OutputDebugString(TEXT(" path d \n"));
return E_FAIL;
}
}


While this works as expected in VC++ .NET 2003 (only the referenced code paths end up in the output), the eVC++4.0 compiler seems to get this wrong - only the last referenced version of the template code gets produced. e.g. if this is called with function<0>(0) and function<1>(1), "path d" is output twice.

Can anyone see what I'm doing wrong, or do they have alternate suggestions? Templates are the standard way of doing this and I believe this is how the GapiDraw source is set up (based on comments on the web site) so I'd like to fix this approach before trying something else (e.g. repeated inlining of the function)
InexorableTash · 02 Aug 2004, 05:23 UTC
Using a template class appears to work:

Code:

template < DWORD dwFlagsConst > class MyClass
{
public:
static HRESULT MyFunction( DWORD dwFlagsArg )
{
DWORD dwFlags = ( dwFlagsConst == -1 )
? dwFlagsArg
: dwFlagsConst;

if( dwFlags == 0 )
{
OutputDebugString(TEXT(" path a \n"));
return S_OK;
}
else
{
OutputDebugString(TEXT(" path b \n"));
return E_FAIL;
}
}
};


You then call it with:

Code:

hr = MyClass<0>::MyFunction(0);


Note that the same parameter is "passed" twice; this allows for unoptimized use of the function, when called like this:

Code:

hr = MyClass<-1>::MyFunction( dwFlags );


So the final calling code would look like:

Code:

HRESULT OuterFunction( args )
{
DWORD dwFlags = ComputeCondensedState();

switch( dwFlags )
{
case 1: return MyClass<1>::function( 1, args ... );
case 2: return MyClass<2>::function( 2, args ... );
...
default:
Assert( "Unoptimized!" );
return MyClass<-1>::function( dwFlags, args ... );
}
}


Any other insights and tips? Sorry for dragging out a non-GapiDraw-related discussion in this forum, but I enjoy lurking here.