Blender V5.3
eevee_cryptomatte_sort.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
10namespace eevee::cryptomatte {
11
12#define CRYPTOMATTE_LEVELS_MAX 16
13
14struct Resources {
17
19
20 void load_samples(int2 texel, int layer, float2 (&samples)[CRYPTOMATTE_LEVELS_MAX])
21 {
23 int layer_id = layer * pass_len;
24
25 /* Read all samples from the cryptomatte layer. */
26 for (int p = 0; p < pass_len; p++) {
27 float4 pass_sample = imageLoadFast(cryptomatte_img, int3(texel, p + layer_id));
28 samples[p * 2] = pass_sample.xy;
29 samples[p * 2 + 1] = pass_sample.zw;
30 }
31 for (int i = pass_len * 2; i < CRYPTOMATTE_LEVELS_MAX; i++) {
32 samples[i] = float2(0.0f);
33 }
34 }
35
37 {
38 /* Sort samples. Lame implementation, can be replaced with a more efficient algorithm. */
39 for (int i = 0; i < cryptomatte_samples_per_layer - 1 && samples[i].y != 0.0f; i++) {
40 int highest_index = i;
41 float highest_weight = samples[i].y;
42 for (int j = i + 1; j < cryptomatte_samples_per_layer && samples[j].y != 0.0f; j++) {
43 if (samples[j].y > highest_weight) {
44 highest_index = j;
45 highest_weight = samples[j].y;
46 }
47 };
48
49 if (highest_index != i) {
50 float2 tmp = samples[i];
51 samples[i] = samples[highest_index];
52 samples[highest_index] = tmp;
53 }
54 }
55 }
56
58 {
60 int layer_id = layer * pass_len;
61
62 /* Store samples back to the cryptomatte layer. */
63 for (int p = 0; p < pass_len; p++) {
64 float4 pass_sample;
65 pass_sample.xy = samples[p * 2];
66 pass_sample.zw = samples[p * 2 + 1];
67 imageStoreFast(cryptomatte_img, int3(texel, p + layer_id), pass_sample);
68 }
69 /* Ensure stores are visible to later reads. */
71 }
72};
73
76 [[resource_table]] const Uniform &uni,
77 [[global_invocation_id]] const uint3 global_id)
78{
79 int2 texel = int2(global_id.xy);
80
81 if (any(greaterThanEqual(texel, uni.uniform_buf.film.extent))) {
82 return;
83 }
84
85 for (int layer = 0; layer < srt.cryptomatte_layer_len; layer++) {
87 srt.load_samples(texel, layer, samples);
88 srt.sort_samples(samples);
89 srt.store_samples(texel, layer, samples);
90 }
91}
92
93} // namespace eevee::cryptomatte
94
PipelineCompute eevee_film_cryptomatte_post(eevee::cryptomatte::sort_samples)
#define CRYPTOMATTE_LEVELS_MAX
#define FILM_GROUP_SIZE
@ SFLOAT_32_32_32_32
#define image(slot, qualifiers, format)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define layer
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
ImageBase< float, 2, true > image2DArray
IMG_TEMPLATE void imageFence(T &)
#define imageLoadFast
#define imageStoreFast
int divide_ceil(int a, int b)
void sort_samples(Resources &srt, const Uniform &uni, const uint3 global_id)
void store_samples(int2 texel, int layer, float2 samples[CRYPTOMATTE_LEVELS_MAX])
void sort_samples(float2(&samples)[CRYPTOMATTE_LEVELS_MAX])
void load_samples(int2 texel, int layer, float2(&samples)[CRYPTOMATTE_LEVELS_MAX])