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 
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,
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, sample;
81     color col, avcol;
82     int R, G, B;
83     intersectstruct local_intersections;
84     int alias;
85     /* end private */
86 
87     primary = camray(&scene, x, y);
88     primary.intstruct = &local_intersections;
89     primary.flags = RT_RAY_REGULAR;
90 
91     serial++;
92     primary.serial = serial;
93     primary.mbox = local_mbox;
94     primary.maxdist = FHUGE;
95     primary.scene = &scene;
96     col = trace(&primary);
97 
98     serial = primary.serial;
99 
100     /* perform antialiasing if enabled.. */
101     if (scene.antialiasing > 0) {
102         for (alias = 0; alias < scene.antialiasing; alias++) {
103             serial++; /* increment serial number */
104             sample = primary; /* copy the regular primary ray to start with */
105             sample.serial = serial;
106 
107             {
108                 sample.d.x += ((std::rand() % 100) - 50) / jitterscale;
109                 sample.d.y += ((std::rand() % 100) - 50) / jitterscale;
110                 sample.d.z += ((std::rand() % 100) - 50) / jitterscale;
111             }
112 
113             avcol = trace(&sample);
114 
115             serial = sample.serial; /* update our overall serial # */
116 
117             col.r += avcol.r;
118             col.g += avcol.g;
119             col.b += avcol.b;
120         }
121 
122         col.r /= (scene.antialiasing + 1.0);
123         col.g /= (scene.antialiasing + 1.0);
124         col.b /= (scene.antialiasing + 1.0);
125     }
126 
127     /* Handle overexposure and underexposure here... */
128     R = (int)(col.r * 255);
129     if (R > 255)
130         R = 255;
131     else if (R < 0)
132         R = 0;
133 
134     G = (int)(col.g * 255);
135     if (G > 255)
136         G = 255;
137     else if (G < 0)
138         G = 0;
139 
140     B = (int)(col.b * 255);
141     if (B > 255)
142         B = 255;
143     else if (B < 0)
144         B = 0;
145 
146     return video->get_color(R, G, B);
147 }
148 
parallel_thread(void)149 static void parallel_thread(void) {
150     // thread-local storage
151     unsigned int serial = 1;
152     unsigned int mboxsize = sizeof(unsigned int) * (max_objectid() + 20);
153     unsigned int *local_mbox = (unsigned int *)alloca(mboxsize);
154     memset(local_mbox, 0, mboxsize);
155 
156     for (int y = starty; y < stopy; y++) {
157         {
158             drawing_area drawing(startx, totaly - y, stopx - startx, 1);
159             for (int x = startx; x < stopx; x++) {
160                 color_t c =
161                     render_one_pixel(x, y, local_mbox, serial, startx, stopx, starty, stopy);
162                 drawing.put_pixel(c);
163             }
164         }
165         if (!video->next_frame())
166             return;
167     }
168 }
169 
thread_trace(thr_parms * parms)170 void *thread_trace(thr_parms *parms) {
171     // shared but read-only so could be private too
172     all_parms = parms;
173     scene = parms->scene;
174     startx = parms->startx;
175     stopx = parms->stopx;
176     starty = parms->starty;
177     stopy = parms->stopy;
178     jitterscale = 40.0 * (scene.hres + scene.vres);
179     totaly = parms->scene.vres - 1;
180 
181     parallel_thread();
182 
183     return (nullptr);
184 }
185