Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
VulkanDescriptorPool.cpp
Go to the documentation of this file.
2 #ifdef USING_VULKAN
3 
4 #include <iostream>
5 
7 #include <ThirdParty/EASTL-master/include/EASTL/string.h>
8 
9 namespace Engine {
10 
11  VulkanDescriptorPool::VulkanDescriptorPool(VulkanLogicalDevice* device)
12  {
13  this->device = device;
14  }
15 
16  VulkanDescriptorPool::~VulkanDescriptorPool()
17  {
18  if (pool != 0)
19  vkDestroyDescriptorPool(device->GetDevice(), pool, nullptr);
20  }
21 
22  void VulkanDescriptorPool::AddPoolSize(VkDescriptorType type, uint32_t count)
23  {
24  VkDescriptorPoolSize size = {};
25  size.type = type;
26  size.descriptorCount = count;
27 
28  poolSizes.push_back(size);
29  }
30 
31  void VulkanDescriptorPool::Compile(uint32_t maxSets)
32  {
33  VkDescriptorPoolCreateInfo createInfo = {};
34  createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
35  createInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
36  createInfo.pPoolSizes = poolSizes.data();
37  createInfo.maxSets = maxSets;
38  createInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
39 
40  VkResult res = vkCreateDescriptorPool(device->GetDevice(), &createInfo, nullptr, &pool);
41  if (res != VK_SUCCESS) {
42  eastl::string s = eastl::string("[ERROR] [CODE:") + std::to_string(res).c_str() + "] Failed to create descriptor pool";
43  std::cout << s.c_str() << std::endl;
44  }
45  }
46 
47  VkDescriptorPool VulkanDescriptorPool::GetPool() const
48  {
49  return pool;
50  }
51 
52  void VulkanDescriptorPool::AllocateDescriptorSet(uint32_t descriptorSetCount, VkDescriptorSetLayout * layouts, VkDescriptorSet * sets) const
53  {
54  VkDescriptorSetAllocateInfo allocInfo = {};
55  allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
56  allocInfo.descriptorPool = pool;
57  allocInfo.descriptorSetCount = descriptorSetCount;
58  allocInfo.pSetLayouts = layouts;
59 
60  VkResult res = vkAllocateDescriptorSets(device->GetDevice(), &allocInfo, sets);
61  if (res != VK_SUCCESS) {
62  eastl::string s = eastl::string("[ERROR] [CODE:") + std::to_string(res).c_str() + "] Failed to allocate descriptor sets";
63  std::cout << s.c_str() << std::endl;
64  }
65  }
66 
67  void VulkanDescriptorPool::DescriptorSetBindToBuffer(VkDescriptorSet set, VkBuffer buffer, VkDeviceSize offset,
68  VkDeviceSize range, uint32_t binding, uint32_t arrayElement,
69  VkDescriptorType type, uint32_t descriptorCount) const
70  {
71  VkDescriptorBufferInfo bufferInfo = {};
72  bufferInfo.buffer = buffer;
73  bufferInfo.offset = offset;
74  bufferInfo.range = range;
75  VkWriteDescriptorSet descriptorWrite = {};
76  descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
77  descriptorWrite.dstSet = set;
78  descriptorWrite.dstBinding = binding;
79  descriptorWrite.dstArrayElement = arrayElement;
80  descriptorWrite.descriptorType = type;
81  descriptorWrite.descriptorCount = descriptorCount;
82  descriptorWrite.pBufferInfo = &bufferInfo;
83 
84  vkUpdateDescriptorSets(device->GetDevice(), 1, &descriptorWrite, 0, nullptr);
85  }
86 
87  void VulkanDescriptorPool::DescriptorSetBindToImage(VkDescriptorSet set, VkImageLayout layout, VkImageView imageView, VkSampler sampler, uint32_t binding, uint32_t arrayElement, VkDescriptorType type, uint32_t descriptorCount) const
88  {
89  VkDescriptorImageInfo imageInfo = {};
90  imageInfo.imageLayout = layout;
91  imageInfo.imageView = imageView;
92  imageInfo.sampler = sampler;
93 
94  VkWriteDescriptorSet writeInfo = {};
95  writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
96  writeInfo.dstSet = set;
97  writeInfo.dstBinding = binding;
98  writeInfo.dstArrayElement = arrayElement;
99  writeInfo.descriptorType = type;
100  writeInfo.descriptorCount = descriptorCount;
101  writeInfo.pImageInfo = &imageInfo;
102 
103  vkUpdateDescriptorSets(device->GetDevice(), 1, &writeInfo, 0, nullptr);
104  }
105 
106 }
107 
108 #endif // USING_VULKAN