Sign in to follow this  
mancubit

Unity API agnostic systems without massive dynamic casts - possible?

Recommended Posts

mancubit    536
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]


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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
teutonicus    612
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
[code]
#if defined ZZZ_PLATFORM_WINDOWS
#include "RendererD3D9.h"
#elif defined ZZZ_PLATFORM_OSX
#include "RendererGL.h"
#elif
...
#endif
[/code]

RendererD3D9.h
[code]
class Renderer
{
...
};
[/code]

RendererGL.h
[code]
class Renderer
{
...
};
[/code]

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

Share this post


Link to post
Share on other sites
mhagain    13430
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 [i]really[/i] 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

Share this post


Link to post
Share on other sites
Narf the Mouse    322
With C#, if you have something like this:
[code]
IAPIObject FactoryCreate( SomethingEnum e )
{
switch( e )
{
case SomethingEnum.DirectX : return new DirectXAPI( ) ;
case SomethingEnum.OpenGL : return new OpenGLAPI( ) ;
case SomethingEnum.CellPhoneSomething : return new CellPhoneAPI( ) ;
}
}
[/code]
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)

Share this post


Link to post
Share on other sites
kunos    2254
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()

Share this post


Link to post
Share on other sites
Hodgman    51338
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 [font=courier new,courier,monospace]virtual[/font] used for this, I quietly add that code to my "[i]crappy middleware - do not use[/i]" list (ok, not really, but it irks me) [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]
Another way to implement this is something like:[code]//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 );
}[/code][quote name='kunos' timestamp='1341466642' post='4955845']
It's a shame in C++ you cannot "hide" the IShader::Apply() method to your users
[/quote]It's ugly, but the C++ equivalent is to make it private and then add the authorised users as friends. Edited by Hodgman

Share this post


Link to post
Share on other sites
L. Spiro    25638
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.[list=1]
[*]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 [i]the same virtual function[/i]. It doesn’t make sense. Just compile a second executable as all games these days do.
[/list]
And if you do decide to compile a second .EXE for each API as you should, you may find [url="http://lspiroengine.com/?p=49"]this article[/url] 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

Share this post


Link to post
Share on other sites
web383    804
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.

Share this post


Link to post
Share on other sites
mancubit    536
@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)

Share this post


Link to post
Share on other sites
Narf the Mouse    322
[quote name='L. Spiro' timestamp='1341498173' post='4955985']
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.[list=1]
[*]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 [i]the same virtual function[/i]. It doesn’t make sense. Just compile a second executable as all games these days do.
[/list]
And if you do decide to compile a second .EXE for each API as you should, you may find [url="http://lspiroengine.com/?p=49"]this article[/url] 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
[/quote]
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".

Share this post


Link to post
Share on other sites
SillyCow    1463
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]

Share this post


Link to post
Share on other sites
L. Spiro    25638
[quote name='Narf the Mouse' timestamp='1341521763' post='4956099']
[quote name='L. Spiro' timestamp='1341498173' post='4955985']
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.[list=1]
[*]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 [i]the same virtual function[/i]. It doesn’t make sense. Just compile a second executable as all games these days do.
[/list]
And if you do decide to compile a second .EXE for each API as you should, you may find [url="http://lspiroengine.com/?p=49"]this article[/url] 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
[/quote]
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".
[/quote]
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

Share this post


Link to post
Share on other sites
Narf the Mouse    322
[quote name='L. Spiro' timestamp='1341532068' post='4956152']
[quote name='Narf the Mouse' timestamp='1341521763' post='4956099']
[quote name='L. Spiro' timestamp='1341498173' post='4955985']
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.[list=1]
[*]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 [i]the same virtual function[/i]. It doesn’t make sense. Just compile a second executable as all games these days do.
[/list]
And if you do decide to compile a second .EXE for each API as you should, you may find [url="http://lspiroengine.com/?p=49"]this article[/url] 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
[/quote]
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".
[/quote]
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
[/quote]
Thanks, sounds about what I was thinking.

*Makes a refactoring note*

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