添加多模型渲染,添加贴图渲染,添加平行光, 暂时不上传assets

This commit is contained in:
2026-09-21 14:07:20 +08:00
parent 381b7c0aa6
commit 7d628f8d8a
37 changed files with 9817 additions and 189 deletions
+5
View File
@@ -8,6 +8,8 @@
#include <vulkan/vulkan.hpp>
#include "MemoryAllocator.h"
namespace Engine {
class Device;
@@ -39,6 +41,9 @@ namespace Engine {
vk::DeviceSize m_BlockSize; // 所属内存块的总大小
MemoryAllocator::Allocation m_Allocation;
MemoryAllocator& m_Allocator;
void* m_MappedPtr = nullptr;
};
@@ -8,11 +8,18 @@
#include <Engine/Core/Types.h>
#include <vulkan/vulkan.hpp>
#include <vector>
namespace Engine {
class Device;
struct DescriptorBinding {
u32 Binding;
vk::DescriptorType Type;
vk::ShaderStageFlags Stages;
};
class DescriptorManager {
public:
DescriptorManager(Device& device);
@@ -21,18 +28,24 @@ namespace Engine {
DescriptorManager(const DescriptorManager&) = delete;
DescriptorManager& operator=(const DescriptorManager&) = delete;
// 创建一个描述符布局:一个 binding,类型是 Uniform Buffer
vk::DescriptorSetLayout CreateUniformBufferLayout(u32 binding);
vk::DescriptorSetLayout CreateLayout(const std::vector<DescriptorBinding>& bindings);
vk::PipelineLayout CreatePipelineLayout(vk::DescriptorSetLayout layout);
// 根据布局分配一个描述符集
vk::DescriptorSet AllocateDescriptorSet(vk::DescriptorSetLayout layout);
// 把 Buffer 绑到描述符集的某个 binding
void UpdateUniformBuffer(vk::DescriptorSet set, u32 binding,
vk::Buffer buffer, u64 size);
void UpdateTexture(vk::DescriptorSet set, u32 binding,
vk::Sampler sampler, vk::ImageView view);
// 单个 set layout 的旧接口(保留)
vk::PipelineLayout CreatePipelineLayout(vk::DescriptorSetLayout layout,
u32 pushConstantSize);
// 多 set layout 的新接口
vk::PipelineLayout CreatePipelineLayout(
const std::vector<vk::DescriptorSetLayout>& layouts,
u32 pushConstantSize);
private:
Device& m_Device;
@@ -43,4 +56,5 @@ namespace Engine {
} // namespace Engine
#endif //HELLOVULKAN_DESCRIPTORMANAGER_H
+23 -3
View File
@@ -6,10 +6,14 @@
#define HELLOVULKAN_MEMORYALLOCATOR_H
#pragma once
#include <vulkan/vulkan.hpp>
#include <array>
#include <vector>
#include "Engine/Core/Types.h"
namespace Engine {
class Device;
@@ -26,23 +30,39 @@ namespace Engine {
vk::DeviceMemory Memory;
vk::DeviceSize Offset;
vk::DeviceSize Size;
void* MappedPtr; // HOST_VISIBLE 时才有
void* MappedPtr = nullptr;
// 内部用于 Free
u32 BlockIndex = 0;
u32 TypeIndex = 0;
bool Valid = false;
};
Allocation Allocate(vk::MemoryRequirements reqs, vk::MemoryPropertyFlags props);
void Free(const Allocation& alloc);
void PrintStats() const;
private:
struct FreeRange {
vk::DeviceSize Offset;
vk::DeviceSize Size;
};
struct Block {
vk::DeviceMemory Memory;
vk::DeviceSize Size;
vk::DeviceSize Used;
vk::DeviceSize Used = 0; // bump 分配用
void* MappedPtr = nullptr;
bool IsMapped = false;
std::vector<FreeRange> FreeList; // 已释放的区域
};
Device& m_Device;
std::array<std::vector<Block>, VK_MAX_MEMORY_TYPES> m_Blocks;
vk::DeviceSize m_DefaultBlockSize = 256ull * 1024 * 1024; // 256 MB
std::array<vk::DeviceSize, VK_MAX_MEMORY_TYPES> m_TotalAllocated{};
vk::DeviceSize GetBlockSize(vk::MemoryPropertyFlags props) const;
};
} // namespace Engine
+5 -3
View File
@@ -30,14 +30,16 @@ namespace Engine {
ShaderManager(const ShaderManager&) = delete;
ShaderManager& operator=(const ShaderManager&) = delete;
// layout 可选:为 nullptr 时不使用描述符
// 改成接受 vector
ShaderObject CreateFromFile(const std::string& spvPath,
vk::ShaderStageFlagBits stage,
vk::DescriptorSetLayout layout = nullptr);
const std::vector<vk::DescriptorSetLayout>& layouts = {},
u32 pushConstantSize = 0);
ShaderObject CreateFromSPIRV(const std::vector<u32>& spirv,
vk::ShaderStageFlagBits stage,
vk::DescriptorSetLayout layout = nullptr);
const std::vector<vk::DescriptorSetLayout>& layouts = {},
u32 pushConstantSize = 0);
void Destroy(const ShaderObject& shader);
+56
View File
@@ -0,0 +1,56 @@
//
// 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 <string>
#include "MemoryAllocator.h"
namespace Engine {
class Device;
class MemoryAllocator;
class Texture {
public:
Texture(Device& device, MemoryAllocator& allocator, const std::string& path);
~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::Sampler GetSampler() const { return m_Sampler; }
private:
Device& m_Device;
vk::Image m_Image;
vk::ImageView m_View;
vk::DeviceMemory m_Memory;
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