[Bf-blender-cvs] [a7e294e1787] temp-eevee-next-cryptomatte: Normalize cryptomatte weights.

Jeroen Bakker noreply at git.blender.org
Mon Sep 12 08:40:07 CEST 2022


Commit: a7e294e17876f094985e73488af6fa4de0accb61
Author: Jeroen Bakker
Date:   Fri Sep 2 11:12:06 2022 +0200
Branches: temp-eevee-next-cryptomatte
https://developer.blender.org/rBa7e294e17876f094985e73488af6fa4de0accb61

Normalize cryptomatte weights.

===================================================================

M	source/blender/draw/engines/eevee_next/eevee_film.cc
M	source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/infos/eevee_film_info.hh

===================================================================

diff --git a/source/blender/draw/engines/eevee_next/eevee_film.cc b/source/blender/draw/engines/eevee_next/eevee_film.cc
index f0fdac24e85..1e7ac790dfb 100644
--- a/source/blender/draw/engines/eevee_next/eevee_film.cc
+++ b/source/blender/draw/engines/eevee_next/eevee_film.cc
@@ -475,6 +475,7 @@ void Film::sync()
     GPUShader *sh = inst_.shaders.static_shader_get(FILM_CRYPTOMATTE_POST);
     DRWShadingGroup *grp = DRW_shgroup_create(sh, cryptomatte_post_ps_);
     DRW_shgroup_uniform_image_ref(grp, "cryptomatte_img", &cryptomatte_tx_);
+    DRW_shgroup_uniform_image_ref(grp, "weight_img", &weight_tx_.current());
     DRW_shgroup_uniform_int_copy(grp, "cryptomatte_layer_len", cryptomatte_layer_count);
     DRW_shgroup_uniform_int_copy(
         grp, "cryptomatte_samples_per_layer", inst_.view_layer->cryptomatte_levels);
diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl
index e0cb9abd5c7..fd98240639e 100644
--- a/source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl
+++ b/source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl
@@ -18,10 +18,9 @@ void cryptomatte_load_samples(ivec2 texel, int layer, out vec2 samples[CRYPTOMAT
   }
 }
 
-bool cryptomatte_sort_samples(inout vec2 samples[CRYPTOMATTE_LEVELS_MAX])
+void cryptomatte_sort_samples(inout vec2 samples[CRYPTOMATTE_LEVELS_MAX])
 {
   /* Sort samples. Lame implementation, can be replaced with a more efficient algorithm. */
-  bool changed = false;
   for (int i = 0; i < cryptomatte_samples_per_layer - 1 && samples[i].y != 0.0; i++) {
     int highest_index = i;
     float highest_weight = samples[i].y;
@@ -36,10 +35,14 @@ bool cryptomatte_sort_samples(inout vec2 samples[CRYPTOMATTE_LEVELS_MAX])
       vec2 tmp = samples[i];
       samples[i] = samples[highest_index];
       samples[highest_index] = tmp;
-      changed = true;
     }
   }
-  return changed;
+}
+void cryptomatte_normalize_weight(float total_weight, inout vec2 samples[CRYPTOMATTE_LEVELS_MAX])
+{
+  for (int i = 0; i < CRYPTOMATTE_LEVELS_MAX; i++) {
+    samples[i].y /= total_weight;
+  }
 }
 
 void cryptomatte_store_samples(ivec2 texel, int layer, in vec2 samples[CRYPTOMATTE_LEVELS_MAX])
@@ -62,10 +65,12 @@ void main()
   for (int layer = 0; layer < cryptomatte_layer_len; layer++) {
     vec2 samples[CRYPTOMATTE_LEVELS_MAX];
     cryptomatte_load_samples(texel, layer, samples);
-    bool changed = cryptomatte_sort_samples(samples);
-    /* TODO(jbakker): Normalize the weights based on the film pixel weight. */
-    if (changed) {
-      cryptomatte_store_samples(texel, layer, samples);
-    }
+    cryptomatte_sort_samples(samples);
+    /* Repeat texture coordinates as the weight can be optimized to a small portion of the film. */
+    float weight = imageLoad(weight_img,
+                             ivec3(texel % imageSize(weight_img).xy, 0))
+                       .x;
+    cryptomatte_normalize_weight(weight, samples);
+    cryptomatte_store_samples(texel, layer, samples);
   }
 }
diff --git a/source/blender/draw/engines/eevee_next/shaders/infos/eevee_film_info.hh b/source/blender/draw/engines/eevee_next/shaders/infos/eevee_film_info.hh
index 05d2d2f5bf0..072d3b1db2d 100644
--- a/source/blender/draw/engines/eevee_next/shaders/infos/eevee_film_info.hh
+++ b/source/blender/draw/engines/eevee_next/shaders/infos/eevee_film_info.hh
@@ -50,6 +50,7 @@ GPU_SHADER_CREATE_INFO(eevee_film_comp)
 GPU_SHADER_CREATE_INFO(eevee_film_cryptomatte_post)
     .do_static_compilation(true)
     .image(0, GPU_RGBA32F, Qualifier::READ_WRITE, ImageType::FLOAT_2D_ARRAY, "cryptomatte_img")
+    .image(0, GPU_R32F, Qualifier::READ, ImageType::FLOAT_2D_ARRAY, "weight_img")
     .push_constant(Type::INT, "cryptomatte_layer_len")
     .push_constant(Type::INT, "cryptomatte_samples_per_layer")
     .local_group_size(FILM_GROUP_SIZE, FILM_GROUP_SIZE)



More information about the Bf-blender-cvs mailing list