我在使用OpenGL-CUDA-Interopt时遇到了一些麻烦 我的设置如下:
我正在使用FrameBufferObject渲染纹理(效果非常好),我正是这样创建的:
void createFrameBuffer(){
// Create the texture for the FBO
glGenTextures(1, &tex_data);
glBindTexture(GL_TEXTURE_2D, tex_data);
SDK_CHECK_ERROR_GL();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F , window_w, window_h, 0, GL_RGBA, GL_FLOAT, NULL);
// Create the frameBuffer
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// Register the texture with cuda
checkCudaErrors(cudaGraphicsGLRegisterImage(&res_data, tex_data, GL_TEXTURE_2D, cudaGraphicsMapFlagsReadOnly));
SDK_CHECK_ERROR_GL();
// attach images
glGenRenderbuffersEXT(1, &depth_rb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, window_w, window_h);
//-------------------------
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_data, 0);
//Attach depth buffer to FBO
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_rb);
//glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, color);
//glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth);
// clean up
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
SDK_CHECK_ERROR_GL();
}
目前我没有内核,但我想将纹理从OpenGL转换为可以与CUDA一起使用的东西。以下是我要转换的内容:
void processImage(){
cudaArray *cuda_data = 0;
checkCudaErrors( cudaGraphicsGLRegisterImage(&res_data, tex_data, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsReadOnly));
float *dData; // Array holding the data
std::cout << "cudaMalloc " << std::endl;
checkCudaErrors( cudaMalloc(&dData, window_w * window_h*4*sizeof(float)));
std::cout << "Mapping Resource" << std::endl;
checkCudaErrors( cudaGraphicsMapResources(1, &res_data) );
std::cout << "cudaGraphicsSubResourceGetMappedArray" << std::endl;
checkCudaErrors( cudaGraphicsSubResourceGetMappedArray(&cuda_data, res_data, 0, 0));
std::cout << "cudaMemcpy2DFromArray" << std::endl;
// Copy from the texture to the graphic Card
checkCudaErrors( cudaMemcpy2DFromArray(dData, window_w*4*sizeof(float), cuda_data ,0 ,0, window_w*4*sizeof(float), window_h, cudaMemcpyDeviceToDevice));
std::cout << "cudaGraphicsUnmapResources" << std::endl;
// Unmap the resource
checkCudaErrors( cudaGraphicsUnmapResources(1, &res_data));
}
问题是当执行程序时CUDA抛出错误:
CUDA错误 E:\工作空间\主\ gradientrenderer \ gradientrenderer \ source.cpp:175 code = 11(cudaErrorInvalidValue)&#34; cudaMemcpy2DFromArray(dData, window_w * 4 * sizeof(float),cuda_data,0,0,window_w * 4 * sizeof(float), window_h,cudaMemcpyDeviceToDevice)&#34;
有人能告诉我我做错了什么吗?由于这是使用预定义内核(无法更改此内核)的更大项目的一部分,我需要调用 cudaMemcpy2DFromArray ,以便将纹理正确地传输到内核。