SimpleGL  1.1.0
A framework for platform independent rendering
descriptor_set.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 
13 
14 using namespace std;
15 using namespace glm;
16 
17 namespace sgl {
18 
19  DescriptorPool::DescriptorPool(void) {
20  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
21 
22  /* Initialize a descriptor pool size */
23  vector<VkDescriptorPoolSize> poolSizes(2);
24  poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
25  poolSizes[0].descriptorCount = 1;
26  poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
27  poolSizes[1].descriptorCount = 1;
28 
29  /* Initialize a descriptor pool create info */
30  VkDescriptorPoolCreateInfo createInfo = {};
31  createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
32  createInfo.maxSets = 1;
33  createInfo.poolSizeCount = poolSizes.size();
34  createInfo.pPoolSizes = poolSizes.data();
35 
36  /* Create the descriptor pool */
37  if (vkCreateDescriptorPool(device, &createInfo, nullptr, &handle) != VK_SUCCESS) {
38  throw runtime_error("Could not create a descriptor pool!");
39  }
40  }
41 
42  DescriptorPool::~DescriptorPool(void) {
43  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
44 
45  /* Destroy the descriptor pool */
46  vkDestroyDescriptorPool(device, handle, nullptr);
47  }
48 
49  VkDescriptorPool DescriptorPool::getHandle() {
50  return handle;
51  }
52 
53  DescriptorSet::DescriptorSet(GraphicsPipeline* graphicsPipeline) {
54  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
55  VkDescriptorPool descriptorPool = VulkanContext::getCurrent()->getDescriptorPool()->getHandle();
56  VkDescriptorSetLayout layout = graphicsPipeline->getDescriptorSetLayout();
57 
58  /* Initialize a descriptor set allocate info */
59  VkDescriptorSetAllocateInfo allocateInfo = {};
60  allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
61  allocateInfo.descriptorPool = descriptorPool;
62  allocateInfo.descriptorSetCount = 1;
63  allocateInfo.pSetLayouts = &layout;
64 
65  /* Allocate the descriptor set */
66  if (vkAllocateDescriptorSets(device, &allocateInfo, &handle) != VK_SUCCESS) {
67  throw runtime_error("Could not allocate descriptor set!");
68  }
69  }
70 
71  DescriptorSet::~DescriptorSet(void) {
72  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
73  VkDescriptorPool descriptorPool = VulkanContext::getCurrent()->getDescriptorPool()->getHandle();
74 
75  /* Free the descriptor set */
76  vkFreeDescriptorSets(device, descriptorPool, 1, &handle);
77  }
78 
79  VkDescriptorSet DescriptorSet::getHandle() {
80  return handle;
81  }
82 
83  void DescriptorSet::update(UniformBuffer* uniformBuffer, std::vector<glm::mat2> data) {
84  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
85 
86  /* Initialize a descriptor buffer info */
87  VkDescriptorBufferInfo bufferInfo = {};
88  bufferInfo.buffer = uniformBuffer->getHandle();
89  bufferInfo.offset = 0;
90  bufferInfo.range = data.size() * sizeof (mat2);
91 
92  /* Initialize a write descriptor set */
93  VkWriteDescriptorSet descriptorWrite = {};
94  descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
95  descriptorWrite.dstSet = handle;
96  descriptorWrite.dstBinding = 0;
97  descriptorWrite.dstArrayElement = 0;
98  descriptorWrite.descriptorCount = 1;
99  descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
100  descriptorWrite.pBufferInfo = &bufferInfo;
101 
102  /* Update the descriptor set */
103  vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
104  }
105 
106  void DescriptorSet::update(UniformBuffer* uniformBuffer, std::vector<glm::mat3> data) {
107  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
108 
109  /* Initialize a descriptor buffer info */
110  VkDescriptorBufferInfo bufferInfo = {};
111  bufferInfo.buffer = uniformBuffer->getHandle();
112  bufferInfo.offset = 0;
113  bufferInfo.range = data.size() * sizeof (mat3);
114 
115  /* Initialize a write descriptor set */
116  VkWriteDescriptorSet descriptorWrite = {};
117  descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
118  descriptorWrite.dstSet = handle;
119  descriptorWrite.dstBinding = 0;
120  descriptorWrite.dstArrayElement = 0;
121  descriptorWrite.descriptorCount = 1;
122  descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
123  descriptorWrite.pBufferInfo = &bufferInfo;
124 
125  /* Update the descriptor set */
126  vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
127  }
128 
129  void DescriptorSet::update(UniformBuffer* uniformBuffer, std::vector<glm::mat4> data) {
130  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
131 
132  /* Initialize a descriptor buffer info */
133  VkDescriptorBufferInfo bufferInfo = {};
134  bufferInfo.buffer = uniformBuffer->getHandle();
135  bufferInfo.offset = 0;
136  bufferInfo.range = data.size() * sizeof (mat4);
137 
138  /* Initialize a write descriptor set */
139  VkWriteDescriptorSet descriptorWrite = {};
140  descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
141  descriptorWrite.dstSet = handle;
142  descriptorWrite.dstBinding = 0;
143  descriptorWrite.dstArrayElement = 0;
144  descriptorWrite.descriptorCount = 1;
145  descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
146  descriptorWrite.pBufferInfo = &bufferInfo;
147 
148  /* Update the descriptor set */
149  vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
150  }
151 
152  void DescriptorSet::update(TextureImage* textureImage) {
153  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
154 
155  /* Initialize a descriptor image info */
156  VkDescriptorImageInfo imageInfo = {};
157  imageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
158  imageInfo.imageView = textureImage->getImageView();
159  imageInfo.sampler = textureImage->getSampler();
160 
161  /* Initialize a write descriptor set */
162  VkWriteDescriptorSet descriptorWrite = {};
163  descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
164  descriptorWrite.dstSet = handle;
165  descriptorWrite.dstBinding = 1;
166  descriptorWrite.dstArrayElement = 0;
167  descriptorWrite.descriptorCount = 1;
168  descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
169  descriptorWrite.pImageInfo = &imageInfo;
170 
171  /* Update the descriptor set */
172  vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
173  }
174 }
VkSampler getSampler()
Returns the texture sampler of the image.
VkDescriptorSetLayout getDescriptorSetLayout()
Returns the descriptor set layout of the graphics pipeline.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
VkBuffer getHandle()
Returns the handle of the uniform buffer.
This class wraps a Vulkan texture image.
This class wraps a Vulkan graphics pipeline.
This class wraps a Vulkan uniform buffer.
VkImageView getImageView()
Returns the image view of the texture image.