Changeset cae7a2c in opengl-game
- Timestamp:
- Jul 24, 2019, 2:59:33 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- de32fda
- Parents:
- d9ef6ab
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rd9ef6ab rcae7a2c 1 /* 2 DESIGN GUIDE 3 4 I should store multiple buffers (e.g. vertex and index buffers) in the same VkBuffer and use offsets into it 5 */ 6 1 7 #include "game-gui-glfw.hpp" 2 8 … … 85 91 86 92 const vector<Vertex> vertices = { 87 {{ 0.0f, -0.5f}, {1.0f, 1.0f, 1.0f}}, 88 {{ 0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}}, 89 {{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}} 93 {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}}, 94 {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}}, 95 {{ 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}, 96 {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}} 97 }; 98 99 const vector<uint16_t> indices = { 100 0, 1, 2, 2, 3, 0 90 101 }; 91 102 … … 153 164 VkBuffer vertexBuffer; 154 165 VkDeviceMemory vertexBufferMemory; 166 VkBuffer indexBuffer; 167 VkDeviceMemory indexBufferMemory; 155 168 156 169 vector<VkFramebuffer> swapChainFramebuffers; … … 194 207 createCommandPool(); 195 208 createVertexBuffer(); 209 createIndexBuffer(); 196 210 createCommandBuffers(); 197 211 createSyncObjects(); … … 879 893 } 880 894 895 void createIndexBuffer() { 896 VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size(); 897 898 VkBuffer stagingBuffer; 899 VkDeviceMemory stagingBufferMemory; 900 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 901 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 902 stagingBuffer, stagingBufferMemory); 903 904 void* data; 905 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 906 memcpy(data, indices.data(), (size_t)bufferSize); 907 vkUnmapMemory(device, stagingBufferMemory); 908 909 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 910 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory); 911 912 copyBuffer(stagingBuffer, indexBuffer, bufferSize); 913 914 vkDestroyBuffer(device, stagingBuffer, nullptr); 915 vkFreeMemory(device, stagingBufferMemory, nullptr); 916 } 917 881 918 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, 882 919 VkBuffer& buffer, VkDeviceMemory& bufferMemory) { … … 996 1033 vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets); 997 1034 998 vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertices.size()), 1, 0, 0); 1035 vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16); 1036 1037 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0); 999 1038 vkCmdEndRenderPass(commandBuffers[i]); 1000 1039 … … 1124 1163 cleanupSwapChain(); 1125 1164 1165 vkDestroyBuffer(device, indexBuffer, nullptr); 1166 vkFreeMemory(device, indexBufferMemory, nullptr); 1167 1126 1168 vkDestroyBuffer(device, vertexBuffer, nullptr); 1127 1169 vkFreeMemory(device, vertexBufferMemory, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.