| Flutter Impeller |

scene_encoder.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/scene/scene_encoder.h"
6 #include "flutter/fml/logging.h"
7 #include "impeller/renderer/render_target.h"
8 #include "impeller/scene/scene_context.h"
9
10 namespace impeller {
11 namespace scene {
12
13 SceneEncoder::SceneEncoder() = default;
14
15 void SceneEncoder::Add(const SceneCommand& command) {
16 // TODO(bdero): Manage multi-pass translucency ordering.
17 commands_.push_back(command);
18 }
19
20 static void EncodeCommand(const SceneContext& scene_context,
21 const Matrix& view_transform,
22 RenderPass& render_pass,
23 const SceneCommand& scene_command) {
24 auto& host_buffer = scene_context.GetTransientsBuffer();
25
26 render_pass.SetCommandLabel(scene_command.label);
27 // TODO(bdero): Configurable stencil ref per-command.
28 render_pass.SetStencilReference(0);
29
30 render_pass.SetPipeline(scene_context.GetPipeline(
31 PipelineKey{scene_command.geometry->GetGeometryType(),
32 scene_command.material->GetMaterialType()},
33 scene_command.material->GetContextOptions(render_pass)));
34
35 scene_command.geometry->BindToCommand(
36 scene_context, host_buffer, view_transform * scene_command.transform,
37 render_pass);
38 scene_command.material->BindToCommand(scene_context, host_buffer,
39 render_pass);
40
41 render_pass.Draw();
42 ;
43 }
44
45 std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
46 const SceneContext& scene_context,
47 const Matrix& camera_transform,
48 RenderTarget render_target) const {
49 {
50 TextureDescriptor ds_texture;
51 ds_texture.type = TextureType::kTexture2DMultisample;
52 ds_texture.format = PixelFormat::kD32FloatS8UInt;
53 ds_texture.size = render_target.GetRenderTargetSize();
54 ds_texture.usage = TextureUsage::kRenderTarget;
55 ds_texture.sample_count = SampleCount::kCount4;
56 ds_texture.storage_mode = StorageMode::kDeviceTransient;
57 auto texture =
58 scene_context.GetContext()->GetResourceAllocator()->CreateTexture(
59 ds_texture);
60
61 DepthAttachment depth;
62 depth.load_action = LoadAction::kClear;
63 depth.store_action = StoreAction::kDontCare;
64 depth.clear_depth = 1.0;
65 depth.texture = texture;
66 render_target.SetDepthAttachment(depth);
67
68 // The stencil and depth buffers must be the same texture for MacOS ARM
69 // and Vulkan.
70 StencilAttachment stencil;
71 stencil.load_action = LoadAction::kClear;
72 stencil.store_action = StoreAction::kDontCare;
73 stencil.clear_stencil = 0u;
74 stencil.texture = texture;
75 render_target.SetStencilAttachment(stencil);
76 }
77
78 auto command_buffer = scene_context.GetContext()->CreateCommandBuffer();
79 if (!command_buffer) {
80 FML_LOG(ERROR) << "Failed to create command buffer.";
81 return nullptr;
82 }
83
84 auto render_pass = command_buffer->CreateRenderPass(render_target);
85 if (!render_pass) {
86 FML_LOG(ERROR) << "Failed to create render pass.";
87 return nullptr;
88 }
89
90 for (auto& command : commands_) {
91 EncodeCommand(scene_context, camera_transform, *render_pass, command);
92 }
93
94 if (!render_pass->EncodeCommands()) {
95 FML_LOG(ERROR) << "Failed to encode render pass commands.";
96 return nullptr;
97 }
98
99 return command_buffer;
100 }
101
102 } // namespace scene
103 } // namespace impeller
impeller::scene::SceneCommand::material
Material * material
Definition: scene_encoder.h:25
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:654
scene_context.h
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::scene::SceneCommand::geometry
Geometry * geometry
Definition: scene_encoder.h:24
impeller::StoreAction::kDontCare
@ kDontCare
impeller::scene::SceneContext
Definition: scene_context.h:41
impeller::scene::Material::GetContextOptions
SceneContextOptions GetContextOptions(const RenderPass &pass) const
Definition: material.cc:62
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::PixelFormat::kD32FloatS8UInt
@ kD32FloatS8UInt
scene_encoder.h
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::scene::SceneEncoder::Add
void Add(const SceneCommand &command)
Definition: scene_encoder.cc:15
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::scene::EncodeCommand
static void EncodeCommand(const SceneContext &scene_context, const Matrix &view_transform, RenderPass &render_pass, const SceneCommand &scene_command)
Definition: scene_encoder.cc:20
impeller::TextureType::kTexture2DMultisample
@ kTexture2DMultisample
impeller::scene::PipelineKey
Definition: pipeline_key.h:23
impeller::StencilAttachment
Definition: formats.h:667
impeller::LoadAction::kClear
@ kClear
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::scene::SceneContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Definition: scene_context.h:57
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:651
impeller::RenderTarget
Definition: render_target.h:38
impeller::StencilAttachment::clear_stencil
uint32_t clear_stencil
Definition: formats.h:668
impeller::RenderPass::SetStencilReference
virtual void SetStencilReference(uint32_t value)
Definition: render_pass.cc:103
impeller::scene::SceneCommand::transform
Matrix transform
Definition: scene_encoder.h:23
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:139
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:188
impeller::scene::SceneCommand
Definition: scene_encoder.h:21
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:653
impeller::scene::SceneContext::GetPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
Definition: scene_context.cc:90
impeller::DepthAttachment::clear_depth
double clear_depth
Definition: formats.h:664
impeller::SampleCount::kCount4
@ kCount4
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::RenderPass::SetPipeline
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor >> &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
impeller::scene::Material::BindToCommand
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, RenderPass &pass) const =0
impeller::DepthAttachment
Definition: formats.h:663
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
render_target.h
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:178
impeller::scene::SceneCommand::label
std::string label
Definition: scene_encoder.h:22
impeller::scene::Geometry::BindToCommand
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const =0
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37