Skip to main content
gamedev.net
🔒 Locked

Rendering Loop Design Patterns

Started by k00k Jul 31, 2006 at 9:51 AM 3 replies 1.4k views
Original Post
k00k
k00k
Hello everyone, been awhile sence I have had a chance to post on here. so here goes. Regarding Render loops what design patterns have you guys found to be effective? My loop is starting to get a bit hairy in my engine here with IF statements doing checks to see if my say Mesh entity class has a shader attached to it, or if it has alpha Blending enabled, ect. Just wondering what you guys do to more effectively manage your loops. or should I just kick out of my loop with IF statements calling other methods to handle those specific rendering types. Well just trying to get some ideas kicked around here so any input would be greatly appreciated. Thanks everyone.
jollyjeffers
jollyjeffers
Hi,

I'm not aware of much discussion regarding render-loops. Searching the DirectXDev archives would probably be a good start - its a gold mine for this sort of question [smile]

Does profiling reveal that your conditional/branching is costing you? Or is this simply from a "coding quality" point of view?

I've been quite a fan of function pointers and "execution buffer" style architectures. In lots of applications there will be substantial coherance between frames, thus building a queue/list of commands can allow you to just for(;;) your way through them in a very elegant way. The insertion/removal/sorting gets a whole lot more complicated though.

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
JasonBlochowiak
JasonBlochowiak
Conditionals in central loops usually indicate a lack of distributed responsibility for behavior of individual entities.

For my renderer, I have a cRenderObject class with a virtual Render() function. Skinned meshes and "regular" meshes have different Render() functions, as do terrain meshes, particle systems, etc.

Each render object also has a cDrawState bound to it, and zero or more cMaterials. Each material also has a draw state, and zero or more cTextures.

Changing the alpha blending mode on an object is done by calling something like mDrawState.SetAlphaBlendMode(cDrawState::ab_Blend);

At the start of each frame, I set DirectX's state to my defaults. When I bind or unbind a draw state, any element that's set in it is copied to the current "draw state dirty list." Before rendering any actual polys, the dirty list is flushed to DirectX. So, for flushing, say, alpha blending, the snippet looks like this:

case cDrawState::c_AlphaBlendMode:	{		cDrawState::eAlphaBlend ab = cDrawState::CurrentAlphaBlendMode();		switch (ab)		{		case cDrawState::ab_Opaque:			cd3d(device.SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE));			break;		case cDrawState::ab_Blend:			cd3d(device.SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE));			cd3d(device.SetRenderState(D3DRS_BLENDOP,	D3DBLENDOP_ADD));			cd3d(device.SetRenderState(D3DRS_SRCBLEND,	D3DBLEND_SRCALPHA));			cd3d(device.SetRenderState(D3DRS_DESTBLEND,	D3DBLEND_INVSRCALPHA));			break;


Inside of the standard mesh renderers, there are triangles grouped by material. Texture binding happens inside of material binding.

There are also a bunch of additional details related to HLSL shader fragment binding and drawstate lock/override methods to support stuff like z pre-pass rendering, but I think that's a bit beyond the scope of my reply. :)
k00k
k00k
JasonBlochowiak : Nice that is exactly what im talking about as of right now instead of using case switches, I am simply just using a few if statements. cool I just was wondering if there was a design pattern that alot of people feel is the quickest/cleanest route to take. but I belive case switches or IF statements tend to be the quickest route with calls to other methods that hold the correct rendering routinge for that givin instance. thanks everyone for your idea'rs :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.