/* * Save the rendered image to disk. */ static void renderio(scenedef * scene) { flt iotime; char msgtxt[256]; rt_timerhandle ioth; /* I/O timer handle */ ioth=rt_timer_create(); rt_timer_start(ioth); if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB96F) { if (scene->imgprocess & RT_IMAGE_NORMALIZE) { normalize_rgb96f(scene->hres, scene->vres, (float *) scene->img); rt_ui_message(MSG_0, "Post-processing: normalizing pixel values."); } if (scene->imgprocess & RT_IMAGE_GAMMA) { gamma_rgb96f(scene->hres, scene->vres, (float *) scene->img, scene->imggamma); rt_ui_message(MSG_0, "Post-processing: gamma correcting pixel values."); } } else if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB24) { if (scene->imgprocess & (RT_IMAGE_NORMALIZE | RT_IMAGE_GAMMA)) rt_ui_message(MSG_0, "Can't post-process 24-bit integer image data"); } /* support cropping of output images for SPECMPI benchmarks */ if (scene->imgcrop.cropmode == RT_CROP_DISABLED) { writeimage(scene->outfilename, scene->hres, scene->vres, scene->img, scene->imgbufformat, scene->imgfileformat); } else { /* crop image before writing if necessary */ if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB96F) { float *imgcrop; imgcrop = image_crop_rgb96f(scene->hres, scene->vres, scene->img, scene->imgcrop.xres, scene->imgcrop.yres, scene->imgcrop.xstart, scene->imgcrop.ystart); writeimage(scene->outfilename, scene->imgcrop.xres, scene->imgcrop.yres, imgcrop, scene->imgbufformat, scene->imgfileformat); free(imgcrop); } else if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB24) { unsigned char *imgcrop; imgcrop = image_crop_rgb24(scene->hres, scene->vres, scene->img, scene->imgcrop.xres, scene->imgcrop.yres, scene->imgcrop.xstart, scene->imgcrop.ystart); writeimage(scene->outfilename, scene->imgcrop.xres, scene->imgcrop.yres, imgcrop, scene->imgbufformat, scene->imgfileformat); free(imgcrop); } } rt_timer_stop(ioth); iotime = rt_timer_time(ioth); rt_timer_destroy(ioth); sprintf(msgtxt, " Image I/O Time: %10.4f seconds", iotime); rt_ui_message(MSG_0, msgtxt); }
int ray(int argc, char **argv) { #else int main(int argc, char **argv) { #endif SceneHandle scene; unsigned int rc; argoptions opt; char * filename; int node, fileindex; rt_timerhandle parsetimer; size_t len; node = rt_initialize(&argc, &argv); rt_set_ui_message(my_ui_message); rt_set_ui_progress(my_ui_progress); if (node == 0) { printf("Tachyon Parallel/Multiprocessor Ray Tracer Version %s \n", TACHYON_VERSION_STRING); printf("Copyright 1994-2010, John E. Stone <*****@*****.**> \n"); printf("------------------------------------------------------------ \n"); } if ((rc = getargs(argc, argv, &opt, node)) != 0) { rt_finalize(); exit(rc); } if (opt.numfiles > 1) { printf("Rendering %d scene files.\n", opt.numfiles); } for (fileindex=0; fileindex<opt.numfiles; fileindex++) { scene = rt_newscene(); /* process command line overrides */ presceneoptions(&opt, scene); filename = opt.filenames[fileindex]; if (opt.numfiles > 1) { printf("\nRendering scene file %d of %d, %s\n", fileindex+1, opt.numfiles, filename); } parsetimer=rt_timer_create(); rt_timer_start(parsetimer); len = strlen(filename); if (len > 4 && (!strcmp(filename+len-4, ".nff") || !strcmp(filename+len-4, ".NFF"))) { rc = ParseNFF(filename, scene); /* must be an NFF file */ } else if (len > 3 && (!strcmp(filename+len-3, ".ac") || !strcmp(filename+len-3, ".AC"))) { rc = ParseAC3D(filename, scene); /* Must be an AC3D file */ } #ifdef USELIBMGF else if (len > 4 && (!strcmp(filename+len-4, ".mgf") || !strcmp(filename+len-4, ".MGF"))) { rc = ParseMGF(filename, scene, 1); /* Must be an MGF file */ } #endif else { rc = readmodel(filename, scene); /* Assume its a Tachyon scene file */ } rt_timer_stop(parsetimer); if (rc == PARSENOERR && node == 0) printf("Scene Parsing Time: %10.4f seconds\n", rt_timer_time(parsetimer)); rt_timer_destroy(parsetimer); if (rc != PARSENOERR && node == 0) { switch(rc) { case PARSEBADFILE: printf("Parser failed due to nonexistent input file: %s\n", filename); break; case PARSEBADSUBFILE: printf("Parser failed due to nonexistent included file.\n"); break; case PARSEBADSYNTAX: printf("Parser failed due to an input file syntax error.\n"); break; case PARSEEOF: printf("Parser unexpectedly hit an end of file.\n"); break; case PARSEALLOCERR: printf("Parser ran out of memory.\n"); break; } if (fileindex+1 < opt.numfiles) printf("Aborting render, continuing with next scene file...\n"); else printf("Aborting render.\n"); rt_deletescene(scene); /* free the scene */ continue; /* process the next scene */ } /* process command line overrides */ postsceneoptions(&opt, scene); /* choose which rendering mode to use */ if (opt.usecamfile == 1) { return animate_scene(opt, scene, node); /* fly using prerecorded data */ } else if (strlen(opt.spaceball) > 0) { return fly_scene(opt, scene, node); /* fly with spaceball etc */ } else { if (opt.numfiles > 1 && opt.nosave != 1) { char multioutfilename[FILENAME_MAX]; sprintf(multioutfilename, opt.outfilename, fileindex); rt_outputfile(scene, multioutfilename); } rt_renderscene(scene); /* Render a single frame */ } rt_deletescene(scene); /* free the scene, get ready for next one */ } rt_finalize(); /* close down the rendering library and MPI */ freeoptions(&opt); /* free parsed command line option data */ return 0; }
/* * main loop for creating animations by playing recorded camera fly-throughs */ static int animate_scene(argoptions opt, SceneHandle scene, int node) { char outfilename[1000]; FILE * camfp; dispHandle * dh = NULL; if (node == 0) dh = tachyon_display_create(scene); /* if we have a camera file, then animate.. */ if ((camfp = fopen(opt.camfilename, "r")) != NULL) { floatvec cv, cu, cc; apivector cmv, cmu, cmc; int frameno = 0; float fps; rt_timerhandle fpstimer; rt_timerhandle animationtimer; rt_set_ui_message(NULL); rt_set_ui_progress(NULL); if (node == 0) printf("Running Camera File: %s\n", opt.camfilename); fpstimer=rt_timer_create(); animationtimer=rt_timer_create(); rt_timer_start(animationtimer); while (!feof(camfp)) { fscanf(camfp, "%f %f %f %f %f %f %f %f %f", &cv.x, &cv.y, &cv.z, &cu.x, &cu.y, &cu.z, &cc.x, &cc.y, &cc.z); cmv.x = cv.x; cmv.y = cv.y; cmv.z = cv.z; cmu.x = cu.x; cmu.y = cu.y; cmu.z = cu.z; cmc.x = cc.x; cmc.y = cc.y; cmc.z = cc.z; if (frameno != 0) { rt_timer_stop(fpstimer); fps = 1.0f / rt_timer_time(fpstimer); } else { fps = 0.0; } rt_timer_start(fpstimer); outfilename[0] = '\0'; if (opt.nosave == 1) { if (node == 0) { printf("\rRendering Frame: %9d %10.4f FPS ", frameno, fps); fflush(stdout); } } else { sprintf(outfilename, opt.outfilename, frameno); if (node == 0) { printf("\rRendering Frame to %s (%10.4f FPS) ", outfilename, fps); fflush(stdout); } } rt_outputfile(scene, outfilename); rt_camera_position(scene, cmc, cmv, cmu); rt_renderscene(scene); if (dh != NULL) tachyon_display_draw(dh); frameno++; } rt_timer_stop(animationtimer); fps = frameno / rt_timer_time(animationtimer); if (node == 0) { printf("\rCompleted animation of %d frames \n", frameno); printf("Animation Time: %10.4f seconds (Averaged %7.4f FPS)\n", rt_timer_time(animationtimer), fps); } rt_timer_destroy(fpstimer); fclose(camfp); } else { if (node == 0) { printf("Couldn't open camera file: %s\n", opt.camfilename); printf("Aborting render.\n"); } rt_deletescene(scene); /* free the scene */ rt_finalize(); /* close down the rendering library and MPI */ return -1; } if (node == 0) { printf("\nFinished Running Camera.\n"); if (dh !=NULL) tachyon_display_delete(dh); } rt_deletescene(scene); /* free the scene */ rt_finalize(); /* close down the rendering library and MPI */ return 0; }
/* * main loop for creating animations by flying using a spaceball * or other 3-D input mechanism. */ static int fly_scene(argoptions opt, SceneHandle scene, int node) { dispHandle * dh = NULL; int done = 0; int frameno = 0; float fps; rt_timerhandle fpstimer; rt_timerhandle animationtimer; char outfilename[1]; #if defined(USESPACEBALL) sbHandle * bh = NULL; #endif if (node == 0) dh = tachyon_display_create(scene); rt_set_ui_message(NULL); rt_set_ui_progress(NULL); if (node == 0) printf("Interactive Camera Flight\n"); outfilename[0] = '\0'; rt_outputfile(scene, outfilename); fpstimer=rt_timer_create(); animationtimer=rt_timer_create(); #if defined(USESPACEBALL) if (node == 0) { #if 1 bh = tachyon_init_spaceball(scene, opt.spaceball); #else if (rt_numnodes() < 2) { bh = tachyon_init_spaceball(scene, opt.spaceball); } else { printf("WARNING: Spaceball mode disabled when running with distributed memory"); } #endif } #endif rt_timer_start(animationtimer); while (!done) { if (frameno != 0) { rt_timer_stop(fpstimer); fps = 1.0f / rt_timer_time(fpstimer); } else { fps = 0.0; } rt_timer_start(fpstimer); if (node == 0) { printf("\rRendering Frame: %9d %10.4f FPS ", frameno, fps); fflush(stdout); } #if defined(USESPACEBALL) if (bh != NULL) done = tachyon_spaceball_update(bh, scene); #endif rt_renderscene(scene); if (dh != NULL) tachyon_display_draw(dh); frameno++; } rt_timer_stop(animationtimer); fps = frameno / rt_timer_time(animationtimer); if (node == 0) { printf("\rCompleted animation of %d frames \n", frameno); printf("Animation Time: %10.4f seconds (Averaged %7.4f FPS)\n", rt_timer_time(animationtimer), fps); } rt_timer_destroy(fpstimer); if (node == 0) { printf("\nFinished Running Camera.\n"); if (dh !=NULL) tachyon_display_delete(dh); } rt_deletescene(scene); /* free the scene */ rt_finalize(); /* close down the rendering library and MPI */ return 0; }
/* * Render the scene */ void renderscene(scenedef * scene) { flt runtime; rt_timerhandle rtth; /* render time timer handle */ /* if certain key aspects of the scene parameters have been changed */ /* since the last frame rendered, or when rendering the scene the */ /* first time, various setup, initialization and memory allocation */ /* routines need to be run in order to prepare for rendering. */ if (scene->scenecheck) rendercheck(scene); if (scene->mynode == 0) rt_ui_progress(0); /* print 0% progress at start of rendering */ /* * Core Ray Tracing Code * * Ideally, as little as possible other than this code should be * executed for rendering a frame. Most if not all memory allocations * should be done outside of the core code, and all setup should be * done outside of here. This will give the best speed when rendering * walk-throughs and similar things. */ rtth=rt_timer_create(); /* create/init rendering timer */ rt_timer_start(rtth); /* start ray tracing timer */ camera_init(scene); /* Initialize all aspects of camera system */ #if defined(MPI) && defined(THR) /* reset the rows counter for this frame */ rt_atomic_int_set(((thr_parms *) scene->threadparms)[0].rowsdone, 0); #endif #ifdef THR /* if using threads, wake up the child threads... */ rt_thread_barrier(((thr_parms *) scene->threadparms)[0].runbar, 1); #endif #ifdef MPI /* if using message passing, start persistent receives */ rt_start_scanlinereceives(scene->parbuf); /* start scanline receives */ #endif /* Actually Ray Trace The Image */ thread_trace(&((thr_parms *) scene->threadparms)[0]); #ifdef MPI rt_waitscanlines(scene->parbuf); /* wait for all scanlines to recv/send */ #endif rt_timer_stop(rtth); /* stop timer for ray tracing runtime */ runtime=rt_timer_time(rtth); rt_timer_destroy(rtth); /* * End of Core Ray Tracing Code * * Anything after here should be UI, tear-down, or reset code * */ if (scene->mynode == 0) { char msgtxt[256]; rt_ui_progress(100); /* print 100% progress when finished rendering */ sprintf(msgtxt, "\n Ray Tracing Time: %10.4f seconds", runtime); rt_ui_message(MSG_0, msgtxt); if (scene->writeimagefile) renderio(scene); } } /* end of renderscene() */
/* * Check the scene to determine whether or not any parameters that affect * the thread pool, the persistent message passing primitives, or other * infrastructure needs to be reconfigured before rendering commences. */ void rendercheck(scenedef * scene) { flt runtime; rt_timerhandle stth; /* setup time timer handle */ if (scene->verbosemode && scene->mynode == 0) { char msgtxt[1024]; int i, totalcpus; flt totalspeed; rt_ui_message(MSG_0, "CPU Information:"); totalspeed = 0.0; totalcpus = 0; for (i=0; i<scene->nodes; i++) { sprintf(msgtxt, " Node %4d: %2d CPUs, CPU Speed %4.2f, Node Speed %6.2f Name: %s", i, scene->cpuinfo[i].numcpus, scene->cpuinfo[i].cpuspeed, scene->cpuinfo[i].nodespeed, scene->cpuinfo[i].machname); rt_ui_message(MSG_0, msgtxt); totalcpus += scene->cpuinfo[i].numcpus; totalspeed += scene->cpuinfo[i].nodespeed; } sprintf(msgtxt, " Total CPUs: %d", totalcpus); rt_ui_message(MSG_0, msgtxt); sprintf(msgtxt, " Total Speed: %f\n", totalspeed); rt_ui_message(MSG_0, msgtxt); } rt_barrier_sync(); /* synchronize all nodes at this point */ stth=rt_timer_create(); rt_timer_start(stth); /* Time the preprocessing of the scene database */ rt_autoshader(scene); /* Adapt to the shading features needed at runtime */ /* Hierarchical grid ray tracing acceleration scheme */ if (scene->boundmode == RT_BOUNDING_ENABLED) engrid_scene(scene, scene->boundthresh); /* if any clipping groups exist, we have to use appropriate */ /* intersection testing logic */ if (scene->cliplist != NULL) { scene->flags |= RT_SHADE_CLIPPING; } /* if there was a preexisting image, free it before continuing */ if (scene->imginternal && (scene->img != NULL)) { free(scene->img); scene->img = NULL; } /* Allocate a new image buffer if necessary */ if (scene->img == NULL) { scene->imginternal = 1; if (scene->verbosemode && scene->mynode == 0) { rt_ui_message(MSG_0, "Allocating Image Buffer."); } /* allocate the image buffer accordinate to pixel format */ if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB24) { scene->img = malloc(scene->hres * scene->vres * 3); } else if (scene->imgbufformat == RT_IMAGE_BUFFER_RGB96F) { scene->img = malloc(sizeof(float) * scene->hres * scene->vres * 3); } else { rt_ui_message(MSG_0, "Illegal image buffer format specifier!"); } if (scene->img == NULL) { scene->imginternal = 0; rt_ui_message(MSG_0, "Warning: Failed To Allocate Image Buffer!"); } } /* if any threads are leftover from a previous scene, and the */ /* scene has changed significantly, we have to collect, and */ /* respawn the worker threads, since lots of things may have */ /* changed which would affect them. */ destroy_render_threads(scene); create_render_threads(scene); /* allocate and initialize persistent scanline receive buffers */ /* which are used by the parallel message passing code. */ scene->parbuf = rt_init_scanlinereceives(scene); /* the scene has been successfully prepared for rendering */ /* unless it gets modified in certain ways, we don't need to */ /* pre-process it ever again. */ scene->scenecheck = 0; rt_timer_stop(stth); /* Preprocessing is finished, stop timing */ runtime=rt_timer_time(stth); rt_timer_destroy(stth); /* Print out relevent timing info */ if (scene->mynode == 0) { char msgtxt[256]; sprintf(msgtxt, "Preprocessing Time: %10.4f seconds",runtime); rt_ui_message(MSG_0, msgtxt); } }