纹理记忆——tex2D基础 [英] Texture memory-tex2D basics

查看:21
本文介绍了纹理记忆——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);

我的问题是为什么我们要在 cf 中都添加 0.5f?这让我很困惑..谢谢你

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 模式,但该模式返回一个浮点数,而不是整数.

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天全站免登陆