ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rxpiece.cpp
Revision: 2.5
Committed: Tue Sep 17 16:29:09 2024 UTC (7 months, 2 weeks ago) by greg
Branch: MAIN
Changes since 2.4: +18 -9 lines
Log Message:
perf(rxpiece): Improved flushing and cleanup operations

File Contents

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