Blender  V3.3
alloc.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
9  int size,
11  float3 weight)
12 {
13  kernel_assert(size <= sizeof(ShaderClosure));
14 
15  if (sd->num_closure_left == 0)
16  return NULL;
17 
18  ccl_private ShaderClosure *sc = &sd->closure[sd->num_closure];
19 
20  sc->type = type;
21  sc->weight = weight;
22 
23  sd->num_closure++;
24  sd->num_closure_left--;
25 
26  return sc;
27 }
28 
30 {
31  /* Allocate extra space for closure that need more parameters. We allocate
32  * in chunks of sizeof(ShaderClosure) starting from the end of the closure
33  * array.
34  *
35  * This lets us keep the same fast array iteration over closures, as we
36  * found linked list iteration and iteration with skipping to be slower. */
37  int num_extra = ((size + sizeof(ShaderClosure) - 1) / sizeof(ShaderClosure));
38 
39  if (num_extra > sd->num_closure_left) {
40  /* Remove previous closure if it was allocated. */
41  sd->num_closure--;
42  sd->num_closure_left++;
43  return NULL;
44  }
45 
46  sd->num_closure_left -= num_extra;
47  return (ccl_private void *)(sd->closure + sd->num_closure + sd->num_closure_left);
48 }
49 
51  int size,
52  float3 weight)
53 {
55 
56  const float sample_weight = fabsf(average(weight));
57 
58  /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
59  * we will not allocate new closure. */
60  if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
62  if (sc == NULL) {
63  return NULL;
64  }
65 
66  sc->sample_weight = sample_weight;
67 
68  return sc;
69  }
70 
71  return NULL;
72 }
73 
74 #ifdef __OSL__
75 ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd,
76  int size,
77  float3 weight,
78  void *data)
79 {
81 
82  const float sample_weight = fabsf(average(weight));
83 
84  /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
85  * we will not allocate new closure. */
86  if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
87  ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
88  if (!sc) {
89  return NULL;
90  }
91 
92  memcpy((void *)sc, data, size);
93 
94  sc->weight = weight;
95  sc->sample_weight = sample_weight;
96 
97  return sc;
98  }
99 
100  return NULL;
101 }
102 #endif
103 
_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 type
CCL_NAMESPACE_BEGIN ccl_device ccl_private ShaderClosure * closure_alloc(ccl_private ShaderData *sd, int size, ClosureType type, float3 weight)
Definition: alloc.h:8
ccl_device ccl_private void * closure_alloc_extra(ccl_private ShaderData *sd, int size)
Definition: alloc.h:29
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, int size, float3 weight)
Definition: alloc.h:50
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define kernel_assert(cond)
Definition: cpu/compat.h:34
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
ClosureType
@ CLOSURE_NONE_ID
#define CLOSURE_WEIGHT_CUTOFF
ShaderData
Definition: kernel/types.h:925
ShaderClosure
Definition: kernel/types.h:726
ccl_device_inline float average(const float2 &a)
Definition: math_float2.h:170
#define fabsf(x)
Definition: metal/compat.h:219
ccl_device_inline bool isfinite_safe(float f)
Definition: util/math.h:353