From adf45c1b554d67c140ec30121adfc8ac15e1ab18 Mon Sep 17 00:00:00 2001 From: Xiang Silian Date: Fri, 19 Sep 2025 11:36:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5face=E7=82=B9mvp=E5=92=8C?= =?UTF-8?q?=E7=BA=B9=E7=90=86=EF=BC=8C=E9=87=8D=E6=9E=84=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=8C=E8=BF=98=E6=9C=AA=E8=87=AA=E6=B5=8B=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/texture_loading/texture_loading.cpp | 720 +++++++++--------- samples/api/texture_loading/texture_loading.h | 24 +- 2 files changed, 362 insertions(+), 382 deletions(-) diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index bf56274..40b6413 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -668,61 +668,8 @@ void TextureLoading::setup_descriptor_pool() VK_CHECK(vkCreateDescriptorPool(get_device().get_handle(), &descriptor_pool_create_info, nullptr, &descriptor_pool)); } -void TextureLoading::setup_descriptor_set_layout_bg() -{ - std::vector set_layout_bindings = - { - // Binding 1 : Fragment shader image sampler - vkb::initializers::descriptor_set_layout_binding( - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT, - 0) }; - VkDescriptorSetLayoutCreateInfo descriptor_layout = - vkb::initializers::descriptor_set_layout_create_info( - set_layout_bindings.data(), - static_cast(set_layout_bindings.size())); - VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &descriptor_layout, nullptr, &descriptor_set_layout_bg)); - - VkPipelineLayoutCreateInfo pipeline_layout_create_info = - vkb::initializers::pipeline_layout_create_info( - &descriptor_set_layout_bg, - 1); - - VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_bg)); -} - -void TextureLoading::setup_descriptor_set_layout_point() -{ - std::vector set_layout_bindings = - { - // Binding 0 : Vertex shader uniform buffer - vkb::initializers::descriptor_set_layout_binding( - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - VK_SHADER_STAGE_VERTEX_BIT, - 0), - // Binding 1 : Fragment shader image sampler - vkb::initializers::descriptor_set_layout_binding( - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT, - 1) - }; - - VkDescriptorSetLayoutCreateInfo descriptor_layout = - vkb::initializers::descriptor_set_layout_create_info( - set_layout_bindings.data(), - static_cast(set_layout_bindings.size())); - - VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &descriptor_layout, nullptr, &descriptor_set_layout_point)); - - VkPipelineLayoutCreateInfo pipeline_layout_create_info = - vkb::initializers::pipeline_layout_create_info( - &descriptor_set_layout_point, - 1); - - VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_point)); -} void TextureLoading::setup_descriptor_set_layout() { @@ -733,11 +680,11 @@ void TextureLoading::setup_descriptor_set_layout() VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), - // Binding 1 : Fragment shader image sampler - vkb::initializers::descriptor_set_layout_binding( - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - VK_SHADER_STAGE_FRAGMENT_BIT, - 1) }; + // Binding 1 : Fragment shader image sampler + vkb::initializers::descriptor_set_layout_binding( + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + VK_SHADER_STAGE_FRAGMENT_BIT, + 1) }; VkDescriptorSetLayoutCreateInfo descriptor_layout = vkb::initializers::descriptor_set_layout_create_info( @@ -780,18 +727,104 @@ void TextureLoading::setup_descriptor_set() VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &buffer_descriptor), - // Binding 1 : Fragment shader texture sampler - // Fragment shader: layout (binding = 1) uniform sampler2D samplerColor; - vkb::initializers::write_descriptor_set( - descriptor_set, - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split) - 1, // Shader binding point 1 - &image_descriptor) // Pointer to the descriptor image for our texture + // Binding 1 : Fragment shader texture sampler + // Fragment shader: layout (binding = 1) uniform sampler2D samplerColor; + vkb::initializers::write_descriptor_set( + descriptor_set, + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split) + 1, // Shader binding point 1 + &image_descriptor) // Pointer to the descriptor image for our texture }; vkUpdateDescriptorSets(get_device().get_handle(), static_cast(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL); } +void TextureLoading::setup_descriptor_set_layout_bg() +{ + std::vector set_layout_bindings = + { + // Binding 1 : Fragment shader image sampler + vkb::initializers::descriptor_set_layout_binding( + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + VK_SHADER_STAGE_FRAGMENT_BIT, + 0) }; + + VkDescriptorSetLayoutCreateInfo descriptor_layout = + vkb::initializers::descriptor_set_layout_create_info( + set_layout_bindings.data(), + static_cast(set_layout_bindings.size())); + + VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &descriptor_layout, nullptr, &descriptor_set_layout_bg)); + + VkPipelineLayoutCreateInfo pipeline_layout_create_info = + vkb::initializers::pipeline_layout_create_info( + &descriptor_set_layout_bg, + 1); + + VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_bg)); +} + +void TextureLoading::setup_descriptor_set_bg() +{ + VkDescriptorSetAllocateInfo alloc_info = + vkb::initializers::descriptor_set_allocate_info( + descriptor_pool, + &descriptor_set_layout_bg, + 1); + + VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &descriptor_set_bg)); + + + + VkDescriptorImageInfo image_descriptor; + image_descriptor.imageView = cam_text.view; + image_descriptor.sampler = cam_text.sampler; + image_descriptor.imageLayout = cam_text.image_layout; + + std::vector write_descriptor_sets = + { + vkb::initializers::write_descriptor_set( + descriptor_set_bg, + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + 0, + &image_descriptor) + }; + + vkUpdateDescriptorSets(get_device().get_handle(), static_cast(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL); +} + + +void TextureLoading::setup_descriptor_set_layout_point() +{ + std::vector set_layout_bindings = + { + // Binding 0 : Vertex shader uniform buffer + vkb::initializers::descriptor_set_layout_binding( + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + VK_SHADER_STAGE_VERTEX_BIT, + 0), + // Binding 1 : Fragment shader image sampler + vkb::initializers::descriptor_set_layout_binding( + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + VK_SHADER_STAGE_FRAGMENT_BIT, + 1) + }; + + VkDescriptorSetLayoutCreateInfo descriptor_layout = + vkb::initializers::descriptor_set_layout_create_info( + set_layout_bindings.data(), + static_cast(set_layout_bindings.size())); + + VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &descriptor_layout, nullptr, &descriptor_set_layout_point)); + + VkPipelineLayoutCreateInfo pipeline_layout_create_info = + vkb::initializers::pipeline_layout_create_info( + &descriptor_set_layout_point, + 1); + + VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_point)); +} + void TextureLoading::setup_descriptor_set_point() { @@ -819,122 +852,71 @@ void TextureLoading::setup_descriptor_set_point() VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &buffer_descriptor), - // Binding 1 : Fragment shader texture sampler - // Fragment shader: layout (binding = 1) uniform sampler2D samplerColor; - vkb::initializers::write_descriptor_set( - descriptor_set_point, - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split) - 1, // Shader binding point 1 - &image_descriptor) // Pointer to the descriptor image for our texture + // Binding 1 : Fragment shader texture sampler + // Fragment shader: layout (binding = 1) uniform sampler2D samplerColor; + vkb::initializers::write_descriptor_set( + descriptor_set_point, + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, // The descriptor set will use a combined image sampler (sampler and image could be split) + 1, // Shader binding point 1 + &image_descriptor) // Pointer to the descriptor image for our texture }; vkUpdateDescriptorSets(get_device().get_handle(), static_cast(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL); } -void TextureLoading::setup_descriptor_set_bg() +// Prepare and initialize uniform buffer containing shader uniforms +void TextureLoading::prepare_uniform_buffers() { - VkDescriptorSetAllocateInfo alloc_info = - vkb::initializers::descriptor_set_allocate_info( - descriptor_pool, - &descriptor_set_layout_bg, - 1); + // Vertex shader uniform buffer block + uniform_buffer_vs = std::make_unique(get_device(), + sizeof(ubo_vs), + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VMA_MEMORY_USAGE_CPU_TO_GPU); - VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &descriptor_set_bg)); - - - - VkDescriptorImageInfo image_descriptor; - image_descriptor.imageView = cam_text.view; - image_descriptor.sampler = cam_text.sampler; - image_descriptor.imageLayout = cam_text.image_layout; - - std::vector write_descriptor_sets = - { - vkb::initializers::write_descriptor_set( - descriptor_set_bg, - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - 0, - &image_descriptor) - }; - - vkUpdateDescriptorSets(get_device().get_handle(), static_cast(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL); + update_uniform_buffers(); } -void TextureLoading::prepare_pipeline_bg() +void TextureLoading::update_uniform_buffers() { - VkPipelineInputAssemblyStateCreateInfo input_assembly_state = - vkb::initializers::pipeline_input_assembly_state_create_info( - VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, - 0, - VK_FALSE); + // Vertex shader + ubo_vs.projection = glm::perspective(glm::radians(60.0f), static_cast(width) / static_cast(height), 0.001f, 256.0f); + glm::mat4 view_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - VkPipelineRasterizationStateCreateInfo rasterization_state = - vkb::initializers::pipeline_rasterization_state_create_info( - VK_POLYGON_MODE_FILL, - VK_CULL_MODE_NONE, - VK_FRONT_FACE_COUNTER_CLOCKWISE, - 0); + ubo_vs.model = view_matrix * glm::translate(glm::mat4(1.0f), camera_pos); + ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); + ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); + ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - VkPipelineColorBlendAttachmentState blend_attachment_state = - vkb::initializers::pipeline_color_blend_attachment_state( - 0xf, - VK_FALSE); + ubo_vs.view_pos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f); - VkPipelineColorBlendStateCreateInfo color_blend_state = - vkb::initializers::pipeline_color_blend_state_create_info( - 1, - &blend_attachment_state); - - VkPipelineDepthStencilStateCreateInfo depth_stencil_state = - vkb::initializers::pipeline_depth_stencil_state_create_info( - VK_FALSE, - VK_FALSE, - VK_COMPARE_OP_GREATER); - - VkPipelineViewportStateCreateInfo viewport_state = - vkb::initializers::pipeline_viewport_state_create_info(1, 1, 0); - - VkPipelineMultisampleStateCreateInfo multisample_state = - vkb::initializers::pipeline_multisample_state_create_info( - VK_SAMPLE_COUNT_1_BIT, - 0); - - std::vector dynamic_state_enables = { - VK_DYNAMIC_STATE_VIEWPORT, - VK_DYNAMIC_STATE_SCISSOR }; - - VkPipelineDynamicStateCreateInfo dynamic_state = - vkb::initializers::pipeline_dynamic_state_create_info( - dynamic_state_enables.data(), - static_cast(dynamic_state_enables.size()), - 0); - - std::array shader_stages; - shader_stages[0] = load_shader("texture_loading", "bg.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shader_stages[1] = load_shader("texture_loading", "bg.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); - - VkPipelineVertexInputStateCreateInfo vertex_input_state_bg = vkb::initializers::pipeline_vertex_input_state_create_info(); - - VkGraphicsPipelineCreateInfo pipeline_create_info = - vkb::initializers::pipeline_create_info( - pipeline_layout_bg, - render_pass, - 0); - - pipeline_create_info.pVertexInputState = &vertex_input_state_bg; - pipeline_create_info.pInputAssemblyState = &input_assembly_state; - pipeline_create_info.pRasterizationState = &rasterization_state; - pipeline_create_info.pColorBlendState = &color_blend_state; - pipeline_create_info.pMultisampleState = &multisample_state; - pipeline_create_info.pViewportState = &viewport_state; - pipeline_create_info.pDepthStencilState = &depth_stencil_state; - pipeline_create_info.pDynamicState = &dynamic_state; - pipeline_create_info.stageCount = static_cast(shader_stages.size()); - pipeline_create_info.pStages = shader_stages.data(); - - VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1, &pipeline_create_info, nullptr, &pipelines.background)); + uniform_buffer_vs->convert_and_update(ubo_vs); } + +void TextureLoading::prepare_uniform_buffers_point() +{ + uniform_buffer_vs_point = std::make_unique(get_device(), + sizeof(ubo_vs_point), + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VMA_MEMORY_USAGE_CPU_TO_GPU); + + update_uniform_buffers_point(); +} + +void TextureLoading::update_uniform_buffers_point() +{ + ubo_vs_point.projection = glm::perspective(glm::radians(60.0f), static_cast(width) / static_cast(height), 0.001f, 256.0f); + glm::mat4 view_matrix = glm::translate(glm::mat4(1.0f), camear_pos_point); + + ubo_vs_point.model = view_matrix * glm::translate(glm::mat4(1.0f), face_pos_point); + ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); + ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); + ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + uniform_buffer_vs_point->convert_and_update(ubo_vs_point); +} + + void TextureLoading::prepare_pipelines() { VkPipelineInputAssemblyStateCreateInfo input_assembly_state = @@ -1026,56 +1008,173 @@ void TextureLoading::prepare_pipelines() VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1, &pipeline_create_info, nullptr, &pipelines.solid)); } -// Prepare and initialize uniform buffer containing shader uniforms -void TextureLoading::prepare_uniform_buffers() +void TextureLoading::prepare_pipeline_bg() { - // Vertex shader uniform buffer block - uniform_buffer_vs = std::make_unique(get_device(), - sizeof(ubo_vs), - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); + VkPipelineInputAssemblyStateCreateInfo input_assembly_state = + vkb::initializers::pipeline_input_assembly_state_create_info( + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, + 0, + VK_FALSE); - update_uniform_buffers(); + VkPipelineRasterizationStateCreateInfo rasterization_state = + vkb::initializers::pipeline_rasterization_state_create_info( + VK_POLYGON_MODE_FILL, + VK_CULL_MODE_NONE, + VK_FRONT_FACE_COUNTER_CLOCKWISE, + 0); + + VkPipelineColorBlendAttachmentState blend_attachment_state = + vkb::initializers::pipeline_color_blend_attachment_state( + 0xf, + VK_FALSE); + + VkPipelineColorBlendStateCreateInfo color_blend_state = + vkb::initializers::pipeline_color_blend_state_create_info( + 1, + &blend_attachment_state); + + VkPipelineDepthStencilStateCreateInfo depth_stencil_state = + vkb::initializers::pipeline_depth_stencil_state_create_info( + VK_FALSE, + VK_FALSE, + VK_COMPARE_OP_GREATER); + + VkPipelineViewportStateCreateInfo viewport_state = + vkb::initializers::pipeline_viewport_state_create_info(1, 1, 0); + + VkPipelineMultisampleStateCreateInfo multisample_state = + vkb::initializers::pipeline_multisample_state_create_info( + VK_SAMPLE_COUNT_1_BIT, + 0); + + std::vector dynamic_state_enables = { + VK_DYNAMIC_STATE_VIEWPORT, + VK_DYNAMIC_STATE_SCISSOR }; + + VkPipelineDynamicStateCreateInfo dynamic_state = + vkb::initializers::pipeline_dynamic_state_create_info( + dynamic_state_enables.data(), + static_cast(dynamic_state_enables.size()), + 0); + + std::array shader_stages; + shader_stages[0] = load_shader("texture_loading", "bg.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shader_stages[1] = load_shader("texture_loading", "bg.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + + VkPipelineVertexInputStateCreateInfo vertex_input_state_bg = vkb::initializers::pipeline_vertex_input_state_create_info(); + + VkGraphicsPipelineCreateInfo pipeline_create_info = + vkb::initializers::pipeline_create_info( + pipeline_layout_bg, + render_pass, + 0); + + pipeline_create_info.pVertexInputState = &vertex_input_state_bg; + pipeline_create_info.pInputAssemblyState = &input_assembly_state; + pipeline_create_info.pRasterizationState = &rasterization_state; + pipeline_create_info.pColorBlendState = &color_blend_state; + pipeline_create_info.pMultisampleState = &multisample_state; + pipeline_create_info.pViewportState = &viewport_state; + pipeline_create_info.pDepthStencilState = &depth_stencil_state; + pipeline_create_info.pDynamicState = &dynamic_state; + pipeline_create_info.stageCount = static_cast(shader_stages.size()); + pipeline_create_info.pStages = shader_stages.data(); + + VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1, &pipeline_create_info, nullptr, &pipelines.background)); } -void TextureLoading::update_uniform_buffers() + +void TextureLoading::prepare_pipeline_point() { - // Vertex shader - ubo_vs.projection = glm::perspective(glm::radians(60.0f), static_cast(width) / static_cast(height), 0.001f, 256.0f); - glm::mat4 view_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - ubo_vs.model = view_matrix * glm::translate(glm::mat4(1.0f), camera_pos); - ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + // 顶点输入绑定描述 (告诉 Vulkan 顶点数据的格式) + VkVertexInputBindingDescription vertex_input_binding_description{}; + vertex_input_binding_description.binding = 0; // 绑定点 + vertex_input_binding_description.stride = sizeof(PointVertex); // 每个顶点的字节大小 + vertex_input_binding_description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - ubo_vs.view_pos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f); + // 顶点输入属性描述 (告诉 Vulkan 每个属性在顶点结构中的位置) + std::array vertex_input_attributes = { + VkVertexInputAttributeDescription{0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(PointVertex, x)}, // 位置 + VkVertexInputAttributeDescription{1, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(PointVertex, r)} // 颜色 + }; - uniform_buffer_vs->convert_and_update(ubo_vs); + VkPipelineVertexInputStateCreateInfo vertex_input_state{ VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO }; + vertex_input_state.vertexBindingDescriptionCount = 1; + vertex_input_state.pVertexBindingDescriptions = &vertex_input_binding_description; + vertex_input_state.vertexAttributeDescriptionCount = static_cast(vertex_input_attributes.size()); + vertex_input_state.pVertexAttributeDescriptions = vertex_input_attributes.data(); + + // 输入装配 (绘制点列表) + VkPipelineInputAssemblyStateCreateInfo input_assembly_state{ VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO }; + input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; // 关键:绘制点 + input_assembly_state.primitiveRestartEnable = VK_FALSE; + + // 光栅化 + VkPipelineRasterizationStateCreateInfo rasterization_state{ VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO }; + rasterization_state.polygonMode = VK_POLYGON_MODE_FILL; + rasterization_state.cullMode = VK_CULL_MODE_NONE; // 通常不对点进行剔除 + rasterization_state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + rasterization_state.lineWidth = 1.0f; // 可以通过 VkPhysicalDeviceFeatures::wideLines 扩展来支持更宽的线 + + // 视口和裁剪 + VkPipelineViewportStateCreateInfo viewport_state{ VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO }; + viewport_state.viewportCount = 1; + viewport_state.scissorCount = 1; + + + // 多重采样 + VkPipelineMultisampleStateCreateInfo multisample_state{ VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO }; + multisample_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + + // 深度和模板测试 (通常对点云启用深度测试) + VkPipelineDepthStencilStateCreateInfo depth_stencil_state{ VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO }; + depth_stencil_state.depthTestEnable = VK_FALSE; + depth_stencil_state.depthWriteEnable = VK_FALSE; + depth_stencil_state.depthCompareOp = VK_COMPARE_OP_ALWAYS; // 或 VK_COMPARE_OP_LESS + depth_stencil_state.depthBoundsTestEnable = VK_FALSE; + depth_stencil_state.stencilTestEnable = VK_FALSE; + + // 颜色混合 (点通常不需要混合) + VkPipelineColorBlendAttachmentState blend_attachment_state{}; + blend_attachment_state.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; + blend_attachment_state.blendEnable = VK_FALSE; + + VkPipelineColorBlendStateCreateInfo color_blend_state{ VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO }; + color_blend_state.attachmentCount = 1; + color_blend_state.pAttachments = &blend_attachment_state; + + // 动态状态 (视口和裁剪矩形将在命令缓冲区中设置) + std::vector dynamic_state_enables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; + VkPipelineDynamicStateCreateInfo dynamic_state{ VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO }; + dynamic_state.dynamicStateCount = static_cast(dynamic_state_enables.size()); + dynamic_state.pDynamicStates = dynamic_state_enables.data(); + + // 加载着色器 + std::array shader_stages; + shader_stages[0] = load_shader("texture_loading", "pointcloud.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shader_stages[1] = load_shader("texture_loading", "pointcloud.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + + // 创建图形管线 + VkGraphicsPipelineCreateInfo pipeline_create_info{ VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO }; + pipeline_create_info.stageCount = static_cast(shader_stages.size()); + pipeline_create_info.pStages = shader_stages.data(); + pipeline_create_info.pVertexInputState = &vertex_input_state; + pipeline_create_info.pInputAssemblyState = &input_assembly_state; + pipeline_create_info.pViewportState = &viewport_state; + pipeline_create_info.pRasterizationState = &rasterization_state; + pipeline_create_info.pMultisampleState = &multisample_state; + pipeline_create_info.pDepthStencilState = &depth_stencil_state; + pipeline_create_info.pColorBlendState = &color_blend_state; + pipeline_create_info.pDynamicState = &dynamic_state; + pipeline_create_info.layout = pipeline_layout_point; + pipeline_create_info.renderPass = render_pass; // 使用主渲染通道 + pipeline_create_info.subpass = 0; // 主子通道 + + VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1, &pipeline_create_info, nullptr, &pipelines.point)); } -void TextureLoading::prepare_uniform_buffers_point() -{ - uniform_buffer_vs_point = std::make_unique(get_device(), - sizeof(ubo_vs_point), - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); - update_uniform_buffers_point(); -} - -void TextureLoading::update_uniform_buffers_point() -{ - ubo_vs_point.projection = glm::perspective(glm::radians(60.0f), static_cast(width) / static_cast(height), 0.001f, 256.0f); - glm::mat4 view_matrix = glm::translate(glm::mat4(1.0f), camear_pos_point); - - ubo_vs_point.model = view_matrix * glm::translate(glm::mat4(1.0f), pos_point); - ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - ubo_vs_point.model = glm::rotate(ubo_vs_point.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - uniform_buffer_vs_point->convert_and_update(ubo_vs_point); -} bool TextureLoading::prepare(const vkb::ApplicationOptions& options) { @@ -1102,19 +1201,19 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) prepare_uniform_buffers_point(); setup_descriptor_set_layout(); + setup_descriptor_set(); + setup_descriptor_set_layout_bg(); + setup_descriptor_set_bg(); + setup_descriptor_set_layout_point(); + setup_descriptor_set_point(); prepare_pipelines(); prepare_pipeline_bg(); prepare_pipeline_point(); setup_descriptor_pool(); - - setup_descriptor_set(); - setup_descriptor_set_bg(); - setup_descriptor_set_point(); - prepared = true; //start(); @@ -1498,167 +1597,42 @@ void TextureLoading::transitionImageLayout(VkCommandBuffer commandBuffer, VkImag 0, nullptr, 0, nullptr, 1, &barrier); } -void TextureLoading::setup_point_descriptor_set_layout() -{ - // --- 修改:为点云 UBO 创建描述符集布局绑定,指向 binding 0 --- - VkDescriptorSetLayoutBinding ubo_layout_binding{}; - ubo_layout_binding.binding = 0; // 与着色器中的 layout(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 layout_info{ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO }; - layout_info.bindingCount = 1; - layout_info.pBindings = &ubo_layout_binding; // 指向我们的 UBO 绑定 - - VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &layout_info, nullptr, &point_descriptor_set_layout)); - // --- 修改结束 --- -} - -void TextureLoading::setup_point_descriptor_set() -{ - // --- 修改:分配点云描述符集 --- - VkDescriptorSetAllocateInfo alloc_info{ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO }; - alloc_info.descriptorPool = descriptor_pool; // 使用您已有的描述符池 - alloc_info.descriptorSetCount = 1; - alloc_info.pSetLayouts = &point_descriptor_set_layout; - - VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &point_descriptor_set)); - // --- 修改结束 --- - - // --- 新增:更新点云描述符集,指向已有的 uniform_buffer_vs --- - update_point_descriptor_set(); // 调用辅助函数进行更新 - // --- 新增结束 --- -} +//void TextureLoading::setup_point_descriptor_set_layout() +//{ +// // --- 修改:为点云 UBO 创建描述符集布局绑定,指向 binding 0 --- +// VkDescriptorSetLayoutBinding ubo_layout_binding{}; +// ubo_layout_binding.binding = 0; // 与着色器中的 layout(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 layout_info{ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO }; +// layout_info.bindingCount = 1; +// layout_info.pBindings = &ubo_layout_binding; // 指向我们的 UBO 绑定 +// +// VK_CHECK(vkCreateDescriptorSetLayout(get_device().get_handle(), &layout_info, nullptr, &point_descriptor_set_layout)); +// // --- 修改结束 --- +//} +// +//void TextureLoading::setup_point_descriptor_set() +//{ +// // --- 修改:分配点云描述符集 --- +// VkDescriptorSetAllocateInfo alloc_info{ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO }; +// alloc_info.descriptorPool = descriptor_pool; // 使用您已有的描述符池 +// alloc_info.descriptorSetCount = 1; +// alloc_info.pSetLayouts = &point_descriptor_set_layout; +// +// VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &point_descriptor_set)); +// // --- 修改结束 --- +// +// // --- 新增:更新点云描述符集,指向已有的 uniform_buffer_vs --- +// update_point_descriptor_set(); // 调用辅助函数进行更新 +// // --- 新增结束 --- +//} -// --- 新增:更新点云描述符集以指向 uniform_buffer_vs 的辅助函数 --- -void TextureLoading::update_point_descriptor_set() -{ - if (!point_uniform_vs) { // 检查主 UBO 缓冲区是否存在 - LOGW("Main uniform buffer (uniform_buffer_vs) not created yet, cannot update point descriptor set."); - return; - } - VkDescriptorBufferInfo buffer_info{}; - buffer_info.buffer = uniform_buffer_vs->get_handle(); - // 关键:指定要绑定的 UBO 数据在缓冲区中的范围 - // 我们绑定整个 ubo_vs,因为着色器只访问前两个 mat4,其余部分被忽略但不影响 - buffer_info.offset = 0; - buffer_info.range = sizeof(ubo_vs); // 绑定整个结构体 - - VkWriteDescriptorSet descriptor_write{ VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET }; - descriptor_write.dstSet = point_descriptor_set; // 目标描述符集 - descriptor_write.dstBinding = 0; // 目标绑定 (binding = 0) - descriptor_write.dstArrayElement = 0; // 数组元素索引 (非数组) - descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_write.descriptorCount = 1; // 更新一个描述符 - descriptor_write.pBufferInfo = &buffer_info; // 指向缓冲区信息 - // pImageInfo 和 pTexelBufferView 对于 Uniform Buffer 不需要 - - vkUpdateDescriptorSets(get_device().get_handle(), 1, &descriptor_write, 0, nullptr); -} -// --- 新增结束 --- - -void TextureLoading::prepare_pipeline_point() -{ - // 创建管线布局 - VkPipelineLayoutCreateInfo pipeline_layout_create_info{ VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO }; - pipeline_layout_create_info.setLayoutCount = 1; - pipeline_layout_create_info.pSetLayouts = &point_descriptor_set_layout; - // 没有 push constants - pipeline_layout_create_info.pushConstantRangeCount = 0; - pipeline_layout_create_info.pPushConstantRanges = nullptr; - - VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &point_pipeline_layout)); - - // 加载着色器 - std::array shader_stages; - shader_stages[0] = load_shader("texture_loading", "pointcloud.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shader_stages[1] = load_shader("texture_loading", "pointcloud.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); - - - // 顶点输入绑定描述 (告诉 Vulkan 顶点数据的格式) - VkVertexInputBindingDescription vertex_input_binding_description{}; - vertex_input_binding_description.binding = 0; // 绑定点 - vertex_input_binding_description.stride = sizeof(PointVertex); // 每个顶点的字节大小 - vertex_input_binding_description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - - // 顶点输入属性描述 (告诉 Vulkan 每个属性在顶点结构中的位置) - std::array vertex_input_attributes = { - VkVertexInputAttributeDescription{0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(PointVertex, x)}, // 位置 - VkVertexInputAttributeDescription{1, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(PointVertex, r)} // 颜色 - }; - - VkPipelineVertexInputStateCreateInfo vertex_input_state{ VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO }; - vertex_input_state.vertexBindingDescriptionCount = 1; - vertex_input_state.pVertexBindingDescriptions = &vertex_input_binding_description; - vertex_input_state.vertexAttributeDescriptionCount = static_cast(vertex_input_attributes.size()); - vertex_input_state.pVertexAttributeDescriptions = vertex_input_attributes.data(); - - // 输入装配 (绘制点列表) - VkPipelineInputAssemblyStateCreateInfo input_assembly_state{ VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO }; - input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; // 关键:绘制点 - input_assembly_state.primitiveRestartEnable = VK_FALSE; - - // 视口和裁剪 - VkPipelineViewportStateCreateInfo viewport_state{ VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO }; - viewport_state.viewportCount = 1; - viewport_state.scissorCount = 1; - - // 光栅化 - VkPipelineRasterizationStateCreateInfo rasterization_state{ VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO }; - rasterization_state.polygonMode = VK_POLYGON_MODE_FILL; - rasterization_state.cullMode = VK_CULL_MODE_NONE; // 通常不对点进行剔除 - rasterization_state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; - rasterization_state.lineWidth = 1.0f; // 可以通过 VkPhysicalDeviceFeatures::wideLines 扩展来支持更宽的线 - - // 多重采样 - VkPipelineMultisampleStateCreateInfo multisample_state{ VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO }; - multisample_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; - - // 深度和模板测试 (通常对点云启用深度测试) - VkPipelineDepthStencilStateCreateInfo depth_stencil_state{ VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO }; - depth_stencil_state.depthTestEnable = VK_FALSE; - depth_stencil_state.depthWriteEnable = VK_FALSE; - depth_stencil_state.depthCompareOp = VK_COMPARE_OP_ALWAYS; // 或 VK_COMPARE_OP_LESS - depth_stencil_state.depthBoundsTestEnable = VK_FALSE; - depth_stencil_state.stencilTestEnable = VK_FALSE; - - // 颜色混合 (点通常不需要混合) - VkPipelineColorBlendAttachmentState blend_attachment_state{}; - blend_attachment_state.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; - blend_attachment_state.blendEnable = VK_FALSE; - - VkPipelineColorBlendStateCreateInfo color_blend_state{ VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO }; - color_blend_state.attachmentCount = 1; - color_blend_state.pAttachments = &blend_attachment_state; - - // 动态状态 (视口和裁剪矩形将在命令缓冲区中设置) - std::vector dynamic_state_enables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; - VkPipelineDynamicStateCreateInfo dynamic_state{ VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO }; - dynamic_state.dynamicStateCount = static_cast(dynamic_state_enables.size()); - dynamic_state.pDynamicStates = dynamic_state_enables.data(); - - // 创建图形管线 - VkGraphicsPipelineCreateInfo pipeline_create_info{ VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO }; - pipeline_create_info.stageCount = static_cast(shader_stages.size()); - pipeline_create_info.pStages = shader_stages.data(); - pipeline_create_info.pVertexInputState = &vertex_input_state; - pipeline_create_info.pInputAssemblyState = &input_assembly_state; - pipeline_create_info.pViewportState = &viewport_state; - pipeline_create_info.pRasterizationState = &rasterization_state; - pipeline_create_info.pMultisampleState = &multisample_state; - pipeline_create_info.pDepthStencilState = &depth_stencil_state; - pipeline_create_info.pColorBlendState = &color_blend_state; - pipeline_create_info.pDynamicState = &dynamic_state; - pipeline_create_info.layout = point_pipeline_layout; - pipeline_create_info.renderPass = render_pass; // 使用主渲染通道 - pipeline_create_info.subpass = 0; // 主子通道 - - VK_CHECK(vkCreateGraphicsPipelines(get_device().get_handle(), pipeline_cache, 1, &pipeline_create_info, nullptr, &point_pipeline)); -} void TextureLoading::update_point_vertex_buffer(float* pos, int pointCount) { @@ -1706,10 +1680,10 @@ void TextureLoading::update_point_vertex_buffer(float* pos, int pointCount) // 2. 更新或创建顶点缓冲区 VkDeviceSize buffer_size = sizeof(PointVertex) * point_count; - if (!point_vertex_buffer || point_vertex_buffer->get_size() < buffer_size) { + if (!vertex_buffer_point || vertex_buffer_point->get_size() < buffer_size) { // 如果缓冲区不存在或太小,则重新创建 - point_vertex_buffer.reset(); - point_vertex_buffer = std::make_unique(get_device(), + vertex_buffer_point.reset(); + vertex_buffer_point = std::make_unique(get_device(), buffer_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU // CPU 可写,GPU 可读 @@ -1717,10 +1691,10 @@ void TextureLoading::update_point_vertex_buffer(float* pos, int pointCount) } // 3. 将数据复制到缓冲区 - void* mapped_data = point_vertex_buffer->map(); + void* mapped_data = vertex_buffer_point->map(); if (mapped_data) { memcpy(mapped_data, vertices.data(), buffer_size); - point_vertex_buffer->unmap(); + vertex_buffer_point->unmap(); } else { LOGE("Failed to map point vertex buffer for update."); diff --git a/samples/api/texture_loading/texture_loading.h b/samples/api/texture_loading/texture_loading.h index ea4bd01..1e2e483 100644 --- a/samples/api/texture_loading/texture_loading.h +++ b/samples/api/texture_loading/texture_loading.h @@ -84,8 +84,8 @@ public: std::unique_ptr uniform_buffer_vs_point; - glm::vec3 camear_pos_point; - glm::vec3 pos_point; + glm::vec3 camear_pos_point = {0, 0, -2.f}; + glm::vec3 face_pos_point; struct { @@ -125,22 +125,28 @@ public: void generate_quad(); void setup_descriptor_pool(); - void setup_descriptor_set_layout(); - void setup_descriptor_set_layout_bg(); - void setup_descriptor_set_layout_point(); + void setup_descriptor_set_layout(); void setup_descriptor_set(); + + void setup_descriptor_set_layout_bg(); void setup_descriptor_set_bg(); + + void setup_descriptor_set_layout_point(); void setup_descriptor_set_point(); + void prepare_uniform_buffers(); + void update_uniform_buffers(); + + void prepare_uniform_buffers_point(); + void update_uniform_buffers_point(); + + void prepare_pipelines(); void prepare_pipeline_bg(); void prepare_pipeline_point(); - void prepare_uniform_buffers(); - void update_uniform_buffers(); - void prepare_uniform_buffers_point(); - void update_uniform_buffers_point(); + bool prepare(const vkb::ApplicationOptions& options) override; virtual void render(float delta_time) override; virtual void view_changed() override;