Sign in to follow this  
relaxok

Multiple draw calls unable to use different OMSetBlendState?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='iedoc' timestamp='1333356231' post='4927405']
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.
[/quote]

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1333401123' post='4927635']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1333402560' post='4927648']
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.
[/quote]

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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this