[Bf-blender-cvs] [625304320be] temp-eevee-next-cryptomatte: Use correct material hash + false color display.

Jeroen Bakker noreply at git.blender.org
Mon Aug 22 15:49:49 CEST 2022


Commit: 625304320beb3030b494add1c087f8af088c7ca2
Author: Jeroen Bakker
Date:   Mon Aug 22 15:49:35 2022 +0200
Branches: temp-eevee-next-cryptomatte
https://developer.blender.org/rB625304320beb3030b494add1c087f8af088c7ca2

Use correct material hash + false color display.

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

M	source/blender/draw/engines/eevee_next/eevee_cryptomatte.cc
M	source/blender/draw/engines/eevee_next/eevee_cryptomatte.hh
M	source/blender/draw/engines/eevee_next/eevee_pipeline.cc
M	source/blender/draw/engines/eevee_next/shaders/eevee_cryptomatte_lib.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_surf_forward_frag.glsl
M	source/blender/draw/engines/eevee_next/shaders/infos/eevee_material_info.hh

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

diff --git a/source/blender/draw/engines/eevee_next/eevee_cryptomatte.cc b/source/blender/draw/engines/eevee_next/eevee_cryptomatte.cc
index c69eb874949..5bcd66949bd 100644
--- a/source/blender/draw/engines/eevee_next/eevee_cryptomatte.cc
+++ b/source/blender/draw/engines/eevee_next/eevee_cryptomatte.cc
@@ -20,84 +20,12 @@ void Cryptomatte::init()
                  "Cryptomatte and film mismatch");
 }
 
-static float hash_id(const ID *id)
+float Cryptomatte::hash(const ID &id) const
 {
-  const char *name = &id->name[2];
+  const char *name = &id.name[2];
   const int name_len = BLI_strnlen(name, MAX_NAME - 2);
   uint32_t cryptomatte_hash = BKE_cryptomatte_hash(name, name_len);
   return BKE_cryptomatte_hash_to_float(cryptomatte_hash);
 }
 
-void Cryptomatte::add_hash(const Object *object, float4 &r_hash) const
-{
-  if (object_offset_ != -1) {
-    r_hash[object_offset_] = hash_id(&object->id);
-  }
-
-  if (asset_offset_ != -1) {
-    const Object *asset_object = object;
-    while (asset_object->parent != nullptr) {
-      asset_object = asset_object->parent;
-    }
-    r_hash[asset_offset_] = hash_id(&asset_object->id);
-  }
-}
-
-void Cryptomatte::add_hash(const ::Material *mat, float4 &r_hash) const
-{
-  if (material_offset_ != -1) {
-    r_hash[material_offset_] = mat ? hash_id(&mat->id) : 0.0f;
-  }
-}
-
-#if 0
-Find API that works from the material shader. (fill object info and material data. with hashes if those layers are enabled?)
-void Cryptomatte::sync_mesh(Object *ob)
-{
-  if (layer_len_ == 0) {
-    return;
-  }
-
-  float4 hash(0.0f, 0.0f, 0.0f, 0.0f);
-  add_hash(ob, hash);
-
-  // TODO(jbakker): decide based on availability of surface geom if we should have an optimzied
-  // path, or that it has more overhead (memory)
-  if (material_offset_ == -1) {
-    GPUBatch *geom = DRW_cache_object_surface_get(ob);
-    if (geom) {
-      DRWShadingGroup *grp = DRW_shgroup_create_sub(mesh_grp_);
-      DRW_shgroup_uniform_vec4_copy(grp, "cryptomatte_hash", hash);
-      DRW_shgroup_call(grp, geom, ob);
-    }
-  }
-  else {
-    bool has_motion = false;
-    // TODO:  inst_.velocity.step_object_sync(ob, ob_handle.object_key,
-    // ob_handle.recalc);
-
-    MaterialArray &material_array = inst_.materials.material_array_get(ob, has_motion);
-
-    GPUBatch **mat_geom = DRW_cache_object_surface_material_get(
-        ob, material_array.gpu_materials.data(), material_array.gpu_materials.size());
-
-    if (mat_geom == nullptr) {
-      return;
-    }
-
-    for (auto i : material_array.gpu_materials.index_range()) {
-      GPUBatch *geom = mat_geom[i];
-      if (geom == nullptr) {
-        continue;
-      }
-      const ::Material *material = GPU_material_get_material(material_array.gpu_materials[i]);
-      add_hash(material, hash);
-      DRWShadingGroup *grp = DRW_shgroup_create_sub(mesh_grp_);
-      DRW_shgroup_uniform_vec4_copy(grp, "cryptomatte_hash", hash);
-      DRW_shgroup_call(grp, geom, ob);
-    }
-  }
-}
-#endif
-
 }  // namespace blender::eevee
\ No newline at end of file
diff --git a/source/blender/draw/engines/eevee_next/eevee_cryptomatte.hh b/source/blender/draw/engines/eevee_next/eevee_cryptomatte.hh
index 20ef3a0ea97..c93497a0478 100644
--- a/source/blender/draw/engines/eevee_next/eevee_cryptomatte.hh
+++ b/source/blender/draw/engines/eevee_next/eevee_cryptomatte.hh
@@ -50,9 +50,7 @@ class Cryptomatte {
 
   void init();
 
- private:
-  void add_hash(const Object *object, float4 &r_hash) const;
-  void add_hash(const Material *material, float4 &r_hash) const;
+  float hash(const ID &id) const;
 };
 
 /** \} */
diff --git a/source/blender/draw/engines/eevee_next/eevee_pipeline.cc b/source/blender/draw/engines/eevee_next/eevee_pipeline.cc
index 0b8246abe2c..daf0fac7f0f 100644
--- a/source/blender/draw/engines/eevee_next/eevee_pipeline.cc
+++ b/source/blender/draw/engines/eevee_next/eevee_pipeline.cc
@@ -127,6 +127,8 @@ DRWShadingGroup *ForwardPipeline::material_opaque_add(::Material *blender_mat, G
   DRW_shgroup_uniform_image_ref(grp, "rp_specular_color_img", &rbufs.specular_color_tx);
   DRW_shgroup_uniform_image_ref(grp, "rp_emission_img", &rbufs.emission_tx);
   DRW_shgroup_uniform_image_ref(grp, "rp_cryptomatte_img", &rbufs.cryptomatte_tx);
+  float material_hash = inst_.cryptomatte.hash(blender_mat->id);
+  DRW_shgroup_uniform_float_copy(grp, "cryptomatte_material_hash", material_hash);
 
   /* TODO(fclem): Make this only needed if material uses it ... somehow. */
   // if (true) {
diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_cryptomatte_lib.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_cryptomatte_lib.glsl
index 9c525da67f9..b29967519c6 100644
--- a/source/blender/draw/engines/eevee_next/shaders/eevee_cryptomatte_lib.glsl
+++ b/source/blender/draw/engines/eevee_next/shaders/eevee_cryptomatte_lib.glsl
@@ -42,6 +42,9 @@ void film_store_cryptomatte_sample(FilmSample dst, int cryptomatte_layer_id, flo
 
 vec4 cryptomatte_false_color(float hash)
 {
-  /* TODO(jbakker): implement this (see CryptomatteOperationm::execute_pixel). */
-  return vec4(hash, 0.0, 0.0, 1.0);
+#define UINT32_MAX (4294967295U)
+
+  uint m3hash = floatBitsToUint(hash);
+  return vec4(
+      hash, float(m3hash << 8) / float(UINT32_MAX), float(m3hash << 16) / float(UINT32_MAX), 1.0);
 }
\ No newline at end of file
diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_surf_forward_frag.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_surf_forward_frag.glsl
index 46b2a39e7f6..9b237dac040 100644
--- a/source/blender/draw/engines/eevee_next/shaders/eevee_surf_forward_frag.glsl
+++ b/source/blender/draw/engines/eevee_next/shaders/eevee_surf_forward_frag.glsl
@@ -106,7 +106,11 @@ void main()
   imageStore(rp_diffuse_color_img, out_texel, vec4(g_diffuse_data.color, 1.0));
   imageStore(rp_specular_color_img, out_texel, vec4(specular_color, 1.0));
   imageStore(rp_emission_img, out_texel, vec4(g_emission, 1.0));
-  imageStore(rp_cryptomatte_img, out_texel, vec4(1.0, 0.5, 0.25, 1.0));
+  /* TODO(jbakker): read object and asset hash from object data? */
+  imageStore(
+      rp_cryptomatte_img,
+      out_texel,
+      vec4(cryptomatte_material_hash, cryptomatte_material_hash, cryptomatte_material_hash, 1.0));
 
   out_radiance.rgb *= 1.0 - g_holdout;
 
diff --git a/source/blender/draw/engines/eevee_next/shaders/infos/eevee_material_info.hh b/source/blender/draw/engines/eevee_next/shaders/infos/eevee_material_info.hh
index af4b746b6f7..c0cd5d27dc8 100644
--- a/source/blender/draw/engines/eevee_next/shaders/infos/eevee_material_info.hh
+++ b/source/blender/draw/engines/eevee_next/shaders/infos/eevee_material_info.hh
@@ -83,6 +83,7 @@ GPU_SHADER_CREATE_INFO(eevee_aov_out)
     .storage_buf(7, Qualifier::READ, "AOVsInfoData", "aov_buf");
 
 GPU_SHADER_CREATE_INFO(eevee_cryptomatte_out)
+    .push_constant(Type::FLOAT, "cryptomatte_material_hash")
     .image_out(7, Qualifier::WRITE, GPU_RGBA32F, "rp_cryptomatte_img");
 
 GPU_SHADER_CREATE_INFO(eevee_surf_deferred)



More information about the Bf-blender-cvs mailing list