[Bf-blender-cvs] [09d8466a385] cycles_embree: Cycles: Embree now traverses only the object subtree for local intersection tests.

Stefan Werner noreply at git.blender.org
Fri Aug 31 15:09:20 CEST 2018


Commit: 09d8466a3852e28689453e00169c9dd7b61e4dad
Author: Stefan Werner
Date:   Fri Aug 31 15:09:18 2018 +0200
Branches: cycles_embree
https://developer.blender.org/rB09d8466a3852e28689453e00169c9dd7b61e4dad

Cycles: Embree now traverses only the object subtree for local intersection tests.

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

M	intern/cycles/bvh/bvh.h
M	intern/cycles/bvh/bvh_embree.cpp
M	intern/cycles/kernel/bvh/bvh.h
M	intern/cycles/kernel/bvh/bvh_embree_traversal.h

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

diff --git a/intern/cycles/bvh/bvh.h b/intern/cycles/bvh/bvh.h
index 92ba8465a6f..f14c8f53c80 100644
--- a/intern/cycles/bvh/bvh.h
+++ b/intern/cycles/bvh/bvh.h
@@ -36,7 +36,6 @@ class Progress;
 
 #define BVH_ALIGN     4096
 #define TRI_NODE_SIZE 3
-#define BVH_CUSTOM -1
 /* Packed BVH
  *
  * BVH stored as it will be used for traversal on the rendering device. */
diff --git a/intern/cycles/bvh/bvh_embree.cpp b/intern/cycles/bvh/bvh_embree.cpp
index 9fbce0bde1e..6da9e79722a 100644
--- a/intern/cycles/bvh/bvh_embree.cpp
+++ b/intern/cycles/bvh/bvh_embree.cpp
@@ -129,12 +129,6 @@ void rtc_filter_occluded_func(const RTCFilterFunctionNArguments* args)
 		if(ctx->max_hits == 0) {
 			return;
 		}
-		/* Only accept hits from the same object and triangles. */
-		if(hit->instID[0]/2 != ctx->sss_object_id || hit->geomID & 1) {
-			/* This tells Embree to continue tracing. */
-			*args->valid = 0;
-			return;
-		}
 
 		/* See triangle_intersect_subsurface() for the native equivalent. */
 		for(int i = min(ctx->max_hits, ctx->ss_isect->num_hits) - 1; i >= 0; --i) {
@@ -163,7 +157,7 @@ void rtc_filter_occluded_func(const RTCFilterFunctionNArguments* args)
 			}
 		}
 		/* record intersection */
-		kernel_embree_convert_hit(kg, ray, hit, &ctx->ss_isect->hits[hit_idx]);
+		kernel_embree_convert_local_hit(kg, ray, hit, &ctx->ss_isect->hits[hit_idx], ctx->sss_object_id);
 		ctx->ss_isect->Ng[hit_idx].x = hit->Ng_x;
 		ctx->ss_isect->Ng[hit_idx].y = hit->Ng_y;
 		ctx->ss_isect->Ng[hit_idx].z = hit->Ng_z;
@@ -255,7 +249,8 @@ bool rtc_progress_func(void* user_ptr, const double n)
 	return !progress->get_cancel();
 }
 
-/* This is to have a shared device between all BVH instances */
+/* This is to have a shared device between all BVH instances.
+   It would be useful to actually to use a separte RTCDevice per Cycles instance. */
 RTCDevice BVHEmbree::rtc_shared_device = NULL;
 int BVHEmbree::rtc_shared_users = 0;
 thread_mutex BVHEmbree::rtc_shared_mutex;
@@ -308,8 +303,7 @@ BVHEmbree::BVHEmbree(const BVHParams& params_, const vector<Object*>& objects_)
 
 	rtcSetDeviceErrorFunction(rtc_shared_device, rtc_error_func, NULL);
 
-	/* BVH_CUSTOM as root index signals to the rest of the code that this is not Cycle's own BVH. */
-	pack.root_index = BVH_CUSTOM;
+	pack.root_index = -1;
 }
 
 BVHEmbree::~BVHEmbree()
@@ -807,10 +801,7 @@ void BVHEmbree::pack_nodes(const BVHNode *)
 		int mesh_curve_offset = mesh->curve_offset;
 
 		/* fill in node indexes for instances */
-		if(bvh->pack.root_index == -1)
-			pack.object_node[object_offset++] = prim_offset;//todo (Stefan)
-		else
-			pack.object_node[object_offset++] = prim_offset; // todo (Stefan)
+		pack.object_node[object_offset++] = prim_offset; // TOOD (stefan)
 
 		mesh_map[mesh] = pack.object_node[object_offset-1];
 
diff --git a/intern/cycles/kernel/bvh/bvh.h b/intern/cycles/kernel/bvh/bvh.h
index 7b0f41d4a70..429964aac57 100644
--- a/intern/cycles/kernel/bvh/bvh.h
+++ b/intern/cycles/kernel/bvh/bvh.h
@@ -243,7 +243,37 @@ ccl_device_intersect bool scene_intersect_local(KernelGlobals *kg,
 		IntersectContext rtc_ctx(&ctx);
 		RTCRay rtc_ray;
 		kernel_embree_setup_ray(ray, rtc_ray, PATH_RAY_ALL_VISIBILITY);
-		rtcOccluded1(kernel_data.bvh.scene, &rtc_ctx.context, &rtc_ray);
+
+		/* Get the Embree scene for this intersection. */
+		RTCGeometry geom = rtcGetGeometry(kernel_data.bvh.scene, local_object * 2);
+		if(geom) {
+			Transform ob_itfm;
+			float3 P = ray.P;
+			float3 dir = ray.D;
+			float3 idir = ray.D;
+			const int object_flag = kernel_tex_fetch(__object_flag, local_object);
+			if(!(object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
+				Transform ob_itfm;
+				rtc_ray.tfar = bvh_instance_motion_push(kg,
+												   local_object,
+												   &ray,
+												   &P,
+												   &dir,
+												   &idir,
+												   ray.t,
+												   &ob_itfm);
+				rtc_ray.org_x = P.x;
+				rtc_ray.org_y = P.y;
+				rtc_ray.org_z = P.z;
+				rtc_ray.dir_x = dir.x;
+				rtc_ray.dir_y = dir.y;
+				rtc_ray.dir_z = dir.z;
+			}
+			RTCScene scene = (RTCScene)rtcGetGeometryUserData(geom);
+			if(scene) {
+				rtcOccluded1(scene, &rtc_ctx.context, &rtc_ray);
+			}
+		}
 
 		return local_isect->num_hits > 0;
 	}
diff --git a/intern/cycles/kernel/bvh/bvh_embree_traversal.h b/intern/cycles/kernel/bvh/bvh_embree_traversal.h
index 4d887106922..a50ecc7e432 100644
--- a/intern/cycles/kernel/bvh/bvh_embree_traversal.h
+++ b/intern/cycles/kernel/bvh/bvh_embree_traversal.h
@@ -104,3 +104,15 @@ ccl_device_inline void kernel_embree_convert_hit(ccl::KernelGlobals *kg, const R
 	}
 	isect->type = kernel_tex_fetch(__prim_type, isect->prim);
 }
+
+ccl_device_inline void kernel_embree_convert_local_hit(ccl::KernelGlobals *kg, const RTCRay *ray, const RTCHit *hit, ccl::Intersection *isect, int local_object_id)
+{
+	isect->u = 1.0f - hit->v - hit->u;
+	isect->v = hit->u;
+	isect->t = ray->tfar;
+	isect->Ng = ccl::make_float3(hit->Ng_x, hit->Ng_y, hit->Ng_z);
+	RTCScene inst_scene = (RTCScene)rtcGetGeometryUserData(rtcGetGeometry(kernel_data.bvh.scene, local_object_id * 2));
+	isect->prim = hit->primID + (intptr_t)rtcGetGeometryUserData(rtcGetGeometry(inst_scene, hit->geomID)) + kernel_tex_fetch(__object_node, local_object_id);
+	isect->object = local_object_id;
+	isect->type = kernel_tex_fetch(__prim_type, isect->prim);
+}



More information about the Bf-blender-cvs mailing list