[Bf-blender-cvs] [bf2306b1d4d] temp-eevee-next-cryptomatte: Calculate the correct material hash.

Jeroen Bakker noreply at git.blender.org
Tue Sep 13 08:29:10 CEST 2022


Commit: bf2306b1d4d7d8bbcfdd5f60e1ac08a898c6671f
Author: Jeroen Bakker
Date:   Tue Sep 13 08:28:54 2022 +0200
Branches: temp-eevee-next-cryptomatte
https://developer.blender.org/rBbf2306b1d4d7d8bbcfdd5f60e1ac08a898c6671f

Calculate the correct material hash.

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

M	source/blender/blenkernel/BKE_cryptomatte.hh
M	source/blender/blenkernel/intern/cryptomatte.cc
M	source/blender/gpu/intern/gpu_codegen.cc

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

diff --git a/source/blender/blenkernel/BKE_cryptomatte.hh b/source/blender/blenkernel/BKE_cryptomatte.hh
index c4a440a1867..dd08f7b5c4f 100644
--- a/source/blender/blenkernel/BKE_cryptomatte.hh
+++ b/source/blender/blenkernel/BKE_cryptomatte.hh
@@ -12,6 +12,7 @@
 
 #include "BKE_cryptomatte.h"
 
+#include "BLI_hash_mm3.h"
 #include "BLI_map.hh"
 #include "BLI_string_ref.hh"
 
@@ -54,10 +55,14 @@ struct CryptomatteHash {
   uint32_t hash;
 
   CryptomatteHash(uint32_t hash);
-  CryptomatteHash(const char *name, int name_len);
-  static CryptomatteHash from_hex_encoded(blender::StringRef hex_encoded);
+  CryptomatteHash(const char *name, int name_len)
+  {
+    hash = BLI_hash_mm3((const unsigned char *)name, name_len, 0);
+  }
 
+  static CryptomatteHash from_hex_encoded(blender::StringRef hex_encoded);
   std::string hex_encoded() const;
+
   /**
    * Convert a cryptomatte hash to a float.
    *
@@ -70,7 +75,20 @@ struct CryptomatteHash {
    *
    * Note that this conversion assumes to be running on a L-endian system.
    */
-  float float_encoded() const;
+  float float_encoded() const
+  {
+    uint32_t mantissa = hash & ((1 << 23) - 1);
+    uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
+    exponent = MAX2(exponent, (uint32_t)1);
+    exponent = MIN2(exponent, (uint32_t)254);
+    exponent = exponent << 23;
+    uint32_t sign = (hash >> 31);
+    sign = sign << 31;
+    uint32_t float_bits = sign | exponent | mantissa;
+    float f;
+    memcpy(&f, &float_bits, sizeof(uint32_t));
+    return f;
+  }
 };
 
 struct CryptomatteLayer {
diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc
index cb360931905..72204f6624e 100644
--- a/source/blender/blenkernel/intern/cryptomatte.cc
+++ b/source/blender/blenkernel/intern/cryptomatte.cc
@@ -505,11 +505,6 @@ CryptomatteHash::CryptomatteHash(uint32_t hash) : hash(hash)
 {
 }
 
-CryptomatteHash::CryptomatteHash(const char *name, const int name_len)
-{
-  hash = BLI_hash_mm3((const unsigned char *)name, name_len, 0);
-}
-
 CryptomatteHash CryptomatteHash::from_hex_encoded(blender::StringRef hex_encoded)
 {
   CryptomatteHash result(0);
@@ -524,21 +519,6 @@ std::string CryptomatteHash::hex_encoded() const
   return encoded.str();
 }
 
-float CryptomatteHash::float_encoded() const
-{
-  uint32_t mantissa = hash & ((1 << 23) - 1);
-  uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
-  exponent = MAX2(exponent, (uint32_t)1);
-  exponent = MIN2(exponent, (uint32_t)254);
-  exponent = exponent << 23;
-  uint32_t sign = (hash >> 31);
-  sign = sign << 31;
-  uint32_t float_bits = sign | exponent | mantissa;
-  float f;
-  memcpy(&f, &float_bits, sizeof(uint32_t));
-  return f;
-}
-
 std::unique_ptr<CryptomatteLayer> CryptomatteLayer::read_from_manifest(
     blender::StringRefNull manifest)
 {
diff --git a/source/blender/gpu/intern/gpu_codegen.cc b/source/blender/gpu/intern/gpu_codegen.cc
index 1e596aea8f8..75e148e0a8f 100644
--- a/source/blender/gpu/intern/gpu_codegen.cc
+++ b/source/blender/gpu/intern/gpu_codegen.cc
@@ -11,6 +11,7 @@
 
 #include "DNA_customdata_types.h"
 #include "DNA_image_types.h"
+#include "DNA_material_types.h"
 
 #include "BLI_ghash.h"
 #include "BLI_hash_mm2a.h"
@@ -20,6 +21,7 @@
 
 #include "PIL_time.h"
 
+#include "BKE_cryptomatte.hh"
 #include "BKE_material.h"
 
 #include "GPU_capabilities.h"
@@ -548,7 +550,16 @@ void GPUCodegen::generate_cryptomatte()
   cryptomatte_input_ = static_cast<GPUInput *>(MEM_callocN(sizeof(GPUInput), __func__));
   cryptomatte_input_->type = GPU_FLOAT;
   cryptomatte_input_->source = GPU_SOURCE_CRYPTOMATTE;
-  cryptomatte_input_->vec[0] = -10.0f;
+
+  float material_hash = 0.0f;
+  Material *material = GPU_material_get_material(&mat);
+  if (material) {
+    blender::bke::cryptomatte::CryptomatteHash hash(material->id.name,
+                                                    BLI_strnlen(material->id.name, MAX_NAME - 2));
+    material_hash = hash.float_encoded();
+  }
+  cryptomatte_input_->vec[0] = material_hash;
+
   BLI_addtail(&ubo_inputs_, BLI_genericNodeN(cryptomatte_input_));
 }



More information about the Bf-blender-cvs mailing list