Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cycles_precompute.cpp « app « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26be3f16f3add356d16ff3f573dc0f69f0eb2cc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "util/math.h"
#include "util/string.h"
#include "util/system.h"

#include "util/hash.h"
#include "util/task.h"

#include "kernel/device/cpu/compat.h"
#include "kernel/device/cpu/globals.h"

#include "kernel/sample/lcg.h"
#include "kernel/sample/mapping.h"

#include "kernel/closure/bsdf_microfacet.h"
#include "kernel/closure/bsdf_microfacet_glass.h"
#include "kernel/closure/bsdf_principled_sheen.h"

#include <iostream>

CCL_NAMESPACE_BEGIN

/* From PBRT: core/montecarlo.h */
inline float VanDerCorput(uint32_t n, uint32_t scramble)
{
  n = (n << 16) | (n >> 16);
  n = ((n & 0x00ff00ff) << 8) | ((n & 0xff00ff00) >> 8);
  n = ((n & 0x0f0f0f0f) << 4) | ((n & 0xf0f0f0f0) >> 4);
  n = ((n & 0x33333333) << 2) | ((n & 0xcccccccc) >> 2);
  n = ((n & 0x55555555) << 1) | ((n & 0xaaaaaaaa) >> 1);
  n ^= scramble;
  return ((n >> 8) & 0xffffff) / float(1 << 24);
}
inline float Sobol2(uint32_t n, uint32_t scramble)
{
  for (uint32_t v = 1 << 31; n != 0; n >>= 1, v ^= v >> 1)
    if (n & 0x1)
      scramble ^= v;
  return ((scramble >> 8) & 0xffffff) / float(1 << 24);
}

static float precompute_sheen_E(float rough, float mu, float u1, float u2)
{
  PrincipledSheenBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_PRINCIPLED_SHEEN_V2_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.roughness = sqr(rough);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_principled_sheen_sample((ShaderClosure *)&bsdf,
                               make_float3(0.0f, 0.0f, 1.0f),
                               make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                               zero_float3(),
                               zero_float3(),
                               u1,
                               u2,
                               &eval,
                               &omega_in,
                               &domega_in_dx,
                               &domega_in_dy,
                               &pdf);
  if (pdf != 0.0f) {
    return clamp(average(eval) / pdf, 0.0f, 1e5f);
  }
  return 0.0f;
}

static float precompute_clearcoat_E(float rough, float mu, float u1, float u2)
{
  MicrofacetBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_MICROFACET_GGX_CLEARCOAT_V2_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
  bsdf.ior = 1.5f;
  bsdf.extra = nullptr;
  bsdf.T = make_float3(1.0f, 0.0f, 0.0f);

  /* Account for the albedo scaling that the closure performs.
   * Dependency warning - this relies on the ggx_E and ggx_E_avg lookup tables! */
  float E = microfacet_ggx_E(mu, rough), E_avg = microfacet_ggx_E_avg(rough);
  float Fss = dielectric_fresnel_Fss(1.5f);
  float Fms = Fss * E_avg / (1.0f - Fss * (1.0f - E_avg));
  float albedo_scale = 1.0f + Fms * ((1.0f - E) / E);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_microfacet_ggx_sample((ShaderClosure *)&bsdf,
                             make_float3(0.0f, 0.0f, 1.0f),
                             make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                             zero_float3(),
                             zero_float3(),
                             u1,
                             u2,
                             &eval,
                             &omega_in,
                             &domega_in_dx,
                             &domega_in_dy,
                             &pdf);
  if (pdf != 0.0f) {
    /* Encode relative to macrosurface Fresnel, saves resolution.
     * TODO: Worth the extra evaluation? */
    return albedo_scale * (average(eval) / pdf) / fresnel_dielectric_cos(mu, 1.5f);
  }
  return 0.0f;
}

static float precompute_ggx_E(float rough, float mu, float u1, float u2)
{
  MicrofacetBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_MICROFACET_GGX_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
  bsdf.ior = 1.0f;
  bsdf.extra = nullptr;
  bsdf.T = make_float3(1.0f, 0.0f, 0.0f);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_microfacet_ggx_sample((ShaderClosure *)&bsdf,
                             make_float3(0.0f, 0.0f, 1.0f),
                             make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                             zero_float3(),
                             zero_float3(),
                             u1,
                             u2,
                             &eval,
                             &omega_in,
                             &domega_in_dx,
                             &domega_in_dy,
                             &pdf);
  if (pdf != 0.0f) {
    return average(eval) / pdf;
  }
  return 0.0f;
}

static float precompute_ggx_refract_E(float rough, float mu, float eta, float u1, float u2)
{
  MicrofacetBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
  bsdf.ior = eta;
  bsdf.extra = nullptr;
  bsdf.T = make_float3(1.0f, 0.0f, 0.0f);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_microfacet_ggx_sample((ShaderClosure *)&bsdf,
                             make_float3(0.0f, 0.0f, 1.0f),
                             make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                             zero_float3(),
                             zero_float3(),
                             u1,
                             u2,
                             &eval,
                             &omega_in,
                             &domega_in_dx,
                             &domega_in_dy,
                             &pdf);
  if (pdf != 0.0f) {
    return average(eval) / pdf;
  }
  return 0.0f;
}

static float precompute_ggx_glass_E(
    float rough, float mu, float eta, float u1, float u2)
{
  MicrofacetBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
  bsdf.ior = eta;
  bsdf.extra = nullptr;
  bsdf.T = make_float3(1.0f, 0.0f, 0.0f);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_microfacet_ggx_glass_sample((ShaderClosure *)&bsdf,
                                   make_float3(0.0f, 0.0f, 1.0f),
                                   make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                                   zero_float3(),
                                   zero_float3(),
                                   u1,
                                   u2,
                                   &eval,
                                   &omega_in,
                                   &domega_in_dx,
                                   &domega_in_dy,
                                   &pdf);
  if (pdf != 0.0f) {
    return average(eval) / pdf;
  }
  return 0.0f;
}

static float precompute_ggx_dielectric_E(float rough, float mu, float eta, float u1, float u2)
{
  {
    /* Reparametrize based on macrosurface fresnel to get more resolution into areas where
     * the Fresnel curve is rapidly changing. Particularly important for eta<1 due to the TIR edge.
     * However, in the eta<1 case, the entire TIR area would be compressed down to a point, which
     * is an issue since there are changes in that range at higher roughnesses.
     * Therefore, the remapping is blended with the identity function for a compromise.
     */
    float F0 = fresnel_dielectric_cos(1.0f, eta);
    auto get_remap = [eta, F0](float x) {
      return mix(x, inverse_lerp(1.0f, F0, fresnel_dielectric_cos(x, eta)), 0.5f);
    };

    float remap_target = mu;
    float start = 0.0f, end = 1.0f;
    while (end - start > 1e-7f) {
      mu = (end + start) * 0.5f;
      if (get_remap(mu) > remap_target) {
        end = mu;
      }
      else {
        start = mu;
      }
    }
  }

  MicrofacetExtrav2 extra;
  MicrofacetBsdf bsdf;
  bsdf.weight = one_float3();
  bsdf.type = CLOSURE_BSDF_MICROFACET_GGX_FRESNEL_V2_ID;
  bsdf.sample_weight = 1.0f;
  bsdf.N = make_float3(0.0f, 0.0f, 1.0f);
  bsdf.alpha_x = bsdf.alpha_y = sqr(rough);
  bsdf.ior = eta;
  bsdf.extra = (MicrofacetExtra *)&extra;
  bsdf.T = make_float3(1.0f, 0.0f, 0.0f);
  extra.metallic = extra.metal_base = extra.metal_edge_factor = zero_float3();

  /* Dependency warning - this relies on the ggx_E and ggx_E_avg lookup tables! */
  float E = microfacet_ggx_E(mu, rough), E_avg = microfacet_ggx_E_avg(rough);
  float Fss = dielectric_fresnel_Fss(eta);
  float Fms = Fss * E_avg / (1.0f - Fss * (1.0f - E_avg));
  extra.dielectric = 1.0f + Fms * ((1.0f - E) / E);

  float3 eval, omega_in, domega_in_dx, domega_in_dy;
  float pdf = 0.0f;
  bsdf_microfacet_ggx_sample((ShaderClosure *)&bsdf,
                             make_float3(0.0f, 0.0f, 1.0f),
                             make_float3(sqrtf(1.0f - sqr(mu)), 0.0f, mu),
                             zero_float3(),
                             zero_float3(),
                             u1,
                             u2,
                             &eval,
                             &omega_in,
                             &domega_in_dx,
                             &domega_in_dy,
                             &pdf);
  if (pdf != 0.0f) {
    return average(eval) / pdf;
  }
  return 0.0f;
}

struct PrecomputeTerm {
  int dim, samples, res;
  std::function<float(float, float, float, float, float)> evaluation;
};

bool cycles_precompute(std::string name);
bool cycles_precompute(std::string name)
{
  std::map<string, PrecomputeTerm> precompute_terms;
  precompute_terms["sheen_E"] = {
      2, 1 << 23, 32, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_sheen_E(rough, mu, u1, u2);
      }};
  precompute_terms["clearcoat_E"] = {
      2, 1 << 23, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_clearcoat_E(rough, mu, u1, u2);
      }};
  precompute_terms["ggx_E"] = {
      2, 1 << 23, 32, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_E(rough, mu, u1, u2);
      }};
  precompute_terms["ggx_E_avg"] = {
      1, 1 << 23, 32, [](float rough, float mu, float ior, float u1, float u2) {
        return 2.0f * mu * precompute_ggx_E(rough, mu, u1, u2);
      }};
  precompute_terms["ggx_glass_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_glass_E(rough, mu, ior, u1, u2);
      }};
  precompute_terms["ggx_glass_inv_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_glass_E(rough, mu, 1.0f / ior, u1, u2);
      }};
  precompute_terms["ggx_refract_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_refract_E(rough, mu, ior, u1, u2);
      }};
  precompute_terms["ggx_refract_inv_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_refract_E(rough, mu, 1.0f / ior, u1, u2);
      }};
  precompute_terms["ggx_dielectric_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_dielectric_E(rough, mu, ior, u1, u2);
      }};
  // TODO: Consider more X resolution for this table.
  precompute_terms["ggx_dielectric_inv_E"] = {
      3, 1 << 20, 16, [](float rough, float mu, float ior, float u1, float u2) {
        return precompute_ggx_dielectric_E(rough, mu, 1.0f / ior, u1, u2);
      }};

  if (precompute_terms.count(name) == 0) {
    return false;
  }

  const PrecomputeTerm &term = precompute_terms[name];

  const int samples = term.samples;
  const int res = term.res;
  const int nz = (term.dim > 2) ? res : 1, ny = res, nx = (term.dim > 1) ? res : 1;

  if (nz > 1) {
    std::cout << "static const float table_" << name << "[" << nz << "][" << ny << "][" << nx
              << "] = {" << std::endl;
  }
  for (int z = 0; z < nz; z++) {
    float *data = new float[nx * ny];
    parallel_for(0, ny, [&](int64_t y) {
      for (int x = 0; x < nx; x++) {
        uint rng = hash_uint2(x, y);
        uint scramble1 = lcg_step_uint(&rng), scramble2 = lcg_step_uint(&rng);
        double sum = 0.0;
        for (int i = 0; i < samples; i++) {
          float rough = 1.0f - (float(y) + lcg_step_float(&rng)) / float(ny);
          float mu = (float(x) + lcg_step_float(&rng)) / float(nx);
          float ior = (float(z) + lcg_step_float(&rng)) / float(nz);
          /* Encode IOR remapped as sqrt(0.5*(IOR-1)) for more resolution at the start, where most
           * of the changes happen (also places the most common range around 1.5 in the center) */
          ior = 1.0f + 2.0f * sqr(ior);
          float u1 = VanDerCorput(i, scramble1);
          float u2 = Sobol2(i, scramble2);

          float value = term.evaluation(rough, mu, ior, u1, u2);
          if (isnan(value)) {
            value = 0.0f;
          }
          sum += (double)value;
        }
        data[y * nx + x] = float(sum / double(samples));
      }
    });

    string filename = name;
    if (nz > 1) {
      filename += string_printf("_%02d", z);
      std::cout << "  {" << std::endl;
    }
    else {
      std::cout << "static const float table_" << name << "[" << ny << "][" << nx << "] = {"
                << std::endl;
    }

    for (int y = 0; y < ny; y++) {
      std::cout << "    {";
      for (int x = 0; x < nx; x++) {
        std::cout << data[y * nx + x] << ((x + 1 == nx) ? "f" : "f, ");
      }
      std::cout << ((y + 1 == ny) ? "}" : "},") << std::endl;
    }
    if (nz > 1) {
      std::cout << ((z + 1 == nz) ? "  }" : "  },") << std::endl;
    }
    else {
      std::cout << "};" << std::endl;
    }

    FILE *f = fopen((filename + ".pfm").c_str(), "w");
    fprintf(f, "Pf\n%d %d\n-1.0\n", nx, ny);
    fwrite(data, sizeof(float), nx * ny, f);
    fclose(f);
  }
  if (nz > 1) {
    std::cout << "};" << std::endl;
  }

  return true;
}

CCL_NAMESPACE_END