ClangFormat: apply to source, most of intern
[blender.git] / intern / cycles / kernel / closure / alloc.h
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
17 CCL_NAMESPACE_BEGIN
18
19 ccl_device ShaderClosure *closure_alloc(ShaderData *sd, int size, ClosureType type, float3 weight)
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
37 ccl_device ccl_addr_space void *closure_alloc_extra(ShaderData *sd, int size)
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
58 ccl_device_inline ShaderClosure *bsdf_alloc(ShaderData *sd, int size, float3 weight)
59 {
60   ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
61
62   if (sc == NULL)
63     return NULL;
64
65   float sample_weight = fabsf(average(weight));
66   sc->sample_weight = sample_weight;
67   return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? sc : NULL;
68 }
69
70 #ifdef __OSL__
71 ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd,
72                                                 int size,
73                                                 float3 weight,
74                                                 void *data)
75 {
76   ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
77
78   if (!sc)
79     return NULL;
80
81   memcpy((void *)sc, data, size);
82
83   float sample_weight = fabsf(average(weight));
84   sc->weight = weight;
85   sc->sample_weight = sample_weight;
86   return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? sc : NULL;
87 }
88 #endif
89
90 CCL_NAMESPACE_END