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

Commit 6f223db

Browse files
authored
[Impeller] Patch the compiler to account for subpass inputs and PSO metadata. (#45739)
Towards flutter/flutter#128911 Drive by fixes flutter/flutter#123795
1 parent 1df0a23 commit 6f223db

File tree

10 files changed

+70
-24
lines changed

10 files changed

+70
-24
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ TEST_P(AiksTest, ColorWheel) {
16361636
auto callback = [&](AiksContext& renderer) -> std::optional<Picture> {
16371637
// UI state.
16381638
static bool cache_the_wheel = true;
1639-
static int current_blend_index = 3;
1639+
static int current_blend_index = 14;
16401640
static float dst_alpha = 1;
16411641
static float src_alpha = 1;
16421642
static Color color0 = Color::Red();

impeller/compiler/code_gen_template.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,30 @@ std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %}
173173
// ===========================================================================
174174
// Metadata for Vulkan =======================================================
175175
// ===========================================================================
176-
static constexpr std::array<DescriptorSetLayout,{{length(buffers)+length(sampled_images)}}> kDescriptorSetLayouts{
176+
static constexpr std::array<DescriptorSetLayout,{{length(buffers)+length(sampled_images)+length(subpass_inputs)}}> kDescriptorSetLayouts{
177177
{% for buffer in buffers %}
178178
DescriptorSetLayout{
179179
{{buffer.binding}}, // binding = {{buffer.binding}}
180+
{{buffer.input_attachment_index}}, // input_attachment_index = {{buffer.input_attachment_index}}
180181
{{buffer.descriptor_type}}, // descriptor_type = {{buffer.descriptor_type}}
181182
{{to_shader_stage(shader_stage)}}, // shader_stage = {{to_shader_stage(shader_stage)}}
182183
},
183184
{% endfor %}
184185
{% for sampled_image in sampled_images %}
185186
DescriptorSetLayout{
186187
{{sampled_image.binding}}, // binding = {{sampled_image.binding}}
188+
{{sampled_image.input_attachment_index}}, // input_attachment_index = {{sampled_image.input_attachment_index}}
187189
{{sampled_image.descriptor_type}}, // descriptor_type = {{sampled_image.descriptor_type}}
188190
{{to_shader_stage(shader_stage)}}, // shader_stage = {{to_shader_stage(shader_stage)}}
189191
},
192+
{% endfor %}
193+
{% for subpass_input in subpass_inputs %}
194+
DescriptorSetLayout{
195+
{{subpass_input.binding}}, // binding = {{subpass_input.binding}}
196+
{{subpass_input.input_attachment_index}}, // input_attachment_index = {{subpass_input.input_attachment_index}}
197+
{{subpass_input.descriptor_type}}, // descriptor_type = {{subpass_input.descriptor_type}}
198+
{{to_shader_stage(shader_stage)}}, // shader_stage = {{to_shader_stage(shader_stage)}}
199+
},
190200
{% endfor %}
191201
};
192202

impeller/compiler/compiler.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ static CompilerBackend CreateMSLCompiler(
115115
static CompilerBackend CreateVulkanCompiler(
116116
const spirv_cross::ParsedIR& ir,
117117
const SourceOptions& source_options) {
118-
// TODO(dnfield): It seems like what we'd want is a CompilerGLSL with
119-
// vulkan_semantics set to true, but that has regressed some things on GLES
120-
// somehow. In the mean time, go back to using CompilerMSL, but set the Metal
121-
// Language version to something really high so that we don't get weird
122-
// complaints about using Metal features while trying to build Vulkan shaders.
123-
// https://github.com/flutter/flutter/issues/123795
124-
return CreateMSLCompiler(
125-
ir, source_options,
126-
spirv_cross::CompilerMSL::Options::make_msl_version(3, 0, 0));
118+
auto vk_compiler = std::make_shared<spirv_cross::CompilerGLSL>(ir);
119+
spirv_cross::CompilerGLSL::Options sl_options;
120+
sl_options.vulkan_semantics = true;
121+
sl_options.force_zero_initialized_variables = true;
122+
sl_options.es = false;
123+
vk_compiler->set_common_options(sl_options);
124+
return CompilerBackend(vk_compiler);
127125
}
128126

129127
static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR& ir,
@@ -218,6 +216,7 @@ static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR& ir,
218216
break;
219217
case TargetPlatform::kSkSL:
220218
compiler = CreateSkSLCompiler(ir, source_options);
219+
break;
221220
}
222221
if (!compiler) {
223222
return {};

impeller/compiler/compiler_backend.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "impeller/compiler/compiler_backend.h"
66

7+
#include <limits>
8+
79
#include "impeller/base/comparable.h"
810

911
namespace impeller {
@@ -44,8 +46,7 @@ uint32_t CompilerBackend::GetExtendedMSLResourceBinding(
4446
if (auto compiler = GetGLSLCompiler()) {
4547
return compiler->get_decoration(id, spv::Decoration::DecorationBinding);
4648
}
47-
const auto kOOBIndex = static_cast<uint32_t>(-1);
48-
return kOOBIndex;
49+
return std::numeric_limits<uint32_t>::max();
4950
}
5051

5152
const spirv_cross::Compiler* CompilerBackend::GetCompiler() const {

impeller/compiler/reflector.cc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "impeller/compiler/reflector.h"
88

99
#include <atomic>
10+
#include <limits>
1011
#include <optional>
1112
#include <set>
1213
#include <sstream>
@@ -216,9 +217,9 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
216217
if (auto storage_buffers_json =
217218
ReflectResources(shader_resources.storage_buffers);
218219
storage_buffers_json.has_value()) {
219-
for (auto uniform_buffer : storage_buffers_json.value()) {
220-
uniform_buffer["descriptor_type"] = "DescriptorType::kStorageBuffer";
221-
buffers.emplace_back(std::move(uniform_buffer));
220+
for (auto storage_buffer : storage_buffers_json.value()) {
221+
storage_buffer["descriptor_type"] = "DescriptorType::kStorageBuffer";
222+
buffers.emplace_back(std::move(storage_buffer));
222223
}
223224
} else {
224225
return std::nullopt;
@@ -261,6 +262,19 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
261262
}
262263
}
263264

265+
{
266+
if (auto inputs = ReflectResources(shader_resources.subpass_inputs);
267+
inputs.has_value()) {
268+
auto& subpass_inputs = root["subpass_inputs"] = nlohmann::json::array_t{};
269+
for (auto input : inputs.value()) {
270+
input["descriptor_type"] = "DescriptorType::kSubpassInput";
271+
subpass_inputs.emplace_back(std::move(input));
272+
}
273+
} else {
274+
return std::nullopt;
275+
}
276+
}
277+
264278
if (auto stage_outputs = ReflectResources(shader_resources.stage_outputs);
265279
stage_outputs.has_value()) {
266280
root["stage_outputs"] = std::move(stage_outputs.value());
@@ -431,6 +445,14 @@ std::vector<size_t> Reflector::ComputeOffsets(
431445
return offsets;
432446
}
433447

448+
static uint32_t GetResourceDecorationIfPresent(const CompilerBackend& compiler,
449+
spirv_cross::ID id,
450+
spv::Decoration decoration) {
451+
return compiler->has_decoration(id, decoration)
452+
? compiler->get_decoration(id, decoration)
453+
: std::numeric_limits<uint32_t>::max();
454+
}
455+
434456
std::optional<nlohmann::json::object_t> Reflector::ReflectResource(
435457
const spirv_cross::Resource& resource,
436458
std::optional<size_t> offset) const {
@@ -445,6 +467,8 @@ std::optional<nlohmann::json::object_t> Reflector::ReflectResource(
445467
resource.id, spv::Decoration::DecorationDescriptorSet);
446468
result["location"] = compiler_->get_decoration(
447469
resource.id, spv::Decoration::DecorationLocation);
470+
result["input_attachment_index"] = GetResourceDecorationIfPresent(
471+
compiler_, resource.id, spv::Decoration::DecorationInputAttachmentIndex);
448472
result["index"] =
449473
compiler_->get_decoration(resource.id, spv::Decoration::DecorationIndex);
450474
result["ext_res_0"] = compiler_.GetExtendedMSLResourceBinding(

impeller/core/shader_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <cstddef>
8+
#include <limits>
89
#include <optional>
910
#include <string_view>
1011
#include <vector>
@@ -144,12 +145,21 @@ enum class DescriptorType {
144145
kSampledImage,
145146
kImage,
146147
kSampler,
148+
kSubpassInput,
147149
};
148150

149151
struct DescriptorSetLayout {
150152
uint32_t binding;
153+
uint32_t input_attachment_index;
151154
DescriptorType descriptor_type;
152155
ShaderStage shader_stage;
156+
157+
constexpr std::optional<uint32_t> GetInputAttachmentIndexIfValid() const {
158+
if (std::numeric_limits<uint32_t>::max() == input_attachment_index) {
159+
return std::nullopt;
160+
}
161+
return input_attachment_index;
162+
}
153163
};
154164

155165
template <size_t Size>

impeller/playground/backend/vulkan/playground_impl_vk.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/fml/logging.h"
1414
#include "flutter/fml/mapping.h"
1515
#include "impeller/entity/vk/entity_shaders_vk.h"
16+
#include "impeller/entity/vk/framebuffer_blend_shaders_vk.h"
1617
#include "impeller/entity/vk/modern_shaders_vk.h"
1718
#include "impeller/fixtures/vk/fixtures_shaders_vk.h"
1819
#include "impeller/playground/imgui/vk/imgui_shaders_vk.h"
@@ -33,6 +34,9 @@ ShaderLibraryMappingsForPlayground() {
3334
impeller_entity_shaders_vk_length),
3435
std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_vk_data,
3536
impeller_modern_shaders_vk_length),
37+
std::make_shared<fml::NonOwnedMapping>(
38+
impeller_framebuffer_blend_shaders_vk_data,
39+
impeller_framebuffer_blend_shaders_vk_length),
3640
std::make_shared<fml::NonOwnedMapping>(
3741
impeller_fixtures_shaders_vk_data,
3842
impeller_fixtures_shaders_vk_length),

impeller/renderer/backend/vulkan/descriptor_pool_vk.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ static vk::UniqueDescriptorPool CreatePool(const vk::Device& device,
2424
std::vector<vk::DescriptorPoolSize> pools = {
2525
{vk::DescriptorType::eCombinedImageSampler, pool_count},
2626
{vk::DescriptorType::eUniformBuffer, pool_count},
27-
{vk::DescriptorType::eStorageBuffer, pool_count}};
27+
{vk::DescriptorType::eStorageBuffer, pool_count},
28+
{vk::DescriptorType::eInputAttachment, pool_count},
29+
};
2830

2931
vk::DescriptorPoolCreateInfo pool_info;
3032
pool_info.setMaxSets(pools.size() * pool_count);

impeller/renderer/backend/vulkan/formats_vk.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,17 @@ constexpr vk::DescriptorType ToVKDescriptorType(DescriptorType type) {
274274
switch (type) {
275275
case DescriptorType::kSampledImage:
276276
return vk::DescriptorType::eCombinedImageSampler;
277-
break;
278277
case DescriptorType::kUniformBuffer:
279278
return vk::DescriptorType::eUniformBuffer;
280-
break;
281279
case DescriptorType::kStorageBuffer:
282280
return vk::DescriptorType::eStorageBuffer;
283-
break;
284281
case DescriptorType::kImage:
285282
return vk::DescriptorType::eSampledImage;
286-
break;
287283
case DescriptorType::kSampler:
288284
return vk::DescriptorType::eSampler;
289-
break;
285+
case DescriptorType::kSubpassInput:
286+
return vk::DescriptorType::eInputAttachment;
290287
}
291-
292288
FML_UNREACHABLE();
293289
}
294290

shell/platform/embedder/embedder_surface_metal_impeller.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "flutter/fml/synchronization/sync_switch.h"
1212
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
1313
#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"
14-
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
14+
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
1515
#include "impeller/entity/mtl/entity_shaders.h"
1616
#include "impeller/entity/mtl/framebuffer_blend_shaders.h"
1717
#include "impeller/entity/mtl/modern_shaders.h"

0 commit comments

Comments
 (0)