2

我在 C 端有一个 1 通道的浮动图像,如下所示:

int width, height;
float* img;

我想将此图像传递给 CUDA 纹理。我正在阅读NVIDIA CUDA C 编程指南(第 42-43 页)并使用教程,编写了如下代码:

主.cpp:

int main()
{
     int width, height;
     float* h_Input;
     ReadImage(&h_Input, &width, &height); // My function which reads the image.
     WriteImage(h_Input, width, height); // works perfectly...

     float* h_Output = (float*) malloc(sizeof(float) * width * height);

     CalculateWithCuda(h_Input, h_Output, width,height);
     WriteImage(h_Output, width, height); // writes an empty-gray colored image.... *WHY???* 
}

内核.cu:

texture<float, cudaTextureType2D, cudaReadModeElementType> texRef; // 2D float texture

__global__ void Kernel(float* output, int width, int height)
{
    int i = blockIdx.y * blockDim.y + threadIdx.y; // row number 
    int j = blockIdx.x * blockDim.x + threadIdx.x; // col number

    if(i < height && j < width)
    {
           float temp = tex2D(texRef, i + 0.5f, j + 0.5f);
           output[i * width + j] = temp ;
    }
} 

void CalculateWithCuda(const float* h_input, float* h_output, int width, int height)
{
    float* d_output;

    // Allocate CUDA array in device memory
    cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0,cudaChannelFormatKindFloat);
    cudaArray* cuArray;
    cudaMallocArray(&cuArray, &channelDesc, width, height);
    // Copy to device memory some data located at address h_data in host memory 
    cudaMemcpyToArray(cuArray, 0, 0, h_input, width * height * sizeof(float) , cudaMemcpyHostToDevice);
    // Set texture parameters
    texRef.addressMode[0] = cudaAddressModeWrap;
    texRef.addressMode[1] = cudaAddressModeWrap;
    texRef.filterMode     = cudaFilterModeLinear;
    texRef.normalized     = true;

    // Bind the array to the texture reference
    cudaBindTextureToArray(texRef, cuArray, channelDesc);

    // Allocate GPU buffers for the output image ..
    cudaMalloc(&d_output, sizeof(float) * width * height);

    dim3 threadsPerBlock(16,16);
    dim3 numBlocks((width/threadsPerBlock.x) + 1, (height/threadsPerBlock.y) + 1);

    Kernel<<<numBlocks, threadsPerBlock>>>(d_output, width,height);

    cudaDeviceSynchronize();

    // Copy output vector from GPU buffer to host memory.
    cudaMemcpy(h_output, d_output, sizeof(float) * width * height, cudaMemcpyDeviceToHost);

    // Free GPU  memory ...
}

正如我在代码中所说;这个内核必须从纹理中读取并给我与输出相同的图像。但是,我正在为输出拍摄一张空的(灰色)图像。我只是在教程中实现了相同的方式,为什么这个纹理不起作用?

如果有人向我展示解决此问题的方法,我将不胜感激...

PS:当然,这不是所有的代码。我只是复制了必要的部分。如果您需要其他详细信息,我也会支持。

提前致谢。

4

1 回答 1

6

当您使用归一化坐标时,通过从 0 到 1(不包括)的坐标访问纹理。您忘记将基于 threadIdx 的整数坐标转换为标准化坐标。

unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y; 
float u = x / (float)width;  
float v = y / (float)height;
于 2012-05-12T18:25:15.970 回答