2

我正在尝试在屏幕上绘制一个带有纹理的四边形,以使纹素和像素完美对齐。听起来很容易。我使用这些着色器绘制了 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) 我得到(我所期望的):
链接到图像(因为我不允许发布图像)

但是对于 outputSize = (102,12) 和 topLeftPos=(0,0.5) 我得到: x=0, y=0.5 的输出
链接到图像(因为我不允许发布图像)

如您所见,有一个 uv 不连续性,其中 2 个三角形连接并且 uv 的插值不准确)。这基本上只发生在 0.5 附近的位置(在 x 和 y 中)(实际上低于 0.49 它正确捕捉到 texel 0 和高于 0.51 它正确捕捉到 texel 1,但在两者之间我得到了这个工件)。

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

4

1 回答 1

1

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

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

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

所以如果你想要完美的映射,你的源方块应该与像素边界对齐,而不是像素中心。

于 2015-01-28T01:02:15.463 回答