终于是画出来了。

This commit is contained in:
xsl
2025-10-20 16:02:41 +08:00
parent e6a776665f
commit 1f0422072a
2 changed files with 19 additions and 18 deletions
+17 -18
View File
@@ -304,15 +304,15 @@ void FaceApp::create_face_pipelines()
vertex_input_attributes.push_back(viaPos); vertex_input_attributes.push_back(viaPos);
VkVertexInputAttributeDescription viaUv{}; VkVertexInputAttributeDescription viaUv{};
viaUv.location = 0; viaUv.location = 1;
viaUv.binding = 1; viaUv.binding = 0;
viaUv.format = VK_FORMAT_R32G32_SFLOAT; viaUv.format = VK_FORMAT_R32G32_SFLOAT;
viaUv.offset = offsetof(TextureLoadingVertexStructure, uv); viaUv.offset = offsetof(TextureLoadingVertexStructure, uv);
vertex_input_attributes.push_back(viaUv); vertex_input_attributes.push_back(viaUv);
VkVertexInputAttributeDescription viaNormal{}; VkVertexInputAttributeDescription viaNormal{};
viaNormal.location = 0; viaNormal.location = 2;
viaNormal.binding = 2; viaNormal.binding = 0;
viaNormal.format = VK_FORMAT_R32G32B32_SFLOAT; viaNormal.format = VK_FORMAT_R32G32B32_SFLOAT;
viaNormal.offset = offsetof(TextureLoadingVertexStructure, normal); viaNormal.offset = offsetof(TextureLoadingVertexStructure, normal);
vertex_input_attributes.push_back(viaNormal); vertex_input_attributes.push_back(viaNormal);
@@ -355,8 +355,8 @@ void FaceApp::setup_descriptor_pool()
descriptor_pool_size.descriptorCount = 6; descriptor_pool_size.descriptorCount = 6;
VkDescriptorPoolSize descriptor_pool_image_size{}; VkDescriptorPoolSize descriptor_pool_image_size{};
descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; descriptor_pool_image_size.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptor_pool_size.descriptorCount = 6; descriptor_pool_image_size.descriptorCount = 6;
std::vector<VkDescriptorPoolSize> pool_sizes; std::vector<VkDescriptorPoolSize> pool_sizes;
@@ -388,7 +388,7 @@ void FaceApp::setup_descriptor_set_layout()
VkDescriptorSetLayoutBinding set_layout_binding_fragment{}; VkDescriptorSetLayoutBinding set_layout_binding_fragment{};
set_layout_binding_fragment.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; set_layout_binding_fragment.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
set_layout_binding_fragment.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; set_layout_binding_fragment.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
set_layout_binding_fragment.binding = 2; set_layout_binding_fragment.binding = 1;
set_layout_binding_fragment.descriptorCount = 1; set_layout_binding_fragment.descriptorCount = 1;
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings{ set_layout_binding_vertex , set_layout_binding_fragment}; std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings{ set_layout_binding_vertex , set_layout_binding_fragment};
@@ -408,9 +408,9 @@ void FaceApp::setup_descriptor_set_layout()
pipeline_layout_create_info.pSetLayouts = &m_descriptorSetLayout; pipeline_layout_create_info.pSetLayouts = &m_descriptorSetLayout;
VkPushConstantRange pushConstantRange{}; VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 只在片段着色器中使用 pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
pushConstantRange.offset = 0; pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(float); // 或者 sizeof(PushConstants) pushConstantRange.size = sizeof(float);
pipeline_layout_create_info.pushConstantRangeCount = 1; pipeline_layout_create_info.pushConstantRangeCount = 1;
pipeline_layout_create_info.pPushConstantRanges = &pushConstantRange; pipeline_layout_create_info.pPushConstantRanges = &pushConstantRange;
@@ -436,9 +436,9 @@ void FaceApp::setup_descriptor_set()
VkDescriptorImageInfo image_descriptor; VkDescriptorImageInfo image_descriptor;
image_descriptor.imageView = tex_demo0.view; // The image's view (images are never directly accessed by the shader, but rather through views defining subresources) image_descriptor.imageView = tex_demo0.view;
image_descriptor.sampler = tex_demo0.sampler; // The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.) image_descriptor.sampler = tex_demo0.sampler;
image_descriptor.imageLayout = tex_demo0.image_layout; // The current layout of the image (Note: Should always fit the actual use, e.g. shader read) image_descriptor.imageLayout = tex_demo0.image_layout;
VkWriteDescriptorSet write_descriptor_set_uniform{}; VkWriteDescriptorSet write_descriptor_set_uniform{};
write_descriptor_set_uniform.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; write_descriptor_set_uniform.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
@@ -472,6 +472,8 @@ void FaceApp::render(VkCommandBuffer commandBuffer)
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);
vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue);
VkDeviceSize offsets[1] = { 0 }; VkDeviceSize offsets[1] = { 0 };
VkBuffer vertexBuffers[] = { m_vertexBuffer }; VkBuffer vertexBuffers[] = { m_vertexBuffer };
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
@@ -536,7 +538,7 @@ void FaceApp::createVertexBuffer()
VkDeviceSize vertexBufferSize = sizeof(TextureLoadingVertexStructure) * obj_vertices.size(); VkDeviceSize vertexBufferSize = sizeof(TextureLoadingVertexStructure) * obj_vertices.size();
VkDeviceSize indexBufferSize = sizeof(uint32_t) * obj_indices.size(); VkDeviceSize indexBufferSize = sizeof(uint32_t) * obj_indices.size();
// 创建顶点缓冲区(设备本地,用于渲染)
VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufferInfo.size = vertexBufferSize; bufferInfo.size = vertexBufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
@@ -547,13 +549,13 @@ void FaceApp::createVertexBuffer()
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_vertexBuffer, &m_vertexBufferAllocation, nullptr); vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_vertexBuffer, &m_vertexBufferAllocation, nullptr);
// 创建暂存缓冲区(CPU可见,用于上传数据)
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_stagingBuffer, &m_stagingBufferAllocation, nullptr); vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &m_stagingBuffer, &m_stagingBufferAllocation, nullptr);
// 创建索引缓冲区
bufferInfo.size = indexBufferSize; bufferInfo.size = indexBufferSize;
bufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; bufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
@@ -563,16 +565,13 @@ void FaceApp::createVertexBuffer()
void FaceApp::uploadVertexData() { void FaceApp::uploadVertexData() {
// 上传顶点数据
void* data; void* data;
vmaMapMemory(allocator, m_stagingBufferAllocation, &data); vmaMapMemory(allocator, m_stagingBufferAllocation, &data);
memcpy(data, obj_vertices.data(), sizeof(TextureLoadingVertexStructure) * obj_vertices.size()); memcpy(data, obj_vertices.data(), sizeof(TextureLoadingVertexStructure) * obj_vertices.size());
vmaUnmapMemory(allocator, m_stagingBufferAllocation); vmaUnmapMemory(allocator, m_stagingBufferAllocation);
// 复制到设备内存
copyBuffer(m_stagingBuffer, m_vertexBuffer, sizeof(TextureLoadingVertexStructure) * obj_vertices.size()); copyBuffer(m_stagingBuffer, m_vertexBuffer, sizeof(TextureLoadingVertexStructure) * obj_vertices.size());
// 上传索引数据(如果需要暂存缓冲区,可以创建另一个)
vmaMapMemory(allocator, m_stagingBufferAllocation, &data); vmaMapMemory(allocator, m_stagingBufferAllocation, &data);
memcpy(data, obj_indices.data(), sizeof(uint32_t) * obj_indices.size()); memcpy(data, obj_indices.data(), sizeof(uint32_t) * obj_indices.size());
vmaUnmapMemory(allocator, m_stagingBufferAllocation); vmaUnmapMemory(allocator, m_stagingBufferAllocation);
+2
View File
@@ -99,6 +99,8 @@ private:
Texture tex_demo0 = {0}; Texture tex_demo0 = {0};
bool faceAppInited = false; bool faceAppInited = false;
float myFloatValue = 1.5f;
}; };
#endif #endif