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:
@@ -47,4 +47,10 @@ target_compile_definitions(Engine PUBLIC TRACY_ON_DEMAND)
|
|||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
target_link_libraries(Engine PUBLIC Threads::Threads)
|
target_link_libraries(Engine PUBLIC Threads::Threads)
|
||||||
|
|
||||||
|
# meshoptimizer(vendored,LOD 网格简化;submodule 机制故障改为直接 vendor)
|
||||||
|
file(GLOB MESHOPT_SOURCES ${CMAKE_SOURCE_DIR}/Lib/meshoptimizer/src/*.cpp)
|
||||||
|
add_library(meshopt STATIC ${MESHOPT_SOURCES})
|
||||||
|
target_include_directories(meshopt PUBLIC ${CMAKE_SOURCE_DIR}/Lib/meshoptimizer/src)
|
||||||
|
target_link_libraries(Engine PUBLIC meshopt)
|
||||||
|
|
||||||
target_compile_definitions(Engine PRIVATE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
|
target_compile_definitions(Engine PRIVATE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
|
||||||
@@ -18,13 +18,18 @@ namespace Engine
|
|||||||
class Buffer;
|
class Buffer;
|
||||||
class Material;
|
class Material;
|
||||||
|
|
||||||
|
// LOD 级数:LOD0 = 原始网格,越高越简(解析期 meshopt_simplify 生成,
|
||||||
|
// 共享顶点数据,只换索引区间)
|
||||||
|
inline constexpr u32 kMeshLodCount = 3;
|
||||||
|
|
||||||
class Mesh
|
class Mesh
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// material 非拥有指针:Model 的材质缓存拥有(同贴图共享一份)
|
// material 非拥有指针:Model 的材质缓存拥有(同贴图共享一份)
|
||||||
|
// lods[0] 必须为原始索引(与 vertices 对应),各级简化链由解析期生成
|
||||||
Mesh(Device& device, MemoryAllocator& allocator,
|
Mesh(Device& device, MemoryAllocator& allocator,
|
||||||
const std::vector<Vertex>& vertices,
|
const std::vector<Vertex>& vertices,
|
||||||
const std::vector<u32>& indices,
|
const std::vector<std::vector<u32>>& lods,
|
||||||
Material* material);
|
Material* material);
|
||||||
~Mesh();
|
~Mesh();
|
||||||
|
|
||||||
@@ -32,11 +37,16 @@ namespace Engine
|
|||||||
Mesh& operator=(const Mesh&) = delete;
|
Mesh& operator=(const Mesh&) = delete;
|
||||||
|
|
||||||
// instanceCount/firstInstance:GPUDriven 按 mesh 分 batch 后,
|
// instanceCount/firstInstance:GPUDriven 按 mesh 分 batch 后,
|
||||||
// 同 mesh 的 N 个实体一次 draw 完成,模型矩阵经实例索引取
|
// 同 mesh 的 N 个实体一次 draw 完成,模型矩阵经实例索引取。
|
||||||
void Draw(vk::CommandBuffer cmd, u32 instanceCount = 1, u32 firstInstance = 0) const;
|
// lod:索引区间选择(越界取最高级)
|
||||||
|
void Draw(vk::CommandBuffer cmd, u32 instanceCount = 1,
|
||||||
|
u32 firstInstance = 0, u32 lod = 0) const;
|
||||||
|
|
||||||
Material* GetMaterial() const { return m_Material; }
|
Material* GetMaterial() const { return m_Material; }
|
||||||
u32 GetIndexCount() const { return m_IndexCount; }
|
u32 GetLodIndexCount(u32 lod) const
|
||||||
|
{
|
||||||
|
return m_Lods[lod < kMeshLodCount ? lod : kMeshLodCount - 1].indexCount;
|
||||||
|
}
|
||||||
|
|
||||||
// 局部空间包围球(构造时从顶点算出),视锥剔除用
|
// 局部空间包围球(构造时从顶点算出),视锥剔除用
|
||||||
struct Bounds {
|
struct Bounds {
|
||||||
@@ -46,10 +56,16 @@ namespace Engine
|
|||||||
const Bounds& GetBounds() const { return m_Bounds; }
|
const Bounds& GetBounds() const { return m_Bounds; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// 同一 index buffer 内连续排布的各级索引区间
|
||||||
|
struct LodRange {
|
||||||
|
u32 firstIndex = 0;
|
||||||
|
u32 indexCount = 0;
|
||||||
|
};
|
||||||
|
|
||||||
Scope<Buffer> m_VertexBuffer;
|
Scope<Buffer> m_VertexBuffer;
|
||||||
Scope<Buffer> m_IndexBuffer;
|
Scope<Buffer> m_IndexBuffer;
|
||||||
|
std::array<LodRange, kMeshLodCount> m_Lods{};
|
||||||
Material* m_Material;
|
Material* m_Material;
|
||||||
u32 m_IndexCount = 0;
|
|
||||||
Bounds m_Bounds;
|
Bounds m_Bounds;
|
||||||
|
|
||||||
u64 m_VertexBytes = 0;
|
u64 m_VertexBytes = 0;
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ namespace Engine
|
|||||||
// worker 线程产出(ParseFromDisk),主线程消费(FromParsed)。
|
// worker 线程产出(ParseFromDisk),主线程消费(FromParsed)。
|
||||||
struct ParsedMesh {
|
struct ParsedMesh {
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<u32> indices;
|
// LOD 链([0] = 原始索引,越往后越简):解析期 meshopt_simplify
|
||||||
|
// 生成(worker 线程),消费端共享顶点只换索引
|
||||||
|
std::vector<std::vector<u32>> lods;
|
||||||
std::array<std::string, Material::kSlotCount> texPaths;
|
std::array<std::string, Material::kSlotCount> texPaths;
|
||||||
MaterialParams params;
|
MaterialParams params;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -73,7 +73,9 @@ namespace Engine {
|
|||||||
// 时实例分散各处),扁平化成连续数组推迟到 appendView
|
// 时实例分散各处),扁平化成连续数组推迟到 appendView
|
||||||
std::vector<std::vector<glm::mat4>> batchWorlds;
|
std::vector<std::vector<glm::mat4>> batchWorlds;
|
||||||
std::vector<std::vector<glm::mat4>> batchPrevWorlds;
|
std::vector<std::vector<glm::mat4>> batchPrevWorlds;
|
||||||
std::unordered_map<Mesh*, u32> lookup;
|
// key = (Mesh*, LOD):同 mesh 不同 LOD 索引区间不同,
|
||||||
|
// 必须分开成 batch(低 8 位 LOD,高位指针,指针 8 字节对齐安全)
|
||||||
|
std::unordered_map<u64, u32> lookup;
|
||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,11 +19,13 @@ namespace Engine {
|
|||||||
|
|
||||||
class Mesh;
|
class Mesh;
|
||||||
|
|
||||||
// GPUDriven:按 mesh 聚合的绘制批
|
// GPUDriven:按 (mesh, LOD) 聚合的绘制批——同 mesh 不同 LOD 的实例
|
||||||
|
// 索引区间不同,不能并成一次 draw
|
||||||
struct InstanceBatch {
|
struct InstanceBatch {
|
||||||
Mesh* mesh;
|
Mesh* mesh;
|
||||||
u32 first; // 在实例矩阵数组中的起始下标
|
u32 first; // 在实例矩阵数组中的起始下标
|
||||||
u32 count; // 实例数
|
u32 count; // 实例数
|
||||||
|
u32 lod = 0; // 绘制用索引区间(Mesh::Draw)
|
||||||
};
|
};
|
||||||
|
|
||||||
// 一个渲染视图的剔除结果(相机视图 / 光源视图各一份)
|
// 一个渲染视图的剔除结果(相机视图 / 光源视图各一份)
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ namespace Engine
|
|||||||
{
|
{
|
||||||
Mesh::Mesh(Device& device, MemoryAllocator& allocator,
|
Mesh::Mesh(Device& device, MemoryAllocator& allocator,
|
||||||
const std::vector<Vertex>& vertices,
|
const std::vector<Vertex>& vertices,
|
||||||
const std::vector<u32>& indices,
|
const std::vector<std::vector<u32>>& lods,
|
||||||
Material* material)
|
Material* material)
|
||||||
: m_Material(material)
|
: m_Material(material)
|
||||||
{
|
{
|
||||||
if (vertices.empty() || indices.empty())
|
if (vertices.empty() || lods.empty() || lods[0].empty())
|
||||||
{
|
{
|
||||||
ENGINE_ERROR("Mesh: empty vertices or indices");
|
ENGINE_ERROR("Mesh: empty vertices or LOD0 indices");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,15 +35,31 @@ namespace Engine
|
|||||||
vk::MemoryPropertyFlagBits::eHostCoherent);
|
vk::MemoryPropertyFlagBits::eHostCoherent);
|
||||||
m_VertexBuffer->Upload(vertices.data(), vbSize);
|
m_VertexBuffer->Upload(vertices.data(), vbSize);
|
||||||
|
|
||||||
u64 ibSize = sizeof(u32) * indices.size();
|
// 各级索引连续拼进同一 index buffer(共享顶点,只换索引区间)
|
||||||
|
std::vector<u32> allIndices;
|
||||||
|
for (const auto& idx : lods) {
|
||||||
|
allIndices.insert(allIndices.end(), idx.begin(), idx.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 ibSize = sizeof(u32) * allIndices.size();
|
||||||
m_IndexBuffer = CreateScope<Buffer>(
|
m_IndexBuffer = CreateScope<Buffer>(
|
||||||
device, allocator, ibSize,
|
device, allocator, ibSize,
|
||||||
vk::BufferUsageFlagBits::eIndexBuffer,
|
vk::BufferUsageFlagBits::eIndexBuffer,
|
||||||
vk::MemoryPropertyFlagBits::eHostVisible |
|
vk::MemoryPropertyFlagBits::eHostVisible |
|
||||||
vk::MemoryPropertyFlagBits::eHostCoherent);
|
vk::MemoryPropertyFlagBits::eHostCoherent);
|
||||||
m_IndexBuffer->Upload(indices.data(), ibSize);
|
m_IndexBuffer->Upload(allIndices.data(), ibSize);
|
||||||
|
|
||||||
m_IndexCount = static_cast<u32>(indices.size());
|
// 记录各级区间。LOD 不足的级复用最后一级的"已有区间"
|
||||||
|
//(first 不得继续累加——buffer 里没有重复数据,累加必越界)
|
||||||
|
u32 first = 0;
|
||||||
|
for (u32 lod = 0; lod < kMeshLodCount; ++lod) {
|
||||||
|
if (lod < lods.size()) {
|
||||||
|
m_Lods[lod] = {first, static_cast<u32>(lods[lod].size())};
|
||||||
|
first += m_Lods[lod].indexCount;
|
||||||
|
} else {
|
||||||
|
m_Lods[lod] = m_Lods[lods.size() - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 局部空间包围球:顶点包围盒 → 中心 + 最大半径(视锥剔除用)
|
// 局部空间包围球:顶点包围盒 → 中心 + 最大半径(视锥剔除用)
|
||||||
{
|
{
|
||||||
@@ -61,10 +77,12 @@ namespace Engine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ENGINE_INFO("Mesh created: {} vertices, {} indices", vertices.size(), indices.size());
|
ENGINE_INFO("Mesh created: {} vertices, LOD0 {} indices", vertices.size(), lods[0].size());
|
||||||
|
|
||||||
m_VertexBytes = sizeof(Vertex) * vertices.size();
|
m_VertexBytes = sizeof(Vertex) * vertices.size();
|
||||||
m_IndexBytes = sizeof(u32) * indices.size();
|
for (const auto& idx : lods) {
|
||||||
|
m_IndexBytes += sizeof(u32) * idx.size();
|
||||||
|
}
|
||||||
STATS.AddMesh(m_VertexBytes, m_IndexBytes);
|
STATS.AddMesh(m_VertexBytes, m_IndexBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,10 +91,14 @@ namespace Engine
|
|||||||
STATS.RemoveMesh(m_VertexBytes, m_IndexBytes);
|
STATS.RemoveMesh(m_VertexBytes, m_IndexBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::Draw(vk::CommandBuffer cmd, u32 instanceCount, u32 firstInstance) const
|
void Mesh::Draw(vk::CommandBuffer cmd, u32 instanceCount, u32 firstInstance,
|
||||||
|
u32 lod) const
|
||||||
{
|
{
|
||||||
|
const LodRange& range =
|
||||||
|
m_Lods[lod < kMeshLodCount ? lod : kMeshLodCount - 1];
|
||||||
cmd.bindVertexBuffers(0, m_VertexBuffer->Get(), {0});
|
cmd.bindVertexBuffers(0, m_VertexBuffer->Get(), {0});
|
||||||
cmd.bindIndexBuffer(m_IndexBuffer->Get(), 0, vk::IndexType::eUint32);
|
cmd.bindIndexBuffer(m_IndexBuffer->Get(), 0, vk::IndexType::eUint32);
|
||||||
cmd.drawIndexed(m_IndexCount, instanceCount, 0, 0, firstInstance);
|
cmd.drawIndexed(range.indexCount, instanceCount, range.firstIndex,
|
||||||
|
0, firstInstance);
|
||||||
}
|
}
|
||||||
} // namespace Engine
|
} // namespace Engine
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <assimp/scene.h>
|
#include <assimp/scene.h>
|
||||||
#include <assimp/postprocess.h>
|
#include <assimp/postprocess.h>
|
||||||
|
|
||||||
|
#include <meshoptimizer.h>
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -81,13 +83,14 @@ Model::Model(Device& device, MemoryAllocator& allocator,
|
|||||||
{{ h, 0.0f, -h}, {0, 1, 0}, {0.0f, 0.0f}},
|
{{ 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<u32> indices = {0, 1, 2, 0, 2, 3};
|
||||||
|
const std::vector<std::vector<u32>> lodIndices = {indices};
|
||||||
|
|
||||||
// 材质/纹理生命周期随模型(缓存持有)
|
// 材质/纹理生命周期随模型(缓存持有)
|
||||||
std::string paths[Material::kSlotCount] = {"", "", "", ""};
|
std::string paths[Material::kSlotCount] = {"", "", "", ""};
|
||||||
Material* mat = GetOrCreateMaterial(paths, MaterialParams{}); // 全默认材质
|
Material* mat = GetOrCreateMaterial(paths, MaterialParams{}); // 全默认材质
|
||||||
|
|
||||||
m_Meshes.push_back(CreateScope<Mesh>(
|
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,
|
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) {
|
for (auto& pm : parsed.meshes) {
|
||||||
Material* mat = model->GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
Material* mat = model->GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
||||||
model->m_Meshes.push_back(CreateScope<Mesh>(
|
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)",
|
ENGINE_INFO("Model loaded: {} ({} meshes, {} textures)",
|
||||||
parsed.path, model->m_Meshes.size(), model->m_TextureCache.size());
|
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) {
|
for (auto& pm : parsed.meshes) {
|
||||||
Material* mat = GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
Material* mat = GetOrCreateMaterial(pm.texPaths.data(), pm.params);
|
||||||
m_Meshes.push_back(CreateScope<Mesh>(
|
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)",
|
ENGINE_INFO("Model loaded: {} ({} meshes, {} textures)",
|
||||||
path, m_Meshes.size(), m_TextureCache.size());
|
path, m_Meshes.size(), m_TextureCache.size());
|
||||||
@@ -256,7 +259,7 @@ ParsedMesh Model::ProcessMesh(const aiMesh* mesh, const aiScene* scene,
|
|||||||
{
|
{
|
||||||
ParsedMesh out;
|
ParsedMesh out;
|
||||||
std::vector<Vertex>& vertices = out.vertices;
|
std::vector<Vertex>& vertices = out.vertices;
|
||||||
std::vector<u32>& indices = out.indices;
|
std::vector<u32> indices;
|
||||||
|
|
||||||
vertices.reserve(mesh->mNumVertices);
|
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.texPaths[slot] = std::move(texPaths[slot]);
|
||||||
}
|
}
|
||||||
out.params = params;
|
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;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ namespace Engine::Passes
|
|||||||
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
||||||
m_Gpu.forwardLayout, 1,
|
m_Gpu.forwardLayout, 1,
|
||||||
batch.mesh->GetMaterial()->GetDescriptorSet(), {});
|
batch.mesh->GetMaterial()->GetDescriptorSet(), {});
|
||||||
batch.mesh->Draw(cmd, batch.count, batch.first);
|
batch.mesh->Draw(cmd, batch.count, batch.first, batch.lod);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.endRendering();
|
cmd.endRendering();
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ namespace Engine::Passes
|
|||||||
&m_Frame.ubo.lightSpace[c]);
|
&m_Frame.ubo.lightSpace[c]);
|
||||||
|
|
||||||
for (const auto& batch : m_Frame.shadowCascades[c].batches) {
|
for (const auto& batch : m_Frame.shadowCascades[c].batches) {
|
||||||
batch.mesh->Draw(cmd, batch.count, batch.first);
|
batch.mesh->Draw(cmd, batch.count, batch.first, batch.lod);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.endRendering();
|
cmd.endRendering();
|
||||||
|
|||||||
@@ -297,7 +297,19 @@ namespace Engine
|
|||||||
glm::vec3(t.World() * glm::vec4(bounds.center, 1.0f));
|
glm::vec3(t.World() * glm::vec4(bounds.center, 1.0f));
|
||||||
const f32 worldRadius = bounds.radius * worldScale;
|
const f32 worldRadius = bounds.radius * worldScale;
|
||||||
|
|
||||||
auto tryAppend = [&](ViewScratch& s, const Frustum& frustum) {
|
// LOD 选级:屏占比近似 = radius/距离(主视图按实例距离;
|
||||||
|
// 阈值 10/20 倍半径,可调)
|
||||||
|
const f32 dist =
|
||||||
|
glm::length(worldCenter - camTransform->GetPosition());
|
||||||
|
u32 mainLod = 0;
|
||||||
|
if (dist >= worldRadius * 20.0f) {
|
||||||
|
mainLod = 2;
|
||||||
|
} else if (dist >= worldRadius * 10.0f) {
|
||||||
|
mainLod = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto tryAppend = [&](ViewScratch& s, const Frustum& frustum,
|
||||||
|
u32 lod) {
|
||||||
if (!frustum.IntersectsSphere(worldCenter, worldRadius)) {
|
if (!frustum.IntersectsSphere(worldCenter, worldRadius)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -309,10 +321,12 @@ namespace Engine
|
|||||||
// 单实体时两序号恰好恒等,多实体共享 mesh 后分叉
|
// 单实体时两序号恰好恒等,多实体共享 mesh 后分叉
|
||||||
// → batches[worldsIdx] 越界)。batch.first(GPU
|
// → batches[worldsIdx] 越界)。batch.first(GPU
|
||||||
// firstInstance 基址)才是 worlds 索引,语义别混
|
// firstInstance 基址)才是 worlds 索引,语义别混
|
||||||
|
const u64 key =
|
||||||
|
(reinterpret_cast<u64>(mesh.get()) << 8) | lod;
|
||||||
const auto [it, inserted] = s.lookup.emplace(
|
const auto [it, inserted] = s.lookup.emplace(
|
||||||
mesh.get(), static_cast<u32>(s.result.batches.size()));
|
key, static_cast<u32>(s.result.batches.size()));
|
||||||
if (inserted) {
|
if (inserted) {
|
||||||
s.result.batches.push_back({mesh.get(), 0, 0});
|
s.result.batches.push_back({mesh.get(), 0, 0, lod});
|
||||||
s.batchWorlds.emplace_back();
|
s.batchWorlds.emplace_back();
|
||||||
s.batchPrevWorlds.emplace_back();
|
s.batchPrevWorlds.emplace_back();
|
||||||
}
|
}
|
||||||
@@ -326,9 +340,12 @@ namespace Engine
|
|||||||
++totalInstances;
|
++totalInstances;
|
||||||
};
|
};
|
||||||
|
|
||||||
tryAppend(m_MainScratch, mainFrustum);
|
tryAppend(m_MainScratch, mainFrustum, mainLod);
|
||||||
for (u32 c = 0; c < kMaxCascades; ++c) {
|
for (u32 c = 0; c < kMaxCascades; ++c) {
|
||||||
tryAppend(m_CascScratch[c], cascFrustum[c]);
|
// 级联降级:越远的级联覆盖范围越大,用越低的 LOD
|
||||||
|
//(阴影本来就是低频信息,误差可接受)
|
||||||
|
tryAppend(m_CascScratch[c], cascFrustum[c],
|
||||||
|
std::min(c, kMeshLodCount - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user