Sampler referencing in HLSL - Sampler parameter must come from a literal expression

The following method works fine when referencing a sampler in HLSL

float3 P = lightScreenPos;
float depth;

if (alpha >= 0.5f)
{
// Reference the correct sampler

// Front hemisphere 'P0'
P.z = P.z + 1.0;
P.x = P.x / P.z;
P.y = P.y / P.z;
P.z = lightLength / LightAttenuation.z;

// Rescale viewport to be [0, 1] (texture coordinate space)
P.x = 0.5f * P.x + 0.5f;
P.y = -0.5f * P.y + 0.5f;

depth = 1.0 - depth;
}
else
{
// Reference the correct sampler

// Back hemisphere 'P1'
P.z = 1.0 - P.z;
P.x = P.x / P.z;
P.y = P.y / P.z;
P.z = lightLength / LightAttenuation.z;

// Rescale viewport to be [0, 1] (texture coordinate space)
P.x = 0.5f * P.x + 0.5f;
P.y = -0.5f * P.y + 0.5f;

depth = 1.0 - depth;
}

// [Standard Depth Calculation]
float mydepth = P.z;
shadow = depth + Bias.x < mydepth ? 0.0f : 1.0f;

If I try and do anything with the sampler reference outside the if statement then I get the following error:

Sampler parameter must come from a literal expression

This code demonstrates that

float3 P = lightScreenPos;

if (alpha >= 0.5f)
{
// Reference the correct sampler

// Front hemisphere 'P0'
P.z = P.z + 1.0;
P.x = P.x / P.z;
P.y = P.y / P.z;
P.z = lightLength / LightAttenuation.z;
}
else
{
// Reference the correct sampler

// Back hemisphere 'P1'
P.z = 1.0 - P.z;
P.x = P.x / P.z;
P.y = P.y / P.z;
P.z = lightLength / LightAttenuation.z;
}

// Rescale viewport to be [0, 1] (texture coordinate space)
P.x = 0.5f * P.x + 0.5f;
P.y = -0.5f * P.y + 0.5f;

// [Standard Depth Calculation]
depth = 1.0 - depth;
float mydepth = P.z;
shadow = depth + Bias.x < mydepth ? 0.0f : 1.0f;

How can I reference the sampler in this manner without triggering the error?

-
I'm not sure, but have you tried to create a function and pass it as an argument? – Blau Aug 4 '12 at 22:36
That works but I end up with lots of functions with many arguments. It's a solution of sorts. – user1423893 Aug 5 '12 at 16:29