Skip to content

Commit

Permalink
WIP on creating utils for creating offscreen render targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 38d4aba commit 17d301b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
19 changes: 10 additions & 9 deletions impeller/playground/playground.mm
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,29 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
return false;
}

TextureDescriptor color0_desc;
color0_desc.format = PixelFormat::kB8G8R8A8UNormInt;
color0_desc.size = {
TextureDescriptor color0_tex;
color0_tex.format = PixelFormat::kB8G8R8A8UNormInt;
color0_tex.size = {
static_cast<ISize::Type>(current_drawable.texture.width),
static_cast<ISize::Type>(current_drawable.texture.height)};
color0_tex.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);

ColorAttachment color0;
color0.texture =
std::make_shared<TextureMTL>(color0_desc, current_drawable.texture);
std::make_shared<TextureMTL>(color0_tex, current_drawable.texture);
color0.clear_color = Color::SkyBlue();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;

TextureDescriptor stencil_texture_descriptor;
stencil_texture_descriptor.format = PixelFormat::kD32FloatS8UNormInt;
stencil_texture_descriptor.size = color0_desc.size;
stencil_texture_descriptor.usage =
TextureDescriptor stencil0_tex;
stencil0_tex.format = PixelFormat::kD32FloatS8UNormInt;
stencil0_tex.size = color0_tex.size;
stencil0_tex.usage =
static_cast<TextureUsageMask>(TextureUsage::kShaderRead) |
static_cast<TextureUsageMask>(TextureUsage::kShaderWrite);
auto stencil_texture =
renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture(
StorageMode::kDeviceTransient, stencil_texture_descriptor);
StorageMode::kDeviceTransient, stencil0_tex);
stencil_texture->SetLabel("PlaygroundMainStencil");

StencilAttachment stencil0;
Expand Down
3 changes: 3 additions & 0 deletions impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ static bool ConfigureStencilAttachment(
}

void RenderPassMTL::SetLabel(std::string label) {
if (label.empty()) {
return;
}
label_ = std::move(label);
transients_buffer_->SetLabel(SPrintF("%s Transients", label_.c_str()));
}
Expand Down
50 changes: 50 additions & 0 deletions impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "impeller/renderer/render_target.h"

#include "impeller/renderer/allocator.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/texture.h"

namespace impeller {
Expand Down Expand Up @@ -70,4 +72,52 @@ const std::optional<StencilAttachment>& RenderTarget::GetStencilAttachment()
return stencil_;
}

RenderTarget RenderTarget::CreateOffscreen(const Context& context,
ISize size,
std::string label) {
TextureDescriptor color_tex0;
color_tex0.format = PixelFormat::kB8G8R8A8UNormInt;
color_tex0.size = size;
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);

TextureDescriptor stencil_tex0;
stencil_tex0.format = PixelFormat::kD32FloatS8UNormInt;
stencil_tex0.size = size;
stencil_tex0.usage =
static_cast<TextureUsageMask>(TextureUsage::kShaderRead) |
static_cast<TextureUsageMask>(TextureUsage::kShaderWrite);

ColorAttachment color0;
color0.clear_color = Color::BlackTransparent();
color0.load_action = LoadAction::kClear;
color0.store_action = StoreAction::kStore;
color0.texture = context.GetPermanentsAllocator()->CreateTexture(
StorageMode::kDevicePrivate, color_tex0);

if (!color0.texture) {
return {};
}

color0.texture->SetLabel(label);

StencilAttachment stencil0;
stencil0.load_action = LoadAction::kClear;
stencil0.store_action = StoreAction::kDontCare;
stencil0.clear_stencil = 0u;
stencil0.texture = context.GetPermanentsAllocator()->CreateTexture(
StorageMode::kDeviceTransient, stencil_tex0);

if (!stencil0.texture) {
return {};
}

stencil0.texture->SetLabel(label);

RenderTarget target;
target.SetColorAttachment(std::move(color0), 0u);
target.SetStencilAttachment(std::move(stencil0));

return target;
}

} // namespace impeller
6 changes: 6 additions & 0 deletions impeller/renderer/render_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

namespace impeller {

class Context;

class RenderTarget {
public:
static RenderTarget CreateOffscreen(const Context& context,
ISize size,
std::string label = "Offscreen");

RenderTarget();

~RenderTarget();
Expand Down

0 comments on commit 17d301b

Please sign in to comment.