Sampler referencing in HLSL - Sampler parameter must come from a literal expression
Posted
by
user1423893
on Game Development
See other posts from Game Development
or by user1423893
Published on 2012-08-04T10:56:01Z
Indexed on
2012/09/07
3:49 UTC
Read the original article
Hit count: 1612
The following method works fine when referencing a sampler in HLSL
float3 P = lightScreenPos;
sampler ShadowSampler = DPFrontShadowSampler;
float depth;
if (alpha >= 0.5f)
{
// Reference the correct sampler
ShadowSampler = DPFrontShadowSampler;
// 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 = tex2D(ShadowSampler, P.xy).x;
depth = 1.0 - depth;
}
else
{
// Reference the correct sampler
ShadowSampler = DPBackShadowSampler;
// 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 = tex2D(ShadowSampler, P.xy).x;
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;
sampler ShadowSampler = DPFrontShadowSampler;
if (alpha >= 0.5f)
{
// Reference the correct sampler
ShadowSampler = DPFrontShadowSampler;
// 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
ShadowSampler = DPBackShadowSampler;
// 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]
float depth = tex2D(ShadowSampler, P.xy).x;
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?
© Game Development or respective owner