1

我无法让引用线性内存的无绑定纹理工作——结果始终是零/黑色读取。我的初始化代码:

缓冲区:

int const num = 4 * 16;
int const size = num * sizeof(float);
cudaMalloc(buffer, size);

auto b = new float[num];
for (int i = 0; i < num; ++i)
{
    b[i] = i % 4 == 0 ? 1 : 1;
}
cudaMemcpy(*buffer, b, size, cudaMemcpyHostToDevice);

纹理对象:

cudaTextureDesc td;
memset(&td, 0, sizeof(td));

td.normalizedCoords = 0;
td.addressMode[0] = cudaAddressModeClamp;
td.addressMode[1] = cudaAddressModeClamp;
td.addressMode[2] = cudaAddressModeClamp;
td.readMode = cudaReadModeElementType;
td.sRGB = 0;

td.filterMode = cudaFilterModePoint;
td.maxAnisotropy = 16;

td.mipmapFilterMode = cudaFilterModePoint;
td.minMipmapLevelClamp = 0;
td.maxMipmapLevelClamp = 0;
td.mipmapLevelBias = 0;

struct cudaResourceDesc resDesc;
memset(&resDesc, 0, sizeof(resDesc));
resDesc.resType = cudaResourceTypeLinear;
resDesc.res.linear.devPtr = *buffer;
resDesc.res.linear.sizeInBytes = size;
resDesc.res.linear.desc.f = cudaChannelFormatKindFloat;
resDesc.res.linear.desc.x = 32;
resDesc.res.linear.desc.y = 32;
resDesc.res.linear.desc.z = 32;
resDesc.res.linear.desc.w = 32;

checkCudaErrors(cudaCreateTextureObject(texture, &resDesc, &td, nullptr));

内核:

__global__ void
    d_render(uchar4 *d_output, uint imageW, uint imageH, float* buffer, cudaTextureObject_t texture)
{
    uint x = blockIdx.x * blockDim.x + threadIdx.x;
    uint y = blockIdx.y * blockDim.y + threadIdx.y;

    if ((x < imageW) && (y < imageH))
    {
        // write output color
        uint i = y * imageW + x;
        //auto f = make_float4(buffer[0], buffer[1], buffer[2], buffer[3]);
        auto f = tex1D<float4>(texture, 0);
        d_output[i] = to_uchar4(f * 255);
    }
}

当将纹理对象提供给内核时,它会用一些有意义的东西 (4099) 进行初始化。Buffer 版本完美运行。

为什么纹理对象返回零/黑色?

4

1 回答 1

7

根据CUDA 编程参考指南,您需要使用tex1Dfetch()从绑定到线性纹理内存tex1D的一维纹理中读取,以及从绑定到 CUDA 数组的一维纹理中读取。这适用于 CUDA 纹理引用和对象传递的 CUDA 纹理。

两个 API 之间的区别在于坐标参数。绑定到线性内存的纹理只能在纹理坐标中寻址(因此在 中的整数坐标参数text1Dfetch()),而数组支持纹理和归一化坐标(因此在 中的浮点坐标参数tex1D)。

于 2013-07-15T15:51:50.660 回答