Fix T68944: Added check for SSE4.1 to denoising node.
authorStefan Werner <stefan.werner@tangent-animation.com>
Tue, 27 Aug 2019 12:03:49 +0000 (14:03 +0200)
committerStefan Werner <stefan.werner@tangent-animation.com>
Tue, 27 Aug 2019 12:03:49 +0000 (14:03 +0200)
Since OpenImageDenoise requires a CPU with SSE 4.1 or newer,
let the node act as passthrough on unsupported CPUs and display
a message in the node itself.

source/blender/blenlib/BLI_system.h
source/blender/blenlib/intern/system.c
source/blender/compositor/operations/COM_DenoiseOperation.cpp
source/blender/editors/space_node/drawnode.c

index f4c0399e9594981d6256893a8a962c0259e7dff7..19797ba23bc5e10156b8fb1d025616ee628a1d3e 100644 (file)
@@ -24,6 +24,7 @@
  */
 
 int BLI_cpu_support_sse2(void);
+int BLI_cpu_support_sse41(void);
 void BLI_system_backtrace(FILE *fp);
 
 /* Get CPU brand, result is to be MEM_freeN()-ed. */
index 88f2e2625e80b8ab59806fe5a3966d8fe6ed6631..941c2b608e65e20e6541aa93d767c33dfe062495 100644 (file)
@@ -179,6 +179,19 @@ char *BLI_cpu_brand_string(void)
   return NULL;
 }
 
+int BLI_cpu_support_sse41(void)
+{
+  int result[4], num;
+  __cpuid(result, 0);
+  num = result[0];
+
+  if (num >= 1) {
+    __cpuid(result, 0x00000001);
+    return (result[2] & ((int)1 << 19)) != 0;
+  }
+  return 0;
+}
+
 void BLI_hostname_get(char *buffer, size_t bufsize)
 {
 #ifndef WIN32
index 233840aa5a885fca4dae66a890d3010dfec6cd9d..82a529dc2ef97dd26205def1caeedabf597ff0a9 100644 (file)
@@ -97,66 +97,69 @@ void DenoiseOperation::generateDenoise(float *data,
     return;
   }
 #ifdef WITH_OPENIMAGEDENOISE
-  oidn::DeviceRef device = oidn::newDevice();
-  device.commit();
+  if (BLI_cpu_support_sse41()) {
+    oidn::DeviceRef device = oidn::newDevice();
+    device.commit();
 
-  oidn::FilterRef filter = device.newFilter("RT");
-  filter.setImage("color",
-                  inputBufferColor,
-                  oidn::Format::Float3,
-                  inputTileColor->getWidth(),
-                  inputTileColor->getHeight(),
-                  0,
-                  4 * sizeof(float));
-  if (inputTileAlbedo && inputTileAlbedo->getBuffer()) {
-    filter.setImage("albedo",
-                    inputTileAlbedo->getBuffer(),
+    oidn::FilterRef filter = device.newFilter("RT");
+    filter.setImage("color",
+                    inputBufferColor,
                     oidn::Format::Float3,
-                    inputTileAlbedo->getWidth(),
-                    inputTileAlbedo->getHeight(),
+                    inputTileColor->getWidth(),
+                    inputTileColor->getHeight(),
                     0,
                     4 * sizeof(float));
-  }
-  if (inputTileNormal && inputTileNormal->getBuffer()) {
-    filter.setImage("normal",
-                    inputTileNormal->getBuffer(),
+    if (inputTileAlbedo && inputTileAlbedo->getBuffer()) {
+      filter.setImage("albedo",
+                      inputTileAlbedo->getBuffer(),
+                      oidn::Format::Float3,
+                      inputTileAlbedo->getWidth(),
+                      inputTileAlbedo->getHeight(),
+                      0,
+                      4 * sizeof(float));
+    }
+    if (inputTileNormal && inputTileNormal->getBuffer()) {
+      filter.setImage("normal",
+                      inputTileNormal->getBuffer(),
+                      oidn::Format::Float3,
+                      inputTileNormal->getWidth(),
+                      inputTileNormal->getHeight(),
+                      0,
+                      3 * sizeof(float));
+    }
+    filter.setImage("output",
+                    data,
                     oidn::Format::Float3,
-                    inputTileNormal->getWidth(),
-                    inputTileNormal->getHeight(),
+                    inputTileColor->getWidth(),
+                    inputTileColor->getHeight(),
                     0,
-                    3 * sizeof(float));
-  }
-  filter.setImage("output",
-                  data,
-                  oidn::Format::Float3,
-                  inputTileColor->getWidth(),
-                  inputTileColor->getHeight(),
-                  0,
-                  4 * sizeof(float));
+                    4 * sizeof(float));
 
-  BLI_assert(settings);
-  if (settings) {
-    filter.set("hdr", settings->hdr);
-    filter.set("srgb", false);
-  }
+    BLI_assert(settings);
+    if (settings) {
+      filter.set("hdr", settings->hdr);
+      filter.set("srgb", false);
+    }
 
-  filter.commit();
-  /* Since it's memory intensive, it's better to run only one instance of OIDN at a time.
-   * OpenImageDenoise is multithreaded internally and should use all available cores nonetheless.
-   */
-  BLI_mutex_lock(&oidn_lock);
-  filter.execute();
-  BLI_mutex_unlock(&oidn_lock);
+    filter.commit();
+    /* Since it's memory intensive, it's better to run only one instance of OIDN at a time.
+     * OpenImageDenoise is multithreaded internally and should use all available cores nonetheless.
+     */
+    BLI_mutex_lock(&oidn_lock);
+    filter.execute();
+    BLI_mutex_unlock(&oidn_lock);
 
-  /* copy the alpha channel, OpenImageDenoise currently only supports RGB */
-  size_t numPixels = inputTileColor->getWidth() * inputTileColor->getHeight();
-  for (size_t i = 0; i < numPixels; ++i) {
-    data[i * 4 + 3] = inputBufferColor[i * 4 + 3];
+    /* copy the alpha channel, OpenImageDenoise currently only supports RGB */
+    size_t numPixels = inputTileColor->getWidth() * inputTileColor->getHeight();
+    for (size_t i = 0; i < numPixels; ++i) {
+      data[i * 4 + 3] = inputBufferColor[i * 4 + 3];
+    }
+    return;
   }
-#else
+#endif
+  /* If built without OIDN or running on an unsupported CPU, just pass through. */
   UNUSED_VARS(inputTileAlbedo, inputTileNormal, settings);
   ::memcpy(data,
            inputBufferColor,
            inputTileColor->getWidth() * inputTileColor->getHeight() * sizeof(float) * 4);
-#endif
 }
index a69eb2546219cf7a53fa15331d697f86cfa8e35a..72a73f89227c69ce4506329a15b91bd1b448a04c 100644 (file)
@@ -2703,6 +2703,10 @@ static void node_composit_buts_denoise(uiLayout *layout, bContext *UNUSED(C), Po
 {
 #ifndef WITH_OPENIMAGEDENOISE
   uiItemL(layout, IFACE_("Disabled, built without OpenImageDenoise"), ICON_ERROR);
+#else
+  if (!BLI_cpu_support_sse41()) {
+    uiItemL(layout, IFACE_("Disabled, CPU with SSE4.1 is required"), ICON_ERROR);
+  }
 #endif
 
   uiItemR(layout, ptr, "use_hdr", 0, NULL, ICON_NONE);