VectorC {VU} Examples
Now that a compiler for the PS2 vector units is available, what sort of new applications are possible for the vector units? Here's some of the examples we've seen game developers are implementing with VectorC {VU}:
- Clipping
- Rendering
- Prototyping
- Advanced rendering pipelines such as volumetric fog and lighting
- Particle systems (fire, smoke etc)
- Procedurally generated geometry (landscapes, flora etc)
- Physics simulation
- Collision detection
- Math libraries / Vector math libraries
- AI, pathfinding etc
- Skinning
- Clipping
We provide intrinsic functions and C++ classes with operator overloading to make writing vector code simple.
Codeplay's compilers are based on autovectorizing technology. That means it's possible to write natural C/C++ and have the compiler work out how best to combine the operations for the vector instructions of the vector unit. The Interactive Optimizer provides the means to ensure that the output assembly is performing the correct operations. Here is an example of natural C for a common function in a vector unit program:
/* Divides a vector by its magnitude */
VUfs MakeUnitVector (VUfs vec)
{
float mag;
mag = sqrt (vec.x*vec.x +
vec.y*vec.y +
vec.z*vec.z);
vec.x /= mag;
vec.y /= mag;
vec.z /= mag;
return vec;
}
Experienced programmers may know exactly what operations are required and write code in such a way that tells the compiler exactly what to do. That way the programmer knows for certain that the optimum code will result in the given vector operation, reducing the reliance on the Interactive Optimizer while still retaining the benefits of having the program in C. For this purpose, built-in Intrinsic functions may be used. The advantage of these intrinsics is that they are portable across all Codeplay's version 2.0 compilers:
VUfs u, v, w;
u = __cpi_add_4f (v, w);
Finally, the neatest way of all to write vector processing code is to create a C++ vector class and use operator overloading, here is an example of that:
VUfs __inline operator+ (VUfs a, VUfs b)
{
VUfs d;
d.x = a.x + b.x;
d.y = a.y + b.y;
d.z = a.z + b.z;
d.w = a.w + b.w;
return d;
}
Then all that's required in your vector unit programs is the following:
VUfs u, v, w;
u = v + w;
More examples
We have written some example demos which demonstrate how easy it is to write full-featured programs on the PS2 vector units by using C/C++ and VectorC {VU}.
Click here for the demos page.
PlayStation is a registered trademark of Sony Computer Entertainment Inc. |