Skip to content

Commit 0ad7e39

Browse files
committed
GPU Vulkan Display: Simplification: Use Push Descriptors instead of the Descriptor Pool
1 parent 1d38f15 commit 0ad7e39

2 files changed

Lines changed: 11 additions & 85 deletions

File tree

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.cxx‎

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ void GPUDisplayBackendVulkan::createDevice()
498498
auto& reqFeatures10 = reqDeviceFeatures.get<vk::PhysicalDeviceFeatures2>().features;
499499
reqFeatures10.geometryShader = reqFeatures10.wideLines = reqFeatures10.largePoints = reqFeatures10.multiDrawIndirect = VK_TRUE;
500500
reqDeviceFeatures.get<vk::PhysicalDeviceVulkan12Features>().timelineSemaphore = VK_TRUE;
501+
reqDeviceFeatures.get<vk::PhysicalDeviceVulkan14Features>().pushDescriptor = VK_TRUE;
501502
reqDeviceFeatures.get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering = reqDeviceFeatures.get<vk::PhysicalDeviceVulkan13Features>().synchronization2 = VK_TRUE;
502503
reqDeviceFeatures.get<vk::PhysicalDeviceSwapchainMaintenance1FeaturesKHR>().swapchainMaintenance1 = VK_TRUE;
503504
reqDeviceFeatures.get<vk::PhysicalDevicePresentIdFeaturesKHR>().presentId = VK_TRUE;
@@ -644,17 +645,6 @@ void GPUDisplayBackendVulkan::createUniformLayoutsAndBuffers()
644645
}
645646
}
646647

647-
std::array<vk::DescriptorPoolSize, 2> poolSizes{};
648-
poolSizes[0].type = vk::DescriptorType::eUniformBuffer;
649-
poolSizes[0].descriptorCount = (uint32_t)mImageCount * 3;
650-
poolSizes[1].type = vk::DescriptorType::eCombinedImageSampler;
651-
poolSizes[1].descriptorCount = (uint32_t)mImageCount * 2;
652-
vk::DescriptorPoolCreateInfo poolInfo{};
653-
poolInfo.poolSizeCount = poolSizes.size();
654-
poolInfo.pPoolSizes = poolSizes.data();
655-
poolInfo.maxSets = (uint32_t)mImageCount * 3;
656-
mDescriptorPool = mDevice.createDescriptorPool(poolInfo, nullptr);
657-
658648
vk::DescriptorSetLayoutBinding uboLayoutBindingMat{};
659649
uboLayoutBindingMat.binding = 0;
660650
uboLayoutBindingMat.descriptorType = vk::DescriptorType::eUniformBuffer;
@@ -668,69 +658,31 @@ void GPUDisplayBackendVulkan::createUniformLayoutsAndBuffers()
668658
vk::DescriptorSetLayoutBinding bindings[2] = {uboLayoutBindingMat, samplerLayoutBinding};
669659

670660
vk::DescriptorSetLayoutCreateInfo layoutInfo{};
661+
layoutInfo.flags = vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptor;
671662
layoutInfo.bindingCount = 1;
672663
layoutInfo.pBindings = bindings;
673664
mUniformDescriptor = mDevice.createDescriptorSetLayout(layoutInfo, nullptr);
674665
layoutInfo.bindingCount = 2;
675666
mUniformDescriptorTexture = mDevice.createDescriptorSetLayout(layoutInfo, nullptr);
676667

677-
vk::DescriptorSetAllocateInfo allocInfo{};
678-
allocInfo.descriptorPool = mDescriptorPool;
679-
allocInfo.descriptorSetCount = (uint32_t)mImageCount;
680-
for (int32_t j = 0; j < 3; j++) { // 0 = Render, 1 = Text, 2 = Texture
681-
std::vector<vk::DescriptorSetLayout> layouts(mImageCount, j ? mUniformDescriptorTexture : mUniformDescriptor);
682-
allocInfo.pSetLayouts = layouts.data();
683-
mDescriptorSets[j] = mDevice.allocateDescriptorSets(allocInfo);
684-
685-
for (uint32_t i = 0; i < mImageCount; i++) {
686-
vk::DescriptorBufferInfo bufferInfo{};
687-
bufferInfo.buffer = mUniformBuffersMat[j][i].buffer;
688-
bufferInfo.offset = 0;
689-
bufferInfo.range = mUniformBuffersMat[j][i].size;
690-
691-
vk::WriteDescriptorSet descriptorWrite{};
692-
descriptorWrite.dstSet = mDescriptorSets[j][i];
693-
descriptorWrite.dstBinding = 0;
694-
descriptorWrite.dstArrayElement = 0;
695-
descriptorWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
696-
descriptorWrite.descriptorCount = 1;
697-
descriptorWrite.pBufferInfo = &bufferInfo;
698-
descriptorWrite.pImageInfo = nullptr;
699-
descriptorWrite.pTexelBufferView = nullptr;
700-
mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
701-
}
702-
}
703668
mUpdateUBOdrawMatrix = mUpdateUBOtextMatrix = mUpdateUBOmixMatrix = -1;
704-
705-
if (mFontImage.sizex && mFontImage.sizey) {
706-
updateFontTextureDescriptor();
707-
}
708669
}
709670

710671
void GPUDisplayBackendVulkan::clearUniformLayoutsAndBuffers()
711672
{
712673
mDevice.destroyDescriptorSetLayout(mUniformDescriptor, nullptr);
713674
mDevice.destroyDescriptorSetLayout(mUniformDescriptorTexture, nullptr);
714-
mDevice.destroyDescriptorPool(mDescriptorPool, nullptr);
715675
for (int32_t j = 0; j < 3; j++) {
716676
clearVector(mUniformBuffersMat[j], [&](auto& x) { clearBuffer(x); });
717677
}
718678
}
719679

720-
void GPUDisplayBackendVulkan::setMixDescriptor(int32_t descriptorIndex, int32_t imageIndex)
680+
void GPUDisplayBackendVulkan::cmdPushDescriptors(vk::CommandBuffer commandBuffer, vk::PipelineLayout layout, const VulkanBuffer& ubo, vk::ImageView textureView)
721681
{
722-
vk::DescriptorImageInfo imageInfo{};
723-
imageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
724-
imageInfo.sampler = mTextureSampler;
725-
imageInfo.imageView = *mRenderTargetView[imageIndex + mImageCount];
726-
vk::WriteDescriptorSet descriptorWrite{};
727-
descriptorWrite.dstSet = mDescriptorSets[2][descriptorIndex];
728-
descriptorWrite.dstBinding = 2;
729-
descriptorWrite.dstArrayElement = 0;
730-
descriptorWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
731-
descriptorWrite.descriptorCount = 1;
732-
descriptorWrite.pImageInfo = &imageInfo;
733-
mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
682+
vk::DescriptorBufferInfo bufferInfo{ubo.buffer, 0, ubo.size};
683+
vk::DescriptorImageInfo imageInfo{mTextureSampler, textureView, vk::ImageLayout::eShaderReadOnlyOptimal};
684+
const vk::WriteDescriptorSet writes[2] = {{{}, 0, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &bufferInfo}, {{}, 2, 0, 1, vk::DescriptorType::eCombinedImageSampler, &imageInfo}};
685+
commandBuffer.pushDescriptorSet(vk::PipelineBindPoint::eGraphics, layout, 0, textureView ? 2 : 1, writes);
734686
}
735687

736688
// ---------------------------- VULKAN TEXTURE SAMPLER ----------------------------
@@ -926,10 +878,6 @@ void GPUDisplayBackendVulkan::createOffscreenBuffers(bool forScreenshot, bool fo
926878
{(float)mRenderWidth, 0, 1.0f, 0.0f},
927879
{(float)mRenderWidth, (float)mRenderHeight, 1.0f, 1.0f}};
928880
mMixingTextureVertexArray = createBuffer(sizeof(vertices), &vertices[0][0], vk::BufferUsageFlagBits::eVertexBuffer, 1);
929-
930-
for (uint32_t i = 0; i < mImageCount; i++) {
931-
setMixDescriptor(i, i);
932-
}
933881
}
934882

935883
mFontVertexBuffer.resize(mImageCount);
@@ -1171,7 +1119,7 @@ void GPUDisplayBackendVulkan::startFillCommandBuffer(vk::CommandBuffer& commandB
11711119

11721120
vk::DeviceSize offsets[] = {0};
11731121
commandBuffer.bindVertexBuffers(0, 1, &mVBO.buffer, offsets);
1174-
commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayout, 0, 1, &mDescriptorSets[0][mCurrentImageIndex], 0, nullptr);
1122+
cmdPushDescriptors(commandBuffer, mPipelineLayout, mUniformBuffersMat[0][imageIndex]);
11751123
}
11761124

11771125
void GPUDisplayBackendVulkan::endFillCommandBuffer(vk::CommandBuffer& commandBuffer)
@@ -1610,7 +1558,7 @@ void GPUDisplayBackendVulkan::finishText()
16101558
writeToBuffer(fontBuffer, fontBufferSize, mFontVertexBufferHost.data());
16111559

16121560
mCommandBuffersText[mCurrentImageIndex].bindPipeline(vk::PipelineBindPoint::eGraphics, mPipelines[3]);
1613-
mCommandBuffersText[mCurrentImageIndex].bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayoutTexture, 0, 1, &mDescriptorSets[1][mCurrentImageIndex], 0, nullptr);
1561+
cmdPushDescriptors(mCommandBuffersText[mCurrentImageIndex], mPipelineLayoutTexture, mUniformBuffersMat[1][mCurrentImageIndex], mFontImage.view);
16141562
vk::DeviceSize offsets[] = {0};
16151563
mCommandBuffersText[mCurrentImageIndex].bindVertexBuffers(0, 1, &fontBuffer.buffer, offsets);
16161564

@@ -1642,7 +1590,7 @@ void GPUDisplayBackendVulkan::mixImages(vk::CommandBuffer commandBuffer, float m
16421590
cmdBeginRenderingLoad(commandBuffer, mRenderTargetImage[mCurrentImageIndex], *mRenderTargetView[mCurrentImageIndex], mRenderWidth, mRenderHeight);
16431591

16441592
commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, mPipelines[4]);
1645-
commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, mPipelineLayoutTexture, 0, 1, &mDescriptorSets[2][mCurrentImageIndex], 0, nullptr);
1593+
cmdPushDescriptors(commandBuffer, mPipelineLayoutTexture, mUniformBuffersMat[2][mCurrentImageIndex], *mRenderTargetView[mCurrentImageIndex + mImageCount]);
16461594
vk::DeviceSize offsets[] = {0};
16471595
commandBuffer.bindVertexBuffers(0, 1, &mMixingTextureVertexArray.buffer, offsets);
16481596

@@ -1753,7 +1701,6 @@ void GPUDisplayBackendVulkan::initializeTextDrawing()
17531701
}
17541702

17551703
mFontImage = createImage(sizex, sizey, bigImage.get(), sizex * sizey, vk::Format::eR8Unorm);
1756-
updateFontTextureDescriptor();
17571704
}
17581705

17591706
void GPUDisplayBackendVulkan::clearFontSymbols()
@@ -1766,24 +1713,6 @@ void GPUDisplayBackendVulkan::clearFontSymbols()
17661713
mFontSymbols.clear();
17671714
}
17681715

1769-
void GPUDisplayBackendVulkan::updateFontTextureDescriptor()
1770-
{
1771-
vk::DescriptorImageInfo imageInfo{};
1772-
imageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
1773-
imageInfo.imageView = mFontImage.view;
1774-
imageInfo.sampler = mTextureSampler;
1775-
for (uint32_t i = 0; i < mImageCount; i++) {
1776-
vk::WriteDescriptorSet descriptorWrite{};
1777-
descriptorWrite.dstSet = mDescriptorSets[1][i];
1778-
descriptorWrite.dstBinding = 2;
1779-
descriptorWrite.dstArrayElement = 0;
1780-
descriptorWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
1781-
descriptorWrite.descriptorCount = 1;
1782-
descriptorWrite.pImageInfo = &imageInfo;
1783-
mDevice.updateDescriptorSets(1, &descriptorWrite, 0, nullptr);
1784-
}
1785-
}
1786-
17871716
void GPUDisplayBackendVulkan::OpenGLPrint(const char* s, float x, float y, float* color, float scale)
17881717
{
17891718
if (!mFreetypeInitialized || mDisplay->drawTextInCompatMode()) {

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
7878
void prepareText() override;
7979
void finishText() override;
8080
void mixImages(vk::CommandBuffer cmdBuffer, float mixSlaveImage);
81-
void setMixDescriptor(int32_t descriptorIndex, int32_t imageIndex);
81+
void cmdPushDescriptors(vk::CommandBuffer commandBuffer, vk::PipelineLayout layout, const VulkanBuffer& ubo, vk::ImageView textureView = {}); // Push descriptors: UBO at binding 0, optional texture at binding 2
8282
void pointSizeFactor(float factor) override;
8383
void lineWidthFactor(float factor) override;
8484
void resizeScene(uint32_t width, uint32_t height) override;
@@ -108,7 +108,6 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
108108
void downsampleToFramebuffer(vk::CommandBuffer& commandBuffer);
109109

110110
void updateSwapChainDetails(const vk::PhysicalDevice& device);
111-
void updateFontTextureDescriptor();
112111
void createDevice();
113112
void createShaders();
114113
void createTextureSampler();
@@ -191,10 +190,8 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
191190
uint32_t mUpdateUBOtextMatrix = -1;
192191
uint32_t mUpdateUBOmixMatrix = -1;
193192
float mLastMVP[16] = {};
194-
std::vector<vk::DescriptorSet> mDescriptorSets[3];
195193
vk::DescriptorSetLayout mUniformDescriptor;
196194
vk::DescriptorSetLayout mUniformDescriptorTexture;
197-
vk::DescriptorPool mDescriptorPool;
198195

199196
VulkanBuffer mVBO;
200197
VulkanBuffer mIndirectCommandBuffer;

0 commit comments

Comments
 (0)