Debian Codegraph @

src/gpu/ganesh/mtl/GrMtlUtil.mm

raw
1 /*
2 * Copyright 2017 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
8 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
9
10 #import <Metal/Metal.h>
11
12 #include "include/core/SkTextureCompressionType.h"
13 #include "include/gpu/ganesh/GrBackendSurface.h"
14 #include "include/private/base/SkMutex.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/core/SkTraceEvent.h"
17 #include "src/gpu/ganesh/GrSurface.h"
18 #include "src/gpu/ganesh/mtl/GrMtlGpu.h"
19 #include "src/gpu/ganesh/mtl/GrMtlRenderTarget.h"
20 #include "src/gpu/ganesh/mtl/GrMtlTexture.h"
21
22 #if !__has_feature(objc_arc)
23 #error This file must be compiled with Arc. Use -fobjc-arc flag
24 #endif
25
26 GR_NORETAIN_BEGIN
27
28 NSError* GrCreateMtlError(NSString* description, GrMtlErrorCode errorCode) {
29 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:description
30 forKey:NSLocalizedDescriptionKey];
31 return [NSError errorWithDomain:@"org.skia.ganesh"
32 code:(NSInteger)errorCode
33 userInfo:userInfo];
34 }
35
36 MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture) {
37 MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init];
38 texDesc.textureType = mtlTexture.textureType;
39 texDesc.pixelFormat = mtlTexture.pixelFormat;
40 texDesc.width = mtlTexture.width;
41 texDesc.height = mtlTexture.height;
42 texDesc.depth = mtlTexture.depth;
43 texDesc.mipmapLevelCount = mtlTexture.mipmapLevelCount;
44 texDesc.arrayLength = mtlTexture.arrayLength;
45 texDesc.sampleCount = mtlTexture.sampleCount;
46 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
47 texDesc.usage = mtlTexture.usage;
48 }
49 return texDesc;
50 }
51
52 id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu,
53 const SkSL::NativeShader& msl,
54 GrContextOptions::ShaderErrorHandler* errorHandler) {
55 TRACE_EVENT0("skia.shaders", "driver_compile_shader");
56 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.fText.c_str())
57 length:msl.fText.size()
58 encoding:NSUTF8StringEncoding
59 freeWhenDone:NO];
60 if (!nsSource) {
61 return nil;
62 }
63 MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
64 // array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+,
65 // and in MSL 1.2 on iOS 10+ (but not MacOS).
66 options.languageVersion = MTLLanguageVersion2_0;
67
68 #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 150000) || (TARGET_OS_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED >= 180000)
69 options.mathMode = MTLMathModeFast;
70 #else
71 options.fastMathEnabled = YES;
72 #endif
73
74 NSError* error = nil;
75 id<MTLLibrary> compiledLibrary;
76 if (@available(macOS 10.15, *)) {
77 compiledLibrary = [gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
78 options:options
79 error:&error];
80 } else {
81 compiledLibrary = GrMtlNewLibraryWithSource(gpu->device(), (NSString* _Nonnull)nsSource,
82 options, &error);
83 }
84 if (!compiledLibrary) {
85 errorHandler->compileError(
86 msl.fText.c_str(), error.debugDescription.UTF8String, /*shaderWasCached=*/false);
87 return nil;
88 }
89
90 return compiledLibrary;
91 }
92
93 void GrPrecompileMtlShaderLibrary(const GrMtlGpu* gpu, const SkSL::NativeShader& msl) {
94 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.fText.c_str())
95 length:msl.fText.size()
96 encoding:NSUTF8StringEncoding
97 freeWhenDone:NO];
98 if (!nsSource) {
99 return;
100 }
101 // Do nothing after completion for now.
102 // TODO: cache the result somewhere so we can use it later.
103 MTLNewLibraryCompletionHandler completionHandler = ^(id<MTLLibrary> library, NSError* error) {};
104 [gpu->device() newLibraryWithSource:(NSString* _Nonnull)nsSource
105 options:nil
106 completionHandler:completionHandler];
107 }
108
109 // Wrapper to get atomic assignment for compiles and pipeline creation
110 class MtlCompileResult : public SkRefCnt {
111 public:
112 MtlCompileResult() : fCompiledObject(nil), fError(nil) {}
113 void set(id compiledObject, NSError* error) {
114 SkAutoMutexExclusive automutex(fMutex);
115 fCompiledObject = compiledObject;
116 fError = error;
117 }
118 std::pair<id, NSError*> get() {
119 SkAutoMutexExclusive automutex(fMutex);
120 return std::make_pair(fCompiledObject, fError);
121 }
122 private:
123 SkMutex fMutex;
124 id fCompiledObject SK_GUARDED_BY(fMutex);
125 NSError* fError SK_GUARDED_BY(fMutex);
126 };
127
128 id<MTLLibrary> GrMtlNewLibraryWithSource(id<MTLDevice> device, NSString* mslCode,
129 MTLCompileOptions* options, NSError** error) {
130 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
131 sk_sp<MtlCompileResult> compileResult(new MtlCompileResult);
132 // We have to increment the ref for the Obj-C block manually because it won't do it for us
133 compileResult->ref();
134 MTLNewLibraryCompletionHandler completionHandler =
135 ^(id<MTLLibrary> library, NSError* compileError) {
136 compileResult->set(library, compileError);
137 dispatch_semaphore_signal(semaphore);
138 compileResult->unref();
139 };
140
141 [device newLibraryWithSource: mslCode
142 options: options
143 completionHandler: completionHandler];
144
145 // Wait 1 second for the compiler
146 constexpr auto kTimeoutNS = 1000000000UL;
147 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
148 if (error) {
149 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
150 NSString* description =
151 [NSString stringWithFormat:@"Compilation took longer than %lu ms",
152 kTimeoutMS];
153 *error = GrCreateMtlError(description, GrMtlErrorCode::kTimeout);
154 }
155 return nil;
156 }
157
158 id<MTLLibrary> compiledLibrary;
159 std::tie(compiledLibrary, *error) = compileResult->get();
160
161 return compiledLibrary;
162 }
163
164 id<MTLRenderPipelineState> GrMtlNewRenderPipelineStateWithDescriptor(
165 id<MTLDevice> device, MTLRenderPipelineDescriptor* pipelineDescriptor, NSError** error) {
166 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
167 sk_sp<MtlCompileResult> compileResult(new MtlCompileResult);
168 // We have to increment the ref for the Obj-C block manually because it won't do it for us
169 compileResult->ref();
170 MTLNewRenderPipelineStateCompletionHandler completionHandler =
171 ^(id<MTLRenderPipelineState> state, NSError* compileError) {
172 compileResult->set(state, compileError);
173 dispatch_semaphore_signal(semaphore);
174 compileResult->unref();
175 };
176
177 [device newRenderPipelineStateWithDescriptor: pipelineDescriptor
178 completionHandler: completionHandler];
179
180 // Wait 1 second for pipeline creation
181 constexpr auto kTimeoutNS = 1000000000UL;
182 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
183 if (error) {
184 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
185 NSString* description =
186 [NSString stringWithFormat:@"Pipeline creation took longer than %lu ms",
187 kTimeoutMS];
188 *error = GrCreateMtlError(description, GrMtlErrorCode::kTimeout);
189 }
190 return nil;
191 }
192
193 id<MTLRenderPipelineState> pipelineState;
194 std::tie(pipelineState, *error) = compileResult->get();
195
196 return pipelineState;
197 }
198
199 id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface) {
200 id<MTLTexture> mtlTexture = nil;
201
202 GrMtlRenderTarget* renderTarget = static_cast<GrMtlRenderTarget*>(surface->asRenderTarget());
203 GrMtlTexture* texture;
204 if (renderTarget) {
205 // We should not be using this for multisampled rendertargets with a separate resolve
206 // texture.
207 if (renderTarget->resolveAttachment()) {
208 SkASSERT(renderTarget->numSamples() > 1);
209 SkASSERT(false);
210 return nil;
211 }
212 mtlTexture = renderTarget->colorMTLTexture();
213 } else {
214 texture = static_cast<GrMtlTexture*>(surface->asTexture());
215 if (texture) {
216 mtlTexture = texture->mtlTexture();
217 }
218 }
219 return mtlTexture;
220 }
221
222
223 //////////////////////////////////////////////////////////////////////////////
224 // CPP Utils
225
226 GrMTLPixelFormat GrGetMTLPixelFormatFromMtlTextureInfo(const GrMtlTextureInfo& info) {
227 id<MTLTexture> GR_NORETAIN mtlTexture = GrGetMTLTexture(info.fTexture.get());
228 return static_cast<GrMTLPixelFormat>(mtlTexture.pixelFormat);
229 }
230
231 GrColorFormatDesc GrMtlFormatDesc(MTLPixelFormat mtlFormat) {
232 switch (mtlFormat) {
233 case MTLPixelFormatRGBA8Unorm:
234 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
235 case MTLPixelFormatR8Unorm:
236 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
237 case MTLPixelFormatA8Unorm:
238 return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
239 case MTLPixelFormatBGRA8Unorm:
240 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
241 case MTLPixelFormatB5G6R5Unorm:
242 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
243 case MTLPixelFormatRGBA16Float:
244 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
245 case MTLPixelFormatR16Float:
246 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
247 case MTLPixelFormatRG8Unorm:
248 return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
249 case MTLPixelFormatRGB10A2Unorm:
250 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
251 case MTLPixelFormatBGR10A2Unorm:
252 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
253 case MTLPixelFormatABGR4Unorm:
254 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
255 case MTLPixelFormatRGBA8Unorm_sRGB:
256 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
257 case MTLPixelFormatR16Unorm:
258 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
259 case MTLPixelFormatRG16Unorm:
260 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
261 case MTLPixelFormatRGBA16Unorm:
262 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
263 case MTLPixelFormatRG16Float:
264 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
265
266 // Compressed texture formats are not expected to have a description.
267 case MTLPixelFormatETC2_RGB8: return GrColorFormatDesc::MakeInvalid();
268 #ifdef SK_BUILD_FOR_MAC
269 case MTLPixelFormatBC1_RGBA: return GrColorFormatDesc::MakeInvalid();
270 #endif
271
272 // This type only describes color channels.
273 case MTLPixelFormatStencil8: return GrColorFormatDesc::MakeInvalid();
274
275 default:
276 return GrColorFormatDesc::MakeInvalid();
277 }
278 }
279
280 SkTextureCompressionType GrMtlFormatToCompressionType(MTLPixelFormat format) {
281 switch (format) {
282 case MTLPixelFormatETC2_RGB8: return SkTextureCompressionType::kETC2_RGB8_UNORM;
283 #ifdef SK_BUILD_FOR_MAC
284 case MTLPixelFormatBC1_RGBA: return SkTextureCompressionType::kBC1_RGBA8_UNORM;
285 #endif
286 default: return SkTextureCompressionType::kNone;
287 }
288
289 SkUNREACHABLE;
290 }
291
292 int GrMtlTextureInfoSampleCount(const GrMtlTextureInfo& info) {
293 id<MTLTexture> texture = GrGetMTLTexture(info.fTexture.get());
294 if (!texture) {
295 return 0;
296 }
297 return texture.sampleCount;
298 }
299
300 int GrMtlFormatStencilBits(MTLPixelFormat format) {
301 switch (format) {
302 case MTLPixelFormatStencil8:
303 return 8;
304 default:
305 return 0;
306 }
307 }
308
309 #if defined(SK_DEBUG) || defined(GPU_TEST_UTILS)
310 bool GrMtlFormatIsBGRA8(GrMTLPixelFormat mtlFormat) {
311 return mtlFormat == MTLPixelFormatBGRA8Unorm;
312 }
313 #endif
314
315 GR_NORETAIN_END