Cycles: Added Cryptomatte output.
[blender.git] / intern / cycles / kernel / split / kernel_buffer_update.h
index 180c0b57077dc984b89e534c87463ca5083e52a7..18eec6372f1699d50f7fde2a962e098ceca06a6c 100644 (file)
@@ -80,8 +80,10 @@ ccl_device void kernel_buffer_update(KernelGlobals *kg,
        PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
        ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
        ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
+       bool ray_was_updated = false;
 
        if(IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER)) {
+               ray_was_updated = true;
                uint sample = state->sample;
                uint buffer_offset = kernel_split_state.buffer_offset[ray_index];
                ccl_global float *buffer = kernel_split_params.tile.buffer + buffer_offset;
@@ -92,6 +94,17 @@ ccl_device void kernel_buffer_update(KernelGlobals *kg,
                ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
        }
 
+       if(kernel_data.film.cryptomatte_passes) {
+               /* Make sure no thread is writing to the buffers. */
+               ccl_barrier(CCL_LOCAL_MEM_FENCE);
+               if(ray_was_updated && state->sample - 1 == kernel_data.integrator.aa_samples) {
+                       uint buffer_offset = kernel_split_state.buffer_offset[ray_index];
+                       ccl_global float *buffer = kernel_split_params.tile.buffer + buffer_offset;
+                       ccl_global float *cryptomatte_buffer = buffer + kernel_data.film.pass_cryptomatte;
+                       kernel_sort_id_slots(cryptomatte_buffer, 2 * kernel_data.film.cryptomatte_depth);
+               }
+       }
+
        if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
                /* We have completed current work; So get next work */
                ccl_global uint *work_pools = kernel_split_params.work_pools;