From 9cd50d1229ab9ea7c471a40a03985cfd13a0b813 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 25 Oct 2023 15:52:19 -0700 Subject: [PATCH] [Impeller] Cache location in metadata. (flutter/engine#46640) Hacky mutable stuff, but avoids recomputing string keys and hashing them multiple times per draw call. [See also ](https://docs.google.com/document/d/1msDN8D4_Hi2zR5JFaFLWDb0IHD3MekuuiaYLJ3Tt6to/edit?resourcekey=0-bZbENKIpXKtrnzU6kkLXcQ) While we cache the uniform location, looking up this value requires computing a string key for every single uniform value on every single command. We can speed this up by stashing the uniform location in the shader metadata, which shouldn't change for the app lifetime. This avoids both the string computation and the hashmap usage. --- .../backend/gles/buffer_bindings_gles.cc | 138 +++++++++++------- .../backend/gles/buffer_bindings_gles.h | 21 ++- .../renderer/backend/gles/pipeline_gles.cc | 2 +- .../renderer/backend/gles/pipeline_gles.h | 2 +- .../renderer/backend/gles/render_pass_gles.cc | 2 +- 5 files changed, 105 insertions(+), 60 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.cc index 65aa3ac26b..9335f4c0b3 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.cc @@ -4,11 +4,9 @@ #include "impeller/renderer/backend/gles/buffer_bindings_gles.h" -#include #include #include -#include "impeller/base/config.h" #include "impeller/base/validation.h" #include "impeller/renderer/backend/gles/device_buffer_gles.h" #include "impeller/renderer/backend/gles/formats_gles.h" @@ -90,6 +88,9 @@ bool BufferBindingsGLES::ReadUniformsBindings(const ProcTableGLES& gl, GLint uniform_count = 0; gl.GetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniform_count); + + // Query the Program for all active uniform locations, and + // record this via normalized key. for (GLint i = 0; i < uniform_count; i++) { std::vector name; name.resize(max_name_size); @@ -139,11 +140,10 @@ bool BufferBindingsGLES::BindVertexAttributes(const ProcTableGLES& gl, return true; } -bool BufferBindingsGLES::BindUniformData( - const ProcTableGLES& gl, - Allocator& transients_allocator, - const Bindings& vertex_bindings, - const Bindings& fragment_bindings) const { +bool BufferBindingsGLES::BindUniformData(const ProcTableGLES& gl, + Allocator& transients_allocator, + const Bindings& vertex_bindings, + const Bindings& fragment_bindings) { for (const auto& buffer : vertex_bindings.buffers) { if (!BindUniformBuffer(gl, transients_allocator, buffer.second.view)) { return false; @@ -173,9 +173,58 @@ bool BufferBindingsGLES::UnbindVertexAttributes(const ProcTableGLES& gl) const { return true; } +GLint BufferBindingsGLES::ComputeTextureLocation( + const ShaderMetadata* metadata) { + auto location = binding_map_.find(metadata->name); + if (location != binding_map_.end()) { + return location->second[0]; + } + auto& locations = binding_map_[metadata->name] = {}; + auto computed_location = + uniform_locations_.find(CreateUniformMemberKey(metadata->name)); + if (computed_location == uniform_locations_.end()) { + locations.push_back(-1); + } else { + locations.push_back(computed_location->second); + } + return locations[0]; +} + +const std::vector& BufferBindingsGLES::ComputeUniformLocations( + const ShaderMetadata* metadata) { + auto location = binding_map_.find(metadata->name); + if (location != binding_map_.end()) { + return location->second; + } + + // For each metadata member, look up the binding location and record + // it in the binding map. + auto& locations = binding_map_[metadata->name] = {}; + for (const auto member : metadata->members) { + if (member.type == ShaderType::kVoid) { + // Void types are used for padding. We are obviously not going to find + // mappings for these. Keep going. + locations.push_back(-1); + continue; + } + + size_t element_count = member.array_elements.value_or(1); + const auto member_key = + CreateUniformMemberKey(metadata->name, member.name, element_count > 1); + const auto computed_location = uniform_locations_.find(member_key); + if (computed_location == uniform_locations_.end()) { + // Uniform was not active. + locations.push_back(-1); + continue; + } + locations.push_back(computed_location->second); + } + return locations; +} + bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl, Allocator& transients_allocator, - const BufferResource& buffer) const { + const BufferResource& buffer) { const auto* metadata = buffer.GetMetadata(); auto device_buffer = buffer.resource.buffer->GetDeviceBuffer(transients_allocator); @@ -194,35 +243,25 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl, return false; } - for (const auto& member : metadata->members) { - if (member.type == ShaderType::kVoid) { - // Void types are used for padding. We are obviously not going to find - // mappings for these. Keep going. + const auto& locations = ComputeUniformLocations(metadata); + for (auto i = 0u; i < metadata->members.size(); i++) { + const auto& member = metadata->members[i]; + auto location = locations[i]; + // Void type or inactive uniform. + if (location == -1) { continue; } size_t element_count = member.array_elements.value_or(1); - - const auto member_key = - CreateUniformMemberKey(metadata->name, member.name, element_count > 1); - const auto location = uniform_locations_.find(member_key); - if (location == uniform_locations_.end()) { - // The list of uniform locations only contains "active" uniforms that are - // not optimized out. So this situation is expected to happen when unused - // uniforms are present in the shader. - continue; - } - size_t element_stride = member.byte_length / element_count; - auto* buffer_data = reinterpret_cast(buffer_ptr + member.offset); std::vector array_element_buffer; if (element_count > 1) { - // When binding uniform arrays, the elements must be contiguous. Copy the - // uniforms to a temp buffer to eliminate any padding needed by the other - // backends. + // When binding uniform arrays, the elements must be contiguous. Copy + // the uniforms to a temp buffer to eliminate any padding needed by the + // other backends. array_element_buffer.resize(member.size * element_count); for (size_t element_i = 0; element_i < element_count; element_i++) { std::memcpy(array_element_buffer.data() + element_i * member.size, @@ -238,34 +277,34 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl, case ShaderType::kFloat: switch (member.size) { case sizeof(Matrix): - gl.UniformMatrix4fv(location->second, // location - element_count, // count - GL_FALSE, // normalize - buffer_data // data + gl.UniformMatrix4fv(location, // location + element_count, // count + GL_FALSE, // normalize + buffer_data // data ); continue; case sizeof(Vector4): - gl.Uniform4fv(location->second, // location - element_count, // count - buffer_data // data + gl.Uniform4fv(location, // location + element_count, // count + buffer_data // data ); continue; case sizeof(Vector3): - gl.Uniform3fv(location->second, // location - element_count, // count - buffer_data // data + gl.Uniform3fv(location, // location + element_count, // count + buffer_data // data ); continue; case sizeof(Vector2): - gl.Uniform2fv(location->second, // location - element_count, // count - buffer_data // data + gl.Uniform2fv(location, // location + element_count, // count + buffer_data // data ); continue; case sizeof(Scalar): - gl.Uniform1fv(location->second, // location - element_count, // count - buffer_data // data + gl.Uniform1fv(location, // location + element_count, // count + buffer_data // data ); continue; } @@ -288,7 +327,7 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl, case ShaderType::kSampledImage: case ShaderType::kSampler: VALIDATION_LOG << "Could not bind uniform buffer data for key: " - << member_key; + << member.name; return false; } } @@ -297,7 +336,7 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl, bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl, const Bindings& bindings, - ShaderStage stage) const { + ShaderStage stage) { size_t active_index = 0; for (const auto& data : bindings.sampled_images) { const auto& texture_gles = TextureGLES::Cast(*data.second.texture.resource); @@ -306,11 +345,8 @@ bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl, return false; } - const auto uniform_key = - CreateUniformMemberKey(data.second.texture.GetMetadata()->name); - auto uniform = uniform_locations_.find(uniform_key); - if (uniform == uniform_locations_.end()) { - VALIDATION_LOG << "Could not find uniform for key: " << uniform_key; + auto location = ComputeTextureLocation(data.second.texture.GetMetadata()); + if (location == -1) { return false; } @@ -343,7 +379,7 @@ bool BufferBindingsGLES::BindTextures(const ProcTableGLES& gl, //-------------------------------------------------------------------------- /// Set the texture uniform location. /// - gl.Uniform1i(uniform->second, active_index); + gl.Uniform1i(location, active_index); //-------------------------------------------------------------------------- /// Bump up the active index at binding. diff --git a/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.h index 62b9a63cd0..39f48ffc91 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/buffer_bindings_gles.h @@ -4,14 +4,14 @@ #pragma once -#include +#include #include #include "flutter/fml/macros.h" +#include "impeller/core/shader_types.h" #include "impeller/renderer/backend/gles/gles.h" #include "impeller/renderer/backend/gles/proc_table_gles.h" #include "impeller/renderer/command.h" -#include "impeller/renderer/vertex_descriptor.h" namespace impeller { @@ -38,7 +38,7 @@ class BufferBindingsGLES { bool BindUniformData(const ProcTableGLES& gl, Allocator& transients_allocator, const Bindings& vertex_bindings, - const Bindings& fragment_bindings) const; + const Bindings& fragment_bindings); bool UnbindVertexAttributes(const ProcTableGLES& gl) const; @@ -55,15 +55,24 @@ class BufferBindingsGLES { GLsizei offset = 0u; }; std::vector vertex_attrib_arrays_; - std::map uniform_locations_; + + std::unordered_map uniform_locations_; + + using BindingMap = std::unordered_map>; + BindingMap binding_map_ = {}; + + const std::vector& ComputeUniformLocations( + const ShaderMetadata* metadata); + + GLint ComputeTextureLocation(const ShaderMetadata* metadata); bool BindUniformBuffer(const ProcTableGLES& gl, Allocator& transients_allocator, - const BufferResource& buffer) const; + const BufferResource& buffer); bool BindTextures(const ProcTableGLES& gl, const Bindings& bindings, - ShaderStage stage) const; + ShaderStage stage); BufferBindingsGLES(const BufferBindingsGLES&) = delete; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.cc index 79efe7454d..eb996613f5 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.cc @@ -35,7 +35,7 @@ const HandleGLES& PipelineGLES::GetProgramHandle() const { return handle_; } -const BufferBindingsGLES* PipelineGLES::GetBufferBindings() const { +BufferBindingsGLES* PipelineGLES::GetBufferBindings() const { return buffer_bindings_.get(); } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.h index 6f84666f9c..6ea8fe1102 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/pipeline_gles.h @@ -28,7 +28,7 @@ class PipelineGLES final [[nodiscard]] bool UnbindProgram() const; - const BufferBindingsGLES* GetBufferBindings() const; + BufferBindingsGLES* GetBufferBindings() const; [[nodiscard]] bool BuildVertexDescriptor(const ProcTableGLES& gl, GLuint program); diff --git a/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc index 5536a3cd6c..265fa1b492 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc @@ -374,7 +374,7 @@ struct RenderPassData { return false; } - const auto& vertex_desc_gles = pipeline.GetBufferBindings(); + auto vertex_desc_gles = pipeline.GetBufferBindings(); //-------------------------------------------------------------------------- /// Bind vertex and index buffers.