Blender V5.0
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
56 const int size,
57 Spectrum weight)
58{
60
61 /* No negative weights allowed. */
62 weight = max(weight, zero_float3());
63
64 const float sample_weight = fabsf(average(weight));
65
66 /* Do not perform weight cutoff for volume shaders, because large volume with low density could
67 * still contribute significantly to the scene. It should be up to the volume shader to decide
68 * the cutoff. */
69 /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
70 * we will not allocate new closure. */
71 const bool volume_valid = (sd->flag & SD_IS_VOLUME_SHADER_EVAL) && (sample_weight > 0.0f);
72 if (volume_valid || sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
73 ccl_private ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
74 if (!sc) {
75 return nullptr;
76 }
77
78 sc->sample_weight = sample_weight;
79
80 return sc;
81 }
82
83 return nullptr;
84}
85
unsigned int uint
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: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
#define kernel_assert(cond)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define CLOSURE_WEIGHT_CUTOFF
ClosureType
@ CLOSURE_NONE_ID
@ SD_IS_VOLUME_SHADER_EVAL
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:348
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
float average(point a)
Definition node_math.h:144
#define fabsf
#define ccl_device
max
Definition text_draw.cc:251
float3 Spectrum