Blender V4.3
cryptomatte_passes.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9/* Element of ID pass stored in the render buffers.
10 * It is `float2` semantically, but it must be unaligned since the offset of ID passes in the
11 * render buffers might not meet expected by compiler alignment. */
16
18 int num_slots,
19 float id,
20 float weight)
21{
23 if (weight == 0.0f) {
24 return;
25 }
26
27 for (int slot = 0; slot < num_slots; slot++) {
29#ifdef __ATOMIC_PASS_WRITE__
30 /* If the loop reaches an empty slot, the ID isn't in any slot yet - so add it! */
31 if (id_buffer[slot].x == ID_NONE) {
32 /* Use an atomic to claim this slot.
33 * If a different thread got here first, try again from this slot on. */
34 float old_id = atomic_compare_and_swap_float(buffer + slot * 2, ID_NONE, id);
35 if (old_id != ID_NONE && old_id != id) {
36 continue;
37 }
38 atomic_add_and_fetch_float(buffer + slot * 2 + 1, weight);
39 break;
40 }
41 /* If there already is a slot for that ID, add the weight.
42 * If no slot was found, add it to the last. */
43 else if (id_buffer[slot].x == id || slot == num_slots - 1) {
44 atomic_add_and_fetch_float(buffer + slot * 2 + 1, weight);
45 break;
46 }
47#else /* __ATOMIC_PASS_WRITE__ */
48 /* If the loop reaches an empty slot, the ID isn't in any slot yet - so add it! */
49 if (id_buffer[slot].x == ID_NONE) {
50 id_buffer[slot].x = id;
51 id_buffer[slot].y = weight;
52 break;
53 }
54 /* If there already is a slot for that ID, add the weight.
55 * If no slot was found, add it to the last. */
56 else if (id_buffer[slot].x == id || slot == num_slots - 1) {
57 id_buffer[slot].y += weight;
58 break;
59 }
60#endif /* __ATOMIC_PASS_WRITE__ */
61 }
62}
63
65{
67 for (int slot = 1; slot < num_slots; ++slot) {
68 if (id_buffer[slot].x == ID_NONE) {
69 return;
70 }
71 /* Since we're dealing with a tiny number of elements, insertion sort should be fine. */
72 int i = slot;
73 while (i > 0 && id_buffer[i].y > id_buffer[i - 1].y) {
74 const CryptoPassBufferElement swap = id_buffer[i];
75 id_buffer[i] = id_buffer[i - 1];
76 id_buffer[i - 1] = swap;
77 --i;
78 }
79 }
80}
81
82/* post-sorting for Cryptomatte */
85 int pixel_index)
86{
87 const int pass_stride = kernel_data.film.pass_stride;
88 const uint64_t render_buffer_offset = (uint64_t)pixel_index * pass_stride;
89 ccl_global float *cryptomatte_buffer = render_buffer + render_buffer_offset +
90 kernel_data.film.pass_cryptomatte;
91 film_sort_cryptomatte_slots(cryptomatte_buffer, 2 * kernel_data.film.cryptomatte_depth);
92}
93
#define atomic_compare_and_swap_float(p, old_val, new_val)
Definition atomic.h:14
#define atomic_add_and_fetch_float(p, x)
Definition atomic.h:13
CCL_NAMESPACE_BEGIN struct CryptoPassBufferElement CryptoPassBufferElement
ccl_device_inline void film_sort_cryptomatte_slots(ccl_global float *buffer, int num_slots)
ccl_device_inline void film_write_cryptomatte_slots(ccl_global float *buffer, int num_slots, float id, float weight)
ccl_device_inline void film_cryptomatte_post(KernelGlobals kg, ccl_global float *render_buffer, int pixel_index)
#define kernel_assert(cond)
#define kernel_data
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
#define ID_NONE
#define swap(a, b)
Definition sort.c:55
unsigned __int64 uint64_t
Definition stdint.h:90