| 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; |
| 175 |
|
void distribPhotonContrib (PhotonMap* pm, unsigned numProc) |
| 176 |
|
{ |
| 177 |
|
EmissionMap emap; |
| 178 |
< |
char errmsg2 [128], shmFname [255]; |
| 178 |
> |
char errmsg2 [128], shmFname [PMAP_TMPFNLEN]; |
| 179 |
|
unsigned srcIdx, proc; |
| 180 |
|
int shmFile, stat, pid; |
| 181 |
|
double *srcFlux, /* Emitted flux per light source */ |
| 182 |
|
srcDistribTarget; /* Target photon count per source */ |
| 183 |
|
PhotonContribCnt *photonCnt; /* Photon emission counter array */ |
| 184 |
< |
const unsigned photonCntSize = sizeof(PhotonContribCnt) * |
| 184 |
> |
unsigned photonCntSize = sizeof(PhotonContribCnt) * |
| 185 |
|
PHOTONCNT_NUMEMIT(nsources); |
| 186 |
< |
FILE *primaryHeap [numProc]; |
| 187 |
< |
PhotonPrimaryIdx primaryOfs [numProc]; |
| 186 |
> |
FILE **primaryHeap = NULL; |
| 187 |
> |
char **primaryHeapFname = NULL; |
| 188 |
> |
PhotonPrimaryIdx *primaryOfs = NULL; |
| 189 |
|
|
| 190 |
|
if (!pm) |
| 191 |
|
error(USER, "no photon map defined in distribPhotonContrib"); |
| 222 |
|
|
| 223 |
|
/* Per-subprocess / per-source target counts */ |
| 224 |
|
pm -> distribTarget /= numProc; |
| 225 |
< |
srcDistribTarget = nsources ? (double)pm -> distribTarget / nsources : 0; |
| 225 |
> |
srcDistribTarget = nsources ? (double)pm -> distribTarget / nsources : 0; |
| 226 |
|
|
| 227 |
+ |
if (!pm -> distribTarget) |
| 228 |
+ |
error(INTERNAL, "no photons to distribute in distribPhotonContrib"); |
| 229 |
+ |
|
| 230 |
|
/* Get photon ports if specified */ |
| 231 |
|
if (ambincl == 1) |
| 232 |
|
getPhotonPorts(); |
| 233 |
|
|
| 234 |
|
/* Get photon sensor modifiers */ |
| 235 |
|
getPhotonSensors(photonSensorList); |
| 236 |
< |
|
| 236 |
> |
|
| 237 |
> |
#if NIX |
| 238 |
|
/* Set up shared mem for photon counters (zeroed by ftruncate) */ |
| 239 |
< |
#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); |
| 239 |
> |
strcpy(shmFname, PMAP_TMPFNAME); |
| 240 |
|
shmFile = mkstemp(shmFname); |
| 304 |
– |
#endif |
| 241 |
|
|
| 242 |
|
if (shmFile < 0 || ftruncate(shmFile, photonCntSize) < 0) |
| 243 |
|
error(SYSTEM, "failed shared mem init in distribPhotonContrib"); |
| 247 |
|
|
| 248 |
|
if (photonCnt == MAP_FAILED) |
| 249 |
|
error(SYSTEM, "failed shared mem mapping in distribPhotonContrib"); |
| 250 |
+ |
#else |
| 251 |
+ |
/* Allocate photon counters statically on Windoze */ |
| 252 |
+ |
if (!(photonCnt = malloc(photonCntSize))) |
| 253 |
+ |
error(SYSTEM, "failed trivial malloc in distribPhotonContrib"); |
| 254 |
+ |
|
| 255 |
+ |
for (srcIdx = 0; srcIdx < PHOTONCNT_NUMEMIT(nsources); srcIdx++) |
| 256 |
+ |
photonCnt [srcIdx] = 0; |
| 257 |
+ |
#endif /* NIX */ |
| 258 |
|
|
| 259 |
+ |
if (verbose) { |
| 260 |
+ |
sprintf(errmsg, "\nIntegrating flux from %d sources", nsources); |
| 261 |
+ |
|
| 262 |
+ |
if (photonPorts) { |
| 263 |
+ |
sprintf(errmsg2, " via %d ports", numPhotonPorts); |
| 264 |
+ |
strcat(errmsg, errmsg2); |
| 265 |
+ |
} |
| 266 |
+ |
|
| 267 |
+ |
strcat(errmsg, "\n"); |
| 268 |
+ |
eputs(errmsg); |
| 269 |
+ |
} |
| 270 |
+ |
|
| 271 |
|
/* ============================================================= |
| 272 |
< |
* FLUX INTEGRATION - Get total flux emitted from light source |
| 272 |
> |
* FLUX INTEGRATION - Get total flux emitted from sources/ports |
| 273 |
|
* ============================================================= */ |
| 274 |
|
for (srcIdx = 0; srcIdx < nsources; srcIdx++) { |
| 275 |
< |
unsigned portCnt = 0; |
| 320 |
< |
|
| 275 |
> |
unsigned portCnt = 0; |
| 276 |
|
srcFlux [srcIdx] = 0; |
| 277 |
|
emap.src = source + srcIdx; |
| 278 |
|
|
| 324 |
– |
if (photonRepTime) |
| 325 |
– |
eputs("\n"); |
| 326 |
– |
|
| 279 |
|
do { /* Need at least one iteration if no ports! */ |
| 280 |
|
emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt |
| 281 |
|
: NULL; |
| 282 |
|
photonPartition [emap.src -> so -> otype] (&emap); |
| 283 |
< |
|
| 284 |
< |
if (photonRepTime) { |
| 285 |
< |
sprintf(errmsg, "Integrating flux from source %s (mod %s) ", |
| 286 |
< |
source [srcIdx].so -> oname, |
| 287 |
< |
objptr(source [srcIdx].so -> omod) -> oname); |
| 336 |
< |
|
| 283 |
> |
|
| 284 |
> |
if (verbose) { |
| 285 |
> |
sprintf(errmsg, "\tIntegrating flux from source %s ", |
| 286 |
> |
source [srcIdx].so -> oname); |
| 287 |
> |
|
| 288 |
|
if (emap.port) { |
| 289 |
|
sprintf(errmsg2, "via port %s ", |
| 290 |
|
photonPorts [portCnt].so -> oname); |
| 291 |
|
strcat(errmsg, errmsg2); |
| 292 |
|
} |
| 293 |
< |
|
| 294 |
< |
sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions); |
| 293 |
> |
|
| 294 |
> |
sprintf(errmsg2, "(%lu partitions)\n", emap.numPartitions); |
| 295 |
|
strcat(errmsg, errmsg2); |
| 296 |
|
eputs(errmsg); |
| 297 |
+ |
#if NIX |
| 298 |
|
fflush(stderr); |
| 299 |
< |
} |
| 299 |
> |
#endif |
| 300 |
> |
} |
| 301 |
|
|
| 302 |
|
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
| 303 |
|
emap.partitionCnt++) { |
| 314 |
|
error(WARNING, errmsg); |
| 315 |
|
} |
| 316 |
|
} |
| 317 |
< |
|
| 318 |
< |
if (photonRepTime) |
| 319 |
< |
eputs("\n"); |
| 320 |
< |
|
| 321 |
< |
/* Init per-subprocess primary heap files */ |
| 322 |
< |
for (proc = 0; proc < numProc; proc++) |
| 323 |
< |
if (!(primaryHeap [proc] = tmpfile())) |
| 317 |
> |
|
| 318 |
> |
/* Allocate & init per-subprocess primary heap files */ |
| 319 |
> |
primaryHeap = calloc(numProc, sizeof(FILE*)); |
| 320 |
> |
primaryHeapFname = calloc(numProc, sizeof(char*)); |
| 321 |
> |
primaryOfs = calloc(numProc, sizeof(PhotonPrimaryIdx)); |
| 322 |
> |
if (!primaryHeap || !primaryHeapFname || !primaryOfs) |
| 323 |
> |
error(SYSTEM, "failed primary heap allocation in " |
| 324 |
> |
"distribPhotonContrib"); |
| 325 |
> |
|
| 326 |
> |
for (proc = 0; proc < numProc; proc++) { |
| 327 |
> |
primaryHeapFname [proc] = malloc(PMAP_TMPFNLEN); |
| 328 |
> |
if (!primaryHeapFname [proc]) |
| 329 |
> |
error(SYSTEM, "failed primary heap file allocation in " |
| 330 |
> |
"distribPhotonContrib"); |
| 331 |
> |
|
| 332 |
> |
mktemp(strcpy(primaryHeapFname [proc], PMAP_TMPFNAME)); |
| 333 |
> |
if (!(primaryHeap [proc] = fopen(primaryHeapFname [proc], "w+b"))) |
| 334 |
|
error(SYSTEM, "failed opening primary heap file in " |
| 335 |
|
"distribPhotonContrib"); |
| 336 |
+ |
} |
| 337 |
+ |
|
| 338 |
+ |
/* Record start time for progress reports */ |
| 339 |
+ |
repStartTime = time(NULL); |
| 340 |
+ |
|
| 341 |
+ |
if (verbose) { |
| 342 |
+ |
sprintf(errmsg, "\nPhoton distribution @ %d procs\n", numProc); |
| 343 |
+ |
eputs(errmsg); |
| 344 |
+ |
} |
| 345 |
|
|
| 346 |
|
/* MAIN LOOP */ |
| 347 |
|
for (proc = 0; proc < numProc; proc++) { |
| 348 |
+ |
#if NIX |
| 349 |
|
if (!(pid = fork())) { |
| 350 |
< |
/* SUBPROCESS ENTERS HERE; |
| 351 |
< |
* all opened and memory mapped files are inherited */ |
| 352 |
< |
|
| 350 |
> |
/* SUBPROCESS ENTERS HERE; opened and mmapped files inherited */ |
| 351 |
> |
#else |
| 352 |
> |
if (1) { |
| 353 |
> |
/* No subprocess under Windoze */ |
| 354 |
> |
#endif |
| 355 |
|
/* Local photon counters for this subprocess */ |
| 356 |
|
unsigned long lastNumPhotons = 0, localNumEmitted = 0; |
| 357 |
< |
double photonFluxSum = 0; /* Running photon flux sum */ |
| 357 |
> |
double photonFluxSum = 0; /* Accum. photon flux */ |
| 358 |
|
|
| 359 |
|
/* Seed RNGs from PID for decorellated photon distribution */ |
| 360 |
|
pmapSeed(randSeed + proc, partState); |
| 383 |
|
while (passCnt < 2) { |
| 384 |
|
if (!passCnt) { |
| 385 |
|
/* INIT PASS 1 */ |
| 386 |
< |
if (++prePassCnt > maxPreDistrib) { |
| 386 |
> |
if (++prePassCnt > maxPreDistrib && !proc) { |
| 387 |
|
/* Warn if no photons contributed after sufficient |
| 388 |
< |
* iterations */ |
| 389 |
< |
sprintf(errmsg, "proc %d, source %s: " |
| 390 |
< |
"too many prepasses, skipped", |
| 391 |
< |
proc, source [srcIdx].so -> oname); |
| 388 |
> |
* iterations; only output from subprocess 0 to reduce |
| 389 |
> |
* console clutter */ |
| 390 |
> |
sprintf(errmsg, |
| 391 |
> |
"source %s: too many prepasses, skipped", |
| 392 |
> |
source [srcIdx].so -> oname); |
| 393 |
|
error(WARNING, errmsg); |
| 394 |
|
break; |
| 395 |
|
} |
| 424 |
|
if (avgPhotonFlux > 0 && |
| 425 |
|
srcPhotonFlux / avgPhotonFlux < FTINY) { |
| 426 |
|
/* Skip source if its photon flux is grossly below the |
| 427 |
< |
* running average, indicating negligible contribs at |
| 428 |
< |
* the expense of excessive distribution time */ |
| 429 |
< |
sprintf(errmsg, "proc %d, source %s: " |
| 430 |
< |
"itsy bitsy photon flux, skipped", |
| 431 |
< |
proc, source [srcIdx].so -> oname); |
| 427 |
> |
* running average, indicating negligible contributions |
| 428 |
> |
* at the expense of excessive distribution time; only |
| 429 |
> |
* output from subproc 0 to reduce console clutter */ |
| 430 |
> |
sprintf(errmsg, |
| 431 |
> |
"source %s: itsy bitsy photon flux, skipped", |
| 432 |
> |
source [srcIdx].so -> oname); |
| 433 |
|
error(WARNING, errmsg); |
| 434 |
|
srcNumEmit = 0; |
| 435 |
|
} |
| 437 |
|
/* Update sum of photon flux per light source */ |
| 438 |
|
photonFluxSum += srcPhotonFlux; |
| 439 |
|
} |
| 440 |
< |
|
| 440 |
> |
|
| 441 |
|
portCnt = 0; |
| 442 |
|
do { /* Need at least one iteration if no ports! */ |
| 443 |
|
emap.src = source + srcIdx; |
| 444 |
|
emap.port = emap.src -> sflags & SDISTANT |
| 445 |
|
? photonPorts + portCnt : NULL; |
| 446 |
|
photonPartition [emap.src -> so -> otype] (&emap); |
| 447 |
< |
|
| 448 |
< |
if (photonRepTime && !proc) { |
| 447 |
> |
|
| 448 |
> |
if (verbose && !proc) { |
| 449 |
> |
/* Output from subproc 0 only to avoid race condition |
| 450 |
> |
* on console I/O */ |
| 451 |
|
if (!passCnt) |
| 452 |
< |
sprintf(errmsg, "PREPASS %d on source %s (mod %s) ", |
| 453 |
< |
prePassCnt, source [srcIdx].so -> oname, |
| 475 |
< |
objptr(source[srcIdx].so->omod) -> oname); |
| 452 |
> |
sprintf(errmsg, "\tPREPASS %d on source %s ", |
| 453 |
> |
prePassCnt, source [srcIdx].so -> oname); |
| 454 |
|
else |
| 455 |
< |
sprintf(errmsg, "MAIN PASS on source %s (mod %s) ", |
| 456 |
< |
source [srcIdx].so -> oname, |
| 457 |
< |
objptr(source[srcIdx].so->omod) -> oname); |
| 480 |
< |
|
| 455 |
> |
sprintf(errmsg, "\tMAIN PASS on source %s ", |
| 456 |
> |
source [srcIdx].so -> oname); |
| 457 |
> |
|
| 458 |
|
if (emap.port) { |
| 459 |
|
sprintf(errmsg2, "via port %s ", |
| 460 |
|
photonPorts [portCnt].so -> oname); |
| 461 |
|
strcat(errmsg, errmsg2); |
| 462 |
|
} |
| 463 |
< |
|
| 463 |
> |
|
| 464 |
|
sprintf(errmsg2, "(%lu partitions)\n", |
| 465 |
|
emap.numPartitions); |
| 466 |
< |
strcat(errmsg, errmsg2); |
| 466 |
> |
strcat(errmsg, errmsg2); |
| 467 |
|
eputs(errmsg); |
| 468 |
+ |
#if NIX |
| 469 |
|
fflush(stderr); |
| 470 |
< |
} |
| 470 |
> |
#endif |
| 471 |
> |
} |
| 472 |
|
|
| 473 |
|
for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions; |
| 474 |
|
emap.partitionCnt++) { |
| 492 |
|
partEmitCnt++; |
| 493 |
|
|
| 494 |
|
/* Update local and shared global emission counter */ |
| 516 |
– |
localNumEmitted += partEmitCnt; |
| 495 |
|
photonCnt [PHOTONCNT_NUMEMIT(srcIdx)] += partEmitCnt; |
| 496 |
+ |
localNumEmitted += partEmitCnt; |
| 497 |
|
|
| 498 |
< |
/* Integer counter avoids FP rounding errors */ |
| 498 |
> |
/* Integer counter avoids FP rounding errors during |
| 499 |
> |
* iteration */ |
| 500 |
|
while (partEmitCnt--) { |
| 501 |
|
RAY photonRay; |
| 502 |
|
|
| 505 |
|
* until absorbed/leaked; emitPhoton() sets the |
| 506 |
|
* emitting light source index in photonRay */ |
| 507 |
|
emitPhoton(&emap, &photonRay); |
| 508 |
+ |
#if 1 |
| 509 |
+ |
if (emap.port) |
| 510 |
+ |
/* !!! PHOTON PORT REJECTION SAMPLING HACK: set |
| 511 |
+ |
* !!! photon port as fake hit object for |
| 512 |
+ |
* !!! primary ray to check for intersection in |
| 513 |
+ |
* !!! tracePhoton() */ |
| 514 |
+ |
photonRay.ro = emap.port -> so; |
| 515 |
+ |
#endif |
| 516 |
|
newPhotonPrimary(pm, &photonRay, primaryHeap[proc]); |
| 517 |
|
/* Set subprocess index in photonRay for post- |
| 518 |
|
* distrib primary index linearisation; this is |
| 526 |
|
photonCnt [PHOTONCNT_NUMPHOT] += pm -> numPhotons - |
| 527 |
|
lastNumPhotons; |
| 528 |
|
lastNumPhotons = pm -> numPhotons; |
| 529 |
+ |
#if !NIX |
| 530 |
+ |
/* Synchronous progress report on Windoze */ |
| 531 |
+ |
if (!proc && photonRepTime > 0 && |
| 532 |
+ |
time(NULL) >= repLastTime + photonRepTime) { |
| 533 |
+ |
unsigned s; |
| 534 |
+ |
repComplete = pm -> distribTarget * numProc; |
| 535 |
+ |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
| 536 |
+ |
|
| 537 |
+ |
for (repEmitted = 0, s = 0; s < nsources; s++) |
| 538 |
+ |
repEmitted += photonCnt [PHOTONCNT_NUMEMIT(s)]; |
| 539 |
+ |
|
| 540 |
+ |
pmapDistribReport(); |
| 541 |
+ |
} |
| 542 |
+ |
#endif |
| 543 |
|
} |
| 544 |
|
|
| 545 |
|
portCnt++; |
| 546 |
|
} while (portCnt < numPhotonPorts); |
| 547 |
|
|
| 548 |
< |
if (pm -> numPhotons == srcNumDistrib) |
| 548 |
> |
if (pm -> numPhotons == srcNumDistrib) { |
| 549 |
|
/* Double predistrib factor in case no photons were stored |
| 550 |
|
* for this source and redo pass 1 */ |
| 551 |
|
srcPreDistrib *= 2; |
| 552 |
+ |
} |
| 553 |
|
else { |
| 554 |
|
/* Now do pass 2 */ |
| 555 |
|
passCnt++; |
| 553 |
– |
/* if (photonRepTime) |
| 554 |
– |
eputs("\n"); */ |
| 556 |
|
} |
| 557 |
|
} |
| 558 |
|
} |
| 559 |
|
|
| 560 |
|
/* Flush heap buffa one final time to prevent data corruption */ |
| 561 |
< |
flushPhotonHeap(pm); |
| 561 |
< |
fclose(pm -> heap); |
| 562 |
< |
|
| 561 |
> |
flushPhotonHeap(pm); |
| 562 |
|
/* Flush final photon primary to primary heap file */ |
| 563 |
|
newPhotonPrimary(pm, NULL, primaryHeap [proc]); |
| 564 |
< |
fclose(primaryHeap [proc]); |
| 564 |
> |
/* Heap files closed automatically on exit |
| 565 |
> |
fclose(pm -> heap); |
| 566 |
> |
fclose(primaryHeap [proc]); */ |
| 567 |
|
|
| 568 |
|
#ifdef DEBUG_PMAP |
| 569 |
< |
sprintf(errmsg, "Proc %d exited with total %ld photons\n", proc, |
| 569 |
> |
sprintf(errmsg, "Proc %d total %ld photons\n", proc, |
| 570 |
|
pm -> numPhotons); |
| 571 |
|
eputs(errmsg); |
| 572 |
+ |
fflush(stderr); |
| 573 |
|
#endif |
| 574 |
|
|
| 575 |
+ |
#if NIX |
| 576 |
+ |
/* Terminate subprocess */ |
| 577 |
|
exit(0); |
| 578 |
+ |
#endif |
| 579 |
|
} |
| 580 |
|
else if (pid < 0) |
| 581 |
|
error(SYSTEM, "failed to fork subprocess in distribPhotonContrib"); |
| 582 |
|
} |
| 583 |
|
|
| 584 |
+ |
#if NIX |
| 585 |
|
/* PARENT PROCESS CONTINUES HERE */ |
| 580 |
– |
/* Record start time and enable progress report signal handler */ |
| 581 |
– |
repStartTime = time(NULL); |
| 586 |
|
#ifdef SIGCONT |
| 587 |
+ |
/* Enable progress report signal handler */ |
| 588 |
|
signal(SIGCONT, pmapDistribReport); |
| 589 |
|
#endif |
| 585 |
– |
/* |
| 586 |
– |
if (photonRepTime) |
| 587 |
– |
eputs("\n"); */ |
| 588 |
– |
|
| 590 |
|
/* Wait for subprocesses to complete while reporting progress */ |
| 591 |
|
proc = numProc; |
| 592 |
|
while (proc) { |
| 600 |
|
|
| 601 |
|
/* Nod off for a bit and update progress */ |
| 602 |
|
sleep(1); |
| 603 |
< |
|
| 604 |
< |
/* Update progress report from shared subprocess counters */ |
| 603 |
> |
|
| 604 |
> |
/* Asynchronous progress report from shared subprocess counters */ |
| 605 |
|
repComplete = pm -> distribTarget * numProc; |
| 606 |
< |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
| 606 |
> |
repProgress = photonCnt [PHOTONCNT_NUMPHOT]; |
| 607 |
> |
|
| 608 |
|
for (repEmitted = 0, srcIdx = 0; srcIdx < nsources; srcIdx++) |
| 609 |
|
repEmitted += photonCnt [PHOTONCNT_NUMEMIT(srcIdx)]; |
| 610 |
|
|
| 617 |
|
else signal(SIGCONT, pmapDistribReport); |
| 618 |
|
#endif |
| 619 |
|
} |
| 620 |
+ |
#endif /* NIX */ |
| 621 |
|
|
| 622 |
|
/* ================================================================ |
| 623 |
|
* POST-DISTRIBUTION - Set photon flux and build kd-tree, etc. |
| 624 |
|
* ================================================================ */ |
| 625 |
|
#ifdef SIGCONT |
| 626 |
+ |
/* Reset signal handler */ |
| 627 |
|
signal(SIGCONT, SIG_DFL); |
| 628 |
|
#endif |
| 629 |
|
free(emap.samples); |
| 630 |
|
|
| 631 |
|
if (!pm -> numPhotons) |
| 632 |
< |
error(USER, "empty photon map"); |
| 632 |
> |
error(USER, "empty contribution photon map"); |
| 633 |
|
|
| 634 |
|
/* Load per-subprocess primary rays into pm -> primary array */ |
| 635 |
< |
pm -> numPrimary = buildPrimaries(pm, primaryHeap, primaryOfs, numProc); |
| 635 |
> |
/* Dumb compilers apparently need the char** cast */ |
| 636 |
> |
pm -> numPrimary = buildPrimaries(pm, primaryHeap, |
| 637 |
> |
(char**)primaryHeapFname, |
| 638 |
> |
primaryOfs, numProc); |
| 639 |
|
if (!pm -> numPrimary) |
| 640 |
|
error(INTERNAL, "no primary rays in contribution photon map"); |
| 641 |
|
|
| 642 |
|
/* Set photon flux per source */ |
| 643 |
|
for (srcIdx = 0; srcIdx < nsources; srcIdx++) |
| 644 |
|
srcFlux [srcIdx] /= photonCnt [PHOTONCNT_NUMEMIT(srcIdx)]; |
| 645 |
< |
|
| 645 |
> |
#if NIX |
| 646 |
|
/* Photon counters no longer needed, unmap shared memory */ |
| 647 |
|
munmap(photonCnt, sizeof(*photonCnt)); |
| 648 |
|
close(shmFile); |
| 642 |
– |
#if 0 |
| 643 |
– |
shm_unlink(shmFname); |
| 644 |
– |
#else |
| 649 |
|
unlink(shmFname); |
| 650 |
+ |
#else |
| 651 |
+ |
free(photonCnt); |
| 652 |
|
#endif |
| 653 |
|
|
| 654 |
< |
if (photonRepTime) { |
| 655 |
< |
eputs("\nBuilding contrib photon map...\n"); |
| 654 |
> |
if (verbose) { |
| 655 |
> |
eputs("\nBuilding contribution photon map...\n"); |
| 656 |
> |
#if NIX |
| 657 |
|
fflush(stderr); |
| 658 |
+ |
#endif |
| 659 |
|
} |
| 660 |
|
|
| 661 |
|
/* Build underlying data structure; heap is destroyed */ |
| 662 |
< |
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); |
| 662 |
> |
buildPhotonMap(pm, srcFlux, primaryOfs, numProc); |
| 663 |
|
|
| 664 |
< |
/* Need at least 2 photons */ |
| 665 |
< |
if (pmap -> squeue.tail < 2) { |
| 666 |
< |
#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 |
| 664 |
> |
/* Free per-subprocess primary heap files */ |
| 665 |
> |
for (proc = 0; proc < numProc; proc++) |
| 666 |
> |
free(primaryHeapFname [proc]); |
| 667 |
|
|
| 668 |
< |
return; |
| 669 |
< |
} |
| 670 |
< |
|
| 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); |
| 668 |
> |
free(primaryHeapFname); |
| 669 |
> |
free(primaryHeap); |
| 670 |
> |
free(primaryOfs); |
| 671 |
|
|
| 672 |
< |
/* Skip the extra photon */ |
| 673 |
< |
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; |
| 672 |
> |
if (verbose) |
| 673 |
> |
eputs("\n"); |
| 674 |
|
} |