重构point 代码

This commit is contained in:
xsl
2025-09-18 22:32:43 +08:00
parent 3ca95b90f8
commit f63f46e975
2 changed files with 152 additions and 61 deletions
+116 -35
View File
@@ -38,32 +38,29 @@ TextureLoading::~TextureLoading()
vkDestroyPipeline(get_device().get_handle(), pipelines.solid, nullptr);
vkDestroyPipeline(get_device().get_handle(), pipelines.background, nullptr);
vkDestroyPipeline(get_device().get_handle(), pipelines.point, nullptr);
vkDestroyPipelineLayout(get_device().get_handle(), pipeline_layout, nullptr);
vkDestroyPipelineLayout(get_device().get_handle(), pipeline_layout_bg, nullptr);
vkDestroyPipelineLayout(get_device().get_handle(), pipeline_layout_point, nullptr);
vkDestroyDescriptorSetLayout(get_device().get_handle(), descriptor_set_layout, nullptr);
vkDestroyDescriptorSetLayout(get_device().get_handle(), descriptor_set_layout_bg, nullptr);
if (point_pipeline) {
vkDestroyPipeline(get_device().get_handle(), point_pipeline, nullptr);
}
if (point_pipeline_layout) {
vkDestroyPipelineLayout(get_device().get_handle(), point_pipeline_layout, nullptr);
}
if (point_descriptor_set_layout) {
vkDestroyDescriptorSetLayout(get_device().get_handle(), point_descriptor_set_layout, nullptr);
}
vkDestroyDescriptorSetLayout(get_device().get_handle(), descriptor_set_layout_point, nullptr);
}
destroy_texture(texture);
destroy_texture(texture_point);
destroy_texture(cam_text);
vertex_buffer.reset();
index_buffer.reset();
uniform_buffer_vs.reset();
point_uniform_vs.reset();
uniform_buffer_vs_point.reset();
point_vertex_buffer.reset(); // BufferC 会自动清理
vertex_buffer_point.reset(); // BufferC 会自动清理
//stop();
}
@@ -696,6 +693,37 @@ void TextureLoading::setup_descriptor_set_layout_bg()
VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_bg));
}
void TextureLoading::setup_descriptor_set_layout_point()
{
std::vector<VkDescriptorSetLayoutBinding> 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<uint32_t>(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()
{
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings =
@@ -764,6 +792,45 @@ void TextureLoading::setup_descriptor_set()
vkUpdateDescriptorSets(get_device().get_handle(), static_cast<uint32_t>(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL);
}
void TextureLoading::setup_descriptor_set_point()
{
VkDescriptorSetAllocateInfo alloc_info =
vkb::initializers::descriptor_set_allocate_info(
descriptor_pool,
&descriptor_set_layout_point,
1);
VK_CHECK(vkAllocateDescriptorSets(get_device().get_handle(), &alloc_info, &descriptor_set_point));
VkDescriptorBufferInfo buffer_descriptor = create_descriptor(*uniform_buffer_vs_point);
VkDescriptorImageInfo image_descriptor;
image_descriptor.imageView = texture_point.view;
image_descriptor.sampler = texture_point.sampler;
image_descriptor.imageLayout = texture_point.image_layout;
std::vector<VkWriteDescriptorSet> write_descriptor_sets =
{
// Binding 0 : Vertex shader uniform buffer
vkb::initializers::write_descriptor_set(
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
};
vkUpdateDescriptorSets(get_device().get_handle(), static_cast<uint32_t>(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL);
}
void TextureLoading::setup_descriptor_set_bg()
{
VkDescriptorSetAllocateInfo alloc_info =
@@ -987,6 +1054,29 @@ void TextureLoading::update_uniform_buffers()
uniform_buffer_vs->convert_and_update(ubo_vs);
}
void TextureLoading::prepare_uniform_buffers_point()
{
uniform_buffer_vs_point = std::make_unique<vkb::core::BufferC>(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<float>(width) / static_cast<float>(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)
{
if (!ApiVulkanSample::prepare(options))
@@ -1007,34 +1097,25 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options)
load_texture();
generate_quad();
prepare_uniform_buffers();
prepare_uniform_buffers_point();
setup_descriptor_set_layout();
setup_descriptor_set_layout_bg();
setup_descriptor_set_layout_point();
prepare_pipelines();
prepare_pipeline_bg();
prepare_pipeline_point();
setup_descriptor_pool();
setup_descriptor_set();
setup_descriptor_set_bg();
setup_point_descriptor_set_layout();
prepare_point_pipeline();
setup_point_descriptor_set();
float z = 0.1;
//build_command_buffers();
float simPoint[] = {
1.0, 1.0, z,
-1.0, 1.0, z,
-1.0, -1.0, z,
1.0, -1.0, z,
0.8, 0.8, z,
-0.8, 0.8, z,
-0.8, -0.8, z,
0.8, -0.8, z,
};
update_point_vertex_buffer(simPoint, 8);
setup_descriptor_set_point();
prepared = true;
//start();
return true;
@@ -1480,7 +1561,7 @@ void TextureLoading::update_point_descriptor_set()
}
// --- 新增结束 ---
void TextureLoading::prepare_point_pipeline()
void TextureLoading::prepare_pipeline_point()
{
// 创建管线布局
VkPipelineLayoutCreateInfo pipeline_layout_create_info{ VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
@@ -1648,15 +1729,15 @@ void TextureLoading::update_point_vertex_buffer(float* pos, int pointCount)
void TextureLoading::draw_point_cloud(VkCommandBuffer command_buffer)
{
if (point_count == 0 || !point_vertex_buffer) {
if (point_count == 0 || !vertex_buffer_point) {
return; // 没有点或缓冲区未准备好
}
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, point_pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, point_pipeline_layout, 0, 1, &point_descriptor_set, 0, nullptr);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.point);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout_point, 0, 1, &descriptor_set_point, 0, nullptr);
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(command_buffer, 0, 1, point_vertex_buffer->get(), offsets);
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffer_point->get(), offsets);
vkCmdDraw(command_buffer, point_count, 1, 0, 0); // 绘制 point_count 个顶点
}