gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:54 25 4
gpt4 key购买 nike

我正在尝试在屏幕上绘制一个带有纹理的四边形,以使纹素和像素完美对齐。听起来很容易。我使用这些着色器绘制了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com