| 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/GpuTypes.h" |
| 11 |
#include "include/gpu/ShaderErrorHandler.h" |
| 12 |
#include "src/gpu/graphite/ComputePipelineDesc.h" |
| 13 |
#include "src/gpu/graphite/ContextUtils.h" |
| 14 |
#include "src/gpu/graphite/Log.h" |
| 15 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 16 |
#include "src/gpu/graphite/mtl/MtlGraphiteUtils.h" |
| 17 |
#include "src/gpu/graphite/mtl/MtlSharedContext.h" |
| 18 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 19 |
#include "src/sksl/SkSLCompiler.h" |
| 20 |
#include "src/sksl/SkSLProgramKind.h" |
| 21 |
#include "src/sksl/SkSLProgramSettings.h" |
| 22 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 23 |
#include "src/sksl/ir/SkSLProgram.h" |
| 24 |
|
| 25 |
namespace skgpu::graphite { |
| 26 |
|
| 27 |
// static |
| 28 |
sk_sp<MtlComputePipeline> MtlComputePipeline::Make(const MtlSharedContext* sharedContext, |
| 29 |
const ComputePipelineDesc& pipelineDesc) { |
| 30 |
sk_cfp<id<MTLLibrary>> library; |
| 31 |
std::string entryPointName; |
| 32 |
ShaderErrorHandler* errorHandler = sharedContext->caps()->shaderErrorHandler(); |
| 33 |
if (pipelineDesc.computeStep()->supportsNativeShader()) { |
| 34 |
auto nativeShader = pipelineDesc.computeStep()->nativeShaderSource( |
| 35 |
ComputeStep::NativeShaderFormat::kMSL); |
| 36 |
library = MtlCompileShaderLibrary(sharedContext, |
| 37 |
pipelineDesc.computeStep()->name(), |
| 38 |
nativeShader.fSource, |
| 39 |
errorHandler); |
| 40 |
if (library == nil) { |
| 41 |
return nullptr; |
| 42 |
} |
| 43 |
entryPointName = std::move(nativeShader.fEntryPoint); |
| 44 |
} else { |
| 45 |
SkSL::NativeShader msl; |
| 46 |
SkSL::Program::Interface interface; |
| 47 |
SkSL::ProgramSettings settings; |
| 48 |
|
| 49 |
SkSL::Compiler skslCompiler; |
| 50 |
std::string sksl = BuildComputeSkSL(sharedContext->caps(), |
| 51 |
pipelineDesc.computeStep(), |
| 52 |
BackendApi::kMetal); |
| 53 |
if (!SkSLToMSL(sharedContext->caps()->shaderCaps(), |
| 54 |
sksl, |
| 55 |
SkSL::ProgramKind::kCompute, |
| 56 |
settings, |
| 57 |
&msl, |
| 58 |
&interface, |
| 59 |
errorHandler)) { |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
library = MtlCompileShaderLibrary( |
| 63 |
sharedContext, pipelineDesc.computeStep()->name(), msl.fText, errorHandler); |
| 64 |
if (library == nil) { |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
entryPointName = "computeMain"; |
| 68 |
} |
| 69 |
|
| 70 |
sk_cfp<MTLComputePipelineDescriptor*> psoDescriptor([MTLComputePipelineDescriptor new]); |
| 71 |
|
| 72 |
(*psoDescriptor).label = @(pipelineDesc.computeStep()->name()); |
| 73 |
|
| 74 |
NSString* entryPoint = [NSString stringWithUTF8String:entryPointName.c_str()]; |
| 75 |
(*psoDescriptor).computeFunction = [library.get() newFunctionWithName:entryPoint]; |
| 76 |
|
| 77 |
// TODO(b/240604614): Populate input data attribute and buffer layout descriptors using the |
| 78 |
// `stageInputDescriptor` property based on the contents of `pipelineDesc` (on iOS 10+ or |
| 79 |
// macOS 10.12+). |
| 80 |
|
| 81 |
// TODO(b/240604614): Define input buffer mutability using the `buffers` property based on |
| 82 |
// the contents of `pipelineDesc` (on iOS 11+ or macOS 10.13+). |
| 83 |
|
| 84 |
// TODO(b/240615224): Metal docs claim that setting the |
| 85 |
// `threadGroupSizeIsMultipleOfThreadExecutionWidth` to YES may improve performance, IF we can |
| 86 |
// guarantee that the thread group size used in a dispatch command is a multiple of |
| 87 |
// `threadExecutionWidth` property of the pipeline state object (otherwise this will cause UB). |
| 88 |
|
| 89 |
NSError* error; |
| 90 |
sk_cfp<id<MTLComputePipelineState>> pso([sharedContext->device() |
| 91 |
newComputePipelineStateWithDescriptor:psoDescriptor.get() |
| 92 |
options:MTLPipelineOptionNone |
| 93 |
reflection:nil |
| 94 |
error:&error]); |
| 95 |
if (!pso) { |
| 96 |
SKGPU_LOG_E("Compute pipeline creation failure:\n%s", error.debugDescription.UTF8String); |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
return sk_sp<MtlComputePipeline>(new MtlComputePipeline(sharedContext, std::move(pso))); |
| 101 |
} |
| 102 |
|
| 103 |
void MtlComputePipeline::freeGpuData() { fPipelineState.reset(); } |
| 104 |
|
| 105 |
} // namespace skgpu::graphite |