- Material:单贴图 → 4 槽(binding=Slot 枚举),MakeCacheKey 组合键 保持材质去重(同贴图组合共享描述符集) - Model:读取 aiTextureType DIFFUSE/NORMALS/SPECULAR/EMISSIVE 四类路径; aiProcess_CalcTangentSpace 生成切线(Vertex +tangent 属性,location 3); 每槽默认纹理兜底(白/平法线(128,128,255)/黑/黑,1x1) - 纹理 sRGB 语义按槽位:颜色图 SRGB 变体,数据图(Normal/Specular)线性 UNORM——Texture 路径加载加 srgb 参数;修复 InitFromPixels 图文 format 不一致的 validation 警告 - UBO 尾部追加 cameraPos(Blinn-Phong V 向量) - simple_shader.frag:TBN 法线贴图(无切线/平法线退化回几何法线)+ Blinn-Phong 高光(specular 图强度系数)+ 自发光直加 - SunTemple 实测:148 张 DDS 全槽位加载(96 张线性变体)、1056 mesh、 validation 零警告
82 lines
2.5 KiB
C++
82 lines
2.5 KiB
C++
//
|
||
// Created by atdunbg on 2026/9/20.
|
||
//
|
||
|
||
#ifndef HELLOVULKAN_TEXTURE_H
|
||
#define HELLOVULKAN_TEXTURE_H
|
||
|
||
#pragma once
|
||
|
||
#include <Engine/Core/Types.h>
|
||
#include <vulkan/vulkan.hpp>
|
||
#include <array>
|
||
#include <string>
|
||
|
||
#include "MemoryAllocator.h"
|
||
|
||
namespace Engine {
|
||
|
||
class Device;
|
||
class MemoryAllocator;
|
||
|
||
class Texture {
|
||
public:
|
||
// 从磁盘图片加载(按扩展名分发:.dds → DdsLoader,其它 → stbi)。
|
||
// srgb:颜色贴图语义(BaseColor/Emissive)为 true;数据图
|
||
//(Normal/Specular 等)为 false —— 决定 vk 格式的 _SRGB 变体
|
||
Texture(Device& device, MemoryAllocator& allocator, const std::string& path,
|
||
bool srgb = true);
|
||
|
||
// 程序化纹理:纯色(1x1),用于地面/调试等不依赖磁盘资产的场景
|
||
struct ProceduralTag {};
|
||
static Scope<Texture> CreateSolidColor(Device& device, MemoryAllocator& allocator,
|
||
u8 r, u8 g, u8 b, bool srgb = true);
|
||
|
||
~Texture();
|
||
|
||
Texture(const Texture&) = delete;
|
||
Texture& operator=(const Texture&) = delete;
|
||
|
||
vk::Image Get() const { return m_Image; }
|
||
vk::ImageView GetView() const { return m_View; }
|
||
u32 GetWidth() const { return m_Width; }
|
||
u32 GetHeight() const { return m_Height; }
|
||
vk::Format GetFormat() const { return m_Format; }
|
||
u32 GetMipLevels() const { return m_MipLevels; }
|
||
|
||
vk::Sampler GetSampler() const { return m_Sampler; }
|
||
|
||
private:
|
||
// 两段式构造:先确定像素来源(磁盘 / 程序化),再走公共上传路径
|
||
Texture(Device& device, MemoryAllocator& allocator, ProceduralTag,
|
||
const std::array<u8, 4>& rgba, bool srgb);
|
||
|
||
// 公共路径:staging → device local image → view/sampler(RGBA8 单 mip)
|
||
void InitFromPixels(const void* pixels, u32 width, u32 height, bool srgb);
|
||
|
||
// DDS 路径:压缩数据直传(BC 硬件采样解码)+ 文件自带 mip 链
|
||
void InitFromDds(const std::string& path, bool srgb);
|
||
|
||
Device& m_Device;
|
||
vk::Image m_Image;
|
||
vk::ImageView m_View;
|
||
|
||
vk::Format m_Format = vk::Format::eUndefined;
|
||
u32 m_MipLevels = 1;
|
||
|
||
vk::Sampler m_Sampler;
|
||
|
||
|
||
u32 m_Width = 0;
|
||
u32 m_Height = 0;
|
||
|
||
u64 m_TextureBytes = 0;
|
||
|
||
MemoryAllocator::Allocation m_ImageAllocation;
|
||
MemoryAllocator& m_Allocator;
|
||
};
|
||
|
||
} // namespace Engine
|
||
|
||
#endif //HELLOVULKAN_TEXTURE_H
|