The same random number was used when sampling from the volume segment and from the direct scattering position, causing correlation issues with light tree. To solve this problem, we ensure the same light is picked for volume segment/direct scattering, equiangular/distance sampling by sampling the light tree only once in volume segment. From the direct scattering position in volume, we sample a position on the picked light as usual. If sampling from the light tree fails, we continue with indirect scattering. For unbiased MIS weight for forward sampling, we retrieve the `P`, `D` and `t` used in volume segment for traversing the light tree. The main changes are: 1. `light_tree_sample()` and `light_distribution_sample()` now only pick lights. Sampling a position on light is done separately via `light_sample()`. 2. `light_tree_sample()` is now only called only once from volume segment. For direct lighting we call `light_sample()`. 3. `light_tree_pdf()` now has a template `<in_volume_segment>`. 4. A new field `emitter_id` is added to struct `LightSample`, which just stores the picked emitter index. 5. Additional field `previous_dt = ray->tmax - ray->tmin` is added to `state->ray`, because we need this quantity for computing the pdf. 6. Distant/Background lights are also picked by light tree in volume segment now, because we have no way to pick them afterwards. The direct sample event for these lights will be handled by `VOLUME_SAMPLE_DISTANCE`. 7. Original paper suggests to use the maximal importance, this results in very poor sampling probability for distant and point lights therefore excessive noise. We have a minimal importance for surface to balance, we could do the same for volume but I do not want to spend much time on this now. Just doing `min_importance = 0.0f` seems to do the job okayish. This way we still won't sample the light with zero `max_importance`. The current solution might perform worse with distance sampling, because the light tree measure is biased towards equiangular sampling. However, it is difficult to perform MIS between equiangular and distance sampling if different lights are picked for each method. This is something we can look into in the future if proved to be a serious regression. Pull Request: https://projects.blender.org/blender/blender/pulls/119389
60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
|
|
|
#pragma once
|
|
|
|
#include "kernel/light/light.h"
|
|
#include "kernel/light/triangle.h"
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
/* Simple CDF based sampling over all lights in the scene, without taking into
|
|
* account shading position or normal. */
|
|
|
|
ccl_device int light_distribution_sample(KernelGlobals kg, const float rand)
|
|
{
|
|
/* This is basically std::upper_bound as used by PBRT, to find a point light or
|
|
* triangle to emit from, proportional to area. a good improvement would be to
|
|
* also sample proportional to power, though it's not so well defined with
|
|
* arbitrary shaders. */
|
|
int first = 0;
|
|
int len = kernel_data.integrator.num_distribution + 1;
|
|
|
|
do {
|
|
int half_len = len >> 1;
|
|
int middle = first + half_len;
|
|
|
|
if (rand < kernel_data_fetch(light_distribution, middle).totarea) {
|
|
len = half_len;
|
|
}
|
|
else {
|
|
first = middle + 1;
|
|
len = len - half_len - 1;
|
|
}
|
|
} while (len > 0);
|
|
|
|
/* Clamping should not be needed but float rounding errors seem to
|
|
* make this fail on rare occasions. */
|
|
int index = clamp(first - 1, 0, kernel_data.integrator.num_distribution - 1);
|
|
|
|
return index;
|
|
}
|
|
|
|
ccl_device_noinline bool light_distribution_sample(KernelGlobals kg,
|
|
const float rand,
|
|
ccl_private LightSample *ls)
|
|
{
|
|
/* Sample light index from distribution. */
|
|
ls->emitter_id = light_distribution_sample(kg, rand);
|
|
ls->pdf_selection = kernel_data.integrator.distribution_pdf_lights;
|
|
return true;
|
|
}
|
|
|
|
ccl_device_inline float light_distribution_pdf_lamp(KernelGlobals kg)
|
|
{
|
|
return kernel_data.integrator.distribution_pdf_lights;
|
|
}
|
|
|
|
CCL_NAMESPACE_END
|