diff --git a/engine/src/flutter/impeller/compiler/code_gen_template.h b/engine/src/flutter/impeller/compiler/code_gen_template.h index 71347134d5..4148ccb068 100644 --- a/engine/src/flutter/impeller/compiler/code_gen_template.h +++ b/engine/src/flutter/impeller/compiler/code_gen_template.h @@ -13,7 +13,13 @@ constexpr std::string_view kReflectionHeaderTemplate = #pragma once -#include "shader_types.h" +// Note: The nogncheck decorations are only to make GN not mad at the template. +// There are no GN rule violations in the generated file itself. +#include "impeller/compositor/buffer_view.h" // nogncheck +#include "impeller/compositor/command.h" // nogncheck +#include "impeller/compositor/sampler.h" // nogncheck +#include "impeller/compositor/texture.h" // nogncheck +#include "impeller/shader_glue/shader_types.h" // nogncheck namespace impeller { @@ -43,7 +49,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader { // =========================================================================== {% for uniform in uniform_buffers %} - static constexpr auto kUniform{{camel_case(uniform.name)}} = ShaderUniformSlot<{{uniform.name}}> { // {{uniform.name}} + static constexpr auto kResource{{camel_case(uniform.name)}} = ShaderUniformSlot<{{uniform.name}}> { // {{uniform.name}} "{{uniform.name}}", // name {{uniform.binding}}u, // binding }; @@ -80,7 +86,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader { // =========================================================================== {% for sampled_image in sampled_images %} - static constexpr auto kSampledImage{{camel_case(sampled_image.name)}} = SampledImageSlot { // {{sampled_image.name}} + static constexpr auto kResource{{camel_case(sampled_image.name)}} = SampledImageSlot { // {{sampled_image.name}} "{{sampled_image.name}}", // name {{sampled_image.msl_res_0}}u, // texture {{sampled_image.msl_res_1}}u, // sampler @@ -110,6 +116,21 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader { }; {% endif %} +{% for proto in bind_prototypes %} + /// {{proto.docstring}} + static {{proto.return_type}} Bind{{proto.name}}({% for arg in proto.args %} +{{arg.type_name}} {{arg.argument_name}}{% if not loop.is_last %}, {% endif %} +{% endfor %}) { + return {{ proto.args.0.argument_name }}.BindResource({% for arg in proto.args %} + {% if loop.is_first %} +{{to_shader_stage(shader_stage)}}, kResource{{ proto.name }}, {% else %} +std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %} + {% endif %} + {% endfor %}); + } + +{% endfor %} + }; // struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 3492c4ea6e..70c443aba9 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -239,7 +239,7 @@ std::optional Reflector::GenerateTemplateArguments() const { }); } - root["bind_prototype"] = EmitBindPrototypes(shader_resources); + root["bind_prototypes"] = EmitBindPrototypes(shader_resources); return root; } @@ -661,11 +661,29 @@ std::string Reflector::GetMemberNameAtIndex( std::vector Reflector::ReflectBindPrototypes( const spirv_cross::ShaderResources& resources) const { std::vector prototypes; + for (const auto& uniform_buffer : resources.uniform_buffers) { + auto& proto = prototypes.emplace_back(BindPrototype{}); + proto.return_type = "bool"; + proto.name = ConvertToCamelCase(uniform_buffer.name); + { + std::stringstream stream; + stream << "Bind uniform buffer for resource named " << uniform_buffer.name + << "."; + proto.docstring = stream.str(); + } + proto.args.push_back(BindPrototypeArgument{ + .type_name = "Command&", + .argument_name = "command", + }); + proto.args.push_back(BindPrototypeArgument{ + .type_name = "BufferView", + .argument_name = "view", + }); + } for (const auto& sampled_image : resources.sampled_images) { auto& proto = prototypes.emplace_back(BindPrototype{}); - proto.return_type = "void"; - proto.name = - SPrintF("Bind%s", ConvertToCamelCase(sampled_image.name).c_str()); + proto.return_type = "bool"; + proto.name = ConvertToCamelCase(sampled_image.name); { std::stringstream stream; stream << "Bind combined image sampler for resource named " @@ -677,11 +695,49 @@ std::vector Reflector::ReflectBindPrototypes( .argument_name = "command", }); proto.args.push_back(BindPrototypeArgument{ - .type_name = "Texture&", + .type_name = "std::shared_ptr", .argument_name = "texture", }); proto.args.push_back(BindPrototypeArgument{ - .type_name = "Sampler&", + .type_name = "std::shared_ptr", + .argument_name = "sampler", + }); + } + for (const auto& separate_image : resources.separate_images) { + auto& proto = prototypes.emplace_back(BindPrototype{}); + proto.return_type = "bool"; + proto.name = ConvertToCamelCase(separate_image.name); + { + std::stringstream stream; + stream << "Bind separate image for resource named " << separate_image.name + << "."; + proto.docstring = stream.str(); + } + proto.args.push_back(BindPrototypeArgument{ + .type_name = "Command&", + .argument_name = "command", + }); + proto.args.push_back(BindPrototypeArgument{ + .type_name = "std::shared_ptr", + .argument_name = "texture", + }); + } + for (const auto& separate_sampler : resources.separate_samplers) { + auto& proto = prototypes.emplace_back(BindPrototype{}); + proto.return_type = "bool"; + proto.name = ConvertToCamelCase(separate_sampler.name); + { + std::stringstream stream; + stream << "Bind separate sampler for resource named " + << separate_sampler.name << "."; + proto.docstring = stream.str(); + } + proto.args.push_back(BindPrototypeArgument{ + .type_name = "Command&", + .argument_name = "command", + }); + proto.args.push_back(BindPrototypeArgument{ + .type_name = "std::shared_ptr", .argument_name = "sampler", }); } diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index ee94d8afae..b71a84aad5 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -117,8 +117,6 @@ class Reflector { std::vector ReadStructMembers( const spirv_cross::TypeID& type_id) const; - bool IsVertexShader() const; - FML_DISALLOW_COPY_AND_ASSIGN(Reflector); }; diff --git a/engine/src/flutter/impeller/compositor/command.h b/engine/src/flutter/impeller/compositor/command.h index 6c7d2e6d46..1e0cd4a4a2 100644 --- a/engine/src/flutter/impeller/compositor/command.h +++ b/engine/src/flutter/impeller/compositor/command.h @@ -8,16 +8,18 @@ #include #include +#include "flutter/fml/logging.h" #include "flutter/fml/macros.h" #include "impeller/compositor/buffer_view.h" #include "impeller/compositor/formats.h" #include "impeller/compositor/pipeline.h" +#include "impeller/compositor/sampler.h" +#include "impeller/compositor/texture.h" +#include "impeller/compositor/vertex_buffer.h" +#include "impeller/shader_glue/shader_types.h" namespace impeller { -class Texture; -class Sampler; - struct Bindings { std::map buffers; std::map> textures; @@ -33,6 +35,30 @@ struct Command { std::string label; PrimitiveType primitive_type; + bool BindVertices(const VertexBuffer& buffer); + + template + bool BindResource(ShaderStage stage, + const ShaderUniformSlot slot, + BufferView view) { + return BindResource(stage, slot.binding, std::move(view)); + } + + bool BindResource(ShaderStage stage, size_t binding, BufferView view); + + bool BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr texture); + + bool BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr sampler); + + bool BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr texture, + std::shared_ptr sampler); + constexpr operator bool() const { return pipeline && pipeline->IsValid(); } }; diff --git a/engine/src/flutter/impeller/compositor/command.mm b/engine/src/flutter/impeller/compositor/command.mm index 7170c57f84..3e505af69b 100644 --- a/engine/src/flutter/impeller/compositor/command.mm +++ b/engine/src/flutter/impeller/compositor/command.mm @@ -6,8 +6,97 @@ #include +#include "impeller/compositor/vertex_descriptor.h" + namespace impeller { -// +bool Command::BindVertices(const VertexBuffer& buffer) { + vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = + buffer.vertex_buffer; + index_buffer = buffer.index_buffer; + index_count = buffer.index_count; + return true; +} + +bool Command::BindResource(ShaderStage stage, size_t binding, BufferView view) { + if (!view) { + return false; + } + + switch (stage) { + case ShaderStage::kVertex: + vertex_bindings.buffers[binding] = view; + return true; + case ShaderStage::kFragment: + fragment_bindings.buffers[binding] = view; + return true; + case ShaderStage::kUnknown: + return false; + } + + return false; +} + +bool Command::BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr texture) { + if (!texture || !texture->IsValid()) { + return false; + } + + if (!slot.HasTexture()) { + FML_DLOG(WARNING) << "Texture named " << slot.name + << " is not actually used in the shader."; + return true; + } + + switch (stage) { + case ShaderStage::kVertex: + vertex_bindings.textures[slot.texture_index] = texture; + return true; + case ShaderStage::kFragment: + fragment_bindings.textures[slot.texture_index] = texture; + return true; + case ShaderStage::kUnknown: + return false; + } + + return false; +} + +bool Command::BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr sampler) { + if (!sampler || !sampler->IsValid()) { + return false; + } + + if (!slot.HasSampler()) { + FML_DLOG(WARNING) << "Sampler named " << slot.name + << " is not actually used in the shader."; + return true; + } + + switch (stage) { + case ShaderStage::kVertex: + vertex_bindings.samplers[slot.sampler_index] = sampler; + return true; + case ShaderStage::kFragment: + fragment_bindings.samplers[slot.sampler_index] = sampler; + return true; + case ShaderStage::kUnknown: + return false; + } + + return false; +} + +bool Command::BindResource(ShaderStage stage, + const SampledImageSlot& slot, + std::shared_ptr texture, + std::shared_ptr sampler) { + return BindResource(stage, slot, texture) && + BindResource(stage, slot, sampler); +} } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.h b/engine/src/flutter/impeller/compositor/texture_descriptor.h index 53b8f316a7..07e52ca645 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.h +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.h @@ -37,9 +37,6 @@ struct TextureDescriptor { mip_count >= 1u // ; } - - static std::optional MakeFromImageResult( - const Image& result); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.mm b/engine/src/flutter/impeller/compositor/texture_descriptor.mm index 1c092b2902..c72e4dfd02 100644 --- a/engine/src/flutter/impeller/compositor/texture_descriptor.mm +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.mm @@ -6,44 +6,6 @@ namespace impeller { -std::optional FormatForImageResultComponents(Image::Format comp) { - switch (comp) { - case Image::Format::Invalid: - return std::nullopt; - case Image::Format::Grey: - return std::nullopt; - case Image::Format::GreyAlpha: - return std::nullopt; - case Image::Format::RGB: - return std::nullopt; - case Image::Format::RGBA: - return PixelFormat::kPixelFormat_R8G8B8A8_UNormInt; - } - return std::nullopt; -} - -std::optional TextureDescriptor::MakeFromImageResult( - const Image& result) { - if (!result.IsValid()) { - return std::nullopt; - } - - const auto pixel_format = FormatForImageResultComponents(result.GetFormat()); - if (!pixel_format.has_value()) { - FML_DLOG(ERROR) << "Unknown image format."; - return std::nullopt; - } - - TextureDescriptor desc; - desc.format = pixel_format.value(); - desc.size = result.GetSize(); - desc.mip_count = result.GetSize().MipCount(); - - if (!desc.IsValid()) { - return std::nullopt; - } - - return desc; -} +// } // namespace impeller diff --git a/engine/src/flutter/impeller/playground/playground.mm b/engine/src/flutter/impeller/playground/playground.mm index bc4392acdf..fabe1b7142 100644 --- a/engine/src/flutter/impeller/playground/playground.mm +++ b/engine/src/flutter/impeller/playground/playground.mm @@ -169,20 +169,25 @@ std::shared_ptr Playground::CreateTextureForFixture( const char* fixture_name) const { CompressedImage compressed_image( flutter::testing::OpenFixtureAsMapping(fixture_name)); + // The decoded image is immediately converted into RGBA as that format is + // known to be supported everywhere. For image sources that don't need 32 bit + // pixel strides, this is overkill. Since this is a test fixture we aren't + // necessarily trying to eke out memory savings here and instead favor + // simplicity. auto image = compressed_image.Decode().ConvertToRGBA(); if (!image.IsValid()) { FML_LOG(ERROR) << "Could not find fixture named " << fixture_name; return nullptr; } - auto texture_descriptor = TextureDescriptor::MakeFromImageResult(image); - if (!texture_descriptor.has_value()) { - FML_LOG(ERROR) << "Could not figure out texture descriptor for fixture " - << fixture_name; - return nullptr; - } + + auto texture_descriptor = TextureDescriptor{}; + texture_descriptor.format = PixelFormat::kPixelFormat_R8G8B8A8_UNormInt; + texture_descriptor.size = image.GetSize(); + texture_descriptor.mip_count = 1u; + auto texture = renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture( - StorageMode::kHostVisible, texture_descriptor.value()); + StorageMode::kHostVisible, texture_descriptor); if (!texture) { FML_LOG(ERROR) << "Could not allocate texture for fixture " << fixture_name; return nullptr; diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index fb67740639..fae9e3a754 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -54,18 +54,17 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { ASSERT_TRUE(sampler); Renderer::RenderCallback callback = [&](const Surface& surface, RenderPass& pass) { - BoxVertexShader::UniformBuffer uniforms; - uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width, - surface.GetSize().height); Command cmd; cmd.label = "Box"; cmd.pipeline = box_pipeline; - cmd.vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] = - vertex_buffer.vertex_buffer; - cmd.vertex_bindings - .buffers[BoxVertexShader::kUniformUniformBuffer.binding] = - pass.GetTransientsBuffer().EmplaceUniform(uniforms); + cmd.BindVertices(vertex_buffer); + + BoxVertexShader::UniformBuffer uniforms; + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width, + surface.GetSize().height); + BoxVertexShader::BindUniformBuffer( + cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms)); BoxFragmentShader::FrameInfo frame_info; frame_info.current_time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF(); @@ -73,24 +72,11 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { frame_info.window_size.x = GetWindowSize().width; frame_info.window_size.y = GetWindowSize().height; - cmd.fragment_bindings - .buffers[BoxFragmentShader::kUniformFrameInfo.binding] = - pass.GetTransientsBuffer().EmplaceUniform(frame_info); - cmd.fragment_bindings - .textures[BoxFragmentShader::kSampledImageContents1Texture - .texture_index] = bridge; - cmd.fragment_bindings - .samplers[BoxFragmentShader::kSampledImageContents1Texture - .sampler_index] = sampler; - cmd.fragment_bindings - .textures[BoxFragmentShader::kSampledImageContents2Texture - .texture_index] = boston; - cmd.fragment_bindings - .samplers[BoxFragmentShader::kSampledImageContents2Texture - .sampler_index] = sampler; + BoxFragmentShader::BindFrameInfo( + cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + BoxFragmentShader::BindContents1Texture(cmd, boston, sampler); + BoxFragmentShader::BindContents2Texture(cmd, bridge, sampler); - cmd.index_buffer = vertex_buffer.index_buffer; - cmd.index_count = vertex_buffer.index_count; cmd.primitive_type = PrimitiveType::kTriangle; if (!pass.RecordCommand(std::move(cmd))) { return false; diff --git a/engine/src/flutter/impeller/tools/impeller.gni b/engine/src/flutter/impeller/tools/impeller.gni index 44f3a7ee6e..9d7b1e3ec1 100644 --- a/engine/src/flutter/impeller/tools/impeller.gni +++ b/engine/src/flutter/impeller/tools/impeller.gni @@ -98,7 +98,7 @@ template("impeller_shaders") { spirv_intermediate = "$target_gen_dir/{{source_file_part}}.spirv" reflection_json_intermediate = "$target_gen_dir/{{source_file_part}}.json" reflection_header_intermediate = "$target_gen_dir/{{source_file_part}}.h" - reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.cc" + reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.mm" outputs = [ metal_intermediate, @@ -164,6 +164,7 @@ template("impeller_shaders") { [ "*.h", "*.cc", + "*.mm", ]) deps = [