Blender  V2.93
alloc.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 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 
20 {
21  kernel_assert(size <= sizeof(ShaderClosure));
22 
23  if (sd->num_closure_left == 0)
24  return NULL;
25 
26  ShaderClosure *sc = &sd->closure[sd->num_closure];
27 
28  sc->type = type;
29  sc->weight = weight;
30 
31  sd->num_closure++;
32  sd->num_closure_left--;
33 
34  return sc;
35 }
36 
38 {
39  /* Allocate extra space for closure that need more parameters. We allocate
40  * in chunks of sizeof(ShaderClosure) starting from the end of the closure
41  * array.
42  *
43  * This lets us keep the same fast array iteration over closures, as we
44  * found linked list iteration and iteration with skipping to be slower. */
45  int num_extra = ((size + sizeof(ShaderClosure) - 1) / sizeof(ShaderClosure));
46 
47  if (num_extra > sd->num_closure_left) {
48  /* Remove previous closure if it was allocated. */
49  sd->num_closure--;
50  sd->num_closure_left++;
51  return NULL;
52  }
53 
54  sd->num_closure_left -= num_extra;
55  return (ccl_addr_space void *)(sd->closure + sd->num_closure + sd->num_closure_left);
56 }
57 
59 {
61 
62  const float sample_weight = fabsf(average(weight));
63 
64  /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
65  * we will not allocate new closure. */
66  if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
67  ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
68  if (sc == NULL) {
69  return NULL;
70  }
71 
72  sc->sample_weight = sample_weight;
73 
74  return sc;
75  }
76 
77  return NULL;
78 }
79 
80 #ifdef __OSL__
81 ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd,
82  int size,
83  float3 weight,
84  void *data)
85 {
87 
88  const float sample_weight = fabsf(average(weight));
89 
90  /* Use comparison this way to help dealing with non-finite weight: if the average is not finite
91  * we will not allocate new closure. */
92  if (sample_weight >= CLOSURE_WEIGHT_CUTOFF) {
93  ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
94  if (!sc) {
95  return NULL;
96  }
97 
98  memcpy((void *)sc, data, size);
99 
100  sc->weight = weight;
101  sc->sample_weight = sample_weight;
102 
103  return sc;
104  }
105 
106  return NULL;
107 }
108 #endif
109 
_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_device ccl_addr_space void * closure_alloc_extra(ShaderData *sd, int size)
Definition: alloc.h:37
ccl_device_inline ShaderClosure * bsdf_alloc(ShaderData *sd, int size, float3 weight)
Definition: alloc.h:58
CCL_NAMESPACE_BEGIN ccl_device ShaderClosure * closure_alloc(ShaderData *sd, int size, ClosureType type, float3 weight)
Definition: alloc.h:19
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define kernel_assert(cond)
#define ccl_addr_space
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define fabsf(x)
ShaderData
ShaderClosure
Definition: kernel_types.h:831
ClosureType
Definition: svm_types.h:527
@ CLOSURE_NONE_ID
Definition: svm_types.h:529
#define CLOSURE_WEIGHT_CUTOFF
Definition: svm_types.h:637
ccl_device_inline float average(const float2 &a)
ccl_device_inline bool isfinite3_safe(float3 v)