简体   繁体   中英

DirectX HLSL shader implicit truncation of vector type error

Hi I'm getting an error in one my pixel shaders, implicit truncation of vector type.

Here is the code causing the error:

float3 colour = 0;
float3 ppColour = SceneTexture.Sample(PointSample, ppIn.UV);
float4 col = SceneTexture.Sample(PointSample, ppIn.UV);
float intensity = 0.0f;
float r = SceneTexture.Sample(PointSample, ppIn.UV).r;
float g = SceneTexture.Sample(PointSample, ppIn.UV).g;
float b = SceneTexture.Sample(PointSample, ppIn.UV).b;
float a = SceneTexture.Sample(PointSample, ppIn.UV).a;
intensity = r + g + b + a;

if (intensity > 5.0f)
{
    for (int count = 0; count < 13; count++)
    {
        colour += SceneTexture.Sample(TrilinearSampler, ppIn.UV + PixelKernel[count] * BlurStrength) * BlurWeights[count];
    }
    return float4(colour, 1.0f);
}

return float4(ppColour, 1.0f);

If I comment out intensity = r + g + b + a; then the project compiles. Can anyone see what I'm doing wrong, thanks.

The reason you get this error is, that you are mulitplying/adding up float3's and float4's. You should 'cast' float3 to float4 with float4(float3, 1.0f) or float4.xyz (which makes it float3)

Inferring some of the 'extra' stuff that is required to actually compile the shader (uniforms, inputs, and making the code into an actual function), I get the following output when trying to compile it:

test.ps(16,9): warning X3206: implicit truncation of vector type
test.ps(29,11): warning X3206: implicit truncation of vector type
test.ps(29,11): error X4014: cannot have gradient operations inside loops with divergent flow control

As you can see, the truncations messages are just warnings, not errors, unless you are using /WX to compile. The issue with these warnings, is that you are assigning the result of a texture sample to a float3 , but the return is actually a float4 . You can either select the appropriate components with a swizzle, or, change the variable type. For example:

float4 ppColour = SceneTexture.Sample(PointSample, ppIn.UV);

The reason for the actual error, is that you cannot do sample interpolation in dynamic loops, or loops inside conditional statements. In this case, your loop is inside the if (intensity > 5.0) conditional. You have two options, either you can remove the conditional, or, you can use SampleLevel instead of Sample:

colour += SceneTexture.SampleLevel(TrilinearSampler, ppIn.UV + PixelKernel[count] * BlurStrength, 0) * BlurWeights[count];

Note: using SampleLevel like this will always sample the top mip-level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM