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

Commit 107dab4

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

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); // no 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

impeller/scene/material.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class UnlitMaterial final : public Material {
6666

6767
void SetColorTexture(std::shared_ptr<Texture> color_texture);
6868

69+
void SetVertexColorWeight(Scalar weight);
70+
6971
// |Material|
7072
std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline(
7173
const SceneContext& scene_context,
@@ -79,6 +81,7 @@ class UnlitMaterial final : public Material {
7981
private:
8082
Color color_ = Color::White();
8183
std::shared_ptr<Texture> color_texture_;
84+
Scalar vertex_color_weight_ = 1;
8285
};
8386

8487
class StandardMaterial final : public Material {

impeller/scene/scene_context.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ namespace scene {
1010

1111
void SceneContextOptions::ApplyToPipelineDescriptor(
1212
PipelineDescriptor& desc) const {
13+
DepthAttachmentDescriptor depth;
14+
depth.depth_compare = CompareFunction::kLess;
15+
depth.depth_write_enabled = true;
16+
desc.SetDepthStencilAttachmentDescriptor(depth);
17+
desc.SetDepthPixelFormat(PixelFormat::kD32FloatS8UInt);
18+
19+
StencilAttachmentDescriptor stencil;
20+
stencil.stencil_compare = CompareFunction::kAlways;
21+
stencil.depth_stencil_pass = StencilOperation::kKeep;
22+
desc.SetStencilAttachmentDescriptors(stencil);
23+
desc.SetStencilPixelFormat(PixelFormat::kD32FloatS8UInt);
24+
1325
desc.SetSampleCount(sample_count);
1426
desc.SetPrimitiveType(primitive_type);
1527
}

impeller/scene/scene_encoder.cc

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,37 @@ static void EncodeCommand(const SceneContext& scene_context,
4949
std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
5050
const SceneContext& scene_context,
5151
const Camera& camera,
52-
const RenderTarget& render_target) const {
52+
RenderTarget render_target) const {
53+
{
54+
TextureDescriptor ds_texture;
55+
ds_texture.type = TextureType::kTexture2DMultisample;
56+
ds_texture.format = PixelFormat::kD32FloatS8UInt;
57+
ds_texture.size = render_target.GetRenderTargetSize();
58+
ds_texture.usage =
59+
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
60+
ds_texture.sample_count = SampleCount::kCount4;
61+
ds_texture.storage_mode = StorageMode::kDeviceTransient;
62+
auto texture =
63+
scene_context.GetContext()->GetResourceAllocator()->CreateTexture(
64+
ds_texture);
65+
66+
DepthAttachment depth;
67+
depth.load_action = LoadAction::kClear;
68+
depth.store_action = StoreAction::kDontCare;
69+
depth.clear_depth = 1.0;
70+
depth.texture = texture;
71+
render_target.SetDepthAttachment(depth);
72+
73+
// The stencil and depth buffers must be the same texture for MacOS ARM
74+
// and Vulkan.
75+
StencilAttachment stencil;
76+
stencil.load_action = LoadAction::kClear;
77+
stencil.store_action = StoreAction::kDontCare;
78+
stencil.clear_stencil = 0u;
79+
stencil.texture = texture;
80+
render_target.SetStencilAttachment(stencil);
81+
}
82+
5383
auto command_buffer = scene_context.GetContext()->CreateCommandBuffer();
5484
if (!command_buffer) {
5585
FML_LOG(ERROR) << "Failed to create command buffer.";

impeller/scene/scene_encoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SceneEncoder {
3636
std::shared_ptr<CommandBuffer> BuildSceneCommandBuffer(
3737
const SceneContext& scene_context,
3838
const Camera& camera,
39-
const RenderTarget& render_target) const;
39+
RenderTarget render_target) const;
4040

4141
std::vector<SceneCommand> commands_;
4242

impeller/scene/scene_unittests.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <cmath>
6+
#include <memory>
7+
58
#include "flutter/testing/testing.h"
69
#include "impeller/geometry/color.h"
710
#include "impeller/geometry/constants.h"
811
#include "impeller/geometry/matrix.h"
12+
#include "impeller/geometry/quaternion.h"
913
#include "impeller/geometry/vector.h"
1014
#include "impeller/playground/playground.h"
1115
#include "impeller/playground/playground_test.h"
16+
#include "impeller/renderer/formats.h"
1217
#include "impeller/scene/camera.h"
1318
#include "impeller/scene/geometry.h"
1419
#include "impeller/scene/importer/scene_flatbuffers.h"
@@ -80,18 +85,27 @@ TEST_P(SceneTest, GLTFScene) {
8085
auto geometry = Geometry::MakeFromFBMesh(*fb_mesh, *allocator);
8186
ASSERT_NE(geometry, nullptr);
8287

88+
std::shared_ptr<UnlitMaterial> material = Material::MakeUnlit();
89+
auto bridge = CreateTextureForFixture("flutter_logo_baked.png");
90+
material->SetColorTexture(bridge);
91+
material->SetVertexColorWeight(0);
92+
8393
Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
8494
auto scene = Scene(GetContext());
8595

8696
auto mesh = SceneEntity::MakeStaticMesh();
87-
mesh->SetMaterial(Material::MakeUnlit());
97+
mesh->SetMaterial(material);
8898
mesh->SetGeometry(geometry);
99+
mesh->SetLocalTransform(Matrix::MakeScale({3, 3, 3}));
89100
scene.Add(mesh);
90101

102+
Quaternion rotation({0, 1, 0}, -GetSecondsElapsed() * 0.5);
103+
Vector3 start_position(-1, -1.5, -5);
104+
91105
// Face towards the +Z direction (+X right, +Y up).
92106
auto camera = Camera::MakePerspective(
93-
/* fov */ Radians(kPiOver4),
94-
/* position */ {2, 2, -5})
107+
/* fov */ Degrees(60),
108+
/* position */ rotation * start_position)
95109
.LookAt(
96110
/* target */ Vector3(),
97111
/* up */ {0, 1, 0});

impeller/scene/shaders/unlit.frag

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
uniform FragInfo {
66
vec4 color;
7+
float vertex_color_weight;
78
}
89
frag_info;
910

@@ -17,6 +18,7 @@ in vec4 v_color;
1718
out vec4 frag_color;
1819

1920
void main() {
20-
frag_color =
21-
texture(base_color_texture, v_texture_coords) * v_color * frag_info.color;
21+
vec4 vertex_color = mix(vec4(1), v_color, frag_info.vertex_color_weight);
22+
frag_color = texture(base_color_texture, v_texture_coords) * vertex_color *
23+
frag_info.color;
2224
}

0 commit comments

Comments
 (0)