問題描述
警告 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;
}