Changeset d9ef6ab in opengl-game
- Timestamp:
- Jul 23, 2019, 10:14:19 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- cae7a2c
- Parents:
- 80edd70
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
r80edd70 rd9ef6ab 857 857 858 858 void createVertexBuffer() { 859 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size(); 860 861 VkBuffer stagingBuffer; 862 VkDeviceMemory stagingBufferMemory; 863 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 864 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 865 stagingBuffer, stagingBufferMemory); 866 867 void* data; 868 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 869 memcpy(data, vertices.data(), (size_t)bufferSize); 870 vkUnmapMemory(device, stagingBufferMemory); 871 872 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 873 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory); 874 875 copyBuffer(stagingBuffer, vertexBuffer, bufferSize); 876 877 vkDestroyBuffer(device, stagingBuffer, nullptr); 878 vkFreeMemory(device, stagingBufferMemory, nullptr); 879 } 880 881 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, 882 VkBuffer& buffer, VkDeviceMemory& bufferMemory) { 859 883 VkBufferCreateInfo bufferInfo = {}; 860 884 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; 861 bufferInfo.size = size of(vertices[0]) * vertices.size();862 bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;885 bufferInfo.size = size; 886 bufferInfo.usage = usage; 863 887 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; 864 888 865 if (vkCreateBuffer(device, &bufferInfo, nullptr, & vertexBuffer) != VK_SUCCESS) {866 throw runtime_error("failed to create vertexbuffer!");867 } 868 869 VkMemoryRequirements mem oryRequirements;870 vkGetBufferMemoryRequirements(device, vertexBuffer, &memoryRequirements);889 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) { 890 throw runtime_error("failed to create buffer!"); 891 } 892 893 VkMemoryRequirements memRequirements; 894 vkGetBufferMemoryRequirements(device, buffer, &memRequirements); 871 895 872 896 VkMemoryAllocateInfo allocInfo = {}; 873 897 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; 874 allocInfo.allocationSize = memoryRequirements.size; 875 allocInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); 876 877 if (vkAllocateMemory(device, &allocInfo, nullptr, &vertexBufferMemory) != VK_SUCCESS) { 878 throw runtime_error("failed to allocate vertex buffer memory!"); 879 } 880 881 vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0); 882 883 void* data; 884 vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data); 885 memcpy(data, vertices.data(), (size_t)bufferInfo.size); 886 vkUnmapMemory(device, vertexBufferMemory); 898 allocInfo.allocationSize = memRequirements.size; 899 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties); 900 901 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) { 902 throw runtime_error("failed to allocate buffer memory!"); 903 } 904 905 vkBindBufferMemory(device, buffer, bufferMemory, 0); 906 } 907 908 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) { 909 VkCommandBufferAllocateInfo allocInfo = {}; 910 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; 911 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 912 allocInfo.commandPool = commandPool; 913 allocInfo.commandBufferCount = 1; 914 915 VkCommandBuffer commandBuffer; 916 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer); 917 918 VkCommandBufferBeginInfo beginInfo = {}; 919 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 920 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 921 922 vkBeginCommandBuffer(commandBuffer, &beginInfo); 923 924 VkBufferCopy copyRegion = {}; 925 copyRegion.srcOffset = 0; 926 copyRegion.dstOffset = 0; 927 copyRegion.size = size; 928 929 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 930 931 vkEndCommandBuffer(commandBuffer); 932 933 VkSubmitInfo submitInfo = {}; 934 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 935 submitInfo.commandBufferCount = 1; 936 submitInfo.pCommandBuffers = &commandBuffer; 937 938 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); 939 vkQueueWaitIdle(graphicsQueue); 940 941 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer); 887 942 } 888 943
Note:
See TracChangeset
for help on using the changeset viewer.