简体   繁体   English

C ++着色器问题

[英]C++ shader question

I am using Nvidia CG and Direct3D9 and have the question about the following code. 我正在使用Nvidia CGDirect3D9 ,并对以下代码有疑问。

It compiles, but doesn't "loads" (using cgLoadProgram wrapper) and the resulting failure is described simplyas D3D failure happened . 它可以编译,但不会“加载”(使用cgLoadProgram包装器),并且将所导致的故障描述为D3D failure happened

It's a part of the pixel shader compiled with shader model set to 3.0 它是将着色器模型设置为3.0编译的像素着色器的一部分

What may be interesting is that this shader loads fine in the following cases: 可能有趣的是,在以下情况下,此着色器可以正常加载:

1) Manually unrolling the while statement (to many if { } statements). 1)手动展开while语句(到许多if { }语句)。

2) Removing the line with the tex2D function in the loop. 2)在循环中删除带有tex2D功能的线。

3) Switching to shader model 2_X and manually unrolling the loop. 3)切换到着色器模型2_X并手动展开循环。


Problem part of the shader code: 着色器代码的问题部分:

float2 tex = float2(1, 1);
float2 dtex = float2(0.01, 0.01);

float h = 1.0 - tex2D(height_texture1, tex);
float height = 1.00;

while ( h < height )
{
    height -= 0.1;
    tex += dtex;

    // Remove the next line and it works (not as expected,
    // of course)
    h = tex2D( height_texture1, tex );
}

If someone knows why this can happen or could test the similiar code in non-CG environment or could help me in some other way, I'm waiting for you ;) 如果有人知道为什么会发生这种情况,或者可以在非CG环境中测试类似的代码,或者可以通过其他方式帮助我,那么我在等您;)

Thanks. 谢谢。

I think you need to determine the gradients before the loop using ddx/ddy on the texture coordinates and then use tex2D(sampler2D samp, float2 s, float2 dx, float2 dy) 我认为你需要在纹理坐标上使用ddx / ddy确定循环前的渐变,然后使用tex2D(sampler2D samp, float2 s, float2 dx, float2 dy)

The GPU always renders quads not pixels (even on pixel borders - superfluous pixels are discarded by the render backend). GPU总是渲染四边形而不是像素(即使在像素边界上 - 渲染后端丢弃了多余的像素)。 This is done because it allows it to always calculate the screen space texture derivates even when you use calculated texture coordinates. 这样做是因为它允许它始终计算屏幕空间纹理派生,即使您使用计算出的纹理坐标也是如此。 It just needs to take the difference between the values at the pixel centers. 只需取像素中心的值之差即可。

But this doesn't work when using dynamic branching like in the code in the question, because the shader processors at the individual pixels could diverge in control flow. 但这在问题代码中使用动态分支时不起作用,因为单个像素处的着色器处理器的控制流可能会有所不同。 So you need to calculate the derivates manually via ddx/ddy before the program flow can diverge. 因此,您需要先通过ddx / ddy手动计算导数,然后才能使程序流分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM