Qu'est-ce que l'importance d'échantillonnage?

33

Qu'est-ce que l'échantillonnage d'importance? Chaque article que j'ai lu parle de «PDF», qu'est-ce que c'est aussi?

D'après ce que je comprends, l'échantillonnage en importance est une technique qui consiste à n'échantillonner que les zones d'un hémisphère plus importantes que d'autres. Donc, idéalement, je devrais échantillonner les rayons vers les sources de lumière pour réduire le bruit et augmenter la vitesse. En outre, certaines BRDF à des angles de pâturage ont peu ou pas de différence dans le calcul, alors utilisez-vous un échantillonnage par importance pour éviter cela?

Si je devais mettre en œuvre un échantillonnage d'importance pour une BRDF Cook-Torrance, comment pourrais-je procéder?

Arjan Singh
la source
C'est un bon lien qui explique ce qu'est un PDF. TL; DR un PDF est une fonction qui décrit la probabilité de nombres aléatoires (continus ou flottants). Générer des nombres aléatoires à partir d'un fichier PDF spécifique peut être difficile et il existe quelques techniques pour le faire. Cela parle de l'un d'entre eux. L'article après celui-ci parle d'une autre manière. blog.demofox.org/2017/08/05/…
Alan Wolfe

Réponses:

51

Réponse courte:

Échantillonnage d'importance est une méthode pour réduire la variance dans l'intégration de Monte Carlo en choisissant un estimateur proche de la forme de la fonction réelle.

PDF est l'abréviation de Probability Density Function . A pdf(x) donne la probabilité qu'un échantillon aléatoire soit généré x .

Longue réponse:

Pour commencer, examinons ce qu'est l'intégration de Monte Carlo et à quoi elle ressemble mathématiquement.

L’intégration de Monte Carlo est une technique permettant d’estimer la valeur d’une intégrale. Il est généralement utilisé lorsqu'il n'y a pas de solution de forme fermée à l'intégrale. Cela ressemble à ceci:

f(x)dx1Ni=1Nf(xi)pdf(xi)

En anglais, cela signifie que vous pouvez approximer une intégrale en faisant la moyenne des échantillons aléatoires successifs de la fonction. Lorsque N devient grand, l’approximation se rapproche de plus en plus de la solution. pdf(xi) représente la fonction de densité de probabilité de chaque échantillon aléatoire.

Faisons un exemple: Calculer la valeur de l'intégrale I .

I=02πexsin(x)dx

Utilisons Monte Carlo Integration:

I1Ni=1Nexsin(xi)pdf(xi)

Un programme python simple pour calculer ceci est:

import random
import math

N = 200000
TwoPi = 2.0 * math.pi

sum = 0.0

for i in range(N):
    x = random.uniform(0, TwoPi)

    fx = math.exp(-x) * math.sin(x)
    pdf = 1 / (TwoPi - 0.0)

    sum += fx / pdf

I = (1 / N) * sum
print(I)

Si on lance le programme on obtient I=0.4986941

En utilisant la séparation par pièces, nous pouvons obtenir la solution exacte:

I=12(1e2π)=0.4990663

NN=2000

[0,2π]pdf(x)=1/(2π0)

L’échantillonnage d’importance ne fonctionne pas n'échantillant uniformément. Au lieu de cela, nous essayons de choisir plus d’échantillons qui contribuent beaucoup au résultat (important) et moins d’échantillons qui contribuent peu au résultat (moins important). D'où le nom, l'importance d'échantillonnage.

ffComparison of good sampling vs bad sampling

Un exemple d’échantillonnage d’importance dans le suivi de trajectoire est la manière de choisir la direction d’un rayon après qu’il atteigne une surface. Si la surface n'est pas parfaitement spéculaire (miroir ou verre), le rayon sortant peut se trouver n'importe où dans l'hémisphère.

Outgoing rays can go anywhere in the hemisphere

Nous pourrions échantillonner uniformément l'hémisphère pour générer le nouveau rayon. Cependant, nous pouvons exploiter le fait que l’équation de rendu a un facteur de cosinus:

Lo(p,ωo)=Le(p,ωo)+ΩF(p,ωje,ωo)Lje(p,ωje)|cosθje|ωje

Plus précisément, nous savons que tous les rayons à l’horizon seront fortement atténués (en particulier, cos(X)). Ainsi, les rayons générés près de l'horizon ne contribueront pas beaucoup à la valeur finale.

To combat this, we use importance sampling. If we generate rays according to a cosine weighted hemisphere, we ensure that more rays are generated well above the horizon, and less near the horizon. This will lower variance and reduce noise.

In your case, you specified that you will be using a Cook-Torrance, microfacet-based BRDF. The common form being:

f(p,ωi,ωo)=F(ωi,h)G(ωi,ωo,h)D(h)4cos(θi)cos(θo)

where

F(ωi,h)=Fresnel functionG(ωi,ωo,h)=Geometry Masking and Shadowing functionD(h)=Normal Distribution Function

The blog "A Graphic's Guy's Note" has an excellent write up on how to sample Cook-Torrance BRDFs. I will refer you to his blog post. That said, I will try to create a brief overview below:

The NDF is generally the dominant portion of the Cook-Torrance BRDF, so if we are going to importance sample, the we should sample based on the NDF.

Cook-Torrance doesn't specify a specific NDF to use; we are free to choose whichever one suits our fancy. That said, there are a few popular NDFs:

  • GGX
  • Beckmann
  • Blinn

Each NDF has it's own formula, thus each must be sampled differently. I am only going to show the final sampling function for each. If you would like to see how the formula is derived, see the blog post.

GGX is defined as:

DGGX(m)=α2π((α21)cos2(θ)+1)2

To sample the spherical coordinates angle θ, we can use the formula:

θ=arccos(α2ξ1(α21)+1)

where ξ is a uniform random variable.

We assume that the NDF is isotropic, so we can sample ϕ uniformly:

ϕ=ξ2

Beckmann is defined as:

DBeckmann(m)=1πα2cos4(θ)etan2(θ)α2

Which can be sampled with:

θ=arccos(11=α2ln(1ξ1))ϕ=ξ2

Lastly, Blinn is defined as:

DBlinn(m)=α+22π(cos(θ))α

Which can be sampled with:

θ=arccos(1ξ1α+1)ϕ=ξ2

Putting it in Practice

Let's look at a basic backwards path tracer:

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);

    // Bounce the ray around the scene
    for (uint bounces = 0; bounces < 10; ++bounces) {
        m_scene->Intersect(ray);

        // The ray missed. Return the background color
        if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
            color += throughput * float3(0.846f, 0.933f, 0.949f);
            break;
        }

        // We hit an object

        // 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 emmisive light
        if (light != nullptr) {
            color += throughput * light->Le();
        }

        float3 normal = normalize(ray.Ng);
        float3 wo = normalize(-ray.dir);
        float3 surfacePos = ray.org + ray.dir * ray.tfar;

        // Get the new ray direction
        // Choose the direction based on the material
        float3 wi = material->Sample(wo, normal, sampler);
        float pdf = material->Pdf(wi, normal);

        // Accumulate the brdf attenuation
        throughput = throughput * material->Eval(wi, wo, normal) / pdf;


        // Shoot a new ray

        // Set the origin at the intersection point
        ray.org = surfacePos;

        // Reset the other ray properties
        ray.dir = wi;
        ray.tnear = 0.001f;
        ray.tfar = embree::inf;
        ray.geomID = RTC_INVALID_GEOMETRY_ID;
        ray.primID = RTC_INVALID_GEOMETRY_ID;
        ray.instID = RTC_INVALID_GEOMETRY_ID;
        ray.mask = 0xFFFFFFFF;
        ray.time = 0.0f;
    }

    m_scene->Camera.FrameBuffer.SplatPixel(x, y, color);
}

IE. we bounce around the scene, accumulating color and light attenuation as we go. At each bounce, we have to choose a new direction for the ray. As mentioned above, we could uniformly sample the hemisphere to generate the new ray. However, the code is smarter; it importance samples the new direction based on the BRDF. (Note: This is the input direction, because we are a backwards path tracer)

// Get the new ray direction
// Choose the direction based on the material
float3 wi = material->Sample(wo, normal, sampler);
float pdf = material->Pdf(wi, normal);

Which could be implemented as:

void LambertBRDF::Sample(float3 outputDirection, float3 normal, UniformSampler *sampler) {
    float rand = sampler->NextFloat();
    float r = std::sqrtf(rand);
    float theta = sampler->NextFloat() * 2.0f * M_PI;

    float x = r * std::cosf(theta);
    float y = r * std::sinf(theta);

    // Project z up to the unit hemisphere
    float z = std::sqrtf(1.0f - x * x - y * y);

    return normalize(TransformToWorld(x, y, z, normal));
}

float3a TransformToWorld(float x, float y, float z, float3a &normal) {
    // Find an axis that is not parallel to normal
    float3a majorAxis;
    if (abs(normal.x) < 0.57735026919f /* 1 / sqrt(3) */) {
        majorAxis = float3a(1, 0, 0);
    } else if (abs(normal.y) < 0.57735026919f /* 1 / sqrt(3) */) {
        majorAxis = float3a(0, 1, 0);
    } else {
        majorAxis = float3a(0, 0, 1);
    }

    // Use majorAxis to create a coordinate system relative to world space
    float3a u = normalize(cross(normal, majorAxis));
    float3a v = cross(normal, u);
    float3a w = normal;


    // Transform from local coordinates to world coordinates
    return u * x +
           v * y +
           w * z;
}

float LambertBRDF::Pdf(float3 inputDirection, float3 normal) {
    return dot(inputDirection, normal) * M_1_PI;
}

After we sample the inputDirection ('wi' in the code), we use that to calculate the value of the BRDF. And then we divide by the pdf as per the Monte Carlo formula:

// Accumulate the brdf attenuation
throughput = throughput * material->Eval(wi, wo, normal) / pdf;

Where Eval() is just the BRDF function itself (Lambert, Blinn-Phong, Cook-Torrance, etc.):

float3 LambertBRDF::Eval(float3 inputDirection, float3 outputDirection, float3 normal) const override {
    return m_albedo * M_1_PI * dot(inputDirection, normal);
}
RichieSams
la source
Nice answer. The OP also asked about Cook-Torrance importance sampling which this answer doesn't touch upon.
PeteUK
6
I updated the answer to add a section about Cook-Torrance
RichieSams
For example GGX, to sample spherical coordinates angle cos(θ) we use the importance sampled formula to calculate the angle and use that in GGX as usual right? Or does the formula replace GGX entirely?
Arjan Singh
3
I added a section to help answer your questions. But, in short, your first method is correct. You use the sampling formula to generate a direction, then you use that new direction in the normal GGX formula and to get the pdf for the Monte Carlo formula.
RichieSams
For GGX how would I calculate/sample wi? I understand how to sample the spherical coordinates angle θ but for the actual direction vector how is that done?
Arjan Singh
11

If you have a 1D function f(x) and you want to integrate this function from say 0 to 1, one way to perform this integration is by taking N random samples in range [0, 1], evaluate f(x) for each sample and calculate the average of the samples. However, this "naive" Monte Carlo integration is said to "converge slowly", i.e. you need a large number of samples to get close to the ground truth, particularly if the function has high frequencies.

With importance sampling, instead of taking N random samples in [0, 1] range, you take more samples in the "important" regions of f(x) that contribute most to the final result. However, because you bias sampling towards the important regions of the function, these samples must be weighted less to counter the bias, which is where the PDF (probability density function) comes along. PDF tells the probability of a sample at given position and is used to calculate weighted average of the samples by dividing the each sample with the PDF value at each sample position.

With Cook-Torrance importance sampling the common practice is to distribute samples based on the normal distribution function NDF. If NDF is already normalized, it can serve directly as PDF, which is convenient since it cancels the term out from the BRDF evaluation. Only thing you need to do then is to distribute sample positions based on PDF and evaluate BRDF without the NDF term, i.e.

f=FGπ(nωi)(nωo)
And calculate average of the sample results multiplied by the solid angle of the domain you integrate over (e.g. 2π for hemisphere).

For NDF you need to calculate Cumulative Distribution Function of the PDF to convert uniformly distributed sample position to PDF weighted sample position. For isotropic NDF this simplifies to 1D function because of the symmetry of the function. For more details about the CDF derivation you can check this old GPU Gems article.

JarkkoL
la source