[Impeller] Fix uniform buffer bindings to point to the right var (flutter/engine#36293)

This commit is contained in:
Kaushik Iska
2022-09-21 07:25:10 -04:00
committed by GitHub
parent 3047e3976a
commit c8047a01e9
10 changed files with 68 additions and 5 deletions

View File

@@ -64,7 +64,9 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
static constexpr auto kResource{{camel_case(buffer.name)}} = ShaderUniformSlot { // {{buffer.name}}
"{{buffer.name}}", // name
{{buffer.ext_res_0}}u, // binding
{{buffer.ext_res_0}}u, // ext_res_0
{{buffer.set}}u, // set
{{buffer.binding}}u, // binding
};
static ShaderMetadata kMetadata{{camel_case(buffer.name)}};
{% endfor %}

View File

@@ -90,6 +90,37 @@ TEST_P(CompilerTest, BindingBaseForFragShader) {
ASSERT_GT(frag_uniform_binding, vert_uniform_binding);
}
TEST_P(CompilerTest, UniformsHaveBindingAndSet) {
if (GetParam() == TargetPlatform::kFlutterSPIRV) {
// This is a failure of reflection which this target doesn't perform.
GTEST_SKIP();
}
ASSERT_TRUE(CanCompileAndReflect("sample_with_binding.vert",
SourceType::kVertexShader));
ASSERT_TRUE(CanCompileAndReflect("sample.frag", SourceType::kFragmentShader));
struct binding_and_set {
uint32_t binding;
uint32_t set;
};
auto get_binding = [&](const char* fixture) -> binding_and_set {
auto json_fd = GetReflectionJson(fixture);
nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
uint32_t binding = shader_json["buffers"][0]["binding"].get<uint32_t>();
uint32_t set = shader_json["buffers"][0]["set"].get<uint32_t>();
return {binding, set};
};
auto vert_uniform_binding = get_binding("sample_with_binding.vert");
auto frag_uniform_binding = get_binding("sample.frag");
ASSERT_EQ(frag_uniform_binding.set, 0u);
ASSERT_EQ(vert_uniform_binding.set, 3u);
ASSERT_EQ(vert_uniform_binding.binding, 17u);
}
#define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
INSTANTIATE_TEST_SUITE_P( \
suite_name, CompilerTest, \

View File

@@ -440,6 +440,8 @@ std::optional<nlohmann::json::object_t> Reflector::ReflectResource(
resource.id, spv::Decoration::DecorationDescriptorSet);
result["binding"] = compiler_->get_decoration(
resource.id, spv::Decoration::DecorationBinding);
result["set"] = compiler_->get_decoration(
resource.id, spv::Decoration::DecorationDescriptorSet);
result["location"] = compiler_->get_decoration(
resource.id, spv::Decoration::DecorationLocation);
result["index"] =

View File

@@ -55,6 +55,7 @@ test_fixtures("file_fixtures") {
"sample.tesc",
"sample.tese",
"sample.vert",
"sample_with_binding.vert",
"sa%m#ple.vert",
"struct_def_bug.vert",
"table_mountain_nx.png",

View File

@@ -21,4 +21,3 @@ void main() {
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition22, 1.0) * inAnotherPosition;
outStuff = texture(world, inPosition);
}

View File

@@ -0,0 +1,23 @@
// 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.
#include "types.h"
layout (set = 3, binding = 17) uniform UniformBufferObject {
Uniforms uniforms;
} ubo;
uniform sampler2D world;
in vec2 inPosition;
in vec3 inPosition22;
in vec4 inAnotherPosition;
in float stuff;
out vec4 outStuff;
void main() {
gl_Position = ubo.uniforms.projection * ubo.uniforms.view * ubo.uniforms.model * vec4(inPosition22, 1.0) * inAnotherPosition;
outStuff = texture(world, inPosition);
}

View File

@@ -43,10 +43,12 @@ bool Command::BindResource(ShaderStage stage,
switch (stage) {
case ShaderStage::kVertex:
vertex_bindings.buffers[slot.binding] = {&metadata, view};
vertex_bindings.uniforms[slot.ext_res_0] = slot;
vertex_bindings.buffers[slot.ext_res_0] = {&metadata, view};
return true;
case ShaderStage::kFragment:
fragment_bindings.buffers[slot.binding] = {&metadata, view};
fragment_bindings.uniforms[slot.ext_res_0] = slot;
fragment_bindings.buffers[slot.ext_res_0] = {&metadata, view};
return true;
case ShaderStage::kCompute:
VALIDATION_LOG << "Use ComputeCommands for compute shader stages.";

View File

@@ -41,6 +41,7 @@ using TextureResource = Resource<std::shared_ptr<const Texture>>;
using SamplerResource = Resource<std::shared_ptr<const Sampler>>;
struct Bindings {
std::map<size_t, ShaderUniformSlot> uniforms;
std::map<size_t, BufferResource> buffers;
std::map<size_t, TextureResource> textures;
std::map<size_t, SamplerResource> samplers;

View File

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

View File

@@ -78,6 +78,8 @@ struct ShaderMetadata {
struct ShaderUniformSlot {
const char* name;
size_t ext_res_0;
size_t set;
size_t binding;
};