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"
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 = render_pass.GetTransientsBuffer();
28 
29  Command cmd;
30  DEBUG_COMMAND_INFO(cmd, scene_command.label);
31  cmd.stencil_reference =
32  0; // TODO(bdero): Configurable stencil ref per-command.
33 
34  cmd.pipeline = scene_context.GetPipeline(
35  PipelineKey{scene_command.geometry->GetGeometryType(),
36  scene_command.material->GetMaterialType()},
37  scene_command.material->GetContextOptions(render_pass));
38 
39  scene_command.geometry->BindToCommand(
40  scene_context, host_buffer, view_transform * scene_command.transform,
41  cmd);
42  scene_command.material->BindToCommand(scene_context, host_buffer, cmd);
43 
44  render_pass.AddCommand(std::move(cmd));
45 }
46 
47 std::shared_ptr<CommandBuffer> SceneEncoder::BuildSceneCommandBuffer(
48  const SceneContext& scene_context,
49  const Matrix& camera_transform,
50  RenderTarget render_target) const {
51  {
52  TextureDescriptor ds_texture;
55  ds_texture.size = render_target.GetRenderTargetSize();
56  ds_texture.usage =
60  auto texture =
61  scene_context.GetContext()->GetResourceAllocator()->CreateTexture(
62  ds_texture);
63 
64  DepthAttachment depth;
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;
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::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:99
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::scene::Material::GetMaterialType
virtual MaterialType GetMaterialType() const =0
impeller::scene::SceneCommand::material
Material * material
Definition: scene_encoder.h:26
impeller::TextureUsageMask
uint64_t TextureUsageMask
Definition: formats.h:274
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:594
scene_context.h
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:42
impeller::scene::SceneCommand::geometry
Geometry * geometry
Definition: scene_encoder.h:25
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::StoreAction::kDontCare
@ kDontCare
impeller::scene::SceneContext
Definition: scene_context.h:39
impeller::scene::Material::GetContextOptions
SceneContextOptions GetContextOptions(const RenderPass &pass) const
Definition: material.cc:62
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:47
impeller::scene::Material::BindToCommand
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, Command &command) const =0
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:45
impeller::SampleCount::kCount4
@ kCount4
scene_encoder.h
impeller::scene::SceneEncoder::Add
void Add(const SceneCommand &command)
Definition: scene_encoder.cc:18
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:41
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:22
impeller::StencilAttachment
Definition: formats.h:607
impeller::LoadAction::kClear
@ kClear
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:591
impeller::PixelFormat::kD32FloatS8UInt
@ kD32FloatS8UInt
impeller::RenderTarget
Definition: render_target.h:48
impeller::StencilAttachment::clear_stencil
uint32_t clear_stencil
Definition: formats.h:608
impeller::scene::SceneCommand::transform
Matrix transform
Definition: scene_encoder.h:24
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:150
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:43
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
impeller::RenderTarget::SetStencilAttachment
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
Definition: render_target.cc:199
impeller::scene::SceneCommand
Definition: scene_encoder.h:22
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:593
impeller::scene::Geometry::BindToCommand
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, Command &command) const =0
impeller::scene::SceneContext::GetPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
Definition: scene_context.cc:76
impeller::DepthAttachment::clear_depth
double clear_depth
Definition: formats.h:604
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:40
impeller::DepthAttachment
Definition: formats.h:603
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:39
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
render_target.h
impeller::RenderTarget::SetDepthAttachment
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
Definition: render_target.cc:189
impeller::scene::SceneCommand::label
std::string label
Definition: scene_encoder.h:23
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:46
impeller
Definition: aiks_context.cc:10
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
impeller::scene::Geometry::GetGeometryType
virtual GeometryType GetGeometryType() const =0