繁体   English   中英

像素和顶点着色器中的缩放问题

[英]Scaling issue in pixel and vertex shader

我有两个着色器:像素和顶点。
他们都旋转图片。
其中一个阶段是缩放图片。 两种情况下的比例因子都是 0.77328159。
遵循着色器和过程的结果。
问题是:为什么对于像素着色器,我们通过缩放因子来划分,而对于顶点着色器,我们将它相乘?
比例因子越大,结果图像越模糊。

像素着色器

float fRadAngle = radians(fAngle);
float2 ptOrigin = input.Tex;
float2 ptPos = ptOrigin;
ptOrigin.x = (ptOrigin.x - 0.5) * fWidthSrc;
ptOrigin.y = (ptOrigin.y - 0.5) * fHeightSrc;
ptPos.x = ptOrigin.x * cos(fRadAngle) - ptOrigin.y * sin(fRadAngle);
ptPos.y = ptOrigin.x * sin(fRadAngle) + ptOrigin.y * cos(fRadAngle);
ptPos.x = ptPos.x / fWidthSrc;
ptPos.y = ptPos.y / fHeightSrc;
ptPos.x = ptPos.x / fScale + 0.5;
ptPos.y = ptPos.y / fScale + 0.5;
float4 pxRGB = float4( 0.0, 0.0, 0.0, 0.0 );
if( ptPos.x >= 0 && ptPos.x <= 1 && ptPos.y >= 0 && ptPos.y <= 1 )
    pxRGB = tx.Sample(sampler_in, ptPos);
return pxRGB;

顶点着色器

float2 ptPos = input.Pos.xy;
float2 ptOrigin = ptPos;
ptOrigin.x = (ptOrigin.x - 0.5) * fWidthSrc;
ptOrigin.y = (ptOrigin.y - 0.5) * fHeightSrc;
ptPos.x = ptOrigin.x * cos(fzRadAngle) - ptOrigin.y * sin(fzRadAngle);
ptPos.y = ptOrigin.x * sin(fzRadAngle) + ptOrigin.y * cos(fzRadAngle);
ptPos.x = ptPos.x / fWidthSrc;
ptPos.y = ptPos.y / fHeightSrc;
ptPos.x = ptPos.x * fScale + 0.5;
ptPos.y = ptPos.y * fScale + 0.5;

output.Pos.xy = ptPos.xy;
return output;

关注: ptPos.x / fScaleptPos.x * fScale

缩放比例 ~ 0.77 的 PS 结果:
(对于 VS 基本相同)

0.7 缩放

PS 缩放 0.1 的结果:
(对于 VS 基本相同)

0.5 缩放

再次。 PS具有逻辑行为。 缩放因子越大,我们得到的图像越小,因此除以较小的数字会产生更大的数字...
为什么VS结果与乘法相同?

在所写的像素着色器中,缩放因子应用于 [0, 1] 范围内的 UV 坐标 (ptPos.xy),用于对纹理进行采样。 正如您所提到的,将此 UV 除以一个减小的值将缩小纹理采样,从而使图像缩小。 发生这种情况是因为 UV 坐标与此 PS 中的像素 position 相关,并且随着缩放因子的增加,更多的像素将映射到 [0,1]。

顶点着色器输出形成基元的顶点,在这些基元上按像素调用像素着色器。 将 VS 中的每个顶点乘以增加的缩放因子将有效地增加执行像素着色器的屏幕空间区域。 这就是为什么将 output 顶点 position 乘以 VS 中增加的缩放因子会产生更大的缩放图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM