2023-08-24 10:54:59 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2022 Blender Authors
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
|
2024-10-04 15:48:22 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-09-25 10:57:02 +02:00
|
|
|
#include "infos/eevee_film_infos.hh"
|
2024-12-02 21:26:15 +01:00
|
|
|
|
|
|
|
|
SHADER_LIBRARY_CREATE_INFO(eevee_film)
|
|
|
|
|
|
2022-09-13 11:07:30 +02:00
|
|
|
/** Storing/merging and sorting cryptomatte samples. */
|
|
|
|
|
|
2025-04-14 13:46:41 +02:00
|
|
|
bool cryptomatte_can_merge_sample(float2 dst, float2 src)
|
2022-09-13 11:07:30 +02:00
|
|
|
{
|
2025-04-14 13:46:41 +02:00
|
|
|
if (all(equal(dst, float2(0.0f, 0.0f)))) {
|
2022-09-13 11:07:30 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dst.x == src.x) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 13:46:41 +02:00
|
|
|
float2 cryptomatte_merge_sample(FilmSample film_sample, float2 dst, float2 src)
|
2022-09-13 11:07:30 +02:00
|
|
|
{
|
2025-04-14 13:46:41 +02:00
|
|
|
return float2(src.x, (dst.y * film_sample.weight + src.y) * film_sample.weight_sum_inv);
|
2022-09-13 11:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 13:46:41 +02:00
|
|
|
float4 cryptomatte_false_color(float hash)
|
2022-09-13 11:07:30 +02:00
|
|
|
{
|
|
|
|
|
uint m3hash = floatBitsToUint(hash);
|
2025-04-14 13:46:41 +02:00
|
|
|
return float4(hash,
|
|
|
|
|
float(m3hash << 8) / float(0xFFFFFFFFu),
|
|
|
|
|
float(m3hash << 16) / float(0xFFFFFFFFu),
|
|
|
|
|
1.0f);
|
2022-09-13 11:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cryptomatte_clear_samples(FilmSample dst)
|
|
|
|
|
{
|
|
|
|
|
int layer_len = imageSize(cryptomatte_img).z;
|
|
|
|
|
for (int i = 0; i < layer_len; i++) {
|
2025-04-14 13:46:41 +02:00
|
|
|
imageStoreFast(cryptomatte_img, int3(dst.texel, i), float4(0.0f));
|
2024-03-14 17:48:30 +01:00
|
|
|
/* Ensure stores are visible to later reads. */
|
|
|
|
|
imageFence(cryptomatte_img);
|
2022-09-13 11:07:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cryptomatte_store_film_sample(FilmSample dst,
|
|
|
|
|
int cryptomatte_layer_id,
|
2025-04-14 13:46:41 +02:00
|
|
|
float2 crypto_sample,
|
|
|
|
|
out float4 out_color)
|
2022-09-13 11:07:30 +02:00
|
|
|
{
|
2025-04-11 18:28:45 +02:00
|
|
|
if (crypto_sample.y == 0.0f) {
|
2022-09-13 11:07:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2023-09-08 21:03:37 +02:00
|
|
|
for (int i = 0; i < uniform_buf.film.cryptomatte_samples_len / 2; i++) {
|
2025-04-14 13:46:41 +02:00
|
|
|
int3 img_co = int3(dst.texel, cryptomatte_layer_id + i);
|
|
|
|
|
float4 sample_pair = imageLoad(cryptomatte_img, img_co);
|
2022-09-13 11:07:30 +02:00
|
|
|
if (cryptomatte_can_merge_sample(sample_pair.xy, crypto_sample)) {
|
2024-07-22 21:14:03 +02:00
|
|
|
sample_pair.xy = cryptomatte_merge_sample(dst, sample_pair.xy, crypto_sample);
|
2022-09-13 11:07:30 +02:00
|
|
|
/* In viewport only one layer is active. */
|
|
|
|
|
/* TODO(jbakker): we are displaying the first sample, but we should display the highest
|
|
|
|
|
* weighted one. */
|
|
|
|
|
if (cryptomatte_layer_id + i == 0) {
|
|
|
|
|
out_color = cryptomatte_false_color(sample_pair.x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (cryptomatte_can_merge_sample(sample_pair.zw, crypto_sample)) {
|
2024-07-22 21:14:03 +02:00
|
|
|
sample_pair.zw = cryptomatte_merge_sample(dst, sample_pair.zw, crypto_sample);
|
2022-09-13 11:07:30 +02:00
|
|
|
}
|
2023-09-08 21:03:37 +02:00
|
|
|
else if (i == uniform_buf.film.cryptomatte_samples_len / 2 - 1) {
|
2022-09-13 11:07:30 +02:00
|
|
|
/* TODO(jbakker): New hash detected, but there is no space left to store it. Currently we
|
|
|
|
|
* will ignore this sample, but ideally we could replace a sample with a lowest weight. */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-07-21 17:13:07 +02:00
|
|
|
imageStoreFast(cryptomatte_img, img_co, sample_pair);
|
2022-09-13 11:07:30 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2024-03-14 17:48:30 +01:00
|
|
|
/* Ensure stores are visible to later reads. */
|
|
|
|
|
imageFence(cryptomatte_img);
|
2022-09-13 11:07:30 +02:00
|
|
|
}
|