[英]DirectX HLSL - Constant Buffer empty in Pixel Shader but not Vertex Shader
[英]DirectX HLSL shader implicit truncation of vector type 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);
如果我注釋掉強度= r + g + b + a; 然后編譯項目。 誰能看到我在做什么錯,謝謝。
出現此錯誤的原因是,您要多加/累加float3和float4。 您應該使用float4(float3,1.0f)或float4.xyz(使其變為float3)將float3 float3
到float4
推斷出實際編譯着色器所需的一些“額外”內容(統一,輸入並將代碼制成實際函數),在嘗試對其進行編譯時會得到以下輸出:
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
如您所見,截斷消息只是警告,不是錯誤,除非您使用/WX
進行編譯。 這些警告的問題在於,您正在將紋理樣本的結果分配給float3
,但是返回實際上是float4
。 您可以選擇適當的組件,也可以更改變量類型。 例如:
float4 ppColour = SceneTexture.Sample(PointSample, ppIn.UV);
發生實際錯誤的原因是,您無法在動態循環或條件語句內的循環中進行樣本插值。 在這種情況下,您的循環位於if (intensity > 5.0)
條件中。 您有兩個選擇,要么可以刪除條件,要么可以使用SampleLevel代替Sample:
colour += SceneTexture.SampleLevel(TrilinearSampler, ppIn.UV + PixelKernel[count] * BlurStrength, 0) * BlurWeights[count];
注意:像這樣使用SampleLevel將始終對頂級Mip級別進行采樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.