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