纹理内存-tex2D基础 [英] Texture memory-tex2D basics

查看:642
本文介绍了纹理内存-tex2D基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用纹理内存时,我遇到了以下代码: -

While using texture memory I have come across the following code:-

uint f = (blockIdx.x * blockDim.x) + threadIdx.x;
uint c = (blockIdx.y * blockDim.y) + threadIdx.y;

uint read = tex2D( refTex, c+0.5f, f+0.5f);

我的问题是为什么我们添加 0.5f c f
这让我感到困惑。
thankyou

My question is why do we add 0.5f to both c and f? This confuses me.. thankyou

推荐答案

在图形中,纹理是一组样本描述了表面的视觉外观。样本是一个点。也就是说,它没有大小(与具有物理大小的像素相反)。当使用样本来确定像素的颜色时,每个样本被定位在其对应像素的精确中心。当寻址具有整数坐标的像素时,给定像素的确切中心成为它的整数坐标加上偏移量0.5(在每个维度上)。

In graphics, a texture is a set of samples that describes the visual appearance of a surface. A sample is a point. That is, it has no size (as opposed to a pixel that has a physical size). When using samples to determine the colors of pixels, each sample is positioned in the exact center of its corresponding pixel. When addressing pixels with whole number coordinates, the exact center for a given pixel becomes its whole number coordinate plus an offset of 0.5 (in each dimension).

换句话说,对纹理坐标添加0.5可以确保当从这些坐标读取时,返回该像素的样本的确切值。

In other words, adding 0.5 to texture coordinates ensures that, when reading from those coordinates, the exact value of the sample for that pixel is returned.

但是,只有当 filterMode ,因为纹理已设置为 cudaFilterModeLinear ,从纹理读取的值在像素内变化。在该模式中,从不在像素的精确中心的坐标读取返回在给定像素的样本和相邻像素的样本之间内插的值。因此,对整数坐标添加0.5有效地否定 cudaFilterModeLinear 模式。但是,由于在纹理坐标中添加0.5会占用内核中的周期,因此最好通过将 filterMode 设置为 cudaFilterModePoint 。然后,从像素内的任何坐标读取返回该像素的确切纹理样本值,因此,纹理样本可以通过使用整数直接读取。

However, it is only when filterMode for the texture has been set to cudaFilterModeLinear that the value that is read from a texture varies within a pixel. In that mode, reading from coordinates that are not in the exact center of a pixel returns values that are interpolated between the sample for the given pixel and the samples for neighboring pixels. So, adding 0.5 to whole number coordinates effectively negates the cudaFilterModeLinear mode. But, since adding 0.5 to the texture coordinates takes up cycles in the kernel, it is better to simply turn off the interpolation by setting filterMode to cudaFilterModePoint. Then, reading from any coordinate within a pixel returns the exact texture sample value for that pixel, and so, texture samples can be read directly by using whole numbers.

当使用 cudaFilterModePoint ,如果在计算纹理坐标时涉及到任何浮点数学,则必须注意确保浮点不准确性不会导致纹理坐标落在范围之外

When using cudaFilterModePoint, if any floating point math is involved in calculating the texture coordinates, care must be taken to ensure that floating point inaccuracies don't cause the texture coordinates to fall outside the range for the intended target pixel.

此外,如注释所提到的,代码中可能存在问题。将0.5f添加到纹理坐标意味着正在使用 cudaFilterModeLinear 模式,但是该模式返回一个float而不是一个int。

Also, as the comments mention, there might be a problem in your code. Adding 0.5f to the texture coordinates implies that the cudaFilterModeLinear mode is being used, but that mode returns a float, not an int.

这篇关于纹理内存-tex2D基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆