Blender V5.3
eevee_cryptomatte.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#pragma once
8
10
12
14{
15 uint m3hash = floatBitsToUint(hash);
16 return float4(hash,
17 float(m3hash << 8) / float(0xFFFFFFFFu),
18 float(m3hash << 16) / float(0xFFFFFFFFu),
19 1.0f);
20}
21
22} // namespace eevee::cryptomatte
23
24namespace eevee {
25
27 /* Buffer containing per object cryptomatte hash.
28 * Indexed by `resource_id`. */
30
32
33 void store(int2 texel, float material_hash, uint resource_id)
34 {
37 texel,
38 float4(cryptomatte_object_buf[resource_id], material_hash, 0.0f));
39 }
40 }
41};
42
45
46 void clear_samples(FilmSample dst)
47 {
48 int layer_len = imageSize(cryptomatte_img).z;
49 for (int i = 0; i < layer_len; i++) {
50 imageStoreFast(cryptomatte_img, int3(dst.texel, i), float4(0.0f));
51 /* Ensure stores are visible to later reads. */
53 }
54 }
55
56 void store_film_sample(FilmSample dst,
57 int cryptomatte_layer_id,
58 int cryptomatte_samples_len,
59 float2 crypto_sample,
60 float4 &out_color)
61 {
62 if (crypto_sample.y == 0.0f) {
63 return;
64 }
65
66 for (int i = 0; i < cryptomatte_samples_len / 2; i++) {
67 int3 img_co = int3(dst.texel, cryptomatte_layer_id + i);
68 float4 sample_pair = imageLoad(cryptomatte_img, img_co);
69 if (can_merge_sample(sample_pair.xy, crypto_sample)) {
70 sample_pair.xy = merge_sample(dst, sample_pair.xy, crypto_sample);
71 /* In viewport only one layer is active. */
72 /* TODO(jbakker): we are displaying the first sample, but we should display the highest
73 * weighted one. */
74 if (cryptomatte_layer_id + i == 0) {
75 out_color = cryptomatte::false_color(sample_pair.x);
76 }
77 }
78 else if (can_merge_sample(sample_pair.zw, crypto_sample)) {
79 sample_pair.zw = merge_sample(dst, sample_pair.zw, crypto_sample);
80 }
81 else if (i == ((cryptomatte_samples_len / 2) - 1)) {
82 /* TODO(jbakker): New hash detected, but there is no space left to store it. Currently we
83 * will ignore this sample, but ideally we could replace a sample with a lowest weight. */
84 continue;
85 }
86 else {
87 continue;
88 }
89 imageStoreFast(cryptomatte_img, img_co, sample_pair);
90 break;
91 }
92 /* Ensure stores are visible to later reads. */
94 }
95
96 private:
97 bool can_merge_sample(float2 dst, float2 src)
98 {
99 if (all(equal(dst, float2(0.0f, 0.0f)))) {
100 return true;
101 }
102 if (dst.x == src.x) {
103 return true;
104 }
105 return false;
106 }
107
108 float2 merge_sample(FilmSample film_sample, float2 dst, float2 src)
109 {
110 float coverage = (dst.y * film_sample.weight + src.y) * film_sample.weight_sum_inv;
111 /* Fix alpha not accumulating to 1 because of float imprecision. */
112 if (coverage > 0.9999f) {
113 coverage = 1.0f;
114 }
115 return float2(src.x, coverage);
116 }
117};
118
119} // namespace eevee
#define RBUFS_CRYPTOMATTE_SLOT
#define CRYPTOMATTE_BUF_SLOT
@ SFLOAT_32_32_32_32
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< float, 2, true > image2DArray
ImageBase< float, 2 > image2D
IMG_TEMPLATE void imageFence(T &)
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageStoreFast
VecBase< float, 2 > float2
float4 false_color(float hash)
#define hash
Definition noise_c.cc:156
const float2(& cryptomatte_object_buf)[]
void store(int2 texel, float material_hash, uint resource_id)
void clear_samples(FilmSample dst)
void store_film_sample(FilmSample dst, int cryptomatte_layer_id, int cryptomatte_samples_len, float2 crypto_sample, float4 &out_color)
float x
float y
float x
Definition sky_math.h:225