在CPU上执行的数学在GPU上有不同的结果

问题描述 投票:0回答:1

我正在尝试让 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 因为分母值非常接近。

opengl glsl
1个回答
2
投票

代码不一样。在 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

© www.soinside.com 2019 - 2024. All rights reserved.