1 |
greg |
2.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id: rxpmain.cpp,v 2.2 2024/08/18 17:24:48 greg Exp $"; |
3 |
|
|
#endif |
4 |
|
|
/* |
5 |
|
|
* rxpiece.cpp - main for rxpiece tile rendering program |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "copyright.h" |
9 |
|
|
|
10 |
|
|
#include <time.h> |
11 |
|
|
#include <signal.h> |
12 |
|
|
#include <sys/mman.h> |
13 |
|
|
#include <unistd.h> |
14 |
|
|
|
15 |
|
|
#include "platform.h" |
16 |
|
|
#include "RpictSimulManager.h" |
17 |
|
|
#include "random.h" |
18 |
|
|
|
19 |
|
|
extern char *progname; /* argv[0] */ |
20 |
|
|
const char *sigerr[NSIG]; /* signal error messages */ |
21 |
|
|
|
22 |
|
|
VIEW ourview = STDVIEW; /* global view parameters */ |
23 |
|
|
int hresolu = 1024; /* horizontal resolution */ |
24 |
|
|
int vresolu = 1024; /* vertical resolution */ |
25 |
|
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
26 |
|
|
int hres, vres; /* current image resolution for srcdraw.c */ |
27 |
|
|
|
28 |
|
|
int tileGrid[2] = {5,5}; // tile subdivisions |
29 |
|
|
|
30 |
|
|
int psample = 4; /* pixel sample size */ |
31 |
|
|
double maxdiff = .05; /* max. difference for interpolation */ |
32 |
|
|
double dstrpix = 0.67; /* square pixel distribution */ |
33 |
|
|
|
34 |
|
|
double mblur = 0.; /* motion blur parameter (unused) */ |
35 |
|
|
|
36 |
|
|
double dblur = 0.; /* depth-of-field blur parameter */ |
37 |
|
|
|
38 |
|
|
int nproc = 1; /* number of processes to run */ |
39 |
|
|
|
40 |
|
|
RpictSimulManager myRPmanager; // global simulation manager |
41 |
|
|
|
42 |
|
|
static void onsig(int signo); |
43 |
|
|
static void onalrm(int signo); |
44 |
|
|
static void sigdie(int signo, const char *msg); |
45 |
|
|
static void printdefaults(void); |
46 |
|
|
static RenderDataType rpiece(char *pout, RenderDataType dt, char *zout); |
47 |
|
|
|
48 |
|
|
/* rxpiece additional features */ |
49 |
|
|
#define RXPIECE_FEATURES "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \ |
50 |
|
|
"ParticipatingMedia=Mist\n" \ |
51 |
|
|
"HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \ |
52 |
|
|
"PixelJitter\nPixelSampling\nPixelDepthOfField\n" \ |
53 |
|
|
"SmallSourceDrawing\n" \ |
54 |
|
|
"AdaptiveShadowTesting\nOutputs=v,l\n" \ |
55 |
|
|
"OutputCS=RGB,XYZ,prims,spec\n" |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
void |
59 |
|
|
quit(int code) /* quit program */ |
60 |
|
|
{ |
61 |
|
|
exit(code); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
int |
66 |
|
|
main(int argc, char *argv[]) |
67 |
|
|
{ |
68 |
|
|
#define check(ol,al) if (argv[i][ol] || \ |
69 |
|
|
badarg(argc-i-1,argv+i+1,al)) \ |
70 |
|
|
goto badopt |
71 |
|
|
#define check_bool(olen,var) switch (argv[i][olen]) { \ |
72 |
|
|
case '\0': var = !var; break; \ |
73 |
|
|
case 'y': case 'Y': case 't': case 'T': \ |
74 |
|
|
case '+': case '1': var = 1; break; \ |
75 |
|
|
case 'n': case 'N': case 'f': case 'F': \ |
76 |
|
|
case '-': case '0': var = 0; break; \ |
77 |
|
|
default: goto badopt; } |
78 |
|
|
RGBPRIMS our_prims; /* private output color primitives */ |
79 |
|
|
RenderDataType dtype = RDTrgbe; // output data flags |
80 |
|
|
char *outfile = NULL; |
81 |
|
|
char *zfile = NULL; |
82 |
|
|
int outfmt = 'c'; |
83 |
|
|
int rval; |
84 |
|
|
int i; |
85 |
|
|
/* global program name */ |
86 |
|
|
progname = argv[0]; |
87 |
|
|
/* feature check only? */ |
88 |
|
|
strcat(RFeatureList, RXPIECE_FEATURES); |
89 |
|
|
if (argc > 1 && !strcmp(argv[1], "-features")) |
90 |
|
|
return feature_status(argc-2, argv+2); |
91 |
|
|
/* option city */ |
92 |
|
|
for (i = 1; i < argc; i++) { |
93 |
|
|
/* expand arguments */ |
94 |
|
|
while ((rval = expandarg(&argc, &argv, i)) > 0) |
95 |
|
|
; |
96 |
|
|
if (rval < 0) { |
97 |
|
|
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
98 |
|
|
error(SYSTEM, errmsg); |
99 |
|
|
} |
100 |
|
|
if (argv[i] == NULL || argv[i][0] != '-') |
101 |
|
|
break; /* break from options */ |
102 |
|
|
if (!strcmp(argv[i], "-version")) { |
103 |
|
|
puts(VersionID); |
104 |
|
|
quit(0); |
105 |
|
|
} |
106 |
|
|
if (!strcmp(argv[i], "-defaults") || |
107 |
|
|
!strcmp(argv[i], "-help")) { |
108 |
|
|
printdefaults(); |
109 |
|
|
quit(0); |
110 |
|
|
} |
111 |
|
|
rval = getrenderopt(argc-i, argv+i); |
112 |
|
|
if (rval >= 0) { |
113 |
|
|
i += rval; |
114 |
|
|
continue; |
115 |
|
|
} |
116 |
|
|
rval = getviewopt(&ourview, argc-i, argv+i); |
117 |
|
|
if (rval >= 0) { |
118 |
|
|
i += rval; |
119 |
|
|
continue; |
120 |
|
|
} |
121 |
|
|
/* rxpiece options */ |
122 |
|
|
switch (argv[i][1]) { |
123 |
|
|
case 'v': /* view file */ |
124 |
|
|
if (argv[i][2] != 'f') |
125 |
|
|
goto badopt; |
126 |
|
|
check(3,"s"); |
127 |
|
|
rval = viewfile(argv[++i], &ourview, NULL); |
128 |
|
|
if (rval < 0) { |
129 |
|
|
sprintf(errmsg, |
130 |
|
|
"cannot open view file \"%s\"", |
131 |
|
|
argv[i]); |
132 |
|
|
error(SYSTEM, errmsg); |
133 |
|
|
} else if (rval == 0) { |
134 |
|
|
sprintf(errmsg, |
135 |
|
|
"bad view file \"%s\"", |
136 |
|
|
argv[i]); |
137 |
|
|
error(USER, errmsg); |
138 |
|
|
} |
139 |
|
|
break; |
140 |
|
|
case 'n': /* number of processes */ |
141 |
|
|
check(2,"i"); |
142 |
|
|
nproc = atoi(argv[++i]); |
143 |
|
|
if (nproc < 0 && (nproc += RadSimulManager::GetNCores()) <= 0) |
144 |
|
|
nproc = 1; |
145 |
|
|
break; |
146 |
|
|
case 'f': /* output format */ |
147 |
|
|
if ((argv[i][2] != 'c') & (argv[i][2] != 'f') |
148 |
|
|
|| argv[i][3]) |
149 |
|
|
goto badopt; |
150 |
|
|
outfmt = argv[i][2]; |
151 |
|
|
break; |
152 |
|
|
case 'p': /* pixel */ |
153 |
|
|
switch (argv[i][2]) { |
154 |
|
|
case 's': /* sample */ |
155 |
|
|
check(3,"i"); |
156 |
|
|
psample = atoi(argv[++i]); |
157 |
|
|
if (psample < 1) psample = 1; |
158 |
|
|
break; |
159 |
|
|
case 't': /* threshold */ |
160 |
|
|
check(3,"f"); |
161 |
|
|
maxdiff = atof(argv[++i]); |
162 |
|
|
break; |
163 |
|
|
case 'j': /* jitter */ |
164 |
|
|
check(3,"f"); |
165 |
|
|
dstrpix = atof(argv[++i]); |
166 |
|
|
break; |
167 |
|
|
case 'a': /* aspect */ |
168 |
|
|
check(3,"f"); |
169 |
|
|
pixaspect = atof(argv[++i]); |
170 |
|
|
break; |
171 |
|
|
case 'd': /* aperture */ |
172 |
|
|
check(3,"f"); |
173 |
|
|
dblur = atof(argv[++i]); |
174 |
|
|
dblur *= (dblur > 0); |
175 |
|
|
break; |
176 |
|
|
case 'R': /* standard RGB output */ |
177 |
|
|
if (strcmp(argv[i]+2, "RGB")) |
178 |
|
|
goto badopt; |
179 |
|
|
myRPmanager.prims = stdprims; |
180 |
|
|
dtype = RDTnewCT(dtype, RDTrgbe); |
181 |
|
|
break; |
182 |
|
|
case 'X': /* XYZ output */ |
183 |
|
|
if (strcmp(argv[i]+2, "XYZ")) |
184 |
|
|
goto badopt; |
185 |
|
|
myRPmanager.prims = xyzprims; |
186 |
|
|
dtype = RDTnewCT(dtype, RDTxyze); |
187 |
|
|
break; |
188 |
|
|
case 'c': /* chromaticities */ |
189 |
|
|
check(3,"ffffffff"); |
190 |
|
|
rval = 0; |
191 |
|
|
for (int j = 0; j < 8; j++) { |
192 |
|
|
our_prims[0][j] = atof(argv[++i]); |
193 |
|
|
rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001; |
194 |
|
|
} |
195 |
|
|
if (rval) { |
196 |
|
|
if (!colorprimsOK(our_prims)) |
197 |
|
|
error(USER, "illegal primary chromaticities"); |
198 |
|
|
myRPmanager.prims = our_prims; |
199 |
|
|
} else |
200 |
|
|
myRPmanager.prims = stdprims; |
201 |
|
|
dtype = RDTnewCT(dtype, RDTrgbe); |
202 |
|
|
break; |
203 |
|
|
default: |
204 |
|
|
goto badopt; |
205 |
|
|
} |
206 |
|
|
break; |
207 |
|
|
case 'd': /* reference depth */ |
208 |
|
|
if (argv[i][2] || !myRPmanager.SetReferenceDepth(argv[++i])) |
209 |
|
|
goto badopt; |
210 |
|
|
dtype = RDTnewDT(dtype, RDTdshort); |
211 |
|
|
break; |
212 |
|
|
case 'x': /* x resolution */ |
213 |
|
|
check(2,"i"); |
214 |
|
|
hresolu = atoi(argv[++i]); |
215 |
|
|
break; |
216 |
|
|
case 'y': /* y resolution */ |
217 |
|
|
check(2,"i"); |
218 |
|
|
vresolu = atoi(argv[++i]); |
219 |
|
|
break; |
220 |
|
|
case 'X': /* horizontal tile subdivisions */ |
221 |
|
|
check(2,"i"); |
222 |
|
|
tileGrid[0] = atoi(argv[++i]); |
223 |
|
|
break; |
224 |
|
|
case 'Y': /* vertical tile subdivisions */ |
225 |
|
|
check(2,"i"); |
226 |
|
|
tileGrid[1] = atoi(argv[++i]); |
227 |
|
|
break; |
228 |
|
|
case 'o': /* output file */ |
229 |
|
|
check(2,"s"); |
230 |
|
|
outfile = argv[++i]; |
231 |
|
|
break; |
232 |
|
|
case 'z': /* z file */ |
233 |
|
|
check(2,"s"); |
234 |
|
|
zfile = argv[++i]; |
235 |
|
|
break; |
236 |
|
|
#if MAXCSAMP>3 |
237 |
|
|
case 'c': /* output spectral results */ |
238 |
|
|
if (argv[i][2] != 'o') |
239 |
|
|
goto badopt; |
240 |
|
|
rval = (myRPmanager.prims == NULL); |
241 |
|
|
check_bool(3,rval); |
242 |
|
|
if (rval) |
243 |
|
|
myRPmanager.prims = NULL; |
244 |
|
|
else if (myRPmanager.prims == NULL) |
245 |
|
|
myRPmanager.prims = stdprims; |
246 |
|
|
dtype = RDTnewCT(dtype, rval ? RDTscolr : RDTrgbe); |
247 |
|
|
break; |
248 |
|
|
#endif |
249 |
|
|
case 'w': /* warnings */ |
250 |
|
|
rval = erract[WARNING].pf != NULL; |
251 |
|
|
check_bool(2,rval); |
252 |
|
|
if (rval) erract[WARNING].pf = wputs; |
253 |
|
|
else erract[WARNING].pf = NULL; |
254 |
|
|
break; |
255 |
|
|
default: |
256 |
|
|
goto badopt; |
257 |
|
|
} |
258 |
|
|
} |
259 |
|
|
if (maxdiff <= FTINY) /* check for useless sampling */ |
260 |
|
|
psample = 1; |
261 |
|
|
if (outfile == NULL) |
262 |
|
|
error(USER, "missing output file (-o option)"); |
263 |
|
|
if (zfile == NULL) /* set up depth output */ |
264 |
|
|
dtype = RDTnewDT(dtype, RDTnone); |
265 |
|
|
else if (!RDTdepthT(dtype)) |
266 |
|
|
dtype = RDTnewDT(dtype, RDTdfloat); |
267 |
|
|
/* check pixel output type */ |
268 |
|
|
if ((myRPmanager.prims == NULL) & (NCSAMP == 3)) { |
269 |
|
|
myRPmanager.prims = stdprims; |
270 |
|
|
dtype = RDTnewCT(dtype, RDTrgbe); |
271 |
|
|
} |
272 |
|
|
if (outfmt == 'f') |
273 |
|
|
switch (RDTcolorT(dtype)) { |
274 |
|
|
case RDTrgbe: |
275 |
|
|
dtype = RDTnewCT(dtype, RDTrgb); |
276 |
|
|
break; |
277 |
|
|
case RDTxyze: |
278 |
|
|
dtype = RDTnewCT(dtype, RDTxyz); |
279 |
|
|
break; |
280 |
|
|
case RDTscolr: |
281 |
|
|
dtype = RDTnewCT(dtype, RDTscolor); |
282 |
|
|
break; |
283 |
|
|
case RDTrgb: |
284 |
|
|
case RDTxyz: |
285 |
|
|
case RDTscolor: |
286 |
|
|
break; |
287 |
|
|
default: |
288 |
|
|
error(INTERNAL, "botched color output type"); |
289 |
|
|
} |
290 |
|
|
/* set up signal handling */ |
291 |
|
|
sigdie(SIGINT, "Interrupt"); |
292 |
|
|
sigdie(SIGHUP, "Hangup"); |
293 |
|
|
sigdie(SIGTERM, "Terminate"); |
294 |
|
|
sigdie(SIGPIPE, "Broken pipe"); |
295 |
|
|
signal(SIGALRM, onalrm); // used to gracefully terminate |
296 |
|
|
#ifdef SIGXCPU |
297 |
|
|
sigdie(SIGXCPU, "CPU limit exceeded"); |
298 |
|
|
sigdie(SIGXFSZ, "File size exceeded"); |
299 |
|
|
#endif |
300 |
|
|
#ifdef NICE |
301 |
|
|
nice(NICE); /* lower priority */ |
302 |
|
|
#endif |
303 |
|
|
if (i < argc-1) |
304 |
|
|
goto badopt; |
305 |
|
|
// load octree |
306 |
|
|
if (!myRPmanager.LoadOctree(argv[i])) |
307 |
|
|
error(USER, "missing octree argument"); |
308 |
|
|
// add new header info |
309 |
|
|
myRPmanager.AddHeader(i, argv); |
310 |
|
|
{ |
311 |
|
|
char buf[128] = "SOFTWARE= "; |
312 |
|
|
strcpy(buf+10, VersionID); |
313 |
|
|
myRPmanager.AddHeader(buf); |
314 |
|
|
} |
315 |
|
|
// render tiles |
316 |
|
|
dtype = rpiece(outfile, dtype, zfile); |
317 |
|
|
|
318 |
|
|
quit(dtype==RDTnone); // clean up and exit |
319 |
|
|
|
320 |
|
|
badopt: |
321 |
|
|
sprintf(errmsg, "command line error at '%s'", argv[i]); |
322 |
|
|
error(USER, errmsg); |
323 |
|
|
return 1; /* pro forma return */ |
324 |
|
|
|
325 |
|
|
#undef check |
326 |
|
|
#undef check_bool |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
|
330 |
|
|
void |
331 |
|
|
wputs( /* warning output function */ |
332 |
|
|
const char *s |
333 |
|
|
) |
334 |
|
|
{ |
335 |
|
|
int lasterrno = errno; |
336 |
|
|
eputs(s); |
337 |
|
|
errno = lasterrno; |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
|
341 |
|
|
void |
342 |
|
|
eputs( /* put string to stderr */ |
343 |
|
|
const char *s |
344 |
|
|
) |
345 |
|
|
{ |
346 |
|
|
static int midline = 0; |
347 |
|
|
|
348 |
|
|
if (!*s) |
349 |
|
|
return; |
350 |
|
|
if (!midline++) { |
351 |
|
|
fputs(progname, stderr); |
352 |
|
|
fputs(": ", stderr); |
353 |
|
|
} |
354 |
|
|
fputs(s, stderr); |
355 |
|
|
if (s[strlen(s)-1] == '\n') { |
356 |
|
|
fflush(stderr); |
357 |
|
|
midline = 0; |
358 |
|
|
} |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
|
362 |
|
|
static void |
363 |
|
|
onsig( /* fatal signal */ |
364 |
|
|
int signo |
365 |
|
|
) |
366 |
|
|
{ |
367 |
|
|
static int gotsig = 0; |
368 |
|
|
|
369 |
|
|
if (gotsig++) /* two signals and we're gone! */ |
370 |
|
|
_exit(signo); |
371 |
|
|
|
372 |
|
|
alarm(30); /* allow 30 seconds to clean up */ |
373 |
|
|
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
374 |
|
|
eputs("signal - "); |
375 |
|
|
eputs(sigerr[signo]); |
376 |
|
|
eputs("\n"); |
377 |
|
|
quit(3); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
|
381 |
|
|
static bool gotALRM = false; // flag for ALRM signal |
382 |
|
|
|
383 |
|
|
static void |
384 |
|
|
onalrm(int signo) |
385 |
|
|
{ |
386 |
|
|
gotALRM = true; |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
|
390 |
|
|
static void |
391 |
|
|
sigdie( /* set fatal signal */ |
392 |
|
|
int signo, |
393 |
|
|
const char *msg |
394 |
|
|
) |
395 |
|
|
{ |
396 |
|
|
if (signal(signo, onsig) == SIG_IGN) |
397 |
|
|
signal(signo, SIG_IGN); |
398 |
|
|
sigerr[signo] = msg; |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
|
402 |
|
|
static void |
403 |
|
|
printdefaults(void) /* print default values to stdout */ |
404 |
|
|
{ |
405 |
|
|
printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc); |
406 |
|
|
printf("-vt%c\t\t\t\t# view type %s\n", ourview.type, |
407 |
|
|
ourview.type==VT_PER ? "perspective" : |
408 |
|
|
ourview.type==VT_PAR ? "parallel" : |
409 |
|
|
ourview.type==VT_HEM ? "hemispherical" : |
410 |
|
|
ourview.type==VT_ANG ? "angular" : |
411 |
|
|
ourview.type==VT_CYL ? "cylindrical" : |
412 |
|
|
ourview.type==VT_PLS ? "planisphere" : |
413 |
|
|
"unknown"); |
414 |
|
|
printf("-vp %f %f %f\t# view point\n", |
415 |
|
|
ourview.vp[0], ourview.vp[1], ourview.vp[2]); |
416 |
|
|
printf("-vd %f %f %f\t# view direction\n", |
417 |
|
|
ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); |
418 |
|
|
printf("-vu %f %f %f\t# view up\n", |
419 |
|
|
ourview.vup[0], ourview.vup[1], ourview.vup[2]); |
420 |
|
|
printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz); |
421 |
|
|
printf("-vv %f\t\t\t# view vertical size\n", ourview.vert); |
422 |
|
|
printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore); |
423 |
|
|
printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft); |
424 |
|
|
printf("-vs %f\t\t\t# view shift\n", ourview.hoff); |
425 |
|
|
printf("-vl %f\t\t\t# view lift\n", ourview.voff); |
426 |
|
|
printf("-x %-9d\t\t\t# x resolution\n", hresolu); |
427 |
|
|
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
428 |
|
|
printf("-X %-9d\t\t\t# horizontal tile divisions\n", tileGrid[0]); |
429 |
|
|
printf("-Y %-9d\t\t\t# vertical tile divisions\n", tileGrid[1]); |
430 |
|
|
if (myRPmanager.prims == stdprims) |
431 |
|
|
printf("-pRGB\t\t\t\t# standard RGB color output\n"); |
432 |
|
|
else if (myRPmanager.prims == xyzprims) |
433 |
|
|
printf("-pXYZ\t\t\t\t# CIE XYZ color output\n"); |
434 |
|
|
else if (myRPmanager.prims != NULL) |
435 |
|
|
printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n", |
436 |
|
|
myRPmanager.prims[RED][0], myRPmanager.prims[RED][1], |
437 |
|
|
myRPmanager.prims[GRN][0], myRPmanager.prims[GRN][1], |
438 |
|
|
myRPmanager.prims[BLU][0], myRPmanager.prims[BLU][1], |
439 |
|
|
myRPmanager.prims[WHT][0], myRPmanager.prims[WHT][1]); |
440 |
|
|
if (NCSAMP > 3) |
441 |
|
|
printf(myRPmanager.prims != NULL ? "-co-\t\t\t\t# output tristimulus colors\n" : |
442 |
|
|
"-co+\t\t\t\t# output spectral values\n"); |
443 |
|
|
printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect); |
444 |
|
|
printf("-pj %f\t\t\t# pixel jitter\n", dstrpix); |
445 |
|
|
printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur); |
446 |
|
|
printf("-ps %-9d\t\t\t# pixel sample\n", psample); |
447 |
|
|
printf("-pt %f\t\t\t# pixel threshold\n", maxdiff); |
448 |
|
|
printf(erract[WARNING].pf != NULL ? |
449 |
|
|
"-w+\t\t\t\t# warning messages on\n" : |
450 |
|
|
"-w-\t\t\t\t# warning messages off\n"); |
451 |
|
|
print_rdefaults(); |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
|
455 |
|
|
// Struct for tracking tiles being rendered in mapped file / shared memory |
456 |
|
|
struct TileProg { |
457 |
|
|
short status; // 0==Unstarted, -1==InProgress, 1==Done |
458 |
|
|
pid_t pID; // process operating on tile |
459 |
|
|
} *tprog = NULL; // shared tile progress array |
460 |
|
|
|
461 |
|
|
#define tile_p(ti) (tprog + (ti)[1]*tileGrid[0] + (ti)[0]) |
462 |
|
|
|
463 |
|
|
// Return true if tile is renderable |
464 |
|
|
static bool |
465 |
|
|
renderable_tile(TileProg *tp) |
466 |
|
|
{ |
467 |
|
|
if (tp->status < 0 && kill(tp->pID, 0) < 0) |
468 |
|
|
tp->status = 0; // dead process - reset |
469 |
|
|
|
470 |
|
|
return !tp->status; |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
|
474 |
|
|
// handle multi-processing if requested, return true if all done |
475 |
|
|
static bool |
476 |
|
|
children_finished() |
477 |
|
|
{ |
478 |
|
|
if (nproc <= 1) // single process -> run in parent |
479 |
|
|
return false; |
480 |
|
|
int cnt = 0; // else count ready-to-go tiles |
481 |
|
|
int ti[2]; |
482 |
|
|
for (ti[1] = 0; ti[1] < tileGrid[1]; ti[1]++) |
483 |
|
|
for (ti[0] = 0; ti[0] < tileGrid[0]; ti[0]++) |
484 |
|
|
cnt += renderable_tile(tile_p(ti)); |
485 |
|
|
if (!cnt) { // nothing left to do? |
486 |
|
|
error(WARNING, "output appears to be complete, nothing added"); |
487 |
|
|
return true; |
488 |
|
|
} |
489 |
|
|
if (cnt < nproc) { |
490 |
|
|
sprintf(errmsg, "only %d renderable tiles, reducing process count", cnt); |
491 |
|
|
error(WARNING, errmsg); |
492 |
|
|
if ((nproc = cnt) == 1) |
493 |
|
|
return false; // back to single process |
494 |
|
|
} |
495 |
|
|
cow_memshare(); // else we'll be sharing memory |
496 |
|
|
fflush(NULL); // and forking children |
497 |
|
|
pid_t cpid; // create nproc children |
498 |
|
|
for (cnt = nproc; cnt && (cpid = fork()) != 0; cnt--) |
499 |
|
|
if (cpid < 0) |
500 |
|
|
error(SYSTEM, "fork error!"); |
501 |
|
|
|
502 |
|
|
if (cpid == 0) { // children render tiles |
503 |
|
|
sleep(nproc - cnt); // avoid race conditions |
504 |
|
|
return false; |
505 |
|
|
} |
506 |
|
|
cow_doneshare(); // parent frees memory and waits |
507 |
|
|
signal(SIGALRM, SIG_IGN); |
508 |
|
|
myRPmanager.Cleanup(true); |
509 |
|
|
int nfailed = 0; |
510 |
|
|
int status; |
511 |
|
|
for (cnt = nproc; cnt && wait(&status) > 0; cnt--) |
512 |
|
|
if (status) { |
513 |
|
|
sprintf(errmsg, "child exited with status %d", status); |
514 |
|
|
error(WARNING, errmsg); |
515 |
|
|
if (!nfailed++ & (cnt > 1)) { |
516 |
|
|
kill(0, SIGALRM); |
517 |
|
|
error(WARNING, "waiting for other tiles to finish..."); |
518 |
|
|
} |
519 |
|
|
} |
520 |
|
|
if (cnt) { |
521 |
|
|
sprintf(errmsg, "lost track of %d children", cnt); |
522 |
|
|
error(WARNING, errmsg); |
523 |
|
|
} |
524 |
|
|
if (nfailed) { |
525 |
|
|
sprintf(errmsg, "%d tiles were not completed", nfailed); |
526 |
|
|
error(USER, errmsg); |
527 |
|
|
} |
528 |
|
|
return true; // all done! |
529 |
|
|
} |
530 |
|
|
|
531 |
|
|
// return next renderable tile, false if everything is done |
532 |
|
|
static bool |
533 |
|
|
nexttile(int ti[2]) |
534 |
|
|
{ |
535 |
|
|
static pid_t ourpID = 0; |
536 |
|
|
static short * tlist = NULL; |
537 |
|
|
static int tlen = 0; |
538 |
|
|
static int tnext = 0; |
539 |
|
|
|
540 |
|
|
if (gotALRM) { // pre-empting new work? |
541 |
|
|
if (tlist) { |
542 |
|
|
sprintf(errmsg, "process %d got alarm, exiting", ourpID); |
543 |
|
|
CHECK(tnext<tlen, WARNING, errmsg); |
544 |
|
|
free(tlist); tlist = NULL; |
545 |
|
|
} |
546 |
|
|
return false; |
547 |
|
|
} |
548 |
|
|
if (!tlist) { // initialize random tile list |
549 |
|
|
ABitMap2 todoMap(tileGrid[0], tileGrid[1]); |
550 |
|
|
tlen = 0; |
551 |
|
|
for (ti[1] = 0; ti[1] < tileGrid[1]; ti[1]++) |
552 |
|
|
for (ti[0] = 0; ti[0] < tileGrid[0]; ti[0]++) |
553 |
|
|
if (renderable_tile(tile_p(ti))) { |
554 |
|
|
todoMap.Set(ti[0], ti[1]); |
555 |
|
|
tlen++; |
556 |
|
|
} |
557 |
|
|
if (!tlen) |
558 |
|
|
return false; // nothing to do! |
559 |
|
|
tlist = (short *)malloc(sizeof(short)*2*tlen); |
560 |
|
|
CHECK(!tlist, SYSTEM, "out of memory in nexttile()"); |
561 |
|
|
tlen = 0; // assign entries |
562 |
|
|
for (ti[0] = ti[1] = 0; todoMap.Find(&ti[0], &ti[1]); ti[0]++) { |
563 |
|
|
tlist[2*tlen] = ti[0]; |
564 |
|
|
tlist[2*tlen+1] = ti[1]; |
565 |
|
|
tlen++; |
566 |
|
|
} |
567 |
|
|
// shuffle order w/ Fisher-Yates |
568 |
|
|
for (int i = 0; i < tlen-1; i++) { |
569 |
|
|
const int ix = irandom(tlen-i) + i; |
570 |
|
|
ti[0] = tlist[2*i]; |
571 |
|
|
ti[1] = tlist[2*i+1]; |
572 |
|
|
tlist[2*i] = tlist[2*ix]; |
573 |
|
|
tlist[2*i+1] = tlist[2*ix+1]; |
574 |
|
|
tlist[2*ix] = ti[0]; |
575 |
|
|
tlist[2*ix+1] = ti[1]; |
576 |
|
|
} |
577 |
|
|
ourpID = getpid(); // save time on system calls |
578 |
|
|
} |
579 |
|
|
while (tnext < tlen) { // find first available |
580 |
|
|
ti[0] = tlist[2*tnext]; |
581 |
|
|
ti[1] = tlist[2*tnext+1]; |
582 |
|
|
tnext++; // take if still unclaimed |
583 |
|
|
if (renderable_tile(tile_p(ti))) { |
584 |
|
|
tile_p(ti)->status = -1; |
585 |
|
|
tile_p(ti)->pID = ourpID; |
586 |
|
|
return true; |
587 |
|
|
} |
588 |
|
|
} |
589 |
|
|
free(tlist); tlist = NULL; // exhausted our list? |
590 |
|
|
return false; |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
|
594 |
|
|
// Principal function for rpiece |
595 |
|
|
static RenderDataType |
596 |
|
|
rpiece(char *pout, RenderDataType dt, char *zout) |
597 |
|
|
{ |
598 |
|
|
if (zout && *zout == '!') |
599 |
|
|
error(USER, "cannot send depth to a command"); |
600 |
|
|
|
601 |
|
|
const bool newOutput = (access(pout, F_OK) < 0); |
602 |
|
|
FILE *pdfp[2]; |
603 |
|
|
if (!newOutput) { // output exists? |
604 |
|
|
dt = myRPmanager.ReopenOutput(pdfp, pout, zout); |
605 |
|
|
if (dt == RDTnone) |
606 |
|
|
quit(1); |
607 |
|
|
if (!fscnresolu(&hresolu, &vresolu, pdfp[0])) |
608 |
|
|
error(USER, "missing picture resolution"); |
609 |
|
|
myRPmanager.NewHeader(pout); // get prev. header info |
610 |
|
|
const char * tval = myRPmanager.GetHeadStr("TILED="); |
611 |
|
|
if (tval) sscanf(tval, "%d %d", &tileGrid[0], &tileGrid[1]); |
612 |
|
|
if (!myRPmanager.GetView()) { |
613 |
|
|
sprintf(errmsg, "missing view in picture file '%s'", pout); |
614 |
|
|
error(USER, errmsg); |
615 |
|
|
} |
616 |
|
|
ourview = *myRPmanager.GetView(); |
617 |
|
|
} |
618 |
|
|
int hvdim[2] = {hresolu, vresolu}; // set up tiled frame |
619 |
|
|
if (!myRPmanager.NewFrame(ourview, hvdim, &pixaspect, tileGrid)) |
620 |
|
|
error(USER, "tiling setup error in rpiece"); |
621 |
|
|
|
622 |
|
|
if ((hvdim[0] != hresolu) | (hvdim[1] != vresolu)) { |
623 |
|
|
if (!newOutput) |
624 |
|
|
error(USER, "unexpected output size adjustment"); |
625 |
|
|
sprintf(errmsg, "resolution adjusted from %dx%d to %dx%d", |
626 |
|
|
hresolu, vresolu, hvdim[0], hvdim[1]); |
627 |
|
|
error(WARNING, errmsg); |
628 |
|
|
hresolu = hvdim[0]; |
629 |
|
|
vresolu = hvdim[1]; |
630 |
|
|
} |
631 |
|
|
if (newOutput){ // open new output here |
632 |
|
|
char buf[64]; |
633 |
|
|
sprintf(buf, "TILED= %d %d\n", tileGrid[0], tileGrid[1]); |
634 |
|
|
myRPmanager.AddHeader(buf); |
635 |
|
|
dt = myRPmanager.NewOutput(pdfp, pout, dt, zout); |
636 |
|
|
if (dt == RDTnone) |
637 |
|
|
quit(1); |
638 |
|
|
fprtresolu(hresolu, vresolu, pdfp[0]); |
639 |
|
|
} |
640 |
|
|
if (RDTdepthT(dt) == RDTdshort) { |
641 |
|
|
if (newOutput) |
642 |
|
|
fprtresolu(hresolu, vresolu, pdfp[1]); |
643 |
|
|
else if (!fscnresolu(&hvdim[0], &hvdim[1], pdfp[1]) || |
644 |
|
|
(hvdim[0] != hresolu) | (hvdim[1] != vresolu)) |
645 |
|
|
error(USER, "mismatched depth file resolution"); |
646 |
|
|
} |
647 |
|
|
// prepare (flat) pixel buffer |
648 |
|
|
const long pdata_beg = ftell(pdfp[0]); |
649 |
|
|
const size_t pixSiz = (RDTcolorT(dt)==RDTrgbe)|(RDTcolorT(dt)==RDTxyze) ? sizeof(COLR) |
650 |
|
|
: (RDTcolorT(dt)==RDTrgb)|(RDTcolorT(dt)==RDTxyz) ? sizeof(COLORV)*3 |
651 |
|
|
: RDTcolorT(dt)==RDTscolr ? LSCOLR : sizeof(COLORV)*NCSAMP; |
652 |
|
|
size_t pmlen = pdata_beg + pixSiz*hresolu*vresolu; |
653 |
|
|
// put tile progress array at end |
654 |
|
|
if (pmlen&7) pmlen += 8 - (pmlen&7); // 8-byte alignment to be safe |
655 |
|
|
pmlen += sizeof(TileProg)*tileGrid[0]*tileGrid[1]; |
656 |
|
|
// map picture file to memory |
657 |
|
|
if (ftruncate(fileno(pdfp[0]), pmlen) < 0) |
658 |
|
|
error(SYSTEM, "cannot extend picture buffer"); |
659 |
|
|
uby8 * pixMap = (uby8 *)mmap(NULL, pmlen, PROT_READ|PROT_WRITE, |
660 |
|
|
MAP_SHARED, fileno(pdfp[0]), 0); |
661 |
|
|
if ((void *)pixMap == MAP_FAILED) |
662 |
|
|
error(SYSTEM, "cannot map picture file into memory"); |
663 |
|
|
// map depth buffer to memory |
664 |
|
|
const long zdata_beg = RDTdepthT(dt) ? ftell(pdfp[1]) : 0L; |
665 |
|
|
const size_t zdpSiz = RDTdepthT(dt)==RDTdshort ? sizeof(short) : |
666 |
|
|
RDTdepthT(dt)==RDTdfloat ? sizeof(float) : 0; |
667 |
|
|
const size_t zmlen = zdata_beg + zdpSiz*hresolu*vresolu; |
668 |
|
|
uby8 * zdMap = NULL; |
669 |
|
|
if (RDTdepthT(dt)) { |
670 |
|
|
if (ftruncate(fileno(pdfp[1]), zmlen) < 0) |
671 |
|
|
error(SYSTEM, "cannot extend depth buffer"); |
672 |
|
|
zdMap = (uby8 *)mmap(NULL, zmlen, PROT_READ|PROT_WRITE, |
673 |
|
|
MAP_SHARED, fileno(pdfp[1]), 0); |
674 |
|
|
if ((void *)zdMap == MAP_FAILED) |
675 |
|
|
error(SYSTEM, "cannot map depth file into memory"); |
676 |
|
|
} |
677 |
|
|
fclose(pdfp[0]); // done with file pointers |
678 |
|
|
if (RDTdepthT(dt)) fclose(pdfp[1]); |
679 |
|
|
// point to tile progress array |
680 |
|
|
tprog = (TileProg *)(pixMap + pmlen - sizeof(TileProg)*tileGrid[0]*tileGrid[1]); |
681 |
|
|
|
682 |
|
|
if (children_finished()) // work done in children? |
683 |
|
|
return dt; |
684 |
|
|
|
685 |
|
|
int ti[2]; // else render tiles |
686 |
|
|
while (nexttile(ti)) { |
687 |
|
|
const int offset = (tileGrid[1]-1-ti[1])*myRPmanager.GetWidth()*myRPmanager.THeight() + |
688 |
|
|
(myRPmanager.THeight()-1)*myRPmanager.GetWidth() + |
689 |
|
|
ti[0]*myRPmanager.TWidth(); |
690 |
|
|
uby8 * pptr = pixMap + pdata_beg + pixSiz*offset; |
691 |
|
|
uby8 * zptr = zdMap + zdata_beg + zdpSiz*offset; |
692 |
|
|
bool ok = false; |
693 |
|
|
switch (RDTcommonE(dt)<<1 | (RDTdepthT(dt)==RDTdshort)) { |
694 |
|
|
case 2: // common-exponent color, float/no depth |
695 |
|
|
ok = myRPmanager.RenderTile((COLRV *)pptr, -myRPmanager.GetWidth(), |
696 |
|
|
(float *)zptr, ti); |
697 |
|
|
break; |
698 |
|
|
case 0: // float color, float/no depth |
699 |
|
|
ok = myRPmanager.RenderTile((COLORV *)pptr, -myRPmanager.GetWidth(), |
700 |
|
|
(float *)zptr, ti); |
701 |
|
|
break; |
702 |
|
|
case 3: // common-exponent color, encoded depth |
703 |
|
|
ok = myRPmanager.RenderTile((COLRV *)pptr, -myRPmanager.GetWidth(), |
704 |
|
|
(short *)zptr, ti); |
705 |
|
|
break; |
706 |
|
|
case 1: // float color, encoded depth |
707 |
|
|
ok = myRPmanager.RenderTile((COLORV *)pptr, -myRPmanager.GetWidth(), |
708 |
|
|
(short *)zptr, ti); |
709 |
|
|
break; |
710 |
|
|
} |
711 |
|
|
if (!ok) { // got an error |
712 |
|
|
sprintf(errmsg, "error rendering tile (%d,%d)/(%d,%d)", |
713 |
|
|
ti[0], ti[1], tileGrid[0], tileGrid[1]); |
714 |
|
|
error(USER, errmsg); |
715 |
|
|
} |
716 |
|
|
tile_p(ti)->status = 1; // mark tile completed |
717 |
|
|
} |
718 |
|
|
/* |
719 |
|
|
munmap(pixMap, pmlen); // technically unnecessary... |
720 |
|
|
if (zdMap) munmap(zdMap, zmlen); |
721 |
|
|
*/ |
722 |
|
|
return dt; // we're done here |
723 |
|
|
} |