4 |
|
|
5 |
|
/* |
6 |
|
====================================================================== |
7 |
< |
Photon map support for light source contributions |
7 |
> |
Photon map for light source contributions |
8 |
|
|
9 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
10 |
|
(c) Lucerne University of Applied Sciences and Arts, |
23 |
|
#include "pmapdiag.h" |
24 |
|
#include "rcontrib.h" |
25 |
|
#include "otypes.h" |
26 |
< |
#include <sys/mman.h> |
27 |
< |
#include <sys/wait.h> |
26 |
> |
#if NIX |
27 |
> |
#include <sys/mman.h> |
28 |
> |
#include <sys/wait.h> |
29 |
> |
#endif |
30 |
|
|
31 |
|
|
30 |
– |
|
31 |
– |
static void setPmapContribParams (PhotonMap *pmap, LUTAB *srcContrib) |
32 |
– |
/* Set parameters for light source contributions */ |
33 |
– |
{ |
34 |
– |
/* Set light source modifier list and appropriate callback to extract |
35 |
– |
* their contributions from the photon map */ |
36 |
– |
if (pmap) { |
37 |
– |
pmap -> srcContrib = srcContrib; |
38 |
– |
pmap -> lookup = photonContrib; |
39 |
– |
/* Ensure we get all requested photon contribs during lookups */ |
40 |
– |
pmap -> gatherTolerance = 1.0; |
41 |
– |
} |
42 |
– |
} |
43 |
– |
|
44 |
– |
|
45 |
– |
|
46 |
– |
static void checkPmapContribs (const PhotonMap *pmap, LUTAB *srcContrib) |
47 |
– |
/* Check modifiers for light source contributions */ |
48 |
– |
{ |
49 |
– |
const PhotonPrimary *primary = pmap -> primaries; |
50 |
– |
PhotonPrimaryIdx i, found = 0; |
51 |
– |
OBJREC *srcMod; |
52 |
– |
|
53 |
– |
/* Make sure at least one of the modifiers is actually in the pmap, |
54 |
– |
* otherwise findPhotons() winds up in an infinite loop! */ |
55 |
– |
for (i = pmap -> numPrimary; i; --i, ++primary) { |
56 |
– |
if (primary -> srcIdx < 0 || primary -> srcIdx >= nsources) |
57 |
– |
error(INTERNAL, "invalid light source index in photon map"); |
58 |
– |
|
59 |
– |
srcMod = findmaterial(source [primary -> srcIdx].so); |
60 |
– |
if ((MODCONT*)lu_find(srcContrib, srcMod -> oname) -> data) |
61 |
– |
++found; |
62 |
– |
} |
63 |
– |
|
64 |
– |
if (!found) |
65 |
– |
error(USER, "modifiers not in photon map"); |
66 |
– |
} |
67 |
– |
|
68 |
– |
|
69 |
– |
|
70 |
– |
void initPmapContrib (LUTAB *srcContrib, unsigned numSrcContrib) |
71 |
– |
{ |
72 |
– |
unsigned t; |
73 |
– |
|
74 |
– |
for (t = 0; t < NUM_PMAP_TYPES; t++) |
75 |
– |
if (photonMaps [t] && t != PMAP_TYPE_CONTRIB) { |
76 |
– |
sprintf(errmsg, "%s photon map does not support contributions", |
77 |
– |
pmapName [t]); |
78 |
– |
error(USER, errmsg); |
79 |
– |
} |
80 |
– |
|
81 |
– |
/* Get params */ |
82 |
– |
setPmapContribParams(contribPmap, srcContrib); |
83 |
– |
|
84 |
– |
if (contribPhotonMapping) { |
85 |
– |
if (contribPmap -> maxGather < numSrcContrib) { |
86 |
– |
/* Adjust density estimate bandwidth if lower than modifier |
87 |
– |
* count, otherwise contributions are missing */ |
88 |
– |
error(WARNING, "contrib density estimate bandwidth too low, " |
89 |
– |
"adjusting to modifier count"); |
90 |
– |
contribPmap -> maxGather = numSrcContrib; |
91 |
– |
} |
92 |
– |
|
93 |
– |
/* Sanity check */ |
94 |
– |
checkPmapContribs(contribPmap, srcContrib); |
95 |
– |
} |
96 |
– |
} |
97 |
– |
|
98 |
– |
|
99 |
– |
|
32 |
|
static PhotonPrimaryIdx newPhotonPrimary (PhotonMap *pmap, |
33 |
|
const RAY *primRay, |
34 |
|
FILE *primHeap) |
43 |
|
return 0; |
44 |
|
|
45 |
|
/* Check if last primary ray has spawned photons (srcIdx >= 0, see |
46 |
< |
* newPhoton()), in which case we write it to the primary heap file |
47 |
< |
* before overwriting it */ |
46 |
> |
* newPhoton()), in which case we save it to the primary heap file |
47 |
> |
* before clobbering it */ |
48 |
|
if (pmap -> lastPrimary.srcIdx >= 0) { |
49 |
|
if (!fwrite(&pmap -> lastPrimary, sizeof(PhotonPrimary), 1, primHeap)) |
50 |
|
error(SYSTEM, "failed writing photon primary in newPhotonPrimary"); |
60 |
|
|
61 |
|
if (primRay) { |
62 |
|
FVECT dvec; |
63 |
< |
|
63 |
> |
|
64 |
> |
#ifdef PMAP_PRIMARYDIR |
65 |
|
/* Reverse incident direction to point to light source */ |
66 |
|
dvec [0] = -primRay -> rdir [0]; |
67 |
|
dvec [1] = -primRay -> rdir [1]; |
68 |
|
dvec [2] = -primRay -> rdir [2]; |
69 |
|
pmap -> lastPrimary.dir = encodedir(dvec); |
70 |
+ |
#endif |
71 |
|
#ifdef PMAP_PRIMARYPOS |
72 |
|
VCOPY(pmap -> lastPrimary.pos, primRay -> rop); |
73 |
|
#endif |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
< |
#ifdef DEBUG_PMAP_CONTRIB |
81 |
> |
#ifdef DEBUG_PMAP |
82 |
|
static int checkPrimaryHeap (FILE *file) |
83 |
|
/* Check heap for ordered primaries */ |
84 |
|
{ |
118 |
|
|
119 |
|
|
120 |
|
static PhotonPrimaryIdx buildPrimaries (PhotonMap *pmap, FILE **primaryHeap, |
121 |
+ |
char **primaryHeapFname, |
122 |
|
PhotonPrimaryIdx *primaryOfs, |
123 |
|
unsigned numHeaps) |
124 |
|
/* Consolidate per-subprocess photon primary heaps into the primary array |
136 |
|
for (heap = 0; heap < numHeaps; heap++) { |
137 |
|
primaryOfs [heap] = pmap -> numPrimary; |
138 |
|
|
139 |
< |
if (fseek(primaryHeap [heap], 0, SEEK_END)) |
139 |
> |
if (fseek(primaryHeap [heap], 0, SEEK_END) < 0) |
140 |
|
error(SYSTEM, "failed photon primary seek in buildPrimaries"); |
141 |
|
pmap -> numPrimary += heapLen = ftell(primaryHeap [heap]) / |
142 |
|
sizeof(PhotonPrimary); |
152 |
|
heapLen, primaryHeap [heap]) != heapLen) |
153 |
|
error(SYSTEM, "failed reading photon primaries in buildPrimaries"); |
154 |
|
|
155 |
< |
fclose(primaryHeap [heap]); |
155 |
> |
fclose(primaryHeap [heap]); |
156 |
> |
unlink(primaryHeapFname [heap]); |
157 |
|
} |
158 |
|
|
159 |
|
return pmap -> numPrimary; |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
+ |
|
176 |
+ |
|
177 |
+ |
|
178 |
|
void distribPhotonContrib (PhotonMap* pm, unsigned numProc) |
179 |
|
{ |
180 |
|
EmissionMap emap; |
181 |
< |
char errmsg2 [128], shmFname [255]; |
181 |
> |
char errmsg2 [128], shmFname [PMAP_TMPFNLEN]; |
182 |
|
unsigned srcIdx, proc; |
183 |
|
int shmFile, stat, pid; |
184 |
|
double *srcFlux, /* Emitted flux per light source */ |
185 |
|
srcDistribTarget; /* Target photon count per source */ |
186 |
|
PhotonContribCnt *photonCnt; /* Photon emission counter array */ |
187 |
< |
const unsigned photonCntSize = sizeof(PhotonContribCnt) * |
187 |
> |
unsigned photonCntSize = sizeof(PhotonContribCnt) * |
188 |
|
PHOTONCNT_NUMEMIT(nsources); |
189 |
< |
FILE *primaryHeap [numProc]; |
190 |
< |
PhotonPrimaryIdx primaryOfs [numProc]; |
189 |
> |
FILE **primaryHeap = NULL; |
190 |
> |
char **primaryHeapFname = NULL; |
191 |
> |
PhotonPrimaryIdx *primaryOfs = NULL; |
192 |
|
|
193 |
|
if (!pm) |
194 |
|
error(USER, "no photon map defined in distribPhotonContrib"); |
225 |
|
|
226 |
|
/* Per-subprocess / per-source target counts */ |
227 |
|
pm -> distribTarget /= numProc; |
228 |
< |
srcDistribTarget = nsources ? (double)pm -> distribTarget / nsources : 0; |
228 |
> |
srcDistribTarget = nsources ? (double)pm -> distribTarget / nsources : 0; |
229 |
|
|
230 |
+ |
if (!pm -> distribTarget) |
231 |
+ |
error(INTERNAL, "no photons to distribute in distribPhotonContrib"); |
232 |
+ |
|
233 |
|
/* Get photon ports if specified */ |
234 |
|
if (ambincl == 1) |
235 |
|
getPhotonPorts(); |
236 |
|
|
237 |
|
/* Get photon sensor modifiers */ |
238 |
|
getPhotonSensors(photonSensorList); |
239 |
< |
|
239 |
> |
|
240 |
> |
#if NIX |
241 |
|
/* Set up shared mem for photon counters (zeroed by ftruncate) */ |
242 |
< |
#if 0 |
299 |
< |
snprintf(shmFname, 255, PMAP_SHMFNAME, getpid()); |
300 |
< |
shmFile = shm_open(shmFname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
301 |
< |
#else |
302 |
< |
strcpy(shmFname, PMAP_SHMFNAME); |
242 |
> |
strcpy(shmFname, PMAP_TMPFNAME); |
243 |
|
shmFile = mkstemp(shmFname); |
304 |
– |
#endif |
244 |
|
|
245 |
|
if (shmFile < 0 || ftruncate(shmFile, photonCntSize) < 0) |
246 |
|
error(SYSTEM, "failed shared mem init in distribPhotonContrib"); |
250 |
|
|
251 |
|
if (photonCnt == MAP_FAILED) |
252 |
|
error(SYSTEM, "failed shared mem mapping in distribPhotonContrib"); |
253 |
+ |
#else |
254 |
+ |
/* Allocate photon counters statically on Windoze */ |
255 |
+ |
if (!(photonCnt = malloc(photonCntSize))) |
256 |
+ |
error(SYSTEM, "failed trivial malloc in distribPhotonContrib"); |
257 |
+ |
|
258 |
+ |
for (srcIdx = 0; srcIdx < PHOTONCNT_NUMEMIT(nsources); srcIdx++) |
259 |
+ |
photonCnt [srcIdx] = 0; |
260 |
+ |
#endif /* NIX */ |
261 |
|
|
262 |
+ |
if (verbose) { |
263 |
+ |
sprintf(errmsg, "\nIntegrating flux from %d sources", nsources); |
264 |
+ |
|
265 |
+ |
if (photonPorts) { |
266 |
+ |
sprintf(errmsg2, " via %d ports", numPhotonPorts); |
267 |
+ |
strcat(errmsg, errmsg2); |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
strcat(errmsg, "\n"); |
271 |
+ |
eputs(errmsg); |
272 |
+ |
} |
273 |
+ |
|
274 |
|
/* ============================================================= |
275 |
< |
* FLUX INTEGRATION - Get total flux emitted from light source |
275 |
> |
* FLUX INTEGRATION - Get total flux emitted from sources/ports |
276 |
|
* ============================================================= */ |
277 |
|
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
278 |
< |
unsigned portCnt = 0; |
320 |
< |
|
278 |
> |
unsigned portCnt = 0; |
279 |
|
srcFlux [srcIdx] = 0; |
280 |
|
emap.src = source + srcIdx; |
281 |
|
|
324 |
– |
if (photonRepTime) |
325 |
– |
eputs("\n"); |
326 |
– |
|
282 |
|
do { /* Need at least one iteration if no ports! */ |
283 |
|
emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt |
284 |
|
: NULL; |
285 |
|
photonPartition [emap.src -> so -> otype] (&emap); |
286 |
< |
|
287 |
< |
if (photonRepTime) { |
288 |
< |
sprintf(errmsg, "Integrating flux from source %s (mod %s) ", |
289 |
< |
source [srcIdx].so -> oname, |
290 |
< |
objptr(source [srcIdx].so -> omod) -> oname); |
336 |
< |
|
286 |
> |
|
287 |
> |
if (verbose) { |
288 |
> |
sprintf(errmsg, "\tIntegrating flux from source %s ", |
289 |
> |
source [srcIdx].so -> oname); |
290 |
> |
|
291 |
|
if (emap.port) { |
292 |
|
sprintf(errmsg2, "via port %s ", |
293 |
|
photonPorts [portCnt].so -> oname); |
294 |
|
strcat(errmsg, errmsg2); |
295 |
|
} |
296 |
< |
|
297 |
< |
sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions); |
296 |
> |
|
297 |
> |
sprintf(errmsg2, "(%lu partitions)\n", emap.numPartitions); |
298 |
|
strcat(errmsg, errmsg2); |
299 |
|
eputs(errmsg); |
300 |
+ |
#if NIX |
301 |
|
fflush(stderr); |
302 |
< |
} |
302 |
> |
#endif |
303 |
> |
} |
304 |
|
|
305 |
|
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
306 |
|
emap.partitionCnt++) { |
317 |
|
error(WARNING, errmsg); |
318 |
|
} |
319 |
|
} |
320 |
< |
|
321 |
< |
if (photonRepTime) |
322 |
< |
eputs("\n"); |
323 |
< |
|
324 |
< |
/* Init per-subprocess primary heap files */ |
325 |
< |
for (proc = 0; proc < numProc; proc++) |
326 |
< |
if (!(primaryHeap [proc] = tmpfile())) |
320 |
> |
|
321 |
> |
/* Allocate & init per-subprocess primary heap files */ |
322 |
> |
primaryHeap = calloc(numProc, sizeof(FILE*)); |
323 |
> |
primaryHeapFname = calloc(numProc, sizeof(char*)); |
324 |
> |
primaryOfs = calloc(numProc, sizeof(PhotonPrimaryIdx)); |
325 |
> |
if (!primaryHeap || !primaryHeapFname || !primaryOfs) |
326 |
> |
error(SYSTEM, "failed primary heap allocation in " |
327 |
> |
"distribPhotonContrib"); |
328 |
> |
|
329 |
> |
for (proc = 0; proc < numProc; proc++) { |
330 |
> |
primaryHeapFname [proc] = malloc(PMAP_TMPFNLEN); |
331 |
> |
if (!primaryHeapFname [proc]) |
332 |
> |
error(SYSTEM, "failed primary heap file allocation in " |
333 |
> |
"distribPhotonContrib"); |
334 |
> |
|
335 |
> |
mktemp(strcpy(primaryHeapFname [proc], PMAP_TMPFNAME)); |
336 |
> |
if (!(primaryHeap [proc] = fopen(primaryHeapFname [proc], "w+b"))) |
337 |
|
error(SYSTEM, "failed opening primary heap file in " |
338 |
|
"distribPhotonContrib"); |
339 |
+ |
} |
340 |
+ |
|
341 |
+ |
/* Record start time for progress reports */ |
342 |
+ |
repStartTime = time(NULL); |
343 |
+ |
|
344 |
+ |
if (verbose) { |
345 |
+ |
sprintf(errmsg, "\nPhoton distribution @ %d procs\n", numProc); |
346 |
+ |
eputs(errmsg); |
347 |
+ |
} |
348 |
|
|
349 |
|
/* MAIN LOOP */ |
350 |
|
for (proc = 0; proc < numProc; proc++) { |
351 |
+ |
#if NIX |
352 |
|
if (!(pid = fork())) { |
353 |
< |
/* SUBPROCESS ENTERS HERE; |
354 |
< |
* all opened and memory mapped files are inherited */ |
355 |
< |
|
353 |
> |
/* SUBPROCESS ENTERS HERE; opened and mmapped files inherited */ |
354 |
> |
#else |
355 |
> |
if (1) { |
356 |
> |
/* No subprocess under Windoze */ |
357 |
> |
#endif |
358 |
|
/* Local photon counters for this subprocess */ |
359 |
|
unsigned long lastNumPhotons = 0, localNumEmitted = 0; |
360 |
< |
double photonFluxSum = 0; /* Running photon flux sum */ |
360 |
> |
double photonFluxSum = 0; /* Accum. photon flux */ |
361 |
|
|
362 |
|
/* Seed RNGs from PID for decorellated photon distribution */ |
363 |
|
pmapSeed(randSeed + proc, partState); |
364 |
< |
pmapSeed(randSeed + proc, emitState); |
365 |
< |
pmapSeed(randSeed + proc, cntState); |
366 |
< |
pmapSeed(randSeed + proc, mediumState); |
367 |
< |
pmapSeed(randSeed + proc, scatterState); |
368 |
< |
pmapSeed(randSeed + proc, rouletteState); |
364 |
> |
pmapSeed(randSeed + (proc + 1) % numProc, emitState); |
365 |
> |
pmapSeed(randSeed + (proc + 2) % numProc, cntState); |
366 |
> |
pmapSeed(randSeed + (proc + 3) % numProc, mediumState); |
367 |
> |
pmapSeed(randSeed + (proc + 4) % numProc, scatterState); |
368 |
> |
pmapSeed(randSeed + (proc + 5) % numProc, rouletteState); |
369 |
> |
|
370 |
> |
#ifdef PMAP_SIGUSR |
371 |
> |
double partNumEmit; |
372 |
> |
unsigned long partEmitCnt; |
373 |
> |
double srcPhotonFlux, avgPhotonFlux; |
374 |
> |
unsigned portCnt, passCnt, prePassCnt; |
375 |
> |
float srcPreDistrib; |
376 |
> |
double srcNumEmit; /* # to emit from source */ |
377 |
> |
unsigned long srcNumDistrib; /* # stored */ |
378 |
> |
|
379 |
> |
void sigUsrDiags() |
380 |
> |
/* Loop diags via SIGUSR1 */ |
381 |
> |
{ |
382 |
> |
sprintf(errmsg, |
383 |
> |
"********************* Proc %d Diags *********************\n" |
384 |
> |
"srcIdx = %d (%s)\nportCnt = %d (%s)\npassCnt = %d\n" |
385 |
> |
"srcFlux = %f\nsrcPhotonFlux = %f\navgPhotonFlux = %f\n" |
386 |
> |
"partNumEmit = %f\npartEmitCnt = %lu\n\n", |
387 |
> |
proc, srcIdx, findmaterial(source [srcIdx].so) -> oname, |
388 |
> |
portCnt, photonPorts [portCnt].so -> oname, |
389 |
> |
passCnt, srcFlux [srcIdx], srcPhotonFlux, avgPhotonFlux, |
390 |
> |
partNumEmit, partEmitCnt); |
391 |
> |
eputs(errmsg); |
392 |
> |
fflush(stderr); |
393 |
> |
} |
394 |
> |
#endif |
395 |
|
|
396 |
+ |
#if PMAP_SIGUSR |
397 |
+ |
signal(SIGUSR1, sigUsrDiags); |
398 |
+ |
#endif |
399 |
+ |
|
400 |
+ |
/* Output child process PID after random delay to prevent corrupted |
401 |
+ |
* console output due to race condition */ |
402 |
+ |
usleep(1e6 * pmapRandom(rouletteState)); |
403 |
+ |
fprintf(stderr, "Proc %d: PID = %d\n", proc, getpid()); |
404 |
+ |
/* Allow time for debugger to attach to child process */ |
405 |
+ |
sleep(10); |
406 |
+ |
|
407 |
|
/* ============================================================= |
408 |
|
* 2-PASS PHOTON DISTRIBUTION |
409 |
|
* Pass 1 (pre): emit fraction of target photon count |
412 |
|
* count |
413 |
|
* ============================================================= */ |
414 |
|
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
415 |
+ |
#ifndef PMAP_SIGUSR |
416 |
|
unsigned portCnt, passCnt = 0, prePassCnt = 0; |
417 |
|
float srcPreDistrib = preDistrib; |
418 |
|
double srcNumEmit = 0; /* # to emit from source */ |
419 |
|
unsigned long srcNumDistrib = pm -> numPhotons; /* # stored */ |
420 |
+ |
#else |
421 |
+ |
passCnt = prePassCnt = 0; |
422 |
+ |
srcPreDistrib = preDistrib; |
423 |
+ |
srcNumEmit = 0; /* # to emit from source */ |
424 |
+ |
srcNumDistrib = pm -> numPhotons; /* # stored */ |
425 |
+ |
#endif |
426 |
|
|
427 |
|
if (srcFlux [srcIdx] < FTINY) |
428 |
|
continue; |
432 |
|
/* INIT PASS 1 */ |
433 |
|
if (++prePassCnt > maxPreDistrib) { |
434 |
|
/* Warn if no photons contributed after sufficient |
435 |
< |
* iterations */ |
436 |
< |
sprintf(errmsg, "proc %d, source %s: " |
437 |
< |
"too many prepasses, skipped", |
438 |
< |
proc, source [srcIdx].so -> oname); |
439 |
< |
error(WARNING, errmsg); |
435 |
> |
* iterations; only output from subprocess 0 to reduce |
436 |
> |
* console clutter */ |
437 |
> |
if (!proc) { |
438 |
> |
sprintf(errmsg, |
439 |
> |
"source %s: too many prepasses, skipped", |
440 |
> |
source [srcIdx].so -> oname); |
441 |
> |
error(WARNING, errmsg); |
442 |
> |
} |
443 |
> |
|
444 |
|
break; |
445 |
|
} |
446 |
|
|
449 |
|
} |
450 |
|
else { |
451 |
|
/* INIT PASS 2 */ |
452 |
+ |
#ifndef PMAP_SIGUSR |
453 |
|
double srcPhotonFlux, avgPhotonFlux; |
454 |
+ |
#endif |
455 |
|
|
456 |
|
/* Based on the outcome of the predistribution we can now |
457 |
|
* figure out how many more photons we have to emit from |
473 |
|
srcPhotonFlux = srcFlux [srcIdx] / srcNumEmit; |
474 |
|
avgPhotonFlux = photonFluxSum / (srcIdx + 1); |
475 |
|
|
476 |
< |
if (avgPhotonFlux > 0 && |
476 |
> |
if (avgPhotonFlux > FTINY && |
477 |
|
srcPhotonFlux / avgPhotonFlux < FTINY) { |
478 |
|
/* Skip source if its photon flux is grossly below the |
479 |
< |
* running average, indicating negligible contribs at |
480 |
< |
* the expense of excessive distribution time */ |
481 |
< |
sprintf(errmsg, "proc %d, source %s: " |
482 |
< |
"itsy bitsy photon flux, skipped", |
483 |
< |
proc, source [srcIdx].so -> oname); |
484 |
< |
error(WARNING, errmsg); |
485 |
< |
srcNumEmit = 0; |
479 |
> |
* running average, indicating negligible contributions |
480 |
> |
* at the expense of excessive distribution time; only |
481 |
> |
* output from subproc 0 to reduce console clutter */ |
482 |
> |
if (!proc) { |
483 |
> |
sprintf(errmsg, |
484 |
> |
"source %s: itsy bitsy photon flux, skipped", |
485 |
> |
source [srcIdx].so -> oname); |
486 |
> |
error(WARNING, errmsg); |
487 |
> |
} |
488 |
> |
|
489 |
> |
srcNumEmit = 0; /* Or just break??? */ |
490 |
|
} |
491 |
|
|
492 |
|
/* Update sum of photon flux per light source */ |
493 |
|
photonFluxSum += srcPhotonFlux; |
494 |
|
} |
495 |
< |
|
495 |
> |
|
496 |
|
portCnt = 0; |
497 |
|
do { /* Need at least one iteration if no ports! */ |
498 |
|
emap.src = source + srcIdx; |
499 |
|
emap.port = emap.src -> sflags & SDISTANT |
500 |
|
? photonPorts + portCnt : NULL; |
501 |
|
photonPartition [emap.src -> so -> otype] (&emap); |
502 |
< |
|
503 |
< |
if (photonRepTime && !proc) { |
502 |
> |
|
503 |
> |
if (verbose && !proc) { |
504 |
> |
/* Output from subproc 0 only to avoid race condition |
505 |
> |
* on console I/O */ |
506 |
|
if (!passCnt) |
507 |
< |
sprintf(errmsg, "PREPASS %d on source %s (mod %s) ", |
508 |
< |
prePassCnt, source [srcIdx].so -> oname, |
475 |
< |
objptr(source[srcIdx].so->omod) -> oname); |
507 |
> |
sprintf(errmsg, "\tPREPASS %d on source %s ", |
508 |
> |
prePassCnt, source [srcIdx].so -> oname); |
509 |
|
else |
510 |
< |
sprintf(errmsg, "MAIN PASS on source %s (mod %s) ", |
511 |
< |
source [srcIdx].so -> oname, |
512 |
< |
objptr(source[srcIdx].so->omod) -> oname); |
480 |
< |
|
510 |
> |
sprintf(errmsg, "\tMAIN PASS on source %s ", |
511 |
> |
source [srcIdx].so -> oname); |
512 |
> |
|
513 |
|
if (emap.port) { |
514 |
|
sprintf(errmsg2, "via port %s ", |
515 |
|
photonPorts [portCnt].so -> oname); |
516 |
|
strcat(errmsg, errmsg2); |
517 |
|
} |
518 |
< |
|
518 |
> |
|
519 |
|
sprintf(errmsg2, "(%lu partitions)\n", |
520 |
|
emap.numPartitions); |
521 |
< |
strcat(errmsg, errmsg2); |
521 |
> |
strcat(errmsg, errmsg2); |
522 |
|
eputs(errmsg); |
523 |
+ |
#if NIX |
524 |
|
fflush(stderr); |
525 |
< |
} |
525 |
> |
#endif |
526 |
> |
} |
527 |
|
|
528 |
|
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
529 |
|
emap.partitionCnt++) { |
530 |
+ |
#ifndef PMAP_SIGUSR |
531 |
|
double partNumEmit; |
532 |
|
unsigned long partEmitCnt; |
533 |
+ |
#endif |
534 |
|
|
535 |
|
/* Get photon origin within current source partishunn |
536 |
|
* and build emission map */ |
549 |
|
partEmitCnt++; |
550 |
|
|
551 |
|
/* Update local and shared global emission counter */ |
516 |
– |
localNumEmitted += partEmitCnt; |
552 |
|
photonCnt [PHOTONCNT_NUMEMIT(srcIdx)] += partEmitCnt; |
553 |
+ |
localNumEmitted += partEmitCnt; |
554 |
|
|
555 |
< |
/* Integer counter avoids FP rounding errors */ |
555 |
> |
/* Integer counter avoids FP rounding errors during |
556 |
> |
* iteration */ |
557 |
|
while (partEmitCnt--) { |
558 |
|
RAY photonRay; |
559 |
|
|
562 |
|
* until absorbed/leaked; emitPhoton() sets the |
563 |
|
* emitting light source index in photonRay */ |
564 |
|
emitPhoton(&emap, &photonRay); |
565 |
+ |
#if 1 |
566 |
+ |
if (emap.port) |
567 |
+ |
/* !!! PHOTON PORT REJECTION SAMPLING HACK: set |
568 |
+ |
* !!! photon port as fake hit object for |
569 |
+ |
* !!! primary ray to check for intersection in |
570 |
+ |
* !!! tracePhoton() */ |
571 |
+ |
photonRay.ro = emap.port -> so; |
572 |
+ |
#endif |
573 |
|
newPhotonPrimary(pm, &photonRay, primaryHeap[proc]); |
574 |
|
/* Set subprocess index in photonRay for post- |
575 |
|
* distrib primary index linearisation; this is |
583 |
|
photonCnt [PHOTONCNT_NUMPHOT] += pm -> numPhotons - |
584 |
|
lastNumPhotons; |
585 |
|
lastNumPhotons = pm -> numPhotons; |
586 |
+ |
#if !NIX |
587 |
+ |
/* Synchronous progress report on Windoze */ |
588 |
+ |
if (!proc && photonRepTime > 0 && |
589 |
+ |
time(NULL) >= repLastTime + photonRepTime) { |
590 |
+ |
unsigned s; |
591 |
+ |
repComplete = pm -> distribTarget * numProc; |
592 |
+ |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
593 |
+ |
|
594 |
+ |
for (repEmitted = 0, s = 0; s < nsources; s++) |
595 |
+ |
repEmitted += photonCnt [PHOTONCNT_NUMEMIT(s)]; |
596 |
+ |
|
597 |
+ |
pmapDistribReport(); |
598 |
+ |
} |
599 |
+ |
#endif |
600 |
|
} |
601 |
|
|
602 |
|
portCnt++; |
603 |
|
} while (portCnt < numPhotonPorts); |
604 |
|
|
605 |
< |
if (pm -> numPhotons == srcNumDistrib) |
605 |
> |
if (pm -> numPhotons == srcNumDistrib) { |
606 |
|
/* Double predistrib factor in case no photons were stored |
607 |
|
* for this source and redo pass 1 */ |
608 |
|
srcPreDistrib *= 2; |
609 |
+ |
} |
610 |
|
else { |
611 |
|
/* Now do pass 2 */ |
612 |
|
passCnt++; |
553 |
– |
/* if (photonRepTime) |
554 |
– |
eputs("\n"); */ |
613 |
|
} |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
|
/* Flush heap buffa one final time to prevent data corruption */ |
618 |
< |
flushPhotonHeap(pm); |
561 |
< |
fclose(pm -> heap); |
562 |
< |
|
618 |
> |
flushPhotonHeap(pm); |
619 |
|
/* Flush final photon primary to primary heap file */ |
620 |
|
newPhotonPrimary(pm, NULL, primaryHeap [proc]); |
621 |
< |
fclose(primaryHeap [proc]); |
621 |
> |
/* Heap files closed automatically on exit |
622 |
> |
fclose(pm -> heap); |
623 |
> |
fclose(primaryHeap [proc]); */ |
624 |
|
|
625 |
|
#ifdef DEBUG_PMAP |
626 |
< |
sprintf(errmsg, "Proc %d exited with total %ld photons\n", proc, |
626 |
> |
sprintf(errmsg, "Proc %d total %ld photons\n", proc, |
627 |
|
pm -> numPhotons); |
628 |
|
eputs(errmsg); |
629 |
+ |
fflush(stderr); |
630 |
|
#endif |
631 |
|
|
632 |
+ |
#ifdef PMAP_SIGUSR |
633 |
+ |
signal(SIGUSR1, SIG_DFL); |
634 |
+ |
#endif |
635 |
+ |
|
636 |
+ |
#if NIX |
637 |
+ |
/* Terminate subprocess */ |
638 |
|
exit(0); |
639 |
+ |
#endif |
640 |
|
} |
641 |
|
else if (pid < 0) |
642 |
|
error(SYSTEM, "failed to fork subprocess in distribPhotonContrib"); |
643 |
|
} |
644 |
|
|
645 |
+ |
#if NIX |
646 |
|
/* PARENT PROCESS CONTINUES HERE */ |
580 |
– |
/* Record start time and enable progress report signal handler */ |
581 |
– |
repStartTime = time(NULL); |
647 |
|
#ifdef SIGCONT |
648 |
+ |
/* Enable progress report signal handler */ |
649 |
|
signal(SIGCONT, pmapDistribReport); |
650 |
|
#endif |
585 |
– |
/* |
586 |
– |
if (photonRepTime) |
587 |
– |
eputs("\n"); */ |
588 |
– |
|
651 |
|
/* Wait for subprocesses to complete while reporting progress */ |
652 |
|
proc = numProc; |
653 |
|
while (proc) { |
661 |
|
|
662 |
|
/* Nod off for a bit and update progress */ |
663 |
|
sleep(1); |
664 |
< |
|
665 |
< |
/* Update progress report from shared subprocess counters */ |
664 |
> |
|
665 |
> |
/* Asynchronous progress report from shared subprocess counters */ |
666 |
|
repComplete = pm -> distribTarget * numProc; |
667 |
< |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
667 |
> |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
668 |
> |
|
669 |
|
for (repEmitted = 0, srcIdx = 0; srcIdx < nsources; srcIdx++) |
670 |
|
repEmitted += photonCnt [PHOTONCNT_NUMEMIT(srcIdx)]; |
671 |
|
|
678 |
|
else signal(SIGCONT, pmapDistribReport); |
679 |
|
#endif |
680 |
|
} |
681 |
+ |
#endif /* NIX */ |
682 |
|
|
683 |
|
/* ================================================================ |
684 |
|
* POST-DISTRIBUTION - Set photon flux and build kd-tree, etc. |
685 |
|
* ================================================================ */ |
686 |
|
#ifdef SIGCONT |
687 |
+ |
/* Reset signal handler */ |
688 |
|
signal(SIGCONT, SIG_DFL); |
689 |
|
#endif |
690 |
|
free(emap.samples); |
691 |
|
|
692 |
|
if (!pm -> numPhotons) |
693 |
< |
error(USER, "empty photon map"); |
693 |
> |
error(USER, "empty contribution photon map"); |
694 |
|
|
695 |
|
/* Load per-subprocess primary rays into pm -> primary array */ |
696 |
< |
pm -> numPrimary = buildPrimaries(pm, primaryHeap, primaryOfs, numProc); |
696 |
> |
/* Dumb compilers apparently need the char** cast */ |
697 |
> |
pm -> numPrimary = buildPrimaries(pm, primaryHeap, |
698 |
> |
(char**)primaryHeapFname, |
699 |
> |
primaryOfs, numProc); |
700 |
|
if (!pm -> numPrimary) |
701 |
|
error(INTERNAL, "no primary rays in contribution photon map"); |
702 |
|
|
703 |
|
/* Set photon flux per source */ |
704 |
|
for (srcIdx = 0; srcIdx < nsources; srcIdx++) |
705 |
|
srcFlux [srcIdx] /= photonCnt [PHOTONCNT_NUMEMIT(srcIdx)]; |
706 |
< |
|
706 |
> |
#if NIX |
707 |
|
/* Photon counters no longer needed, unmap shared memory */ |
708 |
|
munmap(photonCnt, sizeof(*photonCnt)); |
709 |
|
close(shmFile); |
642 |
– |
#if 0 |
643 |
– |
shm_unlink(shmFname); |
644 |
– |
#else |
710 |
|
unlink(shmFname); |
711 |
+ |
#else |
712 |
+ |
free(photonCnt); |
713 |
|
#endif |
714 |
|
|
715 |
< |
if (photonRepTime) { |
716 |
< |
eputs("\nBuilding contrib photon map...\n"); |
715 |
> |
if (verbose) { |
716 |
> |
eputs("\nBuilding contribution photon map...\n"); |
717 |
> |
#if NIX |
718 |
|
fflush(stderr); |
719 |
+ |
#endif |
720 |
|
} |
721 |
|
|
722 |
|
/* Build underlying data structure; heap is destroyed */ |
723 |
< |
buildPhotonMap(pm, srcFlux, primaryOfs, numProc); |
655 |
< |
} |
656 |
< |
|
657 |
< |
|
658 |
< |
|
659 |
< |
void photonContrib (PhotonMap *pmap, RAY *ray, COLOR irrad) |
660 |
< |
/* Sum up light source contributions from photons in pmap->srcContrib */ |
661 |
< |
{ |
662 |
< |
unsigned i; |
663 |
< |
PhotonSearchQueueNode *sqn; |
664 |
< |
float r, invArea; |
665 |
< |
RREAL rayCoeff [3]; |
666 |
< |
Photon *photon; |
667 |
< |
static char warn = 1; |
668 |
< |
|
669 |
< |
setcolor(irrad, 0, 0, 0); |
670 |
< |
|
671 |
< |
if (!pmap -> maxGather) |
672 |
< |
return; |
673 |
< |
|
674 |
< |
/* Ignore sources */ |
675 |
< |
if (ray -> ro && islight(objptr(ray -> ro -> omod) -> otype)) |
676 |
< |
return; |
677 |
< |
|
678 |
< |
/* Get cumulative path coefficient up to photon lookup point */ |
679 |
< |
raycontrib(rayCoeff, ray, PRIMARY); |
680 |
< |
|
681 |
< |
/* Lookup photons */ |
682 |
< |
pmap -> squeue.tail = 0; |
683 |
< |
findPhotons(pmap, ray); |
723 |
> |
buildPhotonMap(pm, srcFlux, primaryOfs, numProc); |
724 |
|
|
725 |
< |
/* Need at least 2 photons */ |
726 |
< |
if (pmap -> squeue.tail < 2) { |
727 |
< |
#ifdef PMAP_NONEFOUND |
688 |
< |
sprintf(errmsg, "no photons found on %s at (%.3f, %.3f, %.3f)", |
689 |
< |
ray -> ro ? ray -> ro -> oname : "<null>", |
690 |
< |
ray -> rop [0], ray -> rop [1], ray -> rop [2]); |
691 |
< |
error(WARNING, errmsg); |
692 |
< |
#endif |
725 |
> |
/* Free per-subprocess primary heap files */ |
726 |
> |
for (proc = 0; proc < numProc; proc++) |
727 |
> |
free(primaryHeapFname [proc]); |
728 |
|
|
729 |
< |
return; |
730 |
< |
} |
731 |
< |
|
697 |
< |
/* Average (squared) radius between furthest two photons to improve |
698 |
< |
* accuracy and get inverse search area 1 / (PI * r^2), with extra |
699 |
< |
* normalisation factor 1 / PI for ambient calculation */ |
700 |
< |
sqn = pmap -> squeue.node + 1; |
701 |
< |
r = max(sqn -> dist2, (sqn + 1) -> dist2); |
702 |
< |
r = 0.25 * (pmap -> maxDist2 + r + 2 * sqrt(pmap -> maxDist2 * r)); |
703 |
< |
invArea = 1 / (PI * PI * r); |
729 |
> |
free(primaryHeapFname); |
730 |
> |
free(primaryHeap); |
731 |
> |
free(primaryOfs); |
732 |
|
|
733 |
< |
/* Skip the extra photon */ |
734 |
< |
for (i = 1 ; i < pmap -> squeue.tail; i++, sqn++) { |
707 |
< |
COLOR flux; |
708 |
< |
|
709 |
< |
/* Get photon's contribution to density estimate */ |
710 |
< |
photon = getNearestPhoton(&pmap -> squeue, sqn -> idx); |
711 |
< |
getPhotonFlux(photon, flux); |
712 |
< |
scalecolor(flux, invArea); |
713 |
< |
#ifdef PMAP_EPANECHNIKOV |
714 |
< |
/* Apply Epanechnikov kernel to photon flux based on photon distance */ |
715 |
< |
scalecolor(flux, 2 * (1 - sqn -> dist2 / r)); |
716 |
< |
#endif |
717 |
< |
addcolor(irrad, flux); |
718 |
< |
|
719 |
< |
if (pmap -> srcContrib) { |
720 |
< |
const PhotonPrimary *primary = pmap -> primaries + |
721 |
< |
photon -> primary; |
722 |
< |
const SRCREC *sp = &source [primary -> srcIdx]; |
723 |
< |
OBJREC *srcMod = findmaterial(sp -> so); |
724 |
< |
MODCONT *srcContrib = (MODCONT*)lu_find(pmap -> srcContrib, |
725 |
< |
srcMod -> oname) -> data; |
726 |
< |
double srcBinReal; |
727 |
< |
int srcBin; |
728 |
< |
RAY srcRay; |
729 |
< |
|
730 |
< |
if (!srcContrib) |
731 |
< |
continue; |
732 |
< |
|
733 |
< |
/* Photon's emitting light source has modifier whose contributions |
734 |
< |
* are sought */ |
735 |
< |
if (srcContrib -> binv -> type != NUM) { |
736 |
< |
/* Use intersection function to set shadow ray parameters if |
737 |
< |
* it's not simply a constant */ |
738 |
< |
rayorigin(&srcRay, SHADOW, NULL, NULL); |
739 |
< |
srcRay.rsrc = primary -> srcIdx; |
740 |
< |
#ifdef PMAP_PRIMARYPOS |
741 |
< |
VCOPY(srcRay.rorg, primary -> pos); |
742 |
< |
#else |
743 |
< |
/* No primary hitpoints; set dummy ray origin and warn once */ |
744 |
< |
srcRay.rorg [0] = srcRay.rorg [1] = srcRay.rorg [2] = 0; |
745 |
< |
if (warn) { |
746 |
< |
error(WARNING, "no photon primary hitpoints for bin evaluation;" |
747 |
< |
" using dummy (0,0,0) !"); |
748 |
< |
warn = 0; |
749 |
< |
} |
750 |
< |
#endif |
751 |
< |
decodedir(srcRay.rdir, primary -> dir); |
752 |
< |
|
753 |
< |
if (!(sp->sflags & SDISTANT |
754 |
< |
? sourcehit(&srcRay) |
755 |
< |
: (*ofun[sp -> so -> otype].funp)(sp -> so, &srcRay))) |
756 |
< |
continue; /* XXX shouldn't happen! */ |
757 |
< |
|
758 |
< |
worldfunc(RCCONTEXT, &srcRay); |
759 |
< |
set_eparams((char *)srcContrib -> params); |
760 |
< |
} |
761 |
< |
|
762 |
< |
if ((srcBinReal = evalue(srcContrib -> binv)) < -.5) |
763 |
< |
continue; /* silently ignore negative bins */ |
764 |
< |
|
765 |
< |
if ((srcBin = srcBinReal + .5) >= srcContrib -> nbins) { |
766 |
< |
error(WARNING, "bad bin number (ignored)"); |
767 |
< |
continue; |
768 |
< |
} |
769 |
< |
|
770 |
< |
if (!contrib) { |
771 |
< |
/* Ray coefficient mode; normalise by light source radiance |
772 |
< |
* after applying distrib pattern */ |
773 |
< |
int j; |
774 |
< |
|
775 |
< |
raytexture(ray, srcMod -> omod); |
776 |
< |
setcolor(ray -> rcol, srcMod -> oargs.farg [0], |
777 |
< |
srcMod -> oargs.farg [1], srcMod -> oargs.farg [2]); |
778 |
< |
multcolor(ray -> rcol, ray -> pcol); |
779 |
< |
for (j = 0; j < 3; j++) |
780 |
< |
flux [j] = ray -> rcol [j] ? flux [j] / ray -> rcol [j] : 0; |
781 |
< |
} |
782 |
< |
|
783 |
< |
multcolor(flux, rayCoeff); |
784 |
< |
addcolor(srcContrib -> cbin [srcBin], flux); |
785 |
< |
} |
786 |
< |
} |
787 |
< |
|
788 |
< |
return; |
733 |
> |
if (verbose) |
734 |
> |
eputs("\n"); |
735 |
|
} |