Skip to content

Commit 6112adc

Browse files
committed
Multisampling
1 parent a628ea9 commit 6112adc

5 files changed

Lines changed: 34 additions & 13 deletions

File tree

src/main.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ VkSurfaceKHR surface;
2020
VulkanSwapchain swapchain;
2121
VkRenderPass renderPass;
2222
std::vector<VulkanImage> depthBuffers;
23+
std::vector<VulkanImage> colorBuffers;
2324
std::vector<VkFramebuffer> framebuffers;
2425
VkCommandPool commandPools[FRAMES_IN_FLIGHT];
2526
VkCommandBuffer commandBuffers[FRAMES_IN_FLIGHT];
@@ -84,19 +85,26 @@ void recreateRenderPass() {
8485
for(uint32_t i = 0; i < depthBuffers.size(); ++i) {
8586
destroyImage(context, &depthBuffers[i]);
8687
}
88+
for(uint32_t i = 0; i < colorBuffers.size(); ++i) {
89+
destroyImage(context, &colorBuffers[i]);
90+
}
8791
destroyRenderpass(context, renderPass);
8892
}
8993
framebuffers.clear();
9094
depthBuffers.clear();
95+
colorBuffers.clear();
9196

92-
renderPass = createRenderPass(context, swapchain.format);
97+
renderPass = createRenderPass(context, swapchain.format, VK_SAMPLE_COUNT_4_BIT);
9398
framebuffers.resize(swapchain.images.size());
9499
depthBuffers.resize(swapchain.images.size());
100+
colorBuffers.resize(swapchain.images.size());
95101
for (uint32_t i = 0; i < swapchain.images.size(); ++i) {
96-
createImage(context, &depthBuffers.data()[i], swapchain.width, swapchain.height, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
102+
createImage(context, &depthBuffers.data()[i], swapchain.width, swapchain.height, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
103+
createImage(context, &colorBuffers.data()[i], swapchain.width, swapchain.height, swapchain.format, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SAMPLE_COUNT_4_BIT);
97104
VkImageView attachments[] = {
98-
swapchain.imageViews[i],
105+
colorBuffers[i].view,
99106
depthBuffers[i].view,
107+
swapchain.imageViews[i],
100108
};
101109
VkFramebufferCreateInfo createInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
102110
createInfo.renderPass = renderPass;
@@ -536,6 +544,10 @@ void shutdownApplication() {
536544
for(uint32_t i = 0; i < depthBuffers.size(); ++i) {
537545
destroyImage(context, &depthBuffers[i]);
538546
}
547+
for(uint32_t i = 0; i < colorBuffers.size(); ++i) {
548+
destroyImage(context, &colorBuffers[i]);
549+
}
550+
colorBuffers.clear();
539551
depthBuffers.clear();
540552
destroyRenderpass(context, renderPass);
541553
destroySwapchain(context, &swapchain);

src/vulkan_base/vulkan_base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ void exitVulkan(VulkanContext* context);
6161
VulkanSwapchain createSwapchain(VulkanContext* context, VkSurfaceKHR surface, VkImageUsageFlags usage, VulkanSwapchain* oldSwapchain = 0);
6262
void destroySwapchain(VulkanContext* context, VulkanSwapchain* swapchain);
6363

64-
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format);
64+
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format, VkSampleCountFlagBits sampleCount);
6565
void destroyRenderpass(VulkanContext* context, VkRenderPass renderPass);
6666

6767
void createBuffer(VulkanContext* context, VulkanBuffer* buffer, uint64_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memoryProperties);
6868
void uploadDataToBuffer(VulkanContext* context, VulkanBuffer* buffer, void* data, size_t size);
6969
void destroyBuffer(VulkanContext* context, VulkanBuffer* buffer);
7070

71-
void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage);
71+
void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage, VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_1_BIT);
7272
void uploadDataToImage(VulkanContext* context, VulkanImage* image, void* data, size_t size, uint32_t width, uint32_t height, VkImageLayout finalLayout, VkAccessFlags dstAccessMask);
7373
void destroyImage(VulkanContext* context, VulkanImage* image);
7474

src/vulkan_base/vulkan_pipeline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFi
6363
rasterizationState.lineWidth = 1.0f;
6464

6565
VkPipelineMultisampleStateCreateInfo multisampleState = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
66-
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
66+
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_4_BIT;
6767

6868
VkPipelineDepthStencilStateCreateInfo depthStencilState = {VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
6969
depthStencilState.depthTestEnable = VK_TRUE;

src/vulkan_base/vulkan_renderpass.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
#include "vulkan_base.h"
22

3-
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format) {
3+
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format, VkSampleCountFlagBits sampleCount) {
44
VkRenderPass renderPass;
55

6-
VkAttachmentDescription attachmentDescriptions[2];
6+
VkAttachmentDescription attachmentDescriptions[3];
77
attachmentDescriptions[0] = {};
88
attachmentDescriptions[0].format = format;
9-
attachmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
9+
attachmentDescriptions[0].samples = sampleCount;
1010
attachmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1111
attachmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1212
attachmentDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
13-
attachmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
13+
attachmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1414
attachmentDescriptions[1] = {};
1515
attachmentDescriptions[1].format = VK_FORMAT_D32_SFLOAT;
16-
attachmentDescriptions[1].samples = VK_SAMPLE_COUNT_1_BIT;
16+
attachmentDescriptions[1].samples = sampleCount;
1717
attachmentDescriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
1818
attachmentDescriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1919
attachmentDescriptions[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2020
attachmentDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
21+
attachmentDescriptions[2] = {};
22+
attachmentDescriptions[2].format = format;
23+
attachmentDescriptions[2].samples = VK_SAMPLE_COUNT_1_BIT;
24+
attachmentDescriptions[2].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
25+
attachmentDescriptions[2].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
26+
attachmentDescriptions[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
27+
attachmentDescriptions[2].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
2128

2229
VkAttachmentReference attachmentReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
2330
VkAttachmentReference depthStencilReference = {1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
31+
VkAttachmentReference resolveTargetReference = {2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
2432

2533
VkSubpassDescription subpass = {};
2634
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
2735
subpass.colorAttachmentCount = 1;
2836
subpass.pColorAttachments = &attachmentReference;
2937
subpass.pDepthStencilAttachment = &depthStencilReference;
38+
subpass.pResolveAttachments = &resolveTargetReference;
3039

3140
VkRenderPassCreateInfo createInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO };
3241
createInfo.attachmentCount = ARRAY_COUNT(attachmentDescriptions);

src/vulkan_base/vulkan_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void destroyBuffer(VulkanContext* context, VulkanBuffer* buffer) {
9999
VK(vkFreeMemory(context->device, buffer->memory, 0));
100100
}
101101

102-
void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage) {
102+
void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage, VkSampleCountFlagBits sampleCount) {
103103
{
104104
VkImageCreateInfo createInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
105105
createInfo.imageType = VK_IMAGE_TYPE_2D;
@@ -112,7 +112,7 @@ void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uin
112112
createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
113113
createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
114114
createInfo.usage = usage;
115-
createInfo.samples = VK_SAMPLE_COUNT_1_BIT;
115+
createInfo.samples = sampleCount;
116116
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
117117
VKA(vkCreateImage(context->device, &createInfo, 0, &image->image));
118118
}

0 commit comments

Comments
 (0)