| 1 |
/* |
| 2 |
* Copyright 2021 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/MtlSharedContext.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/BackendTexture.h" |
| 11 |
#include "include/gpu/graphite/ContextOptions.h" |
| 12 |
#include "include/gpu/graphite/TextureInfo.h" |
| 13 |
#include "src/gpu/graphite/Caps.h" |
| 14 |
#include "src/gpu/graphite/GlobalCache.h" |
| 15 |
#include "src/gpu/graphite/Log.h" |
| 16 |
#include "src/gpu/graphite/mtl/MtlCommandBuffer.h" |
| 17 |
#include "src/gpu/graphite/mtl/MtlResourceProvider.h" |
| 18 |
#include "src/gpu/graphite/mtl/MtlTexture.h" |
| 19 |
#include "src/gpu/graphite/render/CommonDepthStencilSettings.h" |
| 20 |
#include "src/gpu/mtl/MtlMemoryAllocatorImpl.h" |
| 21 |
|
| 22 |
namespace skgpu::graphite { |
| 23 |
|
| 24 |
sk_sp<SharedContext> MtlSharedContext::Make(const MtlBackendContext& context, |
| 25 |
const ContextOptions& options) { |
| 26 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 27 |
// no warning needed |
| 28 |
} else { |
| 29 |
SKGPU_LOG_E("Skia's Graphite backend no longer supports this OS version."); |
| 30 |
#ifdef SK_BUILD_FOR_IOS |
| 31 |
SKGPU_LOG_E("Minimum supported version is iOS/tvOS 13.0."); |
| 32 |
#else |
| 33 |
SKGPU_LOG_E("Minimum supported version is MacOS 10.15."); |
| 34 |
#endif |
| 35 |
return nullptr; |
| 36 |
} |
| 37 |
|
| 38 |
sk_cfp<id<MTLDevice>> device = sk_ret_cfp((id<MTLDevice>)(context.fDevice.get())); |
| 39 |
|
| 40 |
std::unique_ptr<const MtlCaps> caps(new MtlCaps(device.get(), options)); |
| 41 |
|
| 42 |
// TODO: Add memory allocator to context once we figure out synchronization |
| 43 |
sk_sp<MtlMemoryAllocator> memoryAllocator = skgpu::MtlMemoryAllocatorImpl::Make(device.get()); |
| 44 |
if (!memoryAllocator) { |
| 45 |
SkDEBUGFAIL("No supplied Metal memory allocator and unable to create one internally."); |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
return sk_sp<SharedContext>(new MtlSharedContext(std::move(device), |
| 50 |
std::move(memoryAllocator), |
| 51 |
std::move(caps), |
| 52 |
options.fExecutor, |
| 53 |
options.fUserDefinedKnownRuntimeEffects)); |
| 54 |
} |
| 55 |
|
| 56 |
MtlSharedContext::MtlSharedContext(sk_cfp<id<MTLDevice>> device, |
| 57 |
sk_sp<skgpu::MtlMemoryAllocator> memoryAllocator, |
| 58 |
std::unique_ptr<const MtlCaps> caps, |
| 59 |
SkExecutor* executor, |
| 60 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects) |
| 61 |
: SharedContext(std::move(caps), |
| 62 |
BackendApi::kMetal, |
| 63 |
executor, |
| 64 |
userDefinedKnownRuntimeEffects) |
| 65 |
, fMemoryAllocator(std::move(memoryAllocator)) |
| 66 |
, fDevice(std::move(device)) { |
| 67 |
fThreadSafeResourceProvider = std::make_unique<MtlThreadSafeResourceProvider>( |
| 68 |
this->makeResourceProvider(&fSingleOwner, |
| 69 |
SK_InvalidGenID, |
| 70 |
kThreadedSafeResourceBudget)); |
| 71 |
|
| 72 |
static constexpr DepthStencilSettings kIgnoreDSS; |
| 73 |
|
| 74 |
for (const DepthStencilSettings& dss : { kDirectDepthLessPass, |
| 75 |
kDirectDepthLEqualPass, |
| 76 |
kWindingStencilPass, |
| 77 |
kEvenOddStencilPass, |
| 78 |
kRegularCoverPass, |
| 79 |
kInverseCoverPass, |
| 80 |
kIgnoreDSS }) { |
| 81 |
this->createCompatibleDepthStencilState(dss); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
MtlSharedContext::~MtlSharedContext() { |
| 86 |
fThreadSafeResourceProvider.reset(); |
| 87 |
|
| 88 |
// need to clear out resources before the allocator (if any) is removed |
| 89 |
this->globalCache()->deleteResources(); |
| 90 |
} |
| 91 |
|
| 92 |
MtlThreadSafeResourceProvider* MtlSharedContext::threadSafeResourceProvider() const { |
| 93 |
return static_cast<MtlThreadSafeResourceProvider*>(fThreadSafeResourceProvider.get()); |
| 94 |
} |
| 95 |
|
| 96 |
std::unique_ptr<ResourceProvider> MtlSharedContext::makeResourceProvider( |
| 97 |
SingleOwner* singleOwner, |
| 98 |
uint32_t recorderID, |
| 99 |
size_t resourceBudget) { |
| 100 |
return std::unique_ptr<ResourceProvider>(new MtlResourceProvider(this, |
| 101 |
singleOwner, |
| 102 |
recorderID, |
| 103 |
resourceBudget)); |
| 104 |
} |
| 105 |
|
| 106 |
namespace { |
| 107 |
|
| 108 |
MTLCompareFunction compare_op_to_mtl(CompareOp op) { |
| 109 |
switch (op) { |
| 110 |
case CompareOp::kAlways: |
| 111 |
return MTLCompareFunctionAlways; |
| 112 |
case CompareOp::kNever: |
| 113 |
return MTLCompareFunctionNever; |
| 114 |
case CompareOp::kGreater: |
| 115 |
return MTLCompareFunctionGreater; |
| 116 |
case CompareOp::kGEqual: |
| 117 |
return MTLCompareFunctionGreaterEqual; |
| 118 |
case CompareOp::kLess: |
| 119 |
return MTLCompareFunctionLess; |
| 120 |
case CompareOp::kLEqual: |
| 121 |
return MTLCompareFunctionLessEqual; |
| 122 |
case CompareOp::kEqual: |
| 123 |
return MTLCompareFunctionEqual; |
| 124 |
case CompareOp::kNotEqual: |
| 125 |
return MTLCompareFunctionNotEqual; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
MTLStencilOperation stencil_op_to_mtl(StencilOp op) { |
| 130 |
switch (op) { |
| 131 |
case StencilOp::kKeep: |
| 132 |
return MTLStencilOperationKeep; |
| 133 |
case StencilOp::kZero: |
| 134 |
return MTLStencilOperationZero; |
| 135 |
case StencilOp::kReplace: |
| 136 |
return MTLStencilOperationReplace; |
| 137 |
case StencilOp::kInvert: |
| 138 |
return MTLStencilOperationInvert; |
| 139 |
case StencilOp::kIncWrap: |
| 140 |
return MTLStencilOperationIncrementWrap; |
| 141 |
case StencilOp::kDecWrap: |
| 142 |
return MTLStencilOperationDecrementWrap; |
| 143 |
case StencilOp::kIncClamp: |
| 144 |
return MTLStencilOperationIncrementClamp; |
| 145 |
case StencilOp::kDecClamp: |
| 146 |
return MTLStencilOperationDecrementClamp; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
MTLStencilDescriptor* stencil_face_to_mtl(DepthStencilSettings::Face face) { |
| 151 |
MTLStencilDescriptor* result = [[MTLStencilDescriptor alloc] init]; |
| 152 |
result.stencilCompareFunction = compare_op_to_mtl(face.fCompareOp); |
| 153 |
result.readMask = face.fReadMask; |
| 154 |
result.writeMask = face.fWriteMask; |
| 155 |
result.depthStencilPassOperation = stencil_op_to_mtl(face.fDepthStencilPassOp); |
| 156 |
result.stencilFailureOperation = stencil_op_to_mtl(face.fStencilFailOp); |
| 157 |
return result; |
| 158 |
} |
| 159 |
|
| 160 |
} // anonymous namespace |
| 161 |
|
| 162 |
sk_cfp<id<MTLDepthStencilState>> MtlSharedContext::getCompatibleDepthStencilState( |
| 163 |
const DepthStencilSettings& depthStencilSettings) const { |
| 164 |
|
| 165 |
sk_cfp<id<MTLDepthStencilState>>* depthStencilState; |
| 166 |
depthStencilState = fDepthStencilStates.find(depthStencilSettings); |
| 167 |
|
| 168 |
// We've explicitly initialized fDepthStencilStates with all the common depth stencil settings |
| 169 |
// in the ctor - since there are so few of them. This frees us from concurrency concerns (i.e., |
| 170 |
// if we were to lazily create them and store them in a map). However, if a new one is |
| 171 |
// encountered we will need to either add its initialization to the ctor or reconsider this |
| 172 |
// approach. |
| 173 |
SkAssertResult(depthStencilState); |
| 174 |
return *depthStencilState; |
| 175 |
} |
| 176 |
|
| 177 |
void MtlSharedContext::createCompatibleDepthStencilState( |
| 178 |
const DepthStencilSettings& depthStencilSettings) { |
| 179 |
|
| 180 |
MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor alloc] init]; |
| 181 |
SkASSERT(depthStencilSettings.fDepthTestEnabled || |
| 182 |
depthStencilSettings.fDepthCompareOp == CompareOp::kAlways); |
| 183 |
desc.depthCompareFunction = compare_op_to_mtl(depthStencilSettings.fDepthCompareOp); |
| 184 |
if (depthStencilSettings.fDepthTestEnabled) { |
| 185 |
desc.depthWriteEnabled = depthStencilSettings.fDepthWriteEnabled; |
| 186 |
} |
| 187 |
if (depthStencilSettings.fStencilTestEnabled) { |
| 188 |
desc.frontFaceStencil = stencil_face_to_mtl(depthStencilSettings.fFrontStencil); |
| 189 |
desc.backFaceStencil = stencil_face_to_mtl(depthStencilSettings.fBackStencil); |
| 190 |
} |
| 191 |
|
| 192 |
sk_cfp<id<MTLDepthStencilState>> dss( |
| 193 |
[this->device() newDepthStencilStateWithDescriptor: desc]); |
| 194 |
|
| 195 |
fDepthStencilStates.set(depthStencilSettings, std::move(dss)); |
| 196 |
} |
| 197 |
|
| 198 |
sk_sp<GraphicsPipeline> MtlSharedContext::createGraphicsPipeline( |
| 199 |
const RuntimeEffectDictionary* runtimeDict, |
| 200 |
const UniqueKey& pipelineKey, |
| 201 |
const GraphicsPipelineDesc& pipelineDesc, |
| 202 |
const RenderPassDesc& renderPassDesc, |
| 203 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags, |
| 204 |
uint32_t compilationID) { |
| 205 |
return MtlGraphicsPipeline::Make(this, |
| 206 |
runtimeDict, pipelineKey, pipelineDesc, renderPassDesc, |
| 207 |
pipelineCreationFlags, compilationID); |
| 208 |
} |
| 209 |
|
| 210 |
} // namespace skgpu::graphite |