18 VkBuffer stagingBuffer;
19 VkDeviceMemory stagingBufferMemory;
20 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
21 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer,
25 vkMapMemory(
device, stagingBufferMemory, 0, bufferSize, 0, &data);
26 memcpy(data,
vertices.data(), (
size_t) bufferSize);
27 vkUnmapMemory(
device, stagingBufferMemory);
29 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
34 vkDestroyBuffer(
device, stagingBuffer,
nullptr);
35 vkFreeMemory(
device, stagingBufferMemory,
nullptr);
41 VkBuffer stagingBuffer;
42 VkDeviceMemory stagingBufferMemory;
43 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
44 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer,
48 vkMapMemory(
device, stagingBufferMemory, 0, bufferSize, 0, &data);
49 memcpy(data,
vertices.data(), (
size_t) bufferSize);
50 vkUnmapMemory(
device, stagingBufferMemory);
54 vkDestroyBuffer(
device, stagingBuffer,
nullptr);
55 vkFreeMemory(
device, stagingBufferMemory,
nullptr);
59 VkPhysicalDeviceMemoryProperties memProperties;
60 vkGetPhysicalDeviceMemoryProperties(
physicalDevice, &memProperties);
62 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
63 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
68 throw std::runtime_error(
"failed to find suitable memory type!");
72 VkBuffer &buffer, VkDeviceMemory &bufferMemory) {
73 VkBufferCreateInfo bufferInfo{};
74 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
75 bufferInfo.size = size;
76 bufferInfo.usage = usage;
77 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
79 if (vkCreateBuffer(
device, &bufferInfo,
nullptr, &buffer) != VK_SUCCESS) {
80 throw std::runtime_error(
"Failed to create buffer !");
83 VkMemoryRequirements memRequirements;
84 vkGetBufferMemoryRequirements(
device, buffer, &memRequirements);
86 VkMemoryAllocateInfo allocInfo{};
87 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
88 allocInfo.allocationSize = memRequirements.size;
89 allocInfo.memoryTypeIndex =
findMemoryType(memRequirements.memoryTypeBits, properties);
91 if (vkAllocateMemory(
device, &allocInfo,
nullptr, &bufferMemory) != VK_SUCCESS) {
92 throw std::runtime_error(
"Failed to allocate buffer memory !");
95 vkBindBufferMemory(
device, buffer, bufferMemory, 0);
101 VkBufferCopy copyRegion{};
102 copyRegion.size = size;
103 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region);
109 for (
size_t i = 0; i <
vertices.size(); ++i) {
113 vertex.original_color = vertex.color;
115 size_t triangleIndex = i / 3;
119 if (triangleIndex % 6 < 2) {
120 vertex.color =
vec3(0.0f, 0.0f, 0.0f) + col_shift;
121 }
else if (triangleIndex % 6 == 2) {
122 vertex.color =
vec3(0.1f, 0.1f, 0.1f) + col_shift;
123 }
else if (triangleIndex % 6 == 3) {
124 vertex.color =
vec3(0.3f, 0.3f, 0.3f) + col_shift;
125 }
else if (triangleIndex % 6 == 4) {
126 vertex.color =
vec3(0.5f, 0.5f, 0.5f) + col_shift;
128 vertex.color =
vec3(0.7f, 0.7f, 0.7f) + col_shift;