Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0bddc60

Browse files
authored
[Impeller Scene] Depth attachment; baked lighting example (#38118)
* [Impeller Scene] Depth buffer; baked lighting example * vk formats * Remove kD24UNormS8UInt * Address comments
1 parent 2f5b67e commit 0bddc60

File tree

17 files changed

+112
-52
lines changed

17 files changed

+112
-52
lines changed

impeller/fixtures/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ test_fixtures("file_fixtures") {
5959
"boston.jpg",
6060
"embarcadero.jpg",
6161
"flutter_logo.glb",
62+
"flutter_logo_baked.png",
6263
"kalimba.jpg",
6364
"multiple_stages.hlsl",
6465
"resources_limit.vert",
263 KB
Loading

impeller/playground/backend/gles/playground_impl_gles.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ PlaygroundImplGLES::PlaygroundImplGLES()
6868
::glfwWindowHint(GLFW_GREEN_BITS, 8);
6969
::glfwWindowHint(GLFW_BLUE_BITS, 8);
7070
::glfwWindowHint(GLFW_ALPHA_BITS, 8);
71-
::glfwWindowHint(GLFW_DEPTH_BITS, 0); // no depth buffer
71+
::glfwWindowHint(GLFW_DEPTH_BITS, 32); // 32 bit depth buffer
7272
::glfwWindowHint(GLFW_STENCIL_BITS, 8); // 8 bit stencil buffer
7373
::glfwWindowHint(GLFW_SAMPLES, 4); // 4xMSAA
7474

impeller/playground/playground.cc

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -263,35 +263,8 @@ bool Playground::OpenPlaygroundHere(
263263
}
264264
render_target.SetColorAttachment(color0, 0);
265265

266-
#ifndef IMPELLER_ENABLE_VULKAN
267-
{
268-
TextureDescriptor stencil0_tex;
269-
stencil0_tex.storage_mode = StorageMode::kDeviceTransient;
270-
stencil0_tex.type = TextureType::kTexture2D;
271-
stencil0_tex.sample_count = SampleCount::kCount1;
272-
stencil0_tex.format = PixelFormat::kDefaultStencil;
273-
stencil0_tex.size = color0.texture->GetSize();
274-
stencil0_tex.usage =
275-
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
276-
auto stencil_texture =
277-
renderer->GetContext()->GetResourceAllocator()->CreateTexture(
278-
stencil0_tex);
279-
280-
if (!stencil_texture) {
281-
VALIDATION_LOG << "Could not create stencil texture.";
282-
return false;
283-
}
284-
stencil_texture->SetLabel("ImguiStencil");
285-
286-
StencilAttachment stencil0;
287-
stencil0.texture = stencil_texture;
288-
stencil0.clear_stencil = 0;
289-
stencil0.load_action = LoadAction::kClear;
290-
stencil0.store_action = StoreAction::kDontCare;
291-
292-
render_target.SetStencilAttachment(stencil0);
293-
}
294-
#endif
266+
render_target.SetStencilAttachment(std::nullopt);
267+
render_target.SetDepthAttachment(std::nullopt);
295268

296269
auto pass = buffer->CreateRenderPass(render_target);
297270
if (!pass) {

impeller/renderer/backend/gles/texture_gles.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct TexImage2DData {
109109
break;
110110
case PixelFormat::kUnknown:
111111
case PixelFormat::kS8UInt:
112+
case PixelFormat::kD32FloatS8UInt:
112113
case PixelFormat::kR8UNormInt:
113114
case PixelFormat::kR8G8UNormInt:
114115
return;
@@ -136,15 +137,11 @@ struct TexImage2DData {
136137
break;
137138
}
138139
case PixelFormat::kR8G8B8A8UNormIntSRGB:
139-
return;
140140
case PixelFormat::kB8G8R8A8UNormInt:
141-
return;
142141
case PixelFormat::kB8G8R8A8UNormIntSRGB:
143-
return;
144142
case PixelFormat::kS8UInt:
145-
return;
143+
case PixelFormat::kD32FloatS8UInt:
146144
case PixelFormat::kR8UNormInt:
147-
return;
148145
case PixelFormat::kR8G8UNormInt:
149146
return;
150147
}
@@ -279,6 +276,8 @@ static std::optional<GLenum> ToRenderBufferFormat(PixelFormat format) {
279276
return GL_RGBA4;
280277
case PixelFormat::kS8UInt:
281278
return GL_STENCIL_INDEX8;
279+
case PixelFormat::kD32FloatS8UInt:
280+
return GL_DEPTH32F_STENCIL8;
282281
case PixelFormat::kUnknown:
283282
case PixelFormat::kA8UNormInt:
284283
case PixelFormat::kR8UNormInt:

impeller/renderer/backend/metal/formats_mtl.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) {
2727
return PixelFormat::kB8G8R8A8UNormIntSRGB;
2828
case MTLPixelFormatRGBA8Unorm:
2929
return PixelFormat::kR8G8B8A8UNormInt;
30-
case MTLPixelFormatStencil8:
31-
return PixelFormat::kS8UInt;
3230
case MTLPixelFormatRGBA8Unorm_sRGB:
3331
return PixelFormat::kR8G8B8A8UNormIntSRGB;
32+
case MTLPixelFormatStencil8:
33+
return PixelFormat::kS8UInt;
34+
case MTLPixelFormatDepth32Float_Stencil8:
35+
return PixelFormat::kD32FloatS8UInt;
3436
default:
3537
return PixelFormat::kUnknown;
3638
}
@@ -53,10 +55,12 @@ constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
5355
return MTLPixelFormatBGRA8Unorm_sRGB;
5456
case PixelFormat::kR8G8B8A8UNormInt:
5557
return MTLPixelFormatRGBA8Unorm;
56-
case PixelFormat::kS8UInt:
57-
return MTLPixelFormatStencil8;
5858
case PixelFormat::kR8G8B8A8UNormIntSRGB:
5959
return MTLPixelFormatRGBA8Unorm_sRGB;
60+
case PixelFormat::kS8UInt:
61+
return MTLPixelFormatStencil8;
62+
case PixelFormat::kD32FloatS8UInt:
63+
return MTLPixelFormatDepth32Float_Stencil8;
6064
}
6165
return MTLPixelFormatInvalid;
6266
};

impeller/renderer/backend/vulkan/formats_vk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ constexpr vk::Format ToVKImageFormat(PixelFormat format) {
150150
return vk::Format::eB8G8R8A8Srgb;
151151
case PixelFormat::kS8UInt:
152152
return vk::Format::eS8Uint;
153+
case PixelFormat::kD32FloatS8UInt:
154+
return vk::Format::eD32SfloatS8Uint;
153155
case PixelFormat::kR8UNormInt:
154156
return vk::Format::eR8Unorm;
155157
case PixelFormat::kR8G8UNormInt:
@@ -179,6 +181,9 @@ constexpr PixelFormat ToPixelFormat(vk::Format format) {
179181
case vk::Format::eS8Uint:
180182
return PixelFormat::kS8UInt;
181183

184+
case vk::Format::eD32SfloatS8Uint:
185+
return PixelFormat::kD32FloatS8UInt;
186+
182187
case vk::Format::eR8Unorm:
183188
return PixelFormat::kR8UNormInt;
184189

impeller/renderer/formats.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ enum class PixelFormat {
8787
kR8G8B8A8UNormIntSRGB,
8888
kB8G8R8A8UNormInt,
8989
kB8G8R8A8UNormIntSRGB,
90+
91+
// Depth and stencil formats.
9092
kS8UInt,
93+
kD32FloatS8UInt,
9194

9295
// Defaults. If you don't know which ones to use, these are usually a safe
9396
// bet.
@@ -285,6 +288,8 @@ constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
285288
case PixelFormat::kB8G8R8A8UNormInt:
286289
case PixelFormat::kB8G8R8A8UNormIntSRGB:
287290
return 4u;
291+
case PixelFormat::kD32FloatS8UInt:
292+
return 5u;
288293
}
289294
return 0u;
290295
}

impeller/renderer/render_target.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,21 @@ RenderTarget& RenderTarget::SetColorAttachment(
149149
return *this;
150150
}
151151

152-
RenderTarget& RenderTarget::SetDepthAttachment(DepthAttachment attachment) {
153-
if (attachment.IsValid()) {
152+
RenderTarget& RenderTarget::SetDepthAttachment(
153+
std::optional<DepthAttachment> attachment) {
154+
if (!attachment.has_value()) {
155+
depth_ = std::nullopt;
156+
} else if (attachment->IsValid()) {
154157
depth_ = std::move(attachment);
155158
}
156159
return *this;
157160
}
158161

159-
RenderTarget& RenderTarget::SetStencilAttachment(StencilAttachment attachment) {
160-
if (attachment.IsValid()) {
162+
RenderTarget& RenderTarget::SetStencilAttachment(
163+
std::optional<StencilAttachment> attachment) {
164+
if (!attachment.has_value()) {
165+
stencil_ = std::nullopt;
166+
} else if (attachment->IsValid()) {
161167
stencil_ = std::move(attachment);
162168
}
163169
return *this;

impeller/renderer/render_target.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ class RenderTarget {
6161
RenderTarget& SetColorAttachment(const ColorAttachment& attachment,
6262
size_t index);
6363

64-
RenderTarget& SetDepthAttachment(DepthAttachment attachment);
64+
RenderTarget& SetDepthAttachment(std::optional<DepthAttachment> attachment);
6565

66-
RenderTarget& SetStencilAttachment(StencilAttachment attachment);
66+
RenderTarget& SetStencilAttachment(
67+
std::optional<StencilAttachment> attachment);
6768

6869
const std::map<size_t, ColorAttachment>& GetColorAttachments() const;
6970

0 commit comments

Comments
 (0)