Multiple draw calls unable to use different OMSetBlendState?

Started by
8 comments, last by MJP 12 years ago
Hi!

I have a situation that boggles my mind. If i have two meshes, and i use OMSetBlendState before drawing each one (with DrawIndexed), both of them seem to be using whatever the most recent blend state that was set. EIther that, or the map/unmap of the lighting properties buffer is only using the most recent settings. Using PIX seems unreliable here but if it's not bugging out, it would seem to be the latter.

Basically, the first mesh is drawn with an alpha blending blend state, the second mesh is drawn with blending disabled (opaque). If i draw only the first mesh, it's correctly rendered - a plant texture with alpha blending around the branches. If i draw the second mesh after the first mesh, which disables alpha blending (for the 2nd mesh draw only), the first mesh that was working fine is also drawing opaquely.

Can anybody give me any ideas of things to try? OMSetBlendState should only be per draw call right?

I'm out of ideas... I'm not sure what other info would be relevant but feel free to ask..

Thanks
Advertisement
try drawing transparent objects last. You should always draw the transparent objects last anyway so that they have something to blend with on the back buffer. if they are drawn first, they will only blend with whatever is drawn before them, which in this case, it sounds like nothing is drawn before the transparent object, so it probably doesn't appear transparent if it happens to be in front of the opaque object, since the opaque object is drawn last, hope that makes sense

Otherwise, try just adding the line "clip(diffuse.a - 0.25f);" in your pixel shader. this will "clip" or just not draw any pixels with an alpha value of less than .25.
all state changes and or new resources sent to the GPU will affect everything after. so if you want to use the SAME blendstate for both, just se it befor drawing both.

And chaning to correct states per drawcall is insane, sort them and draw them in batches rahter than per state.
And for alpha, you need to draw alpha after all the other drawing. and you have to sort then so that the object farest away is drawn first, and the closest is drawn last.

EA, the gpu dont know what to do if you dont sort your alpha draws.

hope this make sense.
"There will be major features. none to be thought of yet"

try drawing transparent objects last. You should always draw the transparent objects last anyway so that they have something to blend with on the back buffer. if they are drawn first, they will only blend with whatever is drawn before them, which in this case, it sounds like nothing is drawn before the transparent object, so it probably doesn't appear transparent if it happens to be in front of the opaque object, since the opaque object is drawn last, hope that makes sense

Otherwise, try just adding the line "clip(diffuse.a - 0.25f);" in your pixel shader. this will "clip" or just not draw any pixels with an alpha value of less than .25.


Hmm let me try to be more clear.. i'm drawing 2 meshes

1) uses a transparent blend state, imagine a model of a plant where you want to not render the parts of the texture that have 0 alpha
2) drawn opaquely - blending turned off. this is just a regular basic textured model mesh

the two meshes are not the same object, the geometry is in different areas.

i do the following

1) set blend state to the alpha enabled state (your basic alpha, inv alpha blend)
2) draw mesh #1 (needs this transparency)
3) set blend state to the blend disabled state
4) draw mesh #2 (needs to be opaque)

The result on the backbuffer is that mesh #1 is drawn opaquely, not just mesh #2.

If i change the code to draw mesh #1 only, it is drawn correctly with transparency.

This seems to indicate that the first blend state I set is not used for the first draw call.

Regardless of best practices for rendering order, doesn't this seem wrong?
OMSetBlendState isn't so much 'per draw call' as it is 'set until it is changed again'.

Based on what you are seeing it sounds like on the 2nd run of the rendering loop the blend state isn't being set correctly, which I personally would attribute to a bug in your code rather than any sort of DX/driver issues.

The best advice I can give is to look at the device state at each draw call in PIX to see what is going on; you'll probably want to capture a couple of frames and certainly will want to grab the first frame to see if your state is even being set correctly.

OMSetBlendState isn't so much 'per draw call' as it is 'set until it is changed again'.

Based on what you are seeing it sounds like on the 2nd run of the rendering loop the blend state isn't being set correctly, which I personally would attribute to a bug in your code rather than any sort of DX/driver issues.

The best advice I can give is to look at the device state at each draw call in PIX to see what is going on; you'll probably want to capture a couple of frames and certainly will want to grab the first frame to see if your state is even being set correctly.


Speaking of PIX this kind of relates to the other issue -- if i use PIX, the values in the lighting params constant buffer seem to be wrong as if they're using the values from the most recent map/unmap to the cbuffer. The same deal as the above, im updating the lighting cbuffer inbetween draw calls but in PIX when i debug that first draw call, the registers show the values of what would be the cbuffer state used for the 2nd draw call.

I thought maybe these two items were both related to some setup/config error, where all the draw calls are actually deferred to the end and only use the 'last' states set in the frame.

If i debug the copy to cbuffer, or the OMSetBlendState and look at the values they're 100% correct. It is however wrong in practice and in PIX.

Again, PIX shows cbuffer values set inbewteen 1st and 2nd draw call, even when looking at the device state during the first draw call.
Assuming you aren't using deferred contexts then all draw calls are executed when the draw functions are called' nothing is deferred until 'later' and all state during execution would be as it is when that call is made.

Assuming you aren't using deferred contexts then all draw calls are executed when the draw functions are called' nothing is deferred until 'later' and all state during execution would be as it is when that call is made.


I am using deferred contexts but only to prepare buffers and stuff, not when drawing...

Everyone always tells me it's just a PIX bug if it shows wrong values in registers and that it's not reliable.. I guess i can't really be sure until i get a machine with 2 videcoards and can debug shaders with nsight?

This is flat out insane that it seems to be doing this..
I finally figured out the issue.

Apparently, you can't bind a cbuffer to to register cb1 and set it in code to slot 1, if there's nothing in slot 0. If you do, the shader will just use garbage data for the values in your cbuffer structure (well, seemed to be 0's in this case).

I don't know if this is a driver bug or just a quirk, but there you go.
It may be a driver bug. You can always try with the REF device to see if it produces the correct behavior.

This topic is closed to new replies.

Advertisement