scop
a small 3D object loader
Loading...
Searching...
No Matches
VulkanVertex.cpp
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* VulkanVertex.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rbourgea <rbourgea@student.42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/01/12 06:00:31 by rbourgea #+# #+# */
9/* Updated: 2024/01/18 07:46:16 by rbourgea ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "VulkanApp.hpp"
14
16 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
17
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,
22 stagingBufferMemory);
23
24 void *data;
25 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
26 memcpy(data, vertices.data(), (size_t) bufferSize);
27 vkUnmapMemory(device, stagingBufferMemory);
28
29 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
30 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
31
32 copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
33
34 vkDestroyBuffer(device, stagingBuffer, nullptr);
35 vkFreeMemory(device, stagingBufferMemory, nullptr);
36}
37
39 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
40
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,
45 stagingBufferMemory);
46
47 void *data;
48 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
49 memcpy(data, vertices.data(), (size_t) bufferSize);
50 vkUnmapMemory(device, stagingBufferMemory);
51
52 copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
53
54 vkDestroyBuffer(device, stagingBuffer, nullptr);
55 vkFreeMemory(device, stagingBufferMemory, nullptr);
56}
57
58uint32_t VulkanApp::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
59 VkPhysicalDeviceMemoryProperties memProperties;
60 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
61
62 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
63 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
64 return i;
65 }
66 }
67
68 throw std::runtime_error("failed to find suitable memory type!");
69}
70
71void VulkanApp::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
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;
78
79 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
80 throw std::runtime_error("Failed to create buffer !");
81 }
82
83 VkMemoryRequirements memRequirements;
84 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
85
86 VkMemoryAllocateInfo allocInfo{};
87 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
88 allocInfo.allocationSize = memRequirements.size;
89 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
90
91 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
92 throw std::runtime_error("Failed to allocate buffer memory !");
93 }
94
95 vkBindBufferMemory(device, buffer, bufferMemory, 0);
96}
97
98void VulkanApp::copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
99 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
100
101 VkBufferCopy copyRegion{};
102 copyRegion.size = size;
103 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
104
105 endSingleTimeCommands(commandBuffer);
106}
107
109 for (size_t i = 0; i < vertices.size(); ++i) {
110 auto &vertex = vertices[i];
111
112 if (colorMode == NONE)
113 vertex.original_color = vertex.color;
114
115 size_t triangleIndex = i / 3;
116
117 vec3 col_shift = disable_textures ? vec3(0.2f, 0.2f, 0.2f) : vec3(0.0f, 0.0f, 0.0f);
118
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;
127 } else {
128 vertex.color = vec3(0.7f, 0.7f, 0.7f) + col_shift;
129 }
130 }
131 colorMode = DARK;
132}
VkDevice device
void createVertexBuffer()
void updateVertexBuffer()
VkPhysicalDevice physicalDevice
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size)
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
ColorMode colorMode
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
VkBuffer vertexBuffer
void enableDarkMode()
VkCommandBuffer beginSingleTimeCommands()
VkDeviceMemory vertexBufferMemory
void endSingleTimeCommands(VkCommandBuffer commandBuffer)
std::vector< Vertex > vertices
bool disable_textures
Definition Math.hpp:65