Professional Documents
Culture Documents
GDC2006 - C++ For Next Gen Consoles
GDC2006 - C++ For Next Gen Consoles
GDC2006 - C++ For Next Gen Consoles
Consider replacing
virtual void Draw() = 0;
With
Xbox360.cpp: void Draw() { ... }
Windows.cpp: void Draw() { ... }
PS3.cpp: void Draw() { ... }
Maximize Leaf Functions
Leaf functions don’t call other functions, ever
If a potential leaf function calls another
function, the high-level function:
Is much less likely to be inlined
Must set up a stack frame
Must set up registers
Potential solutions
Remove the inner function completely
Inline the inner function
Provide two versions of the outer function
Unroll Inner Loops
Compiler can’t unroll loops where n is variable
Even unrolling from ++i to i+=4 can be a
significant gain
Eliminates three branch instructions
Increases opportunity for code scheduling
// unrolled
e = a.end();
for( i=a.beg(); i!=e; i+=4 ) {
process(i); process(i+1);
process(i+2); process(i+3);
}
Pass Native Types by Value
Tradition says that “large” types are passed
by pointer or reference, but be careful
New consoles have really large registers
Native types include
64-bit int (__int64)
VMX vector (__vector4) – 128 bits!
Quaternion q;
s.push_back( k );
if( (float)i > f )
obj->Draw();
GameObject arr[1000];
a = b + c;
i++;
C++ is Dangerous
With power comes responsibility
Beware constructors
Is initialization the right thing to do?
Beware hidden allocations
Conversion casts may have significant cost
Use virtual functions with care
Beware overloaded operators
Stick to known idioms
Operator++ should be a constant-time operation.
Really.
Summary
There absolutely are many things you can
do to efficiently program next-gen consoles
Two key issues: L2/memory and in-order
processing
Treat memory as you would a hard disk
Watch out for those branches; use tricks like fsel