1/* 2 * Copyright 2022 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "src/gpu/graphite/mtl/MtlComputePipeline.h" 9 10#include "include/gpu/ShaderErrorHandler.h" 11#include "src/gpu/graphite/ComputePipelineDesc.h" 12#include "src/gpu/graphite/Log.h" 13#include "src/gpu/graphite/ResourceProvider.h" 14#include "src/gpu/graphite/mtl/MtlGraphiteUtilsPriv.h" 15#include "src/gpu/graphite/mtl/MtlSharedContext.h" 16#include "src/sksl/SkSLProgramKind.h" 17#include "src/sksl/SkSLProgramSettings.h" 18 19namespace skgpu::graphite { 20 21// static 22sk_sp<MtlComputePipeline> MtlComputePipeline::Make(const MtlSharedContext* sharedContext, 23 const std::string& label, 24 MSLFunction computeMain) { 25 id<MTLLibrary> library = std::get<0>(computeMain); 26 if (!library) { 27 return nullptr; 28 } 29 30 sk_cfp<MTLComputePipelineDescriptor*> psoDescriptor([MTLComputePipelineDescriptor new]); 31 32 (*psoDescriptor).label = @(label.c_str()); 33 34 NSString* entryPointName = [NSString stringWithUTF8String:std::get<1>(computeMain).c_str()]; 35 (*psoDescriptor).computeFunction = [library newFunctionWithName:entryPointName]; 36 37 // TODO(b/240604614): Populate input data attribute and buffer layout descriptors using the 38 // `stageInputDescriptor` property based on the contents of `pipelineDesc` (on iOS 10+ or 39 // macOS 10.12+). 40 41 // TODO(b/240604614): Define input buffer mutability using the `buffers` property based on 42 // the contents of `pipelineDesc` (on iOS 11+ or macOS 10.13+). 43 44 // TODO(b/240615224): Metal docs claim that setting the 45 // `threadGroupSizeIsMultipleOfThreadExecutionWidth` to YES may improve performance, IF we can 46 // guarantee that the thread group size used in a dispatch command is a multiple of 47 // `threadExecutionWidth` property of the pipeline state object (otherwise this will cause UB). 48 49 NSError* error; 50 sk_cfp<id<MTLComputePipelineState>> pso([sharedContext->device() 51 newComputePipelineStateWithDescriptor:psoDescriptor.get() 52 options:MTLPipelineOptionNone 53 reflection:nil 54 error:&error]); 55 if (!pso) { 56 SKGPU_LOG_E("Compute pipeline creation failure:\n%s", error.debugDescription.UTF8String); 57 return nullptr; 58 } 59 60 return sk_sp<MtlComputePipeline>(new MtlComputePipeline(sharedContext, std::move(pso))); 61} 62 63void MtlComputePipeline::freeGpuData() { fPipelineState.reset(); } 64 65} // namespace skgpu::graphite 66