[Impeller] combine uniform metadata and buffer slots. (flutter/engine#45021)

Follow up from https://github.com/flutter/engine/pull/44990

We're spending a ton of time deallocating mostly empty maps, by reducing the number of maps we reduce the amount of memory used, and reduce map lookups.
This commit is contained in:
Jonah Williams
2023-08-23 15:18:47 -07:00
committed by GitHub
parent acd4195c2a
commit 90acd88d7e
8 changed files with 29 additions and 26 deletions

View File

@@ -145,12 +145,12 @@ bool BufferBindingsGLES::BindUniformData(
const Bindings& vertex_bindings,
const Bindings& fragment_bindings) const {
for (const auto& buffer : vertex_bindings.buffers) {
if (!BindUniformBuffer(gl, transients_allocator, buffer.second)) {
if (!BindUniformBuffer(gl, transients_allocator, buffer.second.view)) {
return false;
}
}
for (const auto& buffer : fragment_bindings.buffers) {
if (!BindUniformBuffer(gl, transients_allocator, buffer.second)) {
if (!BindUniformBuffer(gl, transients_allocator, buffer.second.view)) {
return false;
}
}

View File

@@ -225,7 +225,7 @@ bool ComputePassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
for (const auto& buffer : command.bindings.buffers) {
if (!Bind(pass_bindings, *allocator, buffer.first,
buffer.second.resource)) {
buffer.second.view.resource)) {
return false;
}
}

View File

@@ -409,7 +409,7 @@ bool RenderPassMTL::EncodeCommands(const std::shared_ptr<Allocator>& allocator,
ShaderStage stage) -> bool {
for (const auto& buffer : bindings.buffers) {
if (!Bind(pass_bindings, *allocator, stage, buffer.first,
buffer.second.resource)) {
buffer.second.view.resource)) {
return false;
}
}

View File

@@ -125,8 +125,8 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
&desc_set, //
&vk_desc_set //
](const Bindings& bindings) -> bool {
for (const auto& [buffer_index, view] : bindings.buffers) {
const auto& buffer_view = view.resource.buffer;
for (const auto& [buffer_index, data] : bindings.buffers) {
const auto& buffer_view = data.view.resource.buffer;
auto device_buffer = buffer_view->GetDeviceBuffer(allocator);
if (!device_buffer) {
@@ -143,14 +143,14 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
return false;
}
uint32_t offset = view.resource.range.offset;
uint32_t offset = data.view.resource.range.offset;
vk::DescriptorBufferInfo buffer_info;
buffer_info.buffer = buffer;
buffer_info.offset = offset;
buffer_info.range = view.resource.range.length;
buffer_info.range = data.view.resource.range.length;
const ShaderUniformSlot& uniform = bindings.uniforms.at(buffer_index);
const ShaderUniformSlot& uniform = data.slot;
auto layout_it = std::find_if(desc_set.begin(), desc_set.end(),
[&uniform](DescriptorSetLayout& layout) {
return layout.binding == uniform.binding;

View File

@@ -368,8 +368,8 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
&desc_set, //
&vk_desc_set //
](const Bindings& bindings) -> bool {
for (const auto& [buffer_index, view] : bindings.buffers) {
const auto& buffer_view = view.resource.buffer;
for (const auto& [buffer_index, data] : bindings.buffers) {
const auto& buffer_view = data.view.resource.buffer;
auto device_buffer = buffer_view->GetDeviceBuffer(allocator);
if (!device_buffer) {
@@ -391,14 +391,14 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
return false;
}
uint32_t offset = view.resource.range.offset;
uint32_t offset = data.view.resource.range.offset;
vk::DescriptorBufferInfo buffer_info;
buffer_info.buffer = buffer;
buffer_info.offset = offset;
buffer_info.range = view.resource.range.length;
buffer_info.range = data.view.resource.range.length;
const ShaderUniformSlot& uniform = bindings.uniforms.at(buffer_index);
const ShaderUniformSlot& uniform = data.slot;
auto layout_it = std::find_if(desc_set.begin(), desc_set.end(),
[&uniform](DescriptorSetLayout& layout) {
return layout.binding == uniform.binding;

View File

@@ -18,8 +18,8 @@ bool Command::BindVertices(const VertexBuffer& buffer) {
return false;
}
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = {
nullptr, buffer.vertex_buffer};
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] =
BufferAndUniformSlot{.slot = {}, .view = {nullptr, buffer.vertex_buffer}};
index_buffer = buffer.index_buffer;
vertex_count = buffer.vertex_count;
index_type = buffer.index_type;
@@ -30,7 +30,7 @@ BufferView Command::GetVertexBuffer() const {
auto found = vertex_bindings.buffers.find(
VertexDescriptor::kReservedVertexBufferIndex);
if (found != vertex_bindings.buffers.end()) {
return found->second.resource;
return found->second.view.resource;
}
return {};
}
@@ -61,13 +61,12 @@ bool Command::DoBindResource(ShaderStage stage,
switch (stage) {
case ShaderStage::kVertex:
vertex_bindings.uniforms[slot.ext_res_0] = slot;
vertex_bindings.buffers[slot.ext_res_0] = BufferResource(metadata, view);
vertex_bindings.buffers[slot.ext_res_0] = {
.slot = slot, .view = BufferResource(metadata, view)};
return true;
case ShaderStage::kFragment:
fragment_bindings.uniforms[slot.ext_res_0] = slot;
fragment_bindings.buffers[slot.ext_res_0] =
BufferResource(metadata, view);
fragment_bindings.buffers[slot.ext_res_0] = {
.slot = slot, .view = BufferResource(metadata, view)};
return true;
case ShaderStage::kCompute:
VALIDATION_LOG << "Use ComputeCommands for compute shader stages.";

View File

@@ -68,10 +68,15 @@ struct TextureAndSampler {
SamplerResource sampler;
};
/// @brief combines the buffer resource and its uniform slot information.
struct BufferAndUniformSlot {
ShaderUniformSlot slot;
BufferResource view;
};
struct Bindings {
std::map<size_t, ShaderUniformSlot> uniforms;
std::map<size_t, TextureAndSampler> sampled_images;
std::map<size_t, BufferResource> buffers;
std::map<size_t, BufferAndUniformSlot> buffers;
};
//------------------------------------------------------------------------------

View File

@@ -23,8 +23,7 @@ bool ComputeCommand::BindResource(ShaderStage stage,
return false;
}
bindings.uniforms[slot.ext_res_0] = slot;
bindings.buffers[slot.ext_res_0] = {&metadata, view};
bindings.buffers[slot.ext_res_0] = {.slot = slot, .view = {&metadata, view}};
return true;
}