[Bf-blender-cvs] [bc98b748293] soc-2019-embree-gpu: Add Embree access on GPU

MATILLAT Quentin noreply at git.blender.org
Fri Jun 21 19:19:35 CEST 2019


Commit: bc98b748293312914867cb5764ed2beb2941fcb3
Author: MATILLAT Quentin
Date:   Fri Jun 21 19:12:27 2019 +0200
Branches: soc-2019-embree-gpu
https://developer.blender.org/rBbc98b748293312914867cb5764ed2beb2941fcb3

Add Embree access on GPU

Add shrink BVH4 to BVH2 to use Embree access on GPU

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

M	intern/cycles/blender/addon/properties.py
M	intern/cycles/blender/addon/ui.py
M	intern/cycles/blender/blender_sync.cpp
M	intern/cycles/bvh/bvh.cpp
M	intern/cycles/bvh/bvh_embree.cpp
M	intern/cycles/bvh/bvh_embree.h
M	intern/cycles/device/device_cpu.cpp
M	intern/cycles/device/device_cuda.cpp
M	intern/cycles/render/mesh.cpp

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

diff --git a/intern/cycles/blender/addon/properties.py b/intern/cycles/blender/addon/properties.py
index 2670c300d14..4da89ebd4a1 100644
--- a/intern/cycles/blender/addon/properties.py
+++ b/intern/cycles/blender/addon/properties.py
@@ -57,6 +57,13 @@ enum_bvh_layouts = (
     ('BVH8', "BVH8", "", 4),
 )
 
+enum_bvh_builder = (
+    ('INTERNAL', "Internal", "Use old internal BVH builder"),
+    ('EMBREE', "Embree", "Use full featured embree (only on CPU)"),
+    ('EMBREE_CONVERT', "Embree, then convert", "Use embree, then convert it to BVH2"),
+    ('EMBREE_BUILDER', "Embree builder", "Use Embree BVH Builder"),
+)
+
 enum_bvh_types = (
     ('DYNAMIC_BVH', "Dynamic BVH", "Objects can be individually updated, at the cost of slower render time"),
     ('STATIC_BVH', "Static BVH", "Any object modification requires a complete BVH rebuild, but renders faster"),
@@ -522,21 +529,13 @@ class CyclesRenderSettings(bpy.types.PropertyGroup):
         items=enum_bvh_types,
         default='DYNAMIC_BVH',
     )
-    use_bvh_embree: BoolProperty(
-        name="Use Embree",
-        description="Use Embree as ray accelerator",
-        default=False,
-    )
-    use_bvh_embree_converter: BoolProperty(
-        name="Convert Embree to Internal BVH",
-        description="Use Embree as ray accelerator",
-        default=False,
-    )
-    use_bvh_embree_gpu: BoolProperty(
-        name="Use Embree on GPU (experimental)",
-        description="Use Embree as ray accelerator",
-        default=False,
+    bvh_builder: EnumProperty(
+        name="BVH Builder",
+        description="Choose the BVH builder that will be used",
+        items=enum_bvh_builder,
+        default='INTERNAL',
     )
+
     debug_use_spatial_splits: BoolProperty(
         name="Use Spatial Splits",
         description="Use BVH spatial splits: longer builder time, faster render",
diff --git a/intern/cycles/blender/addon/ui.py b/intern/cycles/blender/addon/ui.py
index b970de02fde..8f95586c793 100644
--- a/intern/cycles/blender/addon/ui.py
+++ b/intern/cycles/blender/addon/ui.py
@@ -645,24 +645,13 @@ class CYCLES_RENDER_PT_performance_acceleration_structure(CyclesButtonsPanel, Pa
         cscene = scene.cycles
 
         col = layout.column()
-
-        if _cycles.with_embree:
-            row = col.row()
-            row.active = use_cpu(context)
-            row.prop(cscene, "use_bvh_embree")
-            row2 = col.row()
-            row2.active = cscene.use_bvh_embree
-            row2.prop(cscene, "use_bvh_embree_converter")
-        if _cycles.with_embree:
-            row = col.row()
-            row.active = use_cuda(context)
-            row.prop(cscene, "use_bvh_embree_gpu")
+        col.prop(cscene, "bvh_builder")
         col.prop(cscene, "debug_use_spatial_splits")
         sub = col.column()
-        sub.active = not cscene.use_bvh_embree or not _cycles.with_embree
+        sub.active = cscene.bvh_builder != 'EMBREE' or not _cycles.with_embree
         sub.prop(cscene, "debug_use_hair_bvh")
         sub = col.column()
-        sub.active = not cscene.debug_use_spatial_splits and not cscene.use_bvh_embree
+        sub.active = not cscene.debug_use_spatial_splits and cscene.bvh_builder != 'EMBREE'
         sub.prop(cscene, "debug_bvh_time_steps")
 
 
diff --git a/intern/cycles/blender/blender_sync.cpp b/intern/cycles/blender/blender_sync.cpp
index a39b7f053ff..e249c86f403 100644
--- a/intern/cycles/blender/blender_sync.cpp
+++ b/intern/cycles/blender/blender_sync.cpp
@@ -695,12 +695,17 @@ SceneParams BlenderSync::get_scene_params(BL::Scene &b_scene, bool background)
   }
 
 #ifdef WITH_EMBREE
-  params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree") ? BVH_LAYOUT_EMBREE :
-                                                                   params.bvh_layout;
-  params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree_converter") ? BVH_LAYOUT_EMBREE_CONVERTED :
-                                                                             params.bvh_layout;
-  params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree_gpu") ? BVH_LAYOUT_EMBREE_GPU :
-                                                                       params.bvh_layout;
+  switch (RNA_enum_get(&cscene, "bvh_builder")) {
+  case 1:
+      params.bvh_layout = BVH_LAYOUT_EMBREE;
+      break;
+  case 2:
+      params.bvh_layout = BVH_LAYOUT_EMBREE_CONVERTED;
+      break;
+  case 3:
+      params.bvh_layout = BVH_LAYOUT_EMBREE_GPU;
+      break;
+  }
 #endif
   return params;
 }
diff --git a/intern/cycles/bvh/bvh.cpp b/intern/cycles/bvh/bvh.cpp
index 17687ff6e74..2269ca3be3c 100644
--- a/intern/cycles/bvh/bvh.cpp
+++ b/intern/cycles/bvh/bvh.cpp
@@ -52,6 +52,8 @@ const char *bvh_layout_name(BVHLayout layout)
       return "NONE";
     case BVH_LAYOUT_EMBREE:
       return "EMBREE";
+    case BVH_LAYOUT_EMBREE_CONVERTED:
+      return "EMBREE_CONVERTED";
     case BVH_LAYOUT_EMBREE_GPU:
       return "EMBREE_GPU";
     case BVH_LAYOUT_ALL:
@@ -100,6 +102,7 @@ BVH::BVH(const BVHParams &params_, const vector<Object *> &objects_)
 
 BVH *BVH::create(const BVHParams &params, const vector<Object *> &objects)
 {
+  std::cout << "Using layout : " << bvh_layout_name(params.bvh_layout) << std::endl;
   switch (params.bvh_layout) {
     case BVH_LAYOUT_BVH2:
       return new BVH2(params, objects);
diff --git a/intern/cycles/bvh/bvh_embree.cpp b/intern/cycles/bvh/bvh_embree.cpp
index b2fd464b16d..778315ddaef 100644
--- a/intern/cycles/bvh/bvh_embree.cpp
+++ b/intern/cycles/bvh/bvh_embree.cpp
@@ -199,6 +199,20 @@ BVHNode* print_bvhInfo(RTCScene scene) {
     return nullptr;
 }
 
+BVHNode *bvh_shrink(BVHNode *root) {
+    if(root->num_children() <= 2) return root;
+
+    InnerNode *node = dynamic_cast<InnerNode*>(root);
+
+    node->children[0] = new InnerNode(root->bounds, bvh_shrink(node->children[0]), bvh_shrink(node->children[1]));
+    if(root->num_children() == 3) {
+        node->children[1] = bvh_shrink(node->children[2]);
+    } else {
+        node->children[1] = new InnerNode(root->bounds, bvh_shrink(node->children[2]), bvh_shrink(node->children[3]));
+    }
+    node->num_children_ = 2;
+    return node;
+}
 
 
 /* This gets called by Embree at every valid ray/object intersection.
@@ -429,7 +443,7 @@ thread_mutex BVHEmbree::rtc_shared_mutex;
 
 BVHEmbree::BVHEmbree(const BVHParams &params_, const vector<Object *> &objects_)
     : bvh_layout(params_.bvh_layout),
-      BVH4(params_, objects_),
+      BVH2(params_, objects_),
       scene(NULL),
       mem_used(0),
       top_level(NULL),
@@ -637,9 +651,11 @@ void BVHEmbree::build(Progress &progress, Stats *stats_)
   progress.set_substatus("Packing geometry");
   if(this->bvh_layout == BVH_LAYOUT_EMBREE_CONVERTED) {
     BVHNode *root = print_bvhInfo(scene);
+    std::cout << "SAH4 " << root->computeSubtreeSAHCost(this->params) << std::endl;
     root->print();
+    root = bvh_shrink(root);
     pack_nodes(root);
-    std::cout << "SAH " << root->computeSubtreeSAHCost(this->params) << std::endl;
+    std::cout << "SAH2 " << root->computeSubtreeSAHCost(this->params) << std::endl;
   } else {
     pack_nodes(NULL);
   }
@@ -960,7 +976,7 @@ void BVHEmbree::add_curves(Object *ob, int i)
 void BVHEmbree::pack_nodes(const BVHNode *r)
 {
   if(this->bvh_layout == BVH_LAYOUT_EMBREE_CONVERTED) {
-    BVH4::pack_nodes(r);
+    BVH2::pack_nodes(r);
   } else {
       /* Quite a bit of this code is for compatibility with Cycles' native BVH. */
       if (!params.top_level) {
diff --git a/intern/cycles/bvh/bvh_embree.h b/intern/cycles/bvh/bvh_embree.h
index 9709ef8fbff..963cdd6955d 100644
--- a/intern/cycles/bvh/bvh_embree.h
+++ b/intern/cycles/bvh/bvh_embree.h
@@ -23,7 +23,7 @@
 #  include <embree3/rtcore_scene.h>
 
 #  include "bvh/bvh.h"
-#  include "bvh/bvh4.h"
+#  include "bvh/bvh2.h"
 #  include "bvh/bvh_params.h"
 
 #  include "util/util_thread.h"
@@ -34,10 +34,10 @@ CCL_NAMESPACE_BEGIN
 
 class Mesh;
 
-class BVHEmbree : public BVH4 {
+class BVHEmbree : public BVH2 {
  public:
   virtual void build(Progress &progress, Stats *stats) override;
-  virtual ~BVHEmbree();
+  virtual ~BVHEmbree() override;
   RTCScene scene;
   static void destroy(RTCScene);
 
diff --git a/intern/cycles/device/device_cpu.cpp b/intern/cycles/device/device_cpu.cpp
index 813deae7ac5..01dafcfca6e 100644
--- a/intern/cycles/device/device_cpu.cpp
+++ b/intern/cycles/device/device_cpu.cpp
@@ -338,7 +338,7 @@ class CPUDevice : public Device {
     }
 #endif
 #ifdef WITH_EMBREE
-    bvh_layout_mask |= BVH_LAYOUT_EMBREE | BVH_LAYOUT_EMBREE_CONVERTED;
+    bvh_layout_mask |= BVH_LAYOUT_EMBREE | BVH_LAYOUT_EMBREE_CONVERTED | BVH_LAYOUT_EMBREE_GPU;
 #endif /* WITH_EMBREE */
     return bvh_layout_mask;
   }
diff --git a/intern/cycles/device/device_cuda.cpp b/intern/cycles/device/device_cuda.cpp
index 79a9102a171..49d25914c7f 100644
--- a/intern/cycles/device/device_cuda.cpp
+++ b/intern/cycles/device/device_cuda.cpp
@@ -184,7 +184,7 @@ class CUDADevice : public Device {
 
   virtual BVHLayoutMask get_bvh_layout_mask() const
   {
-    return BVH_LAYOUT_BVH2 | BVH_LAYOUT_EMBREE_GPU;
+    return BVH_LAYOUT_BVH2 | BVH_LAYOUT_EMBREE_GPU | BVH_LAYOUT_EMBREE_CONVERTED;
   }
 
   /*#ifdef NDEBUG
diff --git a/intern/cycles/render/mesh.cpp b/intern/cycles/render/mesh.cpp
index 6f09f854f3a..45cf8e3a149 100644
--- a/intern/cycles/render/mesh.cpp
+++ b/intern/cycles/render/mesh.cpp
@@ -1980,12 +1980,9 @@ void MeshManager::device_update_bvh(Device *device,
   dscene->data.bvh.use_bvh_steps = (scene->params.num_bvh_time_steps != 0);
 
 #ifdef WITH_EMBREE
-  if(bparams.bvh_layout == BVH_LAYOUT_EMBREE_GPU) {
+  if(bparams.bvh_layout == BVH_LAYOUT_EMBREE_GPU || bparams.bvh_layout == BVH_LAYOUT_EMBREE_CONVERTED) {
     dscene->data.bvh.bvh_layout = BVH_LAYOUT_BVH2;
   }
-  if (bparams.bvh_layout == BVH_LAYOUT_EMBREE_CONVERTED) {
-    dscene->data.bvh.bvh_layout = BVH_LAYOUT_BVH4;
-  }
 
   if (bparams.bvh_layout == BVH_LAYOUT_EMBREE) {
     dscene->data.bvh.scene = ((BVHEmbree *)bvh)->scene;



More information about the Bf-blender-cvs mailing list