[Impeller] Update BlitPass::AddCopy to use destination_region instead of origin for buffer to texture copies. (flutter/engine#52555)

Based on https://github.com/flutter/engine/pull/52510

Work towards https://github.com/flutter/flutter/issues/138798

Change IPoint destination_origin to IRect destination_region, which allows us to specify an area smaller than the entire texture to replace. This will eventually allow us to only upload individual glyphs. This fixes the cubemap issue I previously hit: each face needs to track initialization separately.
This commit is contained in:
Jonah Williams
2024-05-08 08:46:06 -07:00
committed by GitHub
parent 13d64bc737
commit e391972983
19 changed files with 165 additions and 48 deletions

View File

@@ -20,6 +20,7 @@
#include "impeller/aiks/image_filter.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/aiks/testing/context_spy.h"
#include "impeller/core/device_buffer.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/constants.h"
@@ -3151,6 +3152,39 @@ TEST_P(AiksTest, CanRenderTextWithLargePerspectiveTransform) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, SetContentsWithRegion) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
// Replace part of the texture with a red rectangle.
std::vector<uint8_t> bytes(100 * 100 * 4);
for (auto i = 0u; i < bytes.size(); i += 4) {
bytes[i] = 255;
bytes[i + 1] = 0;
bytes[i + 2] = 0;
bytes[i + 3] = 255;
}
auto mapping =
std::make_shared<fml::NonOwnedMapping>(bytes.data(), bytes.size());
auto device_buffer =
GetContext()->GetResourceAllocator()->CreateBufferWithCopy(*mapping);
auto cmd_buffer = GetContext()->CreateCommandBuffer();
auto blit_pass = cmd_buffer->CreateBlitPass();
blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), bridge,
IRect::MakeLTRB(50, 50, 150, 150));
auto did_submit =
blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) &&
GetContext()->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok();
ASSERT_TRUE(did_submit);
auto image = std::make_shared<Image>(bridge);
Canvas canvas;
canvas.DrawImage(image, {0, 0}, {});
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
} // namespace testing
} // namespace impeller

View File

@@ -219,7 +219,9 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
}
if (!tex_descriptor.IsValid() ||
source.range.length < tex_descriptor.GetByteSizeOfBaseMipLevel()) {
source.range.length !=
BytesPerPixelForPixelFormat(tex_descriptor.format) *
destination_region.Area()) {
return false;
}
@@ -263,9 +265,9 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
const GLvoid* tex_data =
data.buffer_view.buffer->OnGetContents() + data.buffer_view.range.offset;
{
TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes",
std::to_string(data.buffer_view.range.length).c_str());
// GL_INVALID_OPERATION if the texture array has not been
// defined by a previous glTexImage2D operation.
if (!texture_gles.IsSliceInitialized(slice)) {
gl.TexImage2D(texture_target, // target
0u, // LOD level
data.internal_format, // internal format
@@ -276,8 +278,24 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
data.type, // type
tex_data // data
);
texture_gles.MarkSliceInitialized(slice);
}
{
TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes",
std::to_string(data.buffer_view.range.length).c_str());
gl.TexSubImage2D(texture_target, // target
0u, // LOD level
destination_region.GetX(), // xoffset
destination_region.GetY(), // yoffset
destination_region.GetWidth(), // width
destination_region.GetHeight(), // height
data.external_format, // external format
data.type, // type
tex_data // data
);
}
texture_gles.MarkContentsInitialized();
return true;
}

View File

@@ -130,14 +130,14 @@ bool BlitPassGLES::OnCopyTextureToBufferCommand(
bool BlitPassGLES::OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) {
auto command = std::make_unique<BlitCopyBufferToTextureCommandGLES>();
command->label = label;
command->source = std::move(source);
command->destination = std::move(destination);
command->destination_origin = destination_origin;
command->destination_region = destination_region;
command->label = label;
command->slice = slice;

View File

@@ -57,7 +57,7 @@ class BlitPassGLES final : public BlitPass,
// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) override;

View File

@@ -170,6 +170,7 @@ struct GLProc {
PROC(StencilMaskSeparate); \
PROC(StencilOpSeparate); \
PROC(TexImage2D); \
PROC(TexSubImage2D); \
PROC(TexParameteri); \
PROC(TexParameterfv); \
PROC(Uniform1fv); \

View File

@@ -304,8 +304,8 @@ bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
}
};
contents_initialized_ = reactor_->AddOperation(texture_upload);
return contents_initialized_;
slices_initialized_ = reactor_->AddOperation(texture_upload);
return slices_initialized_[0];
}
// |Texture|
@@ -343,13 +343,10 @@ static std::optional<GLenum> ToRenderBufferFormat(PixelFormat format) {
}
void TextureGLES::InitializeContentsIfNecessary() const {
if (!IsValid()) {
if (!IsValid() || slices_initialized_[0]) {
return;
}
if (contents_initialized_) {
return;
}
contents_initialized_ = true;
slices_initialized_[0] = true;
if (is_wrapped_) {
return;
@@ -455,8 +452,12 @@ bool TextureGLES::Bind() const {
return true;
}
void TextureGLES::MarkContentsInitialized() const {
contents_initialized_ = true;
void TextureGLES::MarkSliceInitialized(size_t slice) const {
slices_initialized_[slice] = true;
}
bool TextureGLES::IsSliceInitialized(size_t slice) const {
return slices_initialized_[slice];
}
bool TextureGLES::GenerateMipmap() {

View File

@@ -5,6 +5,8 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
#define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
#include <bitset>
#include "impeller/base/backend_cast.h"
#include "impeller/core/texture.h"
#include "impeller/renderer/backend/gles/handle_gles.h"
@@ -60,13 +62,18 @@ class TextureGLES final : public Texture,
std::optional<GLuint> GetFBO() const { return wrapped_fbo_; }
void MarkContentsInitialized() const;
// For non cubemap textures, 0 indicates uninitialized and 1 indicates
// initialized. For cubemap textures, each face is initialized separately with
// each bit tracking the initialization of the corresponding slice.
void MarkSliceInitialized(size_t slice) const;
bool IsSliceInitialized(size_t slice) const;
private:
ReactorGLES::Ref reactor_;
const Type type_;
HandleGLES handle_;
mutable bool contents_initialized_ = false;
mutable std::bitset<6> slices_initialized_ = 0;
const bool is_wrapped_;
const std::optional<GLuint> wrapped_fbo_;
bool is_valid_ = false;

View File

@@ -114,22 +114,21 @@ bool BlitCopyBufferToTextureCommandMTL::Encode(
}
auto destination_origin_mtl =
MTLOriginMake(destination_origin.x, destination_origin.y, 0);
auto image_size = destination->GetTextureDescriptor().size;
auto source_size_mtl = MTLSizeMake(image_size.width, image_size.height, 1);
MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0);
auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(),
destination_region.GetHeight(), 1);
auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto destination_bytes_per_row =
source_size_mtl.width * destination_bytes_per_pixel;
auto destination_bytes_per_image =
source_size_mtl.height * destination_bytes_per_row;
destination_region.GetWidth() * destination_bytes_per_pixel;
[encoder copyFromBuffer:source_mtl
sourceOffset:source.range.offset
sourceBytesPerRow:destination_bytes_per_row
sourceBytesPerImage:destination_bytes_per_image
sourceBytesPerImage:
0 // 0 for 2D textures according to
// https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:slice

View File

@@ -55,7 +55,7 @@ class BlitPassMTL final : public BlitPass {
// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) override;

View File

@@ -127,14 +127,14 @@ bool BlitPassMTL::OnCopyTextureToBufferCommand(
bool BlitPassMTL::OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) {
auto command = std::make_unique<BlitCopyBufferToTextureCommandMTL>();
command->label = std::move(label);
command->source = std::move(source);
command->destination = std::move(destination);
command->destination_origin = destination_origin;
command->destination_region = destination_region;
command->slice = slice;
commands_.emplace_back(std::move(command));

View File

@@ -246,10 +246,12 @@ bool BlitCopyBufferToTextureCommandVK::Encode(CommandEncoderVK& encoder) const {
image_copy.setBufferImageHeight(0);
image_copy.setImageSubresource(
vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1));
image_copy.setImageOffset(
vk::Offset3D(destination_origin.x, destination_origin.y, 0));
image_copy.setImageExtent(vk::Extent3D(destination->GetSize().width,
destination->GetSize().height, 1));
image_copy.imageOffset.x = destination_region.GetX();
image_copy.imageOffset.y = destination_region.GetY();
image_copy.imageOffset.z = 0u;
image_copy.imageExtent.width = destination_region.GetWidth();
image_copy.imageExtent.height = destination_region.GetHeight();
image_copy.imageExtent.depth = 1u;
if (!dst.SetLayout(dst_barrier)) {
VALIDATION_LOG << "Could not encode layout transition.";

View File

@@ -96,14 +96,14 @@ bool BlitPassVK::OnCopyTextureToBufferCommand(
bool BlitPassVK::OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) {
auto command = std::make_unique<BlitCopyBufferToTextureCommandVK>();
command->source = std::move(source);
command->destination = std::move(destination);
command->destination_origin = destination_origin;
command->destination_region = destination_region;
command->label = std::move(label);
command->slice = slice;

View File

@@ -6,6 +6,7 @@
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_BLIT_PASS_VK_H_
#include "flutter/impeller/base/config.h"
#include "impeller/geometry/rect.h"
#include "impeller/renderer/backend/vulkan/blit_command_vk.h"
#include "impeller/renderer/blit_pass.h"
@@ -55,7 +56,7 @@ class BlitPassVK final : public BlitPass {
// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) override;
// |BlitPass|

View File

@@ -32,7 +32,7 @@ struct BlitCopyTextureToBufferCommand : public BlitCommand {
struct BlitCopyBufferToTextureCommand : public BlitCommand {
BufferView source;
std::shared_ptr<Texture> destination;
IPoint destination_origin;
IRect destination_region;
uint32_t slice = 0;
};

View File

@@ -122,28 +122,41 @@ bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
bool BlitPass::AddCopy(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::optional<IRect> destination_region,
std::string label,
uint32_t slice) {
if (!destination) {
VALIDATION_LOG << "Attempted to add a texture blit with no destination.";
return false;
}
ISize destination_size = destination->GetSize();
IRect destination_region_value =
destination_region.value_or(IRect::MakeSize(destination_size));
if (destination_region_value.GetX() < 0 ||
destination_region_value.GetY() < 0 ||
destination_region_value.GetRight() > destination_size.width ||
destination_region_value.GetBottom() > destination_size.height) {
VALIDATION_LOG << "Blit region cannot be larger than destination texture.";
return false;
}
auto bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto bytes_per_image =
destination->GetTextureDescriptor().size.Area() * bytes_per_pixel;
auto bytes_per_image = destination_region_value.Area() * bytes_per_pixel;
if (source.range.length != bytes_per_image) {
VALIDATION_LOG
<< "Attempted to add a texture blit with out of bounds access.";
return false;
}
if (slice > 5) {
VALIDATION_LOG << "Invalid value for slice: " << slice;
return false;
}
return OnCopyBufferToTextureCommand(std::move(source), std::move(destination),
destination_origin, std::move(label),
slice);
destination_region_value,
std::move(label), slice);
}
bool BlitPass::GenerateMipmap(std::shared_ptr<Texture> texture,

View File

@@ -89,8 +89,9 @@ class BlitPass {
/// @param[in] source The buffer view to read for copying.
/// @param[in] destination The texture to overwrite using the source
/// contents.
/// @param[in] destination_offset The offset to start writing to in the
/// destination buffer.
/// @param[in] destination_region The offset to start writing to in the
/// destination texture. If not provided, this
/// defaults to the entire texture.
/// @param[in] label The optional debug label to give the
/// command.
/// @param[in] slice For cubemap textures, the slice to write
@@ -98,9 +99,17 @@ class BlitPass {
///
/// @return If the command was valid for subsequent commitment.
///
/// If a region smaller than the texture size is provided, the
/// contents are treated as containing tightly packed pixel data of
/// that region. Only the portion of the texture in this region is
/// replaced and existing data is preserved.
///
/// For example, to replace the top left 10 x 10 region of a larger
/// 100 x 100 texture, the region is {0, 0, 10, 10} and the expected
/// buffer size in bytes is 100 x bpp.
bool AddCopy(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin = {},
std::optional<IRect> destination_region = std::nullopt,
std::string label = "",
uint32_t slice = 0);
@@ -148,7 +157,7 @@ class BlitPass {
virtual bool OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_region,
std::string label,
uint32_t slice) = 0;

View File

@@ -4,6 +4,7 @@
#include "gtest/gtest.h"
#include "impeller/base/validation.h"
#include "impeller/core/device_buffer_descriptor.h"
#include "impeller/core/formats.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/playground/playground_test.h"
@@ -75,5 +76,33 @@ TEST_P(BlitPassTest, BlitPassesForMatchingFormats) {
EXPECT_TRUE(blit_pass->AddCopy(src, dst));
}
TEST_P(BlitPassTest, ChecksInvalidSliceParameters) {
ScopedValidationDisable scope; // avoid noise in output.
auto context = GetContext();
auto cmd_buffer = context->CreateCommandBuffer();
auto blit_pass = cmd_buffer->CreateBlitPass();
TextureDescriptor dst_format;
dst_format.storage_mode = StorageMode::kDevicePrivate;
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
dst_format.size = {100, 100};
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
DeviceBufferDescriptor src_format;
src_format.size = 40000;
src_format.storage_mode = StorageMode::kHostVisible;
auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
ASSERT_TRUE(dst);
ASSERT_TRUE(src);
EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/25));
EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/6));
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/0));
}
} // namespace testing
} // namespace impeller

View File

@@ -82,7 +82,7 @@ class MockBlitPass : public BlitPass {
OnCopyBufferToTextureCommand,
(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
IRect destination_rect,
std::string label,
uint32_t slice),
(override));

View File

@@ -702,6 +702,9 @@ impeller_Play_AiksTest_SaveLayerDrawsBehindSubsequentEntities_Vulkan.png
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Metal.png
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_OpenGLES.png
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Vulkan.png
impeller_Play_AiksTest_SetContentsWithRegion_Metal.png
impeller_Play_AiksTest_SetContentsWithRegion_OpenGLES.png
impeller_Play_AiksTest_SetContentsWithRegion_Vulkan.png
impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_Metal.png
impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_OpenGLES.png
impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_Vulkan.png