警告 X4000:使用可能未初始化的變量 (warning X4000: use of potentially uninitialized variable)


問題描述

警告 X4000:使用可能未初始化的變量 (warning X4000: use of potentially uninitialized variable)

HLSL 編譯器發出錯誤消息“警告 X4000:使用可能未初始化的變量”,代碼如下:

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);
    }
}

我不明白那個警告。有問題的行中根本沒有使用任何變量!

感謝任何幫助。


參考解法

方法 1:

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;
}

(by fpiettekefren)

參考文件

  1. warning X4000: use of potentially uninitialized variable (CC BY‑SA 2.5/3.0/4.0)

#hlsl #directcompute #directx






相關問題

如何將幾何著色器與輸出流一起使用? (How do you use Geometry Shader with Output Stream?)

поўнаэкранны квадрат у піксельным шэйдары мае экранныя каардынаты? (fullscreen quad in pixel shader has screen coordinates?)

硬編碼 HLSL 著色器 (Hardcoding HLSL Shader)

GLSL和HLSL之間的模型視圖區別? (Modelview Difference between GLSL and HLSL?)

未定義的 TEXCOORD 數量 (Undefined number of TEXCOORDs)

像素著色器總是返回白色 (Pixel shader always returning white)

GLSL / HLSL 著色器中的星球大戰全息效果 (Star Wars holographic effect in GLSL / HLSL shader)

在 GLSL 中混合多個紋理 (Blending multiple textures in GLSL)

警告 X4000:使用可能未初始化的變量 (warning X4000: use of potentially uninitialized variable)

著色器中的點積與直接向量分量總和性能 (Dot product vs Direct vector components sum performance in shaders)

Unity Compute Shader 中調用 numthreads 和 Dispatch 的區別 (Difference Between Calling numthreads and Dispatch in a Unity Compute Shader)

DirectX 11 曲面細分著色器不工作 (DirectX 11 Tesellation Shader Not Working)







留言討論