Skip to content

Commit 26ee4f1

Browse files
committed
start adding the depth image
1 parent df4219e commit 26ee4f1

File tree

7 files changed

+147
-37
lines changed

7 files changed

+147
-37
lines changed

.clang-tidy

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ Checks: >
22
clang-analyzer-*,
33
bugprone-*,
44
performance-*,
5-
readability-*,
5+
hicpp-*,
66
modernize-*,
77
cppcoreguidelines-*,
8-
misc-*,
8+
readability-*,
9+
10+
# disable overly noisy checks
911
-clang-analyzer-alpha.*,
1012
-cppcoreguidelines-pro-type-union-access,
1113
-cppcoreguidelines-pro-type-reinterpret-cast,
1214
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
1315
-cppcoreguidelines-avoid-magic-numbers,
16+
-misc-unused-using-decls,
17+
-misc-unused-parameters,
18+
-hicpp-vararg,
1419
-modernize-use-trailing-return-type,
15-
-readability-magic-numbers
20+
-readability-const-parameter,
21+
-readability-const-variable
1622
1723
WarningsAsErrors: ''
18-
19-
HeaderFilterRegex: '.*'
24+
HeaderFilterRegex: '^(include|src)/'
2025
FormatStyle: file
2126

2227
CheckOptions:

src/vulkan/Pipeline.cpp

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,31 @@ std::vector<char> Pipeline::readFile(const std::string &filename) const {
2020

2121
Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
2222
const std::string &vertShaderPath, const std::string &fragShaderPath,
23-
const std::vector<vk::DescriptorSetLayout> &setLayouts, uint32_t samples)
23+
const std::vector<vk::DescriptorSetLayout> &setLayouts, uint32_t samples,
24+
vk::Format depthAttachmentFormat, bool depthWriteEnable)
2425
: m_device(device) {
2526
// 1. Read shader code from files
2627
auto vertShaderCode = readFile(vertShaderPath);
27-
auto fragShaderCode = readFile(fragShaderPath);
28-
2928
auto vertShaderModule = ShaderModule(m_device, vertShaderCode);
30-
auto fragShaderModule = ShaderModule(m_device, fragShaderCode);
3129

3230
vk::PipelineShaderStageCreateInfo vertStageInfo{};
3331
vertStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
3432
vertStageInfo.module = vertShaderModule.getHandle();
3533
vertStageInfo.pName = "main";
3634

37-
vk::PipelineShaderStageCreateInfo fragStageInfo{};
38-
fragStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
39-
fragStageInfo.module = fragShaderModule.getHandle();
40-
fragStageInfo.pName = "main";
35+
std::vector<vk::PipelineShaderStageCreateInfo> shaderStages = {vertStageInfo};
36+
37+
std::unique_ptr<ShaderModule> fragShaderModule;
38+
if (!fragShaderPath.empty()) {
39+
auto fragShaderCode = readFile(fragShaderPath);
40+
fragShaderModule = std::make_unique<ShaderModule>(m_device, fragShaderCode);
4141

42-
vk::PipelineShaderStageCreateInfo shaderStages[] = {vertStageInfo, fragStageInfo};
42+
vk::PipelineShaderStageCreateInfo fragStageInfo{};
43+
fragStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
44+
fragStageInfo.module = fragShaderModule->getHandle();
45+
fragStageInfo.pName = "main";
46+
shaderStages.push_back(fragStageInfo);
47+
}
4348

4449
// 3. Vertex input (empty, for a basic triangle with no vertex buffer)
4550
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{};
@@ -67,19 +72,28 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
6772
// 7. Multisampling (disabled)
6873
vk::PipelineMultisampleStateCreateInfo multisampling{};
6974
multisampling.sampleShadingEnable = VK_FALSE;
70-
7175
multisampling.rasterizationSamples = utils::mapSampleCountFlag(samples);
7276

77+
vk::PipelineDepthStencilStateCreateInfo depthStencil{};
78+
if (depthAttachmentFormat != vk::Format::eUndefined) {
79+
depthStencil.depthTestEnable = VK_TRUE;
80+
depthStencil.depthWriteEnable = depthWriteEnable ? VK_TRUE : VK_FALSE;
81+
depthStencil.depthCompareOp = vk::CompareOp::eLess;
82+
depthStencil.depthBoundsTestEnable = VK_FALSE;
83+
depthStencil.stencilTestEnable = VK_FALSE;
84+
}
85+
7386
// 8. Color blending
7487
vk::PipelineColorBlendAttachmentState colorBlendAttachment{};
7588
colorBlendAttachment.colorWriteMask =
7689
vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
7790
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
7891
colorBlendAttachment.blendEnable = VK_FALSE;
92+
7993
vk::PipelineColorBlendStateCreateInfo colorBlending{};
8094
colorBlending.logicOpEnable = VK_FALSE;
81-
colorBlending.attachmentCount = 1;
82-
colorBlending.pAttachments = &colorBlendAttachment;
95+
colorBlending.attachmentCount = (colorAttachmentFormat != vk::Format::eUndefined) ? 1 : 0;
96+
colorBlending.pAttachments = (colorAttachmentFormat != vk::Format::eUndefined) ? &colorBlendAttachment : nullptr;
8397

8498
// 9. Pipeline layout
8599
vk::PipelineLayoutCreateInfo pipelineLayoutInfo{};
@@ -97,16 +111,16 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
97111

98112
// 11. Dynamic rendering
99113
vk::PipelineRenderingCreateInfo renderingInfo{};
100-
renderingInfo.colorAttachmentCount = 1;
101-
renderingInfo.pColorAttachmentFormats = &colorAttachmentFormat;
114+
renderingInfo.colorAttachmentCount = (colorAttachmentFormat != vk::Format::eUndefined) ? 1 : 0;
115+
renderingInfo.pColorAttachmentFormats = (colorAttachmentFormat != vk::Format::eUndefined) ? &colorAttachmentFormat : nullptr;
102116
renderingInfo.viewMask = 0;
103-
renderingInfo.depthAttachmentFormat = vk::Format::eUndefined;
117+
renderingInfo.depthAttachmentFormat = depthAttachmentFormat;
104118
renderingInfo.stencilAttachmentFormat = vk::Format::eUndefined;
105119

106120
// 11. Create graphics pipeline
107121
vk::GraphicsPipelineCreateInfo pipelineInfo{};
108-
pipelineInfo.stageCount = 2;
109-
pipelineInfo.pStages = shaderStages;
122+
pipelineInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
123+
pipelineInfo.pStages = shaderStages.data();
110124
pipelineInfo.pVertexInputState = &vertexInputInfo;
111125
pipelineInfo.pInputAssemblyState = &inputAssembly;
112126
pipelineInfo.pViewportState = &viewportState;
@@ -129,10 +143,42 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
129143
}
130144

131145
Pipeline::~Pipeline() {
132-
if (m_pipeline)
146+
if (m_pipeline) {
133147
m_device.destroyPipeline(m_pipeline);
134-
if (m_pipelineLayout)
148+
}
149+
if (m_pipelineLayout) {
135150
m_device.destroyPipelineLayout(m_pipelineLayout);
136151
}
152+
}
153+
154+
Pipeline::Pipeline(Pipeline&& other) noexcept
155+
: m_device(other.m_device),
156+
m_pipelineLayout(other.m_pipelineLayout),
157+
m_pipeline(other.m_pipeline)
158+
{
159+
other.m_pipelineLayout = nullptr;
160+
other.m_pipeline = nullptr;
161+
}
162+
163+
Pipeline& Pipeline::operator=(Pipeline&& other) noexcept
164+
{
165+
if (this != &other) {
166+
// Destroy our own Vulkan objects first
167+
if (m_pipeline) {
168+
m_device.destroyPipeline(m_pipeline);
169+
}
170+
if (m_pipelineLayout) {
171+
m_device.destroyPipelineLayout(m_pipelineLayout);
172+
}
173+
m_device = other.m_device;
174+
m_pipelineLayout = other.m_pipelineLayout;
175+
m_pipeline = other.m_pipeline;
176+
177+
other.m_pipelineLayout = nullptr;
178+
other.m_pipeline = nullptr;
179+
}
180+
return *this;
181+
}
182+
137183

138184
} // namespace reactor

src/vulkan/Pipeline.hpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@ namespace reactor {
1010
public:
1111
Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
1212
const std::string& vertShaderPath, const std::string& fragShaderPath,
13-
const std::vector<vk::DescriptorSetLayout>& setLayouts, uint32_t samples);
13+
const std::vector<vk::DescriptorSetLayout>& setLayouts, uint32_t samples,
14+
vk::Format depthAttachmentFormat = vk::Format::eUndefined, bool depthWriteEnable = true);
1415
~Pipeline();
1516

16-
vk::Pipeline get() const { return m_pipeline; }
17-
vk::PipelineLayout getLayout() const { return m_pipelineLayout; }
17+
[[nodiscard]] vk::Pipeline get() const { return m_pipeline; }
18+
[[nodiscard]] vk::PipelineLayout getLayout() const { return m_pipelineLayout; }
19+
20+
// Delete copy operations
21+
Pipeline(const Pipeline&) = delete;
22+
Pipeline& operator=(const Pipeline&) = delete;
23+
24+
// Move operations
25+
Pipeline(Pipeline&& other) noexcept;
26+
Pipeline& operator=(Pipeline&& other) noexcept;
1827

1928
private:
2029
vk::Device m_device;
21-
vk::PipelineLayout m_pipelineLayout{};
22-
vk::Pipeline m_pipeline{};
30+
vk::PipelineLayout m_pipelineLayout;
31+
vk::Pipeline m_pipeline;
32+
33+
[[nodiscard]] std::vector<char> readFile(const std::string& filename) const;
2334

24-
std::vector<char> readFile(const std::string& filename) const;
25-
vk::ShaderModule createShaderModule(const std::vector<char>& code);
2635

27-
// Prevent copying
28-
Pipeline(const Pipeline&) = delete;
29-
Pipeline& operator=(const Pipeline&) = delete;
3036
};
3137

3238
} // namespace reactor

src/vulkan/UniformManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class UniformManager {
3232

3333
// Get the descriptor info need to update a descriptor set.
3434
template <typename T>
35-
vk::DescriptorBufferInfo getDescriptorInfo(size_t frameIndex) const {
35+
[[nodiscard]] vk::DescriptorBufferInfo getDescriptorInfo(size_t frameIndex) const {
3636
const auto& name = m_uboTypeMap.at(std::type_index(typeid(T)));
3737
const auto& buffer = m_uniformBuffers.at(name)[frameIndex];
3838

src/vulkan/VulkanContext.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class VulkanContext {
4242
void createLogicalDevice();
4343

4444
bool isDeviceSuitable(vk::PhysicalDevice device);
45-
QueueFamilyIndices findQueueFamilies(vk::PhysicalDevice device) const;
45+
[[nodiscard]] QueueFamilyIndices findQueueFamilies(vk::PhysicalDevice device) const;
4646

4747
// Member variables - these are owned by the context
4848
vk::Instance m_instance;

src/vulkan/VulkanRenderer.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ VulkanRenderer::VulkanRenderer(const RendererConfig& config, Window& window, Cam
2323
createMSAAImage();
2424
createResolveImages();
2525
createSceneViewImages();
26+
createDepthImages();
2627
createSampler();
2728
createDescriptorSets();
2829

@@ -532,8 +533,55 @@ void VulkanRenderer::createDescriptorSets() {
532533
const auto framesInFlight = m_frameManager->getFramesInFlightCount();
533534
m_sceneViewImageDescriptorSets.resize(framesInFlight);
534535
for (int i = 0; i < framesInFlight; ++i) {
535-
m_sceneViewImageDescriptorSets[i] = m_imgui->createDescriptorSet(m_sceneViewViews[i], m_sampler->get());
536+
m_sceneViewImageDescriptorSets[i] =
537+
m_imgui->createDescriptorSet(m_sceneViewViews[i], m_sampler->get());
536538
}
537539
}
540+
void VulkanRenderer::createDepthImages() {
541+
vk::Format format = vk::Format::eD32Sfloat; // Common depth format
542+
vk::Extent2D extent = m_swapchain->getExtent();
543+
size_t framesInFlight = m_frameManager->getFramesInFlightCount();
544+
545+
for (size_t i = 0; i < m_depthImages.size(); ++i) {
546+
m_context->device().destroyImageView(m_depthViews[i]);
547+
}
548+
m_depthImages.clear();
549+
m_depthViews.clear();
550+
551+
m_depthImages.resize(framesInFlight);
552+
m_depthViews.resize(framesInFlight);
553+
for (size_t i = 0; i < framesInFlight; ++i) {
554+
vk::ImageCreateInfo imageInfo{};
555+
imageInfo.imageType = vk::ImageType::e2D;
556+
imageInfo.extent.width = extent.width;
557+
imageInfo.extent.height = extent.height;
558+
imageInfo.extent.depth = 1;
559+
imageInfo.mipLevels = 1;
560+
imageInfo.arrayLayers = 1;
561+
imageInfo.format = format;
562+
imageInfo.tiling = vk::ImageTiling::eOptimal;
563+
imageInfo.initialLayout = vk::ImageLayout::eUndefined;
564+
imageInfo.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment |
565+
vk::ImageUsageFlagBits::eSampled;
566+
imageInfo.samples = vk::SampleCountFlagBits::e1;
567+
imageInfo.sharingMode = vk::SharingMode::eExclusive;
568+
569+
m_depthImages[i] = std::make_unique<Image>(*m_allocator, imageInfo, VMA_MEMORY_USAGE_GPU_ONLY);
570+
m_imageStateTracker.recordState(m_depthImages[i]->get(), vk::ImageLayout::eUndefined);
571+
572+
vk::ImageViewCreateInfo viewInfo{};
573+
viewInfo.image = m_depthImages[i]->get();
574+
viewInfo.viewType = vk::ImageViewType::e2D;
575+
viewInfo.format = format;
576+
viewInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eDepth;
577+
viewInfo.subresourceRange.baseMipLevel = 0;
578+
viewInfo.subresourceRange.levelCount = 1;
579+
viewInfo.subresourceRange.baseArrayLayer = 0;
580+
viewInfo.subresourceRange.layerCount = 1;
581+
582+
m_depthViews[i] = m_context->device().createImageView(viewInfo);
583+
}
584+
585+
}
538586

539587
} // namespace reactor

src/vulkan/VulkanRenderer.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class VulkanRenderer {
6363
std::vector<vk::ImageView> m_sceneViewViews;
6464
std::vector<vk::DescriptorSet> m_sceneViewImageDescriptorSets;
6565

66+
std::vector<std::unique_ptr<Image>> m_depthImages;
67+
std::vector<vk::ImageView> m_depthViews;
68+
std::vector<vk::DescriptorSet> m_depthImageDescriptorSets;
69+
6670
void createCoreVulkanObjects();
6771
void createSwapchainAndFrameManager();
6872
void createPipelineAndDescriptors();
@@ -72,6 +76,7 @@ class VulkanRenderer {
7276
void createSceneViewImages();
7377
void createSampler();
7478
void createDescriptorSets();
79+
void createDepthImages();
7580

7681
void handleSwapchainResizing();
7782
void beginCommandBuffer(vk::CommandBuffer cmd);

0 commit comments

Comments
 (0)