[Impeller] Add miplevel to Buffer->Texture blit. (flutter/engine#55848)

Part of https://github.com/flutter/flutter/issues/156641
This commit is contained in:
Brandon DeRosier
2024-10-14 09:53:19 -07:00
committed by GitHub
parent 0388529ef9
commit dd68ebf2fb
14 changed files with 99 additions and 17 deletions

View File

@@ -484,7 +484,7 @@ std::shared_ptr<Texture> Playground::CreateTextureCubeForFixture(
auto device_buffer = context_->GetResourceAllocator()->CreateBufferWithCopy(
*images[i].GetAllocation());
blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), texture, {},
"", /*slice=*/i);
"", /*mip_level=*/0, /*slice=*/i);
}
if (!blit_pass->EncodeCommands(context_->GetResourceAllocator()) ||

View File

@@ -270,7 +270,7 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
// defined by a previous glTexImage2D operation.
if (!texture_gles.IsSliceInitialized(slice)) {
gl.TexImage2D(texture_target, // target
0u, // LOD level
mip_level, // LOD level
data.internal_format, // internal format
tex_descriptor.size.width, // width
tex_descriptor.size.height, // height
@@ -285,7 +285,7 @@ bool BlitCopyBufferToTextureCommandGLES::Encode(
{
gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
gl.TexSubImage2D(texture_target, // target
0u, // LOD level
mip_level, // LOD level
destination_region.GetX(), // xoffset
destination_region.GetY(), // yoffset
destination_region.GetWidth(), // width

View File

@@ -132,6 +132,7 @@ bool BlitPassGLES::OnCopyBufferToTextureCommand(
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) {
auto command = std::make_unique<BlitCopyBufferToTextureCommandGLES>();
@@ -140,6 +141,7 @@ bool BlitPassGLES::OnCopyBufferToTextureCommand(
command->destination = std::move(destination);
command->destination_region = destination_region;
command->label = label;
command->mip_level = mip_level;
command->slice = slice;
commands_.push_back(std::move(command));

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_BLIT_PASS_GLES_H_
#define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_BLIT_PASS_GLES_H_
#include <cstdint>
#include <memory>
#include "flutter/impeller/base/config.h"
@@ -63,6 +64,7 @@ class BlitPassGLES final : public BlitPass,
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) override;

View File

@@ -63,6 +63,7 @@ class BlitPassMTL final : public BlitPass {
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) override;

View File

@@ -5,6 +5,7 @@
#include "impeller/renderer/backend/metal/blit_pass_mtl.h"
#include <Metal/Metal.h>
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
#include <cstdint>
#include <memory>
#include <utility>
#include <variant>
@@ -189,6 +190,7 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand(
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) {
auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer();
@@ -226,7 +228,7 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand(
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:slice
destinationLevel:0
destinationLevel:mip_level
destinationOrigin:destination_origin_mtl];
#ifdef IMPELLER_DEBUG

View File

@@ -246,6 +246,7 @@ bool BlitPassVK::OnCopyBufferToTextureCommand(
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) {
const auto& cmd_buffer = command_buffer_->GetCommandBuffer();
@@ -273,8 +274,8 @@ bool BlitPassVK::OnCopyBufferToTextureCommand(
image_copy.setBufferOffset(source.range.offset);
image_copy.setBufferRowLength(0);
image_copy.setBufferImageHeight(0);
image_copy.setImageSubresource(
vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1));
image_copy.setImageSubresource(vk::ImageSubresourceLayers(
vk::ImageAspectFlagBits::eColor, mip_level, slice, 1));
image_copy.imageOffset.x = destination_region.GetX();
image_copy.imageOffset.y = destination_region.GetY();
image_copy.imageOffset.z = 0u;

View File

@@ -64,6 +64,7 @@ class BlitPassVK final : public BlitPass {
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) override;
// |BlitPass|

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BLIT_COMMAND_H_
#define FLUTTER_IMPELLER_RENDERER_BLIT_COMMAND_H_
#include <cstdint>
#include "impeller/core/device_buffer.h"
#include "impeller/core/texture.h"
#include "impeller/geometry/rect.h"
@@ -38,6 +39,7 @@ struct BlitCopyBufferToTextureCommand : public BlitCommand {
BufferView source;
std::shared_ptr<Texture> destination;
IRect destination_region;
uint32_t mip_level = 0;
uint32_t slice = 0;
};

View File

@@ -124,6 +124,7 @@ bool BlitPass::AddCopy(BufferView source,
std::shared_ptr<Texture> destination,
std::optional<IRect> destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) {
if (!destination) {
@@ -150,14 +151,20 @@ bool BlitPass::AddCopy(BufferView source,
<< "Attempted to add a texture blit with out of bounds access.";
return false;
}
if (mip_level >= destination->GetMipCount()) {
VALIDATION_LOG << "Invalid value for mip_level: " << mip_level << ". "
<< "The destination texture has "
<< destination->GetMipCount() << " mip levels.";
return false;
}
if (slice > 5) {
VALIDATION_LOG << "Invalid value for slice: " << slice;
return false;
}
return OnCopyBufferToTextureCommand(std::move(source), std::move(destination),
destination_region_value,
std::move(label), slice, convert_to_read);
return OnCopyBufferToTextureCommand(
std::move(source), std::move(destination), destination_region_value,
std::move(label), mip_level, slice, convert_to_read);
}
bool BlitPass::ConvertTextureToShaderRead(

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BLIT_PASS_H_
#define FLUTTER_IMPELLER_RENDERER_BLIT_PASS_H_
#include <cstdint>
#include <string>
#include "impeller/core/device_buffer.h"
@@ -73,8 +74,8 @@ class BlitPass {
std::string label = "");
//----------------------------------------------------------------------------
/// @brief Record a command to copy the contents of the buffer to
/// the texture.
/// @brief Record a command to copy the contents of a texture to a
/// buffer.
///
/// @param[in] source The texture to read for copying.
/// @param[in] destination The buffer to overwrite using the source
@@ -96,8 +97,8 @@ class BlitPass {
std::string label = "");
//----------------------------------------------------------------------------
/// @brief Record a command to copy the contents of the buffer to
/// the texture.
/// @brief Record a command to copy the contents of a buffer to a
/// texture.
///
/// @param[in] source The buffer view to read for copying.
/// @param[in] destination The texture to overwrite using the source
@@ -107,6 +108,7 @@ class BlitPass {
/// defaults to the entire texture.
/// @param[in] label The optional debug label to give the
/// command.
/// @param[in] mip_level The mip level to write to.
/// @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
@@ -126,6 +128,7 @@ class BlitPass {
std::shared_ptr<Texture> destination,
std::optional<IRect> destination_region = std::nullopt,
std::string label = "",
uint32_t mip_level = 0,
uint32_t slice = 0,
bool convert_to_read = true);
@@ -174,6 +177,7 @@ class BlitPass {
std::shared_ptr<Texture> destination,
IRect destination_region,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read) = 0;

View File

@@ -102,11 +102,14 @@ TEST_P(BlitPassTest, ChecksInvalidSliceParameters) {
ASSERT_TRUE(src);
EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/25));
std::nullopt, "", /*mip_level=*/0,
/*slice=*/25));
EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/6));
std::nullopt, "", /*mip_level=*/0,
/*slice=*/6));
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
std::nullopt, "", /*slice=*/0));
std::nullopt, "", /*mip_level=*/0,
/*slice=*/0));
}
TEST_P(BlitPassTest, CanBlitSmallRegionToUninitializedTexture) {
@@ -128,7 +131,62 @@ TEST_P(BlitPassTest, CanBlitSmallRegionToUninitializedTexture) {
ASSERT_TRUE(dst);
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
IRect::MakeLTRB(0, 0, 1, 1), "", /*slice=*/0));
IRect::MakeLTRB(0, 0, 1, 1), "",
/*mip_level=*/0, /*slice=*/0));
EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
}
TEST_P(BlitPassTest, ChecksInvalidMipLevelParameter) {
ScopedValidationDisable scope;
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 = {1000, 1000};
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
DeviceBufferDescriptor src_format;
src_format.size = 4;
src_format.storage_mode = StorageMode::kHostVisible;
auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
ASSERT_TRUE(dst);
EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
IRect::MakeLTRB(0, 0, 1, 1), "",
/*mip_level=*/1, /*slice=*/0));
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
IRect::MakeLTRB(0, 0, 1, 1), "",
/*mip_level=*/0, /*slice=*/0));
}
TEST_P(BlitPassTest, CanBlitToHigherTextureMipLevels) {
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 = {1000, 1000};
dst_format.mip_count = 4;
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
DeviceBufferDescriptor src_format;
src_format.size = 4;
src_format.storage_mode = StorageMode::kHostVisible;
auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
ASSERT_TRUE(dst);
EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
IRect::MakeLTRB(0, 0, 1, 1), "",
/*mip_level=*/1, /*slice=*/0));
EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
}

View File

@@ -94,6 +94,7 @@ class MockBlitPass : public BlitPass {
std::shared_ptr<Texture> destination,
IRect destination_rect,
std::string label,
uint32_t mip_level,
uint32_t slice,
bool convert_to_read),
(override));

View File

@@ -369,6 +369,7 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
IRect::MakeXYWH(pos.GetLeft() - 1, pos.GetTop() - 1,
size.width, size.height), //
/*label=*/"", //
/*mip_level=*/0, //
/*slice=*/0, //
/*convert_to_read=*/false //
)) {