Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created March 5, 2024 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonahwilliams/3b117c40abf5a4d56b5584349cd9bd9f to your computer and use it in GitHub Desktop.
Save jonahwilliams/3b117c40abf5a4d56b5584349cd9bd9f to your computer and use it in GitHub Desktop.
diff --git a/impeller/entity/render_target_cache.cc b/impeller/entity/render_target_cache.cc
index 3a9710a93e..574019faa9 100644
--- a/impeller/entity/render_target_cache.cc
+++ b/impeller/entity/render_target_cache.cc
@@ -33,7 +33,11 @@ RenderTarget RenderTargetCache::CreateOffscreen(
int mip_count,
const std::string& label,
RenderTarget::AttachmentConfig color_attachment_config,
- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
+ const std::shared_ptr<Texture>& existing_color_texture,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
+ FML_DCHECK(existing_color_texture == nullptr &&
+ existing_depth_stencil_texture == nullptr);
auto config = RenderTargetConfig{
.size = size,
.mip_count = static_cast<size_t>(mip_count),
@@ -44,7 +48,13 @@ RenderTarget RenderTargetCache::CreateOffscreen(
const auto other_config = render_target_data.config;
if (!render_target_data.used_this_frame && other_config == config) {
render_target_data.used_this_frame = true;
- return render_target_data.render_target;
+ auto color0 = render_target_data.render_target.GetColorAttachments()
+ .find(0u)
+ ->second;
+ auto depth = render_target_data.render_target.GetDepthAttachment();
+ return RenderTargetAllocator::CreateOffscreen(
+ context, size, mip_count, label, color_attachment_config,
+ stencil_attachment_config, color0.texture, depth->texture);
}
}
RenderTarget created_target = RenderTargetAllocator::CreateOffscreen(
@@ -66,7 +76,13 @@ RenderTarget RenderTargetCache::CreateOffscreenMSAA(
int mip_count,
const std::string& label,
RenderTarget::AttachmentConfigMSAA color_attachment_config,
- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
+ const std::shared_ptr<Texture>& existing_color_msaa_texture,
+ const std::shared_ptr<Texture>& existing_color_resolve_texture,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
+ FML_DCHECK(existing_color_msaa_texture == nullptr &&
+ existing_color_msaa_texture == nullptr &&
+ existing_depth_stencil_texture == nullptr);
auto config = RenderTargetConfig{
.size = size,
.mip_count = static_cast<size_t>(mip_count),
@@ -77,7 +93,14 @@ RenderTarget RenderTargetCache::CreateOffscreenMSAA(
const auto other_config = render_target_data.config;
if (!render_target_data.used_this_frame && other_config == config) {
render_target_data.used_this_frame = true;
- return render_target_data.render_target;
+ auto color0 = render_target_data.render_target.GetColorAttachments()
+ .find(0u)
+ ->second;
+ auto depth = render_target_data.render_target.GetDepthAttachment();
+ return RenderTargetAllocator::CreateOffscreenMSAA(
+ context, size, mip_count, label, color_attachment_config,
+ stencil_attachment_config, color0.texture, color0.resolve_texture,
+ depth->texture);
}
}
RenderTarget created_target = RenderTargetAllocator::CreateOffscreenMSAA(
diff --git a/impeller/entity/render_target_cache.h b/impeller/entity/render_target_cache.h
index bc60951ca9..92d1dc95e5 100644
--- a/impeller/entity/render_target_cache.h
+++ b/impeller/entity/render_target_cache.h
@@ -33,7 +33,9 @@ class RenderTargetCache : public RenderTargetAllocator {
RenderTarget::AttachmentConfig color_attachment_config =
RenderTarget::kDefaultColorAttachmentConfig,
std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig) override;
+ RenderTarget::kDefaultStencilAttachmentConfig,
+ const std::shared_ptr<Texture>& existing_color_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr) override;
RenderTarget CreateOffscreenMSAA(
const Context& context,
@@ -43,7 +45,10 @@ class RenderTargetCache : public RenderTargetAllocator {
RenderTarget::AttachmentConfigMSAA color_attachment_config =
RenderTarget::kDefaultColorAttachmentConfigMSAA,
std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig) override;
+ RenderTarget::kDefaultStencilAttachmentConfig,
+ const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr) override;
// visible for testing.
size_t CachedTextureCount() const;
diff --git a/impeller/renderer/render_target.cc b/impeller/renderer/render_target.cc
index 9cab7db94d..b52b1ce07e 100644
--- a/impeller/renderer/render_target.cc
+++ b/impeller/renderer/render_target.cc
@@ -261,7 +261,9 @@ RenderTarget RenderTargetAllocator::CreateOffscreen(
int mip_count,
const std::string& label,
RenderTarget::AttachmentConfig color_attachment_config,
- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
+ const std::shared_ptr<Texture>& existing_color_texture,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
if (size.IsEmpty()) {
return {};
}
@@ -280,7 +282,9 @@ RenderTarget RenderTargetAllocator::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 = allocator_->CreateTexture(color_tex0);
+ color0.texture = existing_color_texture == nullptr
+ ? allocator_->CreateTexture(color_tex0)
+ : existing_color_texture;
if (!color0.texture) {
return {};
@@ -289,9 +293,9 @@ RenderTarget RenderTargetAllocator::CreateOffscreen(
target.SetColorAttachment(color0, 0u);
if (stencil_attachment_config.has_value()) {
- target.SetupDepthStencilAttachments(context, *allocator_, size, false,
- label,
- stencil_attachment_config.value());
+ target.SetupDepthStencilAttachments(
+ context, *allocator_, size, false, label,
+ stencil_attachment_config.value(), existing_depth_stencil_texture);
} else {
target.SetStencilAttachment(std::nullopt);
target.SetDepthAttachment(std::nullopt);
@@ -306,7 +310,10 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
int mip_count,
const std::string& label,
RenderTarget::AttachmentConfigMSAA color_attachment_config,
- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
+ const std::shared_ptr<Texture>& existing_color_msaa_texture,
+ const std::shared_ptr<Texture>& existing_color_resolve_texture,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
if (size.IsEmpty()) {
return {};
}
@@ -329,7 +336,9 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
color0_tex_desc.storage_mode = StorageMode::kDevicePrivate;
}
- auto color0_msaa_tex = allocator_->CreateTexture(color0_tex_desc);
+ auto color0_msaa_tex = existing_color_msaa_texture == nullptr
+ ? allocator_->CreateTexture(color0_tex_desc)
+ : existing_color_msaa_texture;
if (!color0_msaa_tex) {
VALIDATION_LOG << "Could not create multisample color texture.";
return {};
@@ -350,7 +359,10 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
static_cast<uint64_t>(TextureUsage::kShaderRead);
color0_resolve_tex_desc.mip_count = mip_count;
- auto color0_resolve_tex = allocator_->CreateTexture(color0_resolve_tex_desc);
+ auto color0_resolve_tex =
+ existing_color_resolve_texture == nullptr
+ ? allocator_->CreateTexture(color0_resolve_tex_desc)
+ : existing_color_resolve_texture;
if (!color0_resolve_tex) {
VALIDATION_LOG << "Could not create color texture.";
return {};
@@ -383,7 +395,8 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
if (stencil_attachment_config.has_value()) {
target.SetupDepthStencilAttachments(context, *allocator_, size, true, label,
- stencil_attachment_config.value());
+ stencil_attachment_config.value(),
+ existing_depth_stencil_texture);
} else {
target.SetDepthAttachment(std::nullopt);
target.SetStencilAttachment(std::nullopt);
@@ -398,7 +411,8 @@ void RenderTarget::SetupDepthStencilAttachments(
ISize size,
bool msaa,
const std::string& label,
- RenderTarget::AttachmentConfig stencil_attachment_config) {
+ RenderTarget::AttachmentConfig stencil_attachment_config,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
TextureDescriptor depth_stencil_texture_desc;
depth_stencil_texture_desc.storage_mode =
stencil_attachment_config.storage_mode;
@@ -413,7 +427,9 @@ void RenderTarget::SetupDepthStencilAttachments(
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
auto depth_stencil_texture =
- allocator.CreateTexture(depth_stencil_texture_desc);
+ existing_depth_stencil_texture == nullptr
+ ? allocator.CreateTexture(depth_stencil_texture_desc)
+ : existing_depth_stencil_texture;
if (!depth_stencil_texture) {
return; // Error messages are handled by `Allocator::CreateTexture`.
}
diff --git a/impeller/renderer/render_target.h b/impeller/renderer/render_target.h
index 0bfce5137c..6d635276ec 100644
--- a/impeller/renderer/render_target.h
+++ b/impeller/renderer/render_target.h
@@ -84,7 +84,8 @@ class RenderTarget final {
bool msaa,
const std::string& label = "Offscreen",
RenderTarget::AttachmentConfig stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig);
+ RenderTarget::kDefaultStencilAttachmentConfig,
+ const std::shared_ptr<Texture>& depth_stencil_texture = nullptr);
SampleCount GetSampleCount() const;
@@ -152,7 +153,9 @@ class RenderTargetAllocator {
RenderTarget::AttachmentConfig color_attachment_config =
RenderTarget::kDefaultColorAttachmentConfig,
std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig);
+ RenderTarget::kDefaultStencilAttachmentConfig,
+ const std::shared_ptr<Texture>& existing_color_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
virtual RenderTarget CreateOffscreenMSAA(
const Context& context,
@@ -162,7 +165,10 @@ class RenderTargetAllocator {
RenderTarget::AttachmentConfigMSAA color_attachment_config =
RenderTarget::kDefaultColorAttachmentConfigMSAA,
std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig);
+ RenderTarget::kDefaultStencilAttachmentConfig,
+ const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
+ const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
/// @brief Mark the beginning of a frame workload.
///
@@ -176,15 +182,6 @@ class RenderTargetAllocator {
virtual void End();
private:
- void SetupDepthStencilAttachments(
- Allocator& allocator,
- const Context& context,
- ISize size,
- bool msaa,
- const std::string& label = "Offscreen",
- RenderTarget::AttachmentConfig stencil_attachment_config =
- RenderTarget::kDefaultStencilAttachmentConfig);
-
std::shared_ptr<Allocator> allocator_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment