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