Blender V4.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
8
10 int size,
11 ClosureType type,
12 Spectrum weight)
13{
14 kernel_assert(size <= sizeof(ShaderClosure));
15
16 if (sd->num_closure_left == 0) {
17 return NULL;
18 }
19
20 ccl_private ShaderClosure *sc = &sd->closure[sd->num_closure];
21
22 sc->type = type;
23 sc->weight = weight;
24
25 sd->num_closure++;
26 sd->num_closure_left--;
27
28 return sc;
29}
30
32{
33 /* Allocate extra space for closure that need more parameters. We allocate
34 * in chunks of sizeof(ShaderClosure) starting from the end of the closure
35 * array.
36 *
37 * This lets us keep the same fast array iteration over closures, as we
38 * found linked list iteration and iteration with skipping to be slower. */
39 int num_extra = ((size + sizeof(ShaderClosure) - 1) / sizeof(ShaderClosure));
40
41 if (num_extra > sd->num_closure_left) {
42 /* Remove previous closure if it was allocated. */
43 sd->num_closure--;
44 sd->num_closure_left++;
45 return NULL;
46 }
47
48 sd->num_closure_left -= num_extra;
49 return (ccl_private void *)(sd->closure + sd->num_closure + sd->num_closure_left);
50}
51
53 int size,
54 Spectrum weight)
55{
57
58 /* No negative weights allowed. */
59 weight = max(weight, zero_float3());
60
61 const float sample_weight = fabsf(average(weight));
62
63 /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
64 * we will not allocate new closure. */
65 if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
67 if (!sc) {
68 return NULL;
69 }
70
71 sc->sample_weight = sample_weight;
72
73 return sc;
74 }
75
76 return NULL;
77}
78
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, int size, Spectrum weight)
Definition alloc.h:52
ccl_device ccl_private void * closure_alloc_extra(ccl_private ShaderData *sd, int size)
Definition alloc.h:31
CCL_NAMESPACE_BEGIN ccl_device ccl_private ShaderClosure * closure_alloc(ccl_private ShaderData *sd, int size, ClosureType type, Spectrum weight)
Definition alloc.h:9
#define kernel_assert(cond)
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define NULL
#define fabsf(x)
ClosureType
@ CLOSURE_NONE_ID
#define CLOSURE_WEIGHT_CUTOFF
ShaderData
ShaderClosure
ccl_device_inline float average(const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:15
float max
SPECTRUM_DATA_TYPE Spectrum
ccl_device_inline bool isfinite_safe(float f)
Definition util/math.h:365