Files
ViewDesignEngine/tests/compute/test_cov_compute_simd.cpp
hm 35baae1555 test(coverage): add core-plus-v2 profile with plugin/capi/spatial/sdf/compute coverage + 20 boundary cases
Wave 8 W48 (task-123).

Profile: tools/coverage/target_profiles.json gains core-plus-v2 = core-plus 59 targets
+ 37 plugin/capi/spatial/sdf/compute targets (96 total; >=85 required). Excludes
test_curved_boolean_baseline (gcov wall-clock sensitive, same as core-plus) and
test_plugin_ecosystem (asserts all official plugin kinds, needs full plugin build).
line_pct_gate = 52.0 derived from the measured 54.01% (floor - 2pp).

Tests: 5 new files / 20 cases covering plugin lifecycle + capability registry
semantics, C API export boundaries, BVH edge cases, SDF field nodes and compute
SIMD lane boundaries. All 20 pass; module-subset ctest (Plugin|Capi|Spatial|Sdf|
Compute|Brep|Curves|Foundation) = 721/721 passed.

Measurement (build-cov8, RelWithDebInfo + VDE_ENABLE_COVERAGE, MinGW gcc 16.1.0):
before core-plus 50.91% (63374/124474), after core-plus-v2 54.01% (70324/130198),
func 62.88% (3568/5674), branch 33.55%; 2418/2418 cases pass. --update-baseline
wrote tools/coverage/baseline.json core-plus-v2 entry and
--min-line-pct baseline --strict-baseline PASSed (status PASS, line gate 54.01%).

Plugin fixture fix: test_plugin_extensions build deps now include vde_service_runner /
vde_service_stl_convert / vde_service_gcs_solve (3 external-service cases failed when
only the target was built; 9/9 pass after the fix).

CI: nightly coverage-nightly adds a core-plus-v2 report-only archive step and an
opt-in hard gate gated by vars.VDE_ENABLE_CORE_PLUS_V2=1; pr-quality dry-run plan
adds --profile core-plus-v2. Evidence: docs/planning/wave8/cov-notes.md and
build/kcp8_evidence/cov/ (before/after/after-gate reports, ctest logs).

Validation: validate_workflows.py --strict PASS 2/2, vde_release.py --check PASS
(14 checks, 0 fail/0 warn), no product (src/) changes.
2026-11-20 14:55:34 +08:00

243 lines
9.7 KiB
C++

#include <gtest/gtest.h>
#include "vde/compute/compute_backend.h"
#include "vde/core/simd_vector.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <vector>
// ═══════════════════════════════════════════════════════════
// Wave 8 补测: compute SIMD 批处理边界
//
// 覆盖此前未执行的路径:
// - n == 0 空批次的 no-op 语义 (SIMD 主循环 + 尾部分支均不写 out)
// - n == 1 与 n == simd_width_d + 1 的单元素/尾部残留通道
// - aabb_intersect 的相切 (闭区间) / 退化 / 全 miss 边界与索引输出
// - frustum_cull_batch 的 p-vertex 边界、全剔除与后端起身份
// ═══════════════════════════════════════════════════════════
using namespace vde::compute;
namespace {
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;
}
double ref_box(double x, double y, double z, double hx, double hy, double hz) {
const double dx = std::abs(x) - hx;
const double dy = std::abs(y) - hy;
const double 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);
}
// 与 CPU 后端 aabb_intersect 相同的闭区间判定
bool ref_aabb_hit(const double* mins, const double* maxs, size_t i,
double rx0, double ry0, double rz0,
double rx1, double ry1, double rz1) {
return 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;
}
// p-vertex 判定: 取 AABB 在平面法向上的最远点, 对全部 6 个内法向平面可见则保留
bool ref_frustum_hit(const double* mins, const double* maxs, size_t i,
const double* planes) {
for (int p = 0; p < 6; ++p) {
const double a = planes[p * 4 + 0];
const double b = planes[p * 4 + 1];
const double c = planes[p * 4 + 2];
const double d = planes[p * 4 + 3];
const double px = a >= 0 ? maxs[i * 3 + 0] : mins[i * 3 + 0];
const double py = b >= 0 ? maxs[i * 3 + 1] : mins[i * 3 + 1];
const double pz = c >= 0 ? maxs[i * 3 + 2] : mins[i * 3 + 2];
if (a * px + b * py + c * pz + d < 0) return false;
}
return true;
}
} // namespace
// ── SDF 球批: 空批 / 单元素 / 尾部残留 ───────────────────
TEST(CovComputeSimd, SphereBatchEmptySingleAndRemainder) {
CpuSimdComputeBackend backend;
const int lanes = static_cast<int>(vde::core::simd_width_d);
ASSERT_GE(lanes, 1);
// n == 0: 不得写 out, 不得崩溃
double sentinel = 12345.0;
double one_x = 0.0, one_y = 0.0, one_z = 0.0;
backend.sdf_sphere_batch(&one_x, &one_y, &one_z, &sentinel, 0, 1.0, 2.0, 3.0, 0.5);
EXPECT_DOUBLE_EQ(sentinel, 12345.0);
// n == 1: 单元素必须与标量一致
const double x = 1.5, y = 2.0, z = 3.0;
double out1 = 0.0;
backend.sdf_sphere_batch(&x, &y, &z, &out1, 1, 1.0, 2.0, 3.0, 0.5);
EXPECT_NEAR(out1, ref_sphere(x, y, z, 1.0, 2.0, 3.0, 0.5), 1e-12);
// n == lanes*2 + 1: 尾部残留通道 (含 1 个元素) 必须全部正确
const int n = lanes * 2 + 1;
std::vector<double> xs(n), ys(n), zs(n), out(n, 999.0);
for (int i = 0; i < n; ++i) {
xs[i] = 0.13 * i - 1.0;
ys[i] = -0.07 * i + 0.5;
zs[i] = 0.05 * i;
}
backend.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), out.data(), n,
-0.25, 0.75, 0.5, 1.25);
for (int i = 0; i < n; ++i) {
EXPECT_NEAR(out[i], ref_sphere(xs[i], ys[i], zs[i], -0.25, 0.75, 0.5, 1.25), 1e-12)
<< "i=" << i;
}
}
// ── SDF 盒批: n=1 与多通道标量对照 ───────────────────────
TEST(CovComputeSimd, BoxBatchMatchesScalarAtLaneBoundaries) {
CpuSimdComputeBackend backend;
const int lanes = static_cast<int>(vde::core::simd_width_d);
const int n = lanes + 3;
std::vector<double> xs(n), ys(n), zs(n), out(n, 0.0);
// 内部点 / 面上点 / 外部点交替, 覆盖 min(max(...),0) 与 sqrt 分支
for (int i = 0; i < n; ++i) {
const double t = static_cast<double>(i) / static_cast<double>(n - 1);
xs[i] = -1.5 + 3.0 * t;
ys[i] = 0.9 * (1.0 - t) - 0.2;
zs[i] = (i % 3 == 0) ? 1.0 : -1.0 + 0.5 * t;
}
backend.sdf_box_batch(xs.data(), ys.data(), zs.data(), out.data(), n, 1.0, 1.0, 1.0);
for (int i = 0; i < n; ++i) {
EXPECT_NEAR(out[i], ref_box(xs[i], ys[i], zs[i], 1.0, 1.0, 1.0), 1e-12)
<< "i=" << i;
}
// 中心点: SDF == -min(h)
const double cx = 0.0, cy = 0.0, cz = 0.0;
double center_out = 0.0;
backend.sdf_box_batch(&cx, &cy, &cz, &center_out, 1, 2.0, 3.0, 4.0);
EXPECT_NEAR(center_out, -2.0, 1e-12);
}
// ── AABB 相交: 相切 / 退化 / 全 miss ─────────────────────
TEST(CovComputeSimd, AabbIntersectBoundaryCases) {
CpuSimdComputeBackend backend;
// 4 个盒相对射线框 [-1,1]^3: 内含 / 完全重合 / 仅 x 面相切 / 远离
const double mins[12] = {-0.5, -0.5, -0.5,
-1.0, -1.0, -1.0,
1.0, 0.5, 0.5,
10.0, 10.0, 10.0};
const double maxs[12] = {0.5, 0.5, 0.5,
1.0, 1.0, 1.0,
2.0, 1.5, 1.5,
11.0, 11.0, 11.0};
std::vector<int> hits(4, -1);
const size_t count = backend.aabb_intersect(mins, maxs, 4,
-1.0, -1.0, -1.0,
1.0, 1.0, 1.0,
hits.data());
EXPECT_EQ(count, 3u);
size_t expected_count = 0;
for (size_t i = 0; i < 4; ++i) {
if (ref_aabb_hit(mins, maxs, i, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0)) {
++expected_count;
}
}
EXPECT_EQ(count, expected_count);
for (size_t k = 0; k < count; ++k) {
EXPECT_GE(hits[k], 0);
EXPECT_LT(hits[k], 4);
const size_t idx = static_cast<size_t>(hits[k]);
EXPECT_TRUE(ref_aabb_hit(mins, maxs, idx, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0));
}
// 全 miss: count == 0
const double far_mins[3] = {5.0, 5.0, 5.0};
const double far_maxs[3] = {6.0, 6.0, 6.0};
std::vector<int> no_hits(1, -7);
EXPECT_EQ(backend.aabb_intersect(far_mins, far_maxs, 1, -1.0, -1.0, -1.0,
1.0, 1.0, 1.0, no_hits.data()),
0u);
// 退化盒 (min == max 落在查询框角上) 按闭区间算命中
const double pt_mins[3] = {1.0, 1.0, 1.0};
const double pt_maxs[3] = {1.0, 1.0, 1.0};
int pt_hit = -1;
EXPECT_EQ(backend.aabb_intersect(pt_mins, pt_maxs, 1, -1.0, -1.0, -1.0,
1.0, 1.0, 1.0, &pt_hit),
1u);
EXPECT_EQ(pt_hit, 0);
// n == 0: 无输出
int untouched = 11;
EXPECT_EQ(backend.aabb_intersect(mins, maxs, 0, -1.0, -1.0, -1.0,
1.0, 1.0, 1.0, &untouched),
0u);
EXPECT_EQ(untouched, 11);
}
// ── 视锥剔除边界 + 后端工厂 ─────────────────────────────
TEST(CovComputeSimd, FrustumCullBoundaryAndFactories) {
CpuSimdComputeBackend backend;
// 盒 [-2,2]^3 对应的 6 个内法向平面
const double planes[24] = {
1, 0, 0, 2, // x >= -2
-1, 0, 0, 2, // x <= 2
0, 1, 0, 2, // y >= -2
0, -1, 0, 2, // y <= 2
0, 0, 1, 2, // z >= -2
0, 0, -1, 2, // z <= 2
};
// 5 个盒: 完全在内 / 部分超出(与边界重叠) / 完全在外 / 与单平面相切 / 退化点
const double mins[15] = {-1.0, -1.0, -1.0,
-3.0, -1.0, -1.0,
4.0, 4.0, 4.0,
2.0, 0.0, 0.0,
-2.0, -2.0, -2.0};
const double maxs[15] = {1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
5.0, 5.0, 5.0,
4.0, 0.5, 0.5,
-2.0, -2.0, -2.0};
std::vector<int> out(5, -1);
const size_t count = backend.frustum_cull_batch(mins, maxs, 5, planes, out.data());
size_t expected = 0;
for (size_t i = 0; i < 5; ++i) {
if (ref_frustum_hit(mins, maxs, i, planes)) ++expected;
}
EXPECT_EQ(count, expected);
for (size_t k = 0; k < count; ++k) {
EXPECT_GE(out[k], 0);
EXPECT_LT(out[k], 5);
EXPECT_TRUE(ref_frustum_hit(mins, maxs, static_cast<size_t>(out[k]), planes));
}
// n == 0: 不写输出
int untouched = 13;
EXPECT_EQ(backend.frustum_cull_batch(mins, maxs, 0, planes, &untouched), 0u);
EXPECT_EQ(untouched, 13);
// 后端工厂: CpuSimd 必须真实可用, default_backend 引用稳定且 lanes > 0
std::unique_ptr<ComputeBackend> cpu = create_backend(BackendType::CpuSimd);
ASSERT_NE(cpu, nullptr);
EXPECT_EQ(cpu->info().type, BackendType::CpuSimd);
EXPECT_GT(cpu->info().lanes, 0);
ComputeBackend& d1 = default_backend();
ComputeBackend& d2 = default_backend();
EXPECT_EQ(&d1, &d2);
EXPECT_GT(d1.info().lanes, 0);
EXPECT_FALSE(d1.info().name.empty());
}