[Impeller] avoid hashing and std::vector growth when binding descriptor sets. (flutter/engine#45070)

We call allocateAndBindDescriptorSets once for every cmd rendered, so this time can bubble up quite a bit. From the traces I've gathered map emplacement (emplace_unique_key_args) and std::vector growth (__push_back_slow_path) contribute the most to this trace.

I think we have physical limits on the number of descriptors we can bind, though I'm not sure if we enforce that anywhere?
This commit is contained in:
Jonah Williams
2023-08-25 10:22:43 -07:00
committed by GitHub
parent 578dc16da4
commit 843b0a5719

View File

@@ -6,7 +6,6 @@
#include <array>
#include <cstdint>
#include <unordered_map>
#include <vector>
#include "flutter/fml/logging.h"
@@ -339,9 +338,15 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
auto& allocator = *context.GetResourceAllocator();
std::unordered_map<uint32_t, vk::DescriptorBufferInfo> buffers;
std::unordered_map<uint32_t, vk::DescriptorImageInfo> images;
std::vector<vk::DescriptorImageInfo> images;
std::vector<vk::DescriptorBufferInfo> buffers;
std::vector<vk::WriteDescriptorSet> writes;
writes.reserve(command.vertex_bindings.buffers.size() +
command.fragment_bindings.buffers.size() +
command.fragment_bindings.sampled_images.size());
images.reserve(command.fragment_bindings.sampled_images.size());
buffers.reserve(command.vertex_bindings.buffers.size() +
command.fragment_bindings.buffers.size());
auto bind_images = [&encoder, //
&images, //
@@ -364,13 +369,14 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
image_info.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
image_info.sampler = sampler.GetSampler();
image_info.imageView = texture_vk.GetImageView();
images.push_back(image_info);
vk::WriteDescriptorSet write_set;
write_set.dstSet = vk_desc_set.value();
write_set.dstBinding = slot.binding;
write_set.descriptorCount = 1u;
write_set.descriptorType = vk::DescriptorType::eCombinedImageSampler;
write_set.pImageInfo = &(images[slot.binding] = image_info);
write_set.pImageInfo = &images.back();
writes.push_back(write_set);
}
@@ -409,6 +415,7 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
buffer_info.buffer = buffer;
buffer_info.offset = offset;
buffer_info.range = data.view.resource.range.length;
buffers.push_back(buffer_info);
const ShaderUniformSlot& uniform = data.slot;
auto layout_it = std::find_if(desc_set.begin(), desc_set.end(),
@@ -427,7 +434,7 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
write_set.dstBinding = uniform.binding;
write_set.descriptorCount = 1u;
write_set.descriptorType = ToVKDescriptorType(layout.descriptor_type);
write_set.pBufferInfo = &(buffers[uniform.binding] = buffer_info);
write_set.pBufferInfo = &buffers.back();
writes.push_back(write_set);
}