7#include <Foundation/Foundation.h>
8#include <Metal/Metal.h>
19#if !__has_feature(objc_arc)
20#error ARC must be enabled !
28PipelineLibraryMTL::~PipelineLibraryMTL() =
default;
30using Callback = std::function<void(MTLRenderPipelineDescriptor*)>;
34 auto descriptor = [[MTLRenderPipelineDescriptor alloc] init];
35 descriptor.label = @(desc.
GetLabel().data());
36 descriptor.rasterSampleCount =
static_cast<NSUInteger
>(desc.
GetSampleCount());
37 bool created_specialized_function =
false;
42 vertex_descriptor->GetStageInputs(),
43 vertex_descriptor->GetStageLayouts())) {
44 descriptor.vertexDescriptor =
50 descriptor.colorAttachments[item.first] =
54 descriptor.depthAttachmentPixelFormat =
56 descriptor.stencilAttachmentPixelFormat =
61 if (entry.first == ShaderStage::kVertex) {
62 descriptor.vertexFunction =
63 ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
65 if (entry.first == ShaderStage::kFragment) {
66 if (constants.empty()) {
67 descriptor.fragmentFunction =
68 ShaderFunctionMTL::Cast(*entry.second).GetMTLFunction();
72 created_specialized_function =
true;
73 ShaderFunctionMTL::Cast(*entry.second)
74 .GetMTLFunctionSpecialized(
76 descriptor.fragmentFunction =
function;
83 if (!created_specialized_function) {
90 auto descriptor = [[MTLComputePipelineDescriptor alloc] init];
91 descriptor.label = @(desc.
GetLabel().c_str());
92 descriptor.computeFunction =
105 return [
device newDepthStencilStateWithDescriptor:descriptor];
109bool PipelineLibraryMTL::IsValid()
const {
110 return device_ !=
nullptr;
114PipelineFuture<PipelineDescriptor> PipelineLibraryMTL::GetPipeline(
115 PipelineDescriptor descriptor,
119 return found->second;
125 RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(
nullptr)};
128 auto promise = std::make_shared<
129 std::promise<std::shared_ptr<Pipeline<PipelineDescriptor>>>>();
130 auto pipeline_future =
131 PipelineFuture<PipelineDescriptor>{descriptor, promise->get_future()};
133 auto weak_this = weak_from_this();
135 auto get_pipeline_descriptor =
137 device = device_](MTLNewRenderPipelineStateCompletionHandler
handler) {
141 [
device newRenderPipelineStateWithDescriptor:descriptor
147 std::optional<std::string> thread_name =
151 [NSThread isMainThread] ?
"main"
152 : [[[NSThread currentThread]
name] UTF8String];
154 auto completion_handler = ^(
155 id<MTLRenderPipelineState> _Nullable render_pipeline_state,
156 NSError* _Nullable
error) {
159 << descriptor.GetLabel() <<
" :"
160 <<
error.localizedDescription.UTF8String <<
" (thread: "
161 << (thread_name.has_value() ? *thread_name :
"unknown")
163 promise->set_value(
nullptr);
167 auto strong_this = weak_this.lock();
169 promise->set_value(
nullptr);
173 auto new_pipeline = std::shared_ptr<PipelineMTL>(
new PipelineMTL(
176 render_pipeline_state,
179 promise->set_value(new_pipeline);
182 ^(id<MTLRenderPipelineState> _Nullable render_pipeline_state,
183 NSError* _Nullable
error) {
185 FML_LOG(INFO) <<
"pipeline creation retry";
189 dispatch_async(dispatch_get_main_queue(), ^{
190 get_pipeline_descriptor(completion_handler);
193 completion_handler(render_pipeline_state,
error);
196#if defined(FML_ARCH_CPU_X86_64)
197 get_pipeline_descriptor(retry_handler);
199 get_pipeline_descriptor(completion_handler);
202 return pipeline_future;
205PipelineFuture<ComputePipelineDescriptor> PipelineLibraryMTL::GetPipeline(
206 ComputePipelineDescriptor descriptor,
208 if (
auto found = compute_pipelines_.find(descriptor);
209 found != compute_pipelines_.end()) {
210 return found->second;
216 RealizedFuture<std::shared_ptr<Pipeline<ComputePipelineDescriptor>>>(
220 auto promise = std::make_shared<
221 std::promise<std::shared_ptr<Pipeline<ComputePipelineDescriptor>>>>();
222 auto pipeline_future = PipelineFuture<ComputePipelineDescriptor>{
223 descriptor, promise->get_future()};
224 compute_pipelines_[descriptor] = pipeline_future;
225 auto weak_this = weak_from_this();
227 auto completion_handler =
228 ^(id<MTLComputePipelineState> _Nullable compute_pipeline_state,
229 MTLComputePipelineReflection* _Nullable reflection,
230 NSError* _Nullable
error) {
233 <<
error.localizedDescription.UTF8String;
234 promise->set_value(
nullptr);
238 auto strong_this = weak_this.lock();
240 VALIDATION_LOG <<
"Library was collected before a pending pipeline "
241 "creation could finish.";
242 promise->set_value(
nullptr);
246 auto new_pipeline = std::shared_ptr<ComputePipelineMTL>(
247 new ComputePipelineMTL(weak_this,
249 compute_pipeline_state
251 promise->set_value(new_pipeline);
256 options:MTLPipelineOptionNone
257 completionHandler:completion_handler];
258 return pipeline_future;
262bool PipelineLibraryMTL::HasPipeline(
const PipelineDescriptor& descriptor) {
267void PipelineLibraryMTL::RemovePipelinesWithEntryPoint(
268 std::shared_ptr<const ShaderFunction> function) {
270 return item->first.GetEntrypointForStage(
function->GetStage())
271 ->IsEqual(*function);
std::shared_ptr< const ShaderFunction > GetStageEntrypoint() const
const std::string & GetLabel() const
std::string_view GetLabel() const
PixelFormat GetDepthPixelFormat() const
std::optional< DepthAttachmentDescriptor > GetDepthStencilAttachmentDescriptor() const
const std::map< ShaderStage, std::shared_ptr< const ShaderFunction > > & GetStageEntrypoints() const
PixelFormat GetStencilPixelFormat() const
const std::vector< Scalar > & GetSpecializationConstants() const
const std::map< size_t, ColorAttachmentDescriptor > & GetColorAttachmentDescriptors() const
std::optional< StencilAttachmentDescriptor > GetBackStencilAttachmentDescriptor() const
const std::shared_ptr< VertexDescriptor > & GetVertexDescriptor() const
std::optional< StencilAttachmentDescriptor > GetFrontStencilAttachmentDescriptor() const
SampleCount GetSampleCount() const
bool SetStageInputsAndLayout(const std::vector< ShaderStageIOSlot > &inputs, const std::vector< ShaderStageBufferLayout > &layouts)
MTLVertexDescriptor * GetMTLVertexDescriptor() const
std::vector< std::pair< uint64_t, std::unique_ptr< GenericRenderPipelineHandle > > > pipelines_
const gchar FlBinaryMessengerMessageHandler handler
const uint8_t uint32_t uint32_t GError ** error
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
#define FML_CHECK(condition)
Dart_NativeFunction function
void erase_if(Collection &container, const std::function< bool(typename Collection::iterator)> &predicate)
static void GetMTLRenderPipelineDescriptor(const PipelineDescriptor &desc, const Callback &callback)
std::function< void(MTLRenderPipelineDescriptor *)> Callback
static id< MTLDepthStencilState > CreateDepthStencilDescriptor(const PipelineDescriptor &desc, id< MTLDevice > device)
MTLDepthStencilDescriptor * ToMTLDepthStencilDescriptor(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
static MTLComputePipelineDescriptor * GetMTLComputePipelineDescriptor(const ComputePipelineDescriptor &desc)
MTLRenderPipelineColorAttachmentDescriptor * ToMTLRenderPipelineColorAttachmentDescriptor(ColorAttachmentDescriptor descriptor)