22#include < fstream>
33#include < stdexcept>
44
5+ #include " VulkanUtils.hpp"
6+
57namespace reactor {
68
7- std::vector<char > Pipeline::readFile (const std::string& filename) const {
9+ std::vector<char > Pipeline::readFile (const std::string & filename) const {
810 std::ifstream file (filename, std::ios::ate | std::ios::binary);
911 if (!file.is_open ())
1012 throw std::runtime_error (" Failed to open shader file: " + filename);
11- size_t fileSize = (size_t )file.tellg ();
13+ size_t fileSize = (size_t )file.tellg ();
1214 std::vector<char > buffer (fileSize);
1315 file.seekg (0 );
1416 file.read (buffer.data (), fileSize);
1517 return buffer;
1618}
1719
18- vk::ShaderModule Pipeline::createShaderModule (const std::vector<char >& code) {
20+ vk::ShaderModule Pipeline::createShaderModule (const std::vector<char > & code) {
1921 vk::ShaderModuleCreateInfo createInfo{};
2022 createInfo.codeSize = code.size ();
21- createInfo.pCode = reinterpret_cast <const uint32_t *>(code.data ());
23+ createInfo.pCode = reinterpret_cast <const uint32_t *>(code.data ());
2224 return m_device.createShaderModule (createInfo);
2325}
2426
2527Pipeline::Pipeline (vk::Device device, vk::Format colorAttachmentFormat,
26- const std::string& vertShaderPath,
27- const std::string& fragShaderPath,
28- const std::vector<vk::DescriptorSetLayout>& setLayouts, uint32_t samples)
29- : m_device(device)
30- {
28+ const std::string &vertShaderPath, const std::string &fragShaderPath,
29+ const std::vector<vk::DescriptorSetLayout> &setLayouts, uint32_t samples)
30+ : m_device(device) {
3131 // 1. Read shader code from files
3232 auto vertShaderCode = readFile (vertShaderPath);
3333 auto fragShaderCode = readFile (fragShaderPath);
@@ -37,14 +37,14 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
3737 vk::ShaderModule fragShaderModule = createShaderModule (fragShaderCode);
3838
3939 vk::PipelineShaderStageCreateInfo vertStageInfo{};
40- vertStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
40+ vertStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
4141 vertStageInfo.module = vertShaderModule;
42- vertStageInfo.pName = " main" ;
42+ vertStageInfo.pName = " main" ;
4343
4444 vk::PipelineShaderStageCreateInfo fragStageInfo{};
45- fragStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
45+ fragStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
4646 fragStageInfo.module = fragShaderModule;
47- fragStageInfo.pName = " main" ;
47+ fragStageInfo.pName = " main" ;
4848
4949 vk::PipelineShaderStageCreateInfo shaderStages[] = {vertStageInfo, fragStageInfo};
5050
@@ -53,85 +53,78 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
5353
5454 // 4. Input assembly
5555 vk::PipelineInputAssemblyStateCreateInfo inputAssembly{};
56- inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;
56+ inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;
5757 inputAssembly.primitiveRestartEnable = VK_FALSE;
5858
5959 // 5. Viewport and scissor (dynamic for flexibility)
6060 vk::PipelineViewportStateCreateInfo viewportState{};
6161 viewportState.viewportCount = 1 ;
62- viewportState.scissorCount = 1 ;
62+ viewportState.scissorCount = 1 ;
6363
6464 // 6. Rasterizer
6565 vk::PipelineRasterizationStateCreateInfo rasterizer{};
66- rasterizer.depthClampEnable = VK_FALSE;
66+ rasterizer.depthClampEnable = VK_FALSE;
6767 rasterizer.rasterizerDiscardEnable = VK_FALSE;
68- rasterizer.polygonMode = vk::PolygonMode::eFill;
69- rasterizer.lineWidth = 1 .0f ;
70- rasterizer.cullMode = vk::CullModeFlagBits::eBack;
71- rasterizer.frontFace = vk::FrontFace::eClockwise;
72- rasterizer.depthBiasEnable = VK_FALSE;
68+ rasterizer.polygonMode = vk::PolygonMode::eFill;
69+ rasterizer.lineWidth = 1 .0f ;
70+ rasterizer.cullMode = vk::CullModeFlagBits::eBack;
71+ rasterizer.frontFace = vk::FrontFace::eClockwise;
72+ rasterizer.depthBiasEnable = VK_FALSE;
7373
7474 // 7. Multisampling (disabled)
7575 vk::PipelineMultisampleStateCreateInfo multisampling{};
7676 multisampling.sampleShadingEnable = VK_FALSE;
7777
78- if (samples > 1 ) {
79- multisampling.rasterizationSamples = vk::SampleCountFlagBits::e4 ;
80- } else {
81- multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1 ;
82- }
78+ multisampling.rasterizationSamples = utils::mapSampleCountFlag (samples);
8379
8480 // 8. Color blending
8581 vk::PipelineColorBlendAttachmentState colorBlendAttachment{};
86- colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR |
87- vk::ColorComponentFlagBits::eG |
88- vk::ColorComponentFlagBits::eB |
89- vk::ColorComponentFlagBits::eA;
82+ colorBlendAttachment.colorWriteMask =
83+ vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
84+ vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
9085 colorBlendAttachment.blendEnable = VK_FALSE;
9186 vk::PipelineColorBlendStateCreateInfo colorBlending{};
92- colorBlending.logicOpEnable = VK_FALSE;
87+ colorBlending.logicOpEnable = VK_FALSE;
9388 colorBlending.attachmentCount = 1 ;
94- colorBlending.pAttachments = &colorBlendAttachment;
89+ colorBlending.pAttachments = &colorBlendAttachment;
9590
9691 // 9. Pipeline layout
9792 vk::PipelineLayoutCreateInfo pipelineLayoutInfo{};
9893 pipelineLayoutInfo.setLayoutCount = static_cast <uint32_t >(setLayouts.size ());
99- pipelineLayoutInfo.pSetLayouts = setLayouts.data ();
94+ pipelineLayoutInfo.pSetLayouts = setLayouts.data ();
10095
10196 m_pipelineLayout = m_device.createPipelineLayout (pipelineLayoutInfo);
10297
10398 // 10. Dynamic state (viewport and scissor set in command buffer)
104- std::vector<vk::DynamicState> dynamicStates = {
105- vk::DynamicState::eViewport,
106- vk::DynamicState::eScissor
107- };
99+ std::vector<vk::DynamicState> dynamicStates = {vk::DynamicState::eViewport,
100+ vk::DynamicState::eScissor};
108101 vk::PipelineDynamicStateCreateInfo dynamicState{};
109102 dynamicState.dynamicStateCount = static_cast <uint32_t >(dynamicStates.size ());
110- dynamicState.pDynamicStates = dynamicStates.data ();
103+ dynamicState.pDynamicStates = dynamicStates.data ();
111104
112105 // 11. Dynamic rendering
113106 vk::PipelineRenderingCreateInfo renderingInfo{};
114- renderingInfo.colorAttachmentCount = 1 ;
107+ renderingInfo.colorAttachmentCount = 1 ;
115108 renderingInfo.pColorAttachmentFormats = &colorAttachmentFormat;
116- renderingInfo.viewMask = 0 ;
117- renderingInfo.depthAttachmentFormat = vk::Format::eUndefined;
109+ renderingInfo.viewMask = 0 ;
110+ renderingInfo.depthAttachmentFormat = vk::Format::eUndefined;
118111 renderingInfo.stencilAttachmentFormat = vk::Format::eUndefined;
119112
120113 // 11. Create graphics pipeline
121114 vk::GraphicsPipelineCreateInfo pipelineInfo{};
122- pipelineInfo.stageCount = 2 ;
123- pipelineInfo.pStages = shaderStages;
124- pipelineInfo.pVertexInputState = &vertexInputInfo;
115+ pipelineInfo.stageCount = 2 ;
116+ pipelineInfo.pStages = shaderStages;
117+ pipelineInfo.pVertexInputState = &vertexInputInfo;
125118 pipelineInfo.pInputAssemblyState = &inputAssembly;
126- pipelineInfo.pViewportState = &viewportState;
119+ pipelineInfo.pViewportState = &viewportState;
127120 pipelineInfo.pRasterizationState = &rasterizer;
128- pipelineInfo.pMultisampleState = &multisampling;
129- pipelineInfo.pColorBlendState = &colorBlending;
130- pipelineInfo.layout = m_pipelineLayout;
131- pipelineInfo.renderPass = VK_NULL_HANDLE;
132- pipelineInfo.subpass = 0 ;
133- pipelineInfo.pDynamicState = &dynamicState;
134- pipelineInfo.pNext = &renderingInfo;
121+ pipelineInfo.pMultisampleState = &multisampling;
122+ pipelineInfo.pColorBlendState = &colorBlending;
123+ pipelineInfo.layout = m_pipelineLayout;
124+ pipelineInfo.renderPass = VK_NULL_HANDLE;
125+ pipelineInfo.subpass = 0 ;
126+ pipelineInfo.pDynamicState = &dynamicState;
127+ pipelineInfo.pNext = &renderingInfo;
135128
136129 auto pipelineResult = m_device.createGraphicsPipeline ({}, pipelineInfo);
137130 if (pipelineResult.result != vk::Result::eSuccess) {
@@ -146,8 +139,10 @@ Pipeline::Pipeline(vk::Device device, vk::Format colorAttachmentFormat,
146139}
147140
148141Pipeline::~Pipeline () {
149- if (m_pipeline) m_device.destroyPipeline (m_pipeline);
150- if (m_pipelineLayout) m_device.destroyPipelineLayout (m_pipelineLayout);
142+ if (m_pipeline)
143+ m_device.destroyPipeline (m_pipeline);
144+ if (m_pipelineLayout)
145+ m_device.destroyPipelineLayout (m_pipelineLayout);
151146}
152147
153148} // namespace reactor
0 commit comments