Вынес управление VkDescriptorSetLayout и VkPipeline в отдельные классы
This commit is contained in:
@@ -18,36 +18,6 @@
|
||||
|
||||
RENDER_ENGINE_FACTORY_GENERATE(UwURenderEngine)
|
||||
|
||||
VkShaderModule UwURenderEngine::create_shader_module(const std::string& code) const
|
||||
{
|
||||
VkShaderModuleCreateInfo shader_module_info{};
|
||||
shader_module_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
shader_module_info.codeSize = code.size();
|
||||
shader_module_info.pCode = reinterpret_cast<const uint32_t*>(code.c_str());
|
||||
|
||||
VkShaderModule shader_module;
|
||||
VK_CHECK(vkCreateShaderModule(device_.get_native(), &shader_module_info, nullptr, &shader_module))
|
||||
return shader_module;
|
||||
}
|
||||
|
||||
|
||||
void UwURenderEngine::create_description_set_layout()
|
||||
{
|
||||
VkDescriptorSetLayoutBinding ubo_layout_binding;
|
||||
ubo_layout_binding.binding = 0;
|
||||
ubo_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
ubo_layout_binding.descriptorCount = 1;
|
||||
ubo_layout_binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
ubo_layout_binding.pImmutableSamplers = nullptr;
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo ubo_layout_info{};
|
||||
ubo_layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||
ubo_layout_info.bindingCount = 1;
|
||||
ubo_layout_info.pBindings = &ubo_layout_binding;
|
||||
|
||||
VK_CHECK(vkCreateDescriptorSetLayout(device_.get_native(), &ubo_layout_info, nullptr, &ubo_layout_binding_))
|
||||
}
|
||||
|
||||
void UwURenderEngine::create_uniform_buffers()
|
||||
{
|
||||
uniform_buffers_.resize(swapchain_.get_images().size());
|
||||
@@ -83,7 +53,7 @@ void UwURenderEngine::create_descriptor_pool()
|
||||
|
||||
void UwURenderEngine::create_descriptor_sets()
|
||||
{
|
||||
std::vector<VkDescriptorSetLayout> layouts(swapchain_.get_images().size(), ubo_layout_binding_);
|
||||
std::vector<VkDescriptorSetLayout> layouts(swapchain_.get_images().size(), layout_binding_.get_native());
|
||||
VkDescriptorSetAllocateInfo descriptor_set_allocate_info{};
|
||||
descriptor_set_allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
descriptor_set_allocate_info.descriptorPool = descriptor_pool_;
|
||||
@@ -115,148 +85,6 @@ void UwURenderEngine::create_descriptor_sets()
|
||||
}
|
||||
}
|
||||
|
||||
void UwURenderEngine::create_graphics_pipeline()
|
||||
{
|
||||
std::ifstream vertex_shader_file("Shaders/vertex.spv", std::ios::binary);
|
||||
if (!vertex_shader_file.is_open())
|
||||
throw std::runtime_error("Vertex shader not found");
|
||||
std::ifstream fragment_shader_file("Shaders/fragment.spv", std::ios::binary);
|
||||
if (!fragment_shader_file.is_open()) {
|
||||
vertex_shader_file.close();
|
||||
throw std::runtime_error("Fragment shader not found");
|
||||
}
|
||||
|
||||
std::string vertex_shader_code((std::istreambuf_iterator<char>(vertex_shader_file)), std::istreambuf_iterator<char>());
|
||||
std::string fragment_shader_code((std::istreambuf_iterator<char>(fragment_shader_file)), std::istreambuf_iterator<char>());
|
||||
vertex_shader_file.close();
|
||||
fragment_shader_file.close();
|
||||
|
||||
VkShaderModule vertex_shader = create_shader_module(vertex_shader_code);
|
||||
VkShaderModule fragment_shader = create_shader_module(fragment_shader_code);
|
||||
|
||||
VkPipelineShaderStageCreateInfo vertex_shader_stage_info{};
|
||||
vertex_shader_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
vertex_shader_stage_info.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
vertex_shader_stage_info.module = vertex_shader;
|
||||
vertex_shader_stage_info.pName = "main";
|
||||
|
||||
VkPipelineShaderStageCreateInfo fragment_shader_stage_info{};
|
||||
fragment_shader_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
fragment_shader_stage_info.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
fragment_shader_stage_info.module = fragment_shader;
|
||||
fragment_shader_stage_info.pName = "main";
|
||||
|
||||
VkPipelineShaderStageCreateInfo shader_stages[] = {vertex_shader_stage_info, fragment_shader_stage_info};
|
||||
const std::vector<VkDynamicState> dynamic_states = {
|
||||
VK_DYNAMIC_STATE_VIEWPORT,
|
||||
VK_DYNAMIC_STATE_SCISSOR
|
||||
};
|
||||
|
||||
VkPipelineDynamicStateCreateInfo dynamic_state_info{};
|
||||
dynamic_state_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
||||
dynamic_state_info.dynamicStateCount = static_cast<uint32_t>(dynamic_states.size());
|
||||
dynamic_state_info.pDynamicStates = dynamic_states.data();
|
||||
|
||||
VkVertexInputBindingDescription binding_description = ModelManager::Vertex::get_binding_description();
|
||||
std::array<VkVertexInputAttributeDescription, 2> attribute_descriptions = ModelManager::Vertex::get_vertex_attribute_descriptions();
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vertex_input_info{};
|
||||
vertex_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||
vertex_input_info.vertexBindingDescriptionCount = 1;
|
||||
vertex_input_info.pVertexBindingDescriptions = &binding_description;
|
||||
vertex_input_info.vertexAttributeDescriptionCount = static_cast<uint32_t>(attribute_descriptions.size());
|
||||
vertex_input_info.pVertexAttributeDescriptions = attribute_descriptions.data();
|
||||
|
||||
VkPipelineInputAssemblyStateCreateInfo input_assembly_info{};
|
||||
input_assembly_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||
input_assembly_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
input_assembly_info.primitiveRestartEnable = VK_FALSE;
|
||||
|
||||
VkViewport viewport{};
|
||||
viewport.x = 0.0f;
|
||||
viewport.y = 0.0f;
|
||||
viewport.width = static_cast<float>(swapchain_.get_extent().width);
|
||||
viewport.height = static_cast<float>(swapchain_.get_extent().height);
|
||||
viewport.minDepth = 0.0f;
|
||||
viewport.maxDepth = 1.0f;
|
||||
|
||||
VkRect2D scissor{};
|
||||
scissor.offset = {0, 0};
|
||||
scissor.extent = swapchain_.get_extent();
|
||||
|
||||
VkPipelineViewportStateCreateInfo viewport_info{};
|
||||
viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||||
viewport_info.viewportCount = 1;
|
||||
viewport_info.pViewports = &viewport;
|
||||
viewport_info.scissorCount = 1;
|
||||
viewport_info.pScissors = &scissor;
|
||||
|
||||
VkPipelineRasterizationStateCreateInfo rasterization_info{};
|
||||
rasterization_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||
rasterization_info.depthClampEnable = VK_FALSE;
|
||||
rasterization_info.rasterizerDiscardEnable = VK_FALSE;
|
||||
rasterization_info.polygonMode = VK_POLYGON_MODE_FILL;
|
||||
rasterization_info.lineWidth = 1.0f;
|
||||
rasterization_info.cullMode = VK_CULL_MODE_BACK_BIT;
|
||||
rasterization_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
||||
rasterization_info.depthBiasEnable = VK_FALSE;
|
||||
|
||||
VkPipelineMultisampleStateCreateInfo multisample_info{};
|
||||
multisample_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||||
multisample_info.sampleShadingEnable = VK_FALSE;
|
||||
multisample_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
|
||||
VkPipelineColorBlendAttachmentState color_blend_attachment_state{};
|
||||
color_blend_attachment_state.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||
color_blend_attachment_state.blendEnable = VK_TRUE;
|
||||
color_blend_attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
color_blend_attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
color_blend_attachment_state.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
color_blend_attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
color_blend_attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
color_blend_attachment_state.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
|
||||
VkPipelineColorBlendStateCreateInfo color_blend_info{};
|
||||
color_blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
color_blend_info.logicOpEnable = VK_FALSE;
|
||||
color_blend_info.attachmentCount = 1;
|
||||
color_blend_info.pAttachments = &color_blend_attachment_state;
|
||||
color_blend_info.blendConstants[0] = 0.0f;
|
||||
color_blend_info.blendConstants[1] = 0.0f;
|
||||
color_blend_info.blendConstants[2] = 0.0f;
|
||||
color_blend_info.blendConstants[3] = 0.0f;
|
||||
|
||||
VkPipelineLayoutCreateInfo pipeline_layout_info{};
|
||||
pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
pipeline_layout_info.setLayoutCount = 1;
|
||||
pipeline_layout_info.pSetLayouts = &ubo_layout_binding_;
|
||||
|
||||
VK_CHECK(vkCreatePipelineLayout(device_.get_native(), &pipeline_layout_info, nullptr, &pipeline_layout_))
|
||||
|
||||
VkGraphicsPipelineCreateInfo graphics_pipeline_info{};
|
||||
graphics_pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||||
graphics_pipeline_info.stageCount = 2;
|
||||
graphics_pipeline_info.pStages = shader_stages;
|
||||
graphics_pipeline_info.layout = pipeline_layout_;
|
||||
graphics_pipeline_info.pColorBlendState = &color_blend_info;
|
||||
graphics_pipeline_info.pDynamicState = &dynamic_state_info;
|
||||
graphics_pipeline_info.pInputAssemblyState = &input_assembly_info;
|
||||
graphics_pipeline_info.pMultisampleState = &multisample_info;
|
||||
graphics_pipeline_info.pRasterizationState = &rasterization_info;
|
||||
graphics_pipeline_info.pViewportState = &viewport_info;
|
||||
graphics_pipeline_info.pVertexInputState = &vertex_input_info;
|
||||
graphics_pipeline_info.pDepthStencilState = nullptr;
|
||||
graphics_pipeline_info.renderPass = render_pass_.get_native();
|
||||
graphics_pipeline_info.subpass = 0;
|
||||
graphics_pipeline_info.basePipelineHandle = nullptr;
|
||||
graphics_pipeline_info.basePipelineIndex = -1;
|
||||
|
||||
VK_CHECK(vkCreateGraphicsPipelines(device_.get_native(), nullptr, 1, &graphics_pipeline_info, nullptr, &graphics_pipeline_))
|
||||
|
||||
vkDestroyShaderModule(device_.get_native(), vertex_shader, nullptr);
|
||||
vkDestroyShaderModule(device_.get_native(), fragment_shader, nullptr);
|
||||
}
|
||||
|
||||
void UwURenderEngine::create_framebuffers()
|
||||
{
|
||||
framebuffers_.resize(swapchain_.get_images().size());
|
||||
@@ -400,13 +228,13 @@ void UwURenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint
|
||||
render_pass_begin_info.pClearValues = &clear_color;
|
||||
vkCmdBeginRenderPass(command_buffer, &render_pass_begin_info, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline_);
|
||||
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_.get_native());
|
||||
|
||||
VkBuffer vertex_buffers[] = {vertex_buffer_};
|
||||
VkDeviceSize offset[] = {0};
|
||||
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offset);
|
||||
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout_, 0, 1, &descriptor_sets_[current_frame_], 0, nullptr);
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_.get_layout(), 0, 1, &descriptor_sets_[current_frame_], 0, nullptr);
|
||||
|
||||
vkCmdBindIndexBuffer(command_buffer, index_buffer_, 0, VK_INDEX_TYPE_UINT16);
|
||||
|
||||
@@ -511,13 +339,13 @@ surface_(instance_, get_window()),
|
||||
physical_device_(instance_, surface_, deviceExtensions),
|
||||
device_(physical_device_, deviceExtensions),
|
||||
swapchain_(surface_, physical_device_, device_, get_window()),
|
||||
render_pass_(device_, swapchain_)
|
||||
render_pass_(device_, swapchain_),
|
||||
layout_binding_(device_),
|
||||
pipeline_(device_, swapchain_, render_pass_, layout_binding_)
|
||||
{
|
||||
create_description_set_layout();
|
||||
create_uniform_buffers();
|
||||
create_descriptor_pool();
|
||||
create_descriptor_sets();
|
||||
create_graphics_pipeline();
|
||||
create_framebuffers();
|
||||
create_command_pool();
|
||||
allocate_vertex_buffer();
|
||||
@@ -563,12 +391,6 @@ UwURenderEngine::~UwURenderEngine()
|
||||
for (auto& i : framebuffers_)
|
||||
vkDestroyFramebuffer(device_.get_native(), i, nullptr);
|
||||
|
||||
if (graphics_pipeline_ != VK_NULL_HANDLE)
|
||||
vkDestroyPipeline(device_.get_native(), graphics_pipeline_, nullptr);
|
||||
|
||||
if (pipeline_layout_ != VK_NULL_HANDLE)
|
||||
vkDestroyPipelineLayout(device_.get_native(), pipeline_layout_, nullptr);
|
||||
|
||||
for (size_t i = 0; i < uniform_buffers_.size(); ++i)
|
||||
{
|
||||
vkUnmapMemory(device_.get_native(), uniform_buffers_memory_[i]);
|
||||
@@ -580,9 +402,6 @@ UwURenderEngine::~UwURenderEngine()
|
||||
uniform_buffers_mapped_.clear();
|
||||
|
||||
vkDestroyDescriptorPool(device_.get_native(), descriptor_pool_, nullptr);
|
||||
|
||||
if (ubo_layout_binding_ != VK_NULL_HANDLE)
|
||||
vkDestroyDescriptorSetLayout(device_.get_native(), ubo_layout_binding_, nullptr);
|
||||
}
|
||||
|
||||
void UwURenderEngine::start()
|
||||
|
||||
Reference in New Issue
Block a user