Blender V5.3
eevee_nodetree_closures_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "gpu_shader_codegen_lib.glsl"
8
9/* The Closure type is never used. Use float as dummy type. */
10#define Closure float
11#define CLOSURE_DEFAULT 0.0f
12
13template<typename T> struct Reservoir {
15 float rand;
18
19 void reset(float rng)
20 {
21 this->rand = rng;
22 this->chosen_weight = 0.0f;
23 this->total_weight = 0.0f;
24 data = T{};
25 }
26
27 void add(T candidate, float candidate_weight)
28 {
29 if (candidate_weight < 1e-5f) {
30 return;
31 }
32 total_weight += candidate_weight;
33
34 float x = candidate_weight / total_weight;
35 bool chosen = (rand < x);
36
37 if (chosen) {
38 data = candidate;
39 chosen_weight = candidate_weight;
40 }
41 /* Assuming that if r is in the interval [0,x] or [x,1], it's still uniformly distributed
42 * within that interval, so remapping to [0,1] again to explore this space of probability. */
43 rand = (chosen) ? (rand / x) : ((rand - x) / (1.0f - x));
44 }
45
46 float get_final_weight() const
47 {
48 if (chosen_weight <= 0.0f) {
49 return 0.0f;
50 }
52 }
53};
54
55template struct Reservoir<ClosureUndetermined>;
56
57ClosureType closure_type_get(ClosureDiffuse /*cl*/)
58{
60}
61
62ClosureType closure_type_get(ClosureTranslucent /*cl*/)
63{
65}
66
67ClosureType closure_type_get(ClosureReflection /*cl*/)
68{
69 return CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID;
70}
71
72ClosureType closure_type_get(ClosureRefraction /*cl*/)
73{
75}
76
77ClosureType closure_type_get(ClosureSubsurface /*cl*/)
78{
80}
81
82ClosureType closure_type_get(ClosureThinRefraction /*cl*/)
83{
85}
86
90void closure_select(Reservoir<ClosureUndetermined> &reservoir, ClosureUndetermined candidate)
91{
92 reservoir.add(candidate, candidate.weight());
93}
void closure_select(Reservoir< ClosureUndetermined > &reservoir, ClosureUndetermined candidate)
ClosureType closure_type_get(ClosureDiffuse)
ClosureType
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSDF_THIN_GLASS_TRANSMISSION_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
#define T
void add(T candidate, float candidate_weight)