2 * Copyright 2011-2016 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ccl_device ShaderClosure *closure_alloc(ShaderData *sd, int size, ClosureType type, float3 weight)
21 kernel_assert(size <= sizeof(ShaderClosure));
23 if(sd->num_closure_left == 0)
26 ShaderClosure *sc = &sd->closure[sd->num_closure];
32 sd->num_closure_left--;
37 ccl_device ccl_addr_space void *closure_alloc_extra(ShaderData *sd, int size)
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
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));
47 if(num_extra > sd->num_closure_left) {
48 /* Remove previous closure if it was allocated. */
50 sd->num_closure_left++;
54 sd->num_closure_left -= num_extra;
55 return (ccl_addr_space void*)(sd->closure + sd->num_closure + sd->num_closure_left);
58 ccl_device_inline ShaderClosure *bsdf_alloc(ShaderData *sd, int size, float3 weight)
60 ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
65 float sample_weight = fabsf(average(weight));
66 sc->sample_weight = sample_weight;
67 return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? sc : NULL;
71 ccl_device_inline ShaderClosure *bsdf_alloc_osl(ShaderData *sd, int size, float3 weight, void *data)
73 ShaderClosure *sc = closure_alloc(sd, size, CLOSURE_NONE_ID, weight);
78 memcpy(sc, data, size);
80 float sample_weight = fabsf(average(weight));
82 sc->sample_weight = sample_weight;
83 return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? sc : NULL;