Shadowrun: Awakened 29 September 2011 - Build 871
trace.tbb.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 /*
00030     The original source for this example is
00031     Copyright (c) 1994-2008 John E. Stone
00032     All rights reserved.
00033 
00034     Redistribution and use in source and binary forms, with or without
00035     modification, are permitted provided that the following conditions
00036     are met:
00037     1. Redistributions of source code must retain the above copyright
00038        notice, this list of conditions and the following disclaimer.
00039     2. Redistributions in binary form must reproduce the above copyright
00040        notice, this list of conditions and the following disclaimer in the
00041        documentation and/or other materials provided with the distribution.
00042     3. The name of the author may not be used to endorse or promote products
00043        derived from this software without specific prior written permission.
00044 
00045     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00046     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00047     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00048     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00049     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00050     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00051     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00052     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00053     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00054     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00055     SUCH DAMAGE.
00056 */
00057 
00058 #include "machine.h"
00059 #include "types.h"
00060 #include "macros.h"
00061 #include "vector.h"
00062 #include "tgafile.h"
00063 #include "trace.h"
00064 #include "light.h"
00065 #include "shade.h"
00066 #include "camera.h"
00067 #include "util.h"
00068 #include "intersect.h"
00069 #include "global.h"
00070 #include "ui.h"
00071 #include "video.h"
00072 
00073 // shared but read-only so could be private too
00074 static thr_parms *all_parms;
00075 static scenedef scene;
00076 static int startx;
00077 static int stopx;
00078 static int starty;
00079 static int stopy;
00080 static flt jitterscale;
00081 static int totaly;
00082 
00083 #include "tbb/task_scheduler_init.h"
00084 #include "tbb/parallel_for.h"
00085 #include "tbb/spin_mutex.h"
00086 #include "tbb/blocked_range2d.h"
00087 
00088 static tbb::spin_mutex MyMutex, MyMutex2;
00089 
00090 static color_t render_one_pixel (int x, int y, unsigned int *local_mbox, unsigned int &serial,
00091                                  int startx, int stopx, int starty, int stopy)
00092 {
00093     /* private vars moved inside loop */
00094     ray primary, sample;
00095     color col, avcol;
00096     int R,G,B;
00097     intersectstruct local_intersections;    
00098     int alias;
00099     /* end private */
00100 
00101     primary=camray(&scene, x, y);
00102     primary.intstruct = &local_intersections;
00103     primary.flags = RT_RAY_REGULAR;
00104 
00105     serial++;
00106     primary.serial = serial;  
00107     primary.mbox = local_mbox;
00108     primary.maxdist = FHUGE;
00109     primary.scene = &scene;
00110     col=trace(&primary);  
00111 
00112     serial = primary.serial;
00113 
00114     /* perform antialiasing if enabled.. */
00115     if (scene.antialiasing > 0) {
00116         for (alias=0; alias < scene.antialiasing; alias++) {
00117 
00118             serial++; /* increment serial number */
00119             sample=primary;  /* copy the regular primary ray to start with */
00120             sample.serial = serial; 
00121 
00122             {
00123                 tbb::spin_mutex::scoped_lock lock (MyMutex);
00124                 sample.d.x+=((rand() % 100) - 50) / jitterscale;
00125                 sample.d.y+=((rand() % 100) - 50) / jitterscale;
00126                 sample.d.z+=((rand() % 100) - 50) / jitterscale;
00127             }
00128 
00129             avcol=trace(&sample);  
00130 
00131             serial = sample.serial; /* update our overall serial # */
00132 
00133             col.r += avcol.r;
00134             col.g += avcol.g;
00135             col.b += avcol.b;
00136         }
00137 
00138         col.r /= (scene.antialiasing + 1.0);
00139         col.g /= (scene.antialiasing + 1.0);
00140         col.b /= (scene.antialiasing + 1.0);
00141     }
00142 
00143     /* Handle overexposure and underexposure here... */
00144     R=(int) (col.r*255);
00145     if (R > 255) R = 255;
00146     else if (R < 0) R = 0;
00147 
00148     G=(int) (col.g*255);
00149     if (G > 255) G = 255;
00150     else if (G < 0) G = 0;
00151 
00152     B=(int) (col.b*255);
00153     if (B > 255) B = 255;
00154     else if (B < 0) B = 0;
00155 
00156     return video->get_color(R, G, B);
00157 }
00158 
00159 class parallel_task {
00160 public:
00161     void operator() (const tbb::blocked_range2d<int> &r) const
00162     {
00163         // task-local storage
00164         unsigned int serial = 1;
00165         unsigned int mboxsize = sizeof(unsigned int)*(max_objectid() + 20);
00166         unsigned int * local_mbox = (unsigned int *) alloca(mboxsize);
00167         memset(local_mbox,0,mboxsize);
00168         if(video->next_frame())
00169         {
00170             drawing_area drawing(r.cols().begin(), totaly-r.rows().end(), r.cols().end() - r.cols().begin(), r.rows().end()-r.rows().begin());
00171             for (int i = 1, y = r.rows().begin(); y != r.rows().end(); ++y, i++) {
00172                 drawing.set_pos(0, drawing.size_y-i);
00173                 for (int x = r.cols().begin(); x != r.cols().end(); x++) {
00174                     color_t c = render_one_pixel (x, y, local_mbox, serial, startx, stopx, starty, stopy);
00175                     drawing.put_pixel(c);
00176                 }
00177             }
00178         }
00179     }
00180 
00181     parallel_task () {}
00182 };
00183 
00184 void * thread_trace(thr_parms * parms)
00185 {
00186     int n, nthreads = tbb::task_scheduler_init::automatic;
00187     char *nthreads_str = getenv ("TBB_NUM_THREADS");
00188     if (nthreads_str && (sscanf (nthreads_str, "%d", &n) > 0) && (n > 0)) nthreads = n;
00189     tbb::task_scheduler_init init (nthreads);
00190 
00191     // shared but read-only so could be private too
00192     all_parms = parms;
00193     scene = parms->scene;
00194     startx = parms->startx;
00195     stopx = parms->stopx;
00196     starty = parms->starty;
00197     stopy = parms->stopy;
00198     jitterscale = 40.0*(scene.hres + scene.vres);
00199     totaly = parms->scene.vres;
00200 
00201     int g, grain_size = 50;
00202     char *grain_str = getenv ("TBB_GRAINSIZE");
00203     if (grain_str && (sscanf (grain_str, "%d", &g) > 0) && (g > 0)) grain_size = g;
00204     tbb::parallel_for (tbb::blocked_range2d<int> (starty, stopy, grain_size, startx, stopx, grain_size), parallel_task (), tbb::simple_partitioner());
00205 
00206     return(NULL);  
00207 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net