添加双纹理采样

This commit is contained in:
xsl
2025-10-22 18:16:03 +08:00
parent ce2444bd07
commit f6dd3ab38e
8 changed files with 178 additions and 17 deletions
+32 -7
View File
@@ -408,7 +408,14 @@ void FaceApp::setup_descriptor_set_layout()
set_layout_binding_fragment.binding = 1;
set_layout_binding_fragment.descriptorCount = 1;
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings{ set_layout_binding_vertex , set_layout_binding_fragment};
// 添加第二个纹理绑定点
VkDescriptorSetLayoutBinding set_layout_binding_fragment1{};
set_layout_binding_fragment1.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
set_layout_binding_fragment1.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
set_layout_binding_fragment1.binding = 2;
set_layout_binding_fragment1.descriptorCount = 1;
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings{ set_layout_binding_vertex , set_layout_binding_fragment, set_layout_binding_fragment1 };
VkDescriptorSetLayoutCreateInfo descriptor_layout{};
@@ -457,6 +464,12 @@ void FaceApp::setup_descriptor_set()
image_descriptor.sampler = tex_demo0.sampler;
image_descriptor.imageLayout = tex_demo0.image_layout;
// 第二个纹理描述符 - 假设你的第二个纹理变量名为 tex_demo1
VkDescriptorImageInfo image_descriptor1;
image_descriptor1.imageView = tex_demo0_ex.view;
image_descriptor1.sampler = tex_demo0_ex.sampler;
image_descriptor1.imageLayout = tex_demo0_ex.image_layout;
VkWriteDescriptorSet write_descriptor_set_uniform{};
write_descriptor_set_uniform.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_descriptor_set_uniform.dstSet = m_descriptor_set;
@@ -474,10 +487,20 @@ void FaceApp::setup_descriptor_set()
write_descriptor_set_image.pImageInfo = &image_descriptor;
write_descriptor_set_image.descriptorCount = 1;
// 添加第二个纹理的写入描述符
VkWriteDescriptorSet write_descriptor_set_image1{};
write_descriptor_set_image1.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_descriptor_set_image1.dstSet = m_descriptor_set;
write_descriptor_set_image1.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write_descriptor_set_image1.dstBinding = 2; // 绑定到位置2
write_descriptor_set_image1.pImageInfo = &image_descriptor1;
write_descriptor_set_image1.descriptorCount = 1;
std::vector<VkWriteDescriptorSet> write_descriptor_sets =
{
write_descriptor_set_uniform,
write_descriptor_set_image
write_descriptor_set_image,
write_descriptor_set_image1, // 添加第二个纹理
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL);
@@ -489,13 +512,13 @@ void FaceApp::render(VkCommandBuffer commandBuffer)
std::unique_lock<std::mutex> lock_point(mtx_point);
//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);
//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);
//vkCmdPushConstants(commandBuffer, m_pipelineLayout_bg, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue);
vkCmdDraw(commandBuffer, 6, 1, 0, 0);
//vkCmdDraw(commandBuffer, 6, 1, 0, 0);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_set, 0, NULL);
@@ -550,6 +573,8 @@ void FaceApp::initVulkan()
createVmaAllocator();
LoadOBJ("face_picture_3dmax.obj", obj_vertices, obj_indices);
loadTexture("demo0.png", tex_demo0, true);
loadTexture("demo0_ex.png", tex_demo0_ex, true);
loadTexture("out.png", tex_bg, false);
createVertexBuffer();
createUniformBuffer();
@@ -665,7 +690,7 @@ void FaceApp::update_face_vertex_buffer(float* pos, int pointCount)
obj_vertices[i].pos[1] = y;
obj_vertices[i].pos[2] = z;
}
calculateVertexNormals(obj_vertices, obj_indices);
uploadVertexData();
}