ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rxpiece.cpp
Revision: 2.6
Committed: Thu Oct 31 19:22:36 2024 UTC (6 months ago) by greg
Branch: MAIN
Changes since 2.5: +3 -1 lines
Log Message:
fix(rxpiece): Eliminate spurious warnings

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id: rxpiece.cpp,v 2.5 2024/09/17 16:29:09 greg Exp $";
3 greg 2.1 #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 greg 2.5 #include "ambient.h"
18     #include "pmapray.h"
19 greg 2.1 #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 greg 2.5 // We could call myRPmanager.Cleanup() but why waste time
61     // unwinding data structures when the whole frame is going away?
62 greg 2.1 void
63     quit(int code) /* quit program */
64     {
65 greg 2.5 ambsync(); // flush ambient cache
66    
67     ray_done_pmap(); /* PMAP: free photon maps */
68    
69 greg 2.1 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 greg 2.5 quit(dtype==RDTnone); // status is 1 on failure
327 greg 2.1
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 greg 2.6 if (!erract[WARNING].pf)
344     return; // warnings were disabled!
345 greg 2.1 int lasterrno = errno;
346     eputs(s);
347     errno = lasterrno;
348     }
349    
350    
351     void
352     eputs( /* put string to stderr */
353     const char *s
354     )
355     {
356     static int midline = 0;
357    
358     if (!*s)
359     return;
360     if (!midline++) {
361     fputs(progname, stderr);
362     fputs(": ", stderr);
363     }
364     fputs(s, stderr);
365     if (s[strlen(s)-1] == '\n') {
366     fflush(stderr);
367     midline = 0;
368     }
369     }
370    
371    
372     static void
373     onsig( /* fatal signal */
374     int signo
375     )
376     {
377     static int gotsig = 0;
378    
379     if (gotsig++) /* two signals and we're gone! */
380     _exit(signo);
381    
382     alarm(30); /* allow 30 seconds to clean up */
383     signal(SIGALRM, SIG_DFL); /* make certain we do die */
384     eputs("signal - ");
385     eputs(sigerr[signo]);
386     eputs("\n");
387     quit(3);
388     }
389    
390    
391     static bool gotALRM = false; // flag for ALRM signal
392    
393     static void
394     onalrm(int signo)
395     {
396     gotALRM = true;
397     }
398    
399    
400     static void
401     sigdie( /* set fatal signal */
402     int signo,
403     const char *msg
404     )
405     {
406     if (signal(signo, onsig) == SIG_IGN)
407     signal(signo, SIG_IGN);
408     sigerr[signo] = msg;
409     }
410    
411    
412     static void
413     printdefaults(void) /* print default values to stdout */
414     {
415     printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
416     printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
417     ourview.type==VT_PER ? "perspective" :
418     ourview.type==VT_PAR ? "parallel" :
419     ourview.type==VT_HEM ? "hemispherical" :
420     ourview.type==VT_ANG ? "angular" :
421     ourview.type==VT_CYL ? "cylindrical" :
422     ourview.type==VT_PLS ? "planisphere" :
423     "unknown");
424     printf("-vp %f %f %f\t# view point\n",
425     ourview.vp[0], ourview.vp[1], ourview.vp[2]);
426     printf("-vd %f %f %f\t# view direction\n",
427     ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
428     printf("-vu %f %f %f\t# view up\n",
429     ourview.vup[0], ourview.vup[1], ourview.vup[2]);
430     printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
431     printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
432     printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore);
433     printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft);
434     printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
435     printf("-vl %f\t\t\t# view lift\n", ourview.voff);
436     printf("-x %-9d\t\t\t# x resolution\n", hresolu);
437     printf("-y %-9d\t\t\t# y resolution\n", vresolu);
438     printf("-X %-9d\t\t\t# horizontal tile divisions\n", tileGrid[0]);
439     printf("-Y %-9d\t\t\t# vertical tile divisions\n", tileGrid[1]);
440     if (myRPmanager.prims == stdprims)
441     printf("-pRGB\t\t\t\t# standard RGB color output\n");
442     else if (myRPmanager.prims == xyzprims)
443     printf("-pXYZ\t\t\t\t# CIE XYZ color output\n");
444     else if (myRPmanager.prims != NULL)
445     printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n",
446     myRPmanager.prims[RED][0], myRPmanager.prims[RED][1],
447     myRPmanager.prims[GRN][0], myRPmanager.prims[GRN][1],
448     myRPmanager.prims[BLU][0], myRPmanager.prims[BLU][1],
449     myRPmanager.prims[WHT][0], myRPmanager.prims[WHT][1]);
450     if (NCSAMP > 3)
451     printf(myRPmanager.prims != NULL ? "-co-\t\t\t\t# output tristimulus colors\n" :
452     "-co+\t\t\t\t# output spectral values\n");
453     printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect);
454     printf("-pj %f\t\t\t# pixel jitter\n", dstrpix);
455     printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur);
456     printf("-ps %-9d\t\t\t# pixel sample\n", psample);
457     printf("-pt %f\t\t\t# pixel threshold\n", maxdiff);
458     printf(erract[WARNING].pf != NULL ?
459     "-w+\t\t\t\t# warning messages on\n" :
460     "-w-\t\t\t\t# warning messages off\n");
461     print_rdefaults();
462     }
463    
464    
465     // Struct for tracking tiles being rendered in mapped file / shared memory
466     struct TileProg {
467     short status; // 0==Unstarted, -1==InProgress, 1==Done
468     pid_t pID; // process operating on tile
469     } *tprog = NULL; // shared tile progress array
470    
471     #define tile_p(ti) (tprog + (ti)[1]*tileGrid[0] + (ti)[0])
472    
473     // Return true if tile is renderable
474     static bool
475     renderable_tile(TileProg *tp)
476     {
477     if (tp->status < 0 && kill(tp->pID, 0) < 0)
478     tp->status = 0; // dead process - reset
479    
480     return !tp->status;
481     }
482    
483    
484     // handle multi-processing if requested, return true if all done
485     static bool
486     children_finished()
487     {
488     if (nproc <= 1) // single process -> run in parent
489     return false;
490     int cnt = 0; // else count ready-to-go tiles
491     int ti[2];
492     for (ti[1] = 0; ti[1] < tileGrid[1]; ti[1]++)
493     for (ti[0] = 0; ti[0] < tileGrid[0]; ti[0]++)
494     cnt += renderable_tile(tile_p(ti));
495 greg 2.2 if (!cnt)
496     return false; // parent can do nothing
497 greg 2.1 if (cnt < nproc) {
498     sprintf(errmsg, "only %d renderable tiles, reducing process count", cnt);
499     error(WARNING, errmsg);
500     if ((nproc = cnt) == 1)
501     return false; // back to single process
502     }
503     cow_memshare(); // else we'll be sharing memory
504     fflush(NULL); // and forking children
505     pid_t cpid; // create nproc children
506     for (cnt = nproc; cnt && (cpid = fork()) != 0; cnt--)
507     if (cpid < 0)
508     error(SYSTEM, "fork error!");
509    
510     if (cpid == 0) { // children render tiles
511     sleep(nproc - cnt); // avoid race conditions
512     return false;
513     }
514     cow_doneshare(); // parent frees memory and waits
515     signal(SIGALRM, SIG_IGN);
516     myRPmanager.Cleanup(true);
517     int nfailed = 0;
518     int status;
519     for (cnt = nproc; cnt && wait(&status) > 0; cnt--)
520     if (status) {
521     sprintf(errmsg, "child exited with status %d", status);
522     error(WARNING, errmsg);
523     if (!nfailed++ & (cnt > 1)) {
524     kill(0, SIGALRM);
525     error(WARNING, "waiting for other tiles to finish...");
526     }
527     }
528     if (cnt) {
529     sprintf(errmsg, "lost track of %d children", cnt);
530     error(WARNING, errmsg);
531     }
532     if (nfailed) {
533     sprintf(errmsg, "%d tiles were not completed", nfailed);
534     error(USER, errmsg);
535     }
536     return true; // all done!
537     }
538    
539     // return next renderable tile, false if everything is done
540     static bool
541     nexttile(int ti[2])
542     {
543     static pid_t ourpID = 0;
544     static short * tlist = NULL;
545     static int tlen = 0;
546     static int tnext = 0;
547    
548     if (gotALRM) { // pre-empting new work?
549     if (tlist) {
550     sprintf(errmsg, "process %d got alarm, exiting", ourpID);
551     CHECK(tnext<tlen, WARNING, errmsg);
552     free(tlist); tlist = NULL;
553     }
554     return false;
555     }
556     if (!tlist) { // initialize random tile list
557     ABitMap2 todoMap(tileGrid[0], tileGrid[1]);
558     tlen = 0;
559     for (ti[1] = 0; ti[1] < tileGrid[1]; ti[1]++)
560     for (ti[0] = 0; ti[0] < tileGrid[0]; ti[0]++)
561     if (renderable_tile(tile_p(ti))) {
562     todoMap.Set(ti[0], ti[1]);
563     tlen++;
564     }
565     if (!tlen)
566     return false; // nothing to do!
567     tlist = (short *)malloc(sizeof(short)*2*tlen);
568     CHECK(!tlist, SYSTEM, "out of memory in nexttile()");
569     tlen = 0; // assign entries
570     for (ti[0] = ti[1] = 0; todoMap.Find(&ti[0], &ti[1]); ti[0]++) {
571     tlist[2*tlen] = ti[0];
572     tlist[2*tlen+1] = ti[1];
573     tlen++;
574     }
575     // shuffle order w/ Fisher-Yates
576     for (int i = 0; i < tlen-1; i++) {
577     const int ix = irandom(tlen-i) + i;
578     ti[0] = tlist[2*i];
579     ti[1] = tlist[2*i+1];
580     tlist[2*i] = tlist[2*ix];
581     tlist[2*i+1] = tlist[2*ix+1];
582     tlist[2*ix] = ti[0];
583     tlist[2*ix+1] = ti[1];
584     }
585     ourpID = getpid(); // save time on system calls
586     }
587     while (tnext < tlen) { // find first available
588     ti[0] = tlist[2*tnext];
589     ti[1] = tlist[2*tnext+1];
590     tnext++; // take if still unclaimed
591     if (renderable_tile(tile_p(ti))) {
592     tile_p(ti)->status = -1;
593     tile_p(ti)->pID = ourpID;
594     return true;
595     }
596     }
597     free(tlist); tlist = NULL; // exhausted our list?
598     return false;
599     }
600    
601    
602     // Principal function for rpiece
603     static RenderDataType
604     rpiece(char *pout, RenderDataType dt, char *zout)
605     {
606     if (zout && *zout == '!')
607     error(USER, "cannot send depth to a command");
608    
609     const bool newOutput = (access(pout, F_OK) < 0);
610     FILE *pdfp[2];
611 greg 2.4 if (newOutput) { // new output file?
612     CHECK((tileGrid[0] <= 1) & (tileGrid[1] <= 1),
613     USER, "bad tiling specification");
614     } else {
615 greg 2.1 dt = myRPmanager.ReopenOutput(pdfp, pout, zout);
616     if (dt == RDTnone)
617     quit(1);
618     if (!fscnresolu(&hresolu, &vresolu, pdfp[0]))
619     error(USER, "missing picture resolution");
620 greg 2.2 pixaspect = .0; // need to leave this as is
621 greg 2.1 myRPmanager.NewHeader(pout); // get prev. header info
622     const char * tval = myRPmanager.GetHeadStr("TILED=");
623     if (tval) sscanf(tval, "%d %d", &tileGrid[0], &tileGrid[1]);
624 greg 2.4 CHECK(myRPmanager.GetView()==NULL,
625     USER, "missing view in picture file");
626 greg 2.1 ourview = *myRPmanager.GetView();
627     }
628     int hvdim[2] = {hresolu, vresolu}; // set up tiled frame
629     if (!myRPmanager.NewFrame(ourview, hvdim, &pixaspect, tileGrid))
630     error(USER, "tiling setup error in rpiece");
631    
632     if ((hvdim[0] != hresolu) | (hvdim[1] != vresolu)) {
633     if (!newOutput)
634     error(USER, "unexpected output size adjustment");
635     sprintf(errmsg, "resolution adjusted from %dx%d to %dx%d",
636     hresolu, vresolu, hvdim[0], hvdim[1]);
637     error(WARNING, errmsg);
638     hresolu = hvdim[0];
639     vresolu = hvdim[1];
640     }
641     if (newOutput){ // open new output here
642     char buf[64];
643     sprintf(buf, "TILED= %d %d\n", tileGrid[0], tileGrid[1]);
644     myRPmanager.AddHeader(buf);
645     dt = myRPmanager.NewOutput(pdfp, pout, dt, zout);
646     if (dt == RDTnone)
647     quit(1);
648     fprtresolu(hresolu, vresolu, pdfp[0]);
649 greg 2.5 fflush(pdfp[0]);
650     if (RDTdepthT(dt) == RDTdshort) {
651 greg 2.1 fprtresolu(hresolu, vresolu, pdfp[1]);
652 greg 2.5 fflush(pdfp[1]);
653     }
654     } else if (RDTdepthT(dt) == RDTdshort &&
655     (!fscnresolu(&hvdim[0], &hvdim[1], pdfp[1]) ||
656     (hvdim[0] != hresolu) | (hvdim[1] != vresolu)))
657     error(USER, "mismatched depth file resolution");
658 greg 2.1 // prepare (flat) pixel buffer
659     const long pdata_beg = ftell(pdfp[0]);
660     const size_t pixSiz = (RDTcolorT(dt)==RDTrgbe)|(RDTcolorT(dt)==RDTxyze) ? sizeof(COLR)
661     : (RDTcolorT(dt)==RDTrgb)|(RDTcolorT(dt)==RDTxyz) ? sizeof(COLORV)*3
662     : RDTcolorT(dt)==RDTscolr ? LSCOLR : sizeof(COLORV)*NCSAMP;
663     size_t pmlen = pdata_beg + pixSiz*hresolu*vresolu;
664     // put tile progress array at end
665     if (pmlen&7) pmlen += 8 - (pmlen&7); // 8-byte alignment to be safe
666     pmlen += sizeof(TileProg)*tileGrid[0]*tileGrid[1];
667     // map picture file to memory
668 greg 2.4 if (newOutput && ftruncate(fileno(pdfp[0]), pmlen) < 0)
669 greg 2.1 error(SYSTEM, "cannot extend picture buffer");
670     uby8 * pixMap = (uby8 *)mmap(NULL, pmlen, PROT_READ|PROT_WRITE,
671     MAP_SHARED, fileno(pdfp[0]), 0);
672     if ((void *)pixMap == MAP_FAILED)
673     error(SYSTEM, "cannot map picture file into memory");
674     // map depth buffer to memory
675     const long zdata_beg = RDTdepthT(dt) ? ftell(pdfp[1]) : 0L;
676     const size_t zdpSiz = RDTdepthT(dt)==RDTdshort ? sizeof(short) :
677     RDTdepthT(dt)==RDTdfloat ? sizeof(float) : 0;
678     const size_t zmlen = zdata_beg + zdpSiz*hresolu*vresolu;
679     uby8 * zdMap = NULL;
680     if (RDTdepthT(dt)) {
681 greg 2.4 if (newOutput && ftruncate(fileno(pdfp[1]), zmlen) < 0)
682 greg 2.1 error(SYSTEM, "cannot extend depth buffer");
683     zdMap = (uby8 *)mmap(NULL, zmlen, PROT_READ|PROT_WRITE,
684     MAP_SHARED, fileno(pdfp[1]), 0);
685     if ((void *)zdMap == MAP_FAILED)
686     error(SYSTEM, "cannot map depth file into memory");
687     }
688     fclose(pdfp[0]); // done with file pointers
689     if (RDTdepthT(dt)) fclose(pdfp[1]);
690     // point to tile progress array
691     tprog = (TileProg *)(pixMap + pmlen - sizeof(TileProg)*tileGrid[0]*tileGrid[1]);
692    
693     if (children_finished()) // work done in children?
694     return dt;
695    
696 greg 2.2 int ndone = 0; // else render tiles
697     int ti[2];
698 greg 2.1 while (nexttile(ti)) {
699     const int offset = (tileGrid[1]-1-ti[1])*myRPmanager.GetWidth()*myRPmanager.THeight() +
700     (myRPmanager.THeight()-1)*myRPmanager.GetWidth() +
701     ti[0]*myRPmanager.TWidth();
702     uby8 * pptr = pixMap + pdata_beg + pixSiz*offset;
703     uby8 * zptr = zdMap + zdata_beg + zdpSiz*offset;
704     bool ok = false;
705     switch (RDTcommonE(dt)<<1 | (RDTdepthT(dt)==RDTdshort)) {
706     case 2: // common-exponent color, float/no depth
707     ok = myRPmanager.RenderTile((COLRV *)pptr, -myRPmanager.GetWidth(),
708     (float *)zptr, ti);
709     break;
710     case 0: // float color, float/no depth
711     ok = myRPmanager.RenderTile((COLORV *)pptr, -myRPmanager.GetWidth(),
712     (float *)zptr, ti);
713     break;
714     case 3: // common-exponent color, encoded depth
715     ok = myRPmanager.RenderTile((COLRV *)pptr, -myRPmanager.GetWidth(),
716     (short *)zptr, ti);
717     break;
718     case 1: // float color, encoded depth
719     ok = myRPmanager.RenderTile((COLORV *)pptr, -myRPmanager.GetWidth(),
720     (short *)zptr, ti);
721     break;
722     }
723     if (!ok) { // got an error
724     sprintf(errmsg, "error rendering tile (%d,%d)/(%d,%d)",
725     ti[0], ti[1], tileGrid[0], tileGrid[1]);
726     error(USER, errmsg);
727     }
728     tile_p(ti)->status = 1; // mark tile completed
729 greg 2.3 ndone++;
730 greg 2.1 }
731 greg 2.2 if (!ndone)
732     error(WARNING, "no tiles need rendering, exit");
733 greg 2.1 /*
734     munmap(pixMap, pmlen); // technically unnecessary...
735     if (zdMap) munmap(zdMap, zmlen);
736     */
737     return dt; // we're done here
738     }