我了解了BRDF部分重要性抽样背后的逻辑。但是,当涉及对光源进行显式采样时,一切都会变得混乱。例如,如果我的场景中有一个点光源,并且如果我不断地在每个帧中直接对其进行采样,那么我是否应该将其算作蒙特卡洛积分的另一个采样?也就是说,我从余弦加权分布中获取一个样本,从点光源中获取另一个样本。是总共两个样本还是一个样本?另外,我应该将直接采样的辐射光除以任何项吗?
我了解了BRDF部分重要性抽样背后的逻辑。但是,当涉及对光源进行显式采样时,一切都会变得混乱。例如,如果我的场景中有一个点光源,并且如果我不断地在每个帧中直接对其进行采样,那么我是否应该将其算作蒙特卡洛积分的另一个采样?也就是说,我从余弦加权分布中获取一个样本,从点光源中获取另一个样本。是总共两个样本还是一个样本?另外,我应该将直接采样的辐射光除以任何项吗?
Answers:
路径跟踪中有多个区域可以进行重要性采样。此外,这些领域中的每个领域都可以使用Veach和Guibas在1995年的论文中首次提出的多重重要性抽样。为了更好地解释,让我们看一下反向路径跟踪器:
void RenderPixel(uint x, uint y, UniformSampler *sampler) {
Ray ray = m_scene->Camera->CalculateRayFromPixel(x, y, sampler);
float3 color(0.0f);
float3 throughput(1.0f);
SurfaceInteraction interaction;
// Bounce the ray around the scene
const uint maxBounces = 15;
for (uint bounces = 0; bounces < maxBounces; ++bounces) {
m_scene->Intersect(ray);
// The ray missed. Return the background color
if (ray.GeomID == INVALID_GEOMETRY_ID) {
color += throughput * m_scene->BackgroundColor;
break;
}
// Fetch the material
Material *material = m_scene->GetMaterial(ray.GeomID);
// The object might be emissive. If so, it will have a corresponding light
// Otherwise, GetLight will return nullptr
Light *light = m_scene->GetLight(ray.GeomID);
// If we hit a light, add the emission
if (light != nullptr) {
color += throughput * light->Le();
}
interaction.Position = ray.Origin + ray.Direction * ray.TFar;
interaction.Normal = normalize(m_scene->InterpolateNormal(ray.GeomID, ray.PrimID, ray.U, ray.V));
interaction.OutputDirection = normalize(-ray.Direction);
// Get the new ray direction
// Choose the direction based on the bsdf
material->bsdf->Sample(interaction, sampler);
float pdf = material->bsdf->Pdf(interaction);
// Accumulate the weight
throughput = throughput * material->bsdf->Eval(interaction) / pdf;
// Shoot a new ray
// Set the origin at the intersection point
ray.Origin = interaction.Position;
// Reset the other ray properties
ray.Direction = interaction.InputDirection;
ray.TNear = 0.001f;
ray.TFar = infinity;
// Russian Roulette
if (bounces > 3) {
float p = std::max(throughput.x, std::max(throughput.y, throughput.z));
if (sampler->NextFloat() > p) {
break;
}
throughput *= 1 / p;
}
}
m_scene->Camera->FrameBufferData.SplatPixel(x, y, color);
}
用英语讲:
使用此代码,只有在射线最终照射到光线时,我们才会获得颜色。另外,它不支持守时光源,因为它们没有面积。
为了解决这个问题,我们在每次反弹时都直接采样灯光。我们必须做一些小改动:
void RenderPixel(uint x, uint y, UniformSampler *sampler) {
Ray ray = m_scene->Camera->CalculateRayFromPixel(x, y, sampler);
float3 color(0.0f);
float3 throughput(1.0f);
SurfaceInteraction interaction;
// Bounce the ray around the scene
const uint maxBounces = 15;
for (uint bounces = 0; bounces < maxBounces; ++bounces) {
m_scene->Intersect(ray);
// The ray missed. Return the background color
if (ray.GeomID == INVALID_GEOMETRY_ID) {
color += throughput * m_scene->BackgroundColor;
break;
}
// Fetch the material
Material *material = m_scene->GetMaterial(ray.GeomID);
// The object might be emissive. If so, it will have a corresponding light
// Otherwise, GetLight will return nullptr
Light *light = m_scene->GetLight(ray.GeomID);
// If this is the first bounce or if we just had a specular bounce,
// we need to add the emmisive light
if ((bounces == 0 || (interaction.SampledLobe & BSDFLobe::Specular) != 0) && light != nullptr) {
color += throughput * light->Le();
}
interaction.Position = ray.Origin + ray.Direction * ray.TFar;
interaction.Normal = normalize(m_scene->InterpolateNormal(ray.GeomID, ray.PrimID, ray.U, ray.V));
interaction.OutputDirection = normalize(-ray.Direction);
// Calculate the direct lighting
color += throughput * SampleLights(sampler, interaction, material->bsdf, light);
// Get the new ray direction
// Choose the direction based on the bsdf
material->bsdf->Sample(interaction, sampler);
float pdf = material->bsdf->Pdf(interaction);
// Accumulate the weight
throughput = throughput * material->bsdf->Eval(interaction) / pdf;
// Shoot a new ray
// Set the origin at the intersection point
ray.Origin = interaction.Position;
// Reset the other ray properties
ray.Direction = interaction.InputDirection;
ray.TNear = 0.001f;
ray.TFar = infinity;
// Russian Roulette
if (bounces > 3) {
float p = std::max(throughput.x, std::max(throughput.y, throughput.z));
if (sampler->NextFloat() > p) {
break;
}
throughput *= 1 / p;
}
}
m_scene->Camera->FrameBufferData.SplatPixel(x, y, color);
}
首先,我们添加“颜色+ =吞吐量* SampleLights(...)”。我将详细介绍有关SampleLights()的信息。但是,从本质上讲,它遍历所有灯光,并返回它们对颜色的贡献,并由BSDF衰减。
很好,但是我们需要再进行一次更改以使其正确。具体来说,当我们开灯时会发生什么。在旧代码中,我们将光的发射添加到了颜色累积中。但是现在我们在每次反射时都直接采样光,因此,如果添加光的发射,我们将“两次浸入”。因此,正确的做法是……什么都不做;我们跳过累积光的发射。
但是,有两个极端的情况:
如果第一束光线照射到光线,则应该直接看到光线的发射。因此,如果我们跳过它,即使它们周围的表面都被照亮,所有的灯都将显示为黑色。
当您击中完全镜面的表面时,您将无法直接对光线进行采样,因为输入光线只有一个输出。好吧,从技术上讲,我们可以检查输入光线是否会照亮,但是没有意义;无论如何,主要的路径跟踪循环都将这样做。因此,如果在击中镜面后立即击中灯光,则需要累积颜色。如果我们不这样做,镜子中的灯光将变黑。
现在,让我们深入研究SampleLights():
float3 SampleLights(UniformSampler *sampler, SurfaceInteraction interaction, BSDF *bsdf, Light *hitLight) const {
std::size_t numLights = m_scene->NumLights();
float3 L(0.0f);
for (uint i = 0; i < numLights; ++i) {
Light *light = &m_scene->Lights[i];
// Don't let a light contribute light to itself
if (light == hitLight) {
continue;
}
L = L + EstimateDirect(light, sampler, interaction, bsdf);
}
return L;
}
用英语讲:
最后,EssentialDirect()仅评估
对于守时光源,这很简单:
float3 EstimateDirect(Light *light, UniformSampler *sampler, SurfaceInteraction &interaction, BSDF *bsdf) const {
// Only sample if the BRDF is non-specular
if ((bsdf->SupportedLobes & ~BSDFLobe::Specular) != 0) {
return float3(0.0f);
}
interaction.InputDirection = normalize(light->Origin - interaction.Position);
return bsdf->Eval(interaction) * light->Li;
}
但是,如果要让灯光具有面积,则首先需要对灯光上的一个点进行采样。因此,完整的定义是:
float3 EstimateDirect(Light *light, UniformSampler *sampler, SurfaceInteraction &interaction, BSDF *bsdf) const {
float3 directLighting = float3(0.0f);
// Only sample if the BRDF is non-specular
if ((bsdf->SupportedLobes & ~BSDFLobe::Specular) != 0) {
float pdf;
float3 Li = light->SampleLi(sampler, m_scene, interaction, &pdf);
// Make sure the pdf isn't zero and the radiance isn't black
if (pdf != 0.0f && !all(Li)) {
directLighting += bsdf->Eval(interaction) * Li / pdf;
}
}
return directLighting;
}
我们可以根据需要实现light-> SampleLi;我们可以统一选择要点或重要性样本。无论哪种情况,我们都将辐射度除以选择点的pdf。再次,以满足蒙特卡洛的要求。
如果BRDF高度依赖于视图,则最好根据BRDF选择一个点,而不是灯光上的随机点。但是我们如何选择呢?基于光还是基于BRDF的样品?
为什么不兼得?输入多个重要性采样。简而言之,我们评估多次,使用不同的采样技术,然后使用基于其pdf的权重将它们平均在一起。在代码中,这是:
float3 EstimateDirect(Light *light, UniformSampler *sampler, SurfaceInteraction &interaction, BSDF *bsdf) const {
float3 directLighting = float3(0.0f);
float3 f;
float lightPdf, scatteringPdf;
// Sample lighting with multiple importance sampling
// Only sample if the BRDF is non-specular
if ((bsdf->SupportedLobes & ~BSDFLobe::Specular) != 0) {
float3 Li = light->SampleLi(sampler, m_scene, interaction, &lightPdf);
// Make sure the pdf isn't zero and the radiance isn't black
if (lightPdf != 0.0f && !all(Li)) {
// Calculate the brdf value
f = bsdf->Eval(interaction);
scatteringPdf = bsdf->Pdf(interaction);
if (scatteringPdf != 0.0f && !all(f)) {
float weight = PowerHeuristic(1, lightPdf, 1, scatteringPdf);
directLighting += f * Li * weight / lightPdf;
}
}
}
// Sample brdf with multiple importance sampling
bsdf->Sample(interaction, sampler);
f = bsdf->Eval(interaction);
scatteringPdf = bsdf->Pdf(interaction);
if (scatteringPdf != 0.0f && !all(f)) {
lightPdf = light->PdfLi(m_scene, interaction);
if (lightPdf == 0.0f) {
// We didn't hit anything, so ignore the brdf sample
return directLighting;
}
float weight = PowerHeuristic(1, scatteringPdf, 1, lightPdf);
float3 Li = light->Le();
directLighting += f * Li * weight / scatteringPdf;
}
return directLighting;
}
用英语讲:
。
inline float PowerHeuristic(uint numf, float fPdf, uint numg, float gPdf) {
float f = numf * fPdf;
float g = numg * gPdf;
return (f * f) / (f * f + g * g);
}
您可以对这些功能进行许多优化/改进,但我已对其进行了简化,以使它们更易于理解。如果您愿意,我可以分享其中的一些改进。
在SampleLights()中,我们遍历所有灯光,并获得它们的贡献。对于少量的灯,这很好,但是对于成百上千的灯,则变得昂贵。幸运的是,我们可以利用蒙特卡洛积分是一个巨大的平均值的事实。例:
让我们定义
目前,我们通过以下方式估算:
但是,计算和都很昂贵,所以我们这样做:
其中是统一随机变量,而定义为:
在这种情况下,因为pdf必须集成为1,并且有2种功能可供选择。
用英语讲:
随着N变大,估计将收敛到正确的解。
我们可以将相同的原理应用于光采样。而不是对每个光源进行采样,我们随机选择一个光源,然后将结果乘以光源数量(这与除以pdf分数相同):
float3 SampleOneLight(UniformSampler *sampler, SurfaceInteraction interaction, BSDF *bsdf, Light *hitLight) const {
std::size_t numLights = m_scene->NumLights();
// Return black if there are no lights
// And don't let a light contribute light to itself
// Aka, if we hit a light
// This is the special case where there is only 1 light
if (numLights == 0 || numLights == 1 && hitLight != nullptr) {
return float3(0.0f);
}
// Don't let a light contribute light to itself
// Choose another one
Light *light;
do {
light = m_scene->RandomOneLight(sampler);
} while (light == hitLight);
return numLights * EstimateDirect(light, sampler, interaction, bsdf);
}
在此代码中,所有灯光均具有平等的机会被拾取。但是,如果愿意,我们可以重视样本。例如,我们可以为较大的灯光提供更高的被拾取机会,或者使灯光更靠近命中表面。您只需要将结果除以pdf,就不再是。
当前的代码仅根据BSDF对“ New Ray”方向进行采样。如果我们还希望根据灯光的位置来重视样本怎么办?
从上面我们学到的知识中,一种方法是根据它们的pdf 来拍摄两条 “新”射线并权重。但是,这既计算量大,又难以递归实现。
为了克服这个问题,我们可以采用仅采样一盏灯所学到的相同原理。也就是说,随机选择一个样本,然后除以选择样本的pdf。
// Get the new ray direction
// Randomly (uniform) choose whether to sample based on the BSDF or the Lights
float p = sampler->NextFloat();
Light *light = m_scene->RandomLight();
if (p < 0.5f) {
// Choose the direction based on the bsdf
material->bsdf->Sample(interaction, sampler);
float bsdfPdf = material->bsdf->Pdf(interaction);
float lightPdf = light->PdfLi(m_scene, interaction);
float weight = PowerHeuristic(1, bsdfPdf, 1, lightPdf);
// Accumulate the throughput
throughput = throughput * weight * material->bsdf->Eval(interaction) / bsdfPdf;
} else {
// Choose the direction based on a light
float lightPdf;
light->SampleLi(sampler, m_scene, interaction, &lightPdf);
float bsdfPdf = material->bsdf->Pdf(interaction);
float weight = PowerHeuristic(1, lightPdf, 1, bsdfPdf);
// Accumulate the throughput
throughput = throughput * weight * material->bsdf->Eval(interaction) / lightPdf;
}
话虽如此,我们真的要重视根据光对“ New Ray”方向进行采样吗?对于直接照明,光能传递受表面的BSDF和光的方向的影响。但是对于间接照明,光能传递几乎完全由之前命中的表面的BSDF定义。因此,增加重要度抽样并不能给我们任何好处。
因此,通常仅使用BSDF对“新方向”进行重要性采样,而对直接照明应用多重重要性采样。