int main(int argc, char** argv) { // Launch CUDA/GL init(argc, argv); cudaGLSetGLDevice( compat_getMaxGflopsDeviceId() ); cudaGLRegisterBufferObject( planetVBO ); cudaGLRegisterBufferObject( velocityVBO ); #if VISUALIZE == 1 initCuda(N_FOR_VIS); #else initCuda(2*128); #endif projection = glm::perspective(fovy, float(width)/float(height), zNear, zFar); view = glm::lookAt(cameraPosition, glm::vec3(0.0, 0.0, 0), glm::vec3(0,1,0)); projection = projection * view; initShaders(program); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMotionFunc(mouseMotion); glutMainLoop(); return 0; }
void OpenGLRenderer::renderPoints(const glm::vec3* positions, const Color256* colors, const int num, const Camera &camera) { //always use the point shaders to render points glUseProgram(points_program_); GLuint mvp_location = glGetUniformLocation(points_program_, "u_mvpMatrix"); //Declare CUDA device pointers for it to use float3* dptr_pos; float3* dptr_col; //Setup position buffer glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); glBufferData(GL_ARRAY_BUFFER, 3 * num*sizeof(float), NULL, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); //Setup color buffer glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); glBufferData(GL_ARRAY_BUFFER, 3 * num*sizeof(float), NULL, GL_DYNAMIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(1); //Register position and normal buffers with CUDA cudaGLRegisterBufferObject(buffers_[0]); cudaGLRegisterBufferObject(buffers_[1]); //Map buffers to CUDA cudaGLMapBufferObject((void**)&dptr_pos, buffers_[0]); cudaGLMapBufferObject((void**)&dptr_col, buffers_[1]); //Copy data to buffer with CUDA copyPointsToGL(positions, colors, dptr_pos, dptr_col, num); //Unmap buffers from CUDA cudaGLUnmapBufferObject(buffers_[0]); cudaGLUnmapBufferObject(buffers_[1]); //Unregister position and normal buffers with CUDA cudaGLUnregisterBufferObject(buffers_[0]); cudaGLUnregisterBufferObject(buffers_[1]); //Send the MVP Matrix glUniformMatrix4fv(mvp_location, 1, GL_FALSE, glm::value_ptr(camera.mvp)); //Draw glPointSize(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_POINTS, 0, 3 * num); }
void initCUDAMemory() { uint resolution = r_width * r_height; void* data = malloc(sizeof(GLubyte) * resolution * 4); glGenBuffers(1, &pbo); glBindBuffer(GL_ARRAY_BUFFER, pbo); glBufferData(GL_ARRAY_BUFFER, sizeof(GLubyte) * resolution * 4, data, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); free(data); cutilSafeCall(cudaGLRegisterBufferObject(pbo)); // initialize the PBO for transferring data from CUDA to openGL CUT_CHECK_ERROR_GL(); glGenTextures(1, &framebuffer); glBindTexture(GL_TEXTURE_2D, framebuffer); 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_RGBA, r_width, r_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); CUT_CHECK_ERROR_GL(); }
PBO::PBO(int p_width, int p_height) : width(p_width), height(p_height), pbo(0) { glGenBuffersARB(1, &pbo); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*4, 0, GL_STREAM_DRAW_ARB); cutilSafeCall(cudaGLRegisterBufferObject(pbo)); }
void Mandelbrot::createBuffer( GLuint* b, int size ) { glGenBuffers( 1, b ); glBindBuffer( GL_PIXEL_UNPACK_BUFFER, *b ); glBufferData( GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_COPY ); checkCudaErrors( cudaGLRegisterBufferObject( *b ), __LINE__, true ); }
void LiGL2D::setVbo(int spaceVect) { GLuint oldVbo = 0; GLuint newVbo = 0; if(vbo != 0){ oldVbo = vbo; vbo = 0; } if(iw != 0 && ih !=0){ GLint bsize; // create buffer object unsigned int size = ((int)iw/(spaceVect+1))*((int)ih/(spaceVect+1)) * 6 * sizeof(float2); glGenBuffers( 1, &newVbo); glBindBuffer( GL_ARRAY_BUFFER, newVbo); // initialize buffer object glBufferData( GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_SIZE_ARB, &bsize); glBindBuffer( GL_ARRAY_BUFFER, 0); // register buffer object with CUDA CUDA_SAFE_CALL(cudaGLRegisterBufferObject(newVbo)); sVbo = ((int)iw/(spaceVect+1))*((int)ih/(spaceVect+1))*6; vbo = newVbo; emit sendVbo(vbo); } if(oldVbo != 0){ CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(oldVbo)); glDeleteBuffers(1, &oldVbo); } }
//////////////////////////////////////////////////////////////////////////////// //! Check if the result is correct or write data to file for external //! regression testing //////////////////////////////////////////////////////////////////////////////// void checkResultCuda(int argc, char** argv, const GLuint& vbo) { cutilSafeCall(cudaGLUnregisterBufferObject(vbo)); // map buffer object glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo ); float* data = (float*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); // check result if(cutCheckCmdLineFlag(argc, (const char**) argv, "regression")) { // write file for regression test cutilCheckError(cutWriteFilef("./data/regression.dat", data, mesh_width * mesh_height * 3, 0.0)); } // unmap GL buffer object if(! glUnmapBuffer(GL_ARRAY_BUFFER)) { fprintf(stderr, "Unmap buffer failed.\n"); fflush(stderr); } cutilSafeCall(cudaGLRegisterBufferObject(vbo)); CUT_CHECK_ERROR_GL(); }
void LiGL2D::setPbo(int image_width, int image_height) { makeCurrent(); iw = image_width; ih = image_height; GLuint oldPbo = 0; GLuint newPbo = 0; GLuint oldTex = 0; if(pbo != 0){ oldPbo = pbo; pbo = 0; oldTex = tex; } if(iw != 0 && ih !=0){ glGenBuffers(1, &newPbo); glBindBuffer(GL_ARRAY_BUFFER, newPbo); glBufferData(GL_ARRAY_BUFFER, image_height*image_width* 4*sizeof(GLubyte),NULL, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); CUDA_SAFE_CALL(cudaGLRegisterBufferObject(newPbo)); createTexture(&tex, iw, ih); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); pbo = newPbo; emit sendPbo(pbo); } if(oldPbo != 0){ CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(oldPbo)); glDeleteBuffers(1, &oldPbo); } if(oldTex != 0){ glDeleteTextures(1, &oldTex); } }
uint CreateCUDABufferObject(unsigned int size, bool colorBuffer) { GLuint buffId = CreateBufferObject(size, colorBuffer); cudaGLRegisterBufferObject(buffId); return buffId; }
void initializeData(char *file) { GLint bsize; unsigned int w, h; size_t file_length= strlen(file); if (!strcmp(&file[file_length-3], "pgm")) { if (cutLoadPGMub(file, &pixels, &w, &h) != CUTTrue) { printf("Failed to load image file: %s\n", file); exit(-1); } g_Bpp = 1; } else if (!strcmp(&file[file_length-3], "ppm")) { if (cutLoadPPM4ub(file, &pixels, &w, &h) != CUTTrue) { printf("Failed to load image file: %s\n", file); exit(-1); } g_Bpp = 4; } else { cudaThreadExit(); exit(-1); } imWidth = (int)w; imHeight = (int)h; setupTexture(imWidth, imHeight, pixels, g_Bpp); memset(pixels, 0x0, g_Bpp * sizeof(Pixel) * imWidth * imHeight); if (!g_bQAReadback) { // use OpenGL Path glGenBuffers(1, &pbo_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer); glBufferData(GL_PIXEL_UNPACK_BUFFER, g_Bpp * sizeof(Pixel) * imWidth * imHeight, pixels, GL_STREAM_DRAW); glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize); if ((GLuint)bsize != (g_Bpp * sizeof(Pixel) * imWidth * imHeight)) { printf("Buffer object (%d) has incorrect size (%d).\n", (unsigned)pbo_buffer, (unsigned)bsize); cudaThreadExit(); exit(-1); } glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); cutilSafeCall(cudaGLRegisterBufferObject(pbo_buffer)); glGenTextures(1, &texid); glBindTexture(GL_TEXTURE_2D, texid); glTexImage2D(GL_TEXTURE_2D, 0, ((g_Bpp==1) ? GL_LUMINANCE : GL_BGRA), imWidth, imHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); } }
int cu2_register_buf(QSP_ARG_DECL Data_Obj *dp) { cudaError_t e; /* how do we check for an error? */ e = cudaGLRegisterBufferObject( OBJ_BUF_ID(dp) ); if( e != cudaSuccess ){ describe_cuda_driver_error2("cu2_register_buf", "cudaGLRegisterBufferObject",e); return -1; } return 0; }
/* * Allocates a GL buffer and texture to be used on the GPU. * */ static void allocateGLTexture(GLuint *bufferID, GLuint *textureID) { glGenBuffers(1, bufferID); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *bufferID); glBufferData(GL_PIXEL_UNPACK_BUFFER, 640 * 480 * 4 * sizeof(GLubyte), NULL, GL_DYNAMIC_COPY); cudaGLRegisterBufferObject(*bufferID); glEnable(GL_TEXTURE_2D); glGenTextures(1, textureID); glBindTexture(GL_TEXTURE_2D, *textureID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 640, 480, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); }
void initPBO(GLuint* pbo){ if (pbo) { // set up vertex data parameter int num_texels = width*height; int num_values = num_texels * 4; int size_tex_data = sizeof(GLubyte) * num_values; // Generate a buffer ID called a PBO (Pixel Buffer Object) glGenBuffers(1,pbo); // Make this the current UNPACK buffer (OpenGL is state-based) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *pbo); // Allocate data for the buffer. 4-channel 8-bit image glBufferData(GL_PIXEL_UNPACK_BUFFER, size_tex_data, NULL, GL_DYNAMIC_COPY); cudaGLRegisterBufferObject( *pbo ); } }
void Vbo:: registerWithCuda() { if (!_registered) { TIME_VBO TaskTimer tt("Vbo::registerWithCuda(), %u, size %s", _vbo, DataStorageVoid::getMemorySizeText(_sz).c_str()); CudaException_SAFE_CALL( cudaGLRegisterBufferObject(_vbo) ); } else { // cudaGLRegisterBufferObject(_vbo); // cudaGetLastError(); } _registered = true; }
//////////////////////////////////////////////////////////////////////////////// //! Create VBO //////////////////////////////////////////////////////////////////////////////// void createVBO(GLuint* vbo) { // create buffer object glGenBuffers(1, vbo); glBindBuffer(GL_ARRAY_BUFFER, *vbo); // initialize buffer object unsigned int size = mesh_width * mesh_height * 4 * sizeof(float); glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // register buffer object with CUDA cutilSafeCall(cudaGLRegisterBufferObject(*vbo)); CUT_CHECK_ERROR_GL(); }
void Renderer::initPBO() { // initialize the PBO for transferring data from CUDA to openGL uint num_texels = image_width * image_height; uint size_tex_data = sizeof(GLubyte) * num_texels * 4; void *data = malloc(size_tex_data); // test init buffer for (int i=0; i<size_tex_data; i+=4) { uchar *datam = (uchar*)data; datam[i+0] = 0; datam[i+1] = 0; datam[i+2] = 255.0 * i / (float)size_tex_data; datam[i+3] = 255; } // create buffer object glGenBuffers(1, &pbo); glBindBuffer(GL_ARRAY_BUFFER, pbo); glBufferData(GL_ARRAY_BUFFER, size_tex_data, data, GL_DYNAMIC_DRAW); free(data); glBindBuffer(GL_ARRAY_BUFFER, 0); // register this buffer object with CUDA checkCudaErrors(cudaGLRegisterBufferObject(pbo)); SDK_CHECK_ERROR_GL(); // create the texture that we use to visualize the ray-tracing result glActiveTexture(GL_TEXTURE0 + RENDER_TEXTURE); glGenTextures(1, &result_texture); glBindTexture(GL_TEXTURE_2D, result_texture); // set basic parameters 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); // buffer data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); SDK_CHECK_ERROR_GL(); // unbind glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0 + UNUSED_TEXTURE); }
void initGLBuffers() { // create pixel buffer object to store final image glGenBuffersARB(1, &pbo); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*4, h_img, GL_STREAM_DRAW_ARB); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); checkCudaErrors(cudaGLRegisterBufferObject(pbo)); // create texture for display glGenTextures(1, &texid); glBindTexture(GL_TEXTURE_2D, texid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); }
static int cu2_register_buf(QSP_ARG_DECL Data_Obj *dp) { #ifdef HAVE_OPENGL cudaError_t e; /* how do we check for an error? */ e = cudaGLRegisterBufferObject( OBJ_BUF_ID(dp) ); if( e != cudaSuccess ){ describe_cuda_driver_error2("cu2_register_buf", "cudaGLRegisterBufferObject",e); return -1; } return 0; #else // ! HAVE_OPENGL WARN("cu2_register_buf: Sorry, no OpenGL support in this build!?"); return -1; #endif // ! HAVE_OPENGL }
int main(int argc, char** argv) { // Launch CUDA/GL init(argc, argv); cudaGLSetGLDevice( compat_getMaxGflopsDeviceId() ); initPBO(&pbo); cudaGLRegisterBufferObject( planetVBO ); #if VISUALIZE == 1 initCuda(N_FOR_VIS, glm::vec4 (cameraPosition, 1)); #else initCuda(20*120); #endif // setDevicePrefetch (prefetchEnabled); perspMat = glm::perspective(fovy, float(width)/float(height), zNear, zFar); view = glm::lookAt(cameraPosition, glm::vec3(0), glm::vec3(0,0,1)); projection = perspMat * view; GLuint passthroughProgram; initShaders(program); glUseProgram(program[HEIGHT_FIELD]); glActiveTexture(GL_TEXTURE0); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
void LiGL2D::init(int image_width, int image_height) { GLint bsize; iw = image_width; ih = image_height; imW = iw; imH = ih; makeCurrent(); // allocation du pbo glGenBuffers(1, &pbo); glBindBuffer(GL_ARRAY_BUFFER, pbo); glBufferData(GL_ARRAY_BUFFER, image_height*image_width* 4*sizeof(GLubyte),NULL, GL_DYNAMIC_DRAW); glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_SIZE_ARB, &bsize); glBindBuffer(GL_ARRAY_BUFFER, 0); CUDA_SAFE_CALL(cudaGLRegisterBufferObject(pbo)); setVbo(10); // allocation de la texture d'affichage createTexture(&tex, image_width, image_height); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); initialise = true; std::cout << "buffer pixel num " << pbo << " taille : " << bsize << "\n"; emit sendPbo(pbo); }
void CMarchingCubes::ComputeIsosurface(ElemType* _pFval, ElemType _isoValue, RenderData* _pRender) { int threads = 128; dim3 grid(m_NumVoxels / threads, 1, 1); // get around maximum grid size of 65535 in each dimension if (grid.x > 65535) { grid.y = grid.x / 32768; grid.x = 32768; } uint totalVerts = 0; int size = m_GridSize.x * m_GridSize.y * m_GridSize.z * sizeof(float); ////////////////////////////////////////////////////////////////////////// int len = m_GridSize.x * m_GridSize.y * m_GridSize.z; float *pFvalTemp = new float[len]; for (int i = 0; i < len; i++) { pFvalTemp[i] = _pFval[i]; } ////////////////////////////////////////////////////////////////////////// float* pdVolumeFval; // ¶¥µãº¯ÊýÖµÎÆÀí(n¡¡Surface) cutilSafeCall(cudaMalloc((void**) &pdVolumeFval, size)); cutilSafeCall(cudaMemcpy(pdVolumeFval, pFvalTemp, size, cudaMemcpyHostToDevice) ); bindVolumeValTexture(pdVolumeFval); delete []pFvalTemp; // calculate number of vertices need per voxel launch_classifyVoxel(grid, threads, m_pdVoxelVerts, m_pdVoxelOccupied, pdVolumeFval, m_GridSize, m_NumVoxels, _isoValue); #if DEBUG_BUFFERS printf("voxelVerts:\n"); dumpBuffer(m_pdVoxelVerts, m_NumVoxels); #endif #if SKIP_EMPTY_VOXELS // scan voxel occupied array cudppScan(m_Scanplan, m_pdVoxelOccupiedScan, m_pdVoxelOccupied, m_NumVoxels); #if DEBUG_BUFFERS printf("voxelOccupiedScan:\n"); dumpBuffer(m_pdVoxelOccupiedScan, m_NumVoxels); #endif // read back values to calculate total number of non-empty voxels // since we are using an exclusive scan, the total is the last value of // the scan result plus the last value in the input array { uint lastElement, lastScanElement; cutilSafeCall(cudaMemcpy((void *) &lastElement, (void *) (m_pdVoxelOccupied + m_NumVoxels - 1), sizeof(uint), cudaMemcpyDeviceToHost)); cutilSafeCall(cudaMemcpy((void *) &lastScanElement, (void *) (m_pdVoxelOccupiedScan + m_NumVoxels - 1), sizeof(uint), cudaMemcpyDeviceToHost)); m_ActiveVoxels = lastElement + lastScanElement; } if (0 == m_ActiveVoxels) { // return if there are no full voxels totalVerts = 0; return; } // compact voxel index array launch_compactVoxels(grid, threads, m_pdCompactedVoxelArray, m_pdVoxelOccupied, m_pdVoxelOccupiedScan, m_NumVoxels); cutilCheckMsg("compactVoxels failed"); #endif // SKIP_EMPTY_VOXELS // scan voxel vertex count array cudppScan(m_Scanplan, m_pdVoxelVertsScan, m_pdVoxelVerts, m_NumVoxels); #if DEBUG_BUFFERS printf("voxelVertsScan:\n"); dumpBuffer(m_pdVoxelVertsScan, m_NumVoxels); #endif // readback total number of vertices { uint lastElement, lastScanElement; cutilSafeCall(cudaMemcpy((void *) &lastElement, (void *) (m_pdVoxelVerts + m_NumVoxels - 1), sizeof(uint), cudaMemcpyDeviceToHost)); cutilSafeCall(cudaMemcpy((void *) &lastScanElement, (void *) (m_pdVoxelVertsScan + m_NumVoxels - 1), sizeof(uint), cudaMemcpyDeviceToHost)); totalVerts = lastElement + lastScanElement; } // create VBOs GLuint posVbo, normalVbo; createVBO(&posVbo, totalVerts * sizeof(float) * 4); cutilSafeCall(cudaGLRegisterBufferObject(posVbo)); createVBO(&normalVbo, totalVerts * sizeof(float) * 4); cutilSafeCall(cudaGLRegisterBufferObject(normalVbo)); // generate triangles, writing to vertex buffers float4 *d_pos = 0, *d_normal = 0; cutilSafeCall(cudaGLMapBufferObject((void**)&d_pos, posVbo)); cutilSafeCall(cudaGLMapBufferObject((void**)&d_normal, normalVbo)); #if SKIP_EMPTY_VOXELS dim3 grid2((int) ceil(m_ActiveVoxels / (float) NTHREADS), 1, 1); #else dim3 grid2((int) ceil(m_NumVoxels / (float) NTHREADS), 1, 1); #endif while(grid2.x > 65535) { grid2.x/=2; grid2.y*=2; } launch_generateTriangles(grid2, NTHREADS, d_pos, d_normal, m_pdCompactedVoxelArray, m_pdVoxelVertsScan, m_pdVolume, pdVolumeFval, m_GridSize, _isoValue, m_ActiveVoxels, m_MaxVerts); cutilSafeCall(cudaGLUnmapBufferObject(normalVbo)); cutilSafeCall(cudaGLUnmapBufferObject(posVbo)); _pRender->posVbo = posVbo; _pRender->normalVbo = normalVbo; _pRender->totalVerts = totalVerts; cutilSafeCall(cudaFree(pdVolumeFval)); }
cudaError_t WINAPI wine_cudaGLRegisterBufferObject( GLuint bufObj ) { WINE_TRACE("\n"); return cudaGLRegisterBufferObject( bufObj ); }
cudaError_t SimCudaHelper::RegisterGLBuffer(GLuint vbo) { return cudaGLRegisterBufferObject(vbo); }
Glue::Glue ( int argc, char ** argv, int windowWidth, int windowHeight, Renderer * renderer, Object3D * model, Light light, Camera camera, bool & outSuccess ) : m_windowWidth( windowWidth ), m_windowHeight( windowHeight ), m_renderer( renderer ), m_model( model ), m_light( light ), m_camera( camera ), m_lastFrameTimeInMilliseconds( 0 ) { // Initialize freeglut and OpenGL glutInit( & argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); glutInitWindowSize( windowWidth, windowHeight ); glutInitWindowPosition( 50, 50 ); glutCreateWindow( "asvo@cuda" ); glutDisplayFunc( displayFunc ); glutMouseFunc( mouseFunc ); glutMotionFunc( motionFunc ); glewInit(); if( ! glewIsSupported( "GL_VERSION_2_0" ) ) { fprintf( stderr, "ERROR: Support for necessary OpenGL extensions missing." ); outSuccess = false; return; } glViewport( 0, 0, windowWidth, windowHeight ); glClearColor( 1, 1, 1, 0 ); glDisable( GL_DEPTH_TEST ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0, 1, 0, 1, 0, 1 ); // Initialize CUDA cudaGLSetGLDevice( 0 ); // Create PBO glGenBuffers( 1, & m_pbo ); glBindBuffer( GL_PIXEL_UNPACK_BUFFER, m_pbo ); glBufferData ( GL_PIXEL_UNPACK_BUFFER, windowResolution() * 4 * sizeof( GLubyte ), nullptr, GL_DYNAMIC_COPY ); cudaGLRegisterBufferObject( m_pbo ); // Create texture to render into and display on the screen glEnable( GL_TEXTURE_2D ); glGenTextures( 1, & m_texture ); glBindTexture( GL_TEXTURE_2D, m_texture ); glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA8, windowWidth, windowHeight, 0, GL_BGRA,GL_UNSIGNED_BYTE, nullptr ); // !!! glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); outSuccess = true; }
/** * Initialization of CUDA and GLFW. */ bool init(int argc, char **argv) { // Set window title to "Student Name: [SM 2.0] GPU Name" cudaDeviceProp deviceProp; int gpuDevice = 0; int device_count = 0; cudaGetDeviceCount(&device_count); if (gpuDevice > device_count) { std::cout << "Error: GPU device number is greater than the number of devices!" << " Perhaps a CUDA-capable GPU is not installed?" << std::endl; return false; } cudaGetDeviceProperties(&deviceProp, gpuDevice); int major = deviceProp.major; int minor = deviceProp.minor; std::ostringstream ss; ss << projectName << " [SM " << major << "." << minor << " " << deviceProp.name << "]"; deviceName = ss.str(); // Window setup stuff glfwSetErrorCallback(errorCallback); if (!glfwInit()) { std::cout << "Error: Could not initialize GLFW!" << " Perhaps OpenGL 3.3 isn't available?" << std::endl; return false; } int width = 1280; int height = 720; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(width, height, deviceName.c_str(), NULL, NULL); if (!window) { glfwTerminate(); return false; } glfwMakeContextCurrent(window); glfwSetKeyCallback(window, keyCallback); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { return false; } // Initialize drawing state initVAO(); // Default to device ID 0. If you have more than one GPU and want to test a non-default one, // change the device ID. cudaGLSetGLDevice(0); cudaGLRegisterBufferObject(planetVBO); // Initialize N-body simulation Nbody::initSimulation(N_FOR_VIS); projection = glm::perspective(fovy, float(width) / float(height), zNear, zFar); glm::mat4 view = glm::lookAt(cameraPosition, glm::vec3(0), glm::vec3(0, 0, 1)); projection = projection * view; initShaders(program); glEnable(GL_DEPTH_TEST); return true; }
void OpenGLRenderer::rasterizeVoxels(const VoxelGrid& geometry, const Camera& camera, const glm::vec3& light) { //startTiming(); //always use the voxel shaders to rasterize voxels with instancing glUseProgram(voxel_program_); GLuint mvp_location = glGetUniformLocation(voxel_program_, "u_mvpMatrix"); GLuint norm_location = glGetUniformLocation(voxel_program_, "u_normMatrix"); GLuint light_location = glGetUniformLocation(voxel_program_, "u_light"); GLuint scale_location = glGetUniformLocation(voxel_program_, "u_scale"); //Declare CUDA device pointers for it to use glm::vec4* dptr_centers; glm::vec4* dptr_colors; //Setup position buffer glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); glBufferData(GL_ARRAY_BUFFER, 4*geometry.size*sizeof(float), NULL, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); //Setup color buffer glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); glBufferData(GL_ARRAY_BUFFER, 4 * geometry.size*sizeof(float), NULL, GL_DYNAMIC_DRAW); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(1); //Register position and normal buffers with CUDA cudaGLRegisterBufferObject(buffers_[0]); cudaGLRegisterBufferObject(buffers_[1]); //Map buffers to CUDA cudaGLMapBufferObject((void**)&dptr_centers, buffers_[0]); cudaGLMapBufferObject((void**)&dptr_colors, buffers_[1]); //Copy data to buffer cudaMemcpy(dptr_centers, geometry.centers, 4*geometry.size*sizeof(float), cudaMemcpyDeviceToDevice); cudaMemcpy(dptr_colors, geometry.colors, 4*geometry.size*sizeof(float), cudaMemcpyDeviceToDevice); //Unmap buffers from CUDA cudaGLUnmapBufferObject(buffers_[0]); cudaGLUnmapBufferObject(buffers_[1]); //Unregister position and normal buffers with CUDA cudaGLUnregisterBufferObject(buffers_[0]); cudaGLUnregisterBufferObject(buffers_[1]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_BUFFER, textures_[0]); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, buffers_[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_BUFFER, textures_[1]); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, buffers_[1]); //Send the MVP Matrix glUniformMatrix4fv(mvp_location, 1, GL_FALSE, glm::value_ptr(camera.mvp)); glm::mat3 norm_mat = glm::mat3(glm::transpose(glm::inverse(camera.model))); glUniformMatrix3fv(norm_location, 1, GL_FALSE, glm::value_ptr(norm_mat)); //Send the light position glUniform3fv(light_location, 1, glm::value_ptr(light)); //Send the scale glUniform1f(scale_location, geometry.scale); //Draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArraysInstanced(GL_TRIANGLES, 0, 36, geometry.size); //float t = stopTiming(); //std::cout << "Draw took: " << t << std::endl; }
int main(int argc, char** argv) { // Launch CUDA/GL init(argc, argv); cudaGLSetGLDevice( compat_getMaxGflopsDeviceId() ); cudaGLRegisterBufferObject( planetVBO ); #if VISUALIZE == 1 initCuda(N_FOR_VIS); #else initCuda(2*128); #endif projection = glm::perspective(fovy, float(width)/float(height), zNear, zFar); view = camera.getViewMatrix(); projection = projection * view; GLuint passthroughProgram; initShaders(program); glUseProgram(program[HEIGHT_FIELD]); glActiveTexture(GL_TEXTURE0); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
int main(int argc, char** argv) { // Launch CUDA/GL init(argc, argv); cudaGLSetGLDevice(0); initPBO(&pbo); cudaGLRegisterBufferObject( planetVBO ); initCuda(N_FOR_VIS); projection = glm::perspective(fovy, float(width)/float(height), zNear, zFar); view = glm::lookAt(cameraPosition, glm::vec3(0), glm::vec3(0,0,1)); projection = projection * view; GLuint passthroughProgram; initShaders(program); glUseProgram(program[HEIGHT_FIELD]); glActiveTexture(GL_TEXTURE0); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }