-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescriptorSetBuilder.cpp
More file actions
202 lines (150 loc) · 5.82 KB
/
DescriptorSetBuilder.cpp
File metadata and controls
202 lines (150 loc) · 5.82 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include"DescriptorSetBuilder.h"
DescriptorSetBuilder::DescriptorSetBuilder(VkDevice& d)
{
device = d;
createDescriptorPool();
}
//VkDescriptorSetPoolの作成
void DescriptorSetBuilder::createDescriptorPool()
{
std::array<VkDescriptorPoolSize, 2> poolSizes{};
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSizes[0].descriptorCount = static_cast<uint32_t>(100 * 2);
poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
poolSizes[1].descriptorCount = static_cast<uint32_t>(100 * 2);
VkDescriptorPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
poolInfo.pPoolSizes = poolSizes.data();
poolInfo.maxSets = static_cast<uint32_t>(1000);
if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
throw std::runtime_error("failed to create descriptor pool!");
}
}
DescriptorSetProperty DescriptorSetBuilder::Build()
{
property.allocateInfo.descriptorSetCount = 1;
property.allocateInfo.pSetLayouts = &property.layout->layout;
return property;
}
void DescriptorSetBuilder::Create(const DescriptorSetProperty& property, VkDescriptorSet& descriptorSet)
{
if (vkAllocateDescriptorSets(device, &property.allocateInfo, &descriptorSet) != VK_SUCCESS)
{
throw std::runtime_error("DescriptorSetBuilder : Create() : VkDescriptorSetの作成に失敗");
}
std::vector<VkWriteDescriptorSet> descriptorWrites(property.buffer.size() + property.texture.size());
for (int i = 0; i < property.buffer.size(); ++i)
{
const auto& bufferProperty = property.buffer[i];
descriptorWrites[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[i].dstSet = descriptorSet;
descriptorWrites[i].dstBinding = bufferProperty.binding;
descriptorWrites[i].dstArrayElement = 0;
descriptorWrites[i].descriptorType = bufferProperty.type;
descriptorWrites[i].descriptorCount = 1;
descriptorWrites[i].pBufferInfo = &bufferProperty.bufferInfo;
}
for (int i = 0; i < property.texture.size(); ++i)
{
const int index = i + static_cast<int>(property.buffer.size());
const auto& imageProperty = property.texture[i];
descriptorWrites[index].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[index].dstSet = descriptorSet;
descriptorWrites[index].dstBinding = imageProperty.binding;
descriptorWrites[index].dstArrayElement = 0;
descriptorWrites[index].descriptorType = imageProperty.type;
descriptorWrites[index].descriptorCount = 1;
descriptorWrites[index].pImageInfo = &imageProperty.imageInfo;
}
vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}
//プロパティの初期化
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::initProperty()
{
bufferProperty.initProperty();
imageProperty.initProperty();
property.initProperty(descriptorPool);
return shared_from_this();
}
//VkDescriptorSetLayoutの設定
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withDescriptorSetLayout(const std::shared_ptr<DescriptorSetLayout>& layout)
{
property.layout = layout;
return shared_from_this();
}
//VkDescriptorSetの個数の設定
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withDescriptorSetCount(const uint32_t& count)
{
property.allocateInfo.descriptorSetCount = count;
return shared_from_this();
}
//VkDescriptorBufferInfoの設定///////
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withBindingBuffer(const uint32_t& binding)
{
bufferProperty.binding = binding;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withTypeBuffer(const VkDescriptorType& type)
{
bufferProperty.type = type;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withBuffer(const std::shared_ptr<GpuBuffer>& buffer)
{
bufferProperty.buffer = buffer;
bufferProperty.bufferInfo.buffer = buffer->buffer;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withOffset(const uint32_t& offset)
{
bufferProperty.bufferInfo.offset = offset;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withRange(const uint32_t& range)
{
bufferProperty.bufferInfo.range = range;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::addBufferInfo()
{
property.buffer.push_back(bufferProperty);
bufferProperty.initProperty();
return shared_from_this();
}
//VkDescriptorImageInfo/////////
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withBindingImage(const uint32_t& binding)
{
imageProperty.binding = binding;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withTypeImage(const VkDescriptorType& type)
{
imageProperty.type = type;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withImageLayout(const VkImageLayout& layout)
{
imageProperty.imageInfo.imageLayout = layout;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withTexture(const std::shared_ptr<Texture> texture)
{
imageProperty.texture = texture;
imageProperty.imageInfo.imageView = texture->viewArray[0];
imageProperty.imageInfo.sampler = texture->sampler;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::withTexture(const std::shared_ptr<Texture> texture, const uint32_t targetLayer)
{
imageProperty.texture = texture;
imageProperty.imageInfo.imageView = texture->viewArray[targetLayer];
imageProperty.imageInfo.sampler = texture->sampler;
return shared_from_this();
}
std::shared_ptr<DescriptorSetBuilder> DescriptorSetBuilder::addImageInfo()
{
property.texture.push_back(imageProperty);
imageProperty.initProperty();
return shared_from_this();
}