/** * @file test_gpu_consistency.cpp * @brief Phase4-GPU:GPU 后端(CUDA/HIP)与 CPU LBVH 的 ray/knn/range 一致性 * * 诚实测试约定: * - 无 GPU 工具链(默认 CPU 构建)或运行期无设备时,需要 GPU 执行的用例统一 * GTEST_SKIP() 并打印原因;绝不用 CPU 结果冒充 GPU 通过。 * - 宿主有 CUDA/HIP 工具链且有设备时(tools/gpu/verify_gpu_build.ps1 自动 * 构建运行):验证节点布局一致 + 10k 三角 ray/knn/range mismatch=0 + * 空/退化输入不崩。 */ #include #include "vde/compute/compute_backend.h" #include "vde/compute/lbvh.h" #include #include #include #include #include using namespace vde::compute; namespace { struct Lcg { std::uint64_t s = 0x9e3779b97f4a7c15ull; double next(double lo, double hi) { s = s * 6364136223846793005ull + 1442695040888963407ull; const double u = static_cast(s >> 11) / static_cast(1ull << 53); return lo + (hi - lo) * u; } }; std::vector random_tris(int n, std::uint64_t seed) { Lcg rng; rng.s = seed; std::vector tv(static_cast(n) * 9); for (int i = 0; i < n; ++i) for (int k = 0; k < 3; ++k) { const size_t o = static_cast(i) * 9 + static_cast(k) * 3; tv[o + 0] = rng.next(-10.0, 10.0); tv[o + 1] = rng.next(-10.0, 10.0); tv[o + 2] = rng.next(-10.0, 10.0); } return tv; } bool gpu_runtime_available() { const GpuCapability cap = probe_gpu_capability(); return cap.toolkit_compiled && cap.device_present; } const char* skip_reason() { const GpuCapability cap = probe_gpu_capability(); if (!cap.toolkit_compiled) return "默认 CPU 构建(未编译 CUDA/HIP):GPU 执行用例跳过;" "宿主有 nvcc/hipcc 时由 tools/gpu/verify_gpu_build.ps1 构建验证"; return "已编译 GPU 后端但运行期无可用设备:设备用例跳过"; } struct RayRef { bool hit = false; double t = 0.0; int prim = -1; }; RayRef brute_ray(const std::vector& tv, const double* o, const double* d) { RayRef r; const int n = static_cast(tv.size() / 9); double best = 1e300; for (int i = 0; i < n; ++i) { double t = 0.0; if (lbvh_tri_ray_t(tv.data(), i, o, d, &t) && t < best) { best = t; r.hit = true; r.prim = i; r.t = t; } } return r; } std::vector brute_knn(const std::vector& tv, const double* p, int k) { const int n = static_cast(tv.size() / 9); std::vector all; all.reserve(static_cast(n)); for (int i = 0; i < n; ++i) all.push_back({lbvh_point_triangle_dist_sq(tv.data(), i, p), i}); std::sort(all.begin(), all.end(), [](const LbvhKnnEntry& a, const LbvhKnnEntry& b) { if (a.dist_sq != b.dist_sq) return a.dist_sq < b.dist_sq; return a.prim < b.prim; }); if (static_cast(all.size()) > k) all.resize(static_cast(k)); return all; } std::vector brute_range(const std::vector& tv, const double* mn, const double* mx) { const int n = static_cast(tv.size() / 9); std::vector out; for (int i = 0; i < n; ++i) if (lbvh_tri_aabb_overlap(tv.data(), i, mn, mx)) out.push_back(i); std::sort(out.begin(), out.end()); return out; } } // namespace TEST(GpuConsistencyTest, ProbeAndDispatch_HonestReport) { const GpuCapability cap = probe_gpu_capability(); if (cap.toolkit_compiled) { EXPECT_EQ(cap.device_present, cap.device_count > 0); } else { EXPECT_FALSE(cap.device_present); EXPECT_EQ(cap.device_count, 0); const auto tv = random_tris(16, 1); const auto cuda = build_bvh_cuda(tv.data(), 16); const auto hip = build_bvh_hip(tv.data(), 16); EXPECT_EQ(cuda.status, BvhBuildStatus::Unsupported); EXPECT_EQ(hip.status, BvhBuildStatus::Unsupported); EXPECT_FALSE(cuda.gpu); EXPECT_FALSE(hip.gpu); EXPECT_EQ(cuda.device_count, 0); EXPECT_EQ(hip.device_count, 0); EXPECT_STREQ(bvh_build_status_name(cuda.status), "unsupported"); } const auto tv = random_tris(64, 2); const auto unified = build_bvh(tv.data(), 64); ASSERT_TRUE(unified.ok()) << unified.to_string(); if (!gpu_runtime_available()) { EXPECT_FALSE(unified.gpu); EXPECT_EQ(unified.backend, "cpu-lbvh"); } } TEST(GpuConsistencyTest, GpuVsCpu_RayKnnRange_10k) { if (!gpu_runtime_available()) GTEST_SKIP() << skip_reason(); constexpr int kN = 10000; const auto tv = random_tris(kN, 42); const auto cpu = build_bvh_cpu_lbvh(tv.data(), kN); ASSERT_TRUE(cpu.ok()); const auto gpu = build_bvh(tv.data(), kN); ASSERT_TRUE(gpu.ok()) << gpu.to_string(); ASSERT_TRUE(gpu.gpu) << "GPU 可用但统一入口回退 CPU:" << gpu.to_string(); ASSERT_EQ(gpu.nodes.size(), cpu.nodes.size()); EXPECT_EQ(gpu.prim_order, cpu.prim_order); for (size_t i = 0; i < cpu.nodes.size() && i < gpu.nodes.size(); ++i) { const auto& a = cpu.nodes[i]; const auto& b = gpu.nodes[i]; for (int k = 0; k < 3; ++k) { EXPECT_NEAR(a.bmin[k], b.bmin[k], 1e-9) << "node " << i; EXPECT_NEAR(a.bmax[k], b.bmax[k], 1e-9) << "node " << i; } EXPECT_EQ(a.left, b.left); EXPECT_EQ(a.right, b.right); EXPECT_EQ(a.first_prim, b.first_prim); EXPECT_EQ(a.prim_count, b.prim_count); } Lcg rng; rng.s = 12345; int ray_mismatch = 0; int knn_mismatch = 0; int range_mismatch = 0; for (int q = 0; q < 64; ++q) { const double o[3] = {rng.next(-12, 12), rng.next(-12, 12), rng.next(-12, 12)}; double d[3] = {rng.next(-1, 1), rng.next(-1, 1), rng.next(-1, 1)}; const double len = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); if (len > 1e-12) { d[0] /= len; d[1] /= len; d[2] /= len; } LbvhRayHit hit; const bool got = lbvh_ray_nearest(gpu, tv.data(), o, d, &hit); const RayRef want = brute_ray(tv, o, d); if (got != want.hit) { ++ray_mismatch; continue; } if (got && (hit.prim != want.prim || std::abs(hit.t - want.t) > 1e-9)) ++ray_mismatch; } for (int q = 0; q < 64; ++q) { const double p[3] = {rng.next(-12, 12), rng.next(-12, 12), rng.next(-12, 12)}; const auto got = lbvh_knn(gpu, tv.data(), p, 8); const auto want = brute_knn(tv, p, 8); if (got.size() != want.size()) { ++knn_mismatch; continue; } for (size_t i = 0; i < got.size(); ++i) if (got[i].prim != want[i].prim || std::abs(got[i].dist_sq - want[i].dist_sq) > 1e-9) ++knn_mismatch; } for (int q = 0; q < 32; ++q) { const double cx = rng.next(-10, 10), cy = rng.next(-10, 10), cz = rng.next(-10, 10), e = rng.next(0.5, 6.0); const double mn[3] = {cx - e, cy - e, cz - e}; const double mx[3] = {cx + e, cy + e, cz + e}; if (lbvh_range_aabb(gpu, tv.data(), mn, mx) != brute_range(tv, mn, mx)) ++range_mismatch; } std::cout << "[GPU] backend=" << gpu.backend << " ray_mismatch=" << ray_mismatch << " knn_mismatch=" << knn_mismatch << " range_mismatch=" << range_mismatch << std::endl; EXPECT_EQ(ray_mismatch, 0); EXPECT_EQ(knn_mismatch, 0); EXPECT_EQ(range_mismatch, 0); } TEST(GpuConsistencyTest, Gpu_EmptyDegenerate_NoCrash) { if (!gpu_runtime_available()) GTEST_SKIP() << skip_reason(); const auto empty = build_bvh(nullptr, 0); EXPECT_EQ(empty.status, BvhBuildStatus::InvalidInput); const auto ec = build_bvh_cuda(nullptr, 0); const auto eh = build_bvh_hip(nullptr, 0); EXPECT_EQ(ec.status, BvhBuildStatus::InvalidInput); EXPECT_EQ(eh.status, BvhBuildStatus::InvalidInput); std::vector degenerate(300 * 9, 0.0); const auto degen = build_bvh(degenerate.data(), 300); ASSERT_TRUE(degen.ok()) << degen.to_string(); LbvhRayHit hit; EXPECT_NO_THROW( lbvh_ray_nearest(degen, degenerate.data(), nullptr, nullptr, &hit)); EXPECT_NO_THROW(lbvh_knn(degen, degenerate.data(), nullptr, 4)); } TEST(GpuConsistencyTest, GpuBackendFactory_Honest) { auto gpu = create_backend(BackendType::Cuda); ASSERT_NE(gpu, nullptr); const auto info = gpu->info(); if (!gpu_runtime_available()) { EXPECT_FALSE(info.gpu); const double xs[4] = {0, 0, 3, 0}; const double ys[4] = {0, 0, 4, 0}; const double zs[4] = {0, 0, 0, 0}; double out[4] = {0, 0, 0, 0}; gpu->sdf_sphere_batch(xs, ys, zs, out, 4, 0, 0, 0, 1.0); EXPECT_NEAR(out[0], -1.0, 1e-12); EXPECT_NEAR(out[2], 4.0, 1e-12); } else { EXPECT_TRUE(info.gpu); } auto hip = create_backend(BackendType::Hip); ASSERT_NE(hip, nullptr); EXPECT_FALSE(hip->info().gpu); }