pow(0, 2.2) 在 hlsl 像素着色器中给出 1?
但 pow(0, 2.0) 给出 0
似乎任何浮点指数给出 1,而整数指数给出 0。
我正在使用 DirectX 9 和 hlsl 编译器“D3DCompiler_43.dll”。 在 Nvidia 和 Ati 卡上证实了这一点。
我很困惑! 这是某种已知的行为或错误吗?
为了说明效果,请尝试以下简单的测试着色器:
// Test shader demonstrating strange pow behaviour
// when brightness becomes 0 resulting color will jump to white
float4x4 WorldViewProjXf : WorldViewProjection < string UIWidget="None";>;
float Brightness
<
string UIName = "Brightness";
float UIMin = 0;
float UIMax = 1;
> = 0;
struct VS_Input
{
float4 position : POSITION0;
};
struct VS_Output
{
float4 position : POSITION0;
};
VS_Output Vertex_Func( VS_Input in_data )
{
VS_Output outData;
outData.position = mul(in_data.position, WorldViewProjXf);
return outData;
}
float4 Fragment_Func( VS_Output in_data ) : COLOR
{
return pow(Brightness, 2.2);
}
technique Main
{
pass p0
{
VertexShader = compile vs_3_0 Vertex_Func();
PixelShader = compile ps_3_0 Fragment_Func();
}
}
But pow(0, 2.0) gives 0
Seems that any float exponent gives 1 while integer exponents give 0.
I am using DirectX 9 and hlsl compiler "D3DCompiler_43.dll".
Confirmed that on Nvidia and Ati cards.
I am confused!
Is that some kind of known behaviour or bug?
To illustrate the effect try the following simple test shader:
// Test shader demonstrating strange pow behaviour
// when brightness becomes 0 resulting color will jump to white
float4x4 WorldViewProjXf : WorldViewProjection < string UIWidget="None";>;
float Brightness
<
string UIName = "Brightness";
float UIMin = 0;
float UIMax = 1;
> = 0;
struct VS_Input
{
float4 position : POSITION0;
};
struct VS_Output
{
float4 position : POSITION0;
};
VS_Output Vertex_Func( VS_Input in_data )
{
VS_Output outData;
outData.position = mul(in_data.position, WorldViewProjXf);
return outData;
}
float4 Fragment_Func( VS_Output in_data ) : COLOR
{
return pow(Brightness, 2.2);
}
technique Main
{
pass p0
{
VertexShader = compile vs_3_0 Vertex_Func();
PixelShader = compile ps_3_0 Fragment_Func();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 HLSL 文档,
pow(x, y)
似乎直接实现为exp(y * log(x))
。由于您的问题中存在x=0
,请再次查看文档log(x) == -INF
。似乎 y 的值此时并不重要,只要它是正数且大于零即可。您可能会不小心将
pow(0.0, 2.0) == 0.0
与pow(0.0, 0.0) == 1.0
进行比较。这是我对正在发生的事情的最好猜测。日志(0) =-INF
参考pow = exp(y * log(x) )
参考Looking at the HLSL docs,
pow(x, y)
appears to be implemented directly asexp(y * log(x))
. Sincex=0
in your question, again looking at the docslog(x) == -INF
. It seems like the value ofy
shouldn't matter at that point as long as it's positive and greater than zero.You might be accidentally comparing
pow(0.0, 2.0) == 0.0
withpow(0.0, 0.0) == 1.0
. That's my best guess for what's happening.log(0)=-INF
referencepow = exp(y * log(x))
reference