API agnostic systems without massive dynamic casts - possible?

Started by
12 comments, last by Narf the Mouse 11 years, 9 months ago
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 smile.png


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 smile.png
Advertisement
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.
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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)
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
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

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 "crappy middleware - do not use" list (ok, not really, but it irks me) tongue.png
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.
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.

  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.
  2. 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.
  3. 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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.
@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)

This topic is closed to new replies.

Advertisement