ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmap.c
(Generate patch)

Comparing ray/src/rt/pmap.c (file contents):
Revision 2.3 by rschregle, Wed Apr 22 20:28:16 2015 UTC vs.
Revision 2.13 by rschregle, Mon Aug 14 21:12:10 2017 UTC

# Line 1 | Line 1
1 + #ifndef lint
2 + static const char RCSid[] = "$Id$";
3 + #endif
4 +
5 +
6   /*
7 <   ==================================================================
7 >   ======================================================================
8     Photon map main module
9  
10     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
11     (c) Fraunhofer Institute for Solar Energy Systems,
12 <       Lucerne University of Applied Sciences & Arts  
13 <   ==================================================================
12 >   (c) Lucerne University of Applied Sciences and Arts,
13 >       supported by the Swiss National Science Foundation (SNSF, #147053)
14 >   ======================================================================
15    
16     $Id$
17   */
18  
19  
14
20   #include "pmap.h"
21   #include "pmapmat.h"
22   #include "pmapsrc.h"
# Line 21 | Line 26
26   #include "pmapdiag.h"
27   #include "otypes.h"
28   #include <time.h>
29 < #include <sys/stat.h>
29 > #if NIX
30 >   #include <sys/stat.h>
31 >   #include <sys/mman.h>
32 >   #include <sys/wait.h>
33 > #endif
34  
35  
27
28 extern char *octname;
29
30 static char PmapRevision [] = "$Revision$";
31
32
33
34 /* Photon map lookup functions per type */
35 void (*pmapLookup [NUM_PMAP_TYPES])(PhotonMap*, RAY*, COLOR) = {
36   photonDensity, photonPreCompDensity, photonDensity, volumePhotonDensity,
37   photonDensity, NULL
38 };
39
40
41
42 void colorNorm (COLOR c)
43 /* Normalise colour channels to average of 1 */
44 {
45   const float avg = colorAvg(c);
46  
47   if (!avg)
48      return;
49      
50   c [0] /= avg;
51   c [1] /= avg;
52   c [2] /= avg;
53 }
54
55
56
57 void loadPmaps (PhotonMap **pmaps, const PhotonMapParams *parm)
58 {
59   unsigned t;
60   struct stat octstat, pmstat;
61   PhotonMap *pm;
62   PhotonMapType type;
63  
64   for (t = 0; t < NUM_PMAP_TYPES; t++)
65      if (setPmapParam(&pm, parm + t)) {        
66         /* Check if photon map newer than octree */
67         if (!stat(pm -> fileName, &pmstat) && !stat(octname, &octstat) &&
68             octstat.st_mtime > pmstat.st_mtime) {
69            sprintf(errmsg, "photon map in file %s may be stale",
70                    pm -> fileName);
71            error(USER, errmsg);
72         }
73        
74         /* Load photon map from file and get its type */
75         if ((type = loadPhotonMap(pm, pm -> fileName)) == PMAP_TYPE_NONE)
76            error(USER, "failed loading photon map");
77            
78         /* Assign to appropriate photon map type (deleting previously
79          * loaded photon map of same type if necessary) */
80         if (pmaps [type]) {
81            deletePhotons(pmaps [type]);
82            free(pmaps [type]);
83         }
84         pmaps [type] = pm;
85        
86         /* Check for invalid density estimate bandwidth */                            
87         if (pm -> maxGather > pm -> heapSize) {
88            error(WARNING, "adjusting density estimate bandwidth");
89            pm -> minGather = pm -> maxGather = pm -> heapSize;
90         }
91      }
92 }
93
94
95
36   void savePmaps (const PhotonMap **pmaps, int argc, char **argv)
37   {
38     unsigned t;
39    
40     for (t = 0; t < NUM_PMAP_TYPES; t++) {
41        if (pmaps [t])
42 <         savePhotonMap(pmaps [t], pmaps [t] -> fileName, t, argc, argv);
42 >         savePhotonMap(pmaps [t], pmaps [t] -> fileName, argc, argv);
43     }
44   }                  
45  
46  
107  
108 void cleanUpPmaps (PhotonMap **pmaps)
109 {
110   unsigned t;
111  
112   for (t = 0; t < NUM_PMAP_TYPES; t++) {
113      if (pmaps [t]) {
114         deletePhotons(pmaps [t]);
115         free(pmaps [t]);
116      }
117   }
118 }
119
120
47      
48   static int photonParticipate (RAY *ray)
49   /* Trace photon through participating medium. Returns 1 if passed through,
# Line 143 | Line 69 | static int photonParticipate (RAY *ray)
69        colorNorm(ray -> rcol);
70        VCOPY(ray -> rorg, ray -> rop);
71        
72 <      if (albedo > FTINY)
72 >      if (albedo > FTINY && ray -> rlvl > 0)
73           /* Add to volume photon map */
74 <         if (ray -> rlvl > 0) addPhoton(volumePmap, ray);
74 >         newPhoton(volumePmap, ray);
75          
76        /* Absorbed? */
77 <      if (pmapRandom(rouletteState) > albedo) return 0;
77 >      if (pmapRandom(rouletteState) > albedo)
78 >         return 0;
79        
80        /* Colour bleeding without attenuation (?) */
81        multcolor(ray -> rcol, ray -> albedo);
# Line 201 | Line 128 | void tracePhoton (RAY *ray)
128   /* Follow photon as it bounces around... */
129   {
130     long mod;
131 <   OBJREC* mat;
131 >   OBJREC *mat, *port = NULL;
132 >  
133 >   if (!ray -> parent) {
134 >      /* !!!  PHOTON PORT REJECTION SAMPLING HACK: get photon port for
135 >       * !!!  primary ray from ray -> ro, then reset the latter to NULL so
136 >       * !!!  as not to interfere with localhit() */
137 >      port = ray -> ro;
138 >      ray -> ro = NULL;
139 >   }
140  
141     if (ray -> rlvl > photonMaxBounce) {
142 + #ifdef PMAP_RUNAWAY_WARN  
143        error(WARNING, "runaway photon!");
144 + #endif      
145        return;
146     }
147 <  
147 >  
148     if (colorAvg(ray -> cext) > FTINY && !photonParticipate(ray))
149        return;
150 <      
150 >
151     if (localhit(ray, &thescene)) {
152        mod = ray -> ro -> omod;
153 <      
153 >
154 >      if (port && ray -> ro != port) {
155 >         /* !!! PHOTON PORT REJECTION SAMPLING HACK !!!
156 >          * Terminate photon if emitted from port without intersecting it;
157 >          * this can happen when the port's partitions extend beyond its
158 >          * actual geometry, e.g.  with polygons.  Since the total flux
159 >          * relayed by the port is based on the (in this case) larger
160 >          * partition area, it is overestimated; terminating these photons
161 >          * constitutes rejection sampling and thereby compensates any bias
162 >          * incurred by the overestimated flux.  */
163 > #ifdef PMAP_PORTREJECT_WARN
164 >         sprintf(errmsg, "photon outside port %s", ray -> ro -> oname);
165 >         error(WARNING, errmsg);
166 > #endif        
167 >         return;
168 >      }
169 >
170        if ((ray -> clipset && inset(ray -> clipset, mod)) || mod == OVOID) {
171           /* Transfer ray if modifier is VOID or clipped within antimatta */
172           RAY tray;
# Line 231 | Line 184 | void tracePhoton (RAY *ray)
184  
185  
186   static void preComputeGlobal (PhotonMap *pmap)
187 < /* Precompute irradiance from global photons for final gathering using
188 <   the first finalGather * pmap -> heapSize photons in the heap. Returns
189 <   new heap with precomputed photons. */
187 > /* Precompute irradiance from global photons for final gathering for  
188 >   a random subset of finalGather * pmap -> numPhotons photons, and builds
189 >   the photon map, discarding the original photons. */
190 > /* !!! NOTE: PRECOMPUTATION WITH OOC CURRENTLY WITHOUT CACHE !!! */  
191   {
192 <   unsigned long i, nuHeapSize;
193 <   unsigned j;
194 <   Photon *nuHeap, *p;
195 <   COLOR irrad;
196 <   RAY ray;
197 <   float nuMinPos [3], nuMaxPos [3];
192 >   unsigned long  i, numPreComp;
193 >   unsigned       j;
194 >   PhotonIdx      pIdx;
195 >   Photon         photon;
196 >   RAY            ray;
197 >   PhotonMap      nuPmap;
198  
199 <   repComplete = nuHeapSize = finalGather * pmap -> heapSize;
199 >   repComplete = numPreComp = finalGather * pmap -> numPhotons;
200    
201 <   if (photonRepTime) {
201 >   if (verbose) {
202        sprintf(errmsg,
203 <              "Precomputing irradiance for %ld global photons...\n",
204 <              nuHeapSize);
203 >              "\nPrecomputing irradiance for %ld global photons\n",
204 >              numPreComp);
205        eputs(errmsg);
206 + #if NIX      
207        fflush(stderr);
208 + #endif      
209     }
210    
211 <   p = nuHeap = (Photon*)malloc(nuHeapSize * sizeof(Photon));
212 <   if (!nuHeap)
213 <      error(USER, "can't allocate photon heap");
214 <      
215 <   for (j = 0; j <= 2; j++) {
216 <      nuMinPos [j] = FHUGE;
261 <      nuMaxPos [j] = -FHUGE;
262 <   }
211 >   /* Copy photon map for precomputed photons */
212 >   memcpy(&nuPmap, pmap, sizeof(PhotonMap));
213 >
214 >   /* Zero counters, init new heap and extents */  
215 >   nuPmap.numPhotons = 0;  
216 >   initPhotonHeap(&nuPmap);
217    
218 +   for (j = 0; j < 3; j++) {  
219 +      nuPmap.minPos [j] = FHUGE;
220 +      nuPmap.maxPos [j] = -FHUGE;
221 +   }
222 +
223     /* Record start time, baby */
224     repStartTime = time(NULL);
225 <   #ifdef SIGCONT
226 <      signal(SIGCONT, pmapPreCompReport);
227 <   #endif
225 > #ifdef SIGCONT
226 >   signal(SIGCONT, pmapPreCompReport);
227 > #endif
228     repProgress = 0;
270   memcpy(nuHeap, pmap -> heap, nuHeapSize * sizeof(Photon));
229    
230 <   for (i = 0, p = nuHeap; i < nuHeapSize; i++, p++) {
231 <      ray.ro = NULL;
232 <      VCOPY(ray.rop, p -> pos);
230 >   photonRay(NULL, &ray, PRIMARY, NULL);
231 >   ray.ro = NULL;
232 >  
233 >   for (i = 0; i < numPreComp; i++) {
234 >      /* Get random photon from stratified distribution in source heap to
235 >       * avoid duplicates and clustering */
236 >      pIdx = firstPhoton(pmap) +
237 >             (unsigned long)((i + pmapRandom(pmap -> randState)) /
238 >                             finalGather);
239 >      getPhoton(pmap, pIdx, &photon);
240        
241 <      /* Update min and max positions & set ray normal */
242 <      for (j = 0; j < 3; j++) {
243 <         if (p -> pos [j] < nuMinPos [j]) nuMinPos [j] = p -> pos [j];
244 <         if (p -> pos [j] > nuMaxPos [j]) nuMaxPos [j] = p -> pos [j];
280 <         ray.ron [j] = p -> norm [j] / 127.0;
281 <      }
241 >      /* Init dummy photon ray with intersection at photon position */
242 >      VCOPY(ray.rop, photon.pos);
243 >      for (j = 0; j < 3; j++)
244 >         ray.ron [j] = photon.norm [j] / 127.0;
245        
246 <      photonDensity(pmap, &ray, irrad);
247 <      setPhotonFlux(p, irrad);
246 >      /* Get density estimate at photon position */
247 >      photonDensity(pmap, &ray, ray.rcol);
248 >                  
249 >      /* Append photon to new heap from ray */
250 >      newPhoton(&nuPmap, &ray);
251 >      
252 >      /* Update progress */
253        repProgress++;
254        
255        if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime)
256           pmapPreCompReport();
257 <      #ifdef SIGCONT
258 <         else signal(SIGCONT, pmapPreCompReport);
259 <      #endif
257 > #ifdef SIGCONT
258 >      else signal(SIGCONT, pmapPreCompReport);
259 > #endif
260     }
261    
262 <   #ifdef SIGCONT  
263 <      signal(SIGCONT, SIG_DFL);
296 <   #endif
262 >   /* Flush heap */
263 >   flushPhotonHeap(&nuPmap);
264    
265 <   /* Replace & rebuild heap */
266 <   free(pmap -> heap);
267 <   pmap -> heap = nuHeap;
301 <   pmap -> heapSize = pmap -> heapEnd = nuHeapSize;
302 <   VCOPY(pmap -> minPos, nuMinPos);
303 <   VCOPY(pmap -> maxPos, nuMaxPos);
265 > #ifdef SIGCONT  
266 >   signal(SIGCONT, SIG_DFL);
267 > #endif
268    
269 <   if (photonRepTime) {
270 <      eputs("Rebuilding global photon heap...\n");
269 >   /* Trash original pmap, replace with precomputed one */
270 >   deletePhotons(pmap);
271 >   memcpy(pmap, &nuPmap, sizeof(PhotonMap));
272 >  
273 >   if (verbose) {
274 >      eputs("\nRebuilding precomputed photon map\n");
275 > #if NIX      
276        fflush(stderr);
277 + #endif      
278     }
279 <  
280 <   balancePhotons(pmap, NULL);
279 >
280 >   /* Rebuild underlying data structure, destroying heap */  
281 >   buildPhotonMap(pmap, NULL, NULL, 1);
282   }
283  
284  
285  
286 < void distribPhotons (PhotonMap **pmaps)
286 > typedef struct {
287 >   unsigned long  numPhotons [NUM_PMAP_TYPES],
288 >                  numEmitted, numComplete;
289 > } PhotonCnt;
290 >
291 >
292 >
293 > void distribPhotons (PhotonMap **pmaps, unsigned numProc)
294   {
295 <   EmissionMap emap;
296 <   char errmsg2 [128];
297 <   unsigned t, srcIdx, passCnt = 0, prePassCnt = 0;
298 <   double totalFlux = 0;
299 <   PhotonMap *pm;
295 >   EmissionMap    emap;
296 >   char           errmsg2 [128], shmFname [PMAP_TMPFNLEN];
297 >   unsigned       t, srcIdx, proc;
298 >   double         totalFlux = 0;
299 >   int            shmFile, stat, pid;
300 >   PhotonMap      *pm;
301 >   PhotonCnt      *photonCnt;
302    
303 <   for (t = 0; t < NUM_PMAP_TYPES && !photonMaps [t]; t++);
303 >   for (t = 0; t < NUM_PMAP_TYPES && !pmaps [t]; t++);
304 >  
305     if (t >= NUM_PMAP_TYPES)
306 <      error(USER, "no photon maps defined");
306 >      error(USER, "no photon maps defined in distribPhotons");
307        
308     if (!nsources)
309 <      error(USER, "no light sources");
309 >      error(USER, "no light sources in distribPhotons");
310  
311     /* ===================================================================
312      * INITIALISATION - Set up emission and scattering funcs
# Line 334 | Line 315 | void distribPhotons (PhotonMap **pmaps)
315     emap.maxPartitions = MAXSPART;
316     emap.partitions = (unsigned char*)malloc(emap.maxPartitions >> 1);
317     if (!emap.partitions)
318 <      error(INTERNAL, "can't allocate source partitions");
318 >      error(INTERNAL, "can't allocate source partitions in distribPhotons");
319        
320     /* Initialise all defined photon maps */
321     for (t = 0; t < NUM_PMAP_TYPES; t++)
322 <      initPhotonMap(photonMaps [t], t);
322 >      if (pmaps [t]) {
323 >         initPhotonMap(pmaps [t], t);
324 >         /* Open photon heapfile */
325 >         initPhotonHeap(pmaps [t]);
326 >         /* Per-subprocess target count */
327 >         pmaps [t] -> distribTarget /= numProc;
328 >        
329 >         if (!pmaps [t] -> distribTarget)
330 >            error(INTERNAL, "no photons to distribute in distribPhotons");
331 >      }
332  
333     initPhotonEmissionFuncs();
334     initPhotonScatterFuncs();
# Line 350 | Line 340 | void distribPhotons (PhotonMap **pmaps)
340     /* Get photon sensor modifiers */
341     getPhotonSensors(photonSensorList);
342    
343 <   /* Seed RNGs for photon distribution */
344 <   pmapSeed(randSeed, partState);
345 <   pmapSeed(randSeed, emitState);
346 <   pmapSeed(randSeed, cntState);
347 <   pmapSeed(randSeed, mediumState);
348 <   pmapSeed(randSeed, scatterState);
349 <   pmapSeed(randSeed, rouletteState);
343 > #if NIX
344 >   /* Set up shared mem for photon counters (zeroed by ftruncate) */
345 >   strcpy(shmFname, PMAP_TMPFNAME);
346 >   shmFile = mkstemp(shmFname);
347 >
348 >   if (shmFile < 0 || ftruncate(shmFile, sizeof(*photonCnt)) < 0)
349 >      error(SYSTEM, "failed shared mem init in distribPhotons");
350 >
351 >   photonCnt = mmap(NULL, sizeof(*photonCnt), PROT_READ | PROT_WRITE,
352 >                    MAP_SHARED, shmFile, 0);
353 >                    
354 >   if (photonCnt == MAP_FAILED)
355 >      error(SYSTEM, "failed mapping shared memory in distribPhotons");
356 > #else
357 >   /* Allocate photon counters statically on Windoze */
358 >   if (!(photonCnt = malloc(sizeof(PhotonCnt))))
359 >      error(SYSTEM, "failed trivial malloc in distribPhotons");
360 >   photonCnt -> numEmitted = photonCnt -> numComplete = 0;      
361 > #endif /* NIX */
362 >
363 >   if (verbose) {
364 >      sprintf(errmsg, "\nIntegrating flux from %d sources", nsources);
365 >      
366 >      if (photonPorts) {
367 >         sprintf(errmsg2, " via %d ports", numPhotonPorts);
368 >         strcat(errmsg, errmsg2);
369 >      }
370 >      
371 >      strcat(errmsg, "\n");
372 >      eputs(errmsg);
373 >   }  
374    
361   if (photonRepTime)
362      eputs("\n");
363  
375     /* ===================================================================
376      * FLUX INTEGRATION - Get total photon flux from light sources
377      * =================================================================== */
378 <   for (srcIdx = 0; srcIdx < nsources; srcIdx++) {        
378 >   for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
379        unsigned portCnt = 0;
380        emap.src = source + srcIdx;
381        
382 <      do {
382 >      do {  /* Need at least one iteration if no ports! */
383           emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt
384                                                     : NULL;
385           photonPartition [emap.src -> so -> otype] (&emap);
386          
387 <         if (photonRepTime) {
388 <            sprintf(errmsg, "Integrating flux from source %s ",
387 >         if (verbose) {
388 >            sprintf(errmsg, "\tIntegrating flux from source %s ",
389                      source [srcIdx].so -> oname);
390 <                    
390 >
391              if (emap.port) {
392                 sprintf(errmsg2, "via port %s ",
393                         photonPorts [portCnt].so -> oname);
394                 strcat(errmsg, errmsg2);
395              }
396 <            
397 <            sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions);
396 >
397 >            sprintf(errmsg2, "(%lu partitions)\n", emap.numPartitions);
398              strcat(errmsg, errmsg2);
399              eputs(errmsg);
400 + #if NIX            
401              fflush(stderr);
402 + #endif            
403           }
404          
405           for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
# Line 401 | Line 414 | void distribPhotons (PhotonMap **pmaps)
414  
415     if (totalFlux < FTINY)
416        error(USER, "zero flux from light sources");
404
405   /* Record start time and enable progress report signal handler */
406   repStartTime = time(NULL);
407   #ifdef SIGCONT
408      signal(SIGCONT, pmapDistribReport);
409   #endif
410   repProgress = prePassCnt = 0;
411  
412   if (photonRepTime)
413      eputs("\n");
414  
415   /* ===================================================================
416    * 2-PASS PHOTON DISTRIBUTION
417    * Pass 1 (pre):  emit fraction of target photon count
418    * Pass 2 (main): based on outcome of pass 1, estimate remaining number
419    *                of photons to emit to approximate target count
420    * =================================================================== */
421   do {
422      double numEmit;
417        
418 <      if (!passCnt) {  
419 <         /* INIT PASS 1 */
426 <         /* Skip if no photons contributed after sufficient iterations; make
427 <          * it clear to user which photon maps are missing so (s)he can
428 <          * check the scene geometry and materials */
429 <         if (++prePassCnt > maxPreDistrib) {
430 <            sprintf(errmsg, "too many prepasses");
418 >   /* Record start time for progress reports */
419 >   repStartTime = time(NULL);
420  
421 <            for (t = 0; t < NUM_PMAP_TYPES; t++)
422 <               if (photonMaps [t] && !photonMaps [t] -> heapEnd) {
423 <                  sprintf(errmsg2, ", no %s photons stored", pmapName [t]);
424 <                  strcat(errmsg, errmsg2);
436 <               }
437 <            
438 <            error(USER, errmsg);
439 <            break;
440 <         }
421 >   if (verbose) {
422 >      sprintf(errmsg, "\nPhoton distribution @ %d procs\n", numProc);
423 >      eputs(errmsg);
424 >   }
425  
426 <         /* Num to emit is fraction of minimum target count */
427 <         numEmit = FHUGE;
426 >   /* MAIN LOOP */  
427 >   for (proc = 0; proc < numProc; proc++) {
428 > #if NIX          
429 >      if (!(pid = fork())) {
430 >         /* SUBPROCESS ENTERS HERE; open and mmapped files inherited */
431 > #else
432 >      if (1) {
433 >         /* No subprocess under Windoze */
434 > #endif
435 >         /* Local photon counters for this subprocess */
436 >         unsigned       passCnt = 0, prePassCnt = 0;
437 >         unsigned long  lastNumPhotons [NUM_PMAP_TYPES];
438 >         unsigned long  localNumEmitted = 0; /* Num photons emitted by this
439 >                                                subprocess alone */
440          
441 <         for (t = 0; t < NUM_PMAP_TYPES; t++)
442 <            if (photonMaps [t])
443 <               numEmit = min(photonMaps [t] -> distribTarget, numEmit);
444 <              
445 <         numEmit *= preDistrib;
446 <      }
447 <
452 <      else {            
453 <         /* INIT PASS 2 */
454 <         /* Based on the outcome of the predistribution we can now estimate
455 <          * how many more photons we have to emit for each photon map to
456 <          * meet its respective target count. This value is clamped to 0 in
457 <          * case the target has already been exceeded in the pass 1. Note
458 <          * repProgress is the number of photons emitted thus far, while
459 <          * heapEnd is the number of photons stored in each photon map. */
460 <         double maxDistribRatio = 0;
461 <
462 <         /* Set the distribution ratio for each map; this indicates how many
463 <          * photons of each respective type are stored per emitted photon,
464 <          * and is used as probability for storing a photon by addPhoton().
465 <          * Since this biases the photon density, addPhoton() promotes the
466 <          * flux of stored photons to compensate. */
467 <         for (t = 0; t < NUM_PMAP_TYPES; t++)
468 <            if ((pm = photonMaps [t])) {
469 <               pm -> distribRatio = (double)pm -> distribTarget /
470 <                                    pm -> heapEnd - 1;
471 <
472 <               /* Check if photon map "overflowed", i.e. exceeded its target
473 <                * count in the prepass; correcting the photon flux via the
474 <                * distribution ratio is no longer possible, as no more
475 <                * photons of this type will be stored, so notify the user
476 <                * rather than deliver incorrect results.
477 <                * In future we should handle this more intelligently by
478 <                * using the photonFlux in each photon map to individually
479 <                * correct the flux after distribution. */
480 <               if (pm -> distribRatio <= FTINY) {
481 <                  sprintf(errmsg,
482 <                          "%s photon map overflow in prepass, reduce -apD",
483 <                          pmapName [t]);
484 <                  error(INTERNAL, errmsg);
485 <               }
441 >         /* Seed RNGs from PID for decorellated photon distribution */
442 >         pmapSeed(randSeed + proc, partState);
443 >         pmapSeed(randSeed + (proc + 1) % numProc, emitState);
444 >         pmapSeed(randSeed + (proc + 2) % numProc, cntState);
445 >         pmapSeed(randSeed + (proc + 3) % numProc, mediumState);
446 >         pmapSeed(randSeed + (proc + 4) % numProc, scatterState);
447 >         pmapSeed(randSeed + (proc + 5) % numProc, rouletteState);
448                    
487               maxDistribRatio = max(pm -> distribRatio, maxDistribRatio);
488            }
489        
490         /* Normalise distribution ratios and calculate number of photons to
491          * emit in main pass */
449           for (t = 0; t < NUM_PMAP_TYPES; t++)
450 <            if ((pm = photonMaps [t]))
451 <               pm -> distribRatio /= maxDistribRatio;
452 <              
453 <         if ((numEmit = repProgress * maxDistribRatio) < FTINY)
454 <            /* No photons left to distribute in main pass */
455 <            break;
456 <      }
457 <      
458 <      /* Set completion count for progress report */
502 <      repComplete = numEmit + repProgress;                            
503 <      
504 <      /* PHOTON DISTRIBUTION LOOP */
505 <      for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
506 <         unsigned portCnt = 0;
507 <         emap.src = source + srcIdx;
508 <                  
450 >            lastNumPhotons [t] = 0;
451 >            
452 >         /* =============================================================
453 >          * 2-PASS PHOTON DISTRIBUTION
454 >          * Pass 1 (pre):  emit fraction of target photon count
455 >          * Pass 2 (main): based on outcome of pass 1, estimate remaining
456 >          *                number of photons to emit to approximate target
457 >          *                count
458 >          * ============================================================= */
459           do {
460 <            emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt
511 <                                                      : NULL;
512 <            photonPartition [emap.src -> so -> otype] (&emap);
460 >            double numEmit;
461              
462 <            if (photonRepTime) {
463 <               if (!passCnt)
464 <                  sprintf(errmsg, "PREPASS %d on source %s ",
465 <                          prePassCnt, source [srcIdx].so -> oname);
466 <               else
467 <                  sprintf(errmsg, "MAIN PASS on source %s ",
468 <                          source [srcIdx].so -> oname);
469 <                      
470 <               if (emap.port) {
471 <                  sprintf(errmsg2, "via port %s ",
472 <                          photonPorts [portCnt].so -> oname);
473 <                  strcat(errmsg, errmsg2);
462 >            if (!passCnt) {  
463 >               /* INIT PASS 1 */              
464 >               /* Skip if no photons contributed after sufficient
465 >                * iterations; make it clear to user which photon maps are
466 >                * missing so (s)he can check geometry and materials */
467 >               if (++prePassCnt > maxPreDistrib) {
468 >                  sprintf(errmsg, "proc %d: too many prepasses", proc);
469 >
470 >                  for (t = 0; t < NUM_PMAP_TYPES; t++)
471 >                     if (pmaps [t] && !pmaps [t] -> numPhotons) {
472 >                        sprintf(errmsg2, ", no %s photons stored",
473 >                                pmapName [t]);
474 >                        strcat(errmsg, errmsg2);
475 >                     }
476 >                  
477 >                  error(USER, errmsg);
478 >                  break;
479                 }
480 +
481 +               /* Num to emit is fraction of minimum target count */
482 +               numEmit = FHUGE;
483                
484 <               sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions);
485 <               strcat(errmsg, errmsg2);
486 <               eputs(errmsg);
487 <               fflush(stderr);
484 >               for (t = 0; t < NUM_PMAP_TYPES; t++)
485 >                  if (pmaps [t])
486 >                     numEmit = min(pmaps [t] -> distribTarget, numEmit);
487 >                    
488 >               numEmit *= preDistrib;
489              }
490 <            
491 <            for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
492 <                 emap.partitionCnt++) {            
493 <               double partNumEmit;
494 <               unsigned long partEmitCnt;
490 >            else {            
491 >               /* INIT PASS 2 */
492 >               /* Based on the outcome of the predistribution we can now
493 >                * estimate how many more photons we have to emit for each
494 >                * photon map to meet its respective target count.  This
495 >                * value is clamped to 0 in case the target has already been
496 >                * exceeded in the pass 1. */
497 >               double maxDistribRatio = 0;
498 >
499 >               /* Set the distribution ratio for each map; this indicates
500 >                * how many photons of each respective type are stored per
501 >                * emitted photon, and is used as probability for storing a
502 >                * photon by newPhoton().  Since this biases the photon
503 >                * density, newPhoton() promotes the flux of stored photons
504 >                * to compensate.  */
505 >               for (t = 0; t < NUM_PMAP_TYPES; t++)
506 >                  if ((pm = pmaps [t])) {
507 >                     pm -> distribRatio = (double)pm -> distribTarget /
508 >                                          pm -> numPhotons - 1;
509 >
510 >                     /* Check if photon map "overflowed", i.e. exceeded its
511 >                      * target count in the prepass; correcting the photon
512 >                      * flux via the distribution ratio is no longer
513 >                      * possible, as no more photons of this type will be
514 >                      * stored, so notify the user rather than deliver
515 >                      * incorrect results.  In future we should handle this
516 >                      * more intelligently by using the photonFlux in each
517 >                      * photon map to individually correct the flux after
518 >                      * distribution.  */
519 >                     if (pm -> distribRatio <= FTINY) {
520 >                        sprintf(errmsg, "%s photon map overflow in "
521 >                                "prepass, reduce -apD", pmapName [t]);
522 >                        error(INTERNAL, errmsg);
523 >                     }
524 >                        
525 >                     maxDistribRatio = max(pm -> distribRatio,
526 >                                           maxDistribRatio);
527 >                  }
528                
529 <               /* Get photon origin within current source partishunn and
530 <                * build emission map */
531 <               photonOrigin [emap.src -> so -> otype] (&emap);
532 <               initPhotonEmission(&emap, pdfSamples);
533 <              
534 <               /* Number of photons to emit from ziss partishunn --
535 <                * proportional to flux; photon ray weight and scalar flux
536 <                * are uniform (the latter only varying in RGB). */
537 <               partNumEmit = numEmit * colorAvg(emap.partFlux) / totalFlux;
538 <               partEmitCnt = (unsigned long)partNumEmit;
549 <              
550 <               /* Probabilistically account for fractional photons */
551 <               if (pmapRandom(cntState) < partNumEmit - partEmitCnt)
552 <                  partEmitCnt++;
529 >               /* Normalise distribution ratios and calculate number of
530 >                * photons to emit in main pass */
531 >               for (t = 0; t < NUM_PMAP_TYPES; t++)
532 >                  if ((pm = pmaps [t]))
533 >                     pm -> distribRatio /= maxDistribRatio;
534 >                    
535 >               if ((numEmit = localNumEmitted * maxDistribRatio) < FTINY)
536 >                  /* No photons left to distribute in main pass */
537 >                  break;
538 >            }
539  
540 <               /* Integer counter avoids FP rounding errors */
541 <               while (partEmitCnt--) {
542 <                  RAY photonRay;
540 >            /* Update shared completion counter for progreport by parent */
541 >            photonCnt -> numComplete += numEmit;                            
542 >
543 >            /* PHOTON DISTRIBUTION LOOP */
544 >            for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
545 >               unsigned portCnt = 0;
546 >               emap.src = source + srcIdx;
547 >
548 >               do {  /* Need at least one iteration if no ports! */
549 >                  emap.port = emap.src -> sflags & SDISTANT
550 >                              ? photonPorts + portCnt : NULL;
551 >                  photonPartition [emap.src -> so -> otype] (&emap);
552 >
553 >                  if (verbose && !proc) {
554 >                     /* Output from subproc 0 only to avoid race condition
555 >                      * on console I/O */
556 >                     if (!passCnt)
557 >                        sprintf(errmsg, "\tPREPASS %d on source %s ",
558 >                                prePassCnt, source [srcIdx].so -> oname);
559 >                     else
560 >                        sprintf(errmsg, "\tMAIN PASS on source %s ",
561 >                                source [srcIdx].so -> oname);
562 >
563 >                     if (emap.port) {
564 >                        sprintf(errmsg2, "via port %s ",
565 >                                photonPorts [portCnt].so -> oname);
566 >                        strcat(errmsg, errmsg2);
567 >                     }
568 >
569 >                     sprintf(errmsg2, "(%lu partitions)\n",
570 >                             emap.numPartitions);
571 >                     strcat(errmsg, errmsg2);
572 >                     eputs(errmsg);
573 > #if NIX                    
574 >                     fflush(stderr);
575 > #endif                    
576 >                  }
577                    
578 <                  /* Emit photon based on PDF and trace through scene until
579 <                   * absorbed/leaked */
580 <                  emitPhoton(&emap, &photonRay);
581 <                  tracePhoton(&photonRay);
578 >                  for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
579 >                       emap.partitionCnt++) {
580 >                     double partNumEmit;
581 >                     unsigned long partEmitCnt;
582 >                    
583 >                     /* Get photon origin within current source partishunn
584 >                      * and build emission map */
585 >                     photonOrigin [emap.src -> so -> otype] (&emap);
586 >                     initPhotonEmission(&emap, pdfSamples);
587 >                    
588 >                     /* Number of photons to emit from ziss partishunn --
589 >                      * proportional to flux; photon ray weight and scalar
590 >                      * flux are uniform (latter only varying in RGB). */
591 >                     partNumEmit = numEmit * colorAvg(emap.partFlux) /
592 >                                   totalFlux;
593 >                     partEmitCnt = (unsigned long)partNumEmit;
594 >                    
595 >                     /* Probabilistically account for fractional photons */
596 >                     if (pmapRandom(cntState) < partNumEmit - partEmitCnt)
597 >                        partEmitCnt++;
598 >
599 >                     /* Update local and shared (global) emission counter */
600 >                     photonCnt -> numEmitted += partEmitCnt;
601 >                     localNumEmitted += partEmitCnt;
602 >
603 >                     /* Integer counter avoids FP rounding errors during
604 >                      * iteration */
605 >                     while (partEmitCnt--) {
606 >                        RAY photonRay;
607 >                        
608 >                        /* Emit photon based on PDF and trace through scene
609 >                         * until absorbed/leaked */
610 >                        emitPhoton(&emap, &photonRay);
611 > #if 1
612 >                        if (emap.port)
613 >                           /* !!!  PHOTON PORT REJECTION SAMPLING HACK: set
614 >                            * !!!  photon port as fake hit object for
615 >                            * !!!  primary ray to check for intersection in
616 >                            * !!!  tracePhoton() */
617 >                           photonRay.ro = emap.port -> so;
618 > #endif
619 >                        tracePhoton(&photonRay);
620 >                     }                                          
621 >
622 >                     /* Update shared global photon count for each pmap */
623 >                     for (t = 0; t < NUM_PMAP_TYPES; t++)
624 >                        if (pmaps [t]) {
625 >                           photonCnt -> numPhotons [t] +=
626 >                              pmaps [t] -> numPhotons - lastNumPhotons [t];
627 >                           lastNumPhotons [t] = pmaps [t] -> numPhotons;
628 >                        }
629 > #if !NIX
630 >                     /* Synchronous progress report on Windoze */
631 >                     if (!proc && photonRepTime > 0 &&
632 >                           time(NULL) >= repLastTime + photonRepTime) {
633 >                        repEmitted = repProgress = photonCnt -> numEmitted;
634 >                        repComplete = photonCnt -> numComplete;                          
635 >                        pmapDistribReport();
636 >                     }
637 > #endif
638 >                  }
639                    
640 <                  /* Record progress */
641 <                  repProgress++;
642 <                  
643 <                  if (photonRepTime > 0 &&
644 <                      time(NULL) >= repLastTime + photonRepTime)
645 <                     pmapDistribReport();
646 <                  #ifdef SIGCONT
647 <                     else signal(SIGCONT, pmapDistribReport);
648 <                  #endif
640 >                  portCnt++;
641 >               } while (portCnt < numPhotonPorts);
642 >            }
643 >            
644 >            for (t = 0; t < NUM_PMAP_TYPES; t++)
645 >               if (pmaps [t] && !pmaps [t] -> numPhotons) {
646 >                  /* Double preDistrib in case a photon map is empty and
647 >                   * redo pass 1 --> possibility of infinite loop for
648 >                   * pathological scenes (e.g.  absorbing materials) */
649 >                  preDistrib *= 2;
650 >                  break;
651                 }
652 +            
653 +            if (t >= NUM_PMAP_TYPES)
654 +               /* No empty photon maps found; now do pass 2 */
655 +               passCnt++;
656 +         } while (passCnt < 2);
657 +        
658 +         /* Flush heap buffa for every pmap one final time;
659 +          * avoids potential data corruption! */
660 +         for (t = 0; t < NUM_PMAP_TYPES; t++)
661 +            if (pmaps [t]) {
662 +               flushPhotonHeap(pmaps [t]);
663 +               /* Heap file closed automatically on exit
664 +                  fclose(pmaps [t] -> heap); */
665 + #ifdef DEBUG_PMAP              
666 +               sprintf(errmsg, "Proc %d: total %ld photons\n", proc,
667 +                       pmaps [t] -> numPhotons);
668 +               eputs(errmsg);
669 + #endif              
670              }
671 <                        
672 <            portCnt++;
673 <         } while (portCnt < numPhotonPorts);
671 > #if NIX
672 >         /* Terminate subprocess */
673 >         exit(0);
674 > #endif
675        }
676 +      else if (pid < 0)
677 +         error(SYSTEM, "failed to fork subprocess in distribPhotons");        
678 +   }
679 +
680 + #if NIX
681 +   /* PARENT PROCESS CONTINUES HERE */
682 + #ifdef SIGCONT
683 +   /* Enable progress report signal handler */
684 +   signal(SIGCONT, pmapDistribReport);
685 + #endif  
686 +   /* Wait for subprocesses complete while reporting progress */
687 +   proc = numProc;
688 +   while (proc) {
689 +      while (waitpid(-1, &stat, WNOHANG) > 0) {
690 +         /* Subprocess exited; check status */
691 +         if (!WIFEXITED(stat) || WEXITSTATUS(stat))
692 +            error(USER, "failed photon distribution");
693        
694 +         --proc;
695 +      }
696 +      
697 +      /* Nod off for a bit and update progress  */
698 +      sleep(1);
699 +
700 +      /* Asynchronous progress report from shared subprocess counters */  
701 +      repEmitted = repProgress = photonCnt -> numEmitted;
702 +      repComplete = photonCnt -> numComplete;      
703 +
704 +      repProgress = repComplete = 0;
705        for (t = 0; t < NUM_PMAP_TYPES; t++)
706 <         if (photonMaps [t] && !photonMaps [t] -> heapEnd) {
707 <            /* Double preDistrib in case a photon map is empty and redo
708 <             * pass 1 --> possibility of infinite loop for pathological
709 <             * scenes (e.g. absorbing materials) */
584 <            preDistrib *= 2;
585 <            break;
706 >         if ((pm = pmaps [t])) {
707 >            /* Get global photon count from shmem updated by subprocs */
708 >            repProgress += pm -> numPhotons = photonCnt -> numPhotons [t];
709 >            repComplete += pm -> distribTarget;
710           }
711 <      
588 <      if (t >= NUM_PMAP_TYPES) {
589 <         /* No empty photon maps found; now do pass 2 */
590 <         passCnt++;
591 <         if (photonRepTime)
592 <            eputs("\n");
593 <      }
594 <   } while (passCnt < 2);
711 >      repComplete *= numProc;
712  
713 +      if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime)
714 +         pmapDistribReport();
715 + #ifdef SIGCONT
716 +      else signal(SIGCONT, pmapDistribReport);
717 + #endif
718 +   }
719 + #endif /* NIX */
720 +
721     /* ===================================================================
722 <    * POST-DISTRIBUTION - Set photon flux and build kd-tree, etc.
722 >    * POST-DISTRIBUTION - Set photon flux and build data struct for photon
723 >    * storage, etc.
724      * =================================================================== */
725 <   #ifdef SIGCONT    
726 <      signal(SIGCONT, SIG_DFL);
727 <   #endif
725 > #ifdef SIGCONT    
726 >   /* Reset signal handler */
727 >   signal(SIGCONT, SIG_DFL);
728 > #endif
729     free(emap.samples);
730    
731 <   /* Set photon flux (repProgress is total num emitted) */
732 <   totalFlux /= repProgress;
733 <  
731 >   /* Set photon flux */
732 >   totalFlux /= photonCnt -> numEmitted;
733 > #if NIX  
734 >   /* Photon counters no longer needed, unmap shared memory */
735 >   munmap(photonCnt, sizeof(*photonCnt));
736 >   close(shmFile);
737 >   unlink(shmFname);
738 > #else
739 >   free(photonCnt);  
740 > #endif      
741 >   if (verbose)
742 >      eputs("\n");
743 >      
744     for (t = 0; t < NUM_PMAP_TYPES; t++)
745 <      if (photonMaps [t]) {
746 <         if (photonRepTime) {
747 <            sprintf(errmsg, "\nBuilding %s photon map...\n", pmapName [t]);
745 >      if (pmaps [t]) {
746 >         if (verbose) {
747 >            sprintf(errmsg, "Building %s photon map\n", pmapName [t]);
748              eputs(errmsg);
749 + #if NIX            
750              fflush(stderr);
751 + #endif            
752           }
753 <      
754 <         balancePhotons(photonMaps [t], &totalFlux);
753 >        
754 >         /* Build underlying data structure; heap is destroyed */
755 >         buildPhotonMap(pmaps [t], &totalFlux, NULL, numProc);
756        }
757        
758     /* Precompute photon irradiance if necessary */
759 <   if (preCompPmap)
759 >   if (preCompPmap) {
760 >      if (verbose)
761 >         eputs("\n");
762        preComputeGlobal(preCompPmap);
763 < }
622 <
623 <
624 <
625 < void photonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad)
626 < /* Photon density estimate. Returns irradiance at ray -> rop. */
627 < {
628 <   unsigned i;
629 <   PhotonSQNode *sq;
630 <   float r;
631 <   COLOR flux;
632 <
633 <   setcolor(irrad, 0, 0, 0);
634 <
635 <   if (!pmap -> maxGather)
636 <      return;
637 <      
638 <   /* Ignore sources */
639 <   if (ray -> ro)
640 <      if (islight(objptr(ray -> ro -> omod) -> otype))
641 <         return;
642 <        
643 <   pmap -> squeueEnd = 0;
644 <   findPhotons(pmap, ray);
763 >   }      
764    
765 <   /* Need at least 2 photons */
766 <   if (pmap -> squeueEnd < 2) {
648 <      #ifdef PMAP_NONEFOUND  
649 <         sprintf(errmsg, "no photons found on %s at (%.3f, %.3f, %.3f)",
650 <                 ray -> ro ? ray -> ro -> oname : "<null>",
651 <                 ray -> rop [0], ray -> rop [1], ray -> rop [2]);
652 <         error(WARNING, errmsg);
653 <      #endif      
654 <
655 <      return;
656 <   }
657 <      
658 <   if (pmap -> minGather == pmap -> maxGather) {
659 <      /* No bias compensation. Just do a plain vanilla estimate */
660 <      sq = pmap -> squeue + 1;
661 <      
662 <      /* Average radius between furthest two photons to improve accuracy */      
663 <      r = max(sq -> dist, (sq + 1) -> dist);
664 <      r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r));  
665 <      
666 <      /* Skip the extra photon */
667 <      for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) {
668 <         getPhotonFlux(sq -> photon, flux);        
669 < #ifdef PMAP_EPANECHNIKOV
670 <         /* Apply Epanechnikov kernel to photon flux (dists are squared) */
671 <         scalecolor(flux, 2 * (1 - sq -> dist / r));
672 < #endif        
673 <         addcolor(irrad, flux);
674 <      }
675 <      
676 <      /* Divide by search area PI * r^2, 1 / PI required as ambient
677 <         normalisation factor */        
678 <      scalecolor(irrad, 1 / (PI * PI * r));
679 <      
680 <      return;
681 <   }
682 <   else
683 <      /* Apply bias compensation to density estimate */
684 <      biasComp(pmap, irrad);
685 < }
686 <
687 <
688 <
689 < void photonPreCompDensity (PhotonMap *pmap, RAY *r, COLOR irrad)
690 < /* Returns precomputed photon density estimate at ray -> rop. */
691 < {
692 <   Photon *p;
693 <  
694 <   setcolor(irrad, 0, 0, 0);
695 <
696 <   /* Ignore sources */
697 <   if (r -> ro && islight(objptr(r -> ro -> omod) -> otype))
698 <      return;
699 <      
700 <   if ((p = find1Photon(preCompPmap, r)))
701 <      getPhotonFlux(p, irrad);
702 < }
703 <
704 <
705 <
706 < void volumePhotonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad)
707 < /* Photon volume density estimate. Returns irradiance at ray -> rop. */
708 < {
709 <   unsigned i;
710 <   PhotonSQNode *sq;
711 <   float gecc2, r, ph;
712 <   COLOR flux;
713 <
714 <   setcolor(irrad, 0, 0, 0);
715 <  
716 <   if (!pmap -> maxGather)
717 <      return;
718 <      
719 <   pmap -> squeueEnd = 0;
720 <   findPhotons(pmap, ray);
721 <  
722 <   /* Need at least 2 photons */
723 <   if (pmap -> squeueEnd < 2)
724 <      return;
725 <      
726 <   if (pmap -> minGather == pmap -> maxGather) {
727 <      /* No bias compensation. Just do a plain vanilla estimate */
728 <      gecc2 = ray -> gecc * ray -> gecc;
729 <      sq = pmap -> squeue + 1;
730 <      
731 <      /* Average radius between furthest two photons to improve accuracy */      
732 <      r = max(sq -> dist, (sq + 1) -> dist);
733 <      r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r));  
734 <      
735 <      /* Skip the extra photon */
736 <      for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) {
737 <         /* Compute phase function for inscattering from photon */
738 <         if (gecc2 <= FTINY)
739 <            ph = 1;
740 <         else {
741 <            ph = DOT(ray -> rdir, sq -> photon -> norm) / 127;
742 <            ph = 1 + gecc2 - 2 * ray -> gecc * ph;
743 <            ph = (1 - gecc2) / (ph * sqrt(ph));
744 <         }
745 <        
746 <         getPhotonFlux(sq -> photon, flux);
747 <         scalecolor(flux, ph);
748 <         addcolor(irrad, flux);
749 <      }
750 <      
751 <      /* Divide by search volume 4 / 3 * PI * r^3 and phase function
752 <         normalization factor 1 / (4 * PI) */
753 <      scalecolor(irrad, 3 / (16 * PI * PI * r * sqrt(r)));
754 <      
755 <      return;
756 <   }
757 <  
758 <   else
759 <      /* Apply bias compensation to density estimate */
760 <      volumeBiasComp(pmap, ray, irrad);
765 >   if (verbose)
766 >      eputs("\n");
767   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines