【问题标题】:Math performed on CPU has different result on GPU在 CPU 上执行的数学在 GPU 上具有不同的结果
【发布时间】:2014-11-04 04:20:47
【问题描述】:

我正在尝试让 GLSL 片段着色器根据传入片段的纹理坐标将其扭曲为 poorly simulate a CRT

After the code failed to work,我将它移植到 C++ 以修改纹理的 RGB 值。代码worked as expected

这让我相信我的 GLSL 代码有问题,即使它在 C++ 中镜像并且运行良好。

关于 GLSL 数学,有什么我不知道的地方会导致这种情况吗?

C++ 代码

const unsigned int RED = 0xFFFF0000;
const unsigned int BLUE = 0xFF0000FF;
const float X_MAX = 429.0f/448.0f;
const float Y_MAX = 320.0f/336.0f;
const float X_CORNER = 410.0f/448.0f;
const float Y_CORNER = 306.0f/336.0f;
const float X_COEF = (X_MAX-X_CORNER) / (Y_CORNER * Y_CORNER);
const float Y_COEF = (Y_MAX-Y_CORNER) / (X_CORNER * X_CORNER);
float FUNCX(float y)
{
    return X_MAX-X_COEF*y*y;
}
float FUNCY(float x)
{
    return Y_MAX-Y_COEF*x*x;
}

unsigned int get(glm::vec2 intex)
{
    intex *= 2.0;       // Transform the texture rectangle from 0..1
    intex.x -= 1.0;     //                  to
    intex.y -= 1.0;     //              -1 .. 1
    glm::vec2 d = glm::vec2(0.0,0.0);
    d.x = FUNCX(intex.y); // get the curve amount for X values based on Y input
    d.y = FUNCY(intex.x); // get the curve amount for Y values based on X input
    if (abs(intex.x/d.x) > 1.0) // if the X value is outside of the curve
        return RED;             // draw RED for debugging
    if (abs(intex.y/d.y) > 1.0) // if the Y value is outside of the curve
        return BLUE;            // draw BLUE for debugging
    glm::vec2 outtex = glm::vec2(0.0f,0.0f);
    outtex.x = 1.0 + intex.x/d.x; // Now the -1 .. 1 values get shifted back
    outtex.y = 1.0 + intex.y/d.y; //                to
    outtex /= 2.0;                //               0 .. 1
    return texture.get(512*outtex.x,512*outtex.y);
}

GLSL 片段着色器

const vec4 RED = vec4(1.0,0.0,0.0,1.0);
const vec4 BLUE = vec4(0.0,0.0,1.0,1.0);
const float X_MAX = 429.0/448.0;
const float Y_MAX = 320.0/336.0;
const float X_CORNER = 410.0/448.0;
const float Y_CORNER = 306.0/336.0;
const float X_COEF = (X_MAX-X_CORNER) / (Y_CORNER * Y_CORNER);
const float Y_COEF = (Y_MAX-Y_CORNER) / (X_CORNER * X_CORNER);
float FUNCX(float y)
{
    return X_MAX-X_COEF*y*y;
}
float FUNCY(float x)
{
    return Y_MAX-Y_COEF*x*x;
}

vec4 get(vec2 intex)
{
    intex *= 2.0;       // Transform the texture rectangle from 0..1
    intex.x -= 1.0;     //                  to
    intex.y -= 1.0;     //              -1 .. 1
    vec2 d = vec2(0.0,0.0);
    d.x = FUNCX(intex.y); // get the curve amount for X values based on Y input
    d.y = FUNCY(intex.x); // get the curve amount for Y values based on X input
    if (abs(intex.x/d.x) > 1.0) // if the X value is outside of the curve
        return RED;             // draw RED for debugging
    if (abs(intex.y/d.y) > 1.0) // if the Y value is outside of the curve
        return BLUE;            // draw BLUE for debugging
    vec2 outtex = vec2(0.0,0.0);
    outtex.x = 1.0 + intex.x/d.x; // Now the -1 .. 1 values get shifted back
    outtex.y = 1.0 + intex.y/d.y; //                to
    outtex /= 2.0;                //               0 .. 1
    return texture2D(texture,outtex);
}

注意:超级马里奥世界图片仅用于测试目的。 注2:C++代码中的512值是使用的纹理大小。

编辑:GLSL 代码中有一个错字,其中 y 值除以 x 而不是 y。这个问题已经修复,new output is the same 因为分母值非常接近。

【问题讨论】:

  • 我确实看到了不同 Nvidia GPU 之间浮点结果的差异(即小舍入差异)。

标签: opengl glsl


【解决方案1】:

代码不一样。在 C++ 代码中:

if (abs(intex.y/d.y) > 1.0) // if the Y value is outside of the curve
    return BLUE;            // draw BLUE for debugging

在 GLSL 代码中:

if (abs(intex.y/d.x) > 1.0) // if the Y value is outside of the curve
    return BLUE;            // draw BLUE for debugging

C++ 版本除以d.y,GLSL 版本除以d.x

【讨论】:

  • 谢谢。我进行了更改,输出与以前相同,因为值几乎相等。
【解决方案2】:

所使用的公式仅支持从 0 到 1 的 UV 值。如果使用了超出该范围的值,则公式将失效。

【讨论】:

    猜你喜欢
    • 2016-07-16
    • 2015-07-24
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2021-09-21
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多