代码写完了,下一步调试运行
This commit is contained in:
+261
-1
@@ -2,6 +2,99 @@
|
||||
#include "hardcode_data.h"
|
||||
#include <sstream>
|
||||
|
||||
inline VkDescriptorSetLayoutBinding descriptor_set_layout_binding(
|
||||
VkDescriptorType type,
|
||||
VkShaderStageFlags flags,
|
||||
uint32_t binding,
|
||||
uint32_t count = 1)
|
||||
{
|
||||
VkDescriptorSetLayoutBinding set_layout_binding{};
|
||||
set_layout_binding.descriptorType = type;
|
||||
set_layout_binding.stageFlags = flags;
|
||||
set_layout_binding.binding = binding;
|
||||
set_layout_binding.descriptorCount = count;
|
||||
return set_layout_binding;
|
||||
}
|
||||
|
||||
inline VkDescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
|
||||
const VkDescriptorSetLayoutBinding* bindings,
|
||||
uint32_t binding_count)
|
||||
{
|
||||
VkDescriptorSetLayoutCreateInfo descriptor_set_layout_create_info{};
|
||||
descriptor_set_layout_create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||
descriptor_set_layout_create_info.pBindings = bindings;
|
||||
descriptor_set_layout_create_info.bindingCount = binding_count;
|
||||
return descriptor_set_layout_create_info;
|
||||
}
|
||||
|
||||
inline VkPipelineLayoutCreateInfo fun_pipeline_layout_create_info(
|
||||
const VkDescriptorSetLayout* set_layouts,
|
||||
uint32_t set_layout_count = 1)
|
||||
{
|
||||
VkPipelineLayoutCreateInfo pipeline_layout_create_info{};
|
||||
pipeline_layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
pipeline_layout_create_info.setLayoutCount = set_layout_count;
|
||||
pipeline_layout_create_info.pSetLayouts = set_layouts;
|
||||
return pipeline_layout_create_info;
|
||||
}
|
||||
|
||||
inline VkDescriptorSetAllocateInfo descriptor_set_allocate_info(
|
||||
VkDescriptorPool descriptor_pool,
|
||||
const VkDescriptorSetLayout* set_layouts,
|
||||
uint32_t descriptor_set_count)
|
||||
{
|
||||
VkDescriptorSetAllocateInfo descriptor_set_allocate_info{};
|
||||
descriptor_set_allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
descriptor_set_allocate_info.descriptorPool = descriptor_pool;
|
||||
descriptor_set_allocate_info.pSetLayouts = set_layouts;
|
||||
descriptor_set_allocate_info.descriptorSetCount = descriptor_set_count;
|
||||
return descriptor_set_allocate_info;
|
||||
}
|
||||
|
||||
VkDescriptorBufferInfo create_descriptor(VkBuffer buffer, VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0)
|
||||
{
|
||||
VkDescriptorBufferInfo descriptor{};
|
||||
descriptor.buffer = buffer;
|
||||
descriptor.range = size;
|
||||
descriptor.offset = offset;
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
inline VkWriteDescriptorSet write_descriptor_set(
|
||||
VkDescriptorSet dst_set,
|
||||
VkDescriptorType type,
|
||||
uint32_t binding,
|
||||
VkDescriptorBufferInfo* buffer_info,
|
||||
uint32_t descriptor_count = 1)
|
||||
{
|
||||
VkWriteDescriptorSet write_descriptor_set{};
|
||||
write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
write_descriptor_set.dstSet = dst_set;
|
||||
write_descriptor_set.descriptorType = type;
|
||||
write_descriptor_set.dstBinding = binding;
|
||||
write_descriptor_set.pBufferInfo = buffer_info;
|
||||
write_descriptor_set.descriptorCount = descriptor_count;
|
||||
return write_descriptor_set;
|
||||
}
|
||||
|
||||
|
||||
inline VkWriteDescriptorSet write_descriptor_set(
|
||||
VkDescriptorSet dst_set,
|
||||
VkDescriptorType type,
|
||||
uint32_t binding,
|
||||
VkDescriptorImageInfo* image_info,
|
||||
uint32_t descriptor_count = 1)
|
||||
{
|
||||
VkWriteDescriptorSet write_descriptor_set{};
|
||||
write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
write_descriptor_set.dstSet = dst_set;
|
||||
write_descriptor_set.descriptorType = type;
|
||||
write_descriptor_set.dstBinding = binding;
|
||||
write_descriptor_set.pImageInfo = image_info;
|
||||
write_descriptor_set.descriptorCount = descriptor_count;
|
||||
return write_descriptor_set;
|
||||
}
|
||||
|
||||
FaceApp* FaceApp::faceIns = nullptr;
|
||||
|
||||
FaceApp::FaceApp(/* args */)
|
||||
@@ -158,10 +251,129 @@ bool FaceApp::LoadOBJ(const std::string& filename,
|
||||
return true;
|
||||
}
|
||||
|
||||
void FaceApp::setup_descriptor_pool()
|
||||
{
|
||||
VkDescriptorPoolSize descriptor_pool_size{};
|
||||
descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptor_pool_size.descriptorCount = 6;
|
||||
|
||||
VkDescriptorPoolSize descriptor_pool_image_size{};
|
||||
descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
descriptor_pool_size.descriptorCount = 6;
|
||||
|
||||
|
||||
std::vector<VkDescriptorPoolSize> pool_sizes;
|
||||
pool_sizes.push_back(descriptor_pool_size);
|
||||
pool_sizes.push_back(descriptor_pool_image_size);
|
||||
|
||||
|
||||
VkDescriptorPoolCreateInfo descriptor_pool_info{};
|
||||
descriptor_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
descriptor_pool_info.poolSizeCount = pool_sizes.size();
|
||||
descriptor_pool_info.pPoolSizes = pool_sizes.data();
|
||||
descriptor_pool_info.maxSets = 6;
|
||||
|
||||
VK_CHECK(vkCreateDescriptorPool(device, &descriptor_pool_info, nullptr, &descriptor_pool));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FaceApp::setup_descriptor_set_layout()
|
||||
{
|
||||
std::vector<VkDescriptorSetLayoutBinding> set_layout_bindings =
|
||||
{
|
||||
// Binding 0 : Vertex shader uniform buffer
|
||||
descriptor_set_layout_binding(
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
0),
|
||||
// Binding 1 : Fragment shader image sampler
|
||||
descriptor_set_layout_binding(
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
1) };
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo descriptor_layout =
|
||||
descriptor_set_layout_create_info(
|
||||
set_layout_bindings.data(),
|
||||
static_cast<uint32_t>(set_layout_bindings.size()));
|
||||
|
||||
VK_CHECK(vkCreateDescriptorSetLayout(device, &descriptor_layout, nullptr, &m_descriptorSetLayout));
|
||||
|
||||
|
||||
VkPipelineLayoutCreateInfo pipeline_layout_create_info =
|
||||
fun_pipeline_layout_create_info(
|
||||
&m_descriptorSetLayout,
|
||||
1);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void FaceApp::setup_descriptor_set()
|
||||
{
|
||||
VkDescriptorSetAllocateInfo alloc_info =
|
||||
descriptor_set_allocate_info(
|
||||
descriptor_pool,
|
||||
&m_descriptorSetLayout,
|
||||
1);
|
||||
|
||||
VK_CHECK(vkAllocateDescriptorSets(device, &alloc_info, &m_descriptor_set));
|
||||
|
||||
VkDescriptorBufferInfo buffer_descriptor = create_descriptor(uniform_buffer_vs);
|
||||
|
||||
|
||||
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.sampler = tex_demo0.sampler; // The sampler (Telling the pipeline how to sample the texture, including repeat, border, etc.)
|
||||
image_descriptor.imageLayout = tex_demo0.image_layout; // The current layout of the image (Note: Should always fit the actual use, e.g. shader read)
|
||||
|
||||
std::vector<VkWriteDescriptorSet> write_descriptor_sets =
|
||||
{
|
||||
// Binding 0 : Vertex shader uniform buffer
|
||||
write_descriptor_set(
|
||||
m_descriptor_set,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&buffer_descriptor),
|
||||
// Binding 1 : Fragment shader texture sampler
|
||||
// Fragment shader: layout (binding = 1) uniform sampler2D samplerColor;
|
||||
write_descriptor_set(
|
||||
m_descriptor_set,
|
||||
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(device, static_cast<uint32_t>(write_descriptor_sets.size()), write_descriptor_sets.data(), 0, NULL);
|
||||
}
|
||||
|
||||
void FaceApp::render(VkCommandBuffer commandBuffer)
|
||||
{
|
||||
Application::render(commandBuffer);
|
||||
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);
|
||||
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
VkBuffer vertexBuffers[] = { m_vertexBuffer };
|
||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
||||
vkCmdBindIndexBuffer(commandBuffer, m_indexBuffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
#ifdef _WIN32
|
||||
vkCmdDrawIndexed(commandBuffer, obj_indices.size(), 1, 0, 0, 0);
|
||||
#else
|
||||
if (getCurrentTimeMillis() - last_update_time < 1000)
|
||||
{
|
||||
vkCmdDrawIndexed(draw_cmd_buffers[i], index_count, 1, 0, 0, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void FaceApp::initVulkan()
|
||||
@@ -172,11 +384,34 @@ void FaceApp::initVulkan()
|
||||
allocatorInfo.device = device;
|
||||
allocatorInfo.instance = instance;
|
||||
vmaCreateAllocator(&allocatorInfo, &allocator);
|
||||
createVertexBuffer();
|
||||
LoadOBJ("face_picture_3dmax.obj", obj_vertices, obj_indices);
|
||||
loadTexture("demo0.png", tex_demo0);
|
||||
createVertexBuffer();
|
||||
createUniformBuffer();
|
||||
setup_descriptor_pool();
|
||||
setup_descriptor_set_layout();
|
||||
setup_descriptor_set();
|
||||
uploadVertexData();
|
||||
}
|
||||
|
||||
void FaceApp::update_uniform_buffers()
|
||||
{
|
||||
uint32_t width = 480;
|
||||
uint32_t height = 480;
|
||||
float zoom = 2;
|
||||
// Vertex shader
|
||||
ubo_vs.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), glm::vec3(0.0f, 0.0f, zoom));
|
||||
|
||||
ubo_vs.model = view_matrix * glm::translate(glm::mat4(1.0f), camera_pos);
|
||||
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
ubo_vs.model = glm::rotate(ubo_vs.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
|
||||
ubo_vs.view_pos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f);
|
||||
|
||||
memcpy(uniform_buffer_mapped, &ubo_vs, sizeof(ubo_vs));
|
||||
}
|
||||
|
||||
void FaceApp::createVertexBuffer()
|
||||
{
|
||||
@@ -296,3 +531,28 @@ void FaceApp::endSingleTimeCommands(VkCommandBuffer commandBuffer) {
|
||||
|
||||
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
||||
}
|
||||
|
||||
void FaceApp::createUniformBuffer()
|
||||
{
|
||||
VkDeviceSize bufferSize = sizeof(ubo_vs);
|
||||
|
||||
VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
||||
bufferInfo.size = bufferSize;
|
||||
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo = {};
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
||||
allocInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
|
||||
// 创建缓冲区和内存分配
|
||||
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo,
|
||||
&uniform_buffer_vs,
|
||||
&uniform_buffer_allocation,
|
||||
nullptr);
|
||||
|
||||
// 映射内存以便直接写入
|
||||
vmaMapMemory(allocator, uniform_buffer_allocation, &uniform_buffer_mapped);
|
||||
|
||||
update_uniform_buffers();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user