只能看到单个白点CUDA / OpenGL互操作

时间:2016-07-04 14:24:56

标签: c++ opengl cuda interop

我正在尝试将C ++代码转换为CUDA代码,用于从外部文件读取数据而不是绘制3D矩阵的程序。我只是一次转换一些代码,但我没有尝试绘制仅由红点组成的小型3D矩阵。我只能看到一个白点,那就是代码:

#define BUFFER_OFFSET(i) ((char *)NULL + (i)) 

GLuint  bufferObj;
cudaGraphicsResource *resource;

int size=5*5*7;
int window_dim=600;

__global__ void kernel(float4 *ptr, int sizei) {

  const unsigned long int blockId = blockIdx.x //1D
    + blockIdx.y * gridDim.x //2D
    + gridDim.x * gridDim.y * blockIdx.z; //3D

  const unsigned long int threadId = (blockId * blockDim.x + threadIdx.x);
  if(threadId<sizei ) {
    ptr[threadId].x=threadIdx.x+0.5f;
    ptr[threadId+sizei].x=1.0f;
    ptr[threadId].y=blockIdx.y+0.5f;
    ptr[threadId+sizei].y=0.0f;
    ptr[threadId].z=blockIdx.z+0.5f;
    ptr[threadId+sizei].z=0.0f;
    ptr[threadId].w=1.0f;
    ptr[threadId+sizei].w=1.0f;
  }
} 

static void key_func( unsigned char key, int x, int y ) {
  switch (key) {
  case 27:
    HANDLE_ERROR( cudaGraphicsUnregisterResource( resource ) );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );
    glDeleteBuffers( 1, &bufferObj );
    exit(0);
  }
}

static void draw_func( void ) {

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  gluLookAt (2.5f, 2.5f, 10.0f, 2.5f, 2.5f, 3.5f, 0.0f, 1.0f, 0.0f);
  glPushMatrix();
  glMatrixMode(GL_MODELVIEW);


  glBindBuffer( GL_ARRAY_BUFFER, bufferObj );
  glVertexPointer( 4, GL_FLOAT, sizeof(float4), 0 ); 
  glColorPointer( 4, GL_FLOAT, sizeof(float4),   BUFFER_OFFSET(sizeof(float4)*size) );
  glPointSize( 3.0 );

  glDrawArrays( GL_POINTS, 0, size); 
  glFlush(); 
  glPopMatrix();
  glutSwapBuffers();
}

static void reshape_func( int w, int h) {
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glFrustum (-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 100.0f); 
  glMatrixMode (GL_MODELVIEW);
  glutPostRedisplay();

}

int main( int argc, char **argv ) {
  cudaDeviceProp  prop;
  int dev;
  float4 *g_vertex_buffer_data = new float4[size*2];

  memset( &prop, 0, sizeof( cudaDeviceProp ) ); 
  prop.major = 3;
  prop.minor = 0;
  HANDLE_ERROR( cudaChooseDevice( &dev, &prop ) );  

  HANDLE_ERROR( cudaGLSetGLDevice( dev ) );   

  glutInit( &argc, argv );  
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
  glutInitWindowSize( window_dim, window_dim );
  glutCreateWindow( "Interop_test" );

  glGenBuffers( 1, &bufferObj );  
  glBindBuffer( GL_ARRAY_BUFFER, bufferObj );
  glBufferData( GL_ARRAY_BUFFER, sizeof(float4)*2*size, NULL, GL_DYNAMIC_DRAW );  

  HANDLE_ERROR( cudaGraphicsGLRegisterBuffer( &resource, bufferObj, cudaGraphicsMapFlagsNone ) );
  HANDLE_ERROR( cudaGraphicsMapResources( 1, &resource, NULL ) );

  float4  *devPtr;
  size_t  sizePtr;
  HANDLE_ERROR( cudaGraphicsResourceGetMappedPointer( (void**)&devPtr, &sizePtr, resource) );

  dim3 dimBlock(5,1,1);
  dim3 dimGrid(1,5,7);

  kernel<<<dimGrid,dimBlock>>>( devPtr, size );
  ////////////Don't know if this is necessary/////////////////
  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float4)*size, &devPtr[0].x);
  glBufferSubData(GL_ARRAY_BUFFER, sizeof(float4)*size, 
            sizeof(float4)*size, &devPtr[size].x);
  /////////////////////////////////////////////////////////////////
  HANDLE_ERROR( cudaGraphicsUnmapResources( 1, &resource, NULL ) );

 glEnable(GL_DEPTH_TEST);
 glutKeyboardFunc( key_func );
 glutDisplayFunc( draw_func );
 glutReshapeFunc( reshape_func);
 glutMainLoop();
}

我也尝试打印出devPtr并获得正确的数据。

1 个答案:

答案 0 :(得分:1)

我发现了哪个问题。我会回答我的问题,以便每个人都可以使用它。 只需像这样编辑draw_func:

static void draw_func( void ) {

  [...]

  glBindBuffer( GL_ARRAY_BUFFER, bufferObj );
  glEnableClientState( GL_VERTEX_ARRAY ); 
  glEnableClientState( GL_COLOR_ARRAY );
  glVertexPointer( 4, GL_FLOAT, sizeof(float4), 0 ); 
  glColorPointer( 4, GL_FLOAT, sizeof(float4), BUFFER_OFFSET(sizeof(float4)*size) ); 
  glPointSize( 3.0 );

  glDrawArrays( GL_POINTS, 0, size); 
  glDisableClientState( GL_VERTEX_ARRAY );
  glDisableClientState( GL_COLOR_ARRAY );

  [...]
}