HLSL 循环/采样问题

发布于 2024-08-03 00:55:45 字数 620 浏览 12 评论 0原文

我有一段 HLSL 代码,如下所示:

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip--)
    {
        indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}

我得到的结果与仅执行一次的循环一致。我检查了 PIX 中的着色器,事情变得更加奇怪,指示代码中位置的黄色箭头进入循环,遍历一次,然后跳回到开头,此时黄色箭头再也不会移动,但光标移动代码并返回结果(PIX 中的错误,或者我只是使用错误?)

我怀疑这可能是与纹理读取被编译器移出循环有关的问题,但是我认为由于我手动设置 LOD,所以 tex2Dlod 没有发生这种情况:/

所以:

1)有什么问题?

2)有什么建议的解决方案吗?

I have a piece of HLSL code which looks like this:

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip--)
    {
        indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}

The results I am getting are consistent with that loop only executing once. I checked the shader in PIX and things got even more weird, the yellow arrow indicating position in the code gets to the loop, goes through it once, and jumps back to the start, at that point the yellow arrow never moves again but the cursor moves through the code and returns a result (a bug in PIX, or am I just using it wrong?)

I have a suspicion this may be a problem to do with texture reads getting moved outside the loop by the compiler, however I thought that didn't happen with tex2Dlod since I'm setting the LOD manually :/

So:

1) What's the problem?

2) Any suggested solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-08-10 00:55:45

问题解决了,这是一个简单的编码错误,我需要在每次迭代中增加 mip 级别,而不是减少它。

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip++)
    {
        indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}

Problem was solved, it was a simple coding mistake, I needed to increase mip level on each iteration, not decrease it.

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip++)
    {
        indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文