-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlve_model.cpp
More file actions
67 lines (55 loc) · 2.56 KB
/
lve_model.cpp
File metadata and controls
67 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "lve_model.hpp"
// std
#include <cassert>
#include <cstring>
namespace lve {
LveModel::LveModel(LveDevice& device, const std::vector<Vertex>& vertices) : lveDevice{ device } {
createVertexBuffers(vertices);
}
LveModel::~LveModel() {
vkDestroyBuffer(lveDevice.device(), vertexBuffer, nullptr);
vkFreeMemory(lveDevice.device(), vertexBufferMemory, nullptr);
}
void LveModel::createVertexBuffers(const std::vector<Vertex>& vertices) {
vertexCount = static_cast<uint32_t>(vertices.size());
assert(vertexCount >= 3 && "Vertex count must be at least 3");
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertexCount;
lveDevice.createBuffer(
bufferSize,
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBuffer,
vertexBufferMemory);
void* data;
vkMapMemory(lveDevice.device(), vertexBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, vertices.data(), static_cast<size_t>(bufferSize));
vkUnmapMemory(lveDevice.device(), vertexBufferMemory);
}
void LveModel::draw(VkCommandBuffer commandBuffer) {
vkCmdDraw(commandBuffer, vertexCount, 1, 0, 0);
}
void LveModel::bind(VkCommandBuffer commandBuffer) {
VkBuffer buffers[] = { vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets);
}
std::vector<VkVertexInputBindingDescription> LveModel::Vertex::getBindingDescriptions() {
std::vector<VkVertexInputBindingDescription> bindingDescriptions(1);
bindingDescriptions[0].binding = 0;
bindingDescriptions[0].stride = sizeof(Vertex);
bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescriptions;
}
std::vector<VkVertexInputAttributeDescription> LveModel::Vertex::getAttributeDescriptions() {
std::vector<VkVertexInputAttributeDescription> attributeDescriptions(2);
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, position);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
return attributeDescriptions;
}
} // namespace lve