Skip to content

Commit b277b6e

Browse files
committed
Add Dynamic Rendering
1 parent 1f2d1cc commit b277b6e

File tree

14 files changed

+1547
-8
lines changed

14 files changed

+1547
-8
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
export module App;
2+
3+
import std;
4+
import glfw;
5+
import vulkan_hpp;
6+
7+
import DataLoader;
8+
import Context;
9+
import Window;
10+
import Device;
11+
import Swapchain;
12+
import DepthImage;
13+
// import RenderPass;
14+
import GraphicsPipeline;
15+
import CommandPool;
16+
import InputAssembly;
17+
import UniformBuffer;
18+
import TextureSampler;
19+
import Descriptor;
20+
import Drawer;
21+
22+
export namespace vht {
23+
class App {
24+
std::shared_ptr<vht::DataLoader> m_data_loader{ nullptr };
25+
std::shared_ptr<vht::Context> m_context{ nullptr };
26+
std::shared_ptr<vht::Window> m_window{ nullptr };
27+
std::shared_ptr<vht::Device> m_device{ nullptr };
28+
std::shared_ptr<vht::Swapchain> m_swapchain{ nullptr };
29+
std::shared_ptr<vht::DepthImage> m_depth_image{ nullptr };
30+
// std::shared_ptr<vht::RenderPass> m_render_pass{ nullptr };
31+
std::shared_ptr<vht::GraphicsPipeline> m_graphics_pipeline{ nullptr };
32+
std::shared_ptr<vht::CommandPool> m_command_pool{ nullptr };
33+
std::shared_ptr<vht::InputAssembly> m_input_assembly{ nullptr };
34+
std::shared_ptr<vht::UniformBuffer> m_uniform_buffer{ nullptr };
35+
std::shared_ptr<vht::TextureSampler> m_texture_sampler{ nullptr };
36+
std::shared_ptr<vht::Descriptor> m_descriptor{ nullptr };
37+
std::shared_ptr<vht::Drawer> m_drawer{ nullptr };
38+
public:
39+
void run() {
40+
init();
41+
while (!glfw::window_should_close(m_window->ptr())) {
42+
glfw::poll_events();
43+
m_drawer->draw();
44+
}
45+
std::println("device waitIdle");
46+
m_device->device().waitIdle();
47+
std::println("finished");
48+
}
49+
private:
50+
void init() {
51+
init_data_loader();
52+
init_context();
53+
init_window();
54+
std::println("window created");
55+
init_device();
56+
std::println("device created");
57+
init_swapchain();
58+
std::println("swapchain created");
59+
init_command_pool(); // 前移命令池的创建
60+
std::println("command pool created");
61+
init_depth_image();
62+
std::println("depth image created");
63+
// init_render_pass();
64+
// std::println("render pass created");
65+
init_graphics_pipeline();
66+
std::println("graphics pipeline created");
67+
init_uniform_buffer();
68+
std::println("uniform buffer created");
69+
init_texture_sampler();
70+
std::println("texture sampler created");
71+
init_input_assembly();
72+
std::println("input assembly created");
73+
init_descriptor();
74+
std::println("descriptor created");
75+
init_drawer();
76+
std::println("drawer created");
77+
}
78+
void init_data_loader() { m_data_loader = std::make_shared<vht::DataLoader>(); }
79+
void init_context() { m_context = std::make_shared<vht::Context>( true ); }
80+
void init_window() { m_window = std::make_shared<vht::Window>( m_context ); }
81+
void init_device() { m_device = std::make_shared<vht::Device>( m_context, m_window ); }
82+
void init_swapchain() { m_swapchain = std::make_shared<vht::Swapchain>( m_window, m_device ); }
83+
void init_depth_image() { m_depth_image = std::make_shared<vht::DepthImage>( m_device, m_swapchain, m_command_pool ); }
84+
// void init_render_pass() { m_render_pass = std::make_shared<vht::RenderPass>( m_window, m_device, m_swapchain, m_depth_image ); }
85+
void init_graphics_pipeline() { m_graphics_pipeline = std::make_shared<vht::GraphicsPipeline>( m_device, m_swapchain, m_depth_image ); }
86+
void init_command_pool() { m_command_pool = std::make_shared<vht::CommandPool>( m_device ); }
87+
void init_input_assembly() { m_input_assembly = std::make_shared<vht::InputAssembly>( m_data_loader, m_device, m_command_pool ); }
88+
void init_uniform_buffer() { m_uniform_buffer = std::make_shared<vht::UniformBuffer>( m_window, m_device, m_swapchain ); }
89+
void init_texture_sampler() { m_texture_sampler = std::make_shared<vht::TextureSampler>( m_device, m_command_pool ); }
90+
void init_descriptor() { m_descriptor = std::make_shared<vht::Descriptor>( m_device, m_graphics_pipeline, m_uniform_buffer, m_texture_sampler ); }
91+
void init_drawer() {
92+
m_drawer = std::make_shared<vht::Drawer>(
93+
m_data_loader,
94+
m_window,
95+
m_device,
96+
m_swapchain,
97+
m_depth_image,
98+
m_graphics_pipeline,
99+
m_command_pool,
100+
m_input_assembly,
101+
m_uniform_buffer,
102+
m_descriptor
103+
);
104+
}
105+
};
106+
}
7.05 KB
Binary file not shown.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
export module DepthImage;
2+
3+
import std;
4+
import vulkan_hpp;
5+
6+
import Tools;
7+
import Device;
8+
import Swapchain;
9+
import CommandPool;
10+
11+
export namespace vht {
12+
13+
/**
14+
* @brief 深度图像相关
15+
* @details
16+
* - 依赖:
17+
* - m_device: 物理/逻辑设备与队列
18+
* - m_swapchain: 交换链
19+
* - 工作:
20+
* - 创建深度图像
21+
* - 创建深度图像视图
22+
* - 可访问成员:
23+
* - image(): 深度图像
24+
* - image_view(): 深度图像视图
25+
* - format(): 深度图像格式
26+
*/
27+
class DepthImage {
28+
std::shared_ptr<vht::Device> m_device{ nullptr };
29+
std::shared_ptr<vht::Swapchain> m_swapchain{ nullptr };
30+
std::shared_ptr<vht::CommandPool> m_command_pool{ nullptr };
31+
vk::raii::DeviceMemory m_memory{ nullptr };
32+
vk::raii::Image m_image{ nullptr };
33+
vk::raii::ImageView m_image_view{ nullptr };
34+
vk::Format m_format{};
35+
public:
36+
explicit DepthImage(
37+
std::shared_ptr<vht::Device> device,
38+
std::shared_ptr<vht::Swapchain> swapchain,
39+
std::shared_ptr<vht::CommandPool> command_pool
40+
): m_device(std::move(device)),
41+
m_swapchain(std::move(swapchain)),
42+
m_command_pool(std::move(command_pool)) {
43+
init();
44+
}
45+
46+
[[nodiscard]]
47+
const vk::raii::Image& image() const { return m_image; }
48+
[[nodiscard]]
49+
const vk::raii::ImageView& image_view() const { return m_image_view; }
50+
[[nodiscard]]
51+
vk::Format format() const { return m_format; }
52+
53+
// 重建深度图像和视图
54+
void recreate() {
55+
m_image_view = nullptr;
56+
m_image = nullptr;
57+
create_depth_resources();
58+
}
59+
private:
60+
void init() {
61+
find_depth_format();
62+
create_depth_resources();
63+
}
64+
// 查找支持的深度格式
65+
void find_depth_format() {
66+
for(const vk::Format format : { vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint }) {
67+
if(const auto props = m_device->physical_device().getFormatProperties(format);
68+
props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment
69+
) {
70+
m_format = format;
71+
return;
72+
}
73+
}
74+
throw std::runtime_error("failed to find supported format!");
75+
}
76+
// 创建深度图像和视图
77+
void create_depth_resources() {
78+
create_image(
79+
m_image,
80+
m_memory,
81+
m_device->device(),
82+
m_device->physical_device(),
83+
m_swapchain->extent().width,
84+
m_swapchain->extent().height,
85+
m_format,
86+
vk::ImageTiling::eOptimal,
87+
vk::ImageUsageFlagBits::eDepthStencilAttachment,
88+
vk::MemoryPropertyFlagBits::eDeviceLocal
89+
);
90+
m_image_view = create_image_view(
91+
m_device->device(),
92+
m_image,
93+
m_format,
94+
vk::ImageAspectFlagBits::eDepth
95+
);
96+
97+
transition_image_layout(
98+
m_command_pool->pool(),
99+
m_device->device(),
100+
m_device->graphics_queue(),
101+
m_image,
102+
vk::ImageLayout::eUndefined,
103+
vk::ImageLayout::eAttachmentOptimal,
104+
vk::ImageAspectFlagBits::eDepth
105+
);
106+
}
107+
108+
};
109+
110+
}
111+
112+
Binary file not shown.

0 commit comments

Comments
 (0)