1

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.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • `any(XY >= SourceSize)` looks suspicious, why would you check bits of boolean value? – user7860670 Aug 31 '19 at 16:00
  • @VTT The `if` operator requires single component bools. Comparing multiple components variables returns multiple component bools, that you convert to single component bools with `any()` or `all()`. – kefren Sep 01 '19 at 10:37

1 Answers1

3

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;
}
kefren
  • 1,042
  • 7
  • 12
  • I posted from iPad and cannot check my code, if there are typos let me know so I can correct them (or feel free to correct them yourself in my answer) – kefren Sep 01 '19 at 10:31
  • It works using an intermediate variable as you said. I marked your answer as accepted. Thanks a lot. – fpiette Sep 03 '19 at 06:25
  • Great, I'm happy I've helped. Btw, I see you have not accepted the answers to the 2 previous questions. I'd like to know if they did not help you, and if you decided to do things some other (better) way….As you've seen, compute shaders are not where you get thousands of people, and if you've found better ways it might be instructive for me. – kefren Sep 04 '19 at 19:08
  • I have accepted asnwers for previous questions. If I miss one, let me know which. Learning direct compute is not easy, there are a lot of traps and the compiler has strange behaviour for a beginner [in HLSL] like me.Thanks for your help! – fpiette Sep 05 '19 at 09:11
  • I agree, GPGPU is not easy at all. After all, consider that we are using the GPU for something it was not meant to do...part of the difficulty is that there’s very little documentation, and very few people doing it, and this that can do it usually keep their secrets, because it’s a sort of niche job...And it’s considered sort of magic by the rest of the coders :D – kefren Sep 08 '19 at 22:34