diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc index 363400df99..d83ef32685 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc @@ -132,7 +132,8 @@ bool BlitPassGLES::OnCopyBufferToTextureCommand( std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) { + uint32_t slice, + bool convert_to_read) { auto command = std::make_unique(); command->label = label; command->source = std::move(source); diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h index 1b5385f874..e34bb1614a 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h @@ -59,7 +59,8 @@ class BlitPassGLES final : public BlitPass, std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) override; + uint32_t slice, + bool convert_to_read) override; // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h index 3363eb969f..1de0f45aed 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h @@ -58,7 +58,8 @@ class BlitPassMTL final : public BlitPass { std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) override; + uint32_t slice, + bool convert_to_read) override; // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm index 9caa449cdf..329fe5197c 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm @@ -166,7 +166,8 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand( std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) { + uint32_t slice, + bool convert_to_read) { auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer(); if (!source_mtl) { return false; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc index 289561a86e..b8c5e9803f 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc @@ -221,13 +221,37 @@ bool BlitPassVK::OnCopyTextureToBufferCommand( return true; } +bool BlitPassVK::ConvertTextureToShaderRead( + const std::shared_ptr& texture) { + auto& encoder = *command_buffer_->GetEncoder(); + const auto& cmd_buffer = encoder.GetCommandBuffer(); + + BarrierVK barrier; + barrier.cmd_buffer = cmd_buffer; + barrier.src_access = vk::AccessFlagBits::eTransferWrite; + barrier.src_stage = vk::PipelineStageFlagBits::eTransfer; + barrier.dst_access = vk::AccessFlagBits::eShaderRead; + barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader; + + barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal; + + const auto& texture_vk = TextureVK::Cast(*texture); + + if (!encoder.Track(texture)) { + return false; + } + + return texture_vk.SetLayout(barrier); +} + // |BlitPass| bool BlitPassVK::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) { + uint32_t slice, + bool convert_to_read) { auto& encoder = *command_buffer_->GetEncoder(); const auto& cmd_buffer = encoder.GetCommandBuffer(); @@ -262,6 +286,9 @@ bool BlitPassVK::OnCopyBufferToTextureCommand( image_copy.imageExtent.height = destination_region.GetHeight(); image_copy.imageExtent.depth = 1u; + // Note: this barrier should do nothing if we're already in the transfer dst + // optimal state. This is important for performance of repeated blit pass + // encoding. if (!dst.SetLayout(dst_barrier)) { VALIDATION_LOG << "Could not encode layout transition."; return false; @@ -274,7 +301,7 @@ bool BlitPassVK::OnCopyBufferToTextureCommand( ); // Transition to shader-read. - { + if (convert_to_read) { BarrierVK barrier; barrier.cmd_buffer = cmd_buffer; barrier.src_access = vk::AccessFlagBits::eTransferWrite; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h index b3967ed8c8..7d9c4c7368 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h @@ -37,6 +37,10 @@ class BlitPassVK final : public BlitPass { bool EncodeCommands( const std::shared_ptr& transients_allocator) const override; + // |BlitPass| + bool ConvertTextureToShaderRead( + const std::shared_ptr& texture) override; + // |BlitPass| bool OnCopyTextureToTextureCommand(std::shared_ptr source, std::shared_ptr destination, @@ -56,7 +60,8 @@ class BlitPassVK final : public BlitPass { std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) override; + uint32_t slice, + bool convert_to_read) override; // |BlitPass| bool OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) override; diff --git a/engine/src/flutter/impeller/renderer/blit_pass.cc b/engine/src/flutter/impeller/renderer/blit_pass.cc index e215b05e8d..0126c6566c 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass.cc @@ -124,7 +124,8 @@ bool BlitPass::AddCopy(BufferView source, std::shared_ptr destination, std::optional destination_region, std::string label, - uint32_t slice) { + uint32_t slice, + bool convert_to_read) { if (!destination) { VALIDATION_LOG << "Attempted to add a texture blit with no destination."; return false; @@ -156,7 +157,12 @@ bool BlitPass::AddCopy(BufferView source, return OnCopyBufferToTextureCommand(std::move(source), std::move(destination), destination_region_value, - std::move(label), slice); + std::move(label), slice, convert_to_read); +} + +bool BlitPass::ConvertTextureToShaderRead( + const std::shared_ptr& texture) { + return true; } bool BlitPass::GenerateMipmap(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/blit_pass.h b/engine/src/flutter/impeller/renderer/blit_pass.h index 5b2f56b33b..6f5f332b3a 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.h +++ b/engine/src/flutter/impeller/renderer/blit_pass.h @@ -31,12 +31,19 @@ class BlitPass { void SetLabel(std::string label); + //---------------------------------------------------------------------------- + /// @brief If the texture is not already in a shader read internal + /// state, then convert it to that state. + /// + /// This API is only used by Vulkan. + virtual bool ConvertTextureToShaderRead( + const std::shared_ptr& texture); + //---------------------------------------------------------------------------- /// @brief Record a command to copy the contents of one texture to /// another texture. The blit area is limited by the intersection /// of the texture coverage with respect the source region and /// destination origin. - /// No work is encoded into the command buffer at this time. /// /// @param[in] source The texture to read for copying. /// @param[in] destination The texture to overwrite using the source @@ -60,7 +67,6 @@ class BlitPass { //---------------------------------------------------------------------------- /// @brief Record a command to copy the contents of the buffer to /// the texture. - /// No work is encoded into the command buffer at this time. /// /// @param[in] source The texture to read for copying. /// @param[in] destination The buffer to overwrite using the source @@ -84,7 +90,6 @@ class BlitPass { //---------------------------------------------------------------------------- /// @brief Record a command to copy the contents of the buffer to /// the texture. - /// No work is encoded into the command buffer at this time. /// /// @param[in] source The buffer view to read for copying. /// @param[in] destination The texture to overwrite using the source @@ -96,6 +101,8 @@ class BlitPass { /// command. /// @param[in] slice For cubemap textures, the slice to write /// data to. + /// @param[in] convert_to_read Whether to convert the texture to a shader + /// read state. Defaults to true. /// /// @return If the command was valid for subsequent commitment. /// @@ -111,11 +118,11 @@ class BlitPass { std::shared_ptr destination, std::optional destination_region = std::nullopt, std::string label = "", - uint32_t slice = 0); + uint32_t slice = 0, + bool convert_to_read = true); //---------------------------------------------------------------------------- /// @brief Record a command to generate all mip levels for a texture. - /// No work is encoded into the command buffer at this time. /// /// @param[in] texture The texture to generate mipmaps for. /// @param[in] label The optional debug label to give the command. @@ -159,7 +166,8 @@ class BlitPass { std::shared_ptr destination, IRect destination_region, std::string label, - uint32_t slice) = 0; + uint32_t slice, + bool convert_to_read) = 0; virtual bool OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) = 0; diff --git a/engine/src/flutter/impeller/renderer/testing/mocks.h b/engine/src/flutter/impeller/renderer/testing/mocks.h index c333ac9624..d4e27449bf 100644 --- a/engine/src/flutter/impeller/renderer/testing/mocks.h +++ b/engine/src/flutter/impeller/renderer/testing/mocks.h @@ -84,7 +84,8 @@ class MockBlitPass : public BlitPass { std::shared_ptr destination, IRect destination_rect, std::string label, - uint32_t slice), + uint32_t slice, + bool convert_to_read), (override)); MOCK_METHOD(bool, OnGenerateMipmapCommand, diff --git a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc index 14485f4dd8..52616ed85d 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc @@ -298,13 +298,21 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, DrawGlyph(canvas, pair.scaled_font, pair.glyph, has_color); - if (!blit_pass->AddCopy(allocator.TakeBufferView(), texture, + // convert_to_read is set to false so that the texture remains in a transfer + // dst layout until we finish writing to it below. This only has an impact + // on Vulkan where we are responsible for managing image layouts. + if (!blit_pass->AddCopy(allocator.TakeBufferView(), // + texture, // IRect::MakeXYWH(pos->GetLeft(), pos->GetTop(), - size.width, size.height))) { + size.width, size.height), // + /*label=*/"", // + /*slice=*/0, // + /*convert_to_read=*/false // + )) { return false; } } - return true; + return blit_pass->ConvertTextureToShaderRead(texture); } std::shared_ptr TypographerContextSkia::CreateGlyphAtlas(