[Impeller] Cache render target texture allocations across frames. (flutter/engine#44527)

When allocating a non-host visible texture for a render target, the allocator will hold onto a reference to this texture descriptor. On the immediate next frame, this texture is made available to replace requested allocations for new render target textures. if this texture is not used, then at the end of the next frame it is discarded.

This removes the vast majority of allocations in most flutter gallery and wonderous. This does not attempt to use different sized textures for the cache.

There are two caveats noted in the PR contents.

Fixes https://github.com/flutter/flutter/issues/131515
This commit is contained in:
Jonah Williams
2023-08-15 17:25:08 -07:00
committed by GitHub
parent e9f54512c8
commit 01c2363b16
25 changed files with 432 additions and 38 deletions

View File

@@ -128,12 +128,14 @@
../../../flutter/impeller/compiler/README.md
../../../flutter/impeller/compiler/compiler_unittests.cc
../../../flutter/impeller/compiler/switches_unittests.cc
../../../flutter/impeller/core/allocator_unittests.cc
../../../flutter/impeller/display_list/dl_unittests.cc
../../../flutter/impeller/display_list/skia_conversions_unittests.cc
../../../flutter/impeller/docs
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
../../../flutter/impeller/entity/entity_unittests.cc
../../../flutter/impeller/entity/geometry/geometry_unittests.cc
../../../flutter/impeller/entity/render_target_cache_unittests.cc
../../../flutter/impeller/fixtures
../../../flutter/impeller/geometry/README.md
../../../flutter/impeller/geometry/geometry_unittests.cc

View File

@@ -1271,6 +1271,8 @@ ORIGIN: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc + ../../.
ORIGIN: ../../../flutter/impeller/entity/geometry/vertices_geometry.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/inline_pass_context.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/render_target_cache.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/render_target_cache.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag + ../../../flutter/LICENSE
@@ -3994,6 +3996,8 @@ FILE: ../../../flutter/impeller/entity/geometry/vertices_geometry.cc
FILE: ../../../flutter/impeller/entity/geometry/vertices_geometry.h
FILE: ../../../flutter/impeller/entity/inline_pass_context.cc
FILE: ../../../flutter/impeller/entity/inline_pass_context.h
FILE: ../../../flutter/impeller/entity/render_target_cache.cc
FILE: ../../../flutter/impeller/entity/render_target_cache.h
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert
FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag

View File

@@ -77,6 +77,7 @@ impeller_component("impeller_unittests") {
"base:base_unittests",
"blobcat:blobcat_unittests",
"compiler:compiler_unittests",
"core:allocator_unittests",
"display_list:skia_conversions_unittests",
"geometry:geometry_unittests",
"runtime_stage:runtime_stage_unittests",
@@ -89,6 +90,7 @@ impeller_component("impeller_unittests") {
"aiks:aiks_unittests",
"display_list:display_list_unittests",
"entity:entity_unittests",
"entity:render_target_cache_unittests",
"fixtures",
"geometry:geometry_unittests",
"image:image_unittests",

View File

@@ -54,10 +54,15 @@ std::shared_ptr<Texture> Picture::RenderToTexture(
// This texture isn't host visible, but we might want to add host visible
// features to Image someday.
auto impeller_context = context.GetContext();
// Do not use the render target cache as the lifecycle of this texture
// will outlive a particular frame.
RenderTargetAllocator render_target_allocator =
RenderTargetAllocator(impeller_context->GetResourceAllocator());
RenderTarget target;
if (impeller_context->GetCapabilities()->SupportsOffscreenMSAA()) {
target = RenderTarget::CreateOffscreenMSAA(
*impeller_context, // context
render_target_allocator, // allocator
size, // size
"Picture Snapshot MSAA", // label
RenderTarget::
@@ -70,6 +75,7 @@ std::shared_ptr<Texture> Picture::RenderToTexture(
} else {
target = RenderTarget::CreateOffscreen(
*impeller_context, // context
render_target_allocator, // allocator
size, // size
"Picture Snapshot", // label
RenderTarget::kDefaultColorAttachmentConfig // color_attachment_config

View File

@@ -49,3 +49,15 @@ impeller_component("core") {
"//flutter/fml",
]
}
impeller_component("allocator_unittests") {
testonly = true
sources = [ "allocator_unittests.cc" ]
deps = [
":core",
"../geometry",
"//flutter/testing:testing_lib",
]
}

View File

@@ -6,6 +6,7 @@
#include "impeller/base/validation.h"
#include "impeller/core/device_buffer.h"
#include "impeller/core/formats.h"
#include "impeller/core/range.h"
namespace impeller {
@@ -57,10 +58,10 @@ std::shared_ptr<Texture> Allocator::CreateTexture(
return OnCreateTexture(desc);
}
void Allocator::DidAcquireSurfaceFrame() {}
uint16_t Allocator::MinimumBytesPerRow(PixelFormat format) const {
return BytesPerPixelForPixelFormat(format);
}
void Allocator::DidAcquireSurfaceFrame() {}
} // namespace impeller

View File

@@ -9,7 +9,9 @@
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "impeller/core/device_buffer_descriptor.h"
#include "impeller/core/texture.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/geometry/size.h"
namespace impeller {

View File

@@ -0,0 +1,83 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "flutter/testing/testing.h"
#include "impeller/core/allocator.h"
#include "impeller/core/formats.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/geometry/size.h"
#include "impeller/renderer/testing/mocks.h"
namespace impeller {
namespace testing {
TEST(AllocatorTest, TextureDescriptorCompatibility) {
// Size.
{
TextureDescriptor desc_a = {.size = ISize(100, 100)};
TextureDescriptor desc_b = {.size = ISize(100, 100)};
TextureDescriptor desc_c = {.size = ISize(101, 100)};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Storage Mode.
{
TextureDescriptor desc_a = {.storage_mode = StorageMode::kDevicePrivate};
TextureDescriptor desc_b = {.storage_mode = StorageMode::kDevicePrivate};
TextureDescriptor desc_c = {.storage_mode = StorageMode::kHostVisible};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Format.
{
TextureDescriptor desc_a = {.format = PixelFormat::kR8G8B8A8UNormInt};
TextureDescriptor desc_b = {.format = PixelFormat::kR8G8B8A8UNormInt};
TextureDescriptor desc_c = {.format = PixelFormat::kB10G10R10A10XR};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Sample Count.
{
TextureDescriptor desc_a = {.sample_count = SampleCount::kCount4};
TextureDescriptor desc_b = {.sample_count = SampleCount::kCount4};
TextureDescriptor desc_c = {.sample_count = SampleCount::kCount1};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Sample Count.
{
TextureDescriptor desc_a = {.type = TextureType::kTexture2DMultisample};
TextureDescriptor desc_b = {.type = TextureType::kTexture2DMultisample};
TextureDescriptor desc_c = {.type = TextureType::kTexture2D};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Compression.
{
TextureDescriptor desc_a = {.compression_type = CompressionType::kLossless};
TextureDescriptor desc_b = {.compression_type = CompressionType::kLossless};
TextureDescriptor desc_c = {.compression_type = CompressionType::kLossy};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
// Mip Count.
{
TextureDescriptor desc_a = {.mip_count = 1};
TextureDescriptor desc_b = {.mip_count = 1};
TextureDescriptor desc_c = {.mip_count = 4};
ASSERT_EQ(desc_a, desc_b);
ASSERT_NE(desc_a, desc_c);
}
}
} // namespace testing
} // namespace impeller

View File

@@ -66,6 +66,21 @@ struct TextureDescriptor {
return IsMultisampleCapable(type) ? count > 1 : count == 1;
}
constexpr bool operator==(const TextureDescriptor& other) const {
return size == other.size && //
storage_mode == other.storage_mode && //
format == other.format && //
usage == other.usage && //
sample_count == other.sample_count && //
type == other.type && //
compression_type == other.compression_type && //
mip_count == other.mip_count;
}
constexpr bool operator!=(const TextureDescriptor& other) const {
return !(*this == other);
}
constexpr bool IsValid() const {
return format != PixelFormat::kUnknown && //
size.IsPositive() && //

View File

@@ -243,6 +243,8 @@ impeller_component("entity") {
"geometry/vertices_geometry.h",
"inline_pass_context.cc",
"inline_pass_context.h",
"render_target_cache.cc",
"render_target_cache.h",
]
if (impeller_debug) {
@@ -283,3 +285,14 @@ impeller_component("entity_unittests") {
"../playground:playground_test",
]
}
impeller_component("render_target_cache_unittests") {
testonly = true
sources = [ "render_target_cache_unittests.cc" ]
deps = [
":entity",
"//flutter/testing:testing_lib",
]
}

View File

@@ -10,6 +10,7 @@
#include "impeller/base/strings.h"
#include "impeller/core/formats.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/render_target_cache.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/pipeline_library.h"
#include "impeller/renderer/render_pass.h"
@@ -162,7 +163,9 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
: context_(std::move(context)),
lazy_glyph_atlas_(std::make_shared<LazyGlyphAtlas>()),
tessellator_(std::make_shared<Tessellator>()),
scene_context_(std::make_shared<scene::SceneContext>(context_)) {
scene_context_(std::make_shared<scene::SceneContext>(context_)),
render_target_cache_(std::make_shared<RenderTargetCache>(
context_->GetResourceAllocator())) {
if (!context_ || !context_->IsValid()) {
return;
}
@@ -359,7 +362,8 @@ std::shared_ptr<Texture> ContentContext::MakeSubpass(
RenderTarget subpass_target;
if (context->GetCapabilities()->SupportsOffscreenMSAA() && msaa_enabled) {
subpass_target = RenderTarget::CreateOffscreenMSAA(
*context, texture_size, SPrintF("%s Offscreen", label.c_str()),
*context, *GetRenderTargetCache().get(), texture_size,
SPrintF("%s Offscreen", label.c_str()),
RenderTarget::kDefaultColorAttachmentConfigMSAA //
#ifndef FML_OS_ANDROID // Reduce PSO variants for Vulkan.
,
@@ -368,7 +372,8 @@ std::shared_ptr<Texture> ContentContext::MakeSubpass(
);
} else {
subpass_target = RenderTarget::CreateOffscreen(
*context, texture_size, SPrintF("%s Offscreen", label.c_str()),
*context, *GetRenderTargetCache().get(), texture_size,
SPrintF("%s Offscreen", label.c_str()),
RenderTarget::kDefaultColorAttachmentConfig //
#ifndef FML_OS_ANDROID // Reduce PSO variants for Vulkan.
,

View File

@@ -17,6 +17,7 @@
#include "impeller/entity/entity.h"
#include "impeller/renderer/capabilities.h"
#include "impeller/renderer/pipeline.h"
#include "impeller/renderer/render_target.h"
#include "impeller/scene/scene_context.h"
#ifdef IMPELLER_DEBUG
@@ -334,6 +335,7 @@ struct ContentContextOptions {
};
class Tessellator;
class RenderTargetCache;
class ContentContext {
public:
@@ -708,6 +710,10 @@ class ContentContext {
return lazy_glyph_atlas_;
}
std::shared_ptr<RenderTargetAllocator> GetRenderTargetCache() const {
return render_target_cache_;
}
private:
std::shared_ptr<Context> context_;
std::shared_ptr<LazyGlyphAtlas> lazy_glyph_atlas_;
@@ -865,6 +871,7 @@ class ContentContext {
bool is_valid_ = false;
std::shared_ptr<Tessellator> tessellator_;
std::shared_ptr<scene::SceneContext> scene_context_;
std::shared_ptr<RenderTargetAllocator> render_target_cache_;
bool wireframe_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(ContentContext);

View File

@@ -45,9 +45,10 @@ bool SceneContents::Render(const ContentContext& renderer,
}
RenderTarget subpass_target = RenderTarget::CreateOffscreenMSAA(
*renderer.GetContext(), // context
ISize(coverage.value().size), // size
"SceneContents", // label
*renderer.GetContext(), // context
*renderer.GetRenderTargetCache().get(), // allocator
ISize(coverage.value().size), // size
"SceneContents", // label
RenderTarget::AttachmentConfigMSAA{
.storage_mode = StorageMode::kDeviceTransient,
.resolve_storage_mode = StorageMode::kDevicePrivate,

View File

@@ -209,9 +209,10 @@ static EntityPassTarget CreateRenderTarget(ContentContext& renderer,
RenderTarget target;
if (context->GetCapabilities()->SupportsOffscreenMSAA()) {
target = RenderTarget::CreateOffscreenMSAA(
*context, // context
size, // size
"EntityPass", // label
*context, // context
*renderer.GetRenderTargetCache().get(), // allocator
size, // size
"EntityPass", // label
RenderTarget::AttachmentConfigMSAA{
.storage_mode = StorageMode::kDeviceTransient,
.resolve_storage_mode = StorageMode::kDevicePrivate,
@@ -222,9 +223,10 @@ static EntityPassTarget CreateRenderTarget(ContentContext& renderer,
);
} else {
target = RenderTarget::CreateOffscreen(
*context, // context
size, // size
"EntityPass", // label
*context, // context
*renderer.GetRenderTargetCache().get(), // allocator
size, // size
"EntityPass", // label
RenderTarget::AttachmentConfig{
.storage_mode = StorageMode::kDevicePrivate,
.load_action = LoadAction::kDontCare,
@@ -248,6 +250,7 @@ uint32_t EntityPass::GetTotalPassReads(ContentContext& renderer) const {
bool EntityPass::Render(ContentContext& renderer,
const RenderTarget& render_target) const {
renderer.GetRenderTargetCache()->Start();
auto root_render_target = render_target;
if (root_render_target.GetColorAttachments().find(0u) ==
@@ -256,8 +259,10 @@ bool EntityPass::Render(ContentContext& renderer,
return false;
}
fml::ScopedCleanupClosure reset_lazy_glyph_atlas(
[&renderer]() { renderer.GetLazyGlyphAtlas()->ResetTextFrames(); });
fml::ScopedCleanupClosure reset_state([&renderer]() {
renderer.GetLazyGlyphAtlas()->ResetTextFrames();
renderer.GetRenderTargetCache()->End();
});
IterateAllEntities([lazy_glyph_atlas =
renderer.GetLazyGlyphAtlas()](const Entity& entity) {
@@ -381,7 +386,8 @@ bool EntityPass::Render(ContentContext& renderer,
// provided by the caller.
else {
root_render_target.SetupStencilAttachment(
*renderer.GetContext(), color0.texture->GetSize(),
*renderer.GetContext(), *renderer.GetRenderTargetCache().get(),
color0.texture->GetSize(),
renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),
"ImpellerOnscreen",
GetDefaultStencilConfig(reads_from_onscreen_backdrop));

View File

@@ -0,0 +1,53 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/entity/render_target_cache.h"
#include "impeller/renderer/render_target.h"
namespace impeller {
RenderTargetCache::RenderTargetCache(std::shared_ptr<Allocator> allocator)
: RenderTargetAllocator(std::move(allocator)) {}
void RenderTargetCache::Start() {
for (auto& td : texture_data_) {
td.used_this_frame = false;
}
}
void RenderTargetCache::End() {
std::vector<TextureData> retain;
for (auto td : texture_data_) {
if (td.used_this_frame) {
retain.push_back(td);
}
}
texture_data_.swap(retain);
}
size_t RenderTargetCache::CachedTextureCount() const {
return texture_data_.size();
}
std::shared_ptr<Texture> RenderTargetCache::CreateTexture(
const TextureDescriptor& desc) {
FML_DCHECK(desc.storage_mode != StorageMode::kHostVisible);
FML_DCHECK(desc.usage &
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget));
for (auto& td : texture_data_) {
const auto other_desc = td.texture->GetTextureDescriptor();
if (!td.used_this_frame && desc == other_desc) {
td.used_this_frame = true;
return td.texture;
}
}
auto result = RenderTargetAllocator::CreateTexture(desc);
texture_data_.push_back(
TextureData{.used_this_frame = true, .texture = result});
return result;
}
} // namespace impeller

View File

@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include "impeller/renderer/render_target.h"
namespace impeller {
/// @brief An implementation of the [RenderTargetAllocator] that caches all
/// allocated texture data for one frame.
///
/// Any textures unused after a frame are immediately discarded.
class RenderTargetCache : public RenderTargetAllocator {
public:
explicit RenderTargetCache(std::shared_ptr<Allocator> allocator);
~RenderTargetCache() = default;
// |RenderTargetAllocator|
void Start() override;
// |RenderTargetAllocator|
void End() override;
// |RenderTargetAllocator|
std::shared_ptr<Texture> CreateTexture(
const TextureDescriptor& desc) override;
// visible for testing.
size_t CachedTextureCount() const;
private:
struct TextureData {
bool used_this_frame;
std::shared_ptr<Texture> texture;
};
std::vector<TextureData> texture_data_;
FML_DISALLOW_COPY_AND_ASSIGN(RenderTargetCache);
};
} // namespace impeller

View File

@@ -0,0 +1,66 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "flutter/testing/testing.h"
#include "impeller/core/allocator.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/entity/render_target_cache.h"
#include "impeller/renderer/testing/mocks.h"
namespace impeller {
namespace testing {
class TestAllocator : public Allocator {
public:
TestAllocator() = default;
~TestAllocator() = default;
ISize GetMaxTextureSizeSupported() const override {
return ISize(1024, 1024);
};
std::shared_ptr<DeviceBuffer> OnCreateBuffer(
const DeviceBufferDescriptor& desc) override {
return std::make_shared<MockDeviceBuffer>(desc);
};
virtual std::shared_ptr<Texture> OnCreateTexture(
const TextureDescriptor& desc) override {
return std::make_shared<MockTexture>(desc);
};
};
TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
auto allocator = std::make_shared<TestAllocator>();
auto render_target_cache = RenderTargetCache(allocator);
auto desc = TextureDescriptor{
.format = PixelFormat::kR8G8B8A8UNormInt,
.size = ISize(100, 100),
.usage = static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)};
render_target_cache.Start();
// Create two textures of the same exact size/shape. Both should be marked
// as used this frame, so the cached data set will contain two.
render_target_cache.CreateTexture(desc);
render_target_cache.CreateTexture(desc);
ASSERT_EQ(render_target_cache.CachedTextureCount(), 2u);
render_target_cache.End();
render_target_cache.Start();
// Next frame, only create one texture. The set will still contain two,
// but one will be removed at the end of the frame.
render_target_cache.CreateTexture(desc);
ASSERT_EQ(render_target_cache.CachedTextureCount(), 2u);
render_target_cache.End();
ASSERT_EQ(render_target_cache.CachedTextureCount(), 1u);
}
} // namespace testing
} // namespace impeller

View File

@@ -21,7 +21,7 @@ class AllocatorGLES final : public Allocator {
ReactorGLES::Ref reactor_;
bool is_valid_ = false;
AllocatorGLES(ReactorGLES::Ref reactor);
explicit AllocatorGLES(ReactorGLES::Ref reactor);
// |Allocator|
bool IsValid() const;

View File

@@ -466,7 +466,7 @@ constexpr vk::AttachmentDescription CreateAttachmentDescription(
switch (kind) {
case AttachmentKind::kColor:
vk_attachment.initialLayout = current_layout;
vk_attachment.finalLayout = vk::ImageLayout::eGeneral;
vk_attachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;
break;
case AttachmentKind::kDepth:
case AttachmentKind::kStencil:

View File

@@ -15,6 +15,7 @@
#include "impeller/core/formats.h"
#include "impeller/core/sampler.h"
#include "impeller/core/shader_types.h"
#include "impeller/renderer/backend/vulkan/barrier_vk.h"
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
@@ -24,11 +25,13 @@
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "vulkan/vulkan_to_string.hpp"
namespace impeller {
static vk::AttachmentDescription CreateAttachmentDescription(
const Attachment& attachment,
const std::shared_ptr<CommandBufferVK>& command_buffer,
bool resolve_texture = false) {
const auto& texture =
resolve_texture ? attachment.resolve_texture : attachment.texture;
@@ -37,7 +40,7 @@ static vk::AttachmentDescription CreateAttachmentDescription(
}
const auto& texture_vk = TextureVK::Cast(*texture);
const auto& desc = texture->GetTextureDescriptor();
const auto current_layout = texture_vk.GetLayout();
auto current_layout = texture_vk.GetLayout();
auto load_action = attachment.load_action;
auto store_action = attachment.store_action;
@@ -52,6 +55,22 @@ static vk::AttachmentDescription CreateAttachmentDescription(
store_action = StoreAction::kStore;
}
if (current_layout != vk::ImageLayout::ePresentSrcKHR &&
current_layout != vk::ImageLayout::eUndefined) {
BarrierVK barrier;
barrier.new_layout = vk::ImageLayout::eGeneral;
barrier.cmd_buffer = command_buffer->GetEncoder()->GetCommandBuffer();
barrier.src_access = vk::AccessFlagBits::eShaderRead;
barrier.src_stage = vk::PipelineStageFlagBits::eFragmentShader;
barrier.dst_access = vk::AccessFlagBits::eColorAttachmentWrite |
vk::AccessFlagBits::eTransferWrite;
barrier.dst_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput |
vk::PipelineStageFlagBits::eTransfer;
texture_vk.SetLayout(barrier);
current_layout = vk::ImageLayout::eGeneral;
}
const auto attachment_desc =
CreateAttachmentDescription(desc.format, //
desc.sample_count, //
@@ -68,7 +87,8 @@ static vk::AttachmentDescription CreateAttachmentDescription(
}
SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
const ContextVK& context) const {
const ContextVK& context,
const std::shared_ptr<CommandBufferVK>& command_buffer) const {
std::vector<vk::AttachmentDescription> attachments;
std::vector<vk::AttachmentReference> color_refs;
@@ -92,11 +112,13 @@ SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
color_refs[bind_point] =
vk::AttachmentReference{static_cast<uint32_t>(attachments.size()),
vk::ImageLayout::eColorAttachmentOptimal};
attachments.emplace_back(CreateAttachmentDescription(color));
attachments.emplace_back(
CreateAttachmentDescription(color, command_buffer));
if (color.resolve_texture) {
resolve_refs[bind_point] = vk::AttachmentReference{
static_cast<uint32_t>(attachments.size()), vk::ImageLayout::eGeneral};
attachments.emplace_back(CreateAttachmentDescription(color, true));
attachments.emplace_back(
CreateAttachmentDescription(color, command_buffer, true));
}
}
@@ -104,7 +126,8 @@ SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
depth_stencil_ref = vk::AttachmentReference{
static_cast<uint32_t>(attachments.size()),
vk::ImageLayout::eDepthStencilAttachmentOptimal};
attachments.emplace_back(CreateAttachmentDescription(depth.value()));
attachments.emplace_back(
CreateAttachmentDescription(depth.value(), command_buffer));
}
if (auto stencil = render_target_.GetStencilAttachment();
@@ -112,7 +135,8 @@ SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
depth_stencil_ref = vk::AttachmentReference{
static_cast<uint32_t>(attachments.size()),
vk::ImageLayout::eDepthStencilAttachmentOptimal};
attachments.emplace_back(CreateAttachmentDescription(stencil.value()));
attachments.emplace_back(
CreateAttachmentDescription(stencil.value(), command_buffer));
}
vk::SubpassDescription subpass_desc;
@@ -594,7 +618,7 @@ bool RenderPassVK::OnEncodeCommands(const Context& context) const {
const auto& target_size = render_target_.GetRenderTargetSize();
auto render_pass = CreateVKRenderPass(vk_context);
auto render_pass = CreateVKRenderPass(vk_context, command_buffer);
if (!render_pass) {
VALIDATION_LOG << "Could not create renderpass.";
return false;

View File

@@ -45,7 +45,8 @@ class RenderPassVK final : public RenderPass {
bool OnEncodeCommands(const Context& context) const override;
SharedHandleVK<vk::RenderPass> CreateVKRenderPass(
const ContextVK& context) const;
const ContextVK& context,
const std::shared_ptr<CommandBufferVK>& command_buffer) const;
SharedHandleVK<vk::Framebuffer> CreateVKFramebuffer(
const ContextVK& context,

View File

@@ -19,7 +19,7 @@ class SwapchainVK;
class SurfaceContextVK : public Context,
public BackendCast<SurfaceContextVK, Context> {
public:
SurfaceContextVK(const std::shared_ptr<ContextVK>& parent);
explicit SurfaceContextVK(const std::shared_ptr<ContextVK>& parent);
// |Context|
~SurfaceContextVK() override;

View File

@@ -14,6 +14,19 @@
namespace impeller {
RenderTargetAllocator::RenderTargetAllocator(
std::shared_ptr<Allocator> allocator)
: allocator_(std::move(allocator)) {}
void RenderTargetAllocator::Start() {}
void RenderTargetAllocator::End() {}
std::shared_ptr<Texture> RenderTargetAllocator::CreateTexture(
const TextureDescriptor& desc) {
return allocator_->CreateTexture(desc);
}
RenderTarget::RenderTarget() = default;
RenderTarget::~RenderTarget() = default;
@@ -209,6 +222,7 @@ const std::optional<StencilAttachment>& RenderTarget::GetStencilAttachment()
RenderTarget RenderTarget::CreateOffscreen(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
const std::string& label,
AttachmentConfig color_attachment_config,
@@ -235,7 +249,7 @@ RenderTarget RenderTarget::CreateOffscreen(
color0.clear_color = color_attachment_config.clear_color;
color0.load_action = color_attachment_config.load_action;
color0.store_action = color_attachment_config.store_action;
color0.texture = context.GetResourceAllocator()->CreateTexture(color_tex0);
color0.texture = allocator.CreateTexture(color_tex0);
if (!color0.texture) {
return {};
@@ -244,7 +258,7 @@ RenderTarget RenderTarget::CreateOffscreen(
target.SetColorAttachment(color0, 0u);
if (stencil_attachment_config.has_value()) {
target.SetupStencilAttachment(context, size, false, label,
target.SetupStencilAttachment(context, allocator, size, false, label,
stencil_attachment_config.value());
} else {
target.SetStencilAttachment(std::nullopt);
@@ -255,6 +269,7 @@ RenderTarget RenderTarget::CreateOffscreen(
RenderTarget RenderTarget::CreateOffscreenMSAA(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
const std::string& label,
AttachmentConfigMSAA color_attachment_config,
@@ -281,8 +296,7 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
color0_tex_desc.size = size;
color0_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
auto color0_msaa_tex =
context.GetResourceAllocator()->CreateTexture(color0_tex_desc);
auto color0_msaa_tex = allocator.CreateTexture(color0_tex_desc);
if (!color0_msaa_tex) {
VALIDATION_LOG << "Could not create multisample color texture.";
return {};
@@ -302,8 +316,7 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
static_cast<uint64_t>(TextureUsage::kRenderTarget) |
static_cast<uint64_t>(TextureUsage::kShaderRead);
auto color0_resolve_tex =
context.GetResourceAllocator()->CreateTexture(color0_resolve_tex_desc);
auto color0_resolve_tex = allocator.CreateTexture(color0_resolve_tex_desc);
if (!color0_resolve_tex) {
VALIDATION_LOG << "Could not create color texture.";
return {};
@@ -324,7 +337,7 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
// Create MSAA stencil texture.
if (stencil_attachment_config.has_value()) {
target.SetupStencilAttachment(context, size, true, label,
target.SetupStencilAttachment(context, allocator, size, true, label,
stencil_attachment_config.value());
} else {
target.SetStencilAttachment(std::nullopt);
@@ -335,6 +348,7 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
void RenderTarget::SetupStencilAttachment(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
bool msaa,
const std::string& label,
@@ -354,8 +368,7 @@ void RenderTarget::SetupStencilAttachment(
stencil0.load_action = stencil_attachment_config.load_action;
stencil0.store_action = stencil_attachment_config.store_action;
stencil0.clear_stencil = 0u;
stencil0.texture =
context.GetResourceAllocator()->CreateTexture(stencil_tex0);
stencil0.texture = allocator.CreateTexture(stencil_tex0);
if (!stencil0.texture) {
return; // Error messages are handled by `Allocator::CreateTexture`.

View File

@@ -17,6 +17,34 @@ namespace impeller {
class Context;
/// @brief a wrapper around the impeller [Allocator] instance that can be used
/// to provide caching of allocated render target textures.
class RenderTargetAllocator {
public:
explicit RenderTargetAllocator(std::shared_ptr<Allocator> allocator);
virtual ~RenderTargetAllocator() = default;
/// @brief Create a new render target texture, or recycle a previously
/// allocated render
/// target texture.
virtual std::shared_ptr<Texture> CreateTexture(const TextureDescriptor& desc);
/// @brief Mark the beginning of a frame workload.
///
/// This may be used to reset any tracking state on whether or not a
/// particular texture instance is still in use.
virtual void Start();
/// @brief Mark the end of a frame workload.
///
/// This may be used to deallocate any unused textures.
virtual void End();
private:
std::shared_ptr<Allocator> allocator_;
};
class RenderTarget final {
public:
struct AttachmentConfig {
@@ -55,6 +83,7 @@ class RenderTarget final {
static RenderTarget CreateOffscreen(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
const std::string& label = "Offscreen",
AttachmentConfig color_attachment_config = kDefaultColorAttachmentConfig,
@@ -63,6 +92,7 @@ class RenderTarget final {
static RenderTarget CreateOffscreenMSAA(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
const std::string& label = "Offscreen MSAA",
AttachmentConfigMSAA color_attachment_config =
@@ -77,6 +107,7 @@ class RenderTarget final {
bool IsValid() const;
void SetupStencilAttachment(const Context& context,
RenderTargetAllocator& allocator,
ISize size,
bool msaa,
const std::string& label = "Offscreen",

View File

@@ -1149,7 +1149,9 @@ TEST_P(RendererTest, StencilMask) {
stencil_config.load_action = LoadAction::kLoad;
stencil_config.store_action = StoreAction::kDontCare;
stencil_config.storage_mode = StorageMode::kHostVisible;
render_target.SetupStencilAttachment(*context,
auto render_target_allocator =
RenderTargetAllocator(context->GetResourceAllocator());
render_target.SetupStencilAttachment(*context, render_target_allocator,
render_target.GetRenderTargetSize(),
true, "stencil", stencil_config);
// Fill the stencil buffer with an checkerboard pattern.