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 
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 
22 
24  return sample_count_;
25 }
26 
28  return pixel_format_;
29 }
30 
32  return has_depth_attachment_;
33 }
34 
37 }
38 
40  return render_target_;
41 }
42 
44  return render_target_size_;
45 }
46 
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 
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()) {
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 
85  return OnEncodeCommands(*context_);
86 }
87 
88 const std::shared_ptr<const Context>& RenderPass::GetContext() const {
89  return context_;
90 }
91 
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 
112  pending_.viewport = viewport;
113 }
114 
116  pending_.scissor = scissor;
117 }
118 
119 void RenderPass::SetInstanceCount(size_t count) {
120  pending_.instance_count = count;
121 }
122 
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|
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 
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|
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:100
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< int64_t >
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
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:398
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< int64_t >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
std
Definition: comparable.h:95
impeller::SampleCount
SampleCount
Definition: formats.h:296
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_blur_unittests.cc:20
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< int64_t >
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