-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.cpp
142 lines (111 loc) · 4.34 KB
/
commands.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright (C) 2016 Nigel Williams
*
* Vulkan Free Surface Modeling Engine (VFSME) is free software:
* you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "commands.h"
#include <stdexcept>
#include <iostream>
#include "shared.h"
namespace vfsme
{
Commands::Commands(const VkPhysicalDeviceMemoryProperties& memProps)
: memProperties(memProps)
{
}
void Commands::SetupImage(VkDevice& device, VkImage& image, const VkExtent3D& extent, const VkFormat& format, VkDeviceMemory& memory, VkMemoryPropertyFlags props, VkBufferUsageFlags usage)
{
VkImageCreateInfo imageCreateInfo = {};
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = format;
imageCreateInfo.extent = extent;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.usage = usage;
//imageCreateInfo.flags = 0;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
VkResult result = vkCreateImage(device, &imageCreateInfo, nullptr, &image);
if (result != VK_SUCCESS)
{
throw std::runtime_error("Image creation failed");
}
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(device, image, &memRequirements);
uint32_t imageSize = 0;
uint32_t imageMemTypeIndex = GetMemoryTypeIndex(device, memRequirements, memProperties, props, imageSize);
VkMemoryAllocateInfo memAllocInfo { };
memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memAllocInfo.allocationSize = imageSize;
memAllocInfo.memoryTypeIndex = imageMemTypeIndex;
result = vkAllocateMemory(device, &memAllocInfo, nullptr, &memory);
if (result != VK_SUCCESS)
{
throw std::runtime_error("Memory allocation failed");
}
result = vkBindImageMemory(device, image, memory, 0);
if (result != VK_SUCCESS)
{
throw std::runtime_error("Bind image failed");
}
}
void Commands::SetupBuffer(VkDevice& device, VkBuffer& buffer, VkDeviceMemory& memory, VkDeviceSize size, VkMemoryPropertyFlags properties, VkBufferUsageFlags usage)
{
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkResult result = vkCreateBuffer(device, &bufferInfo, nullptr, &buffer);
if (result != VK_SUCCESS)
{
throw std::runtime_error("Transfer buffer creation failed");
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
uint32_t allocSize;
uint32_t memTypeIndex = GetMemoryTypeIndex(device, memRequirements, memProperties, properties, allocSize);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = allocSize;
allocInfo.memoryTypeIndex = memTypeIndex;
result = vkAllocateMemory(device, &allocInfo, nullptr, &memory);
if (result != VK_SUCCESS)
{
throw std::runtime_error("Buffer allocation failed");
}
vkBindBufferMemory(device, buffer, memory, 0);
}
uint32_t Commands::GetMemoryTypeIndex(VkDevice& device, const VkMemoryRequirements& memRequirements, const VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags propFlags, uint32_t& allocSize) const
{
uint32_t memTypeIndex = vfsme::InvalidIndex;
for (uint32_t i = 0; i < props.memoryTypeCount; ++i)
{
if ((memRequirements.memoryTypeBits & (1 << i)) &&
(props.memoryTypes[i].propertyFlags & propFlags) == propFlags)
{
memTypeIndex = i;
}
}
if (memTypeIndex == vfsme::InvalidIndex)
{
throw std::runtime_error("Memory property combination not supported");
}
allocSize = memRequirements.size;
return memTypeIndex;
}
};