一、背景
hlsl中有以下代码,
const float2 guassBlurPriority[33] = {
{float2(-16.0, 0.0138)},
{float2(-15.0, 0.0158)},
{float2(-14.0, 0.018)},
{float2(-13.0, 0.0203)},
{float2(-12.0, 0.0226)},
{float2(-11.0, 0.025)},
{float2(-10.0, 0.0274)},
{float2(-9.0, 0.0298)},
{float2(-8.0, 0.0321)},
{float2(-7.0, 0.0343)},
{float2(-6.0, 0.0364)},
{float2(-5.0, 0.0382)},
{float2(-4.0, 0.0397)},
{float2(-3.0, 0.0409)},
{float2(-2.0, 0.0418)},
{float2(-1.0, 0.0424)},
{float2(0.0, 0.0426)},
{float2(1.0, 0.0424)},
{float2(2.0, 0.0418)},
{float2(3.0, 0.0409)},
{float2(4.0, 0.0397)},
{float2(5.0, 0.0382)},
{float2(6.0, 0.0364)},
{float2(7.0, 0.0343)},
{float2(8.0, 0.0321)},
{float2(9.0, 0.0298)},
{float2(10.0, 0.0274)},
{float2(11.0, 0.025)},
{float2(12.0, 0.0226)},
{float2(13.0, 0.0203)},
{float2(14.0, 0.018)},
{float2(15.0, 0.0158)},
{float2(16.0, 0.0138)}
};
/** 进行高斯模糊透明度采样. */
float4 gaussBlurSampling(float2 fTextureCoord, float _degree) {
float4 color = float4(0.0, 0.0, 0.0, 0.0);
float samplingCount = 32.0;
float2 d = float2(_degree*3, 0) / iResolution.xy / samplingCount;
float2 gl_FragCoord = fTextureCoord * iResolution.xy;
float noiseFrequency = 2.0;
for (int i = 0; i < 33; i++) {
float2 priority = guassBlurPriority[i];
float noiseValue = noise2d((gl_FragCoord.xy + priority.xy) * noiseFrequency) - 0.5;
color += tex2D(inputtexture, fTextureCoord + (priority.x + noiseValue) * d) * priority.y;
}
return color /= color.a;
}
函数
gaussBlurSampling
在英特尔显卡下执行正常,但是在英伟达显卡下执行会出现异常,导致渲染出来的画面没有任何模糊效果。
为了解决这一问题,查阅了微软和英伟达的官方文档,试图找到相关的信息,然而在这两个平台上均没有找到,但是在此过程中了解到d3d中hlsl中if else以及for语句的使用细节,记录一下
二、介绍
if else语句
[branch] 选择执行
[flatten]每一行都执行
注意:
The branch attribute may fail if either side of the if statement
contains a gradient function, such as tex2D。
这个里面有两个重要信息:一个是branch属性的分支中不能有tex2D,一个是tex2D属于
gradient function
,这个概念在hlsl文档的很多地方都会出现
for语句
[unroll]拆成一行一行的语句
[roll]默认,正常for语句,但是发生错误时会自动转为[unroll]。比如这个警告:“warning X3570: gradient instruction used in a loop with varying iteration, attempting to unroll the loop”意思是我的代码中在动态迭代中使用了
gradient instruction
,因此将其转为unroll,这也和上文if else的介绍相一致