feat: LOD 网格分级——meshopt 解析期简化 + 按距离/级联选级
vendored meshoptimizer 1.2(submodule 环境故障改直接 vendor)。Mesh 多 LOD:各级索引连续拼进同一 index buffer(共享顶点,LodRange 区间 选择),解析期 meshopt_simplify 生成链(LOD0=原始,每级目标减半, 相对误差 1e-2,小网格复用上级);都在 worker 线程,加载零额外卡顿。 SceneRenderer 按 (mesh, LOD) 聚合 batch(lookup key = 指针<<8|lod): 主视图按屏占比近似 radius/dist 选级(阈值 10/20 倍半径);阴影级联 按级联序降级(越远越简)。Draw 按 batch.lod 选索引区间。 A/B 对比(100 背包 10×10,RTX 5060 Laptop,vsync off): - 纯背包:LOD 开 93-95 fps / 关 90-107 fps —— 无显著差异 - 加 SunTemple:LOD 开 52-56 / 关 51-55 —— 无显著差异 结论:当前场景瓶颈不在顶点数(5060 顶点吞吐富余),LOD 基础设施 保留(几何复杂资产/低端 GPU/更大场景生效),帧率问题转向 Tracy GPU 时间线定位真实瓶颈。 坑:LodRange 记录"LOD 不足复用最后一级"时 first 不得继续累加 (buffer 无重复数据,累加必越界——VUID-08798 抓的)。
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
#include <meshoptimizer.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -81,13 +83,14 @@ Model::Model(Device& device, MemoryAllocator& allocator,
|
||||
{{ h, 0.0f, -h}, {0, 1, 0}, {0.0f, 0.0f}},
|
||||
};
|
||||
const std::vector<u32> indices = {0, 1, 2, 0, 2, 3};
|
||||
const std::vector<std::vector<u32>> lodIndices = {indices};
|
||||
|
||||
// 材质/纹理生命周期随模型(缓存持有)
|
||||
std::string paths[Material::kSlotCount] = {"", "", "", ""};
|
||||
Material* mat = GetOrCreateMaterial(paths, MaterialParams{}); // 全默认材质
|
||||
|
||||
m_Meshes.push_back(CreateScope<Mesh>(
|
||||
m_Device, m_Allocator, vertices, indices, mat));
|
||||
m_Device, m_Allocator, vertices, lodIndices, mat));
|
||||
}
|
||||
|
||||
Scope<Model> Model::CreatePlane(Device& device, MemoryAllocator& allocator,
|
||||
@@ -158,7 +161,7 @@ Scope<Model> Model::FromParsed(Device& device, MemoryAllocator& allocator,
|
||||
for (auto& pm : parsed.meshes) {
|
||||
Material* mat = model->GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
||||
model->m_Meshes.push_back(CreateScope<Mesh>(
|
||||
device, allocator, std::move(pm.vertices), std::move(pm.indices), mat));
|
||||
device, allocator, pm.vertices, pm.lods, mat));
|
||||
}
|
||||
ENGINE_INFO("Model loaded: {} ({} meshes, {} textures)",
|
||||
parsed.path, model->m_Meshes.size(), model->m_TextureCache.size());
|
||||
@@ -177,7 +180,7 @@ void Model::LoadModel(const std::string& path)
|
||||
for (auto& pm : parsed.meshes) {
|
||||
Material* mat = GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
||||
m_Meshes.push_back(CreateScope<Mesh>(
|
||||
m_Device, m_Allocator, std::move(pm.vertices), std::move(pm.indices), mat));
|
||||
m_Device, m_Allocator, pm.vertices, pm.lods, mat));
|
||||
}
|
||||
ENGINE_INFO("Model loaded: {} ({} meshes, {} textures)",
|
||||
path, m_Meshes.size(), m_TextureCache.size());
|
||||
@@ -256,7 +259,7 @@ ParsedMesh Model::ProcessMesh(const aiMesh* mesh, const aiScene* scene,
|
||||
{
|
||||
ParsedMesh out;
|
||||
std::vector<Vertex>& vertices = out.vertices;
|
||||
std::vector<u32>& indices = out.indices;
|
||||
std::vector<u32> indices;
|
||||
|
||||
vertices.reserve(mesh->mNumVertices);
|
||||
|
||||
@@ -386,6 +389,32 @@ ParsedMesh Model::ProcessMesh(const aiMesh* mesh, const aiScene* scene,
|
||||
out.texPaths[slot] = std::move(texPaths[slot]);
|
||||
}
|
||||
out.params = params;
|
||||
|
||||
// ---- LOD 链(解析期生成,worker 线程免费时间):----
|
||||
// LOD0 = 原始;每级目标索引数减半,相对误差 1e-2。顶点共享,
|
||||
// 简化只重排索引(meshopt_simplify 从 LOD0 单次起点逐级向下)
|
||||
out.lods.push_back(std::move(indices));
|
||||
for (u32 lod = 1; lod < kMeshLodCount; ++lod) {
|
||||
const size_t target = out.lods[0].size() >> lod;
|
||||
// 太小的网格不简化:简化收益低于误差风险,直接复用上一级
|
||||
if (target < 96 || out.lods[0].size() < 192) {
|
||||
out.lods.push_back(out.lods.back());
|
||||
continue;
|
||||
}
|
||||
std::vector<u32> dest(out.lods[0].size());
|
||||
float error = 0.0f;
|
||||
const size_t count = meshopt_simplify(
|
||||
dest.data(),
|
||||
out.lods[0].data(), out.lods[0].size(),
|
||||
&out.vertices[0].position.x, out.vertices.size(), sizeof(Vertex),
|
||||
target, 1e-2f, 0, &error);
|
||||
if (count == 0) {
|
||||
out.lods.push_back(out.lods.back()); // 简化失败兜底
|
||||
continue;
|
||||
}
|
||||
dest.resize(count);
|
||||
out.lods.push_back(std::move(dest));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user