Jump to content

  • Log In with Google      Sign In   
  • Create Account

DX11 Instancing and transparency affording draw order?


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

#1 Husbjörn   Members   -  Reputation: 283

Like
0Likes
Like

Posted 30 January 2014 - 02:17 PM

I haven't quite got around to try this yet but I was thinking it over and was thinking it may prove a problem.

 

My rendering framework so far draws any opaque meshes first, the Z-order isn't considered important what with the depth buffer. After that "cheap" transparent frames are drawn, also in no particular order but at least after the opaque ones and finally I sort the remaining transparent frames by their screen depth so that a transparent object behind another transparent object doesn't get clipped by being drawn after the first one.

 

However if I'm to use instancing for transparent frames that would benefit from the latter approach... this would be a problem wouldn't it (since they're all rendered in the same draw call and as such can't be sorted in with other meshes)? Is there any good solution to this or am I missing some point to instancing which means this really isn't an issue?



Sponsor:

#2 SeanMiddleditch   Members   -  Reputation: 7152

Like
0Likes
Like

Posted 30 January 2014 - 03:42 PM

My rendering framework so far draws any opaque meshes first, the Z-order isn't considered important what with the depth buffer.


You may want to profile the difference you get if you sort front-to-back with opaque objects. Yes, the Z-buffer stops rendering from being wrong, but it doesn't avoid the issue with overdraw. Overdraw is when you draw an object far away and then draw over it. This has a performance hit, especially with complex pixel shaders. With early-Z hardware optimizations, the extra runs of the pixel shader and extra writes into the depth/color buffers are avoided if you sort front-to-back. When it's a win will depend on what you're doing specifically.
 

After that "cheap" transparent frames are drawn, also in no particular order


I presume this means additive or screendoor transparency effects?
 

However if I'm to use instancing for transparent frames that would benefit from the latter approach... this would be a problem wouldn't it (since they're all rendered in the same draw call and as such can't be sorted in with other meshes)? Is there any good solution to this or am I missing some point to instancing which means this really isn't an issue?


The result of the draw operation is dependent on the order the primitives are specified. I don't know where it says it in the D3D documentation (it's clearly guaranteed in OpenGL's spec) but it basically has to be true in order to enable correct translucent rendering like your case. This applies to instances as well as individual primitives. Just sort the instance transform data back-to-front before rendering and you'll be fine.

#3 Husbjörn   Members   -  Reputation: 283

Like
0Likes
Like

Posted 30 January 2014 - 04:20 PM


I presume this means additive or screendoor transparency effects?

Not really. As I've been lead to understand by various articles changing rasterizer states, blend states and shaders were to be considered more of a bottleneck (in general) than things like draw order. I suppose it makes sense that it can be the other way around if you have a sufficiently complex set of shaders going though; at the moment I don't though so such optimizations haven't really crossed my mind yet. So basically when I said "cheap" transparency I mean they are just drawn after the completely opaque meshes but prioritize state changes over depth when it comes to sorting. This will likely be good enough for things like floor covers. On the other hand if you have a window and can see a (semi-transparent) body of water through it it wouldn't do if the window was rendered first and alpha-blended with the backgrond before the water surface had been written there, so in that case it makes more sense to prioritize depth over state changes.

 


Just sort the instance transform data back-to-front before rendering and you'll be fine.

I thought about that but then if the instances are scattered about a great deal, wouldn't non-instanced (or other instances for that matter) meshes that are positioned in-between fail to end up where they should in a depth sort?



#4 AgentC   Members   -  Reputation: 1413

Like
0Likes
Like

Posted 30 January 2014 - 05:11 PM

You will have to split up the instance batches if necessary to ensure that the back-to-front order isn't violated, even between draw calls.

 

You can do this by sorting all transparent meshes back-to-front, then going through that list from the beginning and building instance batches where possible (when the mesh + other draw state stays same for multiple consecutive meshes).


Every time you add a boolean member variable, God kills a kitten. Every time you create a Manager class, God kills a kitten. Every time you create a Singleton...

Urho3D (engine)  Hessian (C64 game project)


#5 SeanMiddleditch   Members   -  Reputation: 7152

Like
0Likes
Like

Posted 30 January 2014 - 05:29 PM

Not really. As I've been lead to understand by various articles changing rasterizer states, blend states and shaders were to be considered more of a bottleneck (in general) than things like draw order.


If you want correct output, you _must_ draw things in the necessary order, whether it's a bottleneck or not. Some forms of transparency don't need a specific ordering, though. Additive blending, for instance, gives the same results whether you blend A over B over C (where A and B are transparent and C is opaque) or B over A over C. Generally where efficiency matters and you don't want to sort, you _also_ ensure that you are using a blender operation.
 

I thought about that but then if the instances are scattered about a great deal, wouldn't non-instanced (or other instances for that matter) meshes that are positioned in-between fail to end up where they should in a depth sort?


Ah, I misunderstood, sorry. Yes, in this case, instancing will be an issue. Keep your instancing of translucent objects to small clusters to avoid the likelihood of other translucent objects getting in-between the instances. E.g., a single shattered window may be instanced, but you wouldn't want to instance all windows in the scene together unless they're your only translucent objects. This lets instancing help in the most important cases (lots of little pieces) but lets you use translucency elsewhere.

In general, keep your translucent object count fairly low. You might notice they're rare in games outside of particle systems (which often use only additive blending or transparent cutout). That's because they're slow, especially in complex scenes. In a game, having a smooth experience is more important than having a visually complex one. Even modern game engines focus more on lighting opaque surfaces than on adding lots of translucent objects.

A more advanced technique to allow order-independent translucency would be Depth Peeling, but before considering it read Nicol's answer at https://gamedev.stackexchange.com/questions/26239/how-to-properly-implement-alpha-blending-in-a-complex-3d-scene

#6 Husbjörn   Members   -  Reputation: 283

Like
0Likes
Like

Posted 31 January 2014 - 05:24 PM


If you want correct output, you _must_ draw things in the necessary order, whether it's a bottleneck or not.

I'm aware of this, just to make it clear when I was talking about cheating with "cheap" transparent objects I didn't really mean translucent objects but rather close to 1-bit transparent things like floor covers or tree leaves. There may be some blending but it only occurs along the very egdes then and it therefore can be argued it isn't going to be noticible whether these edges blend properly with other transparent objects behind them. Those kinds of meshes would likely make up most of my transparent ones and actually semi transparent ones won't be very many.

 

So what I then understand is that you can (and should) split up instances in several batches when rendering?

I didn't know you could do that (as said I haven't actually gotten around to implement this yet, will see if I get the time during the weekend) but the approach outlined by AgentC seems to make sense then.

 

 

I'm sure I'll be back with more issues before long, but until then thanks for the descriptions and hints.






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