请教cuda图像缓冲区绑定问题

jywzzz 2010-04-16 02:52:06
那个cuda生产一组顶点集,然后用openGL显示的例子我看过了。

现在我想,就用cuda产生一张图片,然后用openGL显示,不知道为什么总是黑色。

代码如下,谢谢。

1、main.cpp



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gl/glew.h>
#include <gl/glut.h>

#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <vector_types.h>

#include "gen.h"

#define BUFFER_OFFSET(bytes) ((GLubyte*)NULL + (bytes))

#define imgWidth 256
#define imgHeight 256

static GLfloat zoomFactor = 1.0;
static GLuint pixelBuffer;
struct cudaGraphicsResource* cuda_vbo_resource;

void init(){
GLenum err = glewInit();
if(GLEW_OK != err)
printf("Glew Error !\n");

glClearColor(0, 0, 0, 0);
glShadeModel(GL_FLAT);

glGenBuffersARB(1, &pixelBuffer);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pixelBuffer);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, imgWidth * imgHeight * sizeof(GLubyte) * 4,
NULL, GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, pixelBuffer, cudaGraphicsMapFlagsWriteDiscard);
}

void display(){
GLuint *dptr;
GLuint num_bytes;

cudaGraphicsMapResources(1, &cuda_vbo_resource, 0);
cudaGraphicsResourceGetMappedPointer((void**)&dptr,
&num_bytes, cuda_vbo_resource);

cudaMemset(dptr, 0, imgWidth*imgHeight*4);

// Lauch kernel
launch_kernel(dptr, 0, imgWidth, imgHeight);

// Unmap buffer object
cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0);

glClear(GL_COLOR_BUFFER_BIT);

glDisable(GL_DEPTH_TEST);
glRasterPos2i(0, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pixelBuffer);
glDrawPixels(imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

glutSwapBuffers();
glutPostRedisplay();
}

void reshape(int w, int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLdouble)w, 0, (GLdouble)h);
}

void keyboard(unsigned char key, int x, int y){
switch(key){
case ' ':
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(440, 440);
glutInitWindowPosition(100, 100);
glutCreateWindow("List");

init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);

glutMainLoop();

cudaGraphicsUnregisterResource(cuda_vbo_resource);
glBindBufferARB(1, pixelBuffer);
glDeleteBuffersARB(1, &pixelBuffer);

return 0;
}






2、gen.h


#ifndef __KERNER__JYWZZZ
#define __KERNER__JYWZZZ

extern "C" void launch_kernel(unsigned int* positions,
float anim,
unsigned int width,
unsigned int height);

#endif




3、gen.cu


#include "gen.h"

__global__ void createVertices(unsigned int* positions, float anim, unsigned int width, unsigned int height)
{
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;

// Write positions
positions[y * width + x] = 0xff0000ff;
}

void launch_kernel(unsigned int* positions, float anim, unsigned int width, unsigned int height){
// Execute kernel
dim3 dimBlock(16, 16, 1);
dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1);
createVertices<<<dimGrid, dimBlock>>>(positions, anim, width, height);
}

...全文
235 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jywzzz 2010-04-20
  • 打赏
  • 举报
回复
我自己解决了,OpenGL红宝书好好看了一遍。
jywzzz 2010-04-17
  • 打赏
  • 举报
回复
2楼,simpleGL就是我说的那个cuda生成顶点集,然后opengl显示的那个,话说您有没有真正看过?
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jywzzz 的回复:]
2楼,simpleGL就是我说的那个cuda生成顶点集,然后opengl显示的那个,话说您有没有真正看过?
[/Quote]

我机器上编译/执行很好啊.没问题的.呵呵.
从sample改过来,是一步步来的.到哪步不对了,再想办法解决.这才是正确的办法.
  • 打赏
  • 举报
回复
参考sdk中simpleGL的代码.

581

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧