Jump to content

  • Log In with Google      Sign In   
  • Create Account

Platform-agnostic renderer


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

#1 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 11:07 AM

I'm working on a platform-agnostic engine, and I've hit a bit of "coder's block"... When working with plain old DirectX, SlimDX or XNA, the conventional approach to rendering a 3D object is to make a call to the object's "Draw" method and pass it an instance of the D3D Device... But in a multi-platform engine that can work with OpenGL, for instance, this is no good... You would have to implement big, ugly methods to handle every type of rendering you support. I'm now at the stage of developing the scene graph and entity base classes of the engine, and I'm trying to come up with a better way of doing things. I really need some "outside inspiration" on how I might accomplish this.

I already have a "GraphicsManager" class which holds a reference to a "Renderer" instance... then I have, for example, a D3D10Renderer that inherits from Renderer and implements all the DirectX specific stuff on its own. I've done this for D3D11 and will soon do it for OpenGL. This approach works beautifully, except that I'm stumped on how to actually implement drawing of objects without creating a monstrosity!

(BTW, I'm doing this in C# and currently supporting DirectX 10 and 11 through SlimDX)
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

Sponsor:

#2 6677   Members   -  Reputation: 1058

Like
0Likes
Like

Posted 06 September 2012 - 12:06 PM

I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?

#3 Radikalizm   Crossbones+   -  Reputation: 2993

Like
2Likes
Like

Posted 06 September 2012 - 12:20 PM

I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?


These are definitely not the only options, not by far.

It's perfectly possible to write a good platform-agnostic renderer, the only thing you need is a proper understanding of basic object-oriented programming.
The code which your drawable objects use to draw themselves should absolutely not care whether they're dealing with a D3D or an OpenGL renderer, they should only have knowledge of the interface which exposes the rendering API.

Your interface defines the 'skeleton' for what your renderer back-end should be able to do. This could contain methods like drawMesh(), setMaterial(), etc.
From this interface you can derive your D3D renderer or OpenGL renderer, and you just properly fill in the methods defined in your renderer interface so they give the desired result. How you fill these in is up to you and might require some experimenting and research, but it is absolutely possible to do in a clean manner.

I gets all your texture budgets!


#4 ATC   Members   -  Reputation: 551

Like
1Likes
Like

Posted 06 September 2012 - 12:28 PM

I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?


Using OpenGL only defeats the whole purpose of what I'm doing. But anyway, I'm just doing Microsoft.NET builds right now. Going to do a Mono port further down the road.
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#5 ATC   Members   -  Reputation: 551

Like
1Likes
Like

Posted 06 September 2012 - 12:31 PM

I understand that, and I've already implemented the "Renderer" interface in that manner and got that part of things worked out...

So what you're suggesting is that I pass the drawable object to the renderer interface reference (which will "trickle down" to the D3D10, OpenGL or whatever specific implementation) and do my rendering that way? That is a viable idea, I think. Please elaborate if you can. I have a few ideas but I'm just undecided on the best way to do this.
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#6 Radikalizm   Crossbones+   -  Reputation: 2993

Like
7Likes
Like

Posted 06 September 2012 - 12:58 PM

I'll try to keep this brief as I don't want this to become an entire 'How to create a renderer' thread, since that's a very large subject.

When rendering an object with a programmable pipeline you always go through some similar steps, independent of whether you're using OpenGL, D3D, or some other obscure rendering API. Here's a compact and maybe somewhat overly simplified overview, but it shows what I'm talking about:
  • You properly set up your rendering state
  • You bind your required shader data
  • You bind your mesh data
  • You execute a draw call
These functions should be defined by your rendering interface and implemented by your D3DRenderer and OGLRenderer.
When we look at a pseudocode version of a drawable object you would get something like this:

[source lang="csharp"]// Remember that our Renderer class is an interface, and under the hood it could be a D3D renderer, an OGL renderer, or something entirely differentvoid Draw(Renderer renderer){ // Your render state is probably determined by the material your drawable object is using, so we provide a method setMaterial() in our renderer interface. // This will probably also take care of binding your shaders and required shader inputs, but this depends on how you design your material framework. renderer.setMaterial(getMaterial()); // You bind the transformation of your drawable object renderer.bindTransformation(getTransformation()); // You render your mesh renderer.drawMesh(getMesh());}[/source]

What your renderer does with all of this information under the hood isn't important for the drawable object, as long as it renders the object as expected.

Now it's just up to you to design your D3DRenderer and OGLRenderer to conform to this interface so the steps required to render an object are executed correctly. I know from personal experience that this is perfectly possible for any contemporary rendering API.

I gets all your texture budgets!


#7 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 01:12 PM

I'll try to keep this brief as I don't want this to become an entire 'How to create a renderer' thread, since that's a very large subject.

When rendering an object with a programmable pipeline you always go through some similar steps, independent of whether you're using OpenGL, D3D, or some other obscure rendering API. Here's a compact and maybe somewhat overly simplified overview, but it shows what I'm talking about:

  • You properly set up your rendering state
  • You bind your required shader data
  • You bind your mesh data
  • You execute a draw call
These functions should be defined by your rendering interface and implemented by your D3DRenderer and OGLRenderer.
When we look at a pseudocode version of a drawable object you would get something like this:

[source lang="csharp"]// Remember that our Renderer class is an interface, and under the hood it could be a D3D renderer, an OGL renderer, or something entirely differentvoid Draw(Renderer renderer){ // Your render state is probably determined by the material your drawable object is using, so we provide a method setMaterial() in our renderer interface. // This will probably also take care of binding your shaders and required shader inputs, but this depends on how you design your material framework. renderer.setMaterial(getMaterial()); // You bind the transformation of your drawable object renderer.bindTransformation(getTransformation()); // You render your mesh renderer.drawMesh(getMesh());}[/source]

What your renderer does with all of this information under the hood isn't important for the drawable object, as long as it renders the object as expected.

Now it's just up to you to design your D3DRenderer and OGLRenderer to conform to this interface so the steps required to render an object are executed correctly. I know from personal experience that this is perfectly possible for any contemporary rendering API.


Very good post. I considered that very method of doing things, but couldn't decide for/against it. You're pseudocode example just made it click for me, thanks! This seems like a very elegant and concise approach. I will try this out and see if I can get what I want out of it (which I think I can now). Why my brain couldn't hash this out is beyond me... I guess I've been so bogged down in DXGI programming and writing new VectorX, Matrix and Quaternion structures that the simple things can slip by me lol. Just try implementing your own Matrix.Decompose method and you're sure to share a migraine with me. Posted Image

But I'm still open to other suggestions and ideas. This is a prototype of a commercial engine so I want to expose myself to and test a wide variety of approaches and outside ideas. That's why I came back here, after all. Posted Image

+1 rep for the honorable Radikalizm!

Edited by ATC, 06 September 2012 - 01:16 PM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#8 swiftcoder   Senior Moderators   -  Reputation: 10444

Like
4Likes
Like

Posted 06 September 2012 - 01:18 PM

the conventional approach to rendering a 3D object is to make a call to the object's "Draw" method and pass it an instance of the D3D Device

Whether or not it is conventional, it is not a very good approach.

You don't want to think about drawing 'objects' in this data-driven day and age. You want to think about data, and operations on that data.

Think about 'what' a drawing operation needs, conceptually: vertices to specify geometry, index data to specify the faces, textures to define the surface data, shaders to define the surface appearance, and uniform data to specify the transform/lighting/etc.

So now you have:
class RenderOperation
{
	VertexBuffer vertices;
	IndexBuffer indices;
	Texture textures[];
	ShaderProgram shader;
	Uniform uniforms[];
}

class Renderer
{
	void render(RenderOperation ro);
}

And that, right there, is your core renderer interface, in a platform and API-agnostic fashion.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#9 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 02:27 PM

@swiftcoder:

You're right, which is why I'm not taking that "conventional" approach. It works fine if you're writing a game that uses a single rendering API, but not much beyond that; it binds you to one type of renderer and you're stuck with it or forced to write some terribly ugly and long code.

Thank you also for your helpful insights!
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#10 phantom   Moderators   -  Reputation: 7593

Like
0Likes
Like

Posted 06 September 2012 - 03:17 PM

It works fine if you're writing a game that uses a single rendering API, but not much beyond that; it binds you to one type of renderer and you're stuck with it or forced to write some terribly ugly and long code.


It's not even that; it is possible with enough abstraction for the 'conventional' method to work with a few renderer types as the order of operation is the same; where it falls down however is overhead and the coupling of the rendering data to an interface.

With the 'chunk of data' method all the data you need is there, layed out in memory as you need. No function call overhead, no cache issues, easier to sort, easier to filter, easier to vistest and easier to work with.

You can even go one step further and have your objects, at load time, construct a command list for themselves which has all the data needed to draw the object; once you know you are going to draw it and when you copy this command list (simple [command token][data] stream) into the master command list which is then read by your renderer and just executes the commands 'as is' with very little jumping about.

These methods are much more CPU friendly, scale better and are easier to work with when it comes to getting performance.

TLDR; objects shouldn't draw themselves. They should just provide the data needed to draw them.

#11 Radikalizm   Crossbones+   -  Reputation: 2993

Like
0Likes
Like

Posted 06 September 2012 - 03:21 PM

TLDR; objects shouldn't draw themselves. They should just provide the data needed to draw them.


Very true. There's an article about data-driven renderer design in GPU Pro 3 which is a very interesting read and which might be of interest to the OP.

I gets all your texture budgets!


#12 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 03:29 PM

Good point. Would you consider that to be a serious design-flaw of the XNA Framework?
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#13 swiftcoder   Senior Moderators   -  Reputation: 10444

Like
1Likes
Like

Posted 06 September 2012 - 05:02 PM

Good point. Would you consider that to be a serious design-flaw of the XNA Framework?

XNA doesn't need to support multiple underlying APIs, nor drastically distinct platforms. It also isn't a high-performance rendering engine. But the job it was intended for, it does that pretty well.

I guess my point is that real-world APIs have to incorporate compromises - I wouldn't necessarily call them 'design flaws'. You can always make a (non-trivial) API better, but eventually you have to ship a working product, c'est la vie.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#14 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 05:51 PM

True dat. Posted Image

Here's another question... Consider the issue of needed API-agnostic classes for resources (e.g., Texture2D)... Would it be a bad idea to simply create a "wrapper" class, like so:

class Texture2D
{
object _texture;
};

...then let DirectX or OpenGL load the texture in their own platform-specific way, and store that in the object "_texture". Then each individual renderer would just cast it back into their own, API-specific type when they consume it? It seems like a very, very simple and easy way that just might be crazy enough to work.

EDIT: How expensive are cast operations (not boxing; only reference-type >> reference-type, I mean) in general?

Edited by ATC, 06 September 2012 - 05:53 PM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#15 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 06 September 2012 - 06:25 PM

As long as every class knows of it's "parent" it should be quite easy to come up with an architectural solution.

So following on from ATC's example.

struct Texture2D
{
  Core* core;

#ifdef OPENGL_PORT
  GLuint texture;
#else
  SomeOtherNativeTexture* texture
#endif

  Texture2D(Core* core);
};

So now in the constructor, you can always go up to the core (perhaps having an aggregate relationship with GraphicsManager?) and then obtain whatever class is required to load the texture (or in the case of OpenGL, perhaps just switch the context to the current one and load the texture as usual).

Have a look at Irrlicht or Ogre3D to see how they do it.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#16 swiftcoder   Senior Moderators   -  Reputation: 10444

Like
3Likes
Like

Posted 06 September 2012 - 06:38 PM

So now in the constructor, you can always go up to the core (perhaps having an aggregate relationship with GraphicsManager?) and then obtain whatever class is required to load the texture (or in the case of OpenGL, perhaps just switch the context to the current one and load the texture as usual).

You are mixing your data and functionality. If the Texture object isn't responsible for loading itself (and it shouldn't be), then that loading code belongs somewhere else:

class Texture
{
#ifdef _OPENGL_
	GLuint handle;
#else
	 IDirect3DTexture9 *handle;
#endif
}

class ResourceLoader
{
	Texture LoadTexture(string filename);
}

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#17 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 06 September 2012 - 06:51 PM

swiftcoder,

True, but I think the design idea still stands. If you have a parent reference in every class, you can always navigate up the hierarchy if your platform requires something to carry out it's functionality.

I.e your ResourceLoader class might need to obtain a pointer to the X11 Display struct in order for it to create or load an image (i.e XCreatePixmap())

Edited by Karsten_, 06 September 2012 - 06:54 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#18 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 September 2012 - 08:51 PM

Thanks guys,

This discussion gave me a good idea... a "Resource" class which holds an IntPtr, in which is stored the internal pointer of the native DirectX/OGL resource. Classes like "Texture2D" inherit from resource, and that allows my renderer to free up those resources with ease. Going to use my content-loading system to load resources and fill in necessary data-fields like Texture2D.Width, etc...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#19 L. Spiro   Crossbones+   -  Reputation: 14423

Like
0Likes
Like

Posted 07 September 2012 - 04:04 AM

struct Texture2D
{
  Core* core;

#ifdef OPENGL_PORT
  GLuint texture;
#else
  SomeOtherNativeTexture* texture
#endif

  Texture2D(Core* core);
};


I strong suggest not organizing it this way.
Try this instead.


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

#20 phantom   Moderators   -  Reputation: 7593

Like
7Likes
Like

Posted 07 September 2012 - 05:21 AM

Have a look at Irrlicht or Ogre3D to see how they do it.


And then run the fuck away and do it better.

Ogre3D is well named because, like its name sake, it is an ugly overly OOP mess which any sane developer would not touch with some one elses 20ft barge pole.




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