Jump to content

  • Log In with Google      Sign In   
  • Create Account

pimpl for renderer class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Alundra   Members   -  Reputation: 868

Like
0Likes
Like

Posted 18 March 2013 - 09:52 PM

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, 18 March 2013 - 09:57 PM.


Sponsor:

#2 allingm   Members   -  Reputation: 452

Like
1Likes
Like

Posted 18 March 2013 - 11:38 PM

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.



#3 Jason Z   Crossbones+   -  Reputation: 5086

Like
0Likes
Like

Posted 19 March 2013 - 05:59 AM

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.



#4 Hodgman   Moderators   -  Reputation: 30415

Like
0Likes
Like

Posted 19 March 2013 - 06:29 AM

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, 19 March 2013 - 06:31 AM.


#5 Alundra   Members   -  Reputation: 868

Like
0Likes
Like

Posted 19 March 2013 - 07:20 PM

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 :)



#6 Hodgman   Moderators   -  Reputation: 30415

Like
0Likes
Like

Posted 19 March 2013 - 07:44 PM

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.

#7 ray_intellect   Crossbones+   -  Reputation: 258

Like
1Likes
Like

Posted 20 March 2013 - 03:03 PM

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


Edited by ray_intellect, 20 March 2013 - 05:03 PM.


#8 Hodgman   Moderators   -  Reputation: 30415

Like
0Likes
Like

Posted 20 March 2013 - 05:55 PM

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.



#9 Alundra   Members   -  Reputation: 868

Like
1Likes
Like

Posted 21 March 2013 - 07:52 PM

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.



#10 AgentC   Members   -  Reputation: 1352

Like
0Likes
Like

Posted 22 March 2013 - 12:52 PM

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.


Every time you add a boolean member variable, God kills a kitten. Every time you create a Manager class, God kills a kitten. Every time you create a Singleton...

Urho3D (engine)  Hessian (C64 game project)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS