完成顶点缓存创建
parent
7e7f530f3f
commit
b19c7666b2
|
@ -121,6 +121,8 @@ void HelloTriangleApplication::initVulkan() {
|
||||||
|
|
||||||
createCommandPool();
|
createCommandPool();
|
||||||
|
|
||||||
|
createVertexBuffer();
|
||||||
|
|
||||||
createCommandBuffer();
|
createCommandBuffer();
|
||||||
|
|
||||||
createSyncObjects();
|
createSyncObjects();
|
||||||
|
@ -149,6 +151,9 @@ void HelloTriangleApplication::cleanup(GLFWwindow* window) {
|
||||||
|
|
||||||
cleanupSwapChain();
|
cleanupSwapChain();
|
||||||
|
|
||||||
|
vkDestroyBuffer(device, vertexBuffer, nullptr);
|
||||||
|
vkFreeMemory(device, vertexBufferMemory, 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);
|
||||||
|
@ -707,6 +712,56 @@ void HelloTriangleApplication::createCommandPool()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HelloTriangleApplication::createVertexBuffer()
|
||||||
|
{
|
||||||
|
VkBufferCreateInfo bufferInfo{};
|
||||||
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
|
bufferInfo.size = sizeof(vertices[0]) * vertices.size();
|
||||||
|
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||||||
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
|
if (vkCreateBuffer(device,&bufferInfo,nullptr,&vertexBuffer) != VK_SUCCESS)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("failed to create vertex buffer in createVertexBuffer");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryRequirements memRequirement;
|
||||||
|
vkGetBufferMemoryRequirements(device, vertexBuffer, &memRequirement);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo{};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirement.size;
|
||||||
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirement.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||||
|
|
||||||
|
if (vkAllocateMemory(device,&allocInfo,nullptr,&vertexBufferMemory) != VK_SUCCESS)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("failed to allocate vertex buffer memory!");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);
|
||||||
|
|
||||||
|
void* data;
|
||||||
|
vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data);
|
||||||
|
memcpy(data, vertices.data(), (size_t)bufferInfo.size);
|
||||||
|
vkUnmapMemory(device, vertexBufferMemory);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t HelloTriangleApplication::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
||||||
|
{
|
||||||
|
VkPhysicalDeviceMemoryProperties memProperties;
|
||||||
|
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
|
||||||
|
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
|
||||||
|
{
|
||||||
|
if (typeFilter & (1 << i) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw std::runtime_error("failed to find suitable memory type !");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void HelloTriangleApplication::createCommandBuffer()
|
void HelloTriangleApplication::createCommandBuffer()
|
||||||
{
|
{
|
||||||
commandBuffers.resize(MAX_FRAME_IN_FLIGHT);
|
commandBuffers.resize(MAX_FRAME_IN_FLIGHT);
|
||||||
|
@ -761,6 +816,10 @@ void HelloTriangleApplication::recordCommandBuffer(VkCommandBuffer commandBuffer
|
||||||
scissor.offset = { 0,0 };
|
scissor.offset = { 0,0 };
|
||||||
scissor.extent = swapChainExtent;
|
scissor.extent = swapChainExtent;
|
||||||
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||||
|
// bindVertexBuffer
|
||||||
|
VkBuffer vertexBuffers[] = { vertexBuffer };
|
||||||
|
VkDeviceSize offsets[] = { 0 };
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
||||||
|
|
||||||
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
|
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
|
||||||
|
|
||||||
|
|
|
@ -112,11 +112,14 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::vector<Vertex> vertices = {
|
const std::vector<Vertex> vertices = {
|
||||||
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
|
{{0.0f, -0.5f}, {1.0f, 1.0f, 1.0f}},
|
||||||
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
|
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||||
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
|
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
VkBuffer vertexBuffer;
|
||||||
|
VkDeviceMemory vertexBufferMemory;
|
||||||
|
|
||||||
void initWindow(int Width, int Height);
|
void initWindow(int Width, int Height);
|
||||||
|
|
||||||
void createInstance();
|
void createInstance();
|
||||||
|
@ -175,6 +178,10 @@ private:
|
||||||
|
|
||||||
void createCommandPool();
|
void createCommandPool();
|
||||||
|
|
||||||
|
void createVertexBuffer();
|
||||||
|
|
||||||
|
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||||
|
|
||||||
void createCommandBuffer();
|
void createCommandBuffer();
|
||||||
|
|
||||||
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
||||||
|
|
Loading…
Reference in New Issue