• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alundra

pimpl for renderer class

9 posts in this topic

Hi,

The use of virtual is not the good choice for renderer.Because of that I have tried a lot of code design to have one who is nice.

One of them is to have a local class declaration who is public and have a method who return the pointer (to get API-specific data).

 

class CTexture
{
public:
  class CTextureImpl;
public:
  CTexture();
  virtual ~CTexture();
  CTextureImpl* GetTextureImpl() const;
  ...
private:
  CTextureImpl* m_TextureImpl;
};

In the CTexture class implementation, include the good .h, use the CTextureImpl methods in CTexture.

Is it a good design to handle differents renderer ?

Thanks

Edited by Alundra
0

Share this post


Link to post
Share on other sites

Would this work for you?

 

public header

------------------

 

class CTexture; // Use forward declaration to avoid exposing the implementation.

 

class Renderer

{

public:

  // All operations on this texture will happen through the renderer.

  void DoSomethingToTexture(CTexture* tex);

};

 

private cpp/header

------------------------

 

class CTexture

{

  // Do your platform specific stuff in the cpp or private header.  This is where "Renderer" is implemented.

  LowLevelRenderer stuff;

};

 

You can avoid the clutter this way, but you have to gaurentee that the user (public) will only ever pass around the pointer.  If you want to be able to use the texture class direcitly you will have to use Pimpl or Virtuals.  Personally I would use virtuals as the cost compared to the amount of work is low.  Virtuals get expensive when the cost compared to the amount of work is high.  For instance, you'll probably have a few hundred expensive draw calls, but particles might have a million low cost operations.  So, I wouldn't use virtuals on particles.  Of course this all depends on your platform.

1

Share this post


Link to post
Share on other sites

The reason you shouldn't use virtual functions is because there is a vtable lookup for each of the virtual methods.  If you implement the pimpl idiom, then you are also doing something similar (although not exactly the same) by calling a sub-object's methods.

 

Are you CPU bound with your current solution?  Otherwise I would just use an interface if you need to, or just directly instantiate and reference your renderer directly...  That is the fastest way.

 

One final possibility is to make your client code template based, and then you can just create an instance of the client code for each renderer at runtime.  That might take a bit longer to compile, but you wouldn't get the negative performance penalties that you mentioned above.

0

Share this post


Link to post
Share on other sites
In your example, your destructor doesn't need to be virtual because you're not inheriting from this class any more.
Also, your class violates the rule of three / rule of two. If a class has a destructor, then it almost certainly requires a copy constructor and assignment operator (even if they're private and not actually implemented).
 
Another alternative to PIMPL here is to have sub-classes created via the factory pattern, and avoid virtual by casting, e.g.
 
//texture.h
class Texture
{
public:
  static Texture* Create(...);
  static void Destroy( Texture* );
  int GetFoo();
protected:
  Texture() {}
  ~Texture() {} //stop the user from creating a Texture object outside of Create/Destroy
private:
  Texture(const Texture&);
  Texture& operator=(const Texture&); //stop anyone from copying a Texture object
};
 
//texture_gl.cpp
class TextureGL : public Texture
{
public:
  int foo;
...
};
 
//forward on the Texture calls to TextureGL -- a good compiler will optimize these out so the code is perfectly efficient.
Texture* Texture::Create() { return new TextureGL; }
void Texture::Destroy( Texture* t ) { delete (TextureGL*)t; }
int Texture::GetFoo() { return ((TextureGL*)this)->foo; }
Edited by Hodgman
0

Share this post


Link to post
Share on other sites

The reason you shouldn't use virtual functions is because there is a vtable lookup for each of the virtual methods.  If you implement the pimpl idiom, then you are also doing something similar (although not exactly the same) by calling a sub-object's methods.

Another alternative to PIMPL here is to have sub-classes created via the factory pattern, and avoid virtual by casting

 

Is pimpl as bad as virtual ? This is really a bad design ?

 

Also, your class violates the rule of three / rule of two. If a class has a destructor, then it almost certainly requires a copy constructor and assignment operator (even if they're private and not actually implemented).

 

You right, this is why NonCopyable class is nice :)

0

Share this post


Link to post
Share on other sites

Is pimpl as bad as virtual ? This is really a bad design ?

Here's two examples to compare.

First, using vtables, implemented manually instead of using virtual so we can see what's going on:
class Foo;
struct Foo_VTable
{
	typedef void (Foo::*FnDoStuff)(int a, int b);
	FnDoStuff fnDoStuff;
};

class Foo
{
public:
	void DoStuff( int a, int b )
	{
		((this)->*(vtable->fnDoStuff))(a, b);
	}
protected:
	const Foo_VTable* vtable;
};

class Foo_Derived : public Foo
{
public:
	Foo_Derived() : value(0)
	{
		const static Foo_VTable s_vtable = 
		{
			(Foo_VTable::FnDoStuff)&Foo_Derived::DoStuff
		};
		vtable = &s_vtable;
	}
private:
	void DoStuff( int a, int b )
	{
		value = value * a + b;
	}
	int value;
};

void test_vtable()
{
	Foo_Derived d;
	Foo* f = &d;

	f->DoStuff(1, 2);
	//reads v  = f->vtable
	//reads fn = v->fnDoStuff
	//calls fn
	//reads/writes f->value
}
Second, using PIMPL:
class Bar_Impl;
class Bar
{
public:
	Bar();
	void DoStuff( int a, int b );
private:
	Bar_Impl* pimpl;
};

class Bar_Impl
{
public:
	Bar_Impl() : value(0) {}
	void DoStuff( int a, int b )
	{
		value = value * a + b;
	}
private:
	int value;
};
Bar::Bar() { pimpl = new Bar_Impl; }
void Bar::DoStuff( int a, int b ) { pimpl->DoStuff(a, b); }

void test_pimpl()
{
	Bar b;

	b.DoStuff(1, 2);
	//reads p = b.pmpl
	//reads/writes p->value
}
The important operations -- the memory access patterns are shown in comments in the test functions:
vtable:
	//reads v  = f->vtable
	//reads fn = v->fnDoStuff
	//calls fn
	//reads/writes f->value

pimpl:
	//reads p = b.pmpl
	//reads/writes p->value
Both of them suffer from a "double indirection".
The vtable method first has to access the object to get the address of the vtable. Only when that's complete can it access the vtable to find out which function to call. This is likely to cause a cache miss and stall the CPU.
Inside the function it accesses the 'value' member, but that won't cause a cache miss because it lives right next to the address of the vtable, which we fetched initially.

The pimpl method first has to access the wrapper object to get the address of the implementation object. Only when that's complete can it access the 'value' member. Again, this double indirection increases the chance of causing a cache-miss and stalling the CPU.
0

Share this post


Link to post
Share on other sites

There was an article floating about a while back that suggested xbox 360 code should avoid virtual function calls in C++

 

https://www.google.co.uk/search?q=xbox360+programming+virtual+function+overhead&aq=f&oq=xbox360+programming+virtual+function+overhead&aqs=chrome.0.57.12106&sourceid=chrome&ie=UTF-8

Yeah, as well as the potential cache miss still when accessing the vtable, PPC CPU's like the 360's will also suffer a guaranteed branch misprediction stall.

0

Share this post


Link to post
Share on other sites

What do you think about the method to have 2 folders and to include the good one in the project ?

It's to have the same class name, same function name, no one virtual and just some specific function to get platform data.

1

Share this post


Link to post
Share on other sites

What do you think about the method to have 2 folders and to include the good one in the project ?

It's to have the same class name, same function name, no one virtual and just some specific function to get platform data.

 

It has sufficed for me so far, for abstracting between Direct3D9 and OpenGL. In my system, the renderer to use is chosen in a CMake build script. However it is somewhat error-prone to keep the classes in sync manually, and may also necessitate a full rebuild when switching renderers.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0