Skip to content

Commit b4ac538

Browse files
committed
samples
1 parent 2daca79 commit b4ac538

File tree

12 files changed

+133
-68
lines changed

12 files changed

+133
-68
lines changed

src/gpu/abstractions/vulkanImage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <iostream>
77

8-
AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped, uint32_t arrayLayers) {
8+
AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped, uint32_t arrayLayers, VkSampleCountFlagBits samples) {
99
AllocatedImage newImage{};
1010
newImage.device = device;
1111
newImage.imageFormat = format;
@@ -27,7 +27,7 @@ AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat
2727
imageInfo.extent = size;
2828
imageInfo.mipLevels = newImage.mipLevels;
2929
imageInfo.arrayLayers = arrayLayers;
30-
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
30+
imageInfo.samples = samples;
3131
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
3232
imageInfo.usage = usage;
3333
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -87,7 +87,7 @@ AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat
8787
return newImage;
8888
}
8989

90-
AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped) {
90+
AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped, VkSampleCountFlagBits samples) {
9191
AllocatedImage newImage{};
9292
newImage.device = device;
9393
newImage.imageFormat = format;
@@ -109,7 +109,7 @@ AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat forma
109109
imageInfo.extent = size;
110110
imageInfo.mipLevels = newImage.mipLevels;
111111
imageInfo.arrayLayers = 1;
112-
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
112+
imageInfo.samples = samples;
113113
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
114114
imageInfo.usage = usage;
115115
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -170,14 +170,14 @@ AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat forma
170170
}
171171

172172

173-
AllocatedImage createImage(VulkanDevice* device, void* data, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped) {
173+
AllocatedImage createImage(VulkanDevice* device, void* data, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped, VkSampleCountFlagBits samples) {
174174
// upload data to staging upload buffer
175175
size_t dataSize = size.depth * size.height * size.width * 4; // each pixel is 4 bytes (8bit channels RGBA)
176176
AllocatedBuffer uploadBuffer = createBuffer(device, dataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
177177
vmaCopyMemoryToAllocation(device->getAllocator(), data, uploadBuffer.allocation, 0, dataSize);
178178

179179
// create image
180-
AllocatedImage newImage = createImage(device, size, format, usage | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, mipmapped); // not exactly sure why we need transfer src but tutorial says so
180+
AllocatedImage newImage = createImage(device, size, format, usage | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, mipmapped, samples); // not exactly sure why we need transfer src but tutorial says so
181181

182182
// submit copy from staging to real
183183
device->immediateSubmit([&](VkCommandBuffer cmd) {

src/gpu/abstractions/vulkanImage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ struct AllocatedImage {
1818
VulkanDevice* device;
1919
};
2020

21-
AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false, uint32_t arrayLayers = 1);
22-
AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false);
23-
AllocatedImage createImage(VulkanDevice* device, void* data, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false);
21+
AllocatedImage createImageArray(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false, uint32_t arrayLayers = 1, VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT);
22+
AllocatedImage createImage(VulkanDevice* device, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false, VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT);
23+
AllocatedImage createImage(VulkanDevice* device, void* data, VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false, VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT);
2424
void destroyImage(AllocatedImage& image);
2525

2626
bool transitionImageLayout(VkCommandBuffer cmd, AllocatedImage& image, VkImageLayout oldLayout, VkImageLayout newLayout);

src/gpu/abstractions/vulkanPipeline.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const std::vector<VkDynamicState> dynamicStates = {
77

88
void Pipeline::init(VulkanDevice* device, const PipelineInformation& info) {
99
this->device = device;
10-
10+
1111
// shader stages
1212
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
1313
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
@@ -66,7 +66,7 @@ void Pipeline::init(VulkanDevice* device, const PipelineInformation& info) {
6666
VkPipelineMultisampleStateCreateInfo multisampling{};
6767
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
6868
multisampling.sampleShadingEnable = VK_FALSE;
69-
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
69+
multisampling.rasterizationSamples = info.sampleCount;
7070
multisampling.minSampleShading = 1.0f; // unused
7171
multisampling.pSampleMask = nullptr; // unused
7272
multisampling.alphaToCoverageEnable = VK_FALSE; // unused
@@ -100,7 +100,7 @@ void Pipeline::init(VulkanDevice* device, const PipelineInformation& info) {
100100
colorBlending.blendConstants[1] = 0.0f; // unused
101101
colorBlending.blendConstants[2] = 0.0f; // unused
102102
colorBlending.blendConstants[3] = 0.0f; // unused
103-
103+
104104
// pipeline layout
105105
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
106106
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
@@ -123,7 +123,7 @@ void Pipeline::init(VulkanDevice* device, const PipelineInformation& info) {
123123
}
124124
pipelineLayoutInfo.pushConstantRangeCount = static_cast<uint32_t>(pushConstants.size());
125125
pipelineLayoutInfo.pPushConstantRanges = pushConstants.data();
126-
126+
127127
// create pipeline layout
128128
if (vkCreatePipelineLayout(device->getDevice(), &pipelineLayoutInfo, nullptr, &layout) != VK_SUCCESS) {
129129
throw std::runtime_error("failed to create pipeline layout!");

src/gpu/abstractions/vulkanPipeline.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct PipelineInformation {
2020

2121
std::vector<PushConstantDescription> pushConstants;
2222
std::vector<VkDescriptorSetLayout> descriptorSets;
23+
24+
VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_1_BIT;
2325
};
2426

2527
class Pipeline {
@@ -31,13 +33,13 @@ class Pipeline {
3133

3234
inline VkPipeline getHandle() { return handle; }
3335
inline VkPipelineLayout getLayout() { return layout; }
34-
36+
3537
private:
3638
VkPipeline handle;
3739
VkPipelineLayout layout;
3840

3941
std::vector<VkPushConstantRange> pushConstants;
40-
42+
4143
VulkanDevice* device;
4244
};
4345

src/gpu/abstractions/vulkanSwapchain.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
void Swapchain::init(VulkanDevice* device, VkSurfaceKHR surface, std::pair<uint32_t, uint32_t> size) {
44
this->device = device;
5-
5+
66
createSwapchain(surface, size, false);
77

88
// create image semaphores
@@ -40,12 +40,12 @@ void Swapchain::createSwapchain(VkSurfaceKHR surface, std::pair<uint32_t, uint32
4040
else swapchainBuilder.set_desired_present_mode(VK_PRESENT_MODE_IMMEDIATE_KHR);
4141
swapchainBuilder.set_desired_format({VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
4242
if (useOld) swapchainBuilder.set_old_swapchain(swapchain);
43-
43+
4444
auto swapchainRet = swapchainBuilder.build();
4545
if (!swapchainRet)
4646
throwFatalError("Could not create vulkan swapchain. Error: " + swapchainRet.error().message());
4747
if (useOld)
48-
vkb::destroy_swapchain(swapchain);
48+
vkb::destroy_swapchain(swapchain);
4949
swapchain = swapchainRet.value();
5050

5151
// Get image views
@@ -64,19 +64,20 @@ void Swapchain::destroyFramebuffers() {
6464
}
6565

6666
// Create and initialize framebuffers for use with the swapchain
67-
void Swapchain::createFramebuffers(const VkRenderPass renderPass) {
67+
void Swapchain::createFramebuffers(const VkRenderPass renderPass, const AllocatedImage& colorImage) {
6868
framebuffers.resize(swapchain.image_count);
6969

7070
for (size_t i = 0; i < swapchain.image_count; i++) {
71-
const VkImageView attachments[] = {
72-
imageViews[i]
73-
};
71+
std::array<VkImageView, 2> attachments = {
72+
colorImage.imageView, // multisampled color buffer
73+
imageViews[i] // resolve (swapchain) image
74+
};
7475

7576
VkFramebufferCreateInfo framebufferInfo{};
7677
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
7778
framebufferInfo.renderPass = renderPass;
78-
framebufferInfo.attachmentCount = 1;
79-
framebufferInfo.pAttachments = attachments;
79+
framebufferInfo.attachmentCount = attachments.size();
80+
framebufferInfo.pAttachments = attachments.data();
8081
framebufferInfo.width = swapchain.extent.width;
8182
framebufferInfo.height = swapchain.extent.height;
8283
framebufferInfo.layers = 1;

src/gpu/abstractions/vulkanSwapchain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Swapchain {
88
void init(VulkanDevice* device, VkSurfaceKHR surface, std::pair<uint32_t, uint32_t> size);
99
void cleanup();
1010

11-
void createFramebuffers(VkRenderPass renderPass);
11+
void createFramebuffers(VkRenderPass renderPass, const AllocatedImage& colorImage);
1212
void recreate(VkSurfaceKHR surface, std::pair<uint32_t, uint32_t> size);
1313

1414
inline vkb::Swapchain& getSwapchain() { return swapchain; }
@@ -18,7 +18,7 @@ class Swapchain {
1818
private:
1919
void createSwapchain(VkSurfaceKHR surface, std::pair<uint32_t, uint32_t> size, bool useOld);
2020
void destroyFramebuffers();
21-
21+
2222
private:
2323
vkb::Swapchain swapchain;
2424
std::vector<VkFramebuffer> framebuffers;

src/gpu/renderer/rml/rmlRenderer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void RmlRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
2626
rmlPipelineInfo.vertexBindingDescriptions = RmlVertex::getBindingDescriptions();
2727
rmlPipelineInfo.vertexAttributeDescriptions = RmlVertex::getAttributeDescriptions();
2828
rmlPipelineInfo.pushConstants.push_back({VK_SHADER_STAGE_VERTEX_BIT, sizeof(RmlPushConstants)});
29+
rmlPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
2930
untexturedPipeline.init(device, rmlPipelineInfo);
3031
// textured
3132
rmlPipelineInfo.fragShader = rmlFragShaderTextured;

src/gpu/renderer/viewport/elements/elementRenderer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void ElementRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
2222
blockPreviewPipelineInfo.renderPass = renderPass;
2323
blockPreviewPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_VERTEX_BIT, sizeof(BlockPreviewPushConstant) });
2424
blockPreviewPipelineInfo.descriptorSets.push_back(device->getBlockTextureManager().getDescriptorLayout());
25+
blockPreviewPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
2526
blockPreviewPipeline.init(device, blockPreviewPipelineInfo);
2627

2728
destroyShaderModule(device->getDevice(), blockPreviewVertShader);
@@ -37,6 +38,7 @@ void ElementRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
3738
boxSelectionPipelineInfo.renderPass = renderPass;
3839
boxSelectionPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_VERTEX_BIT, offsetof(BoxSelectionPushConstant, state) });
3940
boxSelectionPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(uint32_t) });
41+
boxSelectionPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
4042
boxSelectionPipeline.init(device, boxSelectionPipelineInfo);
4143

4244
destroyShaderModule(device->getDevice(), boxSelectionVertShader);
@@ -51,6 +53,7 @@ void ElementRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
5153
connectionPreviewPipelineInfo.fragShader = connectionPreviewFragShader;
5254
connectionPreviewPipelineInfo.renderPass = renderPass;
5355
connectionPreviewPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_VERTEX_BIT, sizeof(ConnectionPreviewPushConstant) });
56+
connectionPreviewPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
5457
connectionPreviewPipeline.init(device, connectionPreviewPipelineInfo);
5558

5659
destroyShaderModule(device->getDevice(), connectionPreviewVertShader);
@@ -66,6 +69,7 @@ void ElementRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
6669
arrowCirclePipelineInfo.renderPass = renderPass;
6770
arrowCirclePipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_VERTEX_BIT, offsetof(ArrowCirclePushConstant, depth) });
6871
arrowCirclePipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(uint32_t) });
72+
arrowCirclePipelineInfo.sampleCount = device->getMaxUsableSampleCount();
6973
arrowCirclePipeline.init(device, arrowCirclePipelineInfo);
7074

7175
destroyShaderModule(device->getDevice(), arrowCircleVertShader);
@@ -81,6 +85,7 @@ void ElementRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
8185
arrowPipelineInfo.renderPass = renderPass;
8286
arrowPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_VERTEX_BIT, offsetof(ArrowPushConstant, depth) });
8387
arrowPipelineInfo.pushConstants.push_back({ VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(uint32_t) });
88+
arrowPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
8489
arrowPipeline.init(device, arrowPipelineInfo);
8590

8691
destroyShaderModule(device->getDevice(), arrowVertShader);

src/gpu/renderer/viewport/grid/gridRenderer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void GridRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
1818
gridPipelineInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
1919
gridPipelineInfo.pushConstants.push_back({VK_SHADER_STAGE_VERTEX_BIT, fragmentPushOffset});
2020
gridPipelineInfo.pushConstants.push_back({VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(GridPushConstants) - fragmentPushOffset});
21+
gridPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
2122
pipeline.init(device, gridPipelineInfo);
2223

2324
destroyShaderModule(device->getDevice(), gridVertShader);

src/gpu/renderer/viewport/logic/chunking/chunkRenderer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void ChunkRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
3737
blockPipelineInfo.pushConstants.push_back({VK_SHADER_STAGE_VERTEX_BIT, sizeof(ChunkPushConstants)});
3838
blockPipelineInfo.descriptorSets.push_back(stateBufferDescriptorSetLayout);
3939
blockPipelineInfo.descriptorSets.push_back(device->getBlockTextureManager().getDescriptorLayout());
40+
blockPipelineInfo.sampleCount = device->getMaxUsableSampleCount();
4041
blockPipeline.init(device, blockPipelineInfo);
4142

4243
PipelineInformation wirePipelineInfo{};
@@ -47,6 +48,7 @@ void ChunkRenderer::init(VulkanDevice* device, VkRenderPass& renderPass) {
4748
wirePipelineInfo.vertexAttributeDescriptions = WireInstance::getAttributeDescriptions();
4849
wirePipelineInfo.pushConstants.push_back({VK_SHADER_STAGE_VERTEX_BIT, sizeof(ChunkPushConstants)});
4950
wirePipelineInfo.descriptorSets.push_back(stateBufferDescriptorSetLayout);
51+
wirePipelineInfo.sampleCount = device->getMaxUsableSampleCount();
5052
wirePipeline.init(device, wirePipelineInfo);
5153

5254
// destroy shader modules since we won't be recreating pipelines

0 commit comments

Comments
 (0)