Blender  V2.93
kernel_volume.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 /* Ignore paths that have volume throughput below this value, to avoid unnecessary work
20  * and precision issues.
21  * todo: this value could be tweaked or turned into a probability to avoid unnecessary
22  * work in volumes and subsurface scattering. */
23 #define VOLUME_THROUGHPUT_EPSILON 1e-6f
24 
25 /* Events for probalistic scattering */
26 
27 typedef enum VolumeIntegrateResult {
32 
33 /* Volume shader properties
34  *
35  * extinction coefficient = absorption coefficient + scattering coefficient
36  * sigma_t = sigma_a + sigma_s */
37 
38 typedef struct VolumeShaderCoefficients {
43 
44 #ifdef __VOLUME__
45 
46 /* evaluate shader to get extinction coefficient at P */
47 ccl_device_inline bool volume_shader_extinction_sample(KernelGlobals *kg,
48  ShaderData *sd,
50  float3 P,
51  float3 *extinction)
52 {
53  sd->P = P;
54  shader_eval_volume(kg, sd, state, state->volume_stack, PATH_RAY_SHADOW);
55 
56  if (sd->flag & SD_EXTINCTION) {
57  const float density = object_volume_density(kg, sd->object);
58  *extinction = sd->closure_transparent_extinction * density;
59  return true;
60  }
61  else {
62  return false;
63  }
64 }
65 
66 /* evaluate shader to get absorption, scattering and emission at P */
67 ccl_device_inline bool volume_shader_sample(KernelGlobals *kg,
68  ShaderData *sd,
70  float3 P,
72 {
73  sd->P = P;
74  shader_eval_volume(kg, sd, state, state->volume_stack, state->flag);
75 
76  if (!(sd->flag & (SD_EXTINCTION | SD_SCATTER | SD_EMISSION)))
77  return false;
78 
79  coeff->sigma_s = zero_float3();
80  coeff->sigma_t = (sd->flag & SD_EXTINCTION) ? sd->closure_transparent_extinction : zero_float3();
81  coeff->emission = (sd->flag & SD_EMISSION) ? sd->closure_emission_background : zero_float3();
82 
83  if (sd->flag & SD_SCATTER) {
84  for (int i = 0; i < sd->num_closure; i++) {
85  const ShaderClosure *sc = &sd->closure[i];
86 
87  if (CLOSURE_IS_VOLUME(sc->type))
88  coeff->sigma_s += sc->weight;
89  }
90  }
91 
92  const float density = object_volume_density(kg, sd->object);
93  coeff->sigma_s *= density;
94  coeff->sigma_t *= density;
95  coeff->emission *= density;
96 
97  return true;
98 }
99 
100 #endif /* __VOLUME__ */
101 
103 {
104  return exp3(-sigma * t);
105 }
106 
107 ccl_device float kernel_volume_channel_get(float3 value, int channel)
108 {
109  return (channel == 0) ? value.x : ((channel == 1) ? value.y : value.z);
110 }
111 
112 #ifdef __VOLUME__
113 
114 ccl_device float volume_stack_step_size(KernelGlobals *kg, ccl_addr_space VolumeStack *stack)
115 {
116  float step_size = FLT_MAX;
117 
118  for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
119  int shader_flag = kernel_tex_fetch(__shaders, (stack[i].shader & SHADER_MASK)).flags;
120 
121  bool heterogeneous = false;
122 
123  if (shader_flag & SD_HETEROGENEOUS_VOLUME) {
124  heterogeneous = true;
125  }
126  else if (shader_flag & SD_NEED_VOLUME_ATTRIBUTES) {
127  /* We want to render world or objects without any volume grids
128  * as homogeneous, but can only verify this at run-time since other
129  * heterogeneous volume objects may be using the same shader. */
130  int object = stack[i].object;
131  if (object != OBJECT_NONE) {
132  int object_flag = kernel_tex_fetch(__object_flag, object);
133  if (object_flag & SD_OBJECT_HAS_VOLUME_ATTRIBUTES) {
134  heterogeneous = true;
135  }
136  }
137  }
138 
139  if (heterogeneous) {
140  float object_step_size = object_volume_step_size(kg, stack[i].object);
141  object_step_size *= kernel_data.integrator.volume_step_rate;
142  step_size = fminf(object_step_size, step_size);
143  }
144  }
145 
146  return step_size;
147 }
148 
149 ccl_device int volume_stack_sampling_method(KernelGlobals *kg, VolumeStack *stack)
150 {
151  if (kernel_data.integrator.num_all_lights == 0)
152  return 0;
153 
154  int method = -1;
155 
156  for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
157  int shader_flag = kernel_tex_fetch(__shaders, (stack[i].shader & SHADER_MASK)).flags;
158 
159  if (shader_flag & SD_VOLUME_MIS) {
160  return SD_VOLUME_MIS;
161  }
162  else if (shader_flag & SD_VOLUME_EQUIANGULAR) {
163  if (method == 0)
164  return SD_VOLUME_MIS;
165 
166  method = SD_VOLUME_EQUIANGULAR;
167  }
168  else {
169  if (method == SD_VOLUME_EQUIANGULAR)
170  return SD_VOLUME_MIS;
171 
172  method = 0;
173  }
174  }
175 
176  return method;
177 }
178 
179 ccl_device_inline void kernel_volume_step_init(KernelGlobals *kg,
181  const float object_step_size,
182  float t,
183  float *step_size,
184  float *step_shade_offset,
185  float *steps_offset)
186 {
187  const int max_steps = kernel_data.integrator.volume_max_steps;
188  float step = min(object_step_size, t);
189 
190  /* compute exact steps in advance for malloc */
191  if (t > max_steps * step) {
192  step = t / (float)max_steps;
193  }
194 
195  *step_size = step;
196 
197  /* Perform shading at this offset within a step, to integrate over
198  * over the entire step segment. */
199  *step_shade_offset = path_state_rng_1D_hash(kg, state, 0x1e31d8a4);
200 
201  /* Shift starting point of all segment by this random amount to avoid
202  * banding artifacts from the volume bounding shape. */
203  *steps_offset = path_state_rng_1D_hash(kg, state, 0x3d22c7b3);
204 }
205 
206 /* Volume Shadows
207  *
208  * These functions are used to attenuate shadow rays to lights. Both absorption
209  * and scattering will block light, represented by the extinction coefficient. */
210 
211 /* homogeneous volume: assume shader evaluation at the starts gives
212  * the extinction coefficient for the entire line segment */
213 ccl_device void kernel_volume_shadow_homogeneous(KernelGlobals *kg,
215  Ray *ray,
216  ShaderData *sd,
217  float3 *throughput)
218 {
219  float3 sigma_t = zero_float3();
220 
221  if (volume_shader_extinction_sample(kg, sd, state, ray->P, &sigma_t))
222  *throughput *= volume_color_transmittance(sigma_t, ray->t);
223 }
224 
225 /* heterogeneous volume: integrate stepping through the volume until we
226  * reach the end, get absorbed entirely, or run out of iterations */
227 ccl_device void kernel_volume_shadow_heterogeneous(KernelGlobals *kg,
229  Ray *ray,
230  ShaderData *sd,
231  float3 *throughput,
232  const float object_step_size)
233 {
234  float3 tp = *throughput;
235 
236  /* Prepare for stepping.
237  * For shadows we do not offset all segments, since the starting point is
238  * already a random distance inside the volume. It also appears to create
239  * banding artifacts for unknown reasons. */
240  int max_steps = kernel_data.integrator.volume_max_steps;
241  float step_size, step_shade_offset, unused;
242  kernel_volume_step_init(
243  kg, state, object_step_size, ray->t, &step_size, &step_shade_offset, &unused);
244  const float steps_offset = 1.0f;
245 
246  /* compute extinction at the start */
247  float t = 0.0f;
248 
249  float3 sum = zero_float3();
250 
251  for (int i = 0; i < max_steps; i++) {
252  /* advance to new position */
253  float new_t = min(ray->t, (i + steps_offset) * step_size);
254  float dt = new_t - t;
255 
256  float3 new_P = ray->P + ray->D * (t + dt * step_shade_offset);
257  float3 sigma_t = zero_float3();
258 
259  /* compute attenuation over segment */
260  if (volume_shader_extinction_sample(kg, sd, state, new_P, &sigma_t)) {
261  /* Compute expf() only for every Nth step, to save some calculations
262  * because exp(a)*exp(b) = exp(a+b), also do a quick VOLUME_THROUGHPUT_EPSILON
263  * check then. */
264  sum += (-sigma_t * dt);
265  if ((i & 0x07) == 0) { /* ToDo: Other interval? */
266  tp = *throughput * exp3(sum);
267 
268  /* stop if nearly all light is blocked */
271  break;
272  }
273  }
274 
275  /* stop if at the end of the volume */
276  t = new_t;
277  if (t == ray->t) {
278  /* Update throughput in case we haven't done it above */
279  tp = *throughput * exp3(sum);
280  break;
281  }
282  }
283 
284  *throughput = tp;
285 }
286 
287 /* get the volume attenuation over line segment defined by ray, with the
288  * assumption that there are no surfaces blocking light between the endpoints */
289 # if defined(__KERNEL_OPTIX__) && defined(__SHADER_RAYTRACE__)
290 ccl_device_inline void kernel_volume_shadow(KernelGlobals *kg,
291  ShaderData *shadow_sd,
293  Ray *ray,
294  float3 *throughput)
295 {
296  optixDirectCall<void>(1, kg, shadow_sd, state, ray, throughput);
297 }
298 extern "C" __device__ void __direct_callable__kernel_volume_shadow(
299 # else
300 ccl_device_noinline void kernel_volume_shadow(
301 # endif
302  KernelGlobals *kg,
303  ShaderData *shadow_sd,
305  Ray *ray,
306  float3 *throughput)
307 {
308  shader_setup_from_volume(kg, shadow_sd, ray);
309 
310  float step_size = volume_stack_step_size(kg, state->volume_stack);
311  if (step_size != FLT_MAX)
312  kernel_volume_shadow_heterogeneous(kg, state, ray, shadow_sd, throughput, step_size);
313  else
314  kernel_volume_shadow_homogeneous(kg, state, ray, shadow_sd, throughput);
315 }
316 
317 #endif /* __VOLUME__ */
318 
319 /* Equi-angular sampling as in:
320  * "Importance Sampling Techniques for Path Tracing in Participating Media" */
321 
322 ccl_device float kernel_volume_equiangular_sample(Ray *ray, float3 light_P, float xi, float *pdf)
323 {
324  float t = ray->t;
325 
326  float delta = dot((light_P - ray->P), ray->D);
327  float D = safe_sqrtf(len_squared(light_P - ray->P) - delta * delta);
328  if (UNLIKELY(D == 0.0f)) {
329  *pdf = 0.0f;
330  return 0.0f;
331  }
332  float theta_a = -atan2f(delta, D);
333  float theta_b = atan2f(t - delta, D);
334  float t_ = D * tanf((xi * theta_b) + (1 - xi) * theta_a);
335  if (UNLIKELY(theta_b == theta_a)) {
336  *pdf = 0.0f;
337  return 0.0f;
338  }
339  *pdf = D / ((theta_b - theta_a) * (D * D + t_ * t_));
340 
341  return min(t, delta + t_); /* min is only for float precision errors */
342 }
343 
344 ccl_device float kernel_volume_equiangular_pdf(Ray *ray, float3 light_P, float sample_t)
345 {
346  float delta = dot((light_P - ray->P), ray->D);
347  float D = safe_sqrtf(len_squared(light_P - ray->P) - delta * delta);
348  if (UNLIKELY(D == 0.0f)) {
349  return 0.0f;
350  }
351 
352  float t = ray->t;
353  float t_ = sample_t - delta;
354 
355  float theta_a = -atan2f(delta, D);
356  float theta_b = atan2f(t - delta, D);
357  if (UNLIKELY(theta_b == theta_a)) {
358  return 0.0f;
359  }
360 
361  float pdf = D / ((theta_b - theta_a) * (D * D + t_ * t_));
362 
363  return pdf;
364 }
365 
366 /* Distance sampling */
367 
369  float max_t, float3 sigma_t, int channel, float xi, float3 *transmittance, float3 *pdf)
370 {
371  /* xi is [0, 1[ so log(0) should never happen, division by zero is
372  * avoided because sample_sigma_t > 0 when SD_SCATTER is set */
373  float sample_sigma_t = kernel_volume_channel_get(sigma_t, channel);
374  float3 full_transmittance = volume_color_transmittance(sigma_t, max_t);
375  float sample_transmittance = kernel_volume_channel_get(full_transmittance, channel);
376 
377  float sample_t = min(max_t, -logf(1.0f - xi * (1.0f - sample_transmittance)) / sample_sigma_t);
378 
379  *transmittance = volume_color_transmittance(sigma_t, sample_t);
380  *pdf = safe_divide_color(sigma_t * *transmittance, one_float3() - full_transmittance);
381 
382  /* todo: optimization: when taken together with hit/miss decision,
383  * the full_transmittance cancels out drops out and xi does not
384  * need to be remapped */
385 
386  return sample_t;
387 }
388 
389 ccl_device float3 kernel_volume_distance_pdf(float max_t, float3 sigma_t, float sample_t)
390 {
391  float3 full_transmittance = volume_color_transmittance(sigma_t, max_t);
392  float3 transmittance = volume_color_transmittance(sigma_t, sample_t);
393 
394  return safe_divide_color(sigma_t * transmittance, one_float3() - full_transmittance);
395 }
396 
397 /* Emission */
398 
400  int closure_flag,
401  float3 transmittance,
402  float t)
403 {
404  /* integral E * exp(-sigma_t * t) from 0 to t = E * (1 - exp(-sigma_t * t))/sigma_t
405  * this goes to E * t as sigma_t goes to zero
406  *
407  * todo: we should use an epsilon to avoid precision issues near zero sigma_t */
408  float3 emission = coeff->emission;
409 
410  if (closure_flag & SD_EXTINCTION) {
411  float3 sigma_t = coeff->sigma_t;
412 
413  emission.x *= (sigma_t.x > 0.0f) ? (1.0f - transmittance.x) / sigma_t.x : t;
414  emission.y *= (sigma_t.y > 0.0f) ? (1.0f - transmittance.y) / sigma_t.y : t;
415  emission.z *= (sigma_t.z > 0.0f) ? (1.0f - transmittance.z) / sigma_t.z : t;
416  }
417  else
418  emission *= t;
419 
420  return emission;
421 }
422 
423 /* Volume Path */
424 
426  float3 throughput,
427  float rand,
428  float3 *pdf)
429 {
430  /* Sample color channel proportional to throughput and single scattering
431  * albedo, to significantly reduce noise with many bounce, following:
432  *
433  * "Practical and Controllable Subsurface Scattering for Production Path
434  * Tracing". Matt Jen-Yuan Chiang, Peter Kutz, Brent Burley. SIGGRAPH 2016. */
435  float3 weights = fabs(throughput * albedo);
436  float sum_weights = weights.x + weights.y + weights.z;
437  float3 weights_pdf;
438 
439  if (sum_weights > 0.0f) {
440  weights_pdf = weights / sum_weights;
441  }
442  else {
443  weights_pdf = make_float3(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f);
444  }
445 
446  *pdf = weights_pdf;
447 
448  /* OpenCL does not support -> on float3, so don't use pdf->x. */
449  if (rand < weights_pdf.x) {
450  return 0;
451  }
452  else if (rand < weights_pdf.x + weights_pdf.y) {
453  return 1;
454  }
455  else {
456  return 2;
457  }
458 }
459 
460 #ifdef __VOLUME__
461 
462 /* homogeneous volume: assume shader evaluation at the start gives
463  * the volume shading coefficient for the entire line segment */
465 kernel_volume_integrate_homogeneous(KernelGlobals *kg,
467  Ray *ray,
468  ShaderData *sd,
469  PathRadiance *L,
470  ccl_addr_space float3 *throughput,
471  bool probalistic_scatter)
472 {
474 
475  if (!volume_shader_sample(kg, sd, state, ray->P, &coeff))
476  return VOLUME_PATH_MISSED;
477 
478  int closure_flag = sd->flag;
479  float t = ray->t;
480  float3 new_tp;
481 
482 # ifdef __VOLUME_SCATTER__
483  /* randomly scatter, and if we do t is shortened */
484  if (closure_flag & SD_SCATTER) {
485  /* Sample channel, use MIS with balance heuristic. */
486  float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
487  float3 albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
488  float3 channel_pdf;
489  int channel = kernel_volume_sample_channel(albedo, *throughput, rphase, &channel_pdf);
490 
491  /* decide if we will hit or miss */
492  bool scatter = true;
494 
495  if (probalistic_scatter) {
496  float sample_sigma_t = kernel_volume_channel_get(coeff.sigma_t, channel);
497  float sample_transmittance = expf(-sample_sigma_t * t);
498 
499  if (1.0f - xi >= sample_transmittance) {
500  scatter = true;
501 
502  /* rescale random number so we can reuse it */
503  xi = 1.0f - (1.0f - xi - sample_transmittance) / (1.0f - sample_transmittance);
504  }
505  else
506  scatter = false;
507  }
508 
509  if (scatter) {
510  /* scattering */
511  float3 pdf;
512  float3 transmittance;
513  float sample_t;
514 
515  /* distance sampling */
517  ray->t, coeff.sigma_t, channel, xi, &transmittance, &pdf);
518 
519  /* modify pdf for hit/miss decision */
520  if (probalistic_scatter)
521  pdf *= one_float3() - volume_color_transmittance(coeff.sigma_t, t);
522 
523  new_tp = *throughput * coeff.sigma_s * transmittance / dot(channel_pdf, pdf);
524  t = sample_t;
525  }
526  else {
527  /* no scattering */
528  float3 transmittance = volume_color_transmittance(coeff.sigma_t, t);
529  float pdf = dot(channel_pdf, transmittance);
530  new_tp = *throughput * transmittance / pdf;
531  }
532  }
533  else
534 # endif
535  if (closure_flag & SD_EXTINCTION) {
536  /* absorption only, no sampling needed */
537  float3 transmittance = volume_color_transmittance(coeff.sigma_t, t);
538  new_tp = *throughput * transmittance;
539  }
540  else {
541  new_tp = *throughput;
542  }
543 
544  /* integrate emission attenuated by extinction */
545  if (L && (closure_flag & SD_EMISSION)) {
546  float3 transmittance = volume_color_transmittance(coeff.sigma_t, ray->t);
548  &coeff, closure_flag, transmittance, ray->t);
549  path_radiance_accum_emission(kg, L, state, *throughput, emission);
550  }
551 
552  /* modify throughput */
553  if (closure_flag & SD_EXTINCTION) {
554  *throughput = new_tp;
555 
556  /* prepare to scatter to new direction */
557  if (t < ray->t) {
558  /* adjust throughput and move to new location */
559  sd->P = ray->P + t * ray->D;
560 
561  return VOLUME_PATH_SCATTERED;
562  }
563  }
564 
565  return VOLUME_PATH_ATTENUATED;
566 }
567 
568 /* heterogeneous volume distance sampling: integrate stepping through the
569  * volume until we reach the end, get absorbed entirely, or run out of
570  * iterations. this does probabilistically scatter or get transmitted through
571  * for path tracing where we don't want to branch. */
573 kernel_volume_integrate_heterogeneous_distance(KernelGlobals *kg,
575  Ray *ray,
576  ShaderData *sd,
577  PathRadiance *L,
578  ccl_addr_space float3 *throughput,
579  const float object_step_size)
580 {
581  float3 tp = *throughput;
582 
583  /* Prepare for stepping.
584  * Using a different step offset for the first step avoids banding artifacts. */
585  int max_steps = kernel_data.integrator.volume_max_steps;
586  float step_size, step_shade_offset, steps_offset;
587  kernel_volume_step_init(
588  kg, state, object_step_size, ray->t, &step_size, &step_shade_offset, &steps_offset);
589 
590  /* compute coefficients at the start */
591  float t = 0.0f;
592  float3 accum_transmittance = one_float3();
593 
594  /* pick random color channel, we use the Veach one-sample
595  * model with balance heuristic for the channels */
597  float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
598  bool has_scatter = false;
599 
600  for (int i = 0; i < max_steps; i++) {
601  /* advance to new position */
602  float new_t = min(ray->t, (i + steps_offset) * step_size);
603  float dt = new_t - t;
604 
605  float3 new_P = ray->P + ray->D * (t + dt * step_shade_offset);
607 
608  /* compute segment */
609  if (volume_shader_sample(kg, sd, state, new_P, &coeff)) {
610  int closure_flag = sd->flag;
611  float3 new_tp;
612  float3 transmittance;
613  bool scatter = false;
614 
615  /* distance sampling */
616 # ifdef __VOLUME_SCATTER__
617  if ((closure_flag & SD_SCATTER) || (has_scatter && (closure_flag & SD_EXTINCTION))) {
618  has_scatter = true;
619 
620  /* Sample channel, use MIS with balance heuristic. */
621  float3 albedo = safe_divide_color(coeff.sigma_s, coeff.sigma_t);
622  float3 channel_pdf;
623  int channel = kernel_volume_sample_channel(albedo, tp, rphase, &channel_pdf);
624 
625  /* compute transmittance over full step */
626  transmittance = volume_color_transmittance(coeff.sigma_t, dt);
627 
628  /* decide if we will scatter or continue */
629  float sample_transmittance = kernel_volume_channel_get(transmittance, channel);
630 
631  if (1.0f - xi >= sample_transmittance) {
632  /* compute sampling distance */
633  float sample_sigma_t = kernel_volume_channel_get(coeff.sigma_t, channel);
634  float new_dt = -logf(1.0f - xi) / sample_sigma_t;
635  new_t = t + new_dt;
636 
637  /* transmittance and pdf */
638  float3 new_transmittance = volume_color_transmittance(coeff.sigma_t, new_dt);
639  float3 pdf = coeff.sigma_t * new_transmittance;
640 
641  /* throughput */
642  new_tp = tp * coeff.sigma_s * new_transmittance / dot(channel_pdf, pdf);
643  scatter = true;
644  }
645  else {
646  /* throughput */
647  float pdf = dot(channel_pdf, transmittance);
648  new_tp = tp * transmittance / pdf;
649 
650  /* remap xi so we can reuse it and keep thing stratified */
651  xi = 1.0f - (1.0f - xi) / sample_transmittance;
652  }
653  }
654  else
655 # endif
656  if (closure_flag & SD_EXTINCTION) {
657  /* absorption only, no sampling needed */
658  transmittance = volume_color_transmittance(coeff.sigma_t, dt);
659  new_tp = tp * transmittance;
660  }
661  else {
662  transmittance = zero_float3();
663  new_tp = tp;
664  }
665 
666  /* integrate emission attenuated by absorption */
667  if (L && (closure_flag & SD_EMISSION)) {
669  &coeff, closure_flag, transmittance, dt);
670  path_radiance_accum_emission(kg, L, state, tp, emission);
671  }
672 
673  /* modify throughput */
674  if (closure_flag & SD_EXTINCTION) {
675  tp = new_tp;
676 
677  /* stop if nearly all light blocked */
680  tp = zero_float3();
681  break;
682  }
683  }
684 
685  /* prepare to scatter to new direction */
686  if (scatter) {
687  /* adjust throughput and move to new location */
688  sd->P = ray->P + new_t * ray->D;
689  *throughput = tp;
690 
691  return VOLUME_PATH_SCATTERED;
692  }
693  else {
694  /* accumulate transmittance */
695  accum_transmittance *= transmittance;
696  }
697  }
698 
699  /* stop if at the end of the volume */
700  t = new_t;
701  if (t == ray->t)
702  break;
703  }
704 
705  *throughput = tp;
706 
707  return VOLUME_PATH_ATTENUATED;
708 }
709 
710 /* get the volume attenuation and emission over line segment defined by
711  * ray, with the assumption that there are no surfaces blocking light
712  * between the endpoints. distance sampling is used to decide if we will
713  * scatter or not. */
715 kernel_volume_integrate(KernelGlobals *kg,
717  ShaderData *sd,
718  Ray *ray,
719  PathRadiance *L,
720  ccl_addr_space float3 *throughput,
721  float step_size)
722 {
723  shader_setup_from_volume(kg, sd, ray);
724 
725  if (step_size != FLT_MAX)
726  return kernel_volume_integrate_heterogeneous_distance(
727  kg, state, ray, sd, L, throughput, step_size);
728  else
729  return kernel_volume_integrate_homogeneous(kg, state, ray, sd, L, throughput, true);
730 }
731 
732 # ifndef __SPLIT_KERNEL__
733 /* Decoupled Volume Sampling
734  *
735  * VolumeSegment is list of coefficients and transmittance stored at all steps
736  * through a volume. This can then later be used for decoupled sampling as in:
737  * "Importance Sampling Techniques for Path Tracing in Participating Media"
738  *
739  * On the GPU this is only supported (but currently not enabled)
740  * for homogeneous volumes (1 step), due to
741  * no support for malloc/free and too much stack usage with a fix size array. */
742 
743 typedef struct VolumeStep {
744  float3 sigma_s; /* scatter coefficient */
745  float3 sigma_t; /* extinction coefficient */
746  float3 accum_transmittance; /* accumulated transmittance including this step */
747  float3 cdf_distance; /* cumulative density function for distance sampling */
748  float t; /* distance at end of this step */
749  float shade_t; /* jittered distance where shading was done in step */
750  int closure_flag; /* shader evaluation closure flags */
751 } VolumeStep;
752 
753 typedef struct VolumeSegment {
754  VolumeStep stack_step; /* stack storage for homogeneous step, to avoid malloc */
755  VolumeStep *steps; /* recorded steps */
756  int numsteps; /* number of steps */
757  int closure_flag; /* accumulated closure flags from all steps */
758 
759  float3 accum_emission; /* accumulated emission at end of segment */
760  float3 accum_transmittance; /* accumulated transmittance at end of segment */
761  float3 accum_albedo; /* accumulated average albedo over segment */
762 
763  int sampling_method; /* volume sampling method */
764 } VolumeSegment;
765 
766 /* record volume steps to the end of the volume.
767  *
768  * it would be nice if we could only record up to the point that we need to scatter,
769  * but the entire segment is needed to do always scattering, rather than probabilistically
770  * hitting or missing the volume. if we don't know the transmittance at the end of the
771  * volume we can't generate stratified distance samples up to that transmittance */
772 # ifdef __VOLUME_DECOUPLED__
773 ccl_device void kernel_volume_decoupled_record(KernelGlobals *kg,
774  PathState *state,
775  Ray *ray,
776  ShaderData *sd,
777  VolumeSegment *segment,
778  const float object_step_size)
779 {
780  /* prepare for volume stepping */
781  int max_steps;
782  float step_size, step_shade_offset, steps_offset;
783 
784  if (object_step_size != FLT_MAX) {
785  max_steps = kernel_data.integrator.volume_max_steps;
786  kernel_volume_step_init(
787  kg, state, object_step_size, ray->t, &step_size, &step_shade_offset, &steps_offset);
788 
789 # ifdef __KERNEL_CPU__
790  /* NOTE: For the branched path tracing it's possible to have direct
791  * and indirect light integration both having volume segments allocated.
792  * We detect this using index in the pre-allocated memory. Currently we
793  * only support two segments allocated at a time, if more needed some
794  * modifications to the KernelGlobals will be needed.
795  *
796  * This gives us restrictions that decoupled record should only happen
797  * in the stack manner, meaning if there's subsequent call of decoupled
798  * record it'll need to free memory before its caller frees memory.
799  */
800  const int index = kg->decoupled_volume_steps_index;
801  assert(index < sizeof(kg->decoupled_volume_steps) / sizeof(*kg->decoupled_volume_steps));
802  if (kg->decoupled_volume_steps[index] == NULL) {
803  kg->decoupled_volume_steps[index] = (VolumeStep *)malloc(sizeof(VolumeStep) * max_steps);
804  }
805  segment->steps = kg->decoupled_volume_steps[index];
806  ++kg->decoupled_volume_steps_index;
807 # else
808  segment->steps = (VolumeStep *)malloc(sizeof(VolumeStep) * max_steps);
809 # endif
810  }
811  else {
812  max_steps = 1;
813  step_size = ray->t;
814  step_shade_offset = 0.0f;
815  steps_offset = 1.0f;
816  segment->steps = &segment->stack_step;
817  }
818 
819  /* init accumulation variables */
820  float3 accum_emission = zero_float3();
821  float3 accum_transmittance = one_float3();
822  float3 accum_albedo = zero_float3();
823  float3 cdf_distance = zero_float3();
824  float t = 0.0f;
825 
826  segment->numsteps = 0;
827  segment->closure_flag = 0;
828  bool is_last_step_empty = false;
829 
830  VolumeStep *step = segment->steps;
831 
832  for (int i = 0; i < max_steps; i++, step++) {
833  /* advance to new position */
834  float new_t = min(ray->t, (i + steps_offset) * step_size);
835  float dt = new_t - t;
836 
837  float3 new_P = ray->P + ray->D * (t + dt * step_shade_offset);
839 
840  /* compute segment */
841  if (volume_shader_sample(kg, sd, state, new_P, &coeff)) {
842  int closure_flag = sd->flag;
843  float3 sigma_t = coeff.sigma_t;
844 
845  /* compute average albedo for channel sampling */
846  if (closure_flag & SD_SCATTER) {
847  accum_albedo += (dt / ray->t) * safe_divide_color(coeff.sigma_s, sigma_t);
848  }
849 
850  /* compute accumulated transmittance */
851  float3 transmittance = volume_color_transmittance(sigma_t, dt);
852 
853  /* compute emission attenuated by absorption */
854  if (closure_flag & SD_EMISSION) {
856  &coeff, closure_flag, transmittance, dt);
857  accum_emission += accum_transmittance * emission;
858  }
859 
860  accum_transmittance *= transmittance;
861 
862  /* compute pdf for distance sampling */
863  float3 pdf_distance = dt * accum_transmittance * coeff.sigma_s;
864  cdf_distance = cdf_distance + pdf_distance;
865 
866  /* write step data */
867  step->sigma_t = sigma_t;
868  step->sigma_s = coeff.sigma_s;
869  step->closure_flag = closure_flag;
870 
871  segment->closure_flag |= closure_flag;
872 
873  is_last_step_empty = false;
874  segment->numsteps++;
875  }
876  else {
877  if (is_last_step_empty) {
878  /* consecutive empty step, merge */
879  step--;
880  }
881  else {
882  /* store empty step */
883  step->sigma_t = zero_float3();
884  step->sigma_s = zero_float3();
885  step->closure_flag = 0;
886 
887  segment->numsteps++;
888  is_last_step_empty = true;
889  }
890  }
891 
892  step->accum_transmittance = accum_transmittance;
893  step->cdf_distance = cdf_distance;
894  step->t = new_t;
895  step->shade_t = t + dt * step_shade_offset;
896 
897  /* stop if at the end of the volume */
898  t = new_t;
899  if (t == ray->t)
900  break;
901 
902  /* stop if nearly all light blocked */
903  if (accum_transmittance.x < VOLUME_THROUGHPUT_EPSILON &&
904  accum_transmittance.y < VOLUME_THROUGHPUT_EPSILON &&
905  accum_transmittance.z < VOLUME_THROUGHPUT_EPSILON)
906  break;
907  }
908 
909  /* store total emission and transmittance */
910  segment->accum_emission = accum_emission;
911  segment->accum_transmittance = accum_transmittance;
912  segment->accum_albedo = accum_albedo;
913 
914  /* normalize cumulative density function for distance sampling */
915  VolumeStep *last_step = segment->steps + segment->numsteps - 1;
916 
917  if (!is_zero(last_step->cdf_distance)) {
918  VolumeStep *step = &segment->steps[0];
919  int numsteps = segment->numsteps;
920  float3 inv_cdf_distance_sum = safe_invert_color(last_step->cdf_distance);
921 
922  for (int i = 0; i < numsteps; i++, step++)
923  step->cdf_distance *= inv_cdf_distance_sum;
924  }
925 }
926 
927 ccl_device void kernel_volume_decoupled_free(KernelGlobals *kg, VolumeSegment *segment)
928 {
929  if (segment->steps != &segment->stack_step) {
930 # ifdef __KERNEL_CPU__
931  /* NOTE: We only allow free last allocated segment.
932  * No random order of alloc/free is supported.
933  */
934  assert(kg->decoupled_volume_steps_index > 0);
935  assert(segment->steps == kg->decoupled_volume_steps[kg->decoupled_volume_steps_index - 1]);
936  --kg->decoupled_volume_steps_index;
937 # else
938  free(segment->steps);
939 # endif
940  }
941 }
942 # endif /* __VOLUME_DECOUPLED__ */
943 
944 /* scattering for homogeneous and heterogeneous volumes, using decoupled ray
945  * marching.
946  *
947  * function is expected to return VOLUME_PATH_SCATTERED when probalistic_scatter is false */
948 ccl_device VolumeIntegrateResult kernel_volume_decoupled_scatter(KernelGlobals *kg,
949  PathState *state,
950  Ray *ray,
951  ShaderData *sd,
952  float3 *throughput,
953  float rphase,
954  float rscatter,
955  const VolumeSegment *segment,
956  const float3 *light_P,
957  bool probalistic_scatter)
958 {
959  kernel_assert(segment->closure_flag & SD_SCATTER);
960 
961  /* Sample color channel, use MIS with balance heuristic. */
962  float3 channel_pdf;
963  int channel = kernel_volume_sample_channel(
964  segment->accum_albedo, *throughput, rphase, &channel_pdf);
965 
966  float xi = rscatter;
967 
968  /* probabilistic scattering decision based on transmittance */
969  if (probalistic_scatter) {
970  float sample_transmittance = kernel_volume_channel_get(segment->accum_transmittance, channel);
971 
972  if (1.0f - xi >= sample_transmittance) {
973  /* rescale random number so we can reuse it */
974  xi = 1.0f - (1.0f - xi - sample_transmittance) / (1.0f - sample_transmittance);
975  }
976  else {
977  *throughput /= sample_transmittance;
978  return VOLUME_PATH_MISSED;
979  }
980  }
981 
982  VolumeStep *step;
983  float3 transmittance;
984  float pdf, sample_t;
985  float mis_weight = 1.0f;
986  bool distance_sample = true;
987  bool use_mis = false;
988 
989  if (segment->sampling_method && light_P) {
990  if (segment->sampling_method == SD_VOLUME_MIS) {
991  /* multiple importance sample: randomly pick between
992  * equiangular and distance sampling strategy */
993  if (xi < 0.5f) {
994  xi *= 2.0f;
995  }
996  else {
997  xi = (xi - 0.5f) * 2.0f;
998  distance_sample = false;
999  }
1000 
1001  use_mis = true;
1002  }
1003  else {
1004  /* only equiangular sampling */
1005  distance_sample = false;
1006  }
1007  }
1008 
1009  /* distance sampling */
1010  if (distance_sample) {
1011  /* find step in cdf */
1012  step = segment->steps;
1013 
1014  float prev_t = 0.0f;
1015  float3 step_pdf_distance = one_float3();
1016 
1017  if (segment->numsteps > 1) {
1018  float prev_cdf = 0.0f;
1019  float step_cdf = 1.0f;
1020  float3 prev_cdf_distance = zero_float3();
1021 
1022  for (int i = 0;; i++, step++) {
1023  /* todo: optimize using binary search */
1024  step_cdf = kernel_volume_channel_get(step->cdf_distance, channel);
1025 
1026  if (xi < step_cdf || i == segment->numsteps - 1)
1027  break;
1028 
1029  prev_cdf = step_cdf;
1030  prev_t = step->t;
1031  prev_cdf_distance = step->cdf_distance;
1032  }
1033 
1034  /* remap xi so we can reuse it */
1035  xi = (xi - prev_cdf) / (step_cdf - prev_cdf);
1036 
1037  /* pdf for picking step */
1038  step_pdf_distance = step->cdf_distance - prev_cdf_distance;
1039  }
1040 
1041  /* determine range in which we will sample */
1042  float step_t = step->t - prev_t;
1043 
1044  /* sample distance and compute transmittance */
1045  float3 distance_pdf;
1046  sample_t = prev_t + kernel_volume_distance_sample(
1047  step_t, step->sigma_t, channel, xi, &transmittance, &distance_pdf);
1048 
1049  /* modify pdf for hit/miss decision */
1050  if (probalistic_scatter)
1051  distance_pdf *= one_float3() - segment->accum_transmittance;
1052 
1053  pdf = dot(channel_pdf, distance_pdf * step_pdf_distance);
1054 
1055  /* multiple importance sampling */
1056  if (use_mis) {
1057  float equi_pdf = kernel_volume_equiangular_pdf(ray, *light_P, sample_t);
1058  mis_weight = 2.0f * power_heuristic(pdf, equi_pdf);
1059  }
1060  }
1061  /* equi-angular sampling */
1062  else {
1063  /* sample distance */
1064  sample_t = kernel_volume_equiangular_sample(ray, *light_P, xi, &pdf);
1065 
1066  /* find step in which sampled distance is located */
1067  step = segment->steps;
1068 
1069  float prev_t = 0.0f;
1070  float3 step_pdf_distance = one_float3();
1071 
1072  if (segment->numsteps > 1) {
1073  float3 prev_cdf_distance = zero_float3();
1074 
1075  int numsteps = segment->numsteps;
1076  int high = numsteps - 1;
1077  int low = 0;
1078  int mid;
1079 
1080  while (low < high) {
1081  mid = (low + high) >> 1;
1082 
1083  if (sample_t < step[mid].t)
1084  high = mid;
1085  else if (sample_t >= step[mid + 1].t)
1086  low = mid + 1;
1087  else {
1088  /* found our interval in step[mid] .. step[mid+1] */
1089  prev_t = step[mid].t;
1090  prev_cdf_distance = step[mid].cdf_distance;
1091  step += mid + 1;
1092  break;
1093  }
1094  }
1095 
1096  if (low >= numsteps - 1) {
1097  prev_t = step[numsteps - 1].t;
1098  prev_cdf_distance = step[numsteps - 1].cdf_distance;
1099  step += numsteps - 1;
1100  }
1101 
1102  /* pdf for picking step with distance sampling */
1103  step_pdf_distance = step->cdf_distance - prev_cdf_distance;
1104  }
1105 
1106  /* determine range in which we will sample */
1107  float step_t = step->t - prev_t;
1108  float step_sample_t = sample_t - prev_t;
1109 
1110  /* compute transmittance */
1111  transmittance = volume_color_transmittance(step->sigma_t, step_sample_t);
1112 
1113  /* multiple importance sampling */
1114  if (use_mis) {
1115  float3 distance_pdf3 = kernel_volume_distance_pdf(step_t, step->sigma_t, step_sample_t);
1116  float distance_pdf = dot(channel_pdf, distance_pdf3 * step_pdf_distance);
1117  mis_weight = 2.0f * power_heuristic(pdf, distance_pdf);
1118  }
1119  }
1120  if (sample_t < 0.0f || pdf == 0.0f) {
1121  return VOLUME_PATH_MISSED;
1122  }
1123 
1124  /* compute transmittance up to this step */
1125  if (step != segment->steps)
1126  transmittance *= (step - 1)->accum_transmittance;
1127 
1128  /* modify throughput */
1129  *throughput *= step->sigma_s * transmittance * (mis_weight / pdf);
1130 
1131  /* evaluate shader to create closures at shading point */
1132  if (segment->numsteps > 1) {
1133  sd->P = ray->P + step->shade_t * ray->D;
1134 
1136  volume_shader_sample(kg, sd, state, sd->P, &coeff);
1137  }
1138 
1139  /* move to new position */
1140  sd->P = ray->P + sample_t * ray->D;
1141 
1142  return VOLUME_PATH_SCATTERED;
1143 }
1144 # endif /* __SPLIT_KERNEL */
1145 
1146 /* decide if we need to use decoupled or not */
1147 ccl_device bool kernel_volume_use_decoupled(KernelGlobals *kg,
1148  bool heterogeneous,
1149  bool direct,
1150  int sampling_method)
1151 {
1152  /* decoupled ray marching for heterogeneous volumes not supported on the GPU,
1153  * which also means equiangular and multiple importance sampling is not
1154  * support for that case */
1155  if (!kernel_data.integrator.volume_decoupled)
1156  return false;
1157 
1158 # ifdef __KERNEL_GPU__
1159  if (heterogeneous)
1160  return false;
1161 # endif
1162 
1163  /* equiangular and multiple importance sampling only implemented for decoupled */
1164  if (sampling_method != 0)
1165  return true;
1166 
1167  /* for all light sampling use decoupled, reusing shader evaluations is
1168  * typically faster in that case */
1169  if (direct)
1170  return kernel_data.integrator.sample_all_lights_direct;
1171  else
1172  return kernel_data.integrator.sample_all_lights_indirect;
1173 }
1174 
1175 /* Volume Stack
1176  *
1177  * This is an array of object/shared ID's that the current segment of the path
1178  * is inside of. */
1179 
1180 ccl_device void kernel_volume_stack_init(KernelGlobals *kg,
1181  ShaderData *stack_sd,
1183  ccl_addr_space const Ray *ray,
1184  ccl_addr_space VolumeStack *stack)
1185 {
1186  /* NULL ray happens in the baker, does it need proper initialization of
1187  * camera in volume?
1188  */
1189  if (!kernel_data.cam.is_inside_volume || ray == NULL) {
1190  /* Camera is guaranteed to be in the air, only take background volume
1191  * into account in this case.
1192  */
1193  if (kernel_data.background.volume_shader != SHADER_NONE) {
1194  stack[0].shader = kernel_data.background.volume_shader;
1195  stack[0].object = PRIM_NONE;
1196  stack[1].shader = SHADER_NONE;
1197  }
1198  else {
1199  stack[0].shader = SHADER_NONE;
1200  }
1201  return;
1202  }
1203 
1205 
1206  Ray volume_ray = *ray;
1207  volume_ray.t = FLT_MAX;
1208 
1209  const uint visibility = (state->flag & PATH_RAY_ALL_VISIBILITY);
1210  int stack_index = 0, enclosed_index = 0;
1211 
1212 # ifdef __VOLUME_RECORD_ALL__
1213  Intersection hits[2 * VOLUME_STACK_SIZE + 1];
1214  uint num_hits = scene_intersect_volume_all(
1215  kg, &volume_ray, hits, 2 * VOLUME_STACK_SIZE, visibility);
1216  if (num_hits > 0) {
1217  int enclosed_volumes[VOLUME_STACK_SIZE];
1218  Intersection *isect = hits;
1219 
1220  qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
1221 
1222  for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
1223  shader_setup_from_ray(kg, stack_sd, isect, &volume_ray);
1224  if (stack_sd->flag & SD_BACKFACING) {
1225  bool need_add = true;
1226  for (int i = 0; i < enclosed_index && need_add; ++i) {
1227  /* If ray exited the volume and never entered to that volume
1228  * it means that camera is inside such a volume.
1229  */
1230  if (enclosed_volumes[i] == stack_sd->object) {
1231  need_add = false;
1232  }
1233  }
1234  for (int i = 0; i < stack_index && need_add; ++i) {
1235  /* Don't add intersections twice. */
1236  if (stack[i].object == stack_sd->object) {
1237  need_add = false;
1238  break;
1239  }
1240  }
1241  if (need_add && stack_index < VOLUME_STACK_SIZE - 1) {
1242  stack[stack_index].object = stack_sd->object;
1243  stack[stack_index].shader = stack_sd->shader;
1244  ++stack_index;
1245  }
1246  }
1247  else {
1248  /* If ray from camera enters the volume, this volume shouldn't
1249  * be added to the stack on exit.
1250  */
1251  enclosed_volumes[enclosed_index++] = stack_sd->object;
1252  }
1253  }
1254  }
1255 # else
1256  int enclosed_volumes[VOLUME_STACK_SIZE];
1257  int step = 0;
1258 
1259  while (stack_index < VOLUME_STACK_SIZE - 1 && enclosed_index < VOLUME_STACK_SIZE - 1 &&
1260  step < 2 * VOLUME_STACK_SIZE) {
1261  Intersection isect;
1262  if (!scene_intersect_volume(kg, &volume_ray, &isect, visibility)) {
1263  break;
1264  }
1265 
1266  shader_setup_from_ray(kg, stack_sd, &isect, &volume_ray);
1267  if (stack_sd->flag & SD_BACKFACING) {
1268  /* If ray exited the volume and never entered to that volume
1269  * it means that camera is inside such a volume.
1270  */
1271  bool need_add = true;
1272  for (int i = 0; i < enclosed_index && need_add; ++i) {
1273  /* If ray exited the volume and never entered to that volume
1274  * it means that camera is inside such a volume.
1275  */
1276  if (enclosed_volumes[i] == stack_sd->object) {
1277  need_add = false;
1278  }
1279  }
1280  for (int i = 0; i < stack_index && need_add; ++i) {
1281  /* Don't add intersections twice. */
1282  if (stack[i].object == stack_sd->object) {
1283  need_add = false;
1284  break;
1285  }
1286  }
1287  if (need_add) {
1288  stack[stack_index].object = stack_sd->object;
1289  stack[stack_index].shader = stack_sd->shader;
1290  ++stack_index;
1291  }
1292  }
1293  else {
1294  /* If ray from camera enters the volume, this volume shouldn't
1295  * be added to the stack on exit.
1296  */
1297  enclosed_volumes[enclosed_index++] = stack_sd->object;
1298  }
1299 
1300  /* Move ray forward. */
1301  volume_ray.P = ray_offset(stack_sd->P, -stack_sd->Ng);
1302  ++step;
1303  }
1304 # endif
1305  /* stack_index of 0 means quick checks outside of the kernel gave false
1306  * positive, nothing to worry about, just we've wasted quite a few of
1307  * ticks just to come into conclusion that camera is in the air.
1308  *
1309  * In this case we're doing the same above -- check whether background has
1310  * volume.
1311  */
1312  if (stack_index == 0 && kernel_data.background.volume_shader == SHADER_NONE) {
1313  stack[0].shader = kernel_data.background.volume_shader;
1314  stack[0].object = OBJECT_NONE;
1315  stack[1].shader = SHADER_NONE;
1316  }
1317  else {
1318  stack[stack_index].shader = SHADER_NONE;
1319  }
1320 }
1321 
1322 ccl_device void kernel_volume_stack_enter_exit(KernelGlobals *kg,
1323  ShaderData *sd,
1324  ccl_addr_space VolumeStack *stack)
1325 {
1326  /* todo: we should have some way for objects to indicate if they want the
1327  * world shader to work inside them. excluding it by default is problematic
1328  * because non-volume objects can't be assumed to be closed manifolds */
1329 
1330  if (!(sd->flag & SD_HAS_VOLUME))
1331  return;
1332 
1333  if (sd->flag & SD_BACKFACING) {
1334  /* exit volume object: remove from stack */
1335  for (int i = 0; stack[i].shader != SHADER_NONE; i++) {
1336  if (stack[i].object == sd->object) {
1337  /* shift back next stack entries */
1338  do {
1339  stack[i] = stack[i + 1];
1340  i++;
1341  } while (stack[i].shader != SHADER_NONE);
1342 
1343  return;
1344  }
1345  }
1346  }
1347  else {
1348  /* enter volume object: add to stack */
1349  int i;
1350 
1351  for (i = 0; stack[i].shader != SHADER_NONE; i++) {
1352  /* already in the stack? then we have nothing to do */
1353  if (stack[i].object == sd->object)
1354  return;
1355  }
1356 
1357  /* if we exceed the stack limit, ignore */
1358  if (i >= VOLUME_STACK_SIZE - 1)
1359  return;
1360 
1361  /* add to the end of the stack */
1362  stack[i].shader = sd->shader;
1363  stack[i].object = sd->object;
1364  stack[i + 1].shader = SHADER_NONE;
1365  }
1366 }
1367 
1368 # ifdef __SUBSURFACE__
1369 ccl_device void kernel_volume_stack_update_for_subsurface(KernelGlobals *kg,
1370  ShaderData *stack_sd,
1371  Ray *ray,
1372  ccl_addr_space VolumeStack *stack)
1373 {
1374  kernel_assert(kernel_data.integrator.use_volumes);
1375 
1376  Ray volume_ray = *ray;
1377 
1378 # ifdef __VOLUME_RECORD_ALL__
1379  Intersection hits[2 * VOLUME_STACK_SIZE + 1];
1380  uint num_hits = scene_intersect_volume_all(
1381  kg, &volume_ray, hits, 2 * VOLUME_STACK_SIZE, PATH_RAY_ALL_VISIBILITY);
1382  if (num_hits > 0) {
1383  Intersection *isect = hits;
1384 
1385  qsort(hits, num_hits, sizeof(Intersection), intersections_compare);
1386 
1387  for (uint hit = 0; hit < num_hits; ++hit, ++isect) {
1388  shader_setup_from_ray(kg, stack_sd, isect, &volume_ray);
1389  kernel_volume_stack_enter_exit(kg, stack_sd, stack);
1390  }
1391  }
1392 # else
1393  Intersection isect;
1394  int step = 0;
1395  float3 Pend = ray->P + ray->D * ray->t;
1396  while (step < 2 * VOLUME_STACK_SIZE &&
1397  scene_intersect_volume(kg, &volume_ray, &isect, PATH_RAY_ALL_VISIBILITY)) {
1398  shader_setup_from_ray(kg, stack_sd, &isect, &volume_ray);
1399  kernel_volume_stack_enter_exit(kg, stack_sd, stack);
1400 
1401  /* Move ray forward. */
1402  volume_ray.P = ray_offset(stack_sd->P, -stack_sd->Ng);
1403  if (volume_ray.t != FLT_MAX) {
1404  volume_ray.D = normalize_len(Pend - volume_ray.P, &volume_ray.t);
1405  }
1406  ++step;
1407  }
1408 # endif
1409 }
1410 # endif
1411 
1412 /* Clean stack after the last bounce.
1413  *
1414  * It is expected that all volumes are closed manifolds, so at the time when ray
1415  * hits nothing (for example, it is a last bounce which goes to environment) the
1416  * only expected volume in the stack is the world's one. All the rest volume
1417  * entries should have been exited already.
1418  *
1419  * This isn't always true because of ray intersection precision issues, which
1420  * could lead us to an infinite non-world volume in the stack, causing render
1421  * artifacts.
1422  *
1423  * Use this function after the last bounce to get rid of all volumes apart from
1424  * the world's one after the last bounce to avoid render artifacts.
1425  */
1426 ccl_device_inline void kernel_volume_clean_stack(KernelGlobals *kg,
1427  ccl_addr_space VolumeStack *volume_stack)
1428 {
1429  if (kernel_data.background.volume_shader != SHADER_NONE) {
1430  /* Keep the world's volume in stack. */
1431  volume_stack[1].shader = SHADER_NONE;
1432  }
1433  else {
1434  volume_stack[0].shader = SHADER_NONE;
1435  }
1436 }
1437 
1438 #endif /* __VOLUME__ */
1439 
typedef float(TangentPoint)[2]
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
MINLINE float safe_sqrtf(float a)
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNLIKELY(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
static T sum(const btAlignedObjectArray< T > &items)
ccl_device_inline float object_volume_step_size(KernelGlobals *kg, int object)
Definition: geom_object.h:327
ccl_device_inline float object_volume_density(KernelGlobals *kg, int object)
Definition: geom_object.h:318
ccl_device_inline float3 ray_offset(float3 P, float3 Ng)
ccl_device_inline void path_radiance_accum_emission(KernelGlobals *kg, PathRadiance *L, ccl_addr_space PathState *state, float3 throughput, float3 value)
#define kernel_data
#define kernel_assert(cond)
#define ccl_addr_space
#define kernel_tex_fetch(tex, index)
#define logf(x)
#define ccl_optional_struct_init
#define ccl_device
#define expf(x)
#define ccl_device_noinline_cpu
#define ccl_device_inline
#define tanf(x)
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define atan2f(x, y)
#define fminf(x, y)
#define make_float3(x, y, z)
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
ccl_device float power_heuristic(float a, float b)
ccl_device_inline float path_state_rng_1D(KernelGlobals *kg, const ccl_addr_space PathState *state, int dimension)
ccl_device_inline float path_state_rng_1D_hash(KernelGlobals *kg, const ccl_addr_space PathState *state, uint hash)
CCL_NAMESPACE_BEGIN ccl_device_noinline void shader_setup_from_ray(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray)
Definition: kernel_shader.h:59
@ SD_VOLUME_MIS
Definition: kernel_types.h:883
@ SD_VOLUME_EQUIANGULAR
Definition: kernel_types.h:881
@ SD_BACKFACING
Definition: kernel_types.h:843
@ SD_EXTINCTION
Definition: kernel_types.h:855
@ SD_HAS_VOLUME
Definition: kernel_types.h:873
@ SD_NEED_VOLUME_ATTRIBUTES
Definition: kernel_types.h:893
@ SD_SCATTER
Definition: kernel_types.h:857
@ SD_HETEROGENEOUS_VOLUME
Definition: kernel_types.h:877
@ SD_EMISSION
Definition: kernel_types.h:845
#define SHADER_NONE
Definition: kernel_types.h:58
@ PRNG_SCATTER_DISTANCE
Definition: kernel_types.h:247
@ PRNG_PHASE_CHANNEL
Definition: kernel_types.h:246
#define PRIM_NONE
Definition: kernel_types.h:60
@ PATH_RAY_SHADOW
Definition: kernel_types.h:284
@ PATH_RAY_ALL_VISIBILITY
Definition: kernel_types.h:295
@ PATH_RAY_CAMERA
Definition: kernel_types.h:266
#define OBJECT_NONE
Definition: kernel_types.h:59
ShaderData
@ SHADER_MASK
Definition: kernel_types.h:593
@ SD_OBJECT_HAS_VOLUME_ATTRIBUTES
Definition: kernel_types.h:920
#define VOLUME_STACK_SIZE
Definition: kernel_types.h:64
ShaderClosure
Definition: kernel_types.h:831
#define VOLUME_THROUGHPUT_EPSILON
Definition: kernel_volume.h:23
ccl_device float3 volume_color_transmittance(float3 sigma, float t)
ccl_device int kernel_volume_sample_channel(float3 albedo, float3 throughput, float rand, float3 *pdf)
struct VolumeShaderCoefficients VolumeShaderCoefficients
ccl_device float kernel_volume_equiangular_sample(Ray *ray, float3 light_P, float xi, float *pdf)
ccl_device float3 kernel_volume_distance_pdf(float max_t, float3 sigma_t, float sample_t)
VolumeIntegrateResult
Definition: kernel_volume.h:27
@ VOLUME_PATH_MISSED
Definition: kernel_volume.h:30
@ VOLUME_PATH_SCATTERED
Definition: kernel_volume.h:28
@ VOLUME_PATH_ATTENUATED
Definition: kernel_volume.h:29
ccl_device float kernel_volume_channel_get(float3 value, int channel)
ccl_device float kernel_volume_distance_sample(float max_t, float3 sigma_t, int channel, float xi, float3 *transmittance, float3 *pdf)
ccl_device float kernel_volume_equiangular_pdf(Ray *ray, float3 light_P, float sample_t)
ccl_device float3 kernel_volume_emission_integrate(VolumeShaderCoefficients *coeff, int closure_flag, float3 transmittance, float t)
static float P(float k)
Definition: math_interp.c:41
static ulong state[N]
#define L
Segment< FEdge *, Vec3r > segment
static const int steps
Definition: sky_nishita.cpp:28
#define min(a, b)
Definition: sort.c:51
float t
Definition: kernel_types.h:649
float3 P
Definition: kernel_types.h:647
float3 D
Definition: kernel_types.h:648
float z
Definition: sky_float3.h:35
float y
Definition: sky_float3.h:35
float x
Definition: sky_float3.h:35
#define CLOSURE_IS_VOLUME(type)
Definition: svm_types.h:627
__forceinline ssef low(const avxf &a)
Definition: util_avxf.h:277
__forceinline ssef high(const avxf &a)
Definition: util_avxf.h:281
ccl_device_inline float3 safe_divide_color(float3 a, float3 b)
Definition: util_math.h:514
ccl_device_inline float3 safe_invert_color(float3 a)
Definition: util_math.h:503
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline float2 normalize_len(const float2 &a, float *t)
ccl_device_inline bool is_zero(const float2 &a)
ccl_device_inline float2 fabs(const float2 &a)
ccl_device_inline float3 one_float3()
ccl_device_inline float3 exp3(float3 v)
ccl_device_inline float3 zero_float3()
ccl_device_inline float len_squared(const float3 a)
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:29