581
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#ifndef __KERNER__JYWZZZ
#define __KERNER__JYWZZZ
extern "C" void launch_kernel(unsigned int* positions,
float anim,
unsigned int width,
unsigned int height);
#endif
#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);
}