decode 是切换耗时大头(4k ≈ 130-160ms),搬出主线程后运行时切换 只剩 GPU 段(~40ms)。IblGenerator 拆两段式:DecodeHdr(static, 纯 CPU 无线程状态,worker 可调)+ FromDecoded(GPU 生成,主线程); FromHdrEquirect 保留为同步便捷路径。Assets 层经 IAssetUploader 重载 + AssetManager 重载传递预解码产物(路径已缓存则丢弃去重)。 App 层单 in-flight 状态机:worker 只产出纯 CPU 像素数据,忙肋期 新请求记 pending 接力(只保留最终意图),OnUpdate 每帧收割 + SetEnvironment 换绑,析构 join。 实测(RTX 5060 Laptop,4 实例网格 + 运行中连切 3 环境): ~180fps 无感知尖峰;0 validation。
52 lines
1.8 KiB
C++
52 lines
1.8 KiB
C++
//
|
||
// Created by atdunbg on 2026/9/22.
|
||
//
|
||
|
||
#include <Engine/Assets/AssetManager.h>
|
||
#include <Engine/Core/Log.h>
|
||
|
||
namespace Engine::Assets {
|
||
|
||
AssetManager::AssetManager(IAssetUploader& uploader)
|
||
: m_Uploader(uploader)
|
||
{
|
||
ENGINE_INFO("AssetManager created");
|
||
}
|
||
|
||
ModelRef AssetManager::LoadModel(const std::string& path)
|
||
{
|
||
// GPU 上传经接口发生在渲染层实现里,本层只管缓存与句柄
|
||
return m_Models.Load(path,
|
||
[this](const std::string& p) { return m_Uploader.LoadModel(p); });
|
||
}
|
||
|
||
ModelRef AssetManager::CreatePlane(f32 halfSize, u8 r, u8 g, u8 b)
|
||
{
|
||
// 程序化资产复用同一缓存:键 = 伪路径 + 参数
|
||
std::string key = "procedural://plane/" + std::to_string(halfSize) +
|
||
"/" + std::to_string(r) + "," + std::to_string(g) +
|
||
"," + std::to_string(b);
|
||
return m_Models.Load(key,
|
||
[&](const std::string&) { return m_Uploader.CreatePlane(halfSize, r, g, b); });
|
||
}
|
||
|
||
EnvRef AssetManager::LoadEnvironment(const std::string& path)
|
||
{
|
||
// GPU 生成(compute 预积分)经接口发生在渲染层实现里
|
||
return m_Environments.Load(path,
|
||
[this](const std::string& p) { return m_Uploader.LoadEnvironment(p); });
|
||
}
|
||
|
||
EnvRef AssetManager::LoadEnvironment(const std::string& path,
|
||
DecodedEquirect&& decoded)
|
||
{
|
||
// 预解码数据移交 loader(Load 同步调用 lambda,引用捕获安全);
|
||
// 路径已缓存则 loader 不执行,decoded 自然丢弃(去重)
|
||
return m_Environments.Load(path,
|
||
[this, &decoded](const std::string& p) {
|
||
return m_Uploader.LoadEnvironment(p, std::move(decoded));
|
||
});
|
||
}
|
||
|
||
} // namespace Engine::Assets
|