/** * @file test_gpu_bvh.cpp * @brief VDE-1284:GPU BVH 批量射线最近命中查询测试。 */ #include "vde/compute/gpu_bvh.h" #include "vde/compute/lbvh.h" #include #include #include #include #include #include #include namespace { using vde::compute::GpuBvh; using vde::compute::GpuBvhBuildOptions; using vde::compute::GpuBvhClosestPoint; using vde::compute::GpuBvhRayHit; /// z=0 平面上的 2 个三角形方片([-1,1]^2)。 std::vector make_quad() { return { -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, }; } /// 高度场网格(n x n 顶点,z = amp*sin(x)*cos(y)),返回三角形顶点数组。 std::vector make_heightfield(int n, double amp = 0.15) { const double step = 2.0 / static_cast(n - 1); auto z_at = [amp](double x, double y) { return amp * std::sin(x) * std::cos(y); }; std::vector tris; tris.reserve(static_cast(n - 1) * (n - 1) * 18); for (int j = 0; j + 1 < n; ++j) { for (int i = 0; i + 1 < n; ++i) { const double x0 = -1.0 + step * i, x1 = x0 + step; const double y0 = -1.0 + step * j, y1 = y0 + step; const double z00 = z_at(x0, y0), z10 = z_at(x1, y0); const double z01 = z_at(x0, y1), z11 = z_at(x1, y1); tris.insert(tris.end(), {x0, y0, z00, x1, y0, z10, x1, y1, z11}); tris.insert(tris.end(), {x0, y0, z00, x1, y1, z11, x0, y1, z01}); } } return tris; } /// 每个三角形质心上方 4 单位处沿 -z 的射线(每条恰命中对应三角形一次)。 std::vector centroid_rays(const std::vector& tris) { const std::size_t count = tris.size() / 9; std::vector rays(count * 6); for (std::size_t i = 0; i < count; ++i) { const double* v = tris.data() + i * 9; rays[i * 6 + 0] = (v[0] + v[3] + v[6]) / 3.0; rays[i * 6 + 1] = (v[1] + v[4] + v[7]) / 3.0; rays[i * 6 + 2] = (v[2] + v[5] + v[8]) / 3.0 + 4.0; rays[i * 6 + 3] = 0.0; rays[i * 6 + 4] = 0.0; rays[i * 6 + 5] = -1.0; } return rays; } std::vector query(GpuBvh& bvh, const std::vector& rays) { std::vector hits(rays.size() / 6); EXPECT_TRUE(bvh.ray_cast_nearest(rays.data(), hits.size(), hits.data())); return hits; } } // namespace TEST(GpuBvhTest, InvalidInputsAreRejected) { GpuBvh bvh; EXPECT_FALSE(bvh.ready()); EXPECT_FALSE(bvh.gpu_ready()); EXPECT_EQ(bvh.triangle_count(), 0u); GpuBvhRayHit hit{}; EXPECT_FALSE(bvh.build(nullptr, 4)); std::vector tris = make_quad(); EXPECT_FALSE(bvh.build(tris.data(), 0)); EXPECT_FALSE(bvh.ray_cast_nearest(nullptr, 1, &hit)); EXPECT_FALSE(bvh.ray_cast_nearest_cpu(nullptr, 1, &hit)); } TEST(GpuBvhTest, CpuOnlyBuildAndQuery) { GpuBvh bvh; GpuBvhBuildOptions options; options.prefer_gpu = false; std::vector tris = make_quad(); ASSERT_TRUE(bvh.build(tris.data(), tris.size() / 9, options)); EXPECT_TRUE(bvh.ready()); EXPECT_FALSE(bvh.gpu_ready()); EXPECT_EQ(bvh.triangle_count(), 2u); const auto& info = bvh.build_info(); EXPECT_TRUE(info.ok); EXPECT_FALSE(info.gpu); EXPECT_EQ(info.backend, "cpu-lbvh"); EXPECT_GE(info.node_count, 1); std::vector rays = centroid_rays(tris); auto hits = query(bvh, rays); ASSERT_EQ(hits.size(), 2u); for (std::size_t i = 0; i < hits.size(); ++i) { EXPECT_TRUE(hits[i].hit); EXPECT_EQ(hits[i].prim, static_cast(i)); EXPECT_DOUBLE_EQ(hits[i].t, 4.0); } const auto stats = bvh.last_query_stats(); EXPECT_FALSE(stats.gpu); EXPECT_EQ(stats.hit_count, 2u); // 朝上的射线不应命中。 std::vector miss_rays = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0}; auto miss_hits = query(bvh, miss_rays); ASSERT_EQ(miss_hits.size(), 1u); EXPECT_FALSE(miss_hits[0].hit); EXPECT_EQ(miss_hits[0].prim, -1); EXPECT_EQ(bvh.last_query_stats().hit_count, 0u); bvh.clear(); EXPECT_FALSE(bvh.ready()); EXPECT_EQ(bvh.triangle_count(), 0u); EXPECT_FALSE(bvh.ray_cast_nearest(rays.data(), hits.size(), hits.data())); } TEST(GpuBvhTest, CpuQueriesAgreeWithGpuWhenAvailable) { if (!GpuBvh::gpu_supported()) { GTEST_SKIP() << "no OpenCL GPU available"; } std::vector tris = make_heightfield(17); std::vector rays = centroid_rays(tris); GpuBvh cpu; GpuBvhBuildOptions cpu_options; cpu_options.prefer_gpu = false; ASSERT_TRUE(cpu.build(tris.data(), tris.size() / 9, cpu_options)); auto cpu_hits = query(cpu, rays); ASSERT_EQ(cpu_hits.size(), rays.size() / 6); for (const auto& hit : cpu_hits) ASSERT_TRUE(hit.hit); GpuBvh gpu; GpuBvhBuildOptions gpu_options; gpu_options.prefer_gpu = true; ASSERT_TRUE(gpu.build(tris.data(), tris.size() / 9, gpu_options)); EXPECT_TRUE(gpu.build_info().gpu) << gpu.build_info().backend; ASSERT_TRUE(gpu.gpu_ready()); EXPECT_FALSE(gpu.build_info().device_name.empty()); auto gpu_hits = query(gpu, rays); ASSERT_EQ(gpu_hits.size(), cpu_hits.size()); std::size_t mismatched_hits = 0; for (std::size_t i = 0; i < cpu_hits.size(); ++i) { ASSERT_TRUE(gpu_hits[i].hit) << "ray " << i << " missed on GPU"; EXPECT_EQ(gpu_hits[i].prim, cpu_hits[i].prim) << "ray " << i; // GPU 只定位候选;t 由 host 双精度回算,口径与 CPU 完全一致。 EXPECT_DOUBLE_EQ(gpu_hits[i].t, cpu_hits[i].t) << "ray " << i; mismatched_hits += (gpu_hits[i].prim != cpu_hits[i].prim) ? 1 : 0; } EXPECT_EQ(mismatched_hits, 0u); const auto stats = gpu.last_query_stats(); EXPECT_TRUE(stats.gpu); EXPECT_EQ(stats.hit_count, rays.size() / 6); EXPECT_GE(stats.kernel_ms, 0.0); EXPECT_GE(stats.upload_ms, 0.0); EXPECT_GE(stats.readback_ms, 0.0); } TEST(GpuBvhTest, GpuVerifiedQueryMatchesCpuAtLargeWorldOffset) { if (!GpuBvh::gpu_supported()) { GTEST_SKIP() << "no OpenCL GPU available"; } // CAD 模型常远离世界原点:float32 绝对坐标精度退化为 2^-23*|p|(4096 偏移 // 约 4.9e-4),掠边射线可能漏命中或选中远处三角形。实现以模型包围盒中心 // 作为 GPU float32 缓冲的局部原点,并对 miss 做 CPU 双精度复核,因此复核后 // 的结果必须与 CPU 权威逐位一致。 std::vector tris = make_heightfield(25); const double offset = 4096.0; for (std::size_t i = 0; i < tris.size(); ++i) { const std::size_t axis = i % 3; if (axis < 2) tris[i] += offset; } std::mt19937_64 rng(20261104); std::uniform_real_distribution u01(0.0, 1.0); const std::size_t tri_count = tris.size() / 9; constexpr std::size_t kRays = 2048; std::vector rays(kRays * 6); for (std::size_t r = 0; r < kRays; ++r) { const std::size_t t = static_cast(rng() % tri_count); const double* v = tris.data() + t * 9; const double s = u01(rng); double px = 0.0, py = 0.0; // 取三角形两条边之一上的点:精确落在共享边上,float32 舍入最容易 // 把射线推到邻接三角形一侧,正是需要复核的病态场景。 if ((r & 1u) == 0) { px = v[0] + s * (v[3] - v[0]); py = v[1] + s * (v[4] - v[1]); } else { px = v[0] + s * (v[6] - v[0]); py = v[1] + s * (v[7] - v[1]); } rays[r * 6 + 0] = px; rays[r * 6 + 1] = py; rays[r * 6 + 2] = 2.0; rays[r * 6 + 3] = 0.0; rays[r * 6 + 4] = 0.0; rays[r * 6 + 5] = -1.0; } GpuBvh cpu; GpuBvhBuildOptions cpu_options; cpu_options.prefer_gpu = false; ASSERT_TRUE(cpu.build(tris.data(), tri_count, cpu_options)); std::vector cpu_hits(kRays); ASSERT_TRUE(cpu.ray_cast_nearest(rays.data(), kRays, cpu_hits.data())); GpuBvh gpu; GpuBvhBuildOptions gpu_options; gpu_options.prefer_gpu = true; ASSERT_TRUE(gpu.build(tris.data(), tri_count, gpu_options)); ASSERT_TRUE(gpu.gpu_ready()) << gpu.build_info().backend; // 先跑未复核口径,统计内核原始 miss 与差异,确认本场景确实触发复核。 std::vector raw_hits(kRays); ASSERT_TRUE(gpu.ray_cast_nearest(rays.data(), kRays, raw_hits.data(), false)); const auto raw_stats = gpu.last_query_stats(); EXPECT_EQ(raw_stats.verify_count, 0); EXPECT_DOUBLE_EQ(raw_stats.verify_ms, 0.0); std::size_t raw_misses = 0; for (std::size_t i = 0; i < kRays; ++i) { if (!raw_hits[i].hit) ++raw_misses; } std::vector gpu_hits(kRays); ASSERT_TRUE(gpu.ray_cast_nearest(rays.data(), kRays, gpu_hits.data())); const auto stats = gpu.last_query_stats(); EXPECT_TRUE(stats.gpu); EXPECT_EQ(stats.verify_count, static_cast(raw_misses)); EXPECT_GE(stats.verify_ms, 0.0); std::size_t hit_count = 0; for (std::size_t i = 0; i < kRays; ++i) { hit_count += gpu_hits[i].hit ? 1u : 0u; ASSERT_EQ(gpu_hits[i].hit, cpu_hits[i].hit) << "ray " << i; if (!cpu_hits[i].hit) continue; // 射线精确落在共享边上时,两个邻接三角形到射线的距离完全相同 // (实测 dt ≤ 4 ULP),命中哪一个属于并列选择,不要求与 CPU 选中同一 // 图元;距离本身仍必须一致(复核口径下至多 4 ULP)。 const double t_tol = 1e-12 * std::max(1.0, std::fabs(cpu_hits[i].t)); EXPECT_NEAR(gpu_hits[i].t, cpu_hits[i].t, t_tol) << "ray " << i; if (std::fabs(gpu_hits[i].t - cpu_hits[i].t) > t_tol) { EXPECT_EQ(gpu_hits[i].prim, cpu_hits[i].prim) << "ray " << i; } } EXPECT_EQ(stats.hit_count, static_cast(hit_count)); EXPECT_EQ(hit_count, static_cast(stats.hit_count)); } TEST(GpuBvhTest, GpuRequestHonestlyFallsBackWhenUnavailable) { if (GpuBvh::gpu_supported()) { GTEST_SKIP() << "OpenCL GPU available; fallback path not exercised"; } std::vector tris = make_quad(); GpuBvh bvh; GpuBvhBuildOptions options; options.prefer_gpu = true; ASSERT_TRUE(bvh.build(tris.data(), tris.size() / 9, options)); EXPECT_TRUE(bvh.ready()); EXPECT_FALSE(bvh.gpu_ready()); EXPECT_FALSE(bvh.build_info().gpu); EXPECT_EQ(bvh.build_info().backend, "cpu-lbvh"); std::vector rays = centroid_rays(tris); auto hits = query(bvh, rays); for (const auto& hit : hits) EXPECT_TRUE(hit.hit); } TEST(GpuBvhTest, DeviceDescriptionMatchesSupportFlag) { const bool supported = GpuBvh::gpu_supported(); const std::string desc = GpuBvh::device_description(); EXPECT_EQ(supported, !desc.empty()); } /// 最近点查询的查询点集:75% 采样在三角形内部并沿法向偏移(模拟偏差分析/ /// 测厚:真实场景查询点大多贴近表面),25% 均匀落在包围盒内(远场点)。 std::vector closest_query_points(const std::vector& tris, std::size_t count, unsigned seed) { std::mt19937 rng(seed); std::uniform_real_distribution u01(0.0, 1.0); const std::size_t tri_count = tris.size() / 9; std::vector pts(count * 3); for (std::size_t i = 0; i < count; ++i) { double p[3] = {0.0, 0.0, 0.0}; if (tri_count > 0 && (i % 4) != 3) { const std::size_t t = static_cast(rng() % tri_count); const double* v = tris.data() + t * 9; double a = u01(rng), b = u01(rng); if (a + b > 1.0) { a = 1.0 - a; b = 1.0 - b; } const double c = 1.0 - a - b; for (int k = 0; k < 3; ++k) p[k] = a * v[k] + b * v[3 + k] + c * v[6 + k]; const double e1[3] = {v[3] - v[0], v[4] - v[1], v[5] - v[2]}; const double e2[3] = {v[6] - v[0], v[7] - v[1], v[8] - v[2]}; const double n[3] = {e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]}; const double len = std::sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]); const double off = (u01(rng) * 2.0 - 1.0) * 0.2; if (len > 0.0) { for (int k = 0; k < 3; ++k) p[k] += n[k] / len * off; } } else { for (int k = 0; k < 3; ++k) p[k] = u01(rng) * 3.0 - 1.5; } pts[i * 3 + 0] = p[0]; pts[i * 3 + 1] = p[1]; pts[i * 3 + 2] = p[2]; } return pts; } /// 全量暴力最近三角形(测试参考口径)。 void brute_closest(const std::vector& tris, const double* p, double* distance, int* prim) { const std::size_t tri_count = tris.size() / 9; double best_d2 = 0.0; int best_prim = -1; for (std::size_t t = 0; t < tri_count; ++t) { const double d2 = vde::compute::lbvh_point_triangle_dist_sq( tris.data(), static_cast(t), p); if (best_prim < 0 || d2 < best_d2) { best_d2 = d2; best_prim = static_cast(t); } } *prim = best_prim; *distance = best_prim < 0 ? 0.0 : std::sqrt(best_d2); } TEST(GpuBvhTest, ClosestPointInvalidInputsRejected) { GpuBvh bvh; GpuBvhClosestPoint cp{}; std::vector points = {0.0, 0.0, 0.0}; EXPECT_FALSE(bvh.closest_point(points.data(), 1, &cp)); EXPECT_FALSE(bvh.closest_point_cpu(points.data(), 1, &cp)); std::vector tris = make_quad(); GpuBvhBuildOptions options; options.prefer_gpu = false; ASSERT_TRUE(bvh.build(tris.data(), tris.size() / 9, options)); EXPECT_FALSE(bvh.closest_point(nullptr, 1, &cp)); EXPECT_FALSE(bvh.closest_point(points.data(), 1, nullptr)); EXPECT_FALSE(bvh.closest_point_cpu(nullptr, 1, &cp)); // 0 点查询是合法空操作(清空统计并返回 true)。 EXPECT_TRUE(bvh.closest_point(points.data(), 0, &cp)); EXPECT_TRUE(bvh.closest_point_cpu(nullptr, 0, &cp)); EXPECT_TRUE(bvh.closest_point_cpu(points.data(), 0, &cp)); } TEST(GpuBvhTest, ClosestPointCpuMatchesBruteForce) { const std::vector tris = make_heightfield(9); GpuBvh bvh; GpuBvhBuildOptions options; options.prefer_gpu = false; ASSERT_TRUE(bvh.build(tris.data(), tris.size() / 9, options)); constexpr std::size_t kPoints = 200; const std::vector points = closest_query_points(tris, kPoints, 12345); std::vector out(kPoints); ASSERT_TRUE(bvh.closest_point_cpu(points.data(), kPoints, out.data())); ASSERT_EQ(bvh.last_query_stats().hit_count, kPoints); for (std::size_t i = 0; i < kPoints; ++i) { const double* p = points.data() + i * 3; double ref_distance = 0.0; int ref_prim = -1; brute_closest(tris, p, &ref_distance, &ref_prim); ASSERT_TRUE(out[i].found) << "point " << i; EXPECT_GE(out[i].prim, 0); ASSERT_LT(static_cast(out[i].prim), tris.size() / 9); const double tol = 1e-12 * std::max(1.0, ref_distance); // 距离相等即可;最近三角形并列时允许选中不同图元。 EXPECT_NEAR(out[i].distance, ref_distance, tol) << "point " << i; // 返回的最近点必须在选中的三角形上,且到查询点的距离等于 distance。 const double d2 = vde::compute::lbvh_point_triangle_dist_sq( tris.data(), out[i].prim, p); EXPECT_NEAR(out[i].distance, std::sqrt(d2), tol) << "point " << i; const double dx = out[i].point[0] - p[0]; const double dy = out[i].point[1] - p[1]; const double dz = out[i].point[2] - p[2]; EXPECT_NEAR(std::sqrt(dx * dx + dy * dy + dz * dz), out[i].distance, 1e-9 * std::max(1.0, out[i].distance)) << "point " << i; } } TEST(GpuBvhTest, ClosestPointGpuAgreesWithCpu) { if (!GpuBvh::gpu_supported()) { GTEST_SKIP() << "no OpenCL GPU available"; } const std::vector tris = make_heightfield(25); GpuBvh cpu; GpuBvhBuildOptions cpu_options; cpu_options.prefer_gpu = false; ASSERT_TRUE(cpu.build(tris.data(), tris.size() / 9, cpu_options)); GpuBvh gpu; GpuBvhBuildOptions gpu_options; gpu_options.prefer_gpu = true; ASSERT_TRUE(gpu.build(tris.data(), tris.size() / 9, gpu_options)); ASSERT_TRUE(gpu.gpu_ready()) << gpu.build_info().backend; constexpr std::size_t kPoints = 512; const std::vector points = closest_query_points(tris, kPoints, 777); std::vector cpu_out(kPoints); std::vector raw_out(kPoints); std::vector gpu_out(kPoints); ASSERT_TRUE(cpu.closest_point_cpu(points.data(), kPoints, cpu_out.data())); // 先跑未复核口径,量化 float32 候选选择的原始差异。 ASSERT_TRUE(gpu.closest_point(points.data(), kPoints, raw_out.data(), false)); const auto raw_stats = gpu.last_query_stats(); EXPECT_TRUE(raw_stats.gpu); EXPECT_EQ(raw_stats.verify_count, 0); EXPECT_EQ(raw_stats.hit_count, static_cast(kPoints)); std::size_t raw_distance_diffs = 0; double raw_max_diff = 0.0; for (std::size_t i = 0; i < kPoints; ++i) { ASSERT_TRUE(raw_out[i].found) << "point " << i; const double diff = std::fabs(raw_out[i].distance - cpu_out[i].distance); raw_max_diff = std::max(raw_max_diff, diff); if (diff > 1e-9) ++raw_distance_diffs; } // float32 只定位候选;未复核时距离误差应仍远小于模型尺度(这里 ≤1e-3)。 EXPECT_LE(raw_max_diff, 1e-3) << "raw_distance_diffs=" << raw_distance_diffs; // 复核口径:以候选为上界做 CPU 双精度有界遍历,结果必须与 CPU 权威一致。 ASSERT_TRUE(gpu.closest_point(points.data(), kPoints, gpu_out.data(), true)); const auto stats = gpu.last_query_stats(); EXPECT_TRUE(stats.gpu); EXPECT_EQ(stats.verify_count, static_cast(kPoints)); EXPECT_GE(stats.verify_ms, 0.0); EXPECT_EQ(stats.hit_count, static_cast(kPoints)); for (std::size_t i = 0; i < kPoints; ++i) { const double tol = 1e-12 * std::max(1.0, cpu_out[i].distance); ASSERT_TRUE(gpu_out[i].found) << "point " << i; EXPECT_NEAR(gpu_out[i].distance, cpu_out[i].distance, tol) << "point " << i; const double d2 = vde::compute::lbvh_point_triangle_dist_sq( tris.data(), gpu_out[i].prim, points.data() + i * 3); EXPECT_NEAR(gpu_out[i].distance, std::sqrt(d2), tol) << "point " << i; } } TEST(GpuBvhTest, ClosestPointGpuLargeWorldOffset) { if (!GpuBvh::gpu_supported()) { GTEST_SKIP() << "no OpenCL GPU available"; } // 与射线大偏移用例同动机:CAD 模型常远离世界原点,float32 绝对坐标精度 // 退化为 2^-23*|p|。实现以模型包围盒中心为局部原点并做双精度复核,因此 // 4096 偏移下复核后的距离仍须与 CPU 权威一致。 std::vector tris = make_heightfield(25); const double offset = 4096.0; for (std::size_t i = 0; i < tris.size(); ++i) tris[i] += offset; std::vector points = closest_query_points(tris, 256, 424242); GpuBvh cpu; GpuBvhBuildOptions cpu_options; cpu_options.prefer_gpu = false; ASSERT_TRUE(cpu.build(tris.data(), tris.size() / 9, cpu_options)); GpuBvh gpu; GpuBvhBuildOptions gpu_options; gpu_options.prefer_gpu = true; ASSERT_TRUE(gpu.build(tris.data(), tris.size() / 9, gpu_options)); ASSERT_TRUE(gpu.gpu_ready()) << gpu.build_info().backend; constexpr std::size_t kPoints = 256; std::vector cpu_out(kPoints); std::vector gpu_out(kPoints); ASSERT_TRUE(cpu.closest_point_cpu(points.data(), kPoints, cpu_out.data())); ASSERT_TRUE(gpu.closest_point(points.data(), kPoints, gpu_out.data(), true)); for (std::size_t i = 0; i < kPoints; ++i) { ASSERT_TRUE(gpu_out[i].found) << "point " << i; EXPECT_NEAR(gpu_out[i].distance, cpu_out[i].distance, 1e-9) << "point " << i; const double d2 = vde::compute::lbvh_point_triangle_dist_sq( tris.data(), gpu_out[i].prim, points.data() + i * 3); EXPECT_NEAR(gpu_out[i].distance, std::sqrt(d2), 1e-9) << "point " << i; } }