Cycles: Added Cryptomatte output.
[blender.git] / intern / cycles / util / util_atomic.h
1 /*
2  * Copyright 2014 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 #ifndef __UTIL_ATOMIC_H__
18 #define __UTIL_ATOMIC_H__
19
20 #ifndef __KERNEL_GPU__
21
22 /* Using atomic ops header from Blender. */
23 #include "atomic_ops.h"
24
25 #define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
26 #define atomic_compare_and_swap_float(p, old_val, new_val) atomic_cas_float((p), (old_val), (new_val))
27
28 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
29 #define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_add_uint32((p), -1)
30
31 #define CCL_LOCAL_MEM_FENCE 0
32 #define ccl_barrier(flags) (void)0
33
34 #else  /* __KERNEL_GPU__ */
35
36 #ifdef __KERNEL_OPENCL__
37
38 /* Float atomics implementation credits:
39  *   http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
40  */
41 ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *source,
42                                         const float operand)
43 {
44         union {
45                 unsigned int int_value;
46                 float float_value;
47         } new_value;
48         union {
49                 unsigned int int_value;
50                 float float_value;
51         } prev_value;
52         do {
53                 prev_value.float_value = *source;
54                 new_value.float_value = prev_value.float_value + operand;
55         } while(atomic_cmpxchg((volatile ccl_global unsigned int *)source,
56                                prev_value.int_value,
57                                new_value.int_value) != prev_value.int_value);
58         return new_value.float_value;
59 }
60
61 ccl_device_inline float atomic_compare_and_swap_float(volatile ccl_global float *dest,
62                                                       const float old_val, const float new_val)
63 {
64         union {
65                 unsigned int int_value;
66                 float float_value;
67         } new_value, prev_value, result;
68         prev_value.float_value = old_val;
69         new_value.float_value = new_val;
70         result.int_value = atomic_cmpxchg((volatile ccl_global unsigned int *)dest,
71                                        prev_value.int_value, new_value.int_value);
72         return result.float_value;
73 }
74
75 #define atomic_fetch_and_add_uint32(p, x) atomic_add((p), (x))
76 #define atomic_fetch_and_inc_uint32(p) atomic_inc((p))
77 #define atomic_fetch_and_dec_uint32(p) atomic_dec((p))
78
79 #define CCL_LOCAL_MEM_FENCE CLK_LOCAL_MEM_FENCE
80 #define ccl_barrier(flags) barrier(flags)
81
82 #endif  /* __KERNEL_OPENCL__ */
83
84 #ifdef __KERNEL_CUDA__
85
86 #define atomic_add_and_fetch_float(p, x) (atomicAdd((float*)(p), (float)(x)) + (float)(x))
87
88 #define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int*)(p), (unsigned int)(x))
89 #define atomic_fetch_and_sub_uint32(p, x) atomicSub((unsigned int*)(p), (unsigned int)(x))
90 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
91 #define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_sub_uint32((p), 1)
92
93 ccl_device_inline float atomic_compare_and_swap_float(volatile float *dest,
94                                                       const float old_val, const float new_val)
95 {
96         union {
97                 unsigned int int_value;
98                 float float_value;
99         } new_value, prev_value, result;
100         prev_value.float_value = old_val;
101         new_value.float_value = new_val;
102         result.int_value = atomicCAS((unsigned int *)dest, prev_value.int_value,new_value.int_value);
103         return result.float_value;
104 }
105
106 #define CCL_LOCAL_MEM_FENCE
107 #define ccl_barrier(flags) __syncthreads()
108
109 #endif  /* __KERNEL_CUDA__ */
110
111 #endif  /* __KERNEL_GPU__ */
112
113 #endif /* __UTIL_ATOMIC_H__ */