opengl 使用surf2Dwrite从CUDA写入纹理

tzxcd3kk  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(156)

我在CUDA中计算颜色值,我想在OpenGL的纹理中写入。不管实际的颜色值是多少,纹理只包含符号(Í)。因此,显示的纹理只是黑色。
1.我该如何调试它?(编译、运行或使用compute-sanitizer时无错误)
1.我使用uchar 4类型是错的吗?
1.我是否正确使用了曲面对象?(不熟悉)
1.有没有更好的方法来更新纹理?
1.我应该在每次迭代中使用两个纹理吗?(GL中一个用于写入,一个用于读取)
1.除了编程指南之外,是否有任何关于使用CUDA曲面/资源的资源/教程?(我需要多一点才能完全理解这些)
相关代码:

// initialize texture
  glEnable(GL_TEXTURE_2D);

  glGenTextures(1, &texture_1);
  glBindTexture(GL_TEXTURE_2D, texture_1);

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, window_width, window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

  glBindTexture(GL_TEXTURE_2D, 0);
  checkCudaErrors(cudaGraphicsGLRegisterImage(&cuda_resource_1, texture_1, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard));
  memset(&cuda_resource_desc_1, 0, sizeof(cuda_resource_desc_1));
  cuda_resource_desc_1.resType = cudaResourceTypeArray;
// starting the kernel
  size_t num_bytes_1, num_bytes_2;
  cudaArray *resArray_1, *resArray_2;
  cudaSurfaceObject_t surf_object_1, surf_object_2;
  
  checkCudaErrors(cudaGraphicsMapResources(1, &cuda_resource_1, (cudaStream_t)0));                                  
  checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(&cuda_tex_array_1, cuda_resource_1, 0, 0));
  cuda_resource_desc_1.res.array.array = cuda_tex_array_1;                                                          
  checkCudaErrors(cudaCreateSurfaceObject(&surf_object_1, &cuda_resource_desc_1));                                  
  colour_kernel<<<grid, block, shared_memory_size>>>(surf_object_1, solvent_radius, window_width, window_height); // run kernel and update texture 1
  checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_resource_1, (cudaStream_t)0));
// kernel
  int x = (blockIdx.x * blockDim.x) + threadIdx.x;
  int y = (blockIdx.y * blockDim.y) + threadIdx.y;

  if (x > width || y > height)
    return;

  uchar4 data = make_uchar4(0.0, 0.4, 0.2, 0.5);
  surf2Dwrite(data, surface, x * sizeof(uchar4), y);
// test function to read the texture and write it to a txt file
void writeTextureToFile(GLuint texture, int size)
{
    uchar4 *data = new uchar4[size];
    glGetTextureImage(texture, 0, GL_RGBA, GL_FLOAT, size * sizeof(uchar4), data);

    // access the data
    std::ofstream myfile("test.txt");
    if (myfile.is_open())
    {
        for (int i = 0; i < size; i++)
        {
            myfile << data[i].x << ", " << data[i].y << ", " << data[i].z << ", " << data[i].w << "\n";
        }
        myfile.close();
    }
    else
        std::cout << "Unable to open file" << std::endl;
}
aemubtdh

aemubtdh1#

uchar4的使用似乎是问题所在。经过反复试验和谷歌搜索,我使用GL_RGBA32F_ARB(用于glTexImage2D)找到了一个浮点值的工作解决方案。仍然不确定为什么uchar不起作用(我在网上找到了一些利用uchar的其他例子)。

相关问题