Files
ViewDesignEngine/tests/compute/test_compute_backend.cpp
T
hm 1334f0d86b feat(compute): E3c 计算后端抽象(ComputeBackend + CPU SIMD + CUDA 挂载点)
- 新增 vde_compute 模块:批量 SDF(球/盒)+ 批量 AABB 相交
- CpuSimdComputeBackend 复用 Vec4d/BatchAABB4,CudaComputeBackend 为挂载点
- 修复 Vec4d SSE3 缺少 pmmintrin.h 导致 _mm_hadd_pd 未声明
- 修复 sdf_sphere_batch 球心未逐分量广播(零球心掩盖的非零偏移 bug)
- test_voxelize 球心改为非零值防回归
2026-08-09 16:43:56 +08:00

202 lines
7.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/compute/compute_backend.h"
#include "vde/core/simd_vector.h"
#include "vde/mesh/voxelize.h"
#include "vde/mesh/marching_cubes.h"
#include <cmath>
#include <map>
#include <random>
#include <utility>
#include <vector>
using namespace vde::compute;
using namespace vde::core;
namespace {
// 标量球体 SDF(与 marching_cubes.h::sdf_sphere 一致)
double ref_sphere(double x, double y, double z, double cx, double cy, double cz, double r) {
return std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy) + (z - cz) * (z - cz)) - r;
}
// 标量盒 SDF(与 marching_cubes.h::sdf_box 一致)
double ref_box(double x, double y, double z, double hx, double hy, double hz) {
double dx = std::abs(x) - hx, dy = std::abs(y) - hy, dz = std::abs(z) - hz;
return std::sqrt(std::max(dx, 0.0) * std::max(dx, 0.0) +
std::max(dy, 0.0) * std::max(dy, 0.0) +
std::max(dz, 0.0) * std::max(dz, 0.0)) +
std::min(std::max({dx, dy, dz}), 0.0);
}
// 水密性检查(O(F))
bool is_watertight(const vde::mesh::MCMesh& mc) {
std::map<std::pair<int, int>, int> edge_cnt;
for (const auto& t : mc.triangles) {
for (int k = 0; k < 3; ++k) {
int a = t[k], b = t[(k + 1) % 3];
if (a > b) std::swap(a, b);
++edge_cnt[{a, b}];
}
}
if (edge_cnt.empty()) return false;
for (const auto& [key, cnt] : edge_cnt)
if (cnt != 2) return false;
return true;
}
} // namespace
// ═══════════════════════════════════════════════════════════
// CPU SIMD 后端
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, CpuSimd_Info) {
CpuSimdComputeBackend b;
const BackendInfo info = b.info();
EXPECT_EQ(info.type, BackendType::CpuSimd);
EXPECT_FALSE(info.gpu);
EXPECT_EQ(info.lanes, simd_width_d);
EXPECT_EQ(info.name, std::string("cpu-simd(") + [] {
switch (current_simd_isa()) {
case SimdIsa::AVX: return "avx";
case SimdIsa::SSE2: return "sse";
case SimdIsa::NEON: return "neon";
default: return "scalar";
}
}() + ")");
}
TEST(ComputeBackendTest, CpuSimd_SphereBatch_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(42);
std::uniform_real_distribution<double> uni(-2.0, 2.0);
const int n = 1003; // 含非 4 倍数尾部
std::vector<double> xs(n), ys(n), zs(n), simd(n), ref(n);
for (int i = 0; i < n; ++i) {
xs[i] = uni(rng);
ys[i] = uni(rng);
zs[i] = uni(rng);
}
b.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), simd.data(), n, 0.3, -0.2, 0.1, 1.25);
for (int i = 0; i < n; ++i) {
ref[i] = ref_sphere(xs[i], ys[i], zs[i], 0.3, -0.2, 0.1, 1.25);
EXPECT_NEAR(simd[i], ref[i], 1e-12) << "i=" << i;
}
}
TEST(ComputeBackendTest, CpuSimd_BoxBatch_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(7);
std::uniform_real_distribution<double> uni(-2.0, 2.0);
const int n = 502; // 非 4 倍数
std::vector<double> xs(n), ys(n), zs(n), simd(n), ref(n);
for (int i = 0; i < n; ++i) {
xs[i] = uni(rng);
ys[i] = uni(rng);
zs[i] = uni(rng);
}
b.sdf_box_batch(xs.data(), ys.data(), zs.data(), simd.data(), n, 0.8, 0.5, 1.1);
for (int i = 0; i < n; ++i) {
ref[i] = ref_box(xs[i], ys[i], zs[i], 0.8, 0.5, 1.1);
EXPECT_NEAR(simd[i], ref[i], 1e-12) << "i=" << i;
}
}
TEST(ComputeBackendTest, CpuSimd_AabbIntersect_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(99);
std::uniform_real_distribution<double> uni(-3.0, 3.0);
std::uniform_real_distribution<double> sz(0.1, 1.0);
const size_t n = 257; // 非 4 倍数
std::vector<double> mins(n * 3), maxs(n * 3);
for (size_t i = 0; i < n; ++i) {
const double cx = uni(rng), cy = uni(rng), cz = uni(rng);
const double hx = sz(rng), hy = sz(rng), hz = sz(rng);
mins[i * 3 + 0] = cx - hx; maxs[i * 3 + 0] = cx + hx;
mins[i * 3 + 1] = cy - hy; maxs[i * 3 + 1] = cy + hy;
mins[i * 3 + 2] = cz - hz; maxs[i * 3 + 2] = cz + hz;
}
const double rx0 = -0.5, ry0 = -0.5, rz0 = -0.5;
const double rx1 = 0.5, ry1 = 0.5, rz1 = 0.5;
std::vector<int> simd_hits(n), ref_hits(n);
const size_t ns = b.aabb_intersect(mins.data(), maxs.data(), n,
rx0, ry0, rz0, rx1, ry1, rz1, simd_hits.data());
size_t nr = 0;
for (size_t i = 0; i < n; ++i) {
if (mins[i * 3 + 0] <= rx1 && maxs[i * 3 + 0] >= rx0 &&
mins[i * 3 + 1] <= ry1 && maxs[i * 3 + 1] >= ry0 &&
mins[i * 3 + 2] <= rz1 && maxs[i * 3 + 2] >= rz0)
ref_hits[nr++] = static_cast<int>(i);
}
ASSERT_EQ(ns, nr);
for (size_t i = 0; i < ns; ++i)
EXPECT_EQ(simd_hits[i], ref_hits[i]) << "hit #" << i;
}
// ═══════════════════════════════════════════════════════════
// 工厂与默认后端
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, Factory_DefaultBackend_Executes) {
ComputeBackend& b = default_backend();
const BackendInfo info = b.info();
EXPECT_GE(info.lanes, 1);
const double xs[8] = {0, 0.5, -0.5, 1.0, 2.0, -2.0, 0.0, 0.0};
const double ys[8] = {0, 0, 0, 0, 0, 0, 0.5, 0};
const double zs[8] = {0, 0, 0, 0, 0, 0, 0, 0.5};
double out[8];
b.sdf_sphere_batch(xs, ys, zs, out, 8, 0, 0, 0, 1.0);
for (int i = 0; i < 8; ++i)
EXPECT_NEAR(out[i], ref_sphere(xs[i], ys[i], zs[i], 0, 0, 0, 1.0), 1e-12);
}
TEST(ComputeBackendTest, CudaBackend_AvailableOrFallsBack) {
auto b = create_backend(BackendType::Cuda);
ASSERT_NE(b, nullptr);
const BackendInfo info = b->info();
EXPECT_EQ(info.type, BackendType::Cuda);
// 无 CUDA 构建:回退路径结果与 CPU 一致
const double xs[4] = {0, 1, 2, -1};
const double ys[4] = {0, 0, 0, 0};
const double zs[4] = {0, 0, 0, 0};
double out[4];
b->sdf_sphere_batch(xs, ys, zs, out, 4, 0, 0, 0, 1.5);
for (int i = 0; i < 4; ++i)
EXPECT_NEAR(out[i], ref_sphere(xs[i], ys[i], zs[i], 0, 0, 0, 1.5), 1e-12);
// 回退路径的名称标注(gpu=true 仅在真实 CUDA 时)
if (!info.gpu) {
EXPECT_NE(info.name.find("fallback"), std::string::npos);
} else {
EXPECT_NE(info.name.find("cuda"), std::string::npos);
}
}
// ═══════════════════════════════════════════════════════════
// 集成:后端 → VoxelGrid → Marching Cubes
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, BackendSdf_FeedsVoxelSampling_Watertight) {
ComputeBackend& b = default_backend();
const AABB3D bounds{{-1.5, -1.5, -1.5}, {1.5, 1.5, 1.5}};
vde::mesh::VoxelizeParams p;
p.resolution = 32;
// 将 ComputeBackend 的批核包装为 BatchSdfFn 喂给 sample_sdf_batch
auto grid = vde::mesh::sample_sdf_batch(
[&b](const double* xs, const double* ys, const double* zs,
double* out, int n) {
b.sdf_sphere_batch(xs, ys, zs, out, n, 0, 0, 0, 1.0);
},
bounds, p);
auto mc = vde::mesh::marching_cubes(grid, 0.0);
ASSERT_FALSE(mc.triangles.empty());
EXPECT_TRUE(is_watertight(mc)) << "后端驱动的体素采样重建应为水密网格";
}