Skip to content

Instantly share code, notes, and snippets.

@quad-bit
Created January 14, 2021 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quad-bit/c241cd454d0dfc6bb400d17d0ef8dc34 to your computer and use it in GitHub Desktop.
Save quad-bit/c241cd454d0dfc6bb400d17d0ef8dc34 to your computer and use it in GitHub Desktop.
multiple descriptor sets, single UBO, single memory
// descriptorSets : CONCURRENT_FRAMES
// uniformBuffer : 1
// uniformBufferMemory : 1
/////////////////// buffer creation, memory allocation, begins
size_t dataSize = sizeof(std::array<glm::mat4, 3>);
// Describe a new buffer:
auto createInfo = vk::BufferCreateInfo{}
.setSize(static_cast<vk::DeviceSize>(dataSize) * CONCURRENT_FRAMES)
.setUsage(vk::BufferUsageFlagBits::eUniformBuffer);
uniformBuffer = device.createBuffer(createInfo);
vk::MemoryRequirements memReq;
memReq = device.getBufferMemoryRequirements(uniformBuffer);
{
auto memoryAllocInfo = vk::MemoryAllocateInfo{}
.setAllocationSize(memReq.size)
.setMemoryTypeIndex([&]() {
// Get memory types supported by the physical device:
auto memoryProperties = physicalDevice.getMemoryProperties();
// In search for a suitable memory type INDEX:
for (uint32_t i = 0u; i < memoryProperties.memoryTypeCount; ++i) {
// Is this kind of memory suitable for our buffer?
const auto bitmask = memReq.memoryTypeBits;
const auto bit = 1 << i;
if (0 == (bitmask & bit)) {
continue; // => nope
}
// Does this kind of memory support our usage requirements?
if ((memoryProperties.memoryTypes[i].propertyFlags & (vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent))
!= vk::MemoryPropertyFlags{}) {
// Return the INDEX of a suitable memory type
return i;
}
}
throw std::runtime_error("Couldn't find suitable memory.");
}());
// Allocate:
uniformBufferMemory = device.allocateMemory(memoryAllocInfo);
}
// Bind the buffer handle to the memory:
device.bindBufferMemory(uniformBuffer, uniformBufferMemory, 0);
/////////////////// buffer creation, memory allocation, ends
/////////////////// descriptor set update write, begins
for (uint32_t i = 0; i < CONCURRENT_FRAMES; i++)
{
auto uniformBufferInfo = vk::DescriptorBufferInfo{}.setBuffer(uniformBuffer).setOffset(dataSize * i).setRange(dataSize);
std::array<vk::WriteDescriptorSet, 1> writes{
vk::WriteDescriptorSet{}
.setDstSet(descriptorSets[i])
.setDstBinding(0u)
.setDescriptorType(vk::DescriptorType::eUniformBuffer)
.setDescriptorCount(1u).setPBufferInfo(&uniformBufferInfo)
};
// And perform the actual write:
device.updateDescriptorSets(static_cast<uint32_t>(writes.size()), writes.data(), 0u, nullptr);
// It's done. Descriptors are written to the GPU. We can use them now in shaders.
}
/////////////////// descriptor set update write, ends
/////////////////// uniform data write, begins
auto clearColorMappedMemory = device.mapMemory(uniformBufferMemory, dataSize * frameInFlightIndex, dataSize);
memcpy(clearColorMappedMemory, matrices.data(), dataSize);
device.unmapMemory(uniformBufferMemory);
/////////////////// uniform data write, ends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment