windows 脸和线都画出来了。

This commit is contained in:
xsl
2025-10-20 17:56:56 +08:00
parent 1f0422072a
commit 576c302cd1
5 changed files with 263 additions and 43 deletions
+2 -28
View File
@@ -26,12 +26,6 @@ void Application::initWindow()
#endif #endif
} }
void Application::run()
{
initVulkan();
mainLoop();
cleanup();
}
#ifdef _WIN32 #ifdef _WIN32
#else #else
@@ -580,8 +574,7 @@ void Application::cleanup() {
DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
} }
// 等待设备空闲,确保所有命令都完成
vkDeviceWaitIdle(device); vkDeviceWaitIdle(device);
// 正确销毁所有同步对象 // 正确销毁所有同步对象
@@ -591,44 +584,25 @@ void Application::cleanup() {
vkDestroyFence(device, inFlightFences[i], nullptr); vkDestroyFence(device, inFlightFences[i], nullptr);
} }
// 不需要手动释放commandBuffers,销毁命令池时会自动释放
// 销毁命令池
vkDestroyCommandPool(device, commandPool, nullptr);
// 销毁图形管线
vkDestroyPipeline(device, graphicsPipeline, nullptr); vkDestroyPipeline(device, graphicsPipeline, nullptr);
// 销毁管线布局
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
// 销毁渲染流程
vkDestroyRenderPass(device, renderPass, nullptr); vkDestroyRenderPass(device, renderPass, nullptr);
// 销毁图像视图
for (auto imageView : swapChainImageViews) { for (auto imageView : swapChainImageViews) {
vkDestroyImageView(device, imageView, nullptr); vkDestroyImageView(device, imageView, nullptr);
} }
// 销毁交换链
vkDestroySwapchainKHR(device, swapChain, nullptr); vkDestroySwapchainKHR(device, swapChain, nullptr);
// 销毁命令池
vkDestroyCommandPool(device, commandPool, nullptr);
// 销毁逻辑设备
vkDestroyDevice(device, nullptr); vkDestroyDevice(device, nullptr);
// 销毁窗口表面
vkDestroySurfaceKHR(instance, surface, nullptr); vkDestroySurfaceKHR(instance, surface, nullptr);
// 销毁 Vulkan 实例
vkDestroyInstance(instance, nullptr); vkDestroyInstance(instance, nullptr);
#ifdef _WIN32 #ifdef _WIN32
// 销毁 GLFW 窗口
glfwDestroyWindow(window); glfwDestroyWindow(window);
// 终止 GLFW
glfwTerminate(); glfwTerminate();
#endif #endif
} }
+1 -2
View File
@@ -21,13 +21,12 @@ struct Texture
class Application : public AppBase class Application : public AppBase
{ {
public: public:
void run();
virtual void initVulkan(); virtual void initVulkan();
void initWindow(); void initWindow();
void createSurface(); void createSurface();
void mainLoop(); void mainLoop();
void cleanup(); virtual void cleanup();
void createImageViews(); void createImageViews();
void createFramebuffers(); void createFramebuffers();
+246 -9
View File
@@ -15,6 +15,15 @@ FaceApp::~FaceApp()
{ {
} }
void ReceiveFacePoint(float* pos, int pointCount, int width, int height)
{
FaceApp* self = FaceApp::Get();
if (self != nullptr)
{
FaceApp::Get()->update_face_vertex_buffer(pos, pointCount);
}
}
bool FaceApp::LoadOBJ(const std::string& filename, bool FaceApp::LoadOBJ(const std::string& filename,
std::vector<TextureLoadingVertexStructure>& vertices, std::vector<TextureLoadingVertexStructure>& vertices,
std::vector<uint32_t>& indices) { std::vector<uint32_t>& indices) {
@@ -468,7 +477,17 @@ void FaceApp::setup_descriptor_set()
void FaceApp::render(VkCommandBuffer commandBuffer) void FaceApp::render(VkCommandBuffer commandBuffer)
{ {
Application::render(commandBuffer); //Application::render(commandBuffer);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout_bg, 0, 1, &m_descriptor_set_bg, 0, NULL);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline_bg);
vkCmdPushConstants(commandBuffer, m_pipelineLayout_bg, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue);
vkCmdDraw(commandBuffer, 6, 1, 0, 0);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_set, 0, NULL); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_set, 0, NULL);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline);
@@ -504,14 +523,33 @@ void FaceApp::initVulkan()
createVmaAllocator(); createVmaAllocator();
LoadOBJ("face_picture_3dmax.obj", obj_vertices, obj_indices); LoadOBJ("face_picture_3dmax.obj", obj_vertices, obj_indices);
loadTexture("demo0.png", tex_demo0); loadTexture("demo0.png", tex_demo0);
loadTexture("out.png", tex_bg);
createVertexBuffer(); createVertexBuffer();
createUniformBuffer(); createUniformBuffer();
setup_descriptor_pool(); setup_descriptor_pool();
setup_descriptor_set_layout(); setup_descriptor_set_layout();
setup_descriptor_set(); setup_descriptor_set();
create_face_pipelines(); create_face_pipelines();
setup_descriptor_set_layout_bg();
setup_descriptor_set_bg();
create_pipelines_bg();
uploadVertexData(); uploadVertexData();
faceAppInited = true; faceAppInited = true;
#if _WIN32
std::vector<float> floatArray;
std::string& str = HardCodeData::Get().face_result_point_str;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, ',')) {
floatArray.push_back(std::stof(token));
}
ReceiveFacePoint(floatArray.data(), floatArray.size() / 3, 480, 480);
#endif
} }
void FaceApp::update_uniform_buffers() void FaceApp::update_uniform_buffers()
@@ -579,14 +617,7 @@ void FaceApp::uploadVertexData() {
copyBuffer(m_stagingBuffer, m_indexBuffer, sizeof(uint32_t) * obj_indices.size()); copyBuffer(m_stagingBuffer, m_indexBuffer, sizeof(uint32_t) * obj_indices.size());
} }
void ReceiveFacePoint(float* pos, int pointCount, int width, int height)
{
FaceApp* self = FaceApp::Get();
if (self != nullptr)
{
FaceApp::Get()->update_face_vertex_buffer(pos, pointCount);
}
}
void FaceApp::update_face_vertex_buffer(float* pos, int pointCount) void FaceApp::update_face_vertex_buffer(float* pos, int pointCount)
{ {
@@ -675,3 +706,209 @@ void FaceApp::createUniformBuffer()
update_uniform_buffers(); update_uniform_buffers();
} }
void FaceApp::create_pipelines_bg()
{
VkPipelineInputAssemblyStateCreateInfo input_assembly_state{};
input_assembly_state.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
input_assembly_state.flags = 0;
input_assembly_state.primitiveRestartEnable = VK_FALSE;
VkPipelineRasterizationStateCreateInfo rasterization_state{};
rasterization_state.sType = 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.flags = 0;
rasterization_state.depthClampEnable = VK_FALSE;
rasterization_state.lineWidth = 1.0f;
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.blendEnable = VK_FALSE;
colorBlendAttachment.colorWriteMask = 0xf;
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {};
depth_stencil_state.sType = 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_GREATER;
depth_stencil_state.front = depth_stencil_state.back;
depth_stencil_state.back.compareOp = VK_COMPARE_OP_ALWAYS;
VkPipelineViewportStateCreateInfo viewport_state{};
viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewport_state.viewportCount = 1;
viewport_state.scissorCount = 1;
viewport_state.flags = 0;
VkPipelineMultisampleStateCreateInfo multisample_state{};
multisample_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisample_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
multisample_state.flags = 0;
// 视口状态
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = (float)swapChainExtent.width;
viewport.height = (float)swapChainExtent.height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = swapChainExtent;
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
auto vertShaderCode = readFile("shaders/bg.vert.spv");
auto fragShaderCode = readFile("shaders/bg.frag.spv");
VkShaderModule vertShaderModule = createShaderModule(this->device, vertShaderCode);
VkShaderModule fragShaderModule = createShaderModule(this->device, fragShaderCode);
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shader_stages[] = { vertShaderStageInfo, fragShaderStageInfo };
VkPipelineVertexInputStateCreateInfo vertex_input_state{};
vertex_input_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
VkGraphicsPipelineCreateInfo pipeline_create_info{};
pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipeline_create_info.layout = m_pipelineLayout_bg;
pipeline_create_info.renderPass = renderPass;
pipeline_create_info.flags = 0;
pipeline_create_info.basePipelineIndex = -1;
pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_create_info.pVertexInputState = &vertex_input_state;
pipeline_create_info.pInputAssemblyState = &input_assembly_state;
pipeline_create_info.pRasterizationState = &rasterization_state;
pipeline_create_info.pColorBlendState = &colorBlending; //&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.pViewportState = &viewportState;
pipeline_create_info.stageCount = 2;
pipeline_create_info.pStages = shader_stages;
VK_CHECK(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_create_info, nullptr, &m_graphicsPipeline_bg));
}
void FaceApp::setup_descriptor_set_layout_bg()
{
VkDescriptorSetLayoutBinding set_layout_binding{};
set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
set_layout_binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
set_layout_binding.binding = 0;
set_layout_binding.descriptorCount = 1;
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings ={set_layout_binding };
VkDescriptorSetLayoutCreateInfo descriptor_layout{};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptor_layout.pBindings = set_layout_bindings.data();
descriptor_layout.bindingCount = static_cast<uint32_t>(set_layout_bindings.size());
VK_CHECK(vkCreateDescriptorSetLayout(device, &descriptor_layout, nullptr, &m_descriptorSetLayout_bg));
VkPipelineLayoutCreateInfo pipeline_layout_create_info{};
pipeline_layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_create_info.setLayoutCount = 1;
pipeline_layout_create_info.pSetLayouts = &m_descriptorSetLayout_bg;
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 只在片段着色器中使用
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(float); // 或者 sizeof(PushConstants)
pipeline_layout_create_info.pushConstantRangeCount = 1;
pipeline_layout_create_info.pPushConstantRanges = &pushConstantRange;
VK_CHECK(vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &m_pipelineLayout_bg));
}
void FaceApp::setup_descriptor_set_bg()
{
VkDescriptorSetAllocateInfo alloc_info{};
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.descriptorPool = descriptor_pool;
alloc_info.pSetLayouts = &m_descriptorSetLayout_bg;
alloc_info.descriptorSetCount = 1;
VK_CHECK(vkAllocateDescriptorSets(device, &alloc_info, &m_descriptor_set_bg));
VkDescriptorImageInfo image_descriptor;
image_descriptor.imageView = tex_bg.view;
image_descriptor.sampler = tex_bg.sampler;
image_descriptor.imageLayout = tex_bg.image_layout;
VkWriteDescriptorSet write_descriptor_set{};
write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_descriptor_set.dstSet = m_descriptor_set_bg;
write_descriptor_set.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write_descriptor_set.dstBinding = 0;
write_descriptor_set.pImageInfo = &image_descriptor;
write_descriptor_set.descriptorCount = 1;
std::vector<VkWriteDescriptorSet> write_descriptor_sets = { write_descriptor_set };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL);
}
void FaceApp::cleanup()
{
vkDeviceWaitIdle(device);
vkDestroyCommandPool(device, commandPool, nullptr);
vkDestroyPipeline(device, m_graphicsPipeline, nullptr);
vkDestroyPipelineLayout(device, m_pipelineLayout, nullptr);
//vmaUnmapMemory(allocator, m_stagingBufferAllocation);
//vmaUnmapMemory(allocator, uniform_buffer_allocation);
//vmaDestroyBuffer(allocator, uniform_buffer_vs, uniform_buffer_allocation);
//vmaDestroyBuffer(allocator, m_indexBuffer, m_indexBufferAllocation);
//vmaDestroyBuffer(allocator, m_vertexBuffer, m_vertexBufferAllocation);
//vmaDestroyBuffer(allocator, m_stagingBuffer, m_stagingBufferAllocation);
vkDestroyDescriptorSetLayout(device, m_descriptorSetLayout, nullptr);
Application::cleanup();
}
+11 -1
View File
@@ -43,7 +43,7 @@ public:
void update_face_vertex_buffer(float* pos, int pointCount); void update_face_vertex_buffer(float* pos, int pointCount);
virtual bool isInited() { return inited && faceAppInited; } virtual bool isInited() { return inited && faceAppInited; }
virtual void cleanup() override;
private: private:
glm::vec3 rotation = glm::vec3(); glm::vec3 rotation = glm::vec3();
glm::vec3 camera_pos = glm::vec3(); glm::vec3 camera_pos = glm::vec3();
@@ -101,6 +101,16 @@ private:
bool faceAppInited = false; bool faceAppInited = false;
float myFloatValue = 1.5f; float myFloatValue = 1.5f;
Texture tex_bg = {};
void create_pipelines_bg();
void setup_descriptor_set_layout_bg();
void setup_descriptor_set_bg();
VkPipeline m_graphicsPipeline_bg = VK_NULL_HANDLE;
VkPipelineLayout m_pipelineLayout_bg = VK_NULL_HANDLE;
VkDescriptorSetLayout m_descriptorSetLayout_bg = VK_NULL_HANDLE;
VkDescriptorSet m_descriptor_set_bg = VK_NULL_HANDLE;
}; };
#endif #endif
+3 -3
View File
@@ -5,13 +5,13 @@
int main() { int main() {
FaceApp app; FaceApp app;
app.initVulkan();
try { try {
app.run(); app.mainLoop();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
app.cleanup();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }