Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
13namespace impeller {
14namespace scene {
15
16SceneEncoder::SceneEncoder() = default;
17
18void SceneEncoder::Add(const SceneCommand& command) {
19 // TODO(bdero): Manage multi-pass translucency ordering.
20 commands_.push_back(command);
21}
22
23static 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
48std::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;
56 ds_texture.size = render_target.GetRenderTargetSize();
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
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:33
virtual void SetStencilReference(uint32_t value)
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor > > &pipeline)
The pipeline to use for this command.
virtual fml::Status Draw()
Record the currently pending command.
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
ISize GetRenderTargetSize() const
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const =0
virtual void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, RenderPass &pass) const =0
SceneContextOptions GetContextOptions(const RenderPass &pass) const
Definition material.cc:62
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
HostBuffer & GetTransientsBuffer() const
void Add(const SceneCommand &command)
#define FML_LOG(severity)
Definition logging.h:82
FlTexture * texture
static void EncodeCommand(const SceneContext &scene_context, const Matrix &view_transform, RenderPass &render_pass, const SceneCommand &scene_command)
LoadAction load_action
Definition formats.h:641
std::shared_ptr< Texture > texture
Definition formats.h:639
StoreAction store_action
Definition formats.h:642
A 4x4 matrix using column-major storage.
Definition matrix.h:37
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define ERROR(message)