1 /* 2 Copyright (c) 2005-2020 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 static color_t render_one_pixel(int x, 72 int y, 73 unsigned int *local_mbox, 74 unsigned int &serial, 75 int startx, 76 int stopx, 77 int starty, 78 int stopy) { 79 /* private vars moved inside loop */ 80 ray primary; 81 color col; 82 int R, G, B; 83 intersectstruct local_intersections; 84 /* end private */ 85 86 primary = camray(&scene, x, y); 87 primary.intstruct = &local_intersections; 88 primary.flags = RT_RAY_REGULAR; 89 90 serial++; 91 primary.serial = serial; 92 primary.mbox = local_mbox; 93 primary.maxdist = FHUGE; 94 primary.scene = &scene; 95 col = trace(&primary); 96 serial = primary.serial; 97 98 /* Handle overexposure and underexposure here... */ 99 R = (int)(col.r * 255); 100 if (R > 255) 101 R = 255; 102 else if (R < 0) 103 R = 0; 104 105 G = (int)(col.g * 255); 106 if (G > 255) 107 G = 255; 108 else if (G < 0) 109 G = 0; 110 111 B = (int)(col.b * 255); 112 if (B > 255) 113 B = 255; 114 else if (B < 0) 115 B = 0; 116 117 return video->get_color(R, G, B); 118 } 119 120 #if DO_ITT_NOTIFY 121 #include "ittnotify.h" 122 #endif 123 124 #define RUNTIME_SERIAL 1 125 #define RUNTIME_OPENMP 2 126 #define RUNTIME_TBB 3 127 128 #ifndef RUNTIME 129 #define RUNTIME RUNTIME_TBB 130 #endif 131 132 #if RUNTIME == RUNTIME_OPENMP 133 #include <omp.h> 134 #elif RUNTIME == RUNTIME_TBB 135 #include "oneapi/tbb.h" 136 #endif 137 138 static void parallel_thread(void) { 139 unsigned int mboxsize = sizeof(unsigned int) * (max_objectid() + 20); 140 #if RUNTIME == RUNTIME_SERIAL 141 for (int y = starty; y < stopy; y++) 142 #elif RUNTIME == RUNTIME_OPENMP 143 #pragma omp parallel for 144 for (int y = starty; y < stopy; y++) 145 #elif RUNTIME == RUNTIME_TBB 146 oneapi::tbb::parallel_for(starty, stopy, [mboxsize] (int y) 147 #endif 148 { 149 unsigned int serial = 1; 150 unsigned int local_mbox[mboxsize]; 151 memset(local_mbox, 0, mboxsize); 152 drawing_area drawing(startx, totaly - y, stopx - startx, 1); 153 for (int x = startx; x < stopx; x++) { 154 color_t c = render_one_pixel(x, y, local_mbox, serial, startx, stopx, starty, stopy); 155 drawing.put_pixel(c); 156 } 157 video->next_frame(); 158 } 159 #if RUNTIME == RUNTIME_TBB 160 ); 161 #endif 162 } 163 164 void *thread_trace(thr_parms *parms) { 165 // shared but read-only so could be private too 166 all_parms = parms; 167 scene = parms->scene; 168 startx = parms->startx; 169 stopx = parms->stopx; 170 starty = parms->starty; 171 stopy = parms->stopy; 172 jitterscale = 40.0 * (scene.hres + scene.vres); 173 totaly = parms->scene.vres - 1; 174 175 #if DO_ITT_NOTIFY 176 __itt_resume(); 177 #endif 178 parallel_thread(); 179 #if DO_ITT_NOTIFY 180 __itt_pause(); 181 #endif 182 183 return (nullptr); 184 } 185