1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rxpiece.cpp,v 2.1 2024/09/16 23:49:13 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) |
486 |
return false; // parent can do nothing |
487 |
if (cnt < nproc) { |
488 |
sprintf(errmsg, "only %d renderable tiles, reducing process count", cnt); |
489 |
error(WARNING, errmsg); |
490 |
if ((nproc = cnt) == 1) |
491 |
return false; // back to single process |
492 |
} |
493 |
cow_memshare(); // else we'll be sharing memory |
494 |
fflush(NULL); // and forking children |
495 |
pid_t cpid; // create nproc children |
496 |
for (cnt = nproc; cnt && (cpid = fork()) != 0; cnt--) |
497 |
if (cpid < 0) |
498 |
error(SYSTEM, "fork error!"); |
499 |
|
500 |
if (cpid == 0) { // children render tiles |
501 |
sleep(nproc - cnt); // avoid race conditions |
502 |
return false; |
503 |
} |
504 |
cow_doneshare(); // parent frees memory and waits |
505 |
signal(SIGALRM, SIG_IGN); |
506 |
myRPmanager.Cleanup(true); |
507 |
int nfailed = 0; |
508 |
int status; |
509 |
for (cnt = nproc; cnt && wait(&status) > 0; cnt--) |
510 |
if (status) { |
511 |
sprintf(errmsg, "child exited with status %d", status); |
512 |
error(WARNING, errmsg); |
513 |
if (!nfailed++ & (cnt > 1)) { |
514 |
kill(0, SIGALRM); |
515 |
error(WARNING, "waiting for other tiles to finish..."); |
516 |
} |
517 |
} |
518 |
if (cnt) { |
519 |
sprintf(errmsg, "lost track of %d children", cnt); |
520 |
error(WARNING, errmsg); |
521 |
} |
522 |
if (nfailed) { |
523 |
sprintf(errmsg, "%d tiles were not completed", nfailed); |
524 |
error(USER, errmsg); |
525 |
} |
526 |
return true; // all done! |
527 |
} |
528 |
|
529 |
// return next renderable tile, false if everything is done |
530 |
static bool |
531 |
nexttile(int ti[2]) |
532 |
{ |
533 |
static pid_t ourpID = 0; |
534 |
static short * tlist = NULL; |
535 |
static int tlen = 0; |
536 |
static int tnext = 0; |
537 |
|
538 |
if (gotALRM) { // pre-empting new work? |
539 |
if (tlist) { |
540 |
sprintf(errmsg, "process %d got alarm, exiting", ourpID); |
541 |
CHECK(tnext<tlen, WARNING, errmsg); |
542 |
free(tlist); tlist = NULL; |
543 |
} |
544 |
return false; |
545 |
} |
546 |
if (!tlist) { // initialize random tile list |
547 |
ABitMap2 todoMap(tileGrid[0], tileGrid[1]); |
548 |
tlen = 0; |
549 |
for (ti[1] = 0; ti[1] < tileGrid[1]; ti[1]++) |
550 |
for (ti[0] = 0; ti[0] < tileGrid[0]; ti[0]++) |
551 |
if (renderable_tile(tile_p(ti))) { |
552 |
todoMap.Set(ti[0], ti[1]); |
553 |
tlen++; |
554 |
} |
555 |
if (!tlen) |
556 |
return false; // nothing to do! |
557 |
tlist = (short *)malloc(sizeof(short)*2*tlen); |
558 |
CHECK(!tlist, SYSTEM, "out of memory in nexttile()"); |
559 |
tlen = 0; // assign entries |
560 |
for (ti[0] = ti[1] = 0; todoMap.Find(&ti[0], &ti[1]); ti[0]++) { |
561 |
tlist[2*tlen] = ti[0]; |
562 |
tlist[2*tlen+1] = ti[1]; |
563 |
tlen++; |
564 |
} |
565 |
// shuffle order w/ Fisher-Yates |
566 |
for (int i = 0; i < tlen-1; i++) { |
567 |
const int ix = irandom(tlen-i) + i; |
568 |
ti[0] = tlist[2*i]; |
569 |
ti[1] = tlist[2*i+1]; |
570 |
tlist[2*i] = tlist[2*ix]; |
571 |
tlist[2*i+1] = tlist[2*ix+1]; |
572 |
tlist[2*ix] = ti[0]; |
573 |
tlist[2*ix+1] = ti[1]; |
574 |
} |
575 |
ourpID = getpid(); // save time on system calls |
576 |
} |
577 |
while (tnext < tlen) { // find first available |
578 |
ti[0] = tlist[2*tnext]; |
579 |
ti[1] = tlist[2*tnext+1]; |
580 |
tnext++; // take if still unclaimed |
581 |
if (renderable_tile(tile_p(ti))) { |
582 |
tile_p(ti)->status = -1; |
583 |
tile_p(ti)->pID = ourpID; |
584 |
return true; |
585 |
} |
586 |
} |
587 |
free(tlist); tlist = NULL; // exhausted our list? |
588 |
return false; |
589 |
} |
590 |
|
591 |
|
592 |
// Principal function for rpiece |
593 |
static RenderDataType |
594 |
rpiece(char *pout, RenderDataType dt, char *zout) |
595 |
{ |
596 |
if (zout && *zout == '!') |
597 |
error(USER, "cannot send depth to a command"); |
598 |
|
599 |
const bool newOutput = (access(pout, F_OK) < 0); |
600 |
FILE *pdfp[2]; |
601 |
if (!newOutput) { // output exists? |
602 |
dt = myRPmanager.ReopenOutput(pdfp, pout, zout); |
603 |
if (dt == RDTnone) |
604 |
quit(1); |
605 |
if (!fscnresolu(&hresolu, &vresolu, pdfp[0])) |
606 |
error(USER, "missing picture resolution"); |
607 |
pixaspect = .0; // need to leave this as is |
608 |
myRPmanager.NewHeader(pout); // get prev. header info |
609 |
const char * tval = myRPmanager.GetHeadStr("TILED="); |
610 |
if (tval) sscanf(tval, "%d %d", &tileGrid[0], &tileGrid[1]); |
611 |
if (!myRPmanager.GetView()) { |
612 |
sprintf(errmsg, "missing view in picture file '%s'", pout); |
613 |
error(USER, errmsg); |
614 |
} |
615 |
ourview = *myRPmanager.GetView(); |
616 |
} |
617 |
int hvdim[2] = {hresolu, vresolu}; // set up tiled frame |
618 |
if (!myRPmanager.NewFrame(ourview, hvdim, &pixaspect, tileGrid)) |
619 |
error(USER, "tiling setup error in rpiece"); |
620 |
|
621 |
if ((hvdim[0] != hresolu) | (hvdim[1] != vresolu)) { |
622 |
if (!newOutput) |
623 |
error(USER, "unexpected output size adjustment"); |
624 |
sprintf(errmsg, "resolution adjusted from %dx%d to %dx%d", |
625 |
hresolu, vresolu, hvdim[0], hvdim[1]); |
626 |
error(WARNING, errmsg); |
627 |
hresolu = hvdim[0]; |
628 |
vresolu = hvdim[1]; |
629 |
} |
630 |
if (newOutput){ // open new output here |
631 |
char buf[64]; |
632 |
sprintf(buf, "TILED= %d %d\n", tileGrid[0], tileGrid[1]); |
633 |
myRPmanager.AddHeader(buf); |
634 |
dt = myRPmanager.NewOutput(pdfp, pout, dt, zout); |
635 |
if (dt == RDTnone) |
636 |
quit(1); |
637 |
fprtresolu(hresolu, vresolu, pdfp[0]); |
638 |
} |
639 |
if (RDTdepthT(dt) == RDTdshort) { |
640 |
if (newOutput) |
641 |
fprtresolu(hresolu, vresolu, pdfp[1]); |
642 |
else if (!fscnresolu(&hvdim[0], &hvdim[1], pdfp[1]) || |
643 |
(hvdim[0] != hresolu) | (hvdim[1] != vresolu)) |
644 |
error(USER, "mismatched depth file resolution"); |
645 |
} |
646 |
// prepare (flat) pixel buffer |
647 |
const long pdata_beg = ftell(pdfp[0]); |
648 |
const size_t pixSiz = (RDTcolorT(dt)==RDTrgbe)|(RDTcolorT(dt)==RDTxyze) ? sizeof(COLR) |
649 |
: (RDTcolorT(dt)==RDTrgb)|(RDTcolorT(dt)==RDTxyz) ? sizeof(COLORV)*3 |
650 |
: RDTcolorT(dt)==RDTscolr ? LSCOLR : sizeof(COLORV)*NCSAMP; |
651 |
size_t pmlen = pdata_beg + pixSiz*hresolu*vresolu; |
652 |
// put tile progress array at end |
653 |
if (pmlen&7) pmlen += 8 - (pmlen&7); // 8-byte alignment to be safe |
654 |
pmlen += sizeof(TileProg)*tileGrid[0]*tileGrid[1]; |
655 |
// map picture file to memory |
656 |
if (ftruncate(fileno(pdfp[0]), pmlen) < 0) |
657 |
error(SYSTEM, "cannot extend picture buffer"); |
658 |
uby8 * pixMap = (uby8 *)mmap(NULL, pmlen, PROT_READ|PROT_WRITE, |
659 |
MAP_SHARED, fileno(pdfp[0]), 0); |
660 |
if ((void *)pixMap == MAP_FAILED) |
661 |
error(SYSTEM, "cannot map picture file into memory"); |
662 |
// map depth buffer to memory |
663 |
const long zdata_beg = RDTdepthT(dt) ? ftell(pdfp[1]) : 0L; |
664 |
const size_t zdpSiz = RDTdepthT(dt)==RDTdshort ? sizeof(short) : |
665 |
RDTdepthT(dt)==RDTdfloat ? sizeof(float) : 0; |
666 |
const size_t zmlen = zdata_beg + zdpSiz*hresolu*vresolu; |
667 |
uby8 * zdMap = NULL; |
668 |
if (RDTdepthT(dt)) { |
669 |
if (ftruncate(fileno(pdfp[1]), zmlen) < 0) |
670 |
error(SYSTEM, "cannot extend depth buffer"); |
671 |
zdMap = (uby8 *)mmap(NULL, zmlen, PROT_READ|PROT_WRITE, |
672 |
MAP_SHARED, fileno(pdfp[1]), 0); |
673 |
if ((void *)zdMap == MAP_FAILED) |
674 |
error(SYSTEM, "cannot map depth file into memory"); |
675 |
} |
676 |
fclose(pdfp[0]); // done with file pointers |
677 |
if (RDTdepthT(dt)) fclose(pdfp[1]); |
678 |
// point to tile progress array |
679 |
tprog = (TileProg *)(pixMap + pmlen - sizeof(TileProg)*tileGrid[0]*tileGrid[1]); |
680 |
|
681 |
if (children_finished()) // work done in children? |
682 |
return dt; |
683 |
|
684 |
int ndone = 0; // else render tiles |
685 |
int ti[2]; |
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 |
if (!ndone) |
719 |
error(WARNING, "no tiles need rendering, exit"); |
720 |
/* |
721 |
munmap(pixMap, pmlen); // technically unnecessary... |
722 |
if (zdMap) munmap(zdMap, zmlen); |
723 |
*/ |
724 |
return dt; // we're done here |
725 |
} |