Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.






Abstraction in a Game Engine/Framework

Posted by ZBethel, 02 January 2012 · 1,435 views

C++ DX11 D3D
If there's one thing that's dang hard, it's trying to find a decent balance between flexibility and simplicity when writing a good abstraction layer. I'm relatively new to DirectX 11, but so far it is a much nicer API to work with than previous versions of DirectX. It's so nice in fact, that I'm having a hard time finding ways to wrap things into nice little bundle classes without losing core functionality. While I don't particularly enjoy having to play around directly with a graphics API (I'd much rather hide it away behind some nice facade), I must say that the designers are making it hard on me.

I ended up creating an IDX11Graphics interface which encapsulate the device context, the device, and the factory object. I have a simple Init() function which enumerates all mode data from all of the adapters and files them away into a data structure that lets me filter modes by adapter, output, and format. Pretty simple stuff. The initialization stuff is the easiest to encapsulate, which is obvious when you look at the DXUT library that Microsoft provides.

I'm starting to realize that there is little you can do to improve the API without moving up a software abstraction layer. The farther up you go, however, the more flexibility you lose. For instance, I was trying to create a render target wrapper class, but I realized that the components of a render target really weren't meant to be coupled, which is undoubtedly why Microsoft did it in the first place. D'oh! There is a nice relationship between how DirectX handles resources and resource views; it seems clunky to just plop them both together into one helper class.

For instance, how do we handle render targets in a flexible way that actually simplifies the process? We could build a class that creates a texture, render target view, and shader resource view, But then how do we handle swap chains, which have a different creation process? The class could take a pointer to the texture as input to allow the user to create it however they want; or we could remove the shader resource view and put it in another supporting class. As another example, I considered writing one IDX11Texture class to encapsulate all types of textures, but one big immediate problem I ran into is high potential for bloat. In order to accommodate all different types of textures (1D, 2D, 3D, etc.), the class would have include ALL of the data. Ultimately, I think these options detract from the elegance of the SDK and make things more confusing and less flexible. This is one aspect of object oriented programming that drives me crazy. There just doesn't seem to be the Right Way to do it.

On the other hand, in order to simplify things, we have to sacrifice flexibility at some point. It's just going to happen. I guess where I struggle is finding the happy medium where an abstraction can make the programmer's life lots easier, but also allow retain the features that they need for their graphics algorithms. It's definitely hard to know what features are needed when you're just learning the SDK--a fact I know first hand.

I think this is the challenge that appeals to me the most about game engine development. There is so much software engineering involved. If you don't carefully plan out the intricate relationships between components and subsystems, the entire things quickly degrades into chaos. I have high respect for people who can grasp it all.

In any case, I have graphics interface in place that takes care of initialization and device enumeration. I am still deciding what other tasks I should give it--whether it should just be a glorified ID3D11Device, or more of an abstract renderer. Because I am still going to retain DirectX 11 access within my engine, I am hoping that it can become both. What really bugs me is when I create a helper interface that ends up just having 5 lines per function call that parrot things back to the Direct3D device. That doesn't seem constructive to me at all.

In any case, that's all I've got for now.




Interesting read! :)
I would caution against lumping the device and device context together - sooner or later you will want to use deferred contexts, and they are inherently separated from the functionality of the device. In Hieroglyph 3 I have a RendererDX11 class that wraps the device, and a PipelineManagerDX11 class that wraps a device context.

The renderer provides the same types of services that the device does - it creates the other resources and objects to be used by D3D11. So to make life easier, I provide creation methods that automatically track the created objects and ultimately maintain a list of things that need to be released at shutdown.

The pipeline manager is what is used for rendering with the pipeline, and can be used with immediate or deferred contexts interchangeably. This makes it so that you can switch between single or multi-threaded rendering fairly easily. Since the device context is used to manipulate pipeline state, then I add methods to the pipeline manager that make that easier.

The abstractions that you choose should be intended to simplify a problem that you see - don't create abstractions simply to do it! Of course you will never plan everything right from the beginning, but nobody can - that is how you learn. That is also why refactoring is such a big part of software engineering too...

That was a great post - very interesting to read. I look forward to hearing what else you have to say :)
Ah, see, there's another example. My first instinct is try and encapsulate everything into one nice package class. But it's obvious that Microsoft didn't intend for that to be the case. Like I said, I'm relatively new to DirectX 11. I didn't realize that deferred contexts would need that functionality; but now that you mention it, I do see that. Thanks for the heads up!

I've been looking over your Hieroglyph 3 framework. Have you ever read Game Coding Complete? I can see architectural elements in your code that resemble those used by McShaffry in his book (like the render views). It's a lot of source files, but it looks like many of them are quite small (i.e. the classes are pretty thin abstractions). I particularly liked how your wrapped the shaders and effect framework. I may reference your code for my implementation. :)

Your comment about refactoring is very sound. I'm going to devote my next blog post to that, because I really struggle with it. Thanks for the feedback!
PARTNERS