• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ATC

Platform-agnostic renderer

51 posts in this topic

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)
0

Share this post


Link to post
Share on other sites
I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?
0

Share this post


Link to post
Share on other sites
[quote name='6677' timestamp='1346954811' post='4977296']
I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='6677' timestamp='1346954811' post='4977296']
I think your 2 choices (from someone with no experience) are create a monstrosity or use only OpenGL, I assume your compiling on mono?
[/quote]

Using OpenGL [i]only [/i]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.
1

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1346957935' post='4977325']
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:[list]
[*]You properly set up your rendering state
[*]You bind your required shader data
[*]You bind your mesh data
[*]You execute a draw call
[/list]
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 different
void 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.
[/quote]

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

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

+1 rep for the honorable Radikalizm! Edited by ATC
0

Share this post


Link to post
Share on other sites
@swiftcoder:

You're right, which is why I'm not taking that "conventional" approach. It [i]works [/i]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!
0

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1346963270' post='4977355']
It [i]works [/i]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.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1346966226' post='4977366']
TLDR; objects shouldn't draw themselves. They should just provide the data needed to draw them.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
Good point. Would you consider that to be a serious design-flaw of the XNA Framework?
0

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1346966957' post='4977372']
Good point. Would you consider that to be a serious design-flaw of the XNA Framework?[/quote]
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.
1

Share this post


Link to post
Share on other sites
True dat. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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
0

Share this post


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

[code]
struct Texture2D
{
Core* core;

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

Texture2D(Core* core);
};
[/code]

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.
0

Share this post


Link to post
Share on other sites
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_
0

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Karsten_' timestamp='1346977505' post='4977441']
[code]
struct Texture2D
{
Core* core;

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

Texture2D(Core* core);
};
[/code]
[/quote]

I strong suggest not organizing it this way.
Try [url="http://lspiroengine.com/?p=49"]this[/url] instead.


L. Spiro
0

Share this post


Link to post
Share on other sites
Hmm,

I think the Unreal Engine approach is descent.
The User can adjust at runtime in a Config file wich specific *.DLL (win32/win64) / *.SO (Linux/Unix/Mac) Renderer Driver should be used.
You can interface with an abstract Renderdriver proxy class and let the Renderdriver do its thing. Simple but effective and ellegant (just my 2 cents).
0

Share this post


Link to post
Share on other sites
Here's how I'm currently doing things, thanks to some of the brilliant suggestions I've recieved here from our community's most brilliant and senior members... :)

Pseudo-code:

[source lang="csharp"] public class RenderOp
{
public string[] CmdString { get; set; }
public MeshData Mesh { get; set; }
public Material Material { get; set; }
};

public class RenderOpBatch
{
// Pseudo-implementation not shown to save space
};

public abstract class Renderer
{
RenderOpBatch currentBatch;
Queue<RenderOpBatch> RenderBatches;

public virtual void StartRenderBatch() {
currentBatch = new RenderOpBatch();
RenderBatches.Enqueue(currentBatch);
}

public virtual void QueueOp(RenderOp op) {
currentBatch.Add(op);
}

public abstract void FlushAllBatches();

// blah, blah, blah... you get the idea :)
};[/source]

That's not truly how I've implemented it, but just a pseudo-code expression of the idea. For instance, I don't really use Queue<T>, I use a custom collection type that allows me to choose LIFO, FIFO or custom sorting of batches and all sorts of stuff. Any comments/criticisms/suggestions concerning this concept?
0

Share this post


Link to post
Share on other sites
Ok... here's another thing I'm trying to work out: vertex types and input layouts (can't remember what the OpenGL counterpart of an input layout is called... usage hint, maybe?)...

I need to design a sub-system through which new vertex structures can be implemented beyond the common ones the engine will already offer, and the ones I do offer need to adhere to a clean, consistent format. It needs to be written so that the same data can be used to create an D3D "input layout" or an OpenGL "usage hint" (or whatever it's called) on-the-fly as the vertex data is pushed to the renderer. It's hard for me to decide on things sometimes because I'm not sure what parts/features of D3D and OpenGL are so seldomly used that they can just be cut out, and which people are going to be pissed if I don't let them have... Anyway, some of the ideas I have are:

The first thing to consider is how we designate what fields of a vertex are for (and how big they are). We could, like DirectX, just use a string (e.g., "POSITION"). Or we could use some type of enumeration, like this:

[source lang="csharp"] public enum VertexElements
{
NULL = 0x0000,
POSITION = 0x0001,
COLOR = 0x0002,
TEXCOORD = 0x0004,
NORMAL = 0x0008,
BINORMAL = 0x0016,
TANGENT = 0x0032
};[/source]

What might the pros/cons of each method be? And what would be a good way to represent the size of vertex fields without using a platform-specific enumeration like SlimDX's "Format" enum? Or is there yet another unthought-of way of doing this that would be superior to both?

Next, what would be the best way to implement a cohesive vertex typing system that can be broken-down and understood by virtually any type of renderer? I have some thoughts already, and I'll show you what ideas I'm toying with:

1) A common interface all vertex structures inherit from. For example:

[source lang="csharp"] [StructLayout(LayoutKind.Sequential)]
public interface IVertex
{
int SizeInBytes { get; }

VertexElements[] Elements { get; }

byte[] ToBytes();
};[/source]

All vertex types would implement that interface if such a method was used, and they would have to return a static value which is not part of the memory of an actual vertex instance on the stack (as that would throw things off).

2) Create a new struct/class (e.g., "VertexDescription") that houses a nice description of a vertex-type and tells you what's in its guts. The essence of it might look like this (incomplete example):

[source lang="csharp"] public class VertexLayout
{
int sizeInBytes;
VertexElements[] elements;
};[/source]

In addition to this structure, perhabs it might be an idea to implement a new enumeration type which replaces platform-specific enumerations like SlimDX's "Format" but offers the same data in a new way; potential even giving the size in bytes of an element as its own numerical value!?

[source lang="csharp"] public enum ElementFormat
{
byteX1 = 1,
byteX2 = 2,
byteX3 = 3,
byteX4 = 4,
shortX1 = 2,
shortX2 = 4,

// ...and so on...
};[/source]

Anyway, I hope the wisdom of the community can once again offer me some excellent ideas! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

EDIT: The idea of assigning the enum values of "ElementFormat" the size on the element in bytes actually wont work because C# treats enums as numeric values and would not be able to distinguish between them. My bad, didn't think about that. Please disregard that erroneous idea. Edited by ATC
0

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  
Followers 0