Skip to content

Commit 6e8e382

Browse files
committed
Refactor swap chain handling and enhance type safety
Updated swap chain format to use `vk::SurfaceFormatKHR` for better type clarity and consistency. Refined format and color space references throughout the code. Improved `chooseSwapSurfaceFormat` logic for desirable format selection and adjusted pipeline blending constants for correctness. Removed unused shader modules in the graphics pipeline setup.
1 parent 7a28b60 commit 6e8e382

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

attachments/31_compute_shader.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ComputeShaderApplication {
8888

8989
vk::raii::SwapchainKHR swapChain = nullptr;
9090
std::vector<vk::Image> swapChainImages;
91-
vk::Format swapChainImageFormat = vk::Format::eUndefined;
91+
vk::SurfaceFormatKHR swapChainImageFormat;
9292
vk::Extent2D swapChainExtent;
9393
std::vector<vk::raii::ImageView> swapChainImageViews;
9494

@@ -369,7 +369,7 @@ class ComputeShaderApplication {
369369
vk::SwapchainCreateInfoKHR swapChainCreateInfo{
370370
.flags = vk::SwapchainCreateFlagsKHR(),
371371
.surface = surface, .minImageCount = minImageCount,
372-
.imageFormat = swapChainImageFormat, .imageColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear,
372+
.imageFormat = swapChainImageFormat.format, .imageColorSpace = swapChainImageFormat.colorSpace,
373373
.imageExtent = swapChainExtent, .imageArrayLayers =1,
374374
.imageUsage = vk::ImageUsageFlagBits::eColorAttachment, .imageSharingMode = vk::SharingMode::eExclusive,
375375
.preTransform = surfaceCapabilities.currentTransform, .compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque,
@@ -383,7 +383,8 @@ class ComputeShaderApplication {
383383
void createImageViews() {
384384
vk::ImageViewCreateInfo imageViewCreateInfo{
385385
.viewType = vk::ImageViewType::e2D,
386-
.format = swapChainImageFormat,
386+
.format = swapChainImageFormat.format,
387+
.components = {vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity},
387388
.subresourceRange = { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 }
388389
};
389390
for ( auto image : swapChainImages )
@@ -406,8 +407,6 @@ class ComputeShaderApplication {
406407

407408

408409
void createGraphicsPipeline() {
409-
vk::raii::ShaderModule vertShaderModule = createShaderModule(readFile("shaders/vert.spv"));
410-
vk::raii::ShaderModule fragShaderModule = createShaderModule(readFile("shaders/frag.spv"));
411410
vk::raii::ShaderModule shaderModule = createShaderModule(readFile("shaders/slang.spv"));
412411

413412
vk::PipelineShaderStageCreateInfo vertShaderStageInfo{ .stage = vk::ShaderStageFlagBits::eVertex, .module = shaderModule, .pName = "vertMain" };
@@ -441,6 +440,10 @@ class ComputeShaderApplication {
441440
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eZero;
442441

443442
vk::PipelineColorBlendStateCreateInfo colorBlending{ .logicOpEnable = vk::False, .logicOp = vk::LogicOp::eCopy, .attachmentCount = 1, .pAttachments = &colorBlendAttachment };
443+
colorBlending.blendConstants[0] = 0.0f;
444+
colorBlending.blendConstants[1] = 0.0f;
445+
colorBlending.blendConstants[2] = 0.0f;
446+
colorBlending.blendConstants[3] = 0.0f;
444447

445448
std::vector dynamicStates = {
446449
vk::DynamicState::eViewport,
@@ -454,7 +457,7 @@ class ComputeShaderApplication {
454457
pipelineLayoutInfo.pushConstantRangeCount = 0;
455458
pipelineLayout = vk::raii::PipelineLayout( device, pipelineLayoutInfo );
456459

457-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
460+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat.format };
458461
vk::GraphicsPipelineCreateInfo pipelineInfo{.pNext = &pipelineRenderingCreateInfo};
459462
pipelineInfo.stageCount = 2;
460463
pipelineInfo.pStages = shaderStages;
@@ -872,8 +875,14 @@ class ComputeShaderApplication {
872875
return shaderModule;
873876
}
874877

875-
static vk::Format chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats) {
876-
return (availableFormats[0].format == vk::Format::eUndefined) ? vk::Format::eB8G8R8A8Unorm : availableFormats[0].format;
878+
static vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats) {
879+
for (const auto& availableFormat : availableFormats) {
880+
if (availableFormat.format == vk::Format::eB8G8R8A8Srgb && availableFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
881+
return availableFormat;
882+
}
883+
}
884+
885+
return availableFormats[0];
877886
}
878887

879888
static vk::PresentModeKHR chooseSwapPresentMode(const std::vector<vk::PresentModeKHR>& availablePresentModes) {

0 commit comments

Comments
 (0)