| 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
10 #include "impeller/renderer/render_pass.h"
11 #include "impeller/renderer/render_target.h"
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 ///
24 struct PassBindingsCacheMTL {
25 explicit PassBindingsCacheMTL() {}
26
27 ~PassBindingsCacheMTL() = default;
28
29 PassBindingsCacheMTL(const PassBindingsCacheMTL&) = delete;
30
31 PassBindingsCacheMTL(PassBindingsCacheMTL&&) = delete;
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_
impeller
Definition: allocation.cc:12
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:22
render_pass.h
render_target.h
impeller::PassBindingsCacheMTL
Ensures that bindings on the pass are not redundantly set or updated. Avoids making the driver do add...
Definition: pass_bindings_cache_mtl.h:24
impeller::PassBindingsCacheMTL::PassBindingsCacheMTL
PassBindingsCacheMTL(const PassBindingsCacheMTL &)=delete
impeller::PassBindingsCacheMTL::SetScissor
void SetScissor(const IRect &scissor)
Set the encoder scissor rect if the value is different from the current encoder state.
Definition: pass_bindings_cache_mtl.mm:138
impeller::PassBindingsCacheMTL::~PassBindingsCacheMTL
~PassBindingsCacheMTL()=default
impeller::PassBindingsCacheMTL::SetStencilRef
void SetStencilRef(uint32_t stencil_ref)
Set the encoder's stencil reference if the value is different from the current encoder state.
Definition: pass_bindings_cache_mtl.mm:152
impeller::PassBindingsCacheMTL::SetRenderPipelineState
void SetRenderPipelineState(id< MTLRenderPipelineState > pipeline)
Set the render pipeline state for the current encoder.
Definition: pass_bindings_cache_mtl.mm:13
impeller::PassBindingsCacheMTL::SetSampler
bool SetSampler(ShaderStage stage, uint64_t index, id< MTLSamplerState > sampler)
Set the sampler for the given stage and binding.
Definition: pass_bindings_cache_mtl.mm:99
impeller::PassBindingsCacheMTL::SetBuffer
bool SetBuffer(ShaderStage stage, uint64_t index, uint64_t offset, id< MTLBuffer > buffer)
Set the buffer for the given shader stage, binding, and offset.
Definition: pass_bindings_cache_mtl.mm:31
impeller::PassBindingsCacheMTL::SetViewport
void SetViewport(const Viewport &viewport)
Set the viewport if the value is different from the current encoder state.
Definition: pass_bindings_cache_mtl.mm:123
impeller::PassBindingsCacheMTL::SetDepthStencilState
void SetDepthStencilState(id< MTLDepthStencilState > depth_stencil)
Set the depth and stencil state for the current encoder.
Definition: pass_bindings_cache_mtl.mm:22
impeller::PassBindingsCacheMTL::SetEncoder
void SetEncoder(id< MTLRenderCommandEncoder > encoder)
Set the command encoder for this pass bindings cache.
Definition: pass_bindings_cache_mtl.mm:9
impeller::PassBindingsCacheMTL::SetTexture
bool SetTexture(ShaderStage stage, uint64_t index, id< MTLTexture > texture)
Set the texture for the given stage and binding.
Definition: pass_bindings_cache_mtl.mm:75
impeller::PassBindingsCacheMTL::PassBindingsCacheMTL
PassBindingsCacheMTL(PassBindingsCacheMTL &&)=delete
impeller::PassBindingsCacheMTL::PassBindingsCacheMTL
PassBindingsCacheMTL()
Definition: pass_bindings_cache_mtl.h:25
impeller::TRect
Definition: rect.h:122
impeller::Viewport
Definition: formats.h:403