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

Commit 6929e31

Browse files
committed
[Impeller Scene] Depth buffer; baked lighting example
1 parent 8d83b98 commit 6929e31

File tree

16 files changed

+117
-52
lines changed

16 files changed

+117
-52
lines changed

impeller/fixtures/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ test_fixtures("file_fixtures") {
7878
"table_mountain_py.png",
7979
"table_mountain_pz.png",
8080
"test_texture.frag",
81+
"flutter_logo_baked.png",
8182
"types.h",
8283
"wtf.otf",
8384
]
266 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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ struct TexImage2DData {
109109
break;
110110
case PixelFormat::kUnknown:
111111
case PixelFormat::kS8UInt:
112+
case PixelFormat::kD24UNormS8UInt:
113+
case PixelFormat::kD32FloatS8UInt:
112114
case PixelFormat::kR8UNormInt:
113115
case PixelFormat::kR8G8UNormInt:
114116
return;
@@ -136,15 +138,12 @@ struct TexImage2DData {
136138
break;
137139
}
138140
case PixelFormat::kR8G8B8A8UNormIntSRGB:
139-
return;
140141
case PixelFormat::kB8G8R8A8UNormInt:
141-
return;
142142
case PixelFormat::kB8G8R8A8UNormIntSRGB:
143-
return;
144143
case PixelFormat::kS8UInt:
145-
return;
144+
case PixelFormat::kD24UNormS8UInt:
145+
case PixelFormat::kD32FloatS8UInt:
146146
case PixelFormat::kR8UNormInt:
147-
return;
148147
case PixelFormat::kR8G8UNormInt:
149148
return;
150149
}
@@ -279,6 +278,10 @@ static std::optional<GLenum> ToRenderBufferFormat(PixelFormat format) {
279278
return GL_RGBA4;
280279
case PixelFormat::kS8UInt:
281280
return GL_STENCIL_INDEX8;
281+
case PixelFormat::kD24UNormS8UInt:
282+
return GL_DEPTH24_STENCIL8;
283+
case PixelFormat::kD32FloatS8UInt:
284+
return GL_DEPTH32F_STENCIL8;
282285
case PixelFormat::kUnknown:
283286
case PixelFormat::kA8UNormInt:
284287
case PixelFormat::kR8UNormInt:

impeller/renderer/backend/metal/formats_mtl.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ 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 MTLPixelFormatDepth24Unorm_Stencil8:
35+
return PixelFormat::kD24UNormS8UInt;
36+
case MTLPixelFormatDepth32Float_Stencil8:
37+
return PixelFormat::kD32FloatS8UInt;
3438
default:
3539
return PixelFormat::kUnknown;
3640
}
@@ -53,10 +57,14 @@ constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) {
5357
return MTLPixelFormatBGRA8Unorm_sRGB;
5458
case PixelFormat::kR8G8B8A8UNormInt:
5559
return MTLPixelFormatRGBA8Unorm;
56-
case PixelFormat::kS8UInt:
57-
return MTLPixelFormatStencil8;
5860
case PixelFormat::kR8G8B8A8UNormIntSRGB:
5961
return MTLPixelFormatRGBA8Unorm_sRGB;
62+
case PixelFormat::kS8UInt:
63+
return MTLPixelFormatStencil8;
64+
case PixelFormat::kD24UNormS8UInt:
65+
return MTLPixelFormatDepth24Unorm_Stencil8;
66+
case PixelFormat::kD32FloatS8UInt:
67+
return MTLPixelFormatDepth32Float_Stencil8;
6068
}
6169
return MTLPixelFormatInvalid;
6270
};

impeller/renderer/formats.h

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

9296
// Defaults. If you don't know which ones to use, these are usually a safe
9397
// bet.
@@ -284,7 +288,10 @@ constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) {
284288
case PixelFormat::kR8G8B8A8UNormIntSRGB:
285289
case PixelFormat::kB8G8R8A8UNormInt:
286290
case PixelFormat::kB8G8R8A8UNormIntSRGB:
291+
case PixelFormat::kD24UNormS8UInt:
287292
return 4u;
293+
case PixelFormat::kD32FloatS8UInt:
294+
return 5u;
288295
}
289296
return 0u;
290297
}

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

impeller/scene/material.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ void UnlitMaterial::SetColorTexture(std::shared_ptr<Texture> color_texture) {
5555
color_texture_ = std::move(color_texture);
5656
}
5757

58+
void UnlitMaterial::SetVertexColorWeight(Scalar weight) {
59+
vertex_color_weight_ = weight;
60+
}
61+
5862
// |Material|
5963
std::shared_ptr<Pipeline<PipelineDescriptor>> UnlitMaterial::GetPipeline(
6064
const SceneContext& scene_context,
@@ -69,6 +73,7 @@ void UnlitMaterial::BindToCommand(const SceneContext& scene_context,
6973
// Uniform buffer.
7074
UnlitPipeline::FragmentShader::FragInfo info;
7175
info.color = color_;
76+
info.vertex_color_weight = vertex_color_weight_;
7277
UnlitPipeline::FragmentShader::BindFragInfo(command,
7378
buffer.EmplaceUniform(info));
7479

0 commit comments

Comments
 (0)