Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MtlGraphiteUtils.mm
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
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
10
18
19namespace skgpu::graphite {
20
21namespace ContextFactory {
22
23std::unique_ptr<Context> MakeMetal(const MtlBackendContext& backendContext,
24 const ContextOptions& options) {
25 sk_sp<SharedContext> sharedContext = MtlSharedContext::Make(backendContext, options);
26 if (!sharedContext) {
27 return nullptr;
28 }
29
30 sk_cfp<id<MTLCommandQueue>> queue =
31 sk_ret_cfp((id<MTLCommandQueue>)(backendContext.fQueue.get()));
32 auto queueManager = std::make_unique<MtlQueueManager>(std::move(queue), sharedContext.get());
33 if (!queueManager) {
34 return nullptr;
35 }
36
37 return ContextCtorAccessor::MakeContext(std::move(sharedContext),
38 std::move(queueManager),
39 options);
40}
41
42} // namespace ContextFactory
43
45 // TODO: Decide if we want to change this to always return a combined depth and stencil format
46 // to allow more sharing of depth stencil allocations.
47 if (mask == DepthStencilFlags::kDepth) {
48 // MTLPixelFormatDepth16Unorm is also a universally supported option here
49 return MTLPixelFormatDepth32Float;
50 } else if (mask == DepthStencilFlags::kStencil) {
51 return MTLPixelFormatStencil8;
52 } else if (mask == DepthStencilFlags::kDepthStencil) {
53 // MTLPixelFormatDepth24Unorm_Stencil8 is supported on Mac family GPUs.
54 return MTLPixelFormatDepth32Float_Stencil8;
55 }
56 SkASSERT(false);
57 return MTLPixelFormatInvalid;
58}
59
61 switch (format) {
62 case MTLPixelFormatDepth32Float: return DepthStencilFlags::kDepth;
63 case MTLPixelFormatStencil8: return DepthStencilFlags::kStencil;
64 case MTLPixelFormatDepth32Float_Stencil8: return DepthStencilFlags::kDepthStencil;
65 default: return DepthStencilFlags::kNone;
66 }
67
69}
70
71sk_cfp<id<MTLLibrary>> MtlCompileShaderLibrary(const MtlSharedContext* sharedContext,
72 std::string_view label,
73 std::string_view msl,
74 ShaderErrorHandler* errorHandler) {
75 TRACE_EVENT0("skia.shaders", "driver_compile_shader");
76 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.data())
77 length:msl.size()
78 encoding:NSUTF8StringEncoding
79 freeWhenDone:NO];
80 if (!nsSource) {
81 return nil;
82 }
83 MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
84
85 // Framebuffer fetch is supported in MSL 2.3 in MacOS 11+.
86 if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
87 options.languageVersion = MTLLanguageVersion2_3;
88
89 // array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+,
90 // and in MSL 1.2 on iOS 10+ (but not MacOS).
91 } else if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
92 options.languageVersion = MTLLanguageVersion2_0;
93#if defined(SK_BUILD_FOR_IOS)
94 } else if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
95 options.languageVersion = MTLLanguageVersion1_2;
96#endif
97 }
98
99 NSError* error = nil;
100 // TODO: do we need a version with a timeout?
101 sk_cfp<id<MTLLibrary>> compiledLibrary(
102 [sharedContext->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
104 error:&error]);
105 if (!compiledLibrary) {
106 std::string mslStr(msl);
107 errorHandler->compileError(
108 mslStr.c_str(), error.debugDescription.UTF8String, /*shaderWasCached=*/false);
109 return nil;
110 }
111
112 NSString* nsLabel = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(label.data())
113 length:label.size()
114 encoding:NSUTF8StringEncoding
115 freeWhenDone:NO];
116 compiledLibrary.get().label = nsLabel;
117 return compiledLibrary;
118}
119
123
127
128} // namespace skgpu::graphite
const char * options
#define SkUNREACHABLE
Definition SkAssert.h:135
#define SkASSERT(cond)
Definition SkAssert.h:116
T * get() const
Definition SkRefCnt.h:303
virtual void compileError(const char *shader, const char *errors)
static std::unique_ptr< Context > MakeContext(sk_sp< SharedContext >, std::unique_ptr< QueueManager >, const ContextOptions &)
Definition Context.cpp:926
static sk_sp< SharedContext > Make(const MtlBackendContext &, const ContextOptions &)
VkQueue queue
Definition main.cc:55
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
size_t length
SK_API std::unique_ptr< Context > MakeMetal(const MtlBackendContext &, const ContextOptions &)
unsigned int MtlPixelFormat
SkEnumBitMask< DepthStencilFlags > MtlFormatToDepthStencilFlags(MTLPixelFormat format)
MTLPixelFormat MtlDepthStencilFlagsToFormat(SkEnumBitMask< DepthStencilFlags > mask)
size_t MtlFormatBytesPerBlock(MtlPixelFormat format)
SkTextureCompressionType MtlFormatToCompressionType(MtlPixelFormat format)
sk_cfp< id< MTLLibrary > > MtlCompileShaderLibrary(const MtlSharedContext *sharedContext, std::string_view label, std::string_view msl, ShaderErrorHandler *errorHandler)
SkTextureCompressionType MtlFormatToCompressionType(MTLPixelFormat mtlFormat)
Definition MtlUtils.mm:148
size_t MtlFormatBytesPerBlock(MTLPixelFormat mtlFormat)
Definition MtlUtils.mm:119
#define TRACE_EVENT0(category_group, name)