1 /* 2 Copyright (c) 2005-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 The original source for this example is 19 Copyright (c) 1994-2008 John E. Stone 20 All rights reserved. 21 22 Redistribution and use in source and binary forms, with or without 23 modification, are permitted provided that the following conditions 24 are met: 25 1. Redistributions of source code must retain the above copyright 26 notice, this list of conditions and the following disclaimer. 27 2. Redistributions in binary form must reproduce the above copyright 28 notice, this list of conditions and the following disclaimer in the 29 documentation and/or other materials provided with the distribution. 30 3. The name of the author may not be used to endorse or promote products 31 derived from this software without specific prior written permission. 32 33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 SUCH DAMAGE. 44 */ 45 46 #include "machine.hpp" 47 #include "types.hpp" 48 #include "macros.hpp" 49 #include "vector.hpp" 50 #include "tgafile.hpp" 51 #include "trace.hpp" 52 #include "light.hpp" 53 #include "shade.hpp" 54 #include "camera.hpp" 55 #include "util.hpp" 56 #include "intersect.hpp" 57 #include "global.hpp" 58 #include "ui.hpp" 59 #include "tachyon_video.hpp" 60 61 // shared but read-only so could be private too 62 static thr_parms *all_parms; 63 static scenedef scene; 64 static int startx; 65 static int stopx; 66 static int starty; 67 static int stopy; 68 static flt jitterscale; 69 static int totaly; 70 71 #include "oneapi/tbb/parallel_for.h" 72 #include "oneapi/tbb/spin_mutex.h" 73 #include "oneapi/tbb/blocked_range.h" 74 #include "oneapi/tbb/global_control.h" 75 #include "common/utility/get_default_num_threads.hpp" 76 77 static oneapi::tbb::spin_mutex MyMutex, MyMutex2; 78 79 static color_t render_one_pixel(int x, 80 int y, 81 unsigned int *local_mbox, 82 unsigned int &serial, 83 int startx, 84 int stopx, 85 int starty, 86 int stopy) { 87 /* private vars moved inside loop */ 88 ray primary, sample; 89 color col, avcol; 90 int R, G, B; 91 intersectstruct local_intersections; 92 int alias; 93 /* end private */ 94 95 primary = camray(&scene, x, y); 96 primary.intstruct = &local_intersections; 97 primary.flags = RT_RAY_REGULAR; 98 99 serial++; 100 primary.serial = serial; 101 primary.mbox = local_mbox; 102 primary.maxdist = FHUGE; 103 primary.scene = &scene; 104 col = trace(&primary); 105 106 serial = primary.serial; 107 108 /* perform antialiasing if enabled.. */ 109 if (scene.antialiasing > 0) { 110 for (alias = 0; alias < scene.antialiasing; alias++) { 111 serial++; /* increment serial number */ 112 sample = primary; /* copy the regular primary ray to start with */ 113 sample.serial = serial; 114 115 { 116 oneapi::tbb::spin_mutex::scoped_lock lock(MyMutex); 117 sample.d.x += ((rand() % 100) - 50) / jitterscale; 118 sample.d.y += ((rand() % 100) - 50) / jitterscale; 119 sample.d.z += ((rand() % 100) - 50) / jitterscale; 120 } 121 122 avcol = trace(&sample); 123 124 serial = sample.serial; /* update our overall serial # */ 125 126 col.r += avcol.r; 127 col.g += avcol.g; 128 col.b += avcol.b; 129 } 130 131 col.r /= (scene.antialiasing + 1.0); 132 col.g /= (scene.antialiasing + 1.0); 133 col.b /= (scene.antialiasing + 1.0); 134 } 135 136 /* Handle overexposure and underexposure here... */ 137 R = (int)(col.r * 255); 138 if (R > 255) 139 R = 255; 140 else if (R < 0) 141 R = 0; 142 143 G = (int)(col.g * 255); 144 if (G > 255) 145 G = 255; 146 else if (G < 0) 147 G = 0; 148 149 B = (int)(col.b * 255); 150 if (B > 255) 151 B = 255; 152 else if (B < 0) 153 B = 0; 154 155 return video->get_color(R, G, B); 156 } 157 158 class parallel_task { 159 public: 160 void operator()(const oneapi::tbb::blocked_range<int> &r) const { 161 // task-local storage 162 unsigned int serial = 1; 163 unsigned int mboxsize = sizeof(unsigned int) * (max_objectid() + 20); 164 unsigned int *local_mbox = (unsigned int *)alloca(mboxsize); 165 memset(local_mbox, 0, mboxsize); 166 167 for (int y = r.begin(); y != r.end(); ++y) { 168 { 169 drawing_area drawing(startx, totaly - y, stopx - startx, 1); 170 for (int x = startx; x < stopx; x++) { 171 color_t c = 172 render_one_pixel(x, y, local_mbox, serial, startx, stopx, starty, stopy); 173 drawing.put_pixel(c); 174 } 175 } 176 if (!video->next_frame()) 177 return; 178 } 179 } 180 181 parallel_task() {} 182 }; 183 184 void *thread_trace(thr_parms *parms) { 185 int n, nthreads = utility::get_default_num_threads(); 186 char *nthreads_str = getenv("TBB_NUM_THREADS"); 187 if (nthreads_str && (sscanf(nthreads_str, "%d", &n) > 0) && (n > 0)) 188 nthreads = n; 189 oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, nthreads); 190 191 // shared but read-only so could be private too 192 all_parms = parms; 193 scene = parms->scene; 194 startx = parms->startx; 195 stopx = parms->stopx; 196 starty = parms->starty; 197 stopy = parms->stopy; 198 jitterscale = 40.0 * (scene.hres + scene.vres); 199 totaly = parms->scene.vres - 1; 200 201 int g, grain_size = 1; 202 char *grain_str = getenv("TBB_GRAINSIZE"); 203 if (grain_str && (sscanf(grain_str, "%d", &g) > 0) && (g > 0)) 204 grain_size = g; 205 char *sched_str = getenv("TBB_PARTITIONER"); 206 static oneapi::tbb::affinity_partitioner g_ap; 207 if (sched_str && !strncmp(sched_str, "aff", 3)) 208 oneapi::tbb::parallel_for( 209 oneapi::tbb::blocked_range<int>(starty, stopy, grain_size), parallel_task(), g_ap); 210 else if (sched_str && !strncmp(sched_str, "simp", 4)) 211 oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<int>(starty, stopy, grain_size), 212 parallel_task(), 213 oneapi::tbb::simple_partitioner()); 214 else 215 oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<int>(starty, stopy, grain_size), 216 parallel_task(), 217 oneapi::tbb::auto_partitioner()); 218 219 return (nullptr); 220 } 221