c++ - D3D11/C++ 像素着色器中的 uv 插值不准确。如何避免?

标签 c++ direct3d11 uv-mapping

我正在尝试在屏幕上绘制一个带有纹理的四边形,以使纹素和像素完美对齐。听起来很容易。我使用这些着色器绘制了 2 个三角形(作为 TRIANGLE_LIST,所以有 6 个顶点):

struct VSOutput
{
    float4 position     : SV_POSITION;
    float2 uv           : TEXCOORD0;
};

VSOutput VS_Draw(uint index : SV_VertexId)
{
    uint vertexIndex = index % 6;
    // compute face in [0,0]-[1,1] space
    float2 vertex = 0;
    switch (vertexIndex)
    {
        case 0: vertex = float2(0, 0);  break;
        case 1: vertex = float2(1, 0);  break;
        case 2: vertex = float2(0, 1);  break;
        case 3: vertex = float2(0, 1);  break;
        case 4: vertex = float2(1, 0);  break;
        case 5: vertex = float2(1, 1);  break;
    }
    // compute uv
    float2 uv = vertex;
    // scale to size
    vertex = vertex * (float2)outputSize;
    vertex = vertex + topLeftPos;
    // convert to screen space
    VSOutput output;
    output.position = float4(vertex / (float2)outputSize * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0, 1);
    output.uv = uv;
    return output;
}

float4 PS_Draw(VSOutput input) : SV_TARGET
{
    uint2 pixelPos = (uint2)(input.uv * (float2)outputSize);
    // output checker of 4x4
    return (((pixelPos.x >> 2) & 1) ^ ((pixelPos.y >> 2) & 1) != 0) ? float4(0, 1, 1, 0) : float4(1, 1, 0, 0);
}

其中 outputSize 和 topLeftPos 是常量,以像素为单位表示。

现在对于 outputSize = (102,12) 和 topLeftPos=(0,0) 我得到(我所期望的):
link to image (as i'm not allowed to post images)

但是对于 outputSize = (102,12) 和 topLeftPos=(0,0.5) 我得到:x=0, y=0.5 的输出
link to image (as i'm not allowed to post images)

如您所见,有一个 uv 不连续点,其中 2 个三角形连接并且 uv 插值不准确)。这基本上只发生在 .5 周围的位置(在 x 和 y 中)(实际上低于 .49 它正确地捕捉到纹素 0 和高于 .51 它正确地捕捉到纹素 1,但在两者之间我得到这个工件)。

现在我需要这个,因为像素完美映射是必不可少的。谁能告诉我为什么会这样?

最佳答案

要了解正在发生的事情,您需要考虑两件事:

  • 窗口空间中的像素角具有整数坐标,窗口空间中的像素中心具有半整数坐标。
  • 当三角形被光栅化时,D3D11 将所有属性插入到像素中心

所以发生的事情是当 topLeftPos=(0,0) 时,input.uv * (float2)outputSize 的值总是半整数,并且它始终向下舍入到最接近的整数。但是,当 topLeftPos=(0,0.5) 时,(input.uv * (float2)outputSize).y 应始终为整数。但是,由于不可预测的浮点精度问题,它有时会比精确整数小一点,在这种情况下会向下舍入太多。这是您看到拉伸(stretch)方 block 的地方。

因此,如果您想要完美的映射,您的源方 block 应该与像素边界而不是像素中心对齐。

关于c++ - D3D11/C++ 像素着色器中的 uv 插值不准确。如何避免?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922557/

相关文章:

c++ 11 - 使用目录

c++ - enable_if 似乎在类外工作但不在类内

c# - 在非托管 C++ 中创建的 DX11 设备,用于 WPF 窗口

javascript - obj 文件中的 Threejs UV 映射

opengl中的Java vbo,索引纹理坐标,可能吗?

c++ - 为什么在使用标志 ios::ate 和 ios::out 打开时 fstream 被截断?

c++ - D3D11 中语义和顶点布局的意义是什么?

pixel-shader - 像素着色器实际上是做什么的?

c++ - 将 UV 坐标从 OpenGL 标准化为 Vulkan

c++ - 对一元运算如何导致此输出感到困惑