trying to wrap my head around directx+opengl abstraction layer.

Started by
3 comments, last by karpatzio 11 years, 5 months ago
Ive been spending the last couple of days making drafts of a directx+opengl abstraction layer and Ive run across some points that make me want to get input.

So basically, all the drawing data is stored in an "Entity" class as an abstract VertexDecleration class and some of the data like position and orientation are stored in an EntityInstance class. Drawing data is API specific so i have a glVertexDecleration + dxDecleration etc.....
I have a "mesh loader" class that reads a file, creates buffers (on system memory) along with some semantics and stuff. Then the mesh goes through an API specific class, lets call it "mesh\texture binder" that handles VBO, indexing and details like that. Then the entity is stored in a "cache" class. something like this:

class Entity
{
public:
Entity(vec3 position, box boundingVolume, .....);

private:
vertexDecleration m_decl;
}

template <class BINDER, class CACHE>
class Meshloader
{
typedef shared_ptr<Entity> EntityPtr;

public:
EntityPtr loadEntity(const std::string &filename);
void destroyEntity(EntityPtr);

private:
BINDER m_binder;
CACHE m_cache;
}

Now, scenes are managed with a scenemanager that handles instances of Entities and is oblivious of the rest of the system, especially to the underlying VertexDecleration type and API. So FINALLY my question is: how do i elegantly collect entities to be drawn and propegate them to the API specific renderer? I know i can just cast upwards and stuff but that feels flakey to me...

So, id just like input on how you handle stuff like this in your own code... THANKS!
Advertisement
I would suggest making a graphics module and let that be the only set of code (with VERY few exceptions) that changes per API type.
Inside the graphics module you have a CVertexBuffer, CIndexBuffer, etc., classes, and it is these that change with each API.

The outside world’s code is the same on every platform. Entities simply contain a CVertexBuffer and CIndexBuffer, they call an agnostic API to activate those buffers, and to render them. There is no reason an entity should ever know what DirectX or OpenGL is.


Additionally, while I simplified my example classes down to CVertexBuffer and CIndexBuffer, you should actually organize it how I described here: http://lspiroengine.com/?p=49


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

I read your blog post, i think thats sort of what im doing now. Entities, nodes in the scenemanager and the scenemanager themselves dont actually have any API specific code other than "Entity" storing the API agnostic data needed for rendering (position, bounding volume stuff like that) but also the api specific stuff like buffers and textures in the form of say a list of "vertex elements" and "Texture" classes like youv'e talked about in your post that i extend with the information that each API, the handles, sizes and all that.

So the general idea is like this:
[source lang="C++"]enum ElementType
{
Position,
Normal,
TextureCoords
};

class VertexElement
{
....
protected:
ElementType m_type;
Array m_data;
};

typedef std::list<VertexElement, Element Type> ElemetList;

class glVertexElement: public VertexElement
{
....
public:
gluint getHandle();

private:
GLuint m_handle;
GLSizei m_size;
....
};

class glTexture: public Texture
{
...
private:
GLuint m_texture;
};

class Entity
{
...
VertexElement* getElement(unsigned int index);
Texture* getTexture();

private:
Texture m_texture;
ElementList m_elements;
};

class RenderingQueue
{
public:
std::vector<Entity> entities;
}

[/source]

Then these Entities are attached to nodes in the scenemanager which collects them every frame to dispatch to the API specific renderer in the form of a queue of vertexelements and materials, possibly arranged, sorted and grouped for instancing.

Creating an entity and rendering it might go something like this (at the lowest level):
[source lang="cpp"]ElementList *elemList = ElementList::create();

elemList.push_back(VertexElement::create(Position, someBufferOfData));
elemList.push_back(VertexElement::create(Normal, someOtherBufferOfData));
elemList.push_back(VertexElement::create(TextureCoords, yetAnotherBufferOfData));

Texture *texture = Texture::create("./texture.tga");

Entity someEntity(elemList, texture);

SceneManager manager(...);
manager.addNode(someEntity);

Renderer renderer = Renderer::create();

RenderingQueue queue = manager.getQueue();
renderer.render(queue);[/source]
I guess that what i was more specifically asking is that once the renderer does recieve the entities to be rendered, how do i avoid casting the VertexElements and Textures to the glTexture and glVertexElement. But looking at your code i guess i can subclass this.
As for the first section of your post, you have the classes backwards.
Based on your submitted code, glVertexElement is the top-level class, which means some kind of factory or something needs to generate it based on which API is active.
You use glVertexElement to add OpenGL features to VertexElement, which means to use those features you will constantly be casting to glVertexElement, since your entities are storing them as VertexElement types.

The hierarchy I suggested was like this:
VertexElementBase
glVertexElement : public VertexElementBase
VertexElement : public glVertexElement

  1. The class never needs to be cast. By having a VertexElement instance, its OpenGL/Direct3D features are already exposed.
  2. You are violating the single-responsibility principal. The features of the class that are specific to OpenGL or DirectX don’t need to be exposed. No other classes should be handling that information; it is not their responsibility. The glVertexElement class itself should know how to handle its own GLuint handle etc.

I will use an index buffer for my example because it is the smallest and most concise piece of code. Here is the DirectX 9 version for rendering with an index buffer:
/**
* Render the scene with the given vertex buffer.
*
* \param _vbBuffer The buffer
* \param _ui32TotalPrimitives Total primitives to render.
*/
LSVOID LSE_CALL CDirectX9IndexBuffer::RenderApi( const CVertexBuffer &_vbBuffer, LSUINT32 _ui32TotalPrimitives ) const {
HRESULT hRes = CDirectX9::GetDirectX9Device()->SetIndices( m_pibIndexBuffer );
hRes = CDirectX9::GetDirectX9Device()->DrawIndexedPrimitive( m_ptPrimitiveType,
0,
0UL,
_vbBuffer.TotalElements(),
0UL,
_ui32TotalPrimitives + m_ui32AddedPrimitives );
}

The OpenGL version of the same:
/**
* Render the scene with the given vertex buffer.
*
* \param _vbBuffer The buffer
* \param _ui32TotalPrimitives Total primitives to render.
*/
LSVOID LSE_CALL COpenGlIndexBuffer::RenderApi( const CVertexBuffer &/*_vbBuffer*/, LSUINT32 _ui32TotalPrimitives ) const {
if ( m_vBuffer.Length() || m_uiVboId ) {
COpenGl::glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_uiVboId );

static const LSUINT32 ui32Type[] = {
0,
0,
GL_UNSIGNED_SHORT,
0,
GL_UNSIGNED_INT
};
if ( m_ui32PrimitiveTypeApi == GL_TRIANGLES ) {
::glDrawElements( m_ui32PrimitiveTypeApi, _ui32TotalPrimitives * 3UL + (m_ui32AddedPrimitives * 3UL), ui32Type[m_ui32ElementSize], m_uiVboId ?
0 :
&m_vBuffer[0] );
}
else if ( m_ui32PrimitiveTypeApi == GL_TRIANGLE_STRIP ) {
::glDrawElements( m_ui32PrimitiveTypeApi, _ui32TotalPrimitives + 2UL + (m_ui32AddedPrimitives), ui32Type[m_ui32ElementSize], m_uiVboId ?
0 :
&m_vBuffer[0] );
}
}
}


Then the CIndexBuffer class:
class CIndexBuffer : protected
#ifdef LSG_OGL
COpenGlIndexBuffer
#endif // #ifdef LSG_OGL
#ifdef LSG_DX9
CDirectX9IndexBuffer
#endif // #ifdef LSG_DX9

{


And to draw with the index buffer via CIndexBuffer:
/**
* Render using this buffer.
*
* \param _vbBuffer The buffer
* \param _ui32TotalPrimitives Total primitives to render.
*/
LSE_INLINE LSVOID LSE_CALL CIndexBuffer::Render( const CVertexBuffer &_vbBuffer, LSUINT32 _ui32TotalPrimitives ) const {
Parent::RenderApi( _vbBuffer, _ui32TotalPrimitives );
}



The OpenGL IBO handle m_uiVboId is never accessed outside of COpenGlIndexBuffer.
The Direct3D 9 pointer m_pibIndexBuffer is never accessed outside of CDirectX9IndexBuffer.
Those things are the responsibilities of those classes and no others. The index buffer itself knows how to perform a render operation.
Don’t pass this type of responsibility off to anything else.
Also note that no virtual functions are used. RenderApi is guaranteed to exist on from whatever class CIndexBuffer inherits.




For the second part of your question it is not clear if you understand not to pass entities to the render module.

in the form of a queue of vertexelements and materials


I guess that what i was more specifically asking is that once the renderer does recieve the entities to be rendered

Does it pass vertex elements and materials, or does it pass entities?

Either way is incorrect.
Firstly, if you are passing vertex elements and materials, there is too much micro-managing and/or memory copying taking place, as well as another violation of the single responsibility principal.
Instead of passing off materials, textures, vertex buffers, etc., to be handled by the render, why don’t the entities themselves just draw themselves (using abstracted API-agnostic functions exposed by the graphics module)? Entities themselves know best how to do that. If you try to make the renderer handle all the different things that can be passed off to it, you will soon find yourself lost in spaghetti. It will be unmanageable to try to handle all the different ways things can be drawn in one isolated spot.

Secondly, if you are passing entities, this is wrong in of itself. A renderer should never know what an entity is. An entity knows what a renderer is because an entity needs to be rendered and thus needs to know about certain aspects of the renderer. If the renderer then also knows what an entity is, you have a circular dependency or a hackish work-around to avoid it.



I suggest the following:
The Entity class inherits from RenderQueueClient, which provides virtual functions the Entity class can overload for rendering itself.
virtual LSVOID LSE_CALL RenderQueueClient::FinalRender( LSG_RENDER_TYPE _rtRenderType, LSUINTPTR _uiptrUser ) = 0;

Entity : public RenderQueueClient


The scene manager, after culling objects, then asks the remaining objects to put themselves into a render queue. There could be multiple entries for any given entity, since they can have many meshes each.
A render item is this:
/**
* Class CRenderQueueItem
* \brief Information needed by the render queue for sorting.
*
* Description: Information needed by the render queue for sorting.
*/
typedef struct LSG_RENDER_QUEUE_ITEM {
/**
* The shader ID.
*/
LSUINT64 ui64ShaderId;

/**
* Texture ID's.
*/
LSUINT32 ui32Textures[LSG_MAX_TEXTURE_UNITS];
} * LPLSG_RENDER_QUEUE_ITEM, * const LPCLSG_RENDER_QUEUE_ITEM;


And the scene manager goes over each entity to put itself into a render queue this way:
/**
* Adds data to the given render queue set.
*
* \param _rqsSet The render queue set to which to add our render queue items.
* \param _ui32Pass The number of the pass represented by the render queue set.
* \param _rtType The type of render.
* \param _vCamPos Position of the camera.
* \param _vCamDir Direction of the camera.
*/
LSVOID LSE_CALL Entity::AddToRenderQueue( CRenderQueueSet &_rqsSet, LSUINT32 _ui32Pass, LSG_RENDER_TYPE _rtType,
const CVector3 &_vCamPos, const CVector3 &_vCamDir ) {
for ( each render queue item ) {
if ( bOpaque ) {
_rqsSet.OpaqueRenderQueue().AddItem( &m_vRenderParts.rqiStandardQueueItem, // rqiStandardQueueItem is of type LSG_RENDER_QUEUE_ITEM and was precomputed.
this, fDist, I );
}
else {
_rqsSet.AlphaRenderQueue().AddItem( &m_vRenderParts.rqiStandardQueueItem,
this, fDist, I );
}
}
}


(A CRenderQueueSet is a pair of render queues, one for opaque and one for translucent. Entities themselves know where to put each render-queue item they have.)
Notice that it passes a [color=#0000cd]this pointer to the render queue.
When it is time to render, the render queue uses that pointer to call the FinalRender() virtual function mentioned previously.
/**
* Render the sorted items using a non-standard render method.
*
* \param _rtRenderType Type of render.
*/
LSE_INLINE LSVOID LSE_CALL CRenderQueue::Render( LSG_RENDER_TYPE _rtRenderType ) const {
const LSUINT32 * pui32Indices = m_isSorter.GetIndices();
for ( LSUINT32 I = 0UL; I < m_vList.Length(); ++I ) {
m_vList[pui32Indices].prqcSender->FinalRender( _rtRenderType, m_vList[pui32Indices].uiptrUser );
}
}

prqcSender here is the [color=#0000cd]this pointer that was passed to the render queue’s AddItem().



Notice how the render queue was able to sort the items by shader and textures for reduced state switching, but then delagate the actual rendering of each object to the objects themselves.
The render queue is part of the graphics module but it has no idea what entities are nor how to draw them.
It simply exposes the building blocks necessary for rendering and lets renderable objects render themselves.


This is important for several reasons.
Firstly, even if all you did was render models, that would already be a hairy mess to handle all within the renderer.
But you also have water objects with a totally different way to render themselves.
Terrain renders in a totally different way.
Skies and clouds will have yet another entirely new set of ways to render.

Trying to handle all of these in one place is not a happy land.


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

Great! I better understand what you're saying now. The main reason i went down the path that i did was the idea that a renderable shouldn't be allowed to render itself in the context of a whole scene. But really optimizations like avoiding unnecessairy state changes, grouping for instancing and all that stuff don't really require me to centeralize my rendering code.

Thanks!

This topic is closed to new replies.

Advertisement