[Impeller] Allow skipping of inactive uniforms in GLES (flutter/engine#36390)

This commit is contained in:
Brandon DeRosier
2022-09-23 15:27:30 -07:00
committed by GitHub
parent db8601a32e
commit ff8f1bd8be
5 changed files with 94 additions and 2 deletions

View File

@@ -16,6 +16,8 @@ impeller_shaders("shader_fixtures") {
"colors.frag",
"impeller.frag",
"impeller.vert",
"inactive_uniforms.frag",
"inactive_uniforms.vert",
"instanced_draw.frag",
"instanced_draw.vert",
"mipmaps.frag",

View File

@@ -0,0 +1,21 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
uniform FragInfo {
vec4 unused_color;
vec4 color;
}
frag_info;
in vec2 v_position;
out vec4 frag_color;
float SphereDistance(vec2 position, float radius) {
return length(v_position - position) - radius;
}
void main() {
frag_color = frag_info.color;
}

View File

@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
uniform VertInfo {
mat4 mvp;
}
vert_info;
in vec2 position;
out vec2 v_position;
void main() {
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0);
v_position = position;
}

View File

@@ -103,6 +103,9 @@ bool BufferBindingsGLES::ReadUniformsBindings(const ProcTableGLES& gl,
GLsizei written_count = 0u;
GLint uniform_var_size = 0u;
GLenum uniform_type = GL_FLOAT;
// Note: Active uniforms are defined as uniforms that may have an impact on
// the output of the shader. Drivers are allowed to (and often do)
// optimize out unused uniforms.
gl.GetActiveUniform(program, // program
i, // index
max_name_size, // buffer_size
@@ -215,8 +218,10 @@ bool BufferBindingsGLES::BindUniformBuffer(const ProcTableGLES& gl,
member.array_elements > 1);
const auto location = uniform_locations_.find(member_key);
if (location == uniform_locations_.end()) {
VALIDATION_LOG << "Location for uniform member not known: " << member_key;
return false;
// 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 / member.array_elements;

View File

@@ -13,6 +13,8 @@
#include "impeller/fixtures/colors.vert.h"
#include "impeller/fixtures/impeller.frag.h"
#include "impeller/fixtures/impeller.vert.h"
#include "impeller/fixtures/inactive_uniforms.frag.h"
#include "impeller/fixtures/inactive_uniforms.vert.h"
#include "impeller/fixtures/instanced_draw.frag.h"
#include "impeller/fixtures/instanced_draw.vert.h"
#include "impeller/fixtures/mipmaps.frag.h"
@@ -794,5 +796,50 @@ TEST_P(RendererTest, ArrayUniforms) {
OpenPlaygroundHere(callback);
}
TEST_P(RendererTest, InactiveUniforms) {
using VS = InactiveUniformsVertexShader;
using FS = InactiveUniformsFragmentShader;
auto context = GetContext();
auto pipeline_descriptor =
PipelineBuilder<VS, FS>::MakeDefaultPipelineDescriptor(*context);
ASSERT_TRUE(pipeline_descriptor.has_value());
pipeline_descriptor->SetSampleCount(SampleCount::kCount4);
auto pipeline =
context->GetPipelineLibrary()->GetPipeline(pipeline_descriptor).get();
ASSERT_TRUE(pipeline && pipeline->IsValid());
SinglePassCallback callback = [&](RenderPass& pass) {
auto size = pass.GetRenderTargetSize();
Command cmd;
cmd.pipeline = pipeline;
cmd.label = "Inactive Uniform";
VertexBufferBuilder<VS::PerVertexData> builder;
builder.AddVertices({{Point()},
{Point(0, size.height)},
{Point(size.width, 0)},
{Point(size.width, 0)},
{Point(0, size.height)},
{Point(size.width, size.height)}});
cmd.BindVertices(builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
VS::VertInfo vs_uniform;
vs_uniform.mvp =
Matrix::MakeOrthographic(size) * Matrix::MakeScale(GetContentScale());
VS::BindVertInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(vs_uniform));
FS::FragInfo fs_uniform = {.unused_color = Color::Red(),
.color = Color::Green()};
FS::BindFragInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(fs_uniform));
pass.AddCommand(cmd);
return true;
};
OpenPlaygroundHere(callback);
}
} // namespace testing
} // namespace impeller