新增 vde_render/instance_render.{h,cpp}:build_instance_batches(按 master 几何+LOD 归并批次)/ summarize_batches(draw call 归并统计)/ assign_lod_under_budget(预算约束下近处优先升档)/ required_mesh_keys + plan_streaming(常驻集进出流式加载)。新增 test_instance_render 15/15,含百万实例装配基准。
338 lines
14 KiB
C++
338 lines
14 KiB
C++
#include <gtest/gtest.h>
|
||
#include "vde_render/instance_render.h"
|
||
#include "vde/core/transform.h"
|
||
#include <algorithm>
|
||
#include <cstdint>
|
||
#include <vector>
|
||
|
||
using namespace vde::render;
|
||
using vde::core::Transform3D;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 辅助
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
InstanceDraw make_instance(uint32_t master, int lod, double tris,
|
||
double distance = 0.0, double x = 0.0) {
|
||
InstanceDraw inst;
|
||
inst.master_id = master;
|
||
inst.lod_level = lod;
|
||
inst.triangle_count = tris;
|
||
inst.distance = distance;
|
||
inst.transform = Transform3D::Identity();
|
||
inst.transform.translation() = vde::core::Vector3D(x, 0.0, 0.0);
|
||
return inst;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 实例化批处理
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(InstanceRenderTest, BuildInstanceBatches_GroupsByMasterAndLod) {
|
||
std::vector<InstanceDraw> instances = {
|
||
make_instance(1, 0, 100.0),
|
||
make_instance(1, 0, 100.0),
|
||
make_instance(1, 1, 20.0),
|
||
make_instance(2, 0, 50.0),
|
||
make_instance(2, 0, 50.0),
|
||
make_instance(2, 0, 50.0),
|
||
};
|
||
auto batches = build_instance_batches(instances);
|
||
ASSERT_EQ(batches.size(), 3u);
|
||
|
||
// (master, lod) 升序: (1,0) (1,1) (2,0)
|
||
EXPECT_EQ(batches[0].master_id, 1u);
|
||
EXPECT_EQ(batches[0].lod_level, 0);
|
||
EXPECT_EQ(batches[0].instance_indices, (std::vector<int>{0, 1}));
|
||
EXPECT_DOUBLE_EQ(batches[0].total_triangles(), 200.0);
|
||
|
||
EXPECT_EQ(batches[1].master_id, 1u);
|
||
EXPECT_EQ(batches[1].lod_level, 1);
|
||
EXPECT_EQ(batches[1].instance_indices, (std::vector<int>{2}));
|
||
|
||
EXPECT_EQ(batches[2].master_id, 2u);
|
||
EXPECT_EQ(batches[2].lod_level, 0);
|
||
EXPECT_EQ(batches[2].instance_indices, (std::vector<int>{3, 4, 5}));
|
||
EXPECT_DOUBLE_EQ(batches[2].total_triangles(), 150.0);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, BuildInstanceBatches_EmptyInput) {
|
||
auto batches = build_instance_batches({});
|
||
EXPECT_TRUE(batches.empty());
|
||
|
||
auto stats = summarize_batches(batches);
|
||
EXPECT_EQ(stats.instance_count, 0u);
|
||
EXPECT_EQ(stats.draw_calls, 0u);
|
||
EXPECT_EQ(stats.unique_masters, 0u);
|
||
EXPECT_DOUBLE_EQ(stats.instances_per_draw(), 0.0);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, BuildInstanceBatches_DeterministicOrderAndAscendingIndices) {
|
||
// 乱序输入:批次顺序应稳定为 (master, lod) 升序,批内下标升序
|
||
std::vector<InstanceDraw> instances;
|
||
for (int i = 0; i < 30; ++i) instances.push_back(make_instance(2, 1, 10.0));
|
||
for (int i = 0; i < 30; ++i) instances.push_back(make_instance(0, 0, 10.0));
|
||
for (int i = 0; i < 30; ++i) instances.push_back(make_instance(1, 0, 10.0));
|
||
|
||
auto first = build_instance_batches(instances);
|
||
auto second = build_instance_batches(instances);
|
||
|
||
ASSERT_EQ(first.size(), 3u);
|
||
EXPECT_EQ(first[0].master_id, 0u);
|
||
EXPECT_EQ(first[1].master_id, 1u);
|
||
EXPECT_EQ(first[2].master_id, 2u);
|
||
|
||
for (const auto& b : first)
|
||
EXPECT_TRUE(std::is_sorted(b.instance_indices.begin(),
|
||
b.instance_indices.end()));
|
||
|
||
// 两次构建结果一致(确定性)
|
||
ASSERT_EQ(second.size(), first.size());
|
||
for (size_t i = 0; i < first.size(); ++i) {
|
||
EXPECT_EQ(second[i].master_id, first[i].master_id);
|
||
EXPECT_EQ(second[i].lod_level, first[i].lod_level);
|
||
EXPECT_EQ(second[i].instance_indices, first[i].instance_indices);
|
||
}
|
||
}
|
||
|
||
TEST(InstanceRenderTest, SummarizeBatches_DrawCallReduction) {
|
||
// 100 个实例、4 个 master、2 个 LOD → 绘制调用 ≤ 8,远小于 100
|
||
std::vector<InstanceDraw> instances;
|
||
instances.reserve(100);
|
||
for (int i = 0; i < 100; ++i)
|
||
instances.push_back(make_instance(static_cast<uint32_t>(i % 4),
|
||
(i / 4) % 2, 100.0));
|
||
|
||
auto batches = build_instance_batches(instances);
|
||
auto stats = summarize_batches(batches);
|
||
|
||
EXPECT_EQ(stats.instance_count, 100u);
|
||
EXPECT_EQ(stats.unique_masters, 4u);
|
||
EXPECT_EQ(stats.draw_calls, 8u);
|
||
EXPECT_DOUBLE_EQ(stats.instances_per_draw(), 100.0 / 8.0);
|
||
EXPECT_DOUBLE_EQ(stats.total_triangles, 100.0 * 100.0);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 流式 LOD — 预算分配
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
/// 单一 master:level 0 = 400 tris(精细)、1 = 120、2 = 30(最粗)
|
||
StreamLodConfig single_master_config(double budget, double max_distance = 0.0) {
|
||
StreamLodConfig cfg;
|
||
cfg.master_lod_triangles = {{400.0, 120.0, 30.0}};
|
||
cfg.budget_triangles = budget;
|
||
cfg.max_distance = max_distance;
|
||
return cfg;
|
||
}
|
||
|
||
/// 10 个实例,距离 0..9(下标越小越近)
|
||
std::vector<InstanceDraw> ten_instances() {
|
||
std::vector<InstanceDraw> v;
|
||
for (int i = 0; i < 10; ++i)
|
||
v.push_back(make_instance(0, 0, 400.0, static_cast<double>(i)));
|
||
return v;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(InstanceRenderTest, AssignLod_RespectsBudgetAndNearFirst) {
|
||
auto instances = ten_instances();
|
||
// Σ最粗 = 10*30 = 300;预算 480:仅够 2 个实例升到 level 1(各 +90)
|
||
const double budget = 480.0;
|
||
const double total = assign_lod_under_budget(instances, single_master_config(budget));
|
||
|
||
EXPECT_LE(total, budget);
|
||
EXPECT_DOUBLE_EQ(total, 480.0);
|
||
|
||
// 近处 2 个升到 level 1,其余保持 level 2
|
||
EXPECT_EQ(instances[0].lod_level, 1);
|
||
EXPECT_EQ(instances[1].lod_level, 1);
|
||
for (int i = 2; i < 10; ++i) EXPECT_EQ(instances[i].lod_level, 2);
|
||
EXPECT_DOUBLE_EQ(instances[0].triangle_count, 120.0);
|
||
EXPECT_DOUBLE_EQ(instances[9].triangle_count, 30.0);
|
||
|
||
// 距离升序 → 级别非递减(近者不低于远处精度)
|
||
for (int i = 1; i < 10; ++i)
|
||
EXPECT_LE(instances[i - 1].lod_level, instances[i].lod_level);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, AssignLod_UnlimitedBudget_AllHighestDetail) {
|
||
auto instances = ten_instances();
|
||
const double total = assign_lod_under_budget(instances, single_master_config(0.0));
|
||
|
||
EXPECT_DOUBLE_EQ(total, 4000.0);
|
||
for (const auto& inst : instances) {
|
||
EXPECT_EQ(inst.lod_level, 0);
|
||
EXPECT_DOUBLE_EQ(inst.triangle_count, 400.0);
|
||
}
|
||
}
|
||
|
||
TEST(InstanceRenderTest, AssignLod_CoarsestSumBudget_NoUpgrade) {
|
||
auto instances = ten_instances();
|
||
// 预算恰等于最粗总和 → 不得有任何升档(契约边界)
|
||
const double total = assign_lod_under_budget(instances, single_master_config(300.0));
|
||
|
||
EXPECT_DOUBLE_EQ(total, 300.0);
|
||
for (const auto& inst : instances) EXPECT_EQ(inst.lod_level, 2);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, AssignLod_MaxDistanceCapsFar) {
|
||
auto instances = ten_instances();
|
||
// 不限预算,但距离 > 4 的实例(下标 5..9)强制保留最粗
|
||
const double total =
|
||
assign_lod_under_budget(instances, single_master_config(0.0, 4.0));
|
||
|
||
EXPECT_DOUBLE_EQ(total, 5.0 * 400.0 + 5.0 * 30.0);
|
||
for (int i = 0; i <= 4; ++i) EXPECT_EQ(instances[i].lod_level, 0);
|
||
for (int i = 5; i < 10; ++i) EXPECT_EQ(instances[i].lod_level, 2);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, AssignLod_Deterministic) {
|
||
auto a = ten_instances();
|
||
auto b = ten_instances();
|
||
const double ta = assign_lod_under_budget(a, single_master_config(600.0));
|
||
const double tb = assign_lod_under_budget(b, single_master_config(600.0));
|
||
|
||
EXPECT_DOUBLE_EQ(ta, tb);
|
||
ASSERT_EQ(a.size(), b.size());
|
||
for (size_t i = 0; i < a.size(); ++i) {
|
||
EXPECT_EQ(a[i].lod_level, b[i].lod_level);
|
||
EXPECT_DOUBLE_EQ(a[i].triangle_count, b[i].triangle_count);
|
||
}
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 流式 LOD — 网格资源常驻集
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(InstanceRenderTest, LodMeshKey_OrderingEquality) {
|
||
EXPECT_TRUE((LodMeshKey{1, 0}) < (LodMeshKey{1, 1}));
|
||
EXPECT_TRUE((LodMeshKey{1, 1}) < (LodMeshKey{2, 0}));
|
||
EXPECT_FALSE((LodMeshKey{2, 0}) < (LodMeshKey{2, 0}));
|
||
EXPECT_EQ((LodMeshKey{3, 2}), (LodMeshKey{3, 2}));
|
||
EXPECT_NE((LodMeshKey{3, 2}), (LodMeshKey{3, 3}));
|
||
}
|
||
|
||
TEST(InstanceRenderTest, RequiredMeshKeys_DedupSorted) {
|
||
std::vector<InstanceBatch> batches = {
|
||
InstanceBatch{2, 0, 10.0, {0}},
|
||
InstanceBatch{1, 1, 10.0, {1}},
|
||
InstanceBatch{2, 0, 10.0, {2}},
|
||
InstanceBatch{1, 0, 10.0, {3}},
|
||
};
|
||
auto keys = required_mesh_keys(batches);
|
||
ASSERT_EQ(keys.size(), 3u);
|
||
EXPECT_EQ(keys[0], (LodMeshKey{1, 0}));
|
||
EXPECT_EQ(keys[1], (LodMeshKey{1, 1}));
|
||
EXPECT_EQ(keys[2], (LodMeshKey{2, 0}));
|
||
}
|
||
|
||
TEST(InstanceRenderTest, PlanStreaming_LoadEvictResident) {
|
||
const std::vector<LodMeshKey> prev = {{1, 0}, {1, 1}, {2, 0}};
|
||
std::vector<InstanceBatch> batches = {
|
||
InstanceBatch{1, 0, 10.0, {0}},
|
||
InstanceBatch{2, 0, 10.0, {1}},
|
||
InstanceBatch{2, 1, 3.0, {2}},
|
||
};
|
||
auto plan = plan_streaming(prev, batches);
|
||
|
||
EXPECT_EQ(plan.resident, (std::vector<LodMeshKey>{{1, 0}, {2, 0}, {2, 1}}));
|
||
EXPECT_EQ(plan.to_load, (std::vector<LodMeshKey>{{2, 1}}));
|
||
EXPECT_EQ(plan.to_evict, (std::vector<LodMeshKey>{{1, 1}}));
|
||
EXPECT_EQ(plan.resident_count(), 3u);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, PlanStreaming_NoChange) {
|
||
std::vector<InstanceBatch> batches = {
|
||
InstanceBatch{1, 0, 10.0, {0}},
|
||
InstanceBatch{2, 0, 10.0, {1}},
|
||
};
|
||
auto required = required_mesh_keys(batches);
|
||
auto plan = plan_streaming(required, batches);
|
||
|
||
EXPECT_TRUE(plan.to_load.empty());
|
||
EXPECT_TRUE(plan.to_evict.empty());
|
||
EXPECT_EQ(plan.resident, required);
|
||
}
|
||
|
||
TEST(InstanceRenderTest, PlanStreaming_InitialLoad) {
|
||
std::vector<InstanceBatch> batches = {
|
||
InstanceBatch{5, 2, 3.0, {0}},
|
||
};
|
||
auto plan = plan_streaming({}, batches);
|
||
|
||
EXPECT_EQ(plan.to_load, (std::vector<LodMeshKey>{{5, 2}}));
|
||
EXPECT_TRUE(plan.to_evict.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 百万零件级装配基准(批次 4 I1 验收)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(InstanceRenderTest, MillionInstanceAssembly_Benchmark) {
|
||
constexpr int kMasters = 16;
|
||
constexpr int kLevels = 3;
|
||
constexpr int kInstances = 1000000;
|
||
|
||
// 16 个 master、3 级 LOD(level 0 = 400 tris … level 2 = 30 tris)
|
||
StreamLodConfig cfg;
|
||
cfg.master_lod_triangles.assign(
|
||
kMasters, std::vector<double>{400.0, 120.0, 30.0});
|
||
|
||
// 构造百万实例:master 轮转、距离分层(越靠后的实例越远)
|
||
std::vector<InstanceDraw> instances;
|
||
instances.reserve(kInstances);
|
||
for (int i = 0; i < kInstances; ++i) {
|
||
InstanceDraw inst;
|
||
inst.master_id = static_cast<uint32_t>(i % kMasters);
|
||
inst.lod_level = 0;
|
||
inst.triangle_count = 400.0;
|
||
inst.distance = static_cast<double>(i % 1000) * 0.5;
|
||
inst.transform = Transform3D::Identity();
|
||
instances.push_back(inst);
|
||
}
|
||
|
||
// 1) 全精度实例化批处理:百万实例 → 每 master 一次绘制
|
||
auto batches = build_instance_batches(instances);
|
||
auto stats = summarize_batches(batches);
|
||
EXPECT_EQ(stats.instance_count, static_cast<size_t>(kInstances));
|
||
EXPECT_EQ(stats.unique_masters, static_cast<size_t>(kMasters));
|
||
EXPECT_EQ(stats.draw_calls, static_cast<size_t>(kMasters));
|
||
EXPECT_DOUBLE_EQ(stats.instances_per_draw(),
|
||
static_cast<double>(kInstances) / kMasters);
|
||
EXPECT_DOUBLE_EQ(stats.total_triangles, 400.0 * kInstances);
|
||
|
||
// 2) 流式 LOD 预算分配:总面数必须落到预算内,且批次仍受 (master, LOD) 约束
|
||
const double budget = 1.0e8; // 1 亿三角面
|
||
cfg.budget_triangles = budget;
|
||
const double total = assign_lod_under_budget(instances, cfg);
|
||
EXPECT_LE(total, budget);
|
||
// 初始最粗总和 = 1e6 * 30 = 3e7 ≤ budget,契约保证成立
|
||
EXPECT_GE(total, 30.0 * kInstances);
|
||
|
||
// 分配后重算批次:绘制调用数 ≤ master 数 × LOD 级数(实例化仍有效)
|
||
auto lodged = build_instance_batches(instances);
|
||
auto lodged_stats = summarize_batches(lodged);
|
||
EXPECT_EQ(lodged_stats.instance_count, static_cast<size_t>(kInstances));
|
||
EXPECT_LE(lodged_stats.draw_calls,
|
||
static_cast<size_t>(kMasters * kLevels));
|
||
EXPECT_DOUBLE_EQ(lodged_stats.total_triangles, total);
|
||
|
||
// 3) 流式资源计划:首帧全量加载,次帧无变化
|
||
auto plan0 = plan_streaming({}, lodged);
|
||
EXPECT_EQ(plan0.to_load, plan0.resident);
|
||
EXPECT_TRUE(plan0.to_evict.empty());
|
||
EXPECT_LE(plan0.resident_count(), static_cast<size_t>(kMasters * kLevels));
|
||
|
||
auto plan1 = plan_streaming(plan0.resident, lodged);
|
||
EXPECT_TRUE(plan1.to_load.empty());
|
||
EXPECT_TRUE(plan1.to_evict.empty());
|
||
}
|