Blender V5.3
cycles_precompute.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <map>
6
7#include "util/string.h"
8
9#include "util/array.h"
10#include "util/hash.h"
11#include "util/tbb.h"
12
15
16#include <iostream>
17
19
20static float precompute_ggx_E(const float rough, const float mu, const float3 rand)
21{
22 MicrofacetBsdf bsdf;
23 bsdf.weight = one_float3();
24 bsdf.sample_weight = 1.0f;
25 bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
26 bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
27 bsdf.ior = 1.0f;
28 bsdf.T = make_float3(1.0f, 0.0f, 0.0f);
29
31
32 float3 omega_in;
33 Spectrum eval;
34 float pdf = 0.0f;
35 float sampled_eta;
36 float2 sampled_roughness;
38 (ShaderClosure *)&bsdf,
39 make_float3(0.0f, 0.0f, 1.0f),
40 make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
41 rand,
42 &eval,
43 &omega_in,
44 &pdf,
45 &sampled_roughness,
46 &sampled_eta);
47 if (pdf != 0.0f) {
48 return average(eval) / pdf;
49 }
50 return 0.0f;
51}
52
53static float precompute_ggx_glass_E(const float rough,
54 const float mu,
55 const float eta,
56 const float3 rand)
57{
58 MicrofacetBsdf bsdf;
59 bsdf.weight = one_float3();
60 bsdf.sample_weight = 1.0f;
61 bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
62 bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
63 bsdf.ior = eta;
64 bsdf.T = make_float3(1.0f, 0.0f, 0.0f);
65
67
68 float3 omega_in;
69 Spectrum eval;
70 float pdf = 0.0f;
71 float sampled_eta;
72 float2 sampled_roughness;
74 (ShaderClosure *)&bsdf,
75 make_float3(0.0f, 0.0f, 1.0f),
76 make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
77 rand,
78 &eval,
79 &omega_in,
80 &pdf,
81 &sampled_roughness,
82 &sampled_eta);
83 if (pdf != 0.0f) {
84 return average(eval) / pdf;
85 }
86 return 0.0f;
87}
88
90 const float rough, const float mu, const float eta, const float exponent, const float3 rand)
91{
92 MicrofacetBsdf bsdf;
93 bsdf.weight = one_float3();
94 bsdf.sample_weight = 1.0f;
95 bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
96 bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
97 bsdf.ior = eta;
98 bsdf.T = make_float3(1.0f, 0.0f, 0.0f);
99
101
103 fresnel.tint = {one_float3(), one_float3()};
104 fresnel.f0 = make_float3(0.0f, 1.0f, 0.0f);
105 fresnel.f90 = make_float3(1.0f, 1.0f, 0.0f);
106 fresnel.exponent = exponent;
107
109 bsdf.fresnel = &fresnel;
110
111 float3 omega_in;
112 Spectrum eval;
113 float pdf = 0.0f;
114 float sampled_eta;
115 float2 sampled_roughness;
117 (ShaderClosure *)&bsdf,
118 make_float3(0.0f, 0.0f, 1.0f),
119 make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
120 rand,
121 &eval,
122 &omega_in,
123 &pdf,
124 &sampled_roughness,
125 &sampled_eta);
126 if (pdf != 0.0f) {
127 /* The idea here is that the resulting Fresnel factor is always bounded by
128 * F0..F90, so it's enough to precompute and store the interpolation factor. */
129 return saturatef(eval.x / eval.y);
130 }
131 return 0.0f;
132}
133
134inline float ior_parametrization(const float z)
135{
136 /* This parametrization ensures that the entire [1..inf] range of IORs is covered
137 * and that most precision is allocated to the common areas (1-2). */
138 return ior_from_F0(sqr(sqr(z)));
139}
140
141inline float mu_parametrization(const float y)
142{
143 /* This parametrization increases the resolution at grazing angles, where the Fresnel
144 * can have a significant influence on the directional albedo. */
145 return y * y;
146}
147
150 int nx, ny, nz;
151 std::function<float(float, float, float, float3)> evaluation;
152};
153
154static bool cycles_precompute(std::string name)
155{
156 std::map<string, PrecomputeTerm> precompute_terms;
157 /* Overall albedo of the GGX microfacet BRDF, depending on cosI and roughness. */
158 precompute_terms["ggx_E"] = {1 << 23,
161 1,
162 [](const float rough, const float y, float, const float3 rand) {
163 const float mu = mu_parametrization(y);
164 return precompute_ggx_E(rough, mu, rand);
165 }};
166 /* Overall albedo of the GGX microfacet BRDF, averaged over cosI */
167 precompute_terms["ggx_Eavg"] = {1 << 26,
169 1,
170 1,
171 [](const float rough, const float mu, float, const float3 rand) {
172 return 2.0f * mu * precompute_ggx_E(rough, mu, rand);
173 }};
174 /* Overall albedo of the GGX microfacet BSDF with dielectric Fresnel,
175 * depending on cosI and roughness, for IOR>1. */
176 precompute_terms["ggx_glass_E"] = {
177 1 << 23,
181 [](const float rough, const float y, const float z, const float3 rand) {
182 const float ior = ior_parametrization(z);
183 const float mu = mu_parametrization(y);
184 return precompute_ggx_glass_E(rough, mu, ior, rand);
185 }};
186 /* Overall albedo of the GGX microfacet BSDF with dielectric Fresnel,
187 * averaged over cosI, for IOR>1. */
188 precompute_terms["ggx_glass_Eavg"] = {
189 1 << 26,
191 1,
193 [](const float rough, const float mu, const float z, const float3 rand) {
194 const float ior = ior_parametrization(z);
195 return 2.0f * mu * precompute_ggx_glass_E(rough, mu, ior, rand);
196 }};
197 /* Overall albedo of the GGX microfacet BSDF with dielectric Fresnel,
198 * depending on cosI and roughness, for IOR<1. */
199 precompute_terms["ggx_glass_inv_E"] = {
200 1 << 23,
204 [](const float rough, const float y, const float z, const float3 rand) {
205 const float ior = ior_parametrization(z);
206 const float mu = mu_parametrization(y);
207 return precompute_ggx_glass_E(rough, mu, 1.0f / ior, rand);
208 }};
209 /* Overall albedo of the GGX microfacet BSDF with dielectric Fresnel,
210 * averaged over cosI, for IOR<1. */
211 precompute_terms["ggx_glass_inv_Eavg"] = {
212 1 << 26,
214 1,
216 [](const float rough, const float mu, const float z, const float3 rand) {
217 const float ior = ior_parametrization(z);
218 return 2.0f * mu * precompute_ggx_glass_E(rough, mu, 1.0f / ior, rand);
219 }};
220
221 /* Interpolation factor between F0 and F90 for the generalized Schlick Fresnel,
222 * depending on cosI and roughness, for IOR>1, using dielectric Fresnel mode. */
223 precompute_terms["ggx_gen_schlick_ior_s"] = {
224 1 << 20,
228 [](const float rough, const float y, const float z, const float3 rand) {
229 const float ior = ior_parametrization(z);
230 const float mu = mu_parametrization(y);
231 return precompute_ggx_gen_schlick_s(rough, mu, ior, -1.0f, rand);
232 }};
233
234 /* Interpolation factor between F0 and F90 for the generalized Schlick Fresnel,
235 * depending on cosI and roughness, for IOR>1. */
236 precompute_terms["ggx_gen_schlick_s"] = {
237 1 << 20,
241 [](const float rough, const float y, const float z, const float3 rand) {
242 /* Remap 0..1 to 0..inf, with 0.5 mapping to 5 (the default value). */
243 const float exponent = 5.0f * ((1.0f - z) / z);
244 const float mu = mu_parametrization(y);
245 return precompute_ggx_gen_schlick_s(rough, mu, 1.0f, exponent, rand);
246 }};
247
248 if (!precompute_terms.contains(name)) {
249 return false;
250 }
251
252 const PrecomputeTerm &term = precompute_terms[name];
253
254 const int samples = term.samples;
255 const int nz = term.nz;
256 const int ny = term.ny;
257 const int nx = term.nx;
258
259 std::cout << "static const float table_" << name << "[" << nz * ny * nx << "] = {" << std::endl;
260 for (int z = 0; z < nz; z++) {
261 array<float> data(nx * ny);
262 parallel_for(0, nx * ny, [&](int64_t i) {
263 const int y = i / nx;
264 const int x = i % nx;
265 const uint seed = hash_uint2(x, y);
266 double sum = 0.0;
267 for (int sample = 0; sample < samples; sample++) {
268 const float4 rand = sobol_burley_sample_4D(sample, 0, seed, 0xffffffff);
269
270 const float rough = (nx == 1) ? 0.0f : clamp(float(x) / float(nx - 1), 1e-4f, 1.0f);
271 /* We use 1e-2f as min for mu since to the y*y mu parametrization results in 1e-4f. */
272 const float mu = (ny == 1) ? rand.w : clamp(float(y) / float(ny - 1), 1e-2f, 1.0f);
273 const float ior = (nz == 1) ? 0.0f : clamp(float(z) / float(nz - 1), 1e-4f, 0.99f);
274
275 float value = term.evaluation(rough, mu, ior, make_float3(rand));
276 if (isnan(value)) {
277 value = 0.0f;
278 }
279 sum += (double)value;
280 }
281 data[y * nx + x] = saturatef(float(sum / double(samples)));
282 });
283
284 /* Print data formatted as C++ array */
285 for (int y = 0; y < ny; y++) {
286 std::cout << " ";
287 for (int x = 0; x < nx; x++) {
288 std::cout << std::to_string(data[y * nx + x]);
289 if (x + 1 < nx) {
290 /* Next number will follow in same line */
291 std::cout << "f, ";
292 }
293 else if (y + 1 < ny || z + 1 < nz) {
294 /* Next number will follow in next line */
295 std::cout << "f,";
296 }
297 else {
298 /* No next number */
299 std::cout << "f";
300 }
301 }
302 std::cout << std::endl;
303 }
304 /* If the array is three-dimensional, put an empty line between each slice. */
305 if (ny > 1 && z + 1 < nz) {
306 std::cout << std::endl;
307 }
308 }
309 std::cout << "};" << std::endl;
310
311 return true;
312}
313
315
316int main(const int argc, const char **argv)
317{
318 if (argc < 2) {
319 return 1;
320 }
321 return ccl::cycles_precompute(argv[1]) ? 0 : 1;
322}
@ GENERALIZED_SCHLICK
ccl_device int bsdf_microfacet_ggx_glass_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device int bsdf_microfacet_ggx_sample(KernelGlobals kg, const ccl_private ShaderClosure *sc, const float3 Ng, const float3 wi, const float3 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness, ccl_private float *eta)
ccl_device int bsdf_microfacet_ggx_setup(ccl_private MicrofacetBsdf *bsdf)
long long int int64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define GGX_GLASS_E_RES_MU
Definition constants.h:13
#define GGX_GEN_SCHLICK_IOR_S_RES_IOR
Definition constants.h:22
#define GGX_GEN_SCHLICK_IOR_S_RES_MU
Definition constants.h:21
#define GGX_GEN_SCHLICK_S_RES_MU
Definition constants.h:17
#define GGX_GLASS_E_RES_ROUGH
Definition constants.h:12
#define GGX_E_RES_MU
Definition constants.h:10
#define GGX_GEN_SCHLICK_S_RES_ROUGH
Definition constants.h:16
#define GGX_GLASS_E_RES_IOR
Definition constants.h:14
#define GGX_GEN_SCHLICK_IOR_S_RES_ROUGH
Definition constants.h:20
#define GGX_GEN_SCHLICK_S_RES_IOR
Definition constants.h:18
#define GGX_E_RES_ROUGH
Definition constants.h:9
static float precompute_ggx_glass_E(const float rough, const float mu, const float eta, const float3 rand)
float ior_parametrization(const float z)
static CCL_NAMESPACE_BEGIN float precompute_ggx_E(const float rough, const float mu, const float3 rand)
static bool cycles_precompute(std::string name)
float mu_parametrization(const float y)
static float precompute_ggx_gen_schlick_s(const float rough, const float mu, const float eta, const float exponent, const float3 rand)
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
uint hash_uint2(uint kx, uint ky)
#define main()
#define isnan
constexpr T clamp(T, U, U) RET
float ior_from_F0(const float F0)
float average(float2 a)
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
#define sqr
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_device float4 sobol_burley_sample_4D(uint index, const uint dimension_set, uint seed, const uint shuffled_index_mask)
ccl_private void * fresnel
std::function< float(float, float, float, float3)> evaluation
float w
Definition sky_math.h:225
float3 Spectrum