Skip to content

Commit 17d301b

Browse files
chinmaygardednfield
authored andcommitted
WIP on creating utils for creating offscreen render targets.
1 parent 38d4aba commit 17d301b

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

impeller/playground/playground.mm

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,29 @@ static void PlaygroundKeyCallback(GLFWwindow* window,
144144
return false;
145145
}
146146

147-
TextureDescriptor color0_desc;
148-
color0_desc.format = PixelFormat::kB8G8R8A8UNormInt;
149-
color0_desc.size = {
147+
TextureDescriptor color0_tex;
148+
color0_tex.format = PixelFormat::kB8G8R8A8UNormInt;
149+
color0_tex.size = {
150150
static_cast<ISize::Type>(current_drawable.texture.width),
151151
static_cast<ISize::Type>(current_drawable.texture.height)};
152+
color0_tex.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
152153

153154
ColorAttachment color0;
154155
color0.texture =
155-
std::make_shared<TextureMTL>(color0_desc, current_drawable.texture);
156+
std::make_shared<TextureMTL>(color0_tex, current_drawable.texture);
156157
color0.clear_color = Color::SkyBlue();
157158
color0.load_action = LoadAction::kClear;
158159
color0.store_action = StoreAction::kStore;
159160

160-
TextureDescriptor stencil_texture_descriptor;
161-
stencil_texture_descriptor.format = PixelFormat::kD32FloatS8UNormInt;
162-
stencil_texture_descriptor.size = color0_desc.size;
163-
stencil_texture_descriptor.usage =
161+
TextureDescriptor stencil0_tex;
162+
stencil0_tex.format = PixelFormat::kD32FloatS8UNormInt;
163+
stencil0_tex.size = color0_tex.size;
164+
stencil0_tex.usage =
164165
static_cast<TextureUsageMask>(TextureUsage::kShaderRead) |
165166
static_cast<TextureUsageMask>(TextureUsage::kShaderWrite);
166167
auto stencil_texture =
167168
renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture(
168-
StorageMode::kDeviceTransient, stencil_texture_descriptor);
169+
StorageMode::kDeviceTransient, stencil0_tex);
169170
stencil_texture->SetLabel("PlaygroundMainStencil");
170171

171172
StencilAttachment stencil0;

impeller/renderer/backend/metal/render_pass_mtl.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ static bool ConfigureStencilAttachment(
114114
}
115115

116116
void RenderPassMTL::SetLabel(std::string label) {
117+
if (label.empty()) {
118+
return;
119+
}
117120
label_ = std::move(label);
118121
transients_buffer_->SetLabel(SPrintF("%s Transients", label_.c_str()));
119122
}

impeller/renderer/render_target.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "impeller/renderer/render_target.h"
66

7+
#include "impeller/renderer/allocator.h"
8+
#include "impeller/renderer/context.h"
79
#include "impeller/renderer/texture.h"
810

911
namespace impeller {
@@ -70,4 +72,52 @@ const std::optional<StencilAttachment>& RenderTarget::GetStencilAttachment()
7072
return stencil_;
7173
}
7274

75+
RenderTarget RenderTarget::CreateOffscreen(const Context& context,
76+
ISize size,
77+
std::string label) {
78+
TextureDescriptor color_tex0;
79+
color_tex0.format = PixelFormat::kB8G8R8A8UNormInt;
80+
color_tex0.size = size;
81+
color_tex0.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
82+
83+
TextureDescriptor stencil_tex0;
84+
stencil_tex0.format = PixelFormat::kD32FloatS8UNormInt;
85+
stencil_tex0.size = size;
86+
stencil_tex0.usage =
87+
static_cast<TextureUsageMask>(TextureUsage::kShaderRead) |
88+
static_cast<TextureUsageMask>(TextureUsage::kShaderWrite);
89+
90+
ColorAttachment color0;
91+
color0.clear_color = Color::BlackTransparent();
92+
color0.load_action = LoadAction::kClear;
93+
color0.store_action = StoreAction::kStore;
94+
color0.texture = context.GetPermanentsAllocator()->CreateTexture(
95+
StorageMode::kDevicePrivate, color_tex0);
96+
97+
if (!color0.texture) {
98+
return {};
99+
}
100+
101+
color0.texture->SetLabel(label);
102+
103+
StencilAttachment stencil0;
104+
stencil0.load_action = LoadAction::kClear;
105+
stencil0.store_action = StoreAction::kDontCare;
106+
stencil0.clear_stencil = 0u;
107+
stencil0.texture = context.GetPermanentsAllocator()->CreateTexture(
108+
StorageMode::kDeviceTransient, stencil_tex0);
109+
110+
if (!stencil0.texture) {
111+
return {};
112+
}
113+
114+
stencil0.texture->SetLabel(label);
115+
116+
RenderTarget target;
117+
target.SetColorAttachment(std::move(color0), 0u);
118+
target.SetStencilAttachment(std::move(stencil0));
119+
120+
return target;
121+
}
122+
73123
} // namespace impeller

impeller/renderer/render_target.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313

1414
namespace impeller {
1515

16+
class Context;
17+
1618
class RenderTarget {
1719
public:
20+
static RenderTarget CreateOffscreen(const Context& context,
21+
ISize size,
22+
std::string label = "Offscreen");
23+
1824
RenderTarget();
1925

2026
~RenderTarget();

0 commit comments

Comments
 (0)