Cycles: Cleanup, silence strict compiler warning
[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, int size, float3 weight, void *data)
72 {
73         ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
74
75         if(!sc)
76                 return NULL;
77
78         memcpy((void *)sc, data, size);
79
80         float sample_weight = fabsf(average(weight));
81         sc->weight = weight;
82         sc->sample_weight = sample_weight;
83         return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? sc : NULL;
84 }
85 #endif
86
87 CCL_NAMESPACE_END