refactor: Cubemap 拆掉 17 参超级构造——资源三元组 EnvTexture

- image/view/显存分配收口为 EnvTexture 单元(RHI 惯例:三者同生共死),
  Cubemap = 4 个 EnvTexture + 2 采样器的组合,构造 17 参 → 8 参
- UploadCube/UploadTexture2D 直接返回完整资源单元(view 创建收进
  helper),工厂函数只做"生成数据 → 上传 → 组装"
- 析构循环成对释放,杜绝半套泄漏;删除无调用者的 Get()/GetFaceSize()/
  GetPrefilteredMipCount()(shader mip 上限以常量契约)
This commit is contained in:
2026-09-23 19:25:32 +08:00
parent bd4ac6aa12
commit d2b878c077
2 changed files with 92 additions and 138 deletions
+25 -49
View File
@@ -26,6 +26,15 @@ namespace Engine {
class Device;
// GPU 纹理资源三元组(RHI 惯例):image/view/显存分配同生共死,
// 析构统一释放——杜绝"建了 image 丢了 view"类半套泄漏。
// 上传 helper 直接以此为单位产出,消费端只拿引用计数语义的整单元
struct EnvTexture {
vk::Image image{};
vk::ImageView view{};
MemoryAllocator::Allocation allocation;
};
class Cubemap {
public:
// 从等距柱状 HDR 生成(CPU 重采样 + irradiance/prefilter/BRDF 预积分)
@@ -45,68 +54,35 @@ namespace Engine {
Cubemap(const Cubemap&) = delete;
Cubemap& operator=(const Cubemap&) = delete;
// ---- 环境 cube(Skybox 背景)----
vk::Image Get() const { return m_EnvImage; }
vk::ImageView GetView() const { return m_EnvView; }
// ---- 消费端契约(SkyboxPass:背景;SceneGpu:PBR ambient)----
vk::ImageView GetView() const { return m_Env.view; } // 环境 cube
vk::ImageView GetIrradianceView() const { return m_Irr.view; } // 漫反射
vk::ImageView GetPrefilteredView() const { return m_Pref.view; } // 镜面
vk::ImageView GetBrdfLutView() const { return m_Lut.view; } // LUT
// ---- 漫反射辐照度 cube(PBR ambient diffuse 项)----
vk::ImageView GetIrradianceView() const { return m_IrrView; }
// ---- pre-filtered cube(PBR ambient specular 项,按 mip 分 roughness)----
vk::ImageView GetPrefilteredView() const { return m_PrefView; }
u32 GetPrefilteredMipCount() const { return m_PrefMipLevels; }
// ---- BRDF LUT(envBRDF 查表,vec2(NdotV, roughness) → F 缩放/偏置)----
vk::ImageView GetBrdfLutView() const { return m_LutView; }
// cube 资源共享采样器(maxLod 覆盖 pre-filter mip 链)
vk::Sampler GetSampler() const { return m_EnvSampler; }
// LUT 专用采样器(clamp-to-edge):共享的 border 白采样器会在
// uv=1.0 处混入 50% 白色 border,导致高 roughness 镜面偏大
vk::Sampler GetBrdfLutSampler() const { return m_LutSampler; }
vk::Sampler GetSampler() const { return m_Sampler; }
u32 GetFaceSize() const { return m_FaceSize; }
private:
Cubemap(Device& device, MemoryAllocator& allocator,
vk::Image envImage, vk::ImageView envView,
vk::Image irrImage, vk::ImageView irrView,
vk::Image prefImage, vk::ImageView prefView, u32 prefMips,
vk::Image lutImage, vk::ImageView lutView,
vk::Sampler sampler, vk::Sampler lutSampler,
MemoryAllocator::Allocation envAlloc,
MemoryAllocator::Allocation irrAlloc,
MemoryAllocator::Allocation prefAlloc,
MemoryAllocator::Allocation lutAlloc, u32 faceSize)
EnvTexture env, EnvTexture irr, EnvTexture pref,
EnvTexture lut, vk::Sampler envSampler, vk::Sampler lutSampler)
: m_Device(device), m_Allocator(allocator),
m_EnvImage(envImage), m_EnvView(envView),
m_IrrImage(irrImage), m_IrrView(irrView),
m_PrefImage(prefImage), m_PrefView(prefView),
m_PrefMipLevels(prefMips),
m_LutImage(lutImage), m_LutView(lutView),
m_Sampler(sampler), m_LutSampler(lutSampler),
m_EnvAllocation(envAlloc), m_IrrAllocation(irrAlloc),
m_PrefAllocation(prefAlloc), m_LutAllocation(lutAlloc),
m_FaceSize(faceSize) {}
m_Env(env), m_Irr(irr), m_Pref(pref), m_Lut(lut),
m_EnvSampler(envSampler), m_LutSampler(lutSampler) {}
Device& m_Device;
MemoryAllocator& m_Allocator;
vk::Image m_EnvImage;
vk::ImageView m_EnvView;
vk::Image m_IrrImage;
vk::ImageView m_IrrView;
vk::Image m_PrefImage;
vk::ImageView m_PrefView;
u32 m_PrefMipLevels;
vk::Image m_LutImage;
vk::ImageView m_LutView;
vk::Sampler m_Sampler;
EnvTexture m_Env; // 环境 cube(Skybox 背景)
EnvTexture m_Irr; // 漫反射辐照度 cube
EnvTexture m_Pref; // pre-filtered cube(mip 分 roughness)
EnvTexture m_Lut; // BRDF 积分 LUT(2D)
vk::Sampler m_EnvSampler;
vk::Sampler m_LutSampler;
MemoryAllocator::Allocation m_EnvAllocation;
MemoryAllocator::Allocation m_IrrAllocation;
MemoryAllocator::Allocation m_PrefAllocation;
MemoryAllocator::Allocation m_LutAllocation;
u32 m_FaceSize;
};
} // namespace Engine