According to
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509610(v=vs.85).aspx
HLSL supports conditional operator (since version 2.0? I am using 4_0)
I need to set blending state before each Draw call. In FX file I’m doing this:
BlendState BlendNone
{
AlphaToCoverageEnable = FALSE;
BlendEnable[0] = FALSE;
};
BlendState BlendSrcAlphaOne
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = INV_SRC_ALPHA;
BlendOp = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
BlendState BlendSrcAlphaOneMinusSrcAlpha
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = ONE;
BlendOp = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
int g_Blend;
technique10 Render
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( CompileShader( gs_4_0, GS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
SetRasterizerState(rsCullNone);
SetDepthStencilState( EnableDepth, 0 );
if (g_Blend == 0)
{
SetBlendState( BlendNone, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
else if (g_Blend == 1)
{
SetBlendState( BlendSrcAlphaOne, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
else if (g_Blend == 2)
{
SetBlendState( BlendSrcAlphaOneMinusSrcAlpha, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
}
}
This does not compile with error:
error X3000: syntax error: unexpected token ‘if’
If I move the whole block into a separate function like:
void setBlend()
{
if (g_Blend == 0)
{
SetBlendState( BlendNone, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
else if (g_Blend == 1)
{
SetBlendState( BlendSrcAlphaOne, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
else if (g_Blend == 2)
{
SetBlendState( BlendSrcAlphaOneMinusSrcAlpha, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
}
}
technique10 Render
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( CompileShader( gs_4_0, GS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
SetRasterizerState(rsCullNone);
SetDepthStencilState( EnableDepth, 0 );
setBlend();
}
}
it doesn’t compile with error:
error X3000: this FX API is not available in this part your program (SetBlendState)
So, is if supported in HLSL?
Ok, after lots of attempts and reading
http://msdn.microsoft.com/en-us/library/windows/desktop/bb205052(v=vs.85).aspx#Blend
This compiles:
SetBlendState is not strictly a function call and conditional operators are not allowed inside the pass {}. However it’s allowed to pass array of state objects with index instead of just state objects.