Flutter Impeller
pass_bindings_cache_mtl.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_BACKEND_METAL_PASS_BINDINGS_CACHE_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_PASS_BINDINGS_CACHE_MTL_H_
7 
8 #include <Metal/Metal.h>
9 
12 
13 namespace impeller {
14 
15 //-----------------------------------------------------------------------------
16 /// @brief Ensures that bindings on the pass are not redundantly set or
17 /// updated. Avoids making the driver do additional checks and makes
18 /// the frame insights during profiling and instrumentation not
19 /// complain about the same.
20 ///
21 /// There should be no change to rendering if this caching was
22 /// absent.
23 ///
25  explicit PassBindingsCacheMTL() {}
26 
27  ~PassBindingsCacheMTL() = default;
28 
30 
32 
33  /// @brief Set the command encoder for this pass bindings cache.
34  ///
35  /// The encoder must be set before any state adjusting commands can be called.
36  void SetEncoder(id<MTLRenderCommandEncoder> encoder);
37 
38  /// @brief Set the render pipeline state for the current encoder.
39  ///
40  /// If this matches the previous render pipeline state, no update
41  /// is performed.
42  void SetRenderPipelineState(id<MTLRenderPipelineState> pipeline);
43 
44  /// @brief Set the depth and stencil state for the current encoder.
45  ///
46  /// If this matches the previous depth and stencil state, no update
47  /// is performed.
48  void SetDepthStencilState(id<MTLDepthStencilState> depth_stencil);
49 
50  /// @brief Set the buffer for the given shader stage, binding, and offset.
51  ///
52  /// If the buffer is already bound, only the offset is updated.
53  bool SetBuffer(ShaderStage stage,
54  uint64_t index,
55  uint64_t offset,
56  id<MTLBuffer> buffer);
57 
58  /// @brief Set the texture for the given stage and binding.
59  ///
60  /// If the same texture is already bound at the index for this stage, no
61  /// state updates are performed.
62  bool SetTexture(ShaderStage stage, uint64_t index, id<MTLTexture> texture);
63 
64  /// @brief Set the sampler for the given stage and binding.
65  ///
66  /// If the same sampler is already bound at the index for this stage, no
67  /// state updates are performed.
68  bool SetSampler(ShaderStage stage,
69  uint64_t index,
70  id<MTLSamplerState> sampler);
71 
72  /// @brief Set the viewport if the value is different from the current encoder
73  /// state
74  void SetViewport(const Viewport& viewport);
75 
76  /// @brief Set the encoder scissor rect if the value is different from the
77  /// current encoder state.
78  void SetScissor(const IRect& scissor);
79 
80  /// @brief Set the encoder's stencil reference if the value is different from
81  /// the current encoder state.
82  void SetStencilRef(uint32_t stencil_ref);
83 
84  private:
85  struct BufferOffsetPair {
86  id<MTLBuffer> buffer = nullptr;
87  size_t offset = 0u;
88  };
89  using BufferMap = std::map<uint64_t, BufferOffsetPair>;
90  using TextureMap = std::map<uint64_t, id<MTLTexture>>;
91  using SamplerMap = std::map<uint64_t, id<MTLSamplerState>>;
92 
93  id<MTLRenderCommandEncoder> encoder_;
94  id<MTLRenderPipelineState> pipeline_ = nullptr;
95  id<MTLDepthStencilState> depth_stencil_ = nullptr;
96  std::map<ShaderStage, BufferMap> buffers_;
97  std::map<ShaderStage, TextureMap> textures_;
98  std::map<ShaderStage, SamplerMap> samplers_;
99  std::optional<Viewport> viewport_;
100  std::optional<IRect> scissor_;
101  std::optional<uint32_t> stencil_ref_;
102 };
103 
104 } // namespace impeller
105 
106 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_PASS_BINDINGS_CACHE_MTL_H_
Ensures that bindings on the pass are not redundantly set or updated. Avoids making the driver do add...
PassBindingsCacheMTL(const PassBindingsCacheMTL &)=delete
void SetScissor(const IRect &scissor)
Set the encoder scissor rect if the value is different from the current encoder state.
void SetStencilRef(uint32_t stencil_ref)
Set the encoder's stencil reference if the value is different from the current encoder state.
void SetRenderPipelineState(id< MTLRenderPipelineState > pipeline)
Set the render pipeline state for the current encoder.
bool SetSampler(ShaderStage stage, uint64_t index, id< MTLSamplerState > sampler)
Set the sampler for the given stage and binding.
bool SetBuffer(ShaderStage stage, uint64_t index, uint64_t offset, id< MTLBuffer > buffer)
Set the buffer for the given shader stage, binding, and offset.
void SetViewport(const Viewport &viewport)
Set the viewport if the value is different from the current encoder state.
void SetDepthStencilState(id< MTLDepthStencilState > depth_stencil)
Set the depth and stencil state for the current encoder.
void SetEncoder(id< MTLRenderCommandEncoder > encoder)
Set the command encoder for this pass bindings cache.
bool SetTexture(ShaderStage stage, uint64_t index, id< MTLTexture > texture)
Set the texture for the given stage and binding.
PassBindingsCacheMTL(PassBindingsCacheMTL &&)=delete