2 * Copyright 2018 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_inline void kernel_write_id_slots(ccl_global float *buffer, int num_slots, float id, float weight)
21 kernel_assert(id != ID_NONE);
26 for(int slot = 0; slot < num_slots; slot++) {
27 ccl_global float2 *id_buffer = (ccl_global float2*)buffer;
28 #ifdef __ATOMIC_PASS_WRITE__
29 /* If the loop reaches an empty slot, the ID isn't in any slot yet - so add it! */
30 if(id_buffer[slot].x == ID_NONE) {
31 /* Use an atomic to claim this slot.
32 * If a different thread got here first, try again from this slot on. */
33 float old_id = atomic_compare_and_swap_float(buffer+slot*2, ID_NONE, id);
34 if(old_id != ID_NONE && old_id != id) {
37 atomic_add_and_fetch_float(buffer+slot*2+1, weight);
40 /* If there already is a slot for that ID, add the weight.
41 * If no slot was found, add it to the last. */
42 else if(id_buffer[slot].x == id || slot == num_slots - 1) {
43 atomic_add_and_fetch_float(buffer+slot*2+1, weight);
46 #else /* __ATOMIC_PASS_WRITE__ */
47 /* If the loop reaches an empty slot, the ID isn't in any slot yet - so add it! */
48 if(id_buffer[slot].x == ID_NONE) {
49 id_buffer[slot].x = id;
50 id_buffer[slot].y = weight;
53 /* If there already is a slot for that ID, add the weight.
54 * If no slot was found, add it to the last. */
55 else if(id_buffer[slot].x == id || slot == num_slots - 1) {
56 id_buffer[slot].y += weight;
59 #endif /* __ATOMIC_PASS_WRITE__ */
63 ccl_device_inline void kernel_sort_id_slots(ccl_global float *buffer, int num_slots)
65 ccl_global float2 *id_buffer = (ccl_global float2*)buffer;
66 for(int slot = 1; slot < num_slots; ++slot) {
67 if(id_buffer[slot].x == ID_NONE) {
70 /* Since we're dealing with a tiny number of elements, insertion sort should be fine. */
72 while(i > 0 && id_buffer[i].y > id_buffer[i - 1].y) {
73 float2 swap = id_buffer[i];
74 id_buffer[i] = id_buffer[i - 1];
75 id_buffer[i - 1] = swap;
82 /* post-sorting for Cryptomatte */
83 ccl_device void kernel_cryptomatte_post(KernelGlobals *kg, ccl_global float *buffer, uint sample, int x, int y, int offset, int stride)
85 if(sample - 1 == kernel_data.integrator.aa_samples) {
86 int index = offset + x + y * stride;
87 int pass_stride = kernel_data.film.pass_stride;
88 ccl_global float *cryptomatte_buffer = buffer + index * pass_stride + kernel_data.film.pass_cryptomatte;
89 kernel_sort_id_slots(cryptomatte_buffer, 2 * kernel_data.film.cryptomatte_depth);