| 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 "flutter/fml/macros.h"
6
7 #include "flutter/fml/logging.h"
8 #include "impeller/renderer/command.h"
9 #include "impeller/renderer/render_target.h"
10 #include "impeller/scene/scene_context.h"
11 #include "impeller/scene/scene_encoder.h"
12
13 namespace impeller {
14 namespace scene {
15
16 SceneEncoder::SceneEncoder() = default;
17
18 void SceneEncoder::Add(const SceneCommand& command) {
19 // TODO(bdero): Manage multi-pass translucency ordering.
20 commands_.push_back(command);
21 }
22
23 static void EncodeCommand(const SceneContext& scene_context,
24 const Matrix& view_transform,
25 RenderPass& render_pass,
26 const SceneCommand& scene_command) {
27 auto& host_buffer = scene_context.GetTransientsBuffer();
28
29 render_pass.SetCommandLabel(scene_command.label);
30 // TODO(bdero): Configurable stencil ref per-command.
31 render_pass.SetStencilReference(0);
32
33 render_pass.SetPipeline(scene_context.GetPipeline(
34 PipelineKey{scene_command.geometry->GetGeometryType(),
35 scene_command.material->GetMaterialType()},
36 scene_command.material->GetContextOptions(render_pass)));
37
38 scene_command.geometry->BindToCommand(
39 scene_context, host_buffer, view_transform * scene_command.transform,
40 render_pass);
41 scene_command.material->BindToCommand(scene_context, host_buffer,
42 render_pass);
43
44 render_pass.Draw();
45 ;
46 }
47
48 std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
49 const SceneContext& scene_context,
50 const Matrix& camera_transform,
51 RenderTarget render_target) const {
52 {
53 TextureDescriptor ds_texture;
54 ds_texture.type = TextureType::kTexture2DMultisample;
55 ds_texture.format = PixelFormat::kD32FloatS8UInt;
56 ds_texture.size = render_target.GetRenderTargetSize();
57 ds_texture.usage = TextureUsage::kRenderTarget;
58 ds_texture.sample_count = SampleCount::kCount4;
59 ds_texture.storage_mode = StorageMode::kDeviceTransient;
60 auto texture =
61 scene_context.GetContext()->GetResourceAllocator()->CreateTexture(
62 ds_texture);
63
64 DepthAttachment depth;
65 depth.load_action = LoadAction::kClear;
66 depth.store_action = StoreAction::kDontCare;
67 depth.clear_depth = 1.0;
68 depth.texture = texture;
69 render_target.SetDepthAttachment(depth);
70
71 // The stencil and depth buffers must be the same texture for MacOS ARM
72 // and Vulkan.
73 StencilAttachment stencil;
74 stencil.load_action = LoadAction::kClear;
75 stencil.store_action = StoreAction::kDontCare;
76 stencil.clear_stencil = 0u;
77 stencil.texture = texture;
78 render_target.SetStencilAttachment(stencil);
79 }
80
81 auto command_buffer = scene_context.GetContext()->CreateCommandBuffer();
82 if (!command_buffer) {
83 FML_LOG(ERROR) << "Failed to create command buffer.";
84 return nullptr;
85 }
86
87 auto render_pass = command_buffer->CreateRenderPass(render_target);
88 if (!render_pass) {
89 FML_LOG(ERROR) << "Failed to create render pass.";
90 return nullptr;
91 }
92
93 for (auto& command : commands_) {
94 EncodeCommand(scene_context, camera_transform, *render_pass, command);
95 }
96
97 if (!render_pass->EncodeCommands()) {
98 FML_LOG(ERROR) << "Failed to encode render pass commands.";
99 return nullptr;
100 }
101
102 return command_buffer;
103 }
104
105 } // namespace scene
106 } // namespace impeller
impeller::scene::SceneCommand::material
Material * material
Definition: scene_encoder.h:27
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:642
scene_context.h
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:40
impeller::scene::SceneCommand::geometry
Geometry * geometry
Definition: scene_encoder.h:26
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:44
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:43
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:18
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:39
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:23
command.h
impeller::TextureType::kTexture2DMultisample
@ kTexture2DMultisample
impeller::scene::PipelineKey
Definition: pipeline_key.h:23
impeller::StencilAttachment
Definition: formats.h:655
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:639
impeller::RenderTarget
Definition: render_target.h:38
impeller::StencilAttachment::clear_stencil
uint32_t clear_stencil
Definition: formats.h:656
impeller::RenderPass::SetStencilReference
virtual void SetStencilReference(uint32_t value)
Definition: render_pass.cc:103
impeller::scene::SceneCommand::transform
Matrix transform
Definition: scene_encoder.h:25
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:41
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:188
impeller::scene::SceneCommand
Definition: scene_encoder.h:23
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:641
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:652
impeller::SampleCount::kCount4
@ kCount4
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
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:651
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
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:24
impeller::scene::Geometry::BindToCommand
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const =0
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37