Skip to content

Commit 3f454b4

Browse files
committed
Add multisample
1 parent a606451 commit 3f454b4

File tree

12 files changed

+2520
-8
lines changed

12 files changed

+2520
-8
lines changed

docs/codes/0800_multisample/main.cpp

Lines changed: 1784 additions & 0 deletions
Large diffs are not rendered by default.

docs/codes/0800_multisample/main.diff

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
diff --git a/src/main.cpp b/src/main.cpp
2+
index 89a0f11..ad5d479 100644
3+
--- a/src/main.cpp
4+
+++ b/src/main.cpp
5+
@@ -94,6 +94,10 @@ private:
6+
vk::Format m_swapChainImageFormat;
7+
vk::Extent2D m_swapChainExtent;
8+
std::vector<vk::raii::ImageView> m_swapChainImageViews;
9+
+ vk::SampleCountFlagBits m_msaaSamples = vk::SampleCountFlagBits::e1;
10+
+ vk::raii::DeviceMemory m_colorImageMemory{ nullptr };
11+
+ vk::raii::Image m_colorImage{ nullptr };
12+
+ vk::raii::ImageView m_colorImageView{ nullptr };
13+
vk::raii::DeviceMemory m_depthImageMemory{ nullptr };
14+
vk::raii::Image m_depthImage{ nullptr };
15+
vk::raii::ImageView m_depthImageView{ nullptr };
16+
@@ -137,6 +141,7 @@ private:
17+
createDescriptorSetLayout();
18+
createGraphicsPipeline();
19+
createCommandPool();
20+
+ createColorResources();
21+
createDepthResources();
22+
createFramebuffers();
23+
createTextureImage();
24+
@@ -313,6 +318,7 @@ private:
25+
for (const auto& it : physicalDevices) {
26+
if (isDeviceSuitable(it)) {
27+
m_physicalDevice = it;
28+
+ m_msaaSamples = getMaxUsableSampleCount();
29+
break;
30+
}
31+
}
32+
@@ -509,13 +515,13 @@ private:
33+
void createRenderPass() {
34+
vk::AttachmentDescription colorAttachment;
35+
colorAttachment.format = m_swapChainImageFormat;
36+
- colorAttachment.samples = vk::SampleCountFlagBits::e1;
37+
+ colorAttachment.samples = m_msaaSamples;
38+
colorAttachment.loadOp = vk::AttachmentLoadOp::eClear;
39+
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
40+
colorAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
41+
colorAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
42+
colorAttachment.initialLayout = vk::ImageLayout::eUndefined;
43+
- colorAttachment.finalLayout = vk::ImageLayout::ePresentSrcKHR;
44+
+ colorAttachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;
45+
46+
vk::AttachmentReference colorAttachmentRef;
47+
colorAttachmentRef.attachment = 0;
48+
@@ -523,7 +529,7 @@ private:
49+
50+
vk::AttachmentDescription depthAttachment;
51+
depthAttachment.format = findDepthFormat();
52+
- depthAttachment.samples = vk::SampleCountFlagBits::e1;
53+
+ depthAttachment.samples = m_msaaSamples;
54+
depthAttachment.loadOp = vk::AttachmentLoadOp::eClear;
55+
depthAttachment.storeOp = vk::AttachmentStoreOp::eDontCare;
56+
depthAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
57+
@@ -534,13 +540,30 @@ private:
58+
vk::AttachmentReference depthAttachmentRef;
59+
depthAttachmentRef.attachment = 1;
60+
depthAttachmentRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
61+
+
62+
+ vk::AttachmentDescription colorAttachmentResolve;
63+
+ colorAttachmentResolve.format = m_swapChainImageFormat;
64+
+ colorAttachmentResolve.samples = vk::SampleCountFlagBits::e1;
65+
+ colorAttachmentResolve.loadOp = vk::AttachmentLoadOp::eClear;
66+
+ colorAttachmentResolve.storeOp = vk::AttachmentStoreOp::eStore;
67+
+ colorAttachmentResolve.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
68+
+ colorAttachmentResolve.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
69+
+ colorAttachmentResolve.initialLayout = vk::ImageLayout::eUndefined;
70+
+ colorAttachmentResolve.finalLayout = vk::ImageLayout::ePresentSrcKHR;
71+
+
72+
+ vk::AttachmentReference colorAttachmentResolveRef;
73+
+ colorAttachmentResolveRef.attachment = 2;
74+
+ colorAttachmentResolveRef.layout = vk::ImageLayout::eColorAttachmentOptimal;
75+
+
76+
77+
vk::SubpassDescription subpass;
78+
subpass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
79+
- subpass.setColorAttachments( colorAttachmentRef );
80+
+ subpass.colorAttachmentCount = 1;
81+
+ subpass.pColorAttachments = &colorAttachmentRef;
82+
subpass.pDepthStencilAttachment = &depthAttachmentRef;
83+
+ subpass.pResolveAttachments = &colorAttachmentResolveRef;
84+
85+
- auto attachments = { colorAttachment, depthAttachment };
86+
+ auto attachments = { colorAttachment, depthAttachment, colorAttachmentResolve };
87+
vk::RenderPassCreateInfo renderPassInfo;
88+
renderPassInfo.setAttachments( attachments );
89+
renderPassInfo.setSubpasses( subpass );
90+
@@ -635,7 +658,7 @@ private:
91+
rasterizer.depthBiasEnable = false;
92+
93+
vk::PipelineMultisampleStateCreateInfo multisampling;
94+
- multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;
95+
+ multisampling.rasterizationSamples = m_msaaSamples;
96+
multisampling.sampleShadingEnable = false; // default
97+
98+
vk::PipelineColorBlendAttachmentState colorBlendAttachment;
99+
@@ -694,7 +717,11 @@ private:
100+
for (size_t i = 0; i < m_swapChainImageViews.size(); i++) {
101+
vk::FramebufferCreateInfo framebufferInfo;
102+
framebufferInfo.renderPass = m_renderPass;
103+
- std::array<vk::ImageView, 2> attachments { m_swapChainImageViews[i], m_depthImageView };
104+
+ std::array<vk::ImageView, 3> attachments {
105+
+ m_colorImageView,
106+
+ m_depthImageView,
107+
+ m_swapChainImageViews[i]
108+
+ };
109+
framebufferInfo.setAttachments( attachments );
110+
framebufferInfo.width = m_swapChainExtent.width;
111+
framebufferInfo.height = m_swapChainExtent.height;
112+
@@ -872,6 +899,10 @@ private:
113+
m_depthImage = nullptr;
114+
m_depthImageMemory = nullptr;
115+
116+
+ m_colorImageView = nullptr;
117+
+ m_colorImage = nullptr;
118+
+ m_colorImageMemory = nullptr;
119+
+
120+
m_swapChainImageViews.clear();
121+
m_swapChainImages.clear(); // optional
122+
m_swapChain = nullptr;
123+
@@ -879,6 +910,7 @@ private:
124+
125+
createSwapChain();
126+
createImageViews();
127+
+ createColorResources();
128+
createDepthResources();
129+
createFramebuffers();
130+
131+
@@ -1192,6 +1224,7 @@ private:
132+
uint32_t width,
133+
uint32_t height,
134+
uint32_t mipLevels,
135+
+ vk::SampleCountFlagBits numSamples,
136+
vk::Format format,
137+
vk::ImageTiling tilling,
138+
vk::ImageUsageFlags usage,
139+
@@ -1210,7 +1243,7 @@ private:
140+
imageInfo.tiling = tilling;
141+
imageInfo.initialLayout = vk::ImageLayout::eUndefined;
142+
imageInfo.usage = usage;
143+
- imageInfo.samples = vk::SampleCountFlagBits::e1;
144+
+ imageInfo.samples = numSamples;
145+
imageInfo.sharingMode = vk::SharingMode::eExclusive;
146+
147+
image = m_device.createImage(imageInfo);
148+
@@ -1356,6 +1389,7 @@ private:
149+
texWidth,
150+
texHeight,
151+
m_mipLevels,
152+
+ vk::SampleCountFlagBits::e1,
153+
vk::Format::eR8G8B8A8Srgb,
154+
vk::ImageTiling::eOptimal,
155+
vk::ImageUsageFlagBits::eTransferSrc |
156+
@@ -1500,6 +1534,7 @@ private:
157+
m_swapChainExtent.width,
158+
m_swapChainExtent.height,
159+
1,
160+
+ m_msaaSamples,
161+
depthFormat,
162+
vk::ImageTiling::eOptimal,
163+
vk::ImageUsageFlagBits::eDepthStencilAttachment,
164+
@@ -1686,6 +1721,50 @@ private:
165+
endSingleTimeCommands( std::move(commandBuffer) );
166+
}
167+
/////////////////////////////////////////////////////////////////
168+
+
169+
+ /////////////////////////////////////////////////////////////////
170+
+ /// multi sample
171+
+ vk::SampleCountFlagBits getMaxUsableSampleCount() {
172+
+ // vk::PhysicalDeviceProperties
173+
+ auto properties = m_physicalDevice.getProperties();
174+
+
175+
+ vk::SampleCountFlags counts = (
176+
+ properties.limits.framebufferColorSampleCounts &
177+
+ properties.limits.framebufferDepthSampleCounts
178+
+ );
179+
+
180+
+ if(counts & vk::SampleCountFlagBits::e64) return vk::SampleCountFlagBits::e64;
181+
+ if(counts & vk::SampleCountFlagBits::e32) return vk::SampleCountFlagBits::e32;
182+
+ if(counts & vk::SampleCountFlagBits::e16) return vk::SampleCountFlagBits::e16;
183+
+ if(counts & vk::SampleCountFlagBits::e8) return vk::SampleCountFlagBits::e8;
184+
+ if(counts & vk::SampleCountFlagBits::e8) return vk::SampleCountFlagBits::e4;
185+
+ if(counts & vk::SampleCountFlagBits::e8) return vk::SampleCountFlagBits::e2;
186+
+ return vk::SampleCountFlagBits::e1;
187+
+ }
188+
+ void createColorResources() {
189+
+ vk::Format colorFormat = m_swapChainImageFormat;
190+
+
191+
+ createImage(
192+
+ m_swapChainExtent.width,
193+
+ m_swapChainExtent.height,
194+
+ 1,
195+
+ m_msaaSamples,
196+
+ colorFormat,
197+
+ vk::ImageTiling::eOptimal,
198+
+ vk::ImageUsageFlagBits::eTransientAttachment |
199+
+ vk::ImageUsageFlagBits::eColorAttachment,
200+
+ vk::MemoryPropertyFlagBits::eDeviceLocal,
201+
+ m_colorImage,
202+
+ m_colorImageMemory
203+
+ );
204+
+ m_colorImageView = createImageView(
205+
+ m_colorImage,
206+
+ colorFormat,
207+
+ vk::ImageAspectFlagBits::eColor,
208+
+ 1
209+
+ );
210+
+ }
211+
+ /////////////////////////////////////////////////////////////////
212+
};
213+
214+

docs/images/aliasing.png

26.2 KB
Loading

docs/images/antialiasing.png

33.2 KB
Loading

docs/images/multisampling.png

284 KB
Loading
302 KB
Loading
336 KB
Loading

docs/images/sample_shading.png

22.3 KB
Loading

0 commit comments

Comments
 (0)