Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 67e8b82

Browse files
author
Jonah Williams
authored
[Impeller] combine uniform metadata and buffer slots. (#45021)
Follow up from #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.
1 parent 27d75f6 commit 67e8b82

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

impeller/renderer/backend/gles/buffer_bindings_gles.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ bool BufferBindingsGLES::BindUniformData(
145145
const Bindings& vertex_bindings,
146146
const Bindings& fragment_bindings) const {
147147
for (const auto& buffer : vertex_bindings.buffers) {
148-
if (!BindUniformBuffer(gl, transients_allocator, buffer.second)) {
148+
if (!BindUniformBuffer(gl, transients_allocator, buffer.second.view)) {
149149
return false;
150150
}
151151
}
152152
for (const auto& buffer : fragment_bindings.buffers) {
153-
if (!BindUniformBuffer(gl, transients_allocator, buffer.second)) {
153+
if (!BindUniformBuffer(gl, transients_allocator, buffer.second.view)) {
154154
return false;
155155
}
156156
}

impeller/renderer/backend/metal/compute_pass_mtl.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static bool Bind(ComputePassBindingsCache& pass,
225225

226226
for (const auto& buffer : command.bindings.buffers) {
227227
if (!Bind(pass_bindings, *allocator, buffer.first,
228-
buffer.second.resource)) {
228+
buffer.second.view.resource)) {
229229
return false;
230230
}
231231
}

impeller/renderer/backend/metal/render_pass_mtl.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static bool Bind(PassBindingsCache& pass,
409409
ShaderStage stage) -> bool {
410410
for (const auto& buffer : bindings.buffers) {
411411
if (!Bind(pass_bindings, *allocator, stage, buffer.first,
412-
buffer.second.resource)) {
412+
buffer.second.view.resource)) {
413413
return false;
414414
}
415415
}

impeller/renderer/backend/vulkan/compute_pass_vk.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
125125
&desc_set, //
126126
&vk_desc_set //
127127
](const Bindings& bindings) -> bool {
128-
for (const auto& [buffer_index, view] : bindings.buffers) {
129-
const auto& buffer_view = view.resource.buffer;
128+
for (const auto& [buffer_index, data] : bindings.buffers) {
129+
const auto& buffer_view = data.view.resource.buffer;
130130

131131
auto device_buffer = buffer_view->GetDeviceBuffer(allocator);
132132
if (!device_buffer) {
@@ -143,14 +143,14 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
143143
return false;
144144
}
145145

146-
uint32_t offset = view.resource.range.offset;
146+
uint32_t offset = data.view.resource.range.offset;
147147

148148
vk::DescriptorBufferInfo buffer_info;
149149
buffer_info.buffer = buffer;
150150
buffer_info.offset = offset;
151-
buffer_info.range = view.resource.range.length;
151+
buffer_info.range = data.view.resource.range.length;
152152

153-
const ShaderUniformSlot& uniform = bindings.uniforms.at(buffer_index);
153+
const ShaderUniformSlot& uniform = data.slot;
154154
auto layout_it = std::find_if(desc_set.begin(), desc_set.end(),
155155
[&uniform](DescriptorSetLayout& layout) {
156156
return layout.binding == uniform.binding;

impeller/renderer/backend/vulkan/render_pass_vk.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
368368
&desc_set, //
369369
&vk_desc_set //
370370
](const Bindings& bindings) -> bool {
371-
for (const auto& [buffer_index, view] : bindings.buffers) {
372-
const auto& buffer_view = view.resource.buffer;
371+
for (const auto& [buffer_index, data] : bindings.buffers) {
372+
const auto& buffer_view = data.view.resource.buffer;
373373

374374
auto device_buffer = buffer_view->GetDeviceBuffer(allocator);
375375
if (!device_buffer) {
@@ -391,14 +391,14 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
391391
return false;
392392
}
393393

394-
uint32_t offset = view.resource.range.offset;
394+
uint32_t offset = data.view.resource.range.offset;
395395

396396
vk::DescriptorBufferInfo buffer_info;
397397
buffer_info.buffer = buffer;
398398
buffer_info.offset = offset;
399-
buffer_info.range = view.resource.range.length;
399+
buffer_info.range = data.view.resource.range.length;
400400

401-
const ShaderUniformSlot& uniform = bindings.uniforms.at(buffer_index);
401+
const ShaderUniformSlot& uniform = data.slot;
402402
auto layout_it = std::find_if(desc_set.begin(), desc_set.end(),
403403
[&uniform](DescriptorSetLayout& layout) {
404404
return layout.binding == uniform.binding;

impeller/renderer/command.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ bool Command::BindVertices(const VertexBuffer& buffer) {
1818
return false;
1919
}
2020

21-
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = {
22-
nullptr, buffer.vertex_buffer};
21+
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] =
22+
BufferAndUniformSlot{.slot = {}, .view = {nullptr, buffer.vertex_buffer}};
2323
index_buffer = buffer.index_buffer;
2424
vertex_count = buffer.vertex_count;
2525
index_type = buffer.index_type;
@@ -30,7 +30,7 @@ BufferView Command::GetVertexBuffer() const {
3030
auto found = vertex_bindings.buffers.find(
3131
VertexDescriptor::kReservedVertexBufferIndex);
3232
if (found != vertex_bindings.buffers.end()) {
33-
return found->second.resource;
33+
return found->second.view.resource;
3434
}
3535
return {};
3636
}
@@ -61,13 +61,12 @@ bool Command::DoBindResource(ShaderStage stage,
6161

6262
switch (stage) {
6363
case ShaderStage::kVertex:
64-
vertex_bindings.uniforms[slot.ext_res_0] = slot;
65-
vertex_bindings.buffers[slot.ext_res_0] = BufferResource(metadata, view);
64+
vertex_bindings.buffers[slot.ext_res_0] = {
65+
.slot = slot, .view = BufferResource(metadata, view)};
6666
return true;
6767
case ShaderStage::kFragment:
68-
fragment_bindings.uniforms[slot.ext_res_0] = slot;
69-
fragment_bindings.buffers[slot.ext_res_0] =
70-
BufferResource(metadata, view);
68+
fragment_bindings.buffers[slot.ext_res_0] = {
69+
.slot = slot, .view = BufferResource(metadata, view)};
7170
return true;
7271
case ShaderStage::kCompute:
7372
VALIDATION_LOG << "Use ComputeCommands for compute shader stages.";

impeller/renderer/command.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ struct TextureAndSampler {
6868
SamplerResource sampler;
6969
};
7070

71+
/// @brief combines the buffer resource and its uniform slot information.
72+
struct BufferAndUniformSlot {
73+
ShaderUniformSlot slot;
74+
BufferResource view;
75+
};
76+
7177
struct Bindings {
72-
std::map<size_t, ShaderUniformSlot> uniforms;
7378
std::map<size_t, TextureAndSampler> sampled_images;
74-
std::map<size_t, BufferResource> buffers;
79+
std::map<size_t, BufferAndUniformSlot> buffers;
7580
};
7681

7782
//------------------------------------------------------------------------------

impeller/renderer/compute_command.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ bool ComputeCommand::BindResource(ShaderStage stage,
2323
return false;
2424
}
2525

26-
bindings.uniforms[slot.ext_res_0] = slot;
27-
bindings.buffers[slot.ext_res_0] = {&metadata, view};
26+
bindings.buffers[slot.ext_res_0] = {.slot = slot, .view = {&metadata, view}};
2827
return true;
2928
}
3029

0 commit comments

Comments
 (0)