Blender V5.3
alloc.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/types.h"
8
10
11ccl_device ccl_private ShaderClosure *closure_alloc(ccl_private ShaderData *sd,
12 const uint size,
13 ClosureType type,
14 Spectrum weight)
15{
16 kernel_assert(size <= sizeof(ShaderClosure));
17 (void)size;
18
19 if (sd->num_closure_left == 0) {
20 return nullptr;
21 }
22
23 ccl_private ShaderClosure *sc = &sd->closure[sd->num_closure];
24
25 sc->type = type;
26 sc->weight = weight;
27
28 sd->num_closure++;
29 sd->num_closure_left--;
30
31 return sc;
32}
33
35{
36 /* Allocate extra space for closure that need more parameters. We allocate
37 * in chunks of sizeof(ShaderClosure) starting from the end of the closure
38 * array.
39 *
40 * This lets us keep the same fast array iteration over closures, as we
41 * found linked list iteration and iteration with skipping to be slower. */
42 const int num_extra = ((size + sizeof(ShaderClosure) - 1) / sizeof(ShaderClosure));
43
44 if (num_extra > sd->num_closure_left) {
45 /* Remove previous closure if it was allocated. */
46 sd->num_closure--;
47 sd->num_closure_left++;
48 return nullptr;
49 }
50
51 sd->num_closure_left -= num_extra;
52 return (ccl_private void *)(sd->closure + sd->num_closure + sd->num_closure_left);
53}
54
55ccl_device_inline float closure_sample_weight(const int runtime_flag, ccl_private Spectrum &weight)
56{
58
59 /* No negative weights allowed. */
60 weight = max(weight, zero_float3());
61
62 const float sample_weight = fabsf(average(weight));
63
64 /* Do not perform weight cutoff for volume shaders, because large volume with low density could
65 * still contribute significantly to the scene. It should be up to the volume shader to decide
66 * the cutoff. */
67 /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
68 * we will not allocate new closure. */
69 if ((sample_weight >= CLOSURE_WEIGHT_CUTOFF) || (runtime_flag & SR_IS_VOLUME_SHADER_EVAL)) {
70 return sample_weight;
71 }
72
73 return 0.0f;
74}
75
77 const int size,
78 Spectrum weight)
79{
80 const float sample_weight = closure_sample_weight(sd->runtime_flag, weight);
81 if (!(sample_weight > 0.0f)) {
82 return nullptr;
83 }
84
85 ccl_private ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
86 if (!sc) {
87 return nullptr;
88 }
89
90 sc->sample_weight = sample_weight;
91
92 return sc;
93}
94
95/* Allocate BSDF closures that are possibly used for emission. */
96template<class Bsdf>
98 ccl_private Bsdf *bsdf,
99 Spectrum weight)
100{
101 if (!(closure_sample_weight(sd->runtime_flag, weight) > 0.0f)) {
102 return nullptr;
103 }
104
105 if ((sd->num_closure_left == 0) && (sd->shader_flag & SD_HAS_EMISSION)) {
106 /* When evaluating emission we don't allocate closures, but we still need a valid closure to
107 * compute the weight. */
108 bsdf->weight = weight;
109 return bsdf;
110 }
111
112 return (ccl_private Bsdf *)bsdf_alloc(sd, sizeof(Bsdf), weight);
113}
114
CCL_NAMESPACE_BEGIN ccl_device ccl_private ShaderClosure * closure_alloc(ccl_private ShaderData *sd, const uint size, ClosureType type, Spectrum weight)
Definition alloc.h:11
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, const int size, Spectrum weight)
Definition alloc.h:76
ccl_device_inline ccl_private Bsdf * bsdf_alloc_maybe_emission(ccl_private ShaderData *sd, ccl_private Bsdf *bsdf, Spectrum weight)
Definition alloc.h:97
ccl_device_inline float closure_sample_weight(const int runtime_flag, ccl_private Spectrum &weight)
Definition alloc.h:55
ccl_device ccl_private void * closure_alloc_extra(ccl_private ShaderData *sd, const int size)
Definition alloc.h:34
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
btScalar max
#define kernel_assert(cond)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define CLOSURE_WEIGHT_CUTOFF
float average(float2 a)
ClosureType
@ CLOSURE_NONE_ID
@ SR_IS_VOLUME_SHADER_EVAL
@ SD_HAS_EMISSION
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define fabsf
#define ccl_device
float3 Spectrum