首页 > 解决方案 > warning X4000: use of potentially uninitialized variable

问题描述

HLSL compiler emit the error message "warning X4000: use of potentially uninitialized variable" with the following code:

float4 GetPixelColorFromRawImage(
    in ByteAddressBuffer Source,
    in uint2             SourceSize,
    in uint2             XY)
{
    // Check if within range
    if (any(XY >= SourceSize))
        return float4(0.5, 0.0, 0.0, 1.0);  // <<<==== WARNING HERE

    if (BytesPerPixel == 3) {
        // 24 bits RGB color image
        uint4 RGBA = GetPixelRGBAFromRawImage(Source, SourceSize, XY);
        return float4(RGBA.r / 256.0,
                      RGBA.g / 256.0,
                      RGBA.b / 256.0,
                      RGBA.a / 256.0);
    }
    else if (BytesPerPixel == 2) {
        // 16 bit grayscale image
        uint  Gray1 = GetPixel16BitGrayFromRawImage(Source, SourceSize, XY);
        uint  Gray2 = GetByteFromUInt(LUT16.Load(Gray1 & (~3)), Gray1 & 3);
        float Gray3 = (float)Gray2 / 256.0;
        return float4(Gray3, Gray3, Gray3, 1.0);
    }
    else {
        return float4(0.0, 0.0, 0.0, 1.0);
    }
}

I don't understand that warning. There is no variable at all used in the offending line!

Any help appreciated.

标签: directxhlsldirectcompute

解决方案


The compiler sometimes goes crazy with intermediate return calls, and gives errors where there should be none.

You can try a little workaround.

In the beginning of your method, define and instantiate a variable, then update it in the ifs and the return it.

float4 GetPixelColorFromRawImage(
    in ByteAddressBuffer Source,
    in uint2             SourceSize,
    in uint2             XY)
{
    float4 returnVar = float4(0.0, 0.0, 0.0, 0.0);
    // Check if within range
    if (any(XY >= SourceSize))
        returnVar = float4(0.5, 0.0, 0.0, 1.0);  

    if (BytesPerPixel == 3) {
        // 24 bits RGB color image
        uint4 RGBA = GetPixelRGBAFromRawImage(Source, SourceSize, XY);
        returnVar = float4(RGBA.r / 256.0,
                      RGBA.g / 256.0,
                      RGBA.b / 256.0,
                      RGBA.a / 256.0);
        }
    else if (BytesPerPixel == 2) {
        // 16 bit grayscale image
        uint  Gray1 = GetPixel16BitGrayFromRawImage(Source, SourceSize, XY);
        uint  Gray2 = GetByteFromUInt(LUT16.Load(Gray1 & (~3)), Gray1 & 3);
        float Gray3 = (float)Gray2 / 256.0;
        returnVar = float4(Gray3, Gray3, Gray3, 1.0);
        }
    else {
        returnVar = float4(0.0, 0.0, 0.0, 1.0);
        }
    return returnVar;
}

推荐阅读