# HLSL debugging: Damn those conditionals!

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a pretty basic problem with debugging my shader. It works as such, I can make a breakpoint and step through the code - however, when I reach a conditional (if/else), it just walks past it, no matter what it would evaluate as. After it goes through the if-condition, it happily proceeds to ignore the else and goes through that code as well! But when the code runs without breakpoints, there is no problems, and it is obvious that the conditional works just fine. ARGH! Help me please, I can't see what I am doing wrong. This is how I set up the device:
DWORD behaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

hr = m_pD3D->CreateDevice( m_d3dSettings.AdapterOrdinal(), D3DDEVTYPE_REF, m_hWndFocus, behaviorFlags, &m_d3dpp, &m_pd3dDevice );


And this is how I set up the shader (DEBUG_VS and DEBUG_PS are set):
	DWORD dwShaderFlags = 0;

#ifdef DEBUG_VS
#endif
#ifdef DEBUG_PS
#endif

// If this fails, there should be debug output as to
// they the .fx file failed to compile
return false;


The technique looks like this:
technique RenderScene
{
pass p0
{
}
}


I assume that the VS and PS versions are good enough, since the conditionals do work when I don't set breakpoints...

##### Share on other sites
For 1.x shader targets, conditionals don't act as true flow control.

Usually what happens with an if/blah/else/blah in shader versions without true flow control is both the true and false results are computed then the relevent one selected based on the comparison. If you look at the disassembly for your shader you'll see this.

That's why you'll sometimes see the debugger step over both conditions of the if/else.

##### Share on other sites
Ok, that can explain the VS, but how about the PS? It is 2.0?

##### Share on other sites
While VS 2.0 has flow control instructions, PS 2.0 does not. However, PS 2.x and 3.0 do have flow control instructions.

##### Share on other sites
A couple of things:

1) You will get very strange results if you don't compile your shader with optimizations turned off. Code gets jumbled around quite a bit.

2) Most of the time you won't see flow control happen at the hardware level. You'll only see it in ps_3_0, yet most of the time it is impossible to use. Among the most common complaint I get is "Why doesn't my shader use an if statement?" And usually this is just because there was something like gradient (texture loads) inside the flow control.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 41
• ### Forum Statistics

• Total Topics
634837
• Total Posts
3019558
×