[Impeller] [vulkan] reland binding offset for vulkan (flutter/engine#35356)

This commit is contained in:
Kaushik Iska
2022-08-11 17:03:04 -07:00
committed by GitHub
parent e216667e2a
commit e0d55f71b3
9 changed files with 78 additions and 2 deletions

View File

@@ -152,7 +152,6 @@ std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %}
// ===========================================================================
// Metadata for Vulkan =======================================================
// ===========================================================================
{% if length(buffers)+length(sampled_images) > 0 %}
static constexpr std::array<DescriptorSetLayout,{{length(buffers)+length(sampled_images)}}> kDescriptorSetLayouts{
{% for buffer in buffers %}
DescriptorSetLayout{
@@ -171,7 +170,6 @@ std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %}
},
{% endfor %}
};
{% endif %}
}; // struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader

View File

@@ -14,10 +14,15 @@
#include "impeller/compiler/compiler_backend.h"
#include "impeller/compiler/includer.h"
#include "impeller/compiler/logger.h"
#include "impeller/compiler/types.h"
namespace impeller {
namespace compiler {
const uint32_t kFragBindingBase = 128;
const size_t kNumUniformKinds =
int(shaderc_uniform_kind::shaderc_uniform_kind_buffer) + 1;
static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
const SourceOptions& source_options) {
auto sl_compiler = std::make_shared<spirv_cross::CompilerMSL>(ir);
@@ -223,6 +228,15 @@ static void SetLimitations(shaderc::CompileOptions& compiler_opts) {
}
}
void Compiler::SetBindingBase(shaderc::CompileOptions& compiler_opts) const {
for (size_t uniform_kind = 0; uniform_kind < kNumUniformKinds;
uniform_kind++) {
compiler_opts.SetBindingBaseForStage(
ToShaderCShaderKind(SourceType::kFragmentShader),
static_cast<shaderc_uniform_kind>(uniform_kind), kFragBindingBase);
}
}
Compiler::Compiler(const fml::Mapping& source_mapping,
SourceOptions source_options,
Reflector::Options reflector_options)
@@ -333,6 +347,9 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
}
spirv_options.SetAutoBindUniforms(true);
#ifdef IMPELLER_ENABLE_VULKAN
SetBindingBase(spirv_options);
#endif
spirv_options.SetAutoMapLocations(true);
std::vector<std::string> included_file_names;

View File

@@ -57,6 +57,8 @@ class Compiler {
std::string GetDependencyNames(std::string separator) const;
void SetBindingBase(shaderc::CompileOptions& compiler_opts) const;
FML_DISALLOW_COPY_AND_ASSIGN(Compiler);
};

View File

@@ -89,6 +89,13 @@ bool CompilerTest::ValidateDepfileEscaped(const char* fixture_name) const {
return true;
}
std::unique_ptr<fml::FileMapping> CompilerTest::GetReflectionJson(
const char* fixture_name) const {
auto filename = ReflectionJSONName(fixture_name);
auto fd = fml::OpenFileReadOnly(intermediates_directory_, filename.c_str());
return fml::FileMapping::CreateReadOnly(fd);
}
bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
SourceType source_type) const {
auto fixture = flutter::testing::OpenFixtureAsMapping(fixture_name);

View File

@@ -21,6 +21,9 @@ class CompilerTest : public ::testing::TestWithParam<TargetPlatform> {
~CompilerTest();
std::unique_ptr<fml::FileMapping> GetReflectionJson(
const char* fixture_name) const;
bool CanCompileAndReflect(
const char* fixture_name,
SourceType source_type = SourceType::kUnknown) const;

View File

@@ -70,6 +70,31 @@ TEST_P(CompilerTest, ShaderWithSpecialCharactersHasEscapedDepfile) {
ASSERT_TRUE(ValidateDepfileEscaped("sa\%m#ple.vert"));
}
TEST_P(CompilerTest, BindingBaseForFragShader) {
if (GetParam() == TargetPlatform::kFlutterSPIRV) {
// This is a failure of reflection which this target doesn't perform.
GTEST_SKIP();
}
#ifndef IMPELLER_ENABLE_VULKAN
GTEST_SKIP();
#endif
ASSERT_TRUE(CanCompileAndReflect("sample.vert", SourceType::kVertexShader));
ASSERT_TRUE(CanCompileAndReflect("sample.frag", SourceType::kFragmentShader));
auto get_binding = [&](const char* fixture) -> uint32_t {
auto json_fd = GetReflectionJson(fixture);
nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
return shader_json["buffers"][0]["binding"].get<uint32_t>();
};
auto vert_uniform_binding = get_binding("sample.vert");
auto frag_uniform_binding = get_binding("sample.frag");
ASSERT_GT(frag_uniform_binding, vert_uniform_binding);
}
#define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
INSTANTIATE_TEST_SUITE_P( \
suite_name, CompilerTest, \

View File

@@ -44,6 +44,7 @@ test_fixtures("file_fixtures") {
"kalimba.jpg",
"resources_limit.vert",
"sample.comp",
"sample.frag",
"sample.tesc",
"sample.tese",
"sample.vert",

View File

@@ -0,0 +1,14 @@
// 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 color;
}
frag_info;
out vec4 frag_color;
void main() {
frag_color = frag_info.color;
}

View File

@@ -100,6 +100,15 @@ struct PipelineBuilder {
<< VertexShader::kLabel << "'.";
return false;
}
if (!vertex_descriptor->SetDescriptorSetLayouts(
FragmentShader::kDescriptorSetLayouts)) {
VALIDATION_LOG << "Cound not configure vertex descriptor set layout for"
" pipeline named '"
<< VertexShader::kLabel << "'.";
return false;
}
desc.SetVertexDescriptor(std::move(vertex_descriptor));
}