Sign in to follow this  
mancubit

Unity API agnostic systems without massive dynamic casts - possible?

Recommended Posts

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
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
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
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
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
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
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
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
@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
[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
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
[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
[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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628354
    • Total Posts
      2982230
  • Similar Content

    • By koto
      Hello,

      I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

      The game has following features:
      - turn based
      - procedurally generated levels
      - tons of loot
      - spells system
      - trading system
      - some quests
      - some limited crafting
      - door/key/lever mechanics
      - quite a lot of different (look/behaviour) monsters

      Trailer:
      Demo: http://kotogames.com/wp-content/uploads/2017/11/OuaD_demo.zip
      Regards,
      Tom
       
    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
  • Popular Now