bool OffscreenRenderer::makeCurrent(int width, int height, bool sampleBuffers) { ASSERT(mainGlWidget); // Considering weak graphics cards glClear is faster when the color and depth buffers are not greater then they have to be. // So we create an individual buffer for each size in demand. std::unordered_map<unsigned int, Buffer>::iterator it = renderBuffers.find(width << 16 | height << 1 | (sampleBuffers ? 1 : 0)); if(it == renderBuffers.end()) { Buffer& buffer = renderBuffers[width << 16 | height << 1 | (sampleBuffers ? 1 : 0)]; #ifdef FIX_MACOS_BROKEN_TEXTURES_ON_NVIDIA_BUG if(!initFrameBuffer(width, height, buffer)) if(!initPixelBuffer(width, height, sampleBuffers, buffer)) #else if(!initPixelBuffer(width, height, sampleBuffers, buffer)) if(!initFrameBuffer(width, height, buffer)) #endif initHiddenWindow(width, height, buffer); return true; } else { Buffer& buffer = it->second; if(buffer.pbuffer) return buffer.pbuffer->makeCurrent(); if(buffer.frameBufferId) { mainGlWidget->makeCurrent(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, buffer.frameBufferId); return true; } if(buffer.glWidget) buffer.glWidget->makeCurrent(); else mainGlWidget->makeCurrent(); return true; } }
int main(int argc, char** argv) { printInstructions(); initGLUT(&argc, argv); gluOrtho2D(0, W, H, 0); glutKeyboardFunc(keyboard); glutSpecialFunc(handleSpecialKeypress); glutPassiveMotionFunc(mouseMove); glutMotionFunc(mouseDrag); glutDisplayFunc(display); initPixelBuffer(); glutMainLoop(); atexit(exitfunc); return 0; }
// Window resize handler callback //***************************************************************************** void Reshape(int x, int y) { width = x; height = y; initPixelBuffer(); glViewport(0, 0, x, y); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); //std::cout<<viewToWorld<<std::endl; // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { Colour col; if (antiAliasing) { col = anti_aliasing(viewToWorld, width, height, factor, i, j); } else { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; Vector3D _dir (imagePlane[0],imagePlane[1],imagePlane[2]); Vector3D dir = viewToWorld * _dir; origin = viewToWorld * origin;; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Ray3D ray; ray.origin = origin; ray.dir = dir; Colour col = shadeRay(ray); //std:: cout << "i:" <<i << "j:" << j <<"Hit: "<<ray.intersection.none<<std::endl; } _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
int main(int argc, char** argv) { cudaMalloc(&d_vol, NX*NY*NZ*sizeof(float)); // 3D volume data volumeKernelLauncher(d_vol, volumeSize, id, params); printInstructions(); initGLUT(&argc, argv); createMenu(); gluOrtho2D(0, W, H, 0); glutKeyboardFunc(keyboard); glutSpecialFunc(handleSpecialKeypress); glutDisplayFunc(display); initPixelBuffer(); glutMainLoop(); atexit(exitfunc); return 0; }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { //perform antialiasing with a factor of 4 so ray is computed 4 times at a pixel //and multiplied by factor 1/4 and summed together for antialiased image for(float fragmenti = i; fragmenti < i + 1.0f; fragmenti += 0.5f){ for(float fragmentj = j; fragmentj < j + 1.0f; fragmentj += 0.5f){ // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + fragmentj)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + fragmenti)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. //multiply each ray by 1/4 float coef = 0.25f; Ray3D ray; // want ray to be in world frame ray.origin = viewToWorld*origin; ray.dir = viewToWorld*(imagePlane-origin); ray.dir.normalize(); Colour col = shadeRay(ray); //now sum the total contributions from 4 rays per pixel _rbuffer[i*width+j] += int(col[0]*255*coef); _gbuffer[i*width+j] += int(col[1]*255*coef); _bbuffer[i*width+j] += int(col[2]*255*coef); } } } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, std::string fileName ) { computeTransforms(_root); Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0., 0., 0.); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. // ray -> world space Point3D rayOriginWorld = viewToWorld * imagePlane; Vector3D rayDir = imagePlane - origin; Vector3D rayDirWorld = viewToWorld * rayDir; Ray3D ray; ray.origin = rayOriginWorld; ray.dir = rayDirWorld; ray.dir.normalize(); // pixel colour Colour col = shadeRay(ray); _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
void reshape(int w, int h) { width = w; height = h; initPixelBuffer(); // calculate new grid size gridSize = dim3(iDivUp(width, blockSize.x), iDivUp(height, blockSize.y)); glViewport(0, 0, w, h); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0); }
GR_Fluid::GR_Fluid() { GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); } cu::cutilSafeCall(cu::cudaGLSetGLDevice(cu::cutGetMaxGflopsDeviceId())); //displayX = displayY = -1; displayX = displayY = 256; displaySliceX = displaySliceY = 60; displayEnum = 1; //displayEnum = -1; pbo = 0; initPixelBuffer(true); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName, char mode ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. /////////////////////// OUR CODE /////////////////////////////// Vector3D d = Vector3D(imagePlane[0], imagePlane[1], imagePlane[2]); d = viewToWorld*d; origin = viewToWorld*origin; Ray3D ray = Ray3D( origin , d ); if (mode == 'd'){ (*(ray.intersection.mat)).specular = Colour(0,0,0); } Colour col; col = shadeRay(ray, mode); /////////////////////// OUR CODE /////////////////////////////// _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { printf("\n --------------------- \n RENDERING: %s\n", fileName); printf("\t view[%f, %f, %f]\n", view[0], view[1], view[2]); printf("\t eye[%f, %f, %f]\n", eye[0], eye[1], eye[2]); printf("\t up[%f, %f, %f]\n", up[0], up[1], up[2]); Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); clock_t start = clock(); // color each pixel switch ( getAAMode() ) { case Raytracer::AA_SUPER_SAMPLING: render_SS(width, factor, height, viewToWorld, eye); break; case Raytracer::NONE: render_no_AA(width, factor, height, viewToWorld, eye); break; } clock_t end = clock(); double ms = ((end - start) * 1000)/CLOCKS_PER_SEC; printf("RENDER TIME :: %f ms\n", ms); flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Point3D ray_origin = viewToWorld * imagePlane; Vector3D ray_dir = ray_origin - eye; Ray3D ray(ray_origin, ray_dir); Colour col = shadeRay(ray); _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, int AA_level, char* fileName, char renderStyle ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(_scrHeight)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); /** * Extension: Anti-aliasing level via supersampling method * Algorithm: * Generate an image of dimensions equal to a multiple of the original * dimensions (as specified by the AA level) * Then sample individual pixels and average them to compose a pixel * on the actual screen */ _aaLevel = AA_level; initSuperPixelBuffer(); /// A little print for the user fprintf(stderr, "Rendering %dx%d scene, AA-level %d\n", _scrWidth, _scrHeight, _aaLevel); int superi, superj; double offi, offj; // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Prepare to supersample for anti aliasing for (int m = 0; m < _aaLevel; m++) { for (int n = 0; n < _aaLevel; n++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; offj = double(1)/(0.5 * _aaLevel) + double(n)/_aaLevel; offi = double(1)/(0.5 * _aaLevel) + double(m)/_aaLevel; imagePlane[0] = (-double((_scrWidth))/2 + offj + j)/factor; imagePlane[1] = (-double((_scrHeight))/2 + offi + i)/factor; imagePlane[2] = -1; // Create the transformed ray and shoot it out (shadeRay) Vector3D pixelVector = Vector3D(imagePlane[0], imagePlane[1], imagePlane[2]); Vector3D transformedPixelVector = viewToWorld * pixelVector; Point3D transformedOrigin = viewToWorld * origin; Ray3D ray = Ray3D(transformedOrigin, transformedPixelVector); //Check for scene render style Colour col = shadeRay(ray, renderStyle); superi = i*_aaLevel + m; superj = j*_aaLevel + n; _superrbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[0]*255); _supergbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[1]*255); _superbbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[2]*255); } }//End supersampling loop } }//finsihed pixel loop // Now average out the pixels from the super buffer (supersampling) factor = double(1)/(_aaLevel * _aaLevel); unsigned long rtemp; unsigned long gtemp; unsigned long btemp; for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { rtemp = 0; gtemp = 0; btemp = 0; for (int m = 0; m < _aaLevel; m++) { superi = i*_aaLevel + m; for (int n = 0; n < _aaLevel; n++) { superj = j*_aaLevel + n; rtemp += _superrbuffer[superi*(_aaLevel*_scrWidth)+superj]; gtemp += _supergbuffer[superi*(_aaLevel*_scrWidth)+superj]; btemp += _superbbuffer[superi*(_aaLevel*_scrWidth)+superj]; } } _rbuffer[i*_scrWidth+j] = factor * rtemp; _gbuffer[i*_scrWidth+j] = factor * gtemp; _bbuffer[i*_scrWidth+j] = factor * btemp; } } flushPixelBuffer(fileName); }
int main(int argc, char **argv) { pArgc = &argc; pArgv = argv; char *ref_file = NULL; printf("%s Starting...\n\n", sSDKsample); //start logs if (checkCmdLineFlag(argc, (const char **)argv, "help")) { printHelp(); exit(EXIT_SUCCESS); } if (checkCmdLineFlag(argc, (const char **)argv, "file")) { fpsLimit = frameCheckNumber; getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file); } if (ref_file) { if (checkCmdLineFlag(argc, (const char **)argv, "device")) { int device = findCudaDevice(argc, (const char **)argv); if (device < 0) { printf("No CUDA Capable devices found, exiting...\n"); exit(EXIT_SUCCESS); } checkDeviceMeetComputeSpec(argc, argv); } else { int dev = findCapableDevice(argc, argv); if (dev != -1) { cudaSetDevice(dev); } else { cudaDeviceReset(); exit(EXIT_SUCCESS); } } } else { if (checkCmdLineFlag(argc, (const char **)argv, "device")) { printf(" This SDK does not explicitly support -device=n when running with OpenGL.\n"); printf(" When specifying -device=n (n=0,1,2,....) the sample must not use OpenGL.\n"); printf(" See details below to run without OpenGL:\n\n"); printf(" > %s -device=n -file=output.bin\n\n", argv[0]); printf("exiting...\n"); exit(EXIT_SUCCESS); } // First initialize OpenGL context, so we can properly set the GL for CUDA. // This is necessary in order to achieve optimal performance with OpenGL/CUDA interop. initGL(&argc, argv); int dev = findCapableDevice(argc, argv); if (dev != -1) { cudaGLSetGLDevice(dev); } else { exit(EXIT_SUCCESS); } } // load volume data initData(argc, argv); printf( "Press \n" " 'SPACE' to toggle animation\n" " 'p' to toggle pre-integrated transfer function\n" " '+' and '-' to change density (0.01 increments)\n" " ']' and '[' to change brightness\n" " ';' and ''' to modify transfer function offset\n" " '.' and ',' to modify transfer function scale\n\n"); if (ref_file) { runSingleTest(ref_file, argv[0]); } else { // This is the normal rendering path for VolumeRender glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMotionFunc(motion); glutReshapeFunc(reshape); glutIdleFunc(idle); initPixelBuffer(); atexit(cleanup); glutMainLoop(); } cudaDeviceReset(); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, const char* fileName ) { /*****************anti-aliasing************************/ Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); Colour totalCol(0.0,0.0,0.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); scfCache(_root); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { //anti-aliasing, each pixel can have 4 rays, the pixel color determined by the avgerage for(double a = i; a < i+1 ; a += 0.5){ for(double b = j; b < j+1;b += 0.5){ // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + b)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + a)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Point3D originW = viewToWorld * imagePlane; Vector3D directionW = viewToWorld * (imagePlane -origin); directionW.normalize(); Ray3D ray(originW, directionW); Colour col = shadeRay(ray,0,0); //each ray contributed 0.25 color to the final rending color for the pixel _rbuffer[i*width+j] += int(col[0]*255*0.25); _gbuffer[i*width+j] += int(col[1]*255*0.25); _bbuffer[i*width+j] += int(col[2]*255*0.25); } } } } /********************anti-aliasing**************************/ // Matrix4x4 viewToWorld; // _scrWidth = width; // _scrHeight = height; // double factor = (double(height)/2)/tan(fov*M_PI/360.0); // // initPixelBuffer(); // viewToWorld = initInvViewMatrix(eye, view, up); // scfCache(_root); // // Construct a ray for each pixel. // for (int i = 0; i < _scrHeight; i++) { // for (int j = 0; j < _scrWidth; j++) { // // Sets up ray origin and direction in view space, // // image plane is at z = -1. // Point3D origin(0, 0, 0); // Point3D imagePlane; // imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; // imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; // imagePlane[2] = -1; // // // TODO: Convert ray to world space and call // // shadeRay(ray) to generate pixel colour. // // //Vector3D dir = imagePlane-origin; // Ray3D ray(origin,imagePlane-origin); // /*my dode here*/ // ray.origin = viewToWorld*ray.origin; // ray.dir = viewToWorld*ray.dir; // ray.dir.normalize(); // Colour col = shadeRay(ray,0,0);//the original is shadeRay(ray) // // _rbuffer[i*width+j] = int(col[0]*255); // _gbuffer[i*width+j] = int(col[1]*255); // _bbuffer[i*width+j] = int(col[2]*255); // } // } flushPixelBuffer(fileName); }
//////////////////////////////////////////////////////////////////////////////// // Program main //////////////////////////////////////////////////////////////////////////////// int main( int argc, char** argv) { //start logs shrSetLogFileName ("volumeRender.txt"); shrLog("%s Starting...\n\n", argv[0]); if (cutCheckCmdLineFlag(argc, (const char **)argv, "qatest") || cutCheckCmdLineFlag(argc, (const char **)argv, "noprompt")) { g_bQAReadback = true; fpsLimit = frameCheckNumber; } if (cutCheckCmdLineFlag(argc, (const char **)argv, "glverify")) { g_bQAGLVerify = true; fpsLimit = frameCheckNumber; } if (g_bQAReadback) { // use command-line specified CUDA device, otherwise use device with highest Gflops/s if( cutCheckCmdLineFlag(argc, (const char**)argv, "device") ) { cutilDeviceInit(argc, argv); } else { cudaSetDevice( cutGetMaxGflopsDeviceId() ); } } else { // First initialize OpenGL context, so we can properly set the GL for CUDA. // This is necessary in order to achieve optimal performance with OpenGL/CUDA interop. initGL( &argc, argv ); // use command-line specified CUDA device, otherwise use device with highest Gflops/s if( cutCheckCmdLineFlag(argc, (const char**)argv, "device") ) { cutilGLDeviceInit(argc, argv); } else { cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() ); } /* int device; struct cudaDeviceProp prop; cudaGetDevice( &device ); cudaGetDeviceProperties( &prop, device ); if( !strncmp( "Tesla", prop.name, 5 ) ) { shrLog("This sample needs a card capable of OpenGL and display.\n"); shrLog("Please choose a different device with the -device=x argument.\n"); cutilExit(argc, argv); } */ } // parse arguments char *filename; if (cutGetCmdLineArgumentstr( argc, (const char**) argv, "file", &filename)) { volumeFilename = filename; } int n; if (cutGetCmdLineArgumenti( argc, (const char**) argv, "size", &n)) { volumeSize.width = volumeSize.height = volumeSize.depth = n; } if (cutGetCmdLineArgumenti( argc, (const char**) argv, "xsize", &n)) { volumeSize.width = n; } if (cutGetCmdLineArgumenti( argc, (const char**) argv, "ysize", &n)) { volumeSize.height = n; } if (cutGetCmdLineArgumenti( argc, (const char**) argv, "zsize", &n)) { volumeSize.depth = n; } // load volume data char* path = shrFindFilePath(volumeFilename, argv[0]); if (path == 0) { shrLog("Error finding file '%s'\n", volumeFilename); exit(EXIT_FAILURE); } size_t size = volumeSize.width*volumeSize.height*volumeSize.depth*sizeof(VolumeType); void *h_volume = loadRawFile(path, size); initCuda(h_volume, volumeSize); free(h_volume); cutilCheckError( cutCreateTimer( &timer)); shrLog("Press '=' and '-' to change density\n" " ']' and '[' to change brightness\n" " ';' and ''' to modify transfer function offset\n" " '.' and ',' to modify transfer function scale\n\n"); // calculate new grid size gridSize = dim3(iDivUp(width, blockSize.x), iDivUp(height, blockSize.y)); if (g_bQAReadback) { g_CheckRender = new CheckBackBuffer(width, height, 4, false); g_CheckRender->setPixelFormat(GL_RGBA); g_CheckRender->setExecPath(argv[0]); g_CheckRender->EnableQAReadback(true); uint *d_output; cutilSafeCall(cudaMalloc((void**)&d_output, width*height*sizeof(uint))); cutilSafeCall(cudaMemset(d_output, 0, width*height*sizeof(uint))); float modelView[16] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 4.0f, 1.0f }; invViewMatrix[0] = modelView[0]; invViewMatrix[1] = modelView[4]; invViewMatrix[2] = modelView[8]; invViewMatrix[3] = modelView[12]; invViewMatrix[4] = modelView[1]; invViewMatrix[5] = modelView[5]; invViewMatrix[6] = modelView[9]; invViewMatrix[7] = modelView[13]; invViewMatrix[8] = modelView[2]; invViewMatrix[9] = modelView[6]; invViewMatrix[10] = modelView[10]; invViewMatrix[11] = modelView[14]; // call CUDA kernel, writing results to PBO copyInvViewMatrix(invViewMatrix, sizeof(float4)*3); // Start timer 0 and process n loops on the GPU int nIter = 10; for (int i = -1; i < nIter; i++) { if( i == 0 ) { cudaThreadSynchronize(); cutStartTimer(timer); } render_kernel(gridSize, blockSize, d_output, width, height, density, brightness, transferOffset, transferScale); } cudaThreadSynchronize(); cutStopTimer(timer); // Get elapsed time and throughput, then log to sample and master logs double dAvgTime = cutGetTimerValue(timer)/(nIter * 1000.0); shrLogEx(LOGBOTH | MASTER, 0, "volumeRender, Throughput = %.4f MTexels/s, Time = %.5f s, Size = %u Texels, NumDevsUsed = %u, Workgroup = %u\n", (1.0e-6 * width * height)/dAvgTime, dAvgTime, (width * height), 1, blockSize.x * blockSize.y); cutilCheckMsg("Error: render_kernel() execution FAILED"); cutilSafeCall( cudaThreadSynchronize() ); cutilSafeCall( cudaMemcpy(g_CheckRender->imageData(), d_output, width*height*4, cudaMemcpyDeviceToHost) ); g_CheckRender->savePPM(sOriginal[g_Index], true, NULL); if (!g_CheckRender->PPMvsPPM(sOriginal[g_Index], sReference[g_Index], MAX_EPSILON_ERROR, THRESHOLD)) { shrLog("\nFAILED\n\n"); } else { shrLog("\nPASSED\n\n"); } cudaFree(d_output); freeCudaBuffers(); if (g_CheckRender) { delete g_CheckRender; g_CheckRender = NULL; } } else { // This is the normal rendering path for VolumeRender glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutMotionFunc(motion); glutReshapeFunc(reshape); glutIdleFunc(idle); initPixelBuffer(); if (g_bQAGLVerify) { g_CheckRender = new CheckBackBuffer(width, height, 4); g_CheckRender->setPixelFormat(GL_RGBA); g_CheckRender->setExecPath(argv[0]); g_CheckRender->EnableQAReadback(true); } atexit(cleanup); glutMainLoop(); } cudaThreadExit(); shrEXIT(argc, (const char**)argv); }
int main() { // INIT COMS cd = (Comm_data*) malloc(sizeof(Comm_data)); uart = init_clear_uart(cd); alt_timestamp_start(); srand(alt_timestamp()); CardCtrl *cardCtrl = (CardCtrl*)malloc(sizeof(CardCtrl)); PlayerCtrl* playerCtrl = (PlayerCtrl*)malloc(sizeof(PlayerCtrl)); initCards(cardCtrl); printf("Waiting for players\n"); while(connected_count < 3) { receivedFromAndroid(); } printf("Got all players\n"); initPlayers(playerCtrl, connected_count); int y; for (y = 0; y < connected_count; y++) { if (pid_connected[y] == 1) { tell_user_pid_role(y, playerCtrl->players[y]); Message message = receivedFromAndroid(); if (message.type == ACKNOWLEDGE); } } int i, j; for (i = 0; i < connected_count; i++) { drawCardsForId(i, cardCtrl, 2, playerCtrl); } field = malloc(sizeof(Field)); alt_up_char_buffer_dev *charBuffer = initCharBuffer(); alt_up_pixel_buffer_dma_dev *pixelBuffer = initPixelBuffer(); initField(field, playerCtrl, cardCtrl, charBuffer); *leds = *switches; alt_timestamp_start(); srand(alt_timestamp()); Message message; tell_user_all_opponent_range_role(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn)); message = receivedFromAndroid(); if (message.type == ACKNOWLEDGE); tell_user_all_opponent_blue_lives(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn)); message = receivedFromAndroid(); if (message.type == ACKNOWLEDGE); tell_user_their_turn(0); while (1){ int listening = 1; while (listening) { Message message = receivedFromAndroid(); switch (message.type) { case DRAW_CARDS: drawCardsForId(message.fromId, cardCtrl, message.count, playerCtrl); tell_user_ok(message.fromId); break; case UPDATE_HAND: updateHandForId(playerCtrl, message.fromId, message.count, message.cards); break; case UPDATE_BLUE: updateBlueCardsForId(playerCtrl, message.fromId, message.count, message.cards); tell_user_ok(message.fromId); break; case UPDATE_LIVES: updateLivesForId(playerCtrl, message.fromId, message.count); break; case GATLING: startGatling(playerCtrl, message.fromId); tell_user_ok(message.fromId); break; case ALIENS: startAliens(playerCtrl, message.fromId); tell_user_ok(message.fromId); break; case BEER: { //TODO //updateLivesForId(playerCtrl, message.fromId, message.count); Message message = receivedFromAndroid(); if (message.type == UPDATE_LIVES) updateLivesForId(playerCtrl, message.fromId, message.count); updateHand(message.fromId, playerCtrl); break; } case GENERAL_STORE: startStore(playerCtrl, message.fromId, cardCtrl); break; case SALOON: startSaloon(playerCtrl, message.fromId); tell_user_ok(message.fromId); break; case ZAP: { startZap(playerCtrl, message.toId, message.fromId, message.self); if (message.self == 0) { tell_user_ok(message.fromId); } break; } case PANIC: startPanic(playerCtrl, message.toId, message.fromId, message.self); if (message.self == 0) { tell_user_ok(message.fromId); } break; case CAT_BALOU: startCatBalou(playerCtrl, message.toId, message.fromId, message.self); if (message.self == 0) { tell_user_ok(message.fromId); } break; case DUEL: startDuel(playerCtrl, message.toId, message.fromId, message.self); if (message.self == 0) { tell_user_ok(message.fromId); } break; case JAIL: //TODO tell_user_jail(message.toId, message.cards); break; case END_TURN: listening = 0; break; default: break; } alt_up_char_buffer_clear(charBuffer); runField(field, isGameEnd, winningPlayer); } endTurn(playerCtrl); tell_user_all_opponent_range_role(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn)); message = receivedFromAndroid(); if (message.type == ACKNOWLEDGE); tell_user_all_opponent_blue_lives(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn)); message = receivedFromAndroid(); if (message.type == ACKNOWLEDGE); int y; for (y = 0; y < MAX_CARDS; y++ ) { if (playerCtrl->players[playerCtrl->turn].blueCards[y] >= 72 && playerCtrl->players[playerCtrl->turn].blueCards[y] <= 74) { drawCardsForId(playerCtrl->turn, cardCtrl, 1, playerCtrl); break; } } tell_user_their_turn(playerCtrl->turn); alt_up_char_buffer_clear(charBuffer); runField(field, isGameEnd, winningPlayer); } return 0; }
int main(int argc, char **argv) { //----------------------- // Input pointers float *h_Volume; void* allMemoryPointers[500]; int numberOfMemoryPointers = 0; nifti_image* allNiftiImages[500]; int numberOfNiftiImages = 0; int OPENCL_PLATFORM = 0; int OPENCL_DEVICE = 0; // Size parameters int DATA_H, DATA_W, DATA_D; float VOXEL_SIZE_X, VOXEL_SIZE_Y, VOXEL_SIZE_Z; //--------------------- /* Input arguments */ FILE *fp = NULL; // No inputs, so print help text if (argc == 1) { printf("\nThe function renders a volume using direct volume rendering.\n\n"); printf("Usage:\n\n"); printf("RenderVolume volume.nii [options]\n\n"); printf("Options:\n\n"); printf(" -platform The OpenCL platform to use (default 0) \n"); printf(" -device The OpenCL device to use for the specificed platform (default 0) \n"); printf(" -verbose Print extra stuff (default false) \n"); printf(" -debug Get additional debug information saved as nifti files (default no). Warning: This will use a lot of extra memory! \n"); printf("\n\n"); return EXIT_SUCCESS; } // Try to open files else if (argc > 1) { fp = fopen(argv[1],"r"); if (fp == NULL) { printf("Could not open file %s !\n",argv[1]); return EXIT_FAILURE; } fclose(fp); } // Loop over additional inputs int i = 2; while (i < argc) { char *input = argv[i]; char *p; if (strcmp(input,"-platform") == 0) { if ( (i+1) >= argc ) { printf("Unable to read value after -platform !\n"); return EXIT_FAILURE; } OPENCL_PLATFORM = (int)strtol(argv[i+1], &p, 10); if (!isspace(*p) && *p != 0) { printf("OpenCL platform must be an integer! You provided %s \n",argv[i+1]); return EXIT_FAILURE; } else if (OPENCL_PLATFORM < 0) { printf("OpenCL platform must be >= 0!\n"); return EXIT_FAILURE; } i += 2; } else if (strcmp(input,"-device") == 0) { if ( (i+1) >= argc ) { printf("Unable to read value after -device !\n"); return EXIT_FAILURE; } OPENCL_DEVICE = (int)strtol(argv[i+1], &p, 10); if (!isspace(*p) && *p != 0) { printf("OpenCL device must be an integer! You provided %s \n",argv[i+1]); return EXIT_FAILURE; } else if (OPENCL_DEVICE < 0) { printf("OpenCL device must be >= 0!\n"); return EXIT_FAILURE; } i += 2; } else { printf("Unrecognized option! %s \n",argv[i]); return EXIT_FAILURE; } } // Read first volume // ----------------------------------- nifti_image *inputVolume = nifti_image_read(argv[1],1); if (inputVolume == NULL) { printf("Could not open volume to render!\n"); return EXIT_FAILURE; } allNiftiImages[numberOfNiftiImages] = inputVolume; numberOfNiftiImages++; // ----------------------------------- // Get data dimensions from input data DATA_W = inputVolume->nx; DATA_H = inputVolume->ny; DATA_D = inputVolume->nz; // Get voxel sizes from input data VOXEL_SIZE_X = inputVolume->dx; VOXEL_SIZE_Y = inputVolume->dy; VOXEL_SIZE_Z = inputVolume->dz; int VOLUME_SIZE = DATA_W * DATA_H * DATA_D * sizeof(float); // Print some info printf("Authored by K.A. Eklund \n"); printf("Volume size: %i x %i x %i \n", DATA_W, DATA_H, DATA_D); printf("Volume voxel size: %f x %f x %f mm \n", VOXEL_SIZE_X, VOXEL_SIZE_Y, VOXEL_SIZE_Z); // ------------------------------------------------ // Allocate memory on the host AllocateMemory(h_Volume, VOLUME_SIZE, allMemoryPointers, numberOfMemoryPointers, allNiftiImages, numberOfNiftiImages, "INPUT_VOLUME"); // Convert data to floats if ( inputVolume->datatype == DT_SIGNED_SHORT ) { short int *p = (short int*)inputVolume->data; for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++) { h_Volume[i] = (float)p[i]; } } else if ( inputVolume->datatype == DT_UINT8 ) { unsigned char *p = (unsigned char*)inputVolume->data; for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++) { h_Volume[i] = (float)p[i]; } } else if ( inputVolume->datatype == DT_FLOAT ) { float *p = (float*)inputVolume->data; for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++) { h_Volume[i] = p[i]; } } else { printf("Unknown data type in input volume, aborting!\n"); FreeAllMemory(allMemoryPointers,numberOfMemoryPointers); FreeAllNiftiImages(allNiftiImages,numberOfNiftiImages); return EXIT_FAILURE; } //------------------------ // First initialize OpenGL context, so we can properly setup the OpenGL / OpenCL interop. InitGL(&argc, argv); // Create OpenCL context, get device info, select device, select options for image/texture and CL-GL interop //createCLContext(argc, (const char**)argv); createCLContext(OPENCL_PLATFORM, OPENCL_DEVICE); cl_int error; // create a command-queue cqCommandQueue = clCreateCommandQueue(cxGPUContext, cdDevices[uiDeviceUsed], 0, &ciErrNum); //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup); //clGetDeviceInfo(cdDevices[uiDeviceUsed], CL_DEVICE_IMAGE_SUPPORT, sizeof(g_bImageSupport), &g_bImageSupport, NULL); g_bImageSupport = true; // Read the kernel code from file std::fstream kernelFile("volumeRender.cl",std::ios::in); std::ostringstream oss; oss << kernelFile.rdbuf(); std::string src = oss.str(); const char *srcstr = src.c_str(); // Create program and build the code for the selected device cpProgram = clCreateProgramWithSource(cxGPUContext, 1, (const char**)&srcstr, NULL, &error); printf("Create program with source error is %i \n",error); // build the program std::string buildOpts = "-cl-fast-relaxed-math"; buildOpts += g_bImageSupport ? " -DIMAGE_SUPPORT" : ""; ciErrNum = clBuildProgram(cpProgram, 0, NULL, buildOpts.c_str(), NULL, NULL); printf("Build program error is %i \n",error); if (ciErrNum != CL_SUCCESS) { printf("Building failed!\n"); // write out standard error, Build Log and PTX, then cleanup and return error //shrlogEx(LOGBOTH | ERRORMSG, ciErrNum, STDERROR); //oclLogBuildInfo(cpProgram, oclGetFirstDev(cxGPUContext)); //oclLogPtx(cpProgram, oclGetFirstDev(cxGPUContext), "oclVolumeRender.ptx"); Cleanup(EXIT_FAILURE); } // create the kernel ckKernel = clCreateKernel(cpProgram, "d_render", &error); printf("Create kernel error is %i \n",error); //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup); // Init OpenCL initCLVolume(h_Volume, DATA_W, DATA_H, DATA_D); // init timer 1 for fps measurement //shrDeltaT(1); // Create buffers and textures, // and then start main GLUT rendering loop for processing and rendering, // or otherwise run No-GL Q/A test sequence initPixelBuffer(); glutMainLoop(); // Normally unused return path Cleanup(EXIT_SUCCESS); // Free all memory FreeAllMemory(allMemoryPointers,numberOfMemoryPointers); FreeAllNiftiImages(allNiftiImages,numberOfNiftiImages); return EXIT_SUCCESS; }