重构point 代码
This commit is contained in:
@@ -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 个顶点
|
||||
}
|
||||
|
||||
@@ -35,31 +35,14 @@ struct TextureLoadingVertexStructure
|
||||
float normal[3];
|
||||
};
|
||||
|
||||
struct PointVertex {
|
||||
float x, y, z; // 位置
|
||||
float r, g, b; // 颜色 (可以根据需要修改)
|
||||
};
|
||||
|
||||
class TextureLoading : public ApiVulkanSample
|
||||
{
|
||||
public:
|
||||
// --- 新增:用于点云渲染的结构 ---
|
||||
struct PointVertex {
|
||||
float x, y, z; // 位置
|
||||
float r, g, b; // 颜色 (可以根据需要修改)
|
||||
};
|
||||
|
||||
std::unique_ptr<vkb::core::BufferC> point_vertex_buffer;
|
||||
uint32_t point_count = 0;
|
||||
VkPipeline point_pipeline;
|
||||
VkPipelineLayout point_pipeline_layout;
|
||||
VkDescriptorSetLayout point_descriptor_set_layout;
|
||||
VkDescriptorSet point_descriptor_set;
|
||||
|
||||
void setup_point_descriptor_set_layout();
|
||||
void setup_point_descriptor_set();
|
||||
void prepare_point_pipeline();
|
||||
void update_point_vertex_buffer(float* pos, int pointCount);
|
||||
void draw_point_cloud(VkCommandBuffer command_buffer);
|
||||
void update_point_descriptor_set();
|
||||
|
||||
|
||||
|
||||
struct Texture
|
||||
{
|
||||
VkSampler sampler;
|
||||
@@ -72,13 +55,15 @@ public:
|
||||
};
|
||||
|
||||
Texture texture = { 0 };
|
||||
Texture texture_point = { 0 };
|
||||
Texture cam_text = { 0 };
|
||||
|
||||
std::unique_ptr<vkb::core::BufferC> vertex_buffer;
|
||||
std::unique_ptr<vkb::core::BufferC> index_buffer;
|
||||
uint32_t index_count;
|
||||
|
||||
std::unique_ptr<vkb::core::BufferC> uniform_buffer_vs;
|
||||
std::unique_ptr<vkb::core::BufferC> vertex_buffer_point;
|
||||
uint32_t point_count = 0;
|
||||
|
||||
struct
|
||||
{
|
||||
@@ -88,27 +73,39 @@ public:
|
||||
float lod_bias = 0.0f;
|
||||
} ubo_vs;
|
||||
|
||||
std::unique_ptr<vkb::core::BufferC> point_uniform_vs;
|
||||
struct
|
||||
{
|
||||
glm::mat4 projection;
|
||||
glm::mat4 model;
|
||||
glm::mat4 view;
|
||||
} point_ubo;
|
||||
} ubo_vs_point;
|
||||
|
||||
std::unique_ptr<vkb::core::BufferC> uniform_buffer_vs;
|
||||
std::unique_ptr<vkb::core::BufferC> uniform_buffer_vs_point;
|
||||
|
||||
|
||||
glm::vec3 camear_pos_point;
|
||||
glm::vec3 pos_point;
|
||||
|
||||
struct
|
||||
{
|
||||
VkPipeline solid;
|
||||
VkPipeline background;
|
||||
VkPipeline point;
|
||||
} pipelines;
|
||||
|
||||
|
||||
VkPipelineLayout pipeline_layout;
|
||||
VkPipelineLayout pipeline_layout_bg;
|
||||
VkPipelineLayout pipeline_layout_point;
|
||||
|
||||
VkDescriptorSet descriptor_set;
|
||||
VkDescriptorSet descriptor_set_bg;
|
||||
VkDescriptorSet descriptor_set_point;
|
||||
|
||||
VkDescriptorSetLayout descriptor_set_layout;
|
||||
VkDescriptorSetLayout descriptor_set_layout_bg;
|
||||
VkDescriptorSetLayout descriptor_set_layout_point;
|
||||
|
||||
static TextureLoading* Get() {
|
||||
return this_instance;
|
||||
@@ -122,16 +119,28 @@ public:
|
||||
void load_texture();
|
||||
void destroy_texture(Texture texture);
|
||||
void build_command_buffers() override;
|
||||
|
||||
void draw();
|
||||
void draw_point_cloud(VkCommandBuffer command_buffer);
|
||||
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();
|
||||
void setup_descriptor_set_bg();
|
||||
void setup_descriptor_set_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;
|
||||
@@ -146,7 +155,8 @@ public:
|
||||
void transitionImageLayout(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
|
||||
|
||||
public:
|
||||
void prepare_pipeline_bg();
|
||||
|
||||
void update_point_vertex_buffer(float* pos, int pointCount);
|
||||
private:
|
||||
std::thread workerThread;
|
||||
bool running = false;
|
||||
|
||||
Reference in New Issue
Block a user