Jump to content

  • Log In with Google      Sign In   
  • Create Account

API agnostic systems without massive dynamic casts - possible?


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
13 replies to this topic

#1 mancubit   Members   -  Reputation: 518

Like
0Likes
Like

Posted 04 July 2012 - 03:05 PM

I am currently trying to build up an api agnostic rendering engine. I do this simply for fun and I hope to learn a lot from it, so its nothing really professional or anything, but should serve as a basis for rendering tryouts or maybe a game someday.

The thing i have problems with is, how i should handle the borders between multiplattform and api specific code. I cant really find a way to avoid massive dynamic casts here. I know this may sound like premature optimization (which to a certain extend, this possible is) but as I said I want to gain experience and I dont think I have found the best possible solution yet - so I decided to ask the community Posted Image


So lets take for example the shader system:

I have a abstract base class called "Shader" which represents a single shader (vertex shader, pixel shader etc. ) and I have an abstract "Renderer" class which can set a specific shader by passing it an object of base class "Shader" like this:
[source lang="cpp"]virtual void Renderer::SetVertexShader(Shader* shader) = 0;[/source]

So lets imagine i have an api-specific shader (derived from Shader) called "ShaderDX11" and a corresponding renderer (derived from "Renderer") called "RendererDX11". RendererDX11 now implements the SetVertexShader method and performs the api-specific stuff to activate the shader.

Now I cant figure out how i could prevent a dynamic cast here to access the object "ShaderDX11" because I only have a pointer to a "Shader" object. Basically I know that this can only be an object of type "ShaderDX11", yet I dont know how i could prevent an dynamic cast everytime I set a single shader.

The thing that bothers me, is that I have to perform a dynamic cast for every single resource that interacts with api-specific code (buffers, textures, shaders, render states, etc.). Is it common practice to make massive use of dynamic casts here? Or do I just miss somthing here?

Thanks for your help Posted Image

Sponsor:

#2 teutonicus   Members   -  Reputation: 518

Like
2Likes
Like

Posted 04 July 2012 - 04:17 PM

You've only really got one graphics API per platform (except Windows, where you have to worry about D3D9/D3D11), so you switch renderer implementation at compile time. No dynamic casts. Something like:

Renderer.h
#if defined ZZZ_PLATFORM_WINDOWS
	 #include "RendererD3D9.h"
#elif defined ZZZ_PLATFORM_OSX
	 #include "RendererGL.h"
#elif
...
#endif

RendererD3D9.h
class Renderer
{
	 ...
};

RendererGL.h
class Renderer
{
	 ...
};

Create separate D3D9/D3D11 builds for Windows and select most appropriate in game's launcher.

#3 ApochPiQ   Moderators   -  Reputation: 15697

Like
5Likes
Like

Posted 04 July 2012 - 04:33 PM

Tell, don't ask.

Instead of saying "are you a DX11 shader? Oh, ok, then do this" you say "hey, I know you're some kind of shader, so do the appropriate thing for your API" and use a virtual function to have the implementation do the right thing.

#4 mhagain   Crossbones+   -  Reputation: 7960

Like
0Likes
Like

Posted 04 July 2012 - 05:05 PM

Rather than passing an actual API-specific shader or other object your API-agnostic code will define it's own API-agnostic handles. An unsigned int might be good for this as it maps nicely to a GL texture or other object name and can be used as a table lookup for D3D. Otherwise you might prefer to store them in an std::vector, return the index into the vector when you create an object, then pass the index into the vector as your param when setting/binding. If you're feeling adventurous you could even use the memory location of an object as your handle (I really don't recommend that... it would work fine though, it's just a mite hairy), whatever, just use something that's not API-specific but that be used to retrieve the actual API-specific object in your API-specific code.

Edited by mhagain, 04 July 2012 - 05:06 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Narf the Mouse   Members   -  Reputation: 318

Like
1Likes
Like

Posted 04 July 2012 - 05:58 PM

With C#, if you have something like this:
IAPIObject FactoryCreate( SomethingEnum e )
{
	 switch( e )
	 {
	 	 case SomethingEnum.DirectX : return new DirectXAPI( ) ;
	 	 case SomethingEnum.OpenGL : return new OpenGLAPI( ) ;
	 	 case SomethingEnum.CellPhoneSomething : return new CellPhoneAPI( ) ;
	 }
}
My understanding (which may be wrong) is that, due to being dynamically-compiled, it thereafter can make optimizations based on the knowledge that the IAPI object returned is a specific, identifiable object.

I could be wrong, though.

(Note: I posted about C# as you didn't put a language limitation in your post)

#6 kunos   Crossbones+   -  Reputation: 2207

Like
1Likes
Like

Posted 04 July 2012 - 11:37 PM

I don't understand why you are calling it a dynamic cast tho. You have a IRenderer interface implemented by RendererDX11 with a SetShader(Shader* shader) function. Once you are in the function you _KNOW_ that shader is a ShaderDX11 so you can static cast it or brutally C cast it. Another possibility is to have the ShaderDX11 have a pointer to the RendererDX11 passed in at creation time and a "Apply" virtual method. Something like this:

class IRenderer
{
void SetShader(IShader* shader) { shader->Apply();}
}

class RendererDX11 : IRenderer
{
void SetShader(ShaderDX11* shader) { // do your things }
}

class IShader
{
virtual void Apply()=0;
}

class ShaderDX11 : IShader
{
RendererDX11* renderer;
void Apply() { renderer->SetShader(this);}
}

Of course the problem here would be that you have 2 ways to do the same thing: IRenderer::SetShader and IShader::Apply perform the same operation. It's a shame in C++ you cannot "hide" the IShader::Apply() method to your users, in C# that would be "internal" so the only way to set the shader would be to call IRenderer::SetShader()
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#7 Hodgman   Moderators   -  Reputation: 30353

Like
3Likes
Like

Posted 04 July 2012 - 11:57 PM

I second teutonicus that you only need one API per build of your code. There's no need to support two rendering APIs within the one executable. Every time I see virtual used for this, I quietly add that code to my "crappy middleware - do not use" list (ok, not really, but it irks me) Posted Image
Another way to implement this is something like:
//renderer.h
struct Shader;
class Renderer
{
public:
  void SetVertexShader(Shader*);
protected:
  Renderer();
};

//renderer_d9.h
#include "renderer.h"
class RendererD9 : public Renderer
{
public:
  void SetVertexShader(Shader*);
private:
  Device* m_device;
};
inline void Renderer::SetVertexShader(Shader* shader)
{
  static_cast<RendererD9*>(this)->SetVertexShader( shader );
}

//shader_d9.h / shader_GL.h / etc...
struct Shader
{
  foo bar;
};

//renderer_d9.cpp
#include "renderer_d9.h"
#include "shader_d9.h"
void RendererD9::SetVertexShader(Shader* shader)
{
  m_device->SetVertexShader( shader->bar );
}

It's a shame in C++ you cannot "hide" the IShader::Apply() method to your users

It's ugly, but the C++ equivalent is to make it private and then add the authorised users as friends.

Edited by Hodgman, 04 July 2012 - 11:59 PM.


#8 L. Spiro   Crossbones+   -  Reputation: 13576

Like
2Likes
Like

Posted 05 July 2012 - 08:22 AM

I was basically going to say the same thing as above.

The last game I played that supported OpenGL and DirectX within the same executable was Unreal Tournament 2003, and that was almost 10 years ago.

And it basically makes sense.
  • Dynamic cast is something you never need to use within your entire career. I still to this day have never actually used that type of cast in code.
  • Dynamic casts are not an optimization anyway. There is a lot of overhead associated with this type of cast, and it is generally regarded as a “never use” situation.
  • Even if any specific user wanted to the option to run OpenGL or DirectX within the same executable, that user is only going to make that choice once. It is not like a users is going to suddenly decide in the middle of a game that he or she wants to switch from OpenGL to DirectX. That basically means all those virtual functions have no meaning. 10 months later you just called the same virtual functions over and over and basically just wasted time. Virtual functions make sense when the type of object might change during run-time, but, in a game, the change from DirectX to OpenGL or vice-versa is only going to happen at most once a year. Meanwhile, every frame of every day of every month of every year you are taking the hit from the same virtual function. It doesn’t make sense. Just compile a second executable as all games these days do.
And if you do decide to compile a second .EXE for each API as you should, you may find this article helpful. Trust me when I say you definitely do need a strategy to avoid spaghetti when you start compiling multiple versions of the same executable.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 web383   Members   -  Reputation: 772

Like
0Likes
Like

Posted 05 July 2012 - 12:00 PM

What Hodgman described above is known as the Pimpl idiom - http://en.wikipedia.org/wiki/Opaque_pointer

It's a nice alternative to inheriting from an abstract class.

#10 mancubit   Members   -  Reputation: 518

Like
0Likes
Like

Posted 05 July 2012 - 02:18 PM

@all: thanks - everything you say makes perfect sense. I guess i will go with preprocessor solution then (it makes even the code easier)
@L. Spiro: the link to your post is interesting, thanks for that too.
@kunos: yes you are right, dynamic casts aren't necessary, static_casts would be sufficient (anyway i want to minimize casting at all)

#11 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 05 July 2012 - 02:56 PM

I was basically going to say the same thing as above.

The last game I played that supported OpenGL and DirectX within the same executable was Unreal Tournament 2003, and that was almost 10 years ago.

And it basically makes sense.

  • Dynamic cast is something you never need to use within your entire career. I still to this day have never actually used that type of cast in code.
  • Dynamic casts are not an optimization anyway. There is a lot of overhead associated with this type of cast, and it is generally regarded as a “never use” situation.
  • Even if any specific user wanted to the option to run OpenGL or DirectX within the same executable, that user is only going to make that choice once. It is not like a users is going to suddenly decide in the middle of a game that he or she wants to switch from OpenGL to DirectX. That basically means all those virtual functions have no meaning. 10 months later you just called the same virtual functions over and over and basically just wasted time. Virtual functions make sense when the type of object might change during run-time, but, in a game, the change from DirectX to OpenGL or vice-versa is only going to happen at most once a year. Meanwhile, every frame of every day of every month of every year you are taking the hit from the same virtual function. It doesn’t make sense. Just compile a second executable as all games these days do.
And if you do decide to compile a second .EXE for each API as you should, you may find this article helpful. Trust me when I say you definitely do need a strategy to avoid spaghetti when you start compiling multiple versions of the same executable.


L. Spiro

How do you handle the user switching between APIs? The simplest method which would also allow end-user API switching would seem to be to just have the launcher swap around API.dlls - Copy the relevant API.dll from the DirectX/OpenGL/Whatever folder into the dlls folder when the user clicks "Ok".

#12 SillyCow   Members   -  Reputation: 849

Like
0Likes
Like

Posted 05 July 2012 - 03:25 PM

Did this a couple of years back (no shaders back then, so the code below is just an example).

What I did was create an API-Factory object that had a generic interface, but generated and operated on API specific objects.

something like this:

[source ]class API_Factory{ virtual void AbstractShader createShader(...)=0;}class AbstractShader{ virtual void Apply()=0;}class DX11_Factory:API_Factory{ AbstractShader createShader(...){return new DX11_Shader(this.DX11Context);}}class DX11_Shader:AbstractShader{ void Apply(){DX11Context.ActivateShader(this.blabla);};}[/source]

My new android game : Enemies of the Crown

My previous android game : Killer Bees


#13 L. Spiro   Crossbones+   -  Reputation: 13576

Like
0Likes
Like

Posted 05 July 2012 - 05:47 PM


I was basically going to say the same thing as above.

The last game I played that supported OpenGL and DirectX within the same executable was Unreal Tournament 2003, and that was almost 10 years ago.

And it basically makes sense.

  • Dynamic cast is something you never need to use within your entire career. I still to this day have never actually used that type of cast in code.
  • Dynamic casts are not an optimization anyway. There is a lot of overhead associated with this type of cast, and it is generally regarded as a “never use” situation.
  • Even if any specific user wanted to the option to run OpenGL or DirectX within the same executable, that user is only going to make that choice once. It is not like a users is going to suddenly decide in the middle of a game that he or she wants to switch from OpenGL to DirectX. That basically means all those virtual functions have no meaning. 10 months later you just called the same virtual functions over and over and basically just wasted time. Virtual functions make sense when the type of object might change during run-time, but, in a game, the change from DirectX to OpenGL or vice-versa is only going to happen at most once a year. Meanwhile, every frame of every day of every month of every year you are taking the hit from the same virtual function. It doesn’t make sense. Just compile a second executable as all games these days do.
And if you do decide to compile a second .EXE for each API as you should, you may find this article helpful. Trust me when I say you definitely do need a strategy to avoid spaghetti when you start compiling multiple versions of the same executable.


L. Spiro

How do you handle the user switching between APIs? The simplest method which would also allow end-user API switching would seem to be to just have the launcher swap around API.dlls - Copy the relevant API.dll from the DirectX/OpenGL/Whatever folder into the dlls folder when the user clicks "Ok".

The appropriate DLL’s would be copied to the install directory at install time.
You then have a launcher which allows the user to select one version or another and executes it.

Or the user can select only one version at install time and only that is installed (with some ability to change that later).

Or the launcher remembers the last choice and automatically launches it (with some way to change that in the future).

There are many ways to go about it.


L. Spiro

Edited by L. Spiro, 05 July 2012 - 05:55 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#14 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 05 July 2012 - 06:57 PM



I was basically going to say the same thing as above.

The last game I played that supported OpenGL and DirectX within the same executable was Unreal Tournament 2003, and that was almost 10 years ago.

And it basically makes sense.

  • Dynamic cast is something you never need to use within your entire career. I still to this day have never actually used that type of cast in code.
  • Dynamic casts are not an optimization anyway. There is a lot of overhead associated with this type of cast, and it is generally regarded as a “never use” situation.
  • Even if any specific user wanted to the option to run OpenGL or DirectX within the same executable, that user is only going to make that choice once. It is not like a users is going to suddenly decide in the middle of a game that he or she wants to switch from OpenGL to DirectX. That basically means all those virtual functions have no meaning. 10 months later you just called the same virtual functions over and over and basically just wasted time. Virtual functions make sense when the type of object might change during run-time, but, in a game, the change from DirectX to OpenGL or vice-versa is only going to happen at most once a year. Meanwhile, every frame of every day of every month of every year you are taking the hit from the same virtual function. It doesn’t make sense. Just compile a second executable as all games these days do.
And if you do decide to compile a second .EXE for each API as you should, you may find this article helpful. Trust me when I say you definitely do need a strategy to avoid spaghetti when you start compiling multiple versions of the same executable.


L. Spiro

How do you handle the user switching between APIs? The simplest method which would also allow end-user API switching would seem to be to just have the launcher swap around API.dlls - Copy the relevant API.dll from the DirectX/OpenGL/Whatever folder into the dlls folder when the user clicks "Ok".

The appropriate DLL’s would be copied to the install directory at install time.
You then have a launcher which allows the user to select one version or another and executes it.

Or the user can select only one version at install time and only that is installed (with some ability to change that later).

Or the launcher remembers the last choice and automatically launches it (with some way to change that in the future).

There are many ways to go about it.


L. Spiro

Thanks, sounds about what I was thinking.

*Makes a refactoring note*




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