Flutter Impeller
render_pass.h
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 #ifndef FLUTTER_IMPELLER_RENDERER_RENDER_PASS_H_
6 #define FLUTTER_IMPELLER_RENDERER_RENDER_PASS_H_
7 
8 #include <string>
9 
10 #include "fml/status.h"
11 #include "impeller/core/formats.h"
18 
19 namespace impeller {
20 
21 class HostBuffer;
22 class Allocator;
23 
24 //------------------------------------------------------------------------------
25 /// @brief Render passes encode render commands directed as one specific
26 /// render target into an underlying command buffer.
27 ///
28 /// Render passes can be obtained from the command buffer in which
29 /// the pass is meant to encode commands into.
30 ///
31 /// @see `CommandBuffer`
32 ///
33 class RenderPass : public ResourceBinder {
34  public:
35  virtual ~RenderPass();
36 
37  const std::shared_ptr<const Context>& GetContext() const;
38 
39  const RenderTarget& GetRenderTarget() const;
40 
41  ISize GetRenderTargetSize() const;
42 
43  const Matrix& GetOrthographicTransform() const;
44 
45  virtual bool IsValid() const = 0;
46 
47  void SetLabel(std::string label);
48 
49  /// @brief Reserve [command_count] commands in the HAL command buffer.
50  ///
51  /// Note: this is not the native command buffer.
52  virtual void ReserveCommands(size_t command_count) {
53  commands_.reserve(command_count);
54  }
55 
56  //----------------------------------------------------------------------------
57  /// The pipeline to use for this command.
58  virtual void SetPipeline(
59  const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline);
60 
61  //----------------------------------------------------------------------------
62  /// The debugging label to use for the command.
63  virtual void SetCommandLabel(std::string_view label);
64 
65  //----------------------------------------------------------------------------
66  /// The reference value to use in stenciling operations. Stencil configuration
67  /// is part of pipeline setup and can be read from the pipelines descriptor.
68  ///
69  /// @see `Pipeline`
70  /// @see `PipelineDescriptor`
71  ///
72  virtual void SetStencilReference(uint32_t value);
73 
74  virtual void SetBaseVertex(uint64_t value);
75 
76  //----------------------------------------------------------------------------
77  /// The viewport coordinates that the rasterizer linearly maps normalized
78  /// device coordinates to.
79  /// If unset, the viewport is the size of the render target with a zero
80  /// origin, znear=0, and zfar=1.
81  ///
82  virtual void SetViewport(Viewport viewport);
83 
84  //----------------------------------------------------------------------------
85  /// The scissor rect to use for clipping writes to the render target. The
86  /// scissor rect must lie entirely within the render target.
87  /// If unset, no scissor is applied.
88  ///
89  virtual void SetScissor(IRect scissor);
90 
91  //----------------------------------------------------------------------------
92  /// The number of instances of the given set of vertices to render. Not all
93  /// backends support rendering more than one instance at a time.
94  ///
95  /// @warning Setting this to more than one will limit the availability of
96  /// backends to use with this command.
97  ///
98  virtual void SetInstanceCount(size_t count);
99 
100  //----------------------------------------------------------------------------
101  /// @brief Specify the vertex and index buffer to use for this command.
102  ///
103  /// @param[in] buffer The vertex and index buffer definition. If possible,
104  /// this value should be moved and not copied.
105  ///
106  /// @return returns if the binding was updated.
107  ///
108  virtual bool SetVertexBuffer(VertexBuffer buffer);
109 
110  /// Record the currently pending command.
111  virtual fml::Status Draw();
112 
113  // |ResourceBinder|
114  virtual bool BindResource(ShaderStage stage,
115  DescriptorType type,
116  const ShaderUniformSlot& slot,
117  const ShaderMetadata& metadata,
118  BufferView view) override;
119 
120  virtual bool BindResource(
121  ShaderStage stage,
122  DescriptorType type,
123  const ShaderUniformSlot& slot,
124  const std::shared_ptr<const ShaderMetadata>& metadata,
125  BufferView view);
126 
127  // |ResourceBinder|
128  virtual bool BindResource(
129  ShaderStage stage,
130  DescriptorType type,
131  const SampledImageSlot& slot,
132  const ShaderMetadata& metadata,
133  std::shared_ptr<const Texture> texture,
134  const std::unique_ptr<const Sampler>& sampler) override;
135 
136  //----------------------------------------------------------------------------
137  /// @brief Encode the recorded commands to the underlying command buffer.
138  ///
139  /// @return If the commands were encoded to the underlying command
140  /// buffer.
141  ///
142  bool EncodeCommands() const;
143 
144  //----------------------------------------------------------------------------
145  /// @brief Accessor for the current Commands.
146  ///
147  /// @details Visible for testing.
148  ///
149  virtual const std::vector<Command>& GetCommands() const { return commands_; }
150 
151  //----------------------------------------------------------------------------
152  /// @brief The sample count of the attached render target.
153  SampleCount GetSampleCount() const;
154 
155  //----------------------------------------------------------------------------
156  /// @brief The pixel format of the attached render target.
158 
159  //----------------------------------------------------------------------------
160  /// @brief Whether the render target has a depth attachment.
161  bool HasDepthAttachment() const;
162 
163  //----------------------------------------------------------------------------
164  /// @brief Whether the render target has an stencil attachment.
165  bool HasStencilAttachment() const;
166 
167  protected:
168  const std::shared_ptr<const Context> context_;
169  // The following properties: sample_count, pixel_format,
170  // has_stencil_attachment, and render_target_size are cached on the
171  // RenderTarget to speed up numerous lookups during rendering. This is safe as
172  // the RenderTarget itself is copied into the RenderTarget and only exposed as
173  // a const reference.
180  std::vector<Command> commands_;
182 
183  //----------------------------------------------------------------------------
184  /// @brief Record a command for subsequent encoding to the underlying
185  /// command buffer. No work is encoded into the command buffer at
186  /// this time.
187  ///
188  /// @param[in] command The command
189  ///
190  /// @return If the command was valid for subsequent commitment.
191  ///
192  bool AddCommand(Command&& command);
193 
194  RenderPass(std::shared_ptr<const Context> context,
195  const RenderTarget& target);
196 
197  virtual void OnSetLabel(std::string label) = 0;
198 
199  virtual bool OnEncodeCommands(const Context& context) const = 0;
200 
201  private:
202  RenderPass(const RenderPass&) = delete;
203 
204  RenderPass& operator=(const RenderPass&) = delete;
205 
206  Command pending_;
207 };
208 
209 } // namespace impeller
210 
211 #endif // FLUTTER_IMPELLER_RENDERER_RENDER_PASS_H_
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::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
vertex_buffer.h
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
formats.h
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::RenderPass::GetCommands
virtual const std::vector< Command > & GetCommands() const
Accessor for the current Commands.
Definition: render_pass.h:149
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::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
command.h
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
resource_binder.h
impeller::ResourceBinder
An interface for binding resources. This is implemented by |Command| and |ComputeCommand| to make GPU...
Definition: resource_binder.h:23
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::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::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Viewport
Definition: formats.h:398
command_buffer.h
impeller::RenderPass::ReserveCommands
virtual void ReserveCommands(size_t command_count)
Reserve [command_count] commands in the HAL command buffer.
Definition: render_pass.h:52
impeller::BufferView
Definition: buffer_view.h:15
impeller::RenderPass::SetLabel
void SetLabel(std::string label)
Definition: render_pass.cc:51
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
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
render_target.h
impeller::RenderPass::has_stencil_attachment_
const bool has_stencil_attachment_
Definition: render_pass.h:177
impeller::RenderPass::IsValid
virtual bool IsValid() const =0
impeller::RenderPass::orthographic_
const Matrix orthographic_
Definition: render_pass.h:181
shader_types.h
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
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