-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpuBufferBuilder.cpp
More file actions
57 lines (46 loc) · 1.78 KB
/
GpuBufferBuilder.cpp
File metadata and controls
57 lines (46 loc) · 1.78 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
#include"GpuBufferBuilder.h"
//gpu上のメモリから設定に適合するメモリを探す
uint32_t GpuBufferBuilder::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
//デバイス上の使用可能なメモリすべてから、設定の適合するメモリを探す
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
{
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties)
== properties)
{
return i;
}
}
throw std::runtime_error("適合するメモリが見つかりませんでした");
}
//public///////////////////////////////////
GpuBufferBuilder::GpuBufferBuilder(VkPhysicalDevice& p, VkDevice& d)
{
physicalDevice = p;
device = d;
}
void GpuBufferBuilder::Create(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties
, VkBuffer& buffer, VkDeviceMemory& memory)
{
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
{
throw std::runtime_error("バッファオブジェクトの作成に失敗しました");
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
if (vkAllocateMemory(device, &allocInfo, nullptr, &memory) != VK_SUCCESS) {
throw std::runtime_error("バッファの作成に失敗しました");
}
vkBindBufferMemory(device, buffer, memory, 0);
}