arrow_back_ios Back to List

PPU virtual methods on SPU

Offload KB - features

Old Content Alert

Please note that this is a old document archive and the will most likely be out-dated or superseded by various other products and is purely here for historical purposes.

Whatever your view on the use of virtual functions in game code - they can and are frequently used (even if just for corner cases). Offload fully supports virtual functions on the SPU.


#include <liboffload>

struct Foo
{
	virtual void f();
	virtual void f(int);
};

static void func(Foo* foo)
{
	__blockingoffload(foo)
	{
		foo->f();
		foo->f(42);
	};
}

In the above example, the virtual functions f are being called from the SPU, and will result in a runtime fail. The Offload compiler will warn on compile that the virtual functions are not present in the function domain, but will halt execution at runtime when it cannot resolve these functions. Offload requires virtual functions to be explicitly added to a function domain, a comma separated list of pointers that could be called within the Offload context (See /kb/132.html for further details on domains).

#include <liboffload>

struct Foo
{
	virtual void f();
	virtual void f(int);
};

static void func(Foo* foo)
{
	__blockingoffload[Foo::f]()
	{
		foo->f();
		foo->f(42);
	};
}

As seen in the example above, the Offload block has extra information added to it. The function domain is specified by using square brackets surrounding a comma separated list of functions.


#include <liboffload>

struct Foo
{
	virtual void f();
	virtual void f(int);
	virtual int g();
};

static void func(Foo* foo)
{
	__blockingoffload[Foo::f, Foo::g]()
	{
		foo->f();
		foo->f(42);
		int bar = foo->g();
	};
}

Adding in new virtual functions to the function domain is simple!Virtual functions used on local data on the SPU require a slightly modified syntax to work.


#include <liboffload>

struct Foo
{
	virtual void f();
	virtual void f(int);
	virtual int g();
};

static void func()
{
	__blockingoffload[Foo::f this, Foo::g this]()
	{
		Foo foo;
		foo.f();
		foo.f(42);
		int bar = foo.g();
	};
}

As can be seen in the above example, the function domain pointers are now followed by the keyword this - which denotes that the virtual pointers work on local SPU data.