| Flutter Impeller |

render_pass.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/renderer/render_pass.h"
6 #include "fml/status.h"
7
8 namespace impeller {
9
10 RenderPass::RenderPass(std::shared_ptr<const Context> context,
11 const RenderTarget& target)
12 : context_(std::move(context)),
13 sample_count_(target.GetSampleCount()),
14 pixel_format_(target.GetRenderTargetPixelFormat()),
15 has_depth_attachment_(target.GetDepthAttachment().has_value()),
16 has_stencil_attachment_(target.GetStencilAttachment().has_value()),
17 render_target_size_(target.GetRenderTargetSize()),
18 render_target_(target),
19 orthographic_(Matrix::MakeOrthographic(render_target_size_)) {}
20
21 RenderPass::~RenderPass() {}
22
23 SampleCount RenderPass::GetSampleCount() const {
24 return sample_count_;
25 }
26
27 PixelFormat RenderPass::GetRenderTargetPixelFormat() const {
28 return pixel_format_;
29 }
30
31 bool RenderPass::HasDepthAttachment() const {
32 return has_depth_attachment_;
33 }
34
35 bool RenderPass::HasStencilAttachment() const {
36 return has_stencil_attachment_;
37 }
38
39 const RenderTarget& RenderPass::GetRenderTarget() const {
40 return render_target_;
41 }
42
43 ISize RenderPass::GetRenderTargetSize() const {
44 return render_target_size_;
45 }
46
47 const Matrix& RenderPass::GetOrthographicTransform() const {
48 return orthographic_;
49 }
50
51 void RenderPass::SetLabel(std::string label) {
52 if (label.empty()) {
53 return;
54 }
55 OnSetLabel(std::move(label));
56 }
57
58 bool RenderPass::AddCommand(Command&& command) {
59 if (!command.IsValid()) {
60 VALIDATION_LOG << "Attempted to add an invalid command to the render pass.";
61 return false;
62 }
63
64 if (command.scissor.has_value()) {
65 auto target_rect = IRect::MakeSize(render_target_.GetRenderTargetSize());
66 if (!target_rect.Contains(command.scissor.value())) {
67 VALIDATION_LOG << "Cannot apply a scissor that lies outside the bounds "
68 "of the render target.";
69 return false;
70 }
71 }
72
73 if (command.vertex_buffer.vertex_count == 0u ||
74 command.instance_count == 0u) {
75 // Essentially a no-op. Don't record the command but this is not necessary
76 // an error either.
77 return true;
78 }
79
80 commands_.emplace_back(std::move(command));
81 return true;
82 }
83
84 bool RenderPass::EncodeCommands() const {
85 return OnEncodeCommands(*context_);
86 }
87
88 const std::shared_ptr<const Context>& RenderPass::GetContext() const {
89 return context_;
90 }
91
92 void RenderPass::SetPipeline(
93 const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline) {
94 pending_.pipeline = pipeline;
95 }
96
97 void RenderPass::SetCommandLabel(std::string_view label) {
98 #ifdef IMPELLER_DEBUG
99 pending_.label = std::string(label);
100 #endif // IMPELLER_DEBUG
101 }
102
103 void RenderPass::SetStencilReference(uint32_t value) {
104 pending_.stencil_reference = value;
105 }
106
107 void RenderPass::SetBaseVertex(uint64_t value) {
108 pending_.base_vertex = value;
109 }
110
111 void RenderPass::SetViewport(Viewport viewport) {
112 pending_.viewport = viewport;
113 }
114
115 void RenderPass::SetScissor(IRect scissor) {
116 pending_.scissor = scissor;
117 }
118
119 void RenderPass::SetInstanceCount(size_t count) {
120 pending_.instance_count = count;
121 }
122
123 bool RenderPass::SetVertexBuffer(VertexBuffer buffer) {
124 return pending_.BindVertices(std::move(buffer));
125 }
126
127 fml::Status RenderPass::Draw() {
128 auto result = AddCommand(std::move(pending_));
129 pending_ = Command{};
130 if (result) {
131 return fml::Status();
132 }
133 return fml::Status(fml::StatusCode::kInvalidArgument,
134 "Failed to encode command");
135 }
136
137 // |ResourceBinder|
138 bool RenderPass::BindResource(ShaderStage stage,
139 DescriptorType type,
140 const ShaderUniformSlot& slot,
141 const ShaderMetadata& metadata,
142 BufferView view) {
143 return pending_.BindResource(stage, type, slot, metadata, view);
144 }
145
146 bool RenderPass::BindResource(
147 ShaderStage stage,
148 DescriptorType type,
149 const ShaderUniformSlot& slot,
150 const std::shared_ptr<const ShaderMetadata>& metadata,
151 BufferView view) {
152 return pending_.BindResource(stage, type, slot, metadata, std::move(view));
153 }
154
155 // |ResourceBinder|
156 bool RenderPass::BindResource(ShaderStage stage,
157 DescriptorType type,
158 const SampledImageSlot& slot,
159 const ShaderMetadata& metadata,
160 std::shared_ptr<const Texture> texture,
161 const std::unique_ptr<const Sampler>& sampler) {
162 return pending_.BindResource(stage, type, slot, metadata, std::move(texture),
163 sampler);
164 }
165
166 } // namespace impeller
impeller::Pipeline< PipelineDescriptor >
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:92
impeller::RenderPass::GetRenderTarget
const RenderTarget & GetRenderTarget() const
Definition: render_pass.cc:39
impeller::Command::scissor
std::optional< IRect > scissor
Definition: command.h:139
impeller::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
impeller::RenderPass::RenderPass
RenderPass(std::shared_ptr< const Context > context, const RenderTarget &target)
Definition: render_pass.cc:10
impeller::ShaderMetadata
Definition: shader_types.h:72
impeller::VertexBuffer
Definition: vertex_buffer.h:13
impeller::Command::viewport
std::optional< Viewport > viewport
Definition: command.h:133
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:22
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:123
impeller::RenderPass::has_depth_attachment_
const bool has_depth_attachment_
Definition: render_pass.h:176
impeller::Command::instance_count
size_t instance_count
Definition: command.h:147
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:47
impeller::Command::base_vertex
uint64_t base_vertex
Definition: command.h:126
impeller::RenderPass::pixel_format_
const PixelFormat pixel_format_
Definition: render_pass.h:175
impeller::RenderPass::BindResource
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: render_pass.cc:138
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::RenderPass::SetInstanceCount
virtual void SetInstanceCount(size_t count)
Definition: render_pass.cc:119
impeller::RenderPass::EncodeCommands
bool EncodeCommands() const
Encode the recorded commands to the underlying command buffer.
Definition: render_pass.cc:84
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:43
impeller::TSize
Definition: size.h:19
impeller::RenderPass::SetViewport
virtual void SetViewport(Viewport viewport)
Definition: render_pass.cc:111
render_pass.h
impeller::RenderPass::OnEncodeCommands
virtual bool OnEncodeCommands(const Context &context) const =0
impeller::SampledImageSlot
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::RenderPass::GetSampleCount
SampleCount GetSampleCount() const
The sample count of the attached render target.
Definition: render_pass.cc:23
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderPass::HasDepthAttachment
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
Definition: render_pass.cc:31
impeller::RenderPass::render_target_
const RenderTarget render_target_
Definition: render_pass.h:179
impeller::RenderPass::SetStencilReference
virtual void SetStencilReference(uint32_t value)
Definition: render_pass.cc:103
impeller::RenderPass::SetScissor
virtual void SetScissor(IRect scissor)
Definition: render_pass.cc:115
impeller::RenderPass::~RenderPass
virtual ~RenderPass()
Definition: render_pass.cc:21
impeller::RenderPass::commands_
std::vector< Command > commands_
Definition: render_pass.h:180
impeller::RenderPass::render_target_size_
const ISize render_target_size_
Definition: render_pass.h:178
impeller::RenderTarget::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_target.cc:139
impeller::Viewport
Definition: formats.h:397
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Command::BindResource
bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: command.cc:25
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
impeller::BufferView
Definition: buffer_view.h:15
impeller::RenderPass::SetLabel
void SetLabel(std::string label)
Definition: render_pass.cc:51
impeller::TRect::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
std
Definition: comparable.h:95
impeller::SampleCount
SampleCount
Definition: formats.h:295
impeller::RenderPass::HasStencilAttachment
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:35
impeller::RenderPass::OnSetLabel
virtual void OnSetLabel(std::string label)=0
impeller::RenderPass::context_
const std::shared_ptr< const Context > context_
Definition: render_pass.h:168
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::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::RenderPass::has_stencil_attachment_
const bool has_stencil_attachment_
Definition: render_pass.h:177
impeller::RenderPass::orthographic_
const Matrix orthographic_
Definition: render_pass.h:181
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:58
impeller
Definition: aiks_blend_unittests.cc:18
impeller::RenderPass::GetContext
const std::shared_ptr< const Context > & GetContext() const
Definition: render_pass.cc:88
impeller::RenderPass::SetBaseVertex
virtual void SetBaseVertex(uint64_t value)
Definition: render_pass.cc:107
impeller::TRect
Definition: rect.h:122
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::DescriptorType
DescriptorType
Definition: shader_types.h:153
impeller::RenderPass::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:27
impeller::RenderPass::sample_count_
const SampleCount sample_count_
Definition: render_pass.h:174