SpatialLightDistribution::~SpatialLightDistribution() { // Gather statistics about how well the computed distributions are across // the buckets. // This is slightly ugly: we are depending on the destructor running // before statistics are reported (which is currently the case at least). for (size_t i = 0; i < nBuckets; ++i) { LOG(INFO) << "Bucket " << i << ", size " << voxelDistribution[i].size() << ", bucket count " << voxelDistribution[i].bucket_count() << ", load factor " << voxelDistribution[i].load_factor() << ", max load factor " << voxelDistribution[i].max_load_factor(); ReportValue(hashBucketLoad, voxelDistribution[i].size()); } }
const Distribution1D *SpatialLightDistribution::Lookup(const Point3f &p) const { ProfilePhase _(Prof::LightDistribLookup); ++nLookups; // First, compute integer voxel coordinates for the given point |p| // with respect to the overall voxel grid. Vector3f offset = scene.WorldBound().Offset(p); // offset in [0,1]. Point3i pi; for (int i = 0; i < 3; ++i) // The clamp should almost never be necessary, but is there to be // robust to computed intersection points being slightly outside // the scene bounds due to floating-point roundoff error. pi[i] = Clamp(int(offset[i] * nVoxels[i]), 0, nVoxels[i] - 1); // Pack the 3D integer voxel coordinates into a single 64-bit value. uint64_t packedPos = (uint64_t(pi[0]) << 40) | (uint64_t(pi[1]) << 20) | pi[2]; CHECK_NE(packedPos, invalidPackedPos); // Compute a hash value from the packed voxel coordinates. We could // just take packedPos mod the hash table size, but since packedPos // isn't necessarily well distributed on its own, it's worthwhile to do // a little work to make sure that its bits values are individually // fairly random. For details of and motivation for the following, see: // http://zimbry.blogspot.ch/2011/09/better-bit-mixing-improving-on.html uint64_t hash = packedPos; hash ^= (hash >> 31); hash *= 0x7fb5d329728ea185; hash ^= (hash >> 27); hash *= 0x81dadef4bc2dd44d; hash ^= (hash >> 33); hash %= hashTableSize; CHECK_GE(hash, 0); // Now, see if the hash table already has an entry for the voxel. We'll // use quadratic probing when the hash table entry is already used for // another value; step stores the square root of the probe step. int step = 1; int nProbes = 0; while (true) { ++nProbes; HashEntry &entry = hashTable[hash]; // Does the hash table entry at offset |hash| match the current point? uint64_t entryPackedPos = entry.packedPos.load(std::memory_order_acquire); if (entryPackedPos == packedPos) { // Yes! Most of the time, there should already by a light // sampling distribution available. Distribution1D *dist = entry.distribution.load(std::memory_order_acquire); if (dist == nullptr) { // Rarely, another thread will have already done a lookup // at this point, found that there isn't a sampling // distribution, and will already be computing the // distribution for the point. In this case, we spin until // the sampling distribution is ready. We assume that this // is a rare case, so don't do anything more sophisticated // than spinning. ProfilePhase _(Prof::LightDistribSpinWait); while ((dist = entry.distribution.load(std::memory_order_acquire)) == nullptr) // spin :-(. If we were fancy, we'd have any threads // that hit this instead help out with computing the // distribution for the voxel... ; } // We have a valid sampling distribution. ReportValue(nProbesPerLookup, nProbes); return dist; } else if (entryPackedPos != invalidPackedPos) { // The hash table entry we're checking has already been // allocated for another voxel. Advance to the next entry with // quadratic probing. hash += step * step; if (hash >= hashTableSize) hash %= hashTableSize; ++step; } else { // We have found an invalid entry. (Though this may have // changed since the load into entryPackedPos above.) Use an // atomic compare/exchange to try to claim this entry for the // current position. uint64_t invalid = invalidPackedPos; if (entry.packedPos.compare_exchange_weak(invalid, packedPos)) { // Success; we've claimed this position for this voxel's // distribution. Now compute the sampling distribution and // add it to the hash table. As long as packedPos has been // set but the entry's distribution pointer is nullptr, any // other threads looking up the distribution for this voxel // will spin wait until the distribution pointer is // written. Distribution1D *dist = ComputeDistribution(pi); entry.distribution.store(dist, std::memory_order_release); ReportValue(nProbesPerLookup, nProbes); return dist; } } } }
Spectrum VolPathIntegrator::Li(const RayDifferential &r, const Scene &scene, Sampler &sampler, MemoryArena &arena, int depth) const { ProfilePhase p(Prof::SamplerIntegratorLi); Spectrum L(0.f), beta(1.f); RayDifferential ray(r); bool specularBounce = false; int bounces; // Added after book publication: etaScale tracks the accumulated effect // of radiance scaling due to rays passing through refractive // boundaries (see the derivation on p. 527 of the third edition). We // track this value in order to remove it from beta when we apply // Russian roulette; this is worthwhile, since it lets us sometimes // avoid terminating refracted rays that are about to be refracted back // out of a medium and thus have their beta value increased. Float etaScale = 1; for (bounces = 0;; ++bounces) { // Intersect _ray_ with scene and store intersection in _isect_ SurfaceInteraction isect; bool foundIntersection = scene.Intersect(ray, &isect); // Sample the participating medium, if present MediumInteraction mi; if (ray.medium) beta *= ray.medium->Sample(ray, sampler, arena, &mi); if (beta.IsBlack()) break; // Handle an interaction with a medium or a surface if (mi.IsValid()) { // Terminate path if ray escaped or _maxDepth_ was reached if (bounces >= maxDepth) break; ++volumeInteractions; // Handle scattering at point in medium for volumetric path tracer const Distribution1D *lightDistrib = lightDistribution->Lookup(mi.p); L += beta * UniformSampleOneLight(mi, scene, arena, sampler, true, lightDistrib); Vector3f wo = -ray.d, wi; mi.phase->Sample_p(wo, &wi, sampler.Get2D()); ray = mi.SpawnRay(wi); } else { ++surfaceInteractions; // Handle scattering at point on surface for volumetric path tracer // Possibly add emitted light at intersection if (bounces == 0 || specularBounce) { // Add emitted light at path vertex or from the environment if (foundIntersection) L += beta * isect.Le(-ray.d); else for (const auto &light : scene.infiniteLights) L += beta * light->Le(ray); } // Terminate path if ray escaped or _maxDepth_ was reached if (!foundIntersection || bounces >= maxDepth) break; // Compute scattering functions and skip over medium boundaries isect.ComputeScatteringFunctions(ray, arena, true); if (!isect.bsdf) { ray = isect.SpawnRay(ray.d); bounces--; continue; } // Sample illumination from lights to find attenuated path // contribution const Distribution1D *lightDistrib = lightDistribution->Lookup(isect.p); L += beta * UniformSampleOneLight(isect, scene, arena, sampler, true, lightDistrib); // Sample BSDF to get new path direction Vector3f wo = -ray.d, wi; Float pdf; BxDFType flags; Spectrum f = isect.bsdf->Sample_f(wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); if (f.IsBlack() || pdf == 0.f) break; beta *= f * AbsDot(wi, isect.shading.n) / pdf; DCHECK(std::isinf(beta.y()) == false); specularBounce = (flags & BSDF_SPECULAR) != 0; if ((flags & BSDF_SPECULAR) && (flags & BSDF_TRANSMISSION)) { Float eta = isect.bsdf->eta; // Update the term that tracks radiance scaling for refraction // depending on whether the ray is entering or leaving the // medium. etaScale *= (Dot(wo, isect.n) > 0) ? (eta * eta) : 1 / (eta * eta); } ray = isect.SpawnRay(ray, wi, flags, isect.bsdf->eta); // Account for attenuated subsurface scattering, if applicable if (isect.bssrdf && (flags & BSDF_TRANSMISSION)) { // Importance sample the BSSRDF SurfaceInteraction pi; Spectrum S = isect.bssrdf->Sample_S( scene, sampler.Get1D(), sampler.Get2D(), arena, &pi, &pdf); DCHECK(std::isinf(beta.y()) == false); if (S.IsBlack() || pdf == 0) break; beta *= S / pdf; // Account for the attenuated direct subsurface scattering // component L += beta * UniformSampleOneLight(pi, scene, arena, sampler, true, lightDistribution->Lookup(pi.p)); // Account for the indirect subsurface scattering component Spectrum f = pi.bsdf->Sample_f(pi.wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); if (f.IsBlack() || pdf == 0) break; beta *= f * AbsDot(wi, pi.shading.n) / pdf; DCHECK(std::isinf(beta.y()) == false); specularBounce = (flags & BSDF_SPECULAR) != 0; ray = pi.SpawnRay(wi); } } // Possibly terminate the path with Russian roulette // Factor out radiance scaling due to refraction in rrBeta. Spectrum rrBeta = beta * etaScale; if (rrBeta.MaxComponentValue() < rrThreshold && bounces > 3) { Float q = std::max((Float).05, 1 - rrBeta.MaxComponentValue()); if (sampler.Get1D() < q) break; beta /= 1 - q; DCHECK(std::isinf(beta.y()) == false); } } ReportValue(pathLength, bounces); return L; }
Spectrum ConnectBDPT( const Scene &scene, Vertex *lightVertices, Vertex *cameraVertices, int s, int t, const Distribution1D &lightDistr, const std::unordered_map<const Light *, size_t> &lightToIndex, const Camera &camera, Sampler &sampler, Point2f *pRaster, Float *misWeightPtr) { Spectrum L(0.f); // Ignore invalid connections related to infinite area lights if (t > 1 && s != 0 && cameraVertices[t - 1].type == VertexType::Light) return Spectrum(0.f); // Perform connection and write contribution to _L_ Vertex sampled; if (s == 0) { // Interpret the camera subpath as a complete path const Vertex &pt = cameraVertices[t - 1]; if (pt.IsLight()) L = pt.Le(scene, cameraVertices[t - 2]) * pt.beta; DCHECK(!L.HasNaNs()); } else if (t == 1) { // Sample a point on the camera and connect it to the light subpath const Vertex &qs = lightVertices[s - 1]; if (qs.IsConnectible()) { VisibilityTester vis; Vector3f wi; Float pdf; Spectrum Wi = camera.Sample_Wi(qs.GetInteraction(), sampler.Get2D(), &wi, &pdf, pRaster, &vis); if (pdf > 0 && !Wi.IsBlack()) { // Initialize dynamically sampled vertex and _L_ for $t=1$ case sampled = Vertex::CreateCamera(&camera, vis.P1(), Wi / pdf); L = qs.beta * qs.f(sampled, TransportMode::Importance) * sampled.beta; if (qs.IsOnSurface()) L *= AbsDot(wi, qs.ns()); DCHECK(!L.HasNaNs()); // Only check visibility after we know that the path would // make a non-zero contribution. if (!L.IsBlack()) L *= vis.Tr(scene, sampler); } } } else if (s == 1) { // Sample a point on a light and connect it to the camera subpath const Vertex &pt = cameraVertices[t - 1]; if (pt.IsConnectible()) { Float lightPdf; VisibilityTester vis; Vector3f wi; Float pdf; int lightNum = lightDistr.SampleDiscrete(sampler.Get1D(), &lightPdf); const std::shared_ptr<Light> &light = scene.lights[lightNum]; Spectrum lightWeight = light->Sample_Li( pt.GetInteraction(), sampler.Get2D(), &wi, &pdf, &vis); if (pdf > 0 && !lightWeight.IsBlack()) { EndpointInteraction ei(vis.P1(), light.get()); sampled = Vertex::CreateLight(ei, lightWeight / (pdf * lightPdf), 0); sampled.pdfFwd = sampled.PdfLightOrigin(scene, pt, lightDistr, lightToIndex); L = pt.beta * pt.f(sampled, TransportMode::Radiance) * sampled.beta; if (pt.IsOnSurface()) L *= AbsDot(wi, pt.ns()); // Only check visibility if the path would carry radiance. if (!L.IsBlack()) L *= vis.Tr(scene, sampler); } } } else { // Handle all other bidirectional connection cases const Vertex &qs = lightVertices[s - 1], &pt = cameraVertices[t - 1]; if (qs.IsConnectible() && pt.IsConnectible()) { L = qs.beta * qs.f(pt, TransportMode::Importance) * pt.f(qs, TransportMode::Radiance) * pt.beta; VLOG(2) << "General connect s: " << s << ", t: " << t << " qs: " << qs << ", pt: " << pt << ", qs.f(pt): " << qs.f(pt, TransportMode::Importance) << ", pt.f(qs): " << pt.f(qs, TransportMode::Radiance) << ", G: " << G(scene, sampler, qs, pt) << ", dist^2: " << DistanceSquared(qs.p(), pt.p()); if (!L.IsBlack()) L *= G(scene, sampler, qs, pt); } } ++totalPaths; if (L.IsBlack()) ++zeroRadiancePaths; ReportValue(pathLength, s + t - 2); // Compute MIS weight for connection strategy Float misWeight = L.IsBlack() ? 0.f : MISWeight(scene, lightVertices, cameraVertices, sampled, s, t, lightDistr, lightToIndex); VLOG(2) << "MIS weight for (s,t) = (" << s << ", " << t << ") connection: " << misWeight; DCHECK(!std::isnan(misWeight)); L *= misWeight; if (misWeightPtr) *misWeightPtr = misWeight; return L; }
Spectrum PathIntegrator::Li(const RayDifferential &r, const Scene &scene, Sampler &sampler, MemoryArena &arena, int depth) const { ProfilePhase p(Prof::SamplerIntegratorLi); Spectrum L(0.f), beta(1.f); RayDifferential ray(r); bool specularBounce = false; int bounces; for (bounces = 0;; ++bounces) { // Find next path vertex and accumulate contribution VLOG(2) << "Path tracer bounce " << bounces << ", current L = " << L << ", beta = " << beta; // Intersect _ray_ with scene and store intersection in _isect_ SurfaceInteraction isect; bool foundIntersection = scene.Intersect(ray, &isect); // Possibly add emitted light at intersection if (bounces == 0 || specularBounce) { // Add emitted light at path vertex or from the environment if (foundIntersection) { L += beta * isect.Le(-ray.d); VLOG(2) << "Added Le -> L = " << L; } else { for (const auto &light : scene.infiniteLights) L += beta * light->Le(ray); VLOG(2) << "Added infinite area lights -> L = " << L; } } // Terminate path if ray escaped or _maxDepth_ was reached if (!foundIntersection || bounces >= maxDepth) break; // Compute scattering functions and skip over medium boundaries isect.ComputeScatteringFunctions(ray, arena, true); if (!isect.bsdf) { VLOG(2) << "Skipping intersection due to null bsdf"; ray = isect.SpawnRay(ray.d); bounces--; continue; } const Distribution1D *distrib = lightDistribution->Lookup(isect.p); // Sample illumination from lights to find path contribution. // (But skip this for perfectly specular BSDFs.) if (isect.bsdf->NumComponents(BxDFType(BSDF_ALL & ~BSDF_SPECULAR)) > 0) { ++totalPaths; Spectrum Ld = beta * UniformSampleOneLight(isect, scene, arena, sampler, false, distrib); VLOG(2) << "Sampled direct lighting Ld = " << Ld; if (Ld.IsBlack()) ++zeroRadiancePaths; CHECK_GE(Ld.y(), 0.f); L += Ld; } // Sample BSDF to get new path direction Vector3f wo = -ray.d, wi; Float pdf; BxDFType flags; Spectrum f = isect.bsdf->Sample_f(wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); VLOG(2) << "Sampled BSDF, f = " << f << ", pdf = " << pdf; if (f.IsBlack() || pdf == 0.f) break; beta *= f * AbsDot(wi, isect.shading.n) / pdf; VLOG(2) << "Updated beta = " << beta; CHECK_GE(beta.y(), 0.f); DCHECK(!std::isinf(beta.y())); specularBounce = (flags & BSDF_SPECULAR) != 0; ray = isect.SpawnRay(wi); // Account for subsurface scattering, if applicable if (isect.bssrdf && (flags & BSDF_TRANSMISSION)) { // Importance sample the BSSRDF SurfaceInteraction pi; Spectrum S = isect.bssrdf->Sample_S( scene, sampler.Get1D(), sampler.Get2D(), arena, &pi, &pdf); DCHECK(!std::isinf(beta.y())); if (S.IsBlack() || pdf == 0) break; beta *= S / pdf; // Account for the direct subsurface scattering component L += beta * UniformSampleOneLight(pi, scene, arena, sampler, false, lightDistribution->Lookup(pi.p)); // Account for the indirect subsurface scattering component Spectrum f = pi.bsdf->Sample_f(pi.wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); if (f.IsBlack() || pdf == 0) break; beta *= f * AbsDot(wi, pi.shading.n) / pdf; DCHECK(!std::isinf(beta.y())); specularBounce = (flags & BSDF_SPECULAR) != 0; ray = pi.SpawnRay(wi); } // Possibly terminate the path with Russian roulette if (beta.y() < rrThreshold && bounces > 3) { Float q = std::max((Float).05, 1 - beta.MaxComponentValue()); VLOG(2) << "RR termination probability q = " << q; if (sampler.Get1D() < q) break; beta /= 1 - q; VLOG(2) << "After RR survival, beta = " << beta; DCHECK(!std::isinf(beta.y())); } } ReportValue(pathLength, bounces); return L; }
// VolPathIntegrator Method Definitions Spectrum VolPathIntegrator::Li(const RayDifferential &r, const Scene &scene, Sampler &sampler, MemoryArena &arena, int depth) const { ProfilePhase p(Prof::SamplerIntegratorLi); Spectrum L(0.f), beta(1.f); RayDifferential ray(r); bool specularBounce = false; int bounces; for (bounces = 0;; ++bounces) { // Intersect _ray_ with scene and store intersection in _isect_ SurfaceInteraction isect; bool foundIntersection = scene.Intersect(ray, &isect); // Sample the participating medium, if present MediumInteraction mi; if (ray.medium) beta *= ray.medium->Sample(ray, sampler, arena, &mi); if (beta.IsBlack()) break; // Handle an interaction with a medium or a surface if (mi.IsValid()) { // Terminate path if ray escaped or _maxDepth_ was reached if (bounces >= maxDepth) break; ++volumeInteractions; // Handle scattering at point in medium for volumetric path tracer L += beta * UniformSampleOneLight(mi, scene, arena, sampler, true); Vector3f wo = -ray.d, wi; mi.phase->Sample_p(wo, &wi, sampler.Get2D()); ray = mi.SpawnRay(wi); } else { ++surfaceInteractions; // Handle scattering at point on surface for volumetric path tracer // Possibly add emitted light at intersection if (bounces == 0 || specularBounce) { // Add emitted light at path vertex or from the environment if (foundIntersection) L += beta * isect.Le(-ray.d); else for (const auto &light : scene.lights) L += beta * light->Le(ray); } // Terminate path if ray escaped or _maxDepth_ was reached if (!foundIntersection || bounces >= maxDepth) break; // Compute scattering functions and skip over medium boundaries isect.ComputeScatteringFunctions(ray, arena, true); if (!isect.bsdf) { ray = isect.SpawnRay(ray.d); bounces--; continue; } // Sample illumination from lights to find attenuated path // contribution L += beta * UniformSampleOneLight(isect, scene, arena, sampler, true); // Sample BSDF to get new path direction Vector3f wo = -ray.d, wi; Float pdf; BxDFType flags; Spectrum f = isect.bsdf->Sample_f(wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); if (f.IsBlack() || pdf == 0.f) break; beta *= f * AbsDot(wi, isect.shading.n) / pdf; Assert(std::isinf(beta.y()) == false); specularBounce = (flags & BSDF_SPECULAR) != 0; ray = isect.SpawnRay(wi); // Account for attenuated subsurface scattering, if applicable if (isect.bssrdf && (flags & BSDF_TRANSMISSION)) { // Importance sample the BSSRDF SurfaceInteraction pi; Spectrum S = isect.bssrdf->Sample_S( scene, sampler.Get1D(), sampler.Get2D(), arena, &pi, &pdf); #ifndef NDEBUG Assert(std::isinf(beta.y()) == false); #endif if (S.IsBlack() || pdf == 0) break; beta *= S / pdf; // Account for the attenuated direct subsurface scattering // component L += beta * UniformSampleOneLight(pi, scene, arena, sampler, true); // Account for the indirect subsurface scattering component Spectrum f = pi.bsdf->Sample_f(pi.wo, &wi, sampler.Get2D(), &pdf, BSDF_ALL, &flags); if (f.IsBlack() || pdf == 0) break; beta *= f * AbsDot(wi, pi.shading.n) / pdf; #ifndef NDEBUG Assert(std::isinf(beta.y()) == false); #endif specularBounce = (flags & BSDF_SPECULAR) != 0; ray = pi.SpawnRay(wi); } } // Possibly terminate the path with Russian roulette if (bounces > 3) { Float q = std::max((Float).05, 1 - beta.y()); if (sampler.Get1D() < q) break; beta /= 1 - q; Assert(std::isinf(beta.y()) == false); } } ReportValue(pathLength, bounces); return L; }