7#include <Metal/Metal.h>
10#include "flutter/fml/backtrace.h"
11#include "flutter/fml/closure.h"
12#include "flutter/fml/logging.h"
13#include "flutter/fml/trace_event.h"
29ComputePassMTL::ComputePassMTL(std::shared_ptr<const Context> context,
30 id<MTLCommandBuffer>
buffer)
31 : ComputePass(
std::move(context)), buffer_(
buffer) {
35 encoder_ = [buffer_ computeCommandEncoderWithDispatchType:
36 MTLDispatchType::MTLDispatchTypeConcurrent];
40 pass_bindings_cache_.SetEncoder(encoder_);
44ComputePassMTL::~ComputePassMTL() =
default;
46bool ComputePassMTL::IsValid()
const {
50void ComputePassMTL::OnSetLabel(
const std::string& label) {
55 [encoder_ setLabel:@(label.c_str())];
59void ComputePassMTL::SetCommandLabel(std::string_view label) {
61 [encoder_ pushDebugGroup:@(label.data())];
65void ComputePassMTL::SetPipeline(
67 pass_bindings_cache_.SetComputePipelineState(
68 ComputePipelineMTL::Cast(*pipeline).GetMTLComputePipelineState());
72void ComputePassMTL::AddBufferMemoryBarrier() {
73 [encoder_ memoryBarrierWithScope:MTLBarrierScopeBuffers];
77void ComputePassMTL::AddTextureMemoryBarrier() {
78 [encoder_ memoryBarrierWithScope:MTLBarrierScopeTextures];
91 const std::shared_ptr<const DeviceBuffer>& device_buffer = view.
buffer;
96 id<MTLBuffer>
buffer = DeviceBufferMTL::Cast(*device_buffer).GetMTLBuffer();
107bool ComputePassMTL::BindResource(
112 std::shared_ptr<const Texture>
texture,
113 const std::unique_ptr<const Sampler>& sampler) {
114 if (!sampler || !
texture->IsValid()) {
119 TextureMTL::Cast(*texture).GetMTLTexture());
120 pass_bindings_cache_.SetSampler(
121 slot.
texture_index, SamplerMTL::Cast(*sampler).GetMTLSamplerState());
128 "Invalid grid size for compute command.");
136 auto max_total_threads_per_threadgroup =
static_cast<int64_t
>(
137 pass_bindings_cache_.GetPipeline().maxTotalThreadsPerThreadgroup);
141 int64_t thread_groups = std::max(
142 static_cast<int64_t
>(
143 std::ceil(
width * 1.0 / max_total_threads_per_threadgroup * 1.0)),
146 dispatchThreadgroups:MTLSizeMake(thread_groups, 1, 1)
147 threadsPerThreadgroup:MTLSizeMake(max_total_threads_per_threadgroup, 1,
150 while (
width *
height > max_total_threads_per_threadgroup) {
156 [encoder_ dispatchThreadgroups:size threadsPerThreadgroup:size];
161 [encoder_ popDebugGroup];
168bool ComputePassMTL::EncodeCommands()
const {
169 [encoder_ endEncoding];
Describes the fixed function and programmable aspects of rendering and compute operations performed b...
static const uint8_t buffer[]
std::shared_ptr< const DeviceBuffer > buffer
Metadata required to bind a combined texture and sampler.
size_t texture_index
ext_res_0 is the Metal binding value.
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.