From b19c7666b2bff4ba22ce3f755fc8e27f04fdf120 Mon Sep 17 00:00:00 2001 From: InkSoul Date: Thu, 14 Mar 2024 14:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=A1=B6=E7=82=B9=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VulkanTutorial.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ VulkanTutorial.h | 9 ++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/VulkanTutorial.cpp b/VulkanTutorial.cpp index f4b102a..256d0b6 100644 --- a/VulkanTutorial.cpp +++ b/VulkanTutorial.cpp @@ -121,6 +121,8 @@ void HelloTriangleApplication::initVulkan() { createCommandPool(); + createVertexBuffer(); + createCommandBuffer(); createSyncObjects(); @@ -149,6 +151,9 @@ void HelloTriangleApplication::cleanup(GLFWwindow* window) { cleanupSwapChain(); + vkDestroyBuffer(device, vertexBuffer, nullptr); + vkFreeMemory(device, vertexBufferMemory, nullptr); + vkDestroyPipeline(device, graphicsPipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, 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() { commandBuffers.resize(MAX_FRAME_IN_FLIGHT); @@ -761,6 +816,10 @@ void HelloTriangleApplication::recordCommandBuffer(VkCommandBuffer commandBuffer scissor.offset = { 0,0 }; scissor.extent = swapChainExtent; vkCmdSetScissor(commandBuffer, 0, 1, &scissor); + // bindVertexBuffer + VkBuffer vertexBuffers[] = { vertexBuffer }; + VkDeviceSize offsets[] = { 0 }; + vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); vkCmdDraw(commandBuffer, 3, 1, 0, 0); diff --git a/VulkanTutorial.h b/VulkanTutorial.h index 36718b6..19839eb 100644 --- a/VulkanTutorial.h +++ b/VulkanTutorial.h @@ -112,11 +112,14 @@ private: }; const std::vector 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, 0.0f, 1.0f}} }; + VkBuffer vertexBuffer; + VkDeviceMemory vertexBufferMemory; + void initWindow(int Width, int Height); void createInstance(); @@ -175,6 +178,10 @@ private: void createCommandPool(); + void createVertexBuffer(); + + uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); + void createCommandBuffer(); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);