-
-
Save quad-bit/e3f5fa30f2012beb3dd2816d894b990f to your computer and use it in GitHub Desktop.
multiple descriptorset, multiple UBOS, single memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// descriptorSets : CONCURRENT_FRAMES | |
// uniformBuffer : CONCURRENT_FRAMES | |
// 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)) | |
.setUsage(vk::BufferUsageFlagBits::eUniformBuffer); | |
for(uint32_t i = 0; i < CONCURRENT_FRAMES; i++) | |
uniformBuffer[i] = device.createBuffer(createInfo); | |
vk::MemoryRequirements memReq; | |
memReq = device.getBufferMemoryRequirements(uniformBuffer[0]); | |
{ | |
auto memoryAllocInfo = vk::MemoryAllocateInfo{} | |
.setAllocationSize(memReq.size * CONCURRENT_FRAMES) | |
.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: | |
for (uint32_t i = 0; i < CONCURRENT_FRAMES; i++) | |
device.bindBufferMemory(uniformBuffer[i], uniformBufferMemory, memReq.size * i); | |
/////////////////// 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[i]).setOffset(0).setRange(sizeof(std::array<glm::mat4, 3>)); | |
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, memReq.size * frameInFlightIndex, memReq.size); | |
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