10#import <Metal/Metal.h>
22#if !__has_feature(objc_arc)
23#error This file must be compiled with Arc. Use -fobjc-arc flag
29 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:description
30 forKey:NSLocalizedDescriptionKey];
31 return [NSError errorWithDomain:
@"org.skia.ganesh"
32 code:(NSInteger)errorCode
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;
53 const std::string& msl,
56 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:
const_cast<char*
>(msl.c_str())
58 encoding:NSUTF8StringEncoding
63 MTLCompileOptions*
options = [[MTLCompileOptions alloc]
init];
66 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
67 options.languageVersion = MTLLanguageVersion2_0;
68#if defined(SK_BUILD_FOR_IOS)
69 }
else if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
70 options.languageVersion = MTLLanguageVersion1_2;
76 id<MTLLibrary> compiledLibrary;
77 if (@available(macOS 10.15, *)) {
78 compiledLibrary = [gpu->
device() newLibraryWithSource:(NSString* _Nonnull)nsSource
85 if (!compiledLibrary) {
87 msl.c_str(),
error.debugDescription.UTF8String,
false);
91 return compiledLibrary;
95 const std::string& msl) {
96 NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:
const_cast<char*
>(msl.c_str())
98 encoding:NSUTF8StringEncoding
105 MTLNewLibraryCompletionHandler completionHandler = ^(id<MTLLibrary> library, NSError*
error) {};
106 [gpu->
device() newLibraryWithSource:(NSString* _Nonnull)nsSource
108 completionHandler:completionHandler];
117 fCompiledObject = compiledObject;
120 std::pair<id, NSError*>
get() {
122 return std::make_pair(fCompiledObject, fError);
126 id fCompiledObject SK_GUARDED_BY(fMutex);
127 NSError* fError SK_GUARDED_BY(fMutex);
132 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
135 compileResult->ref();
136 MTLNewLibraryCompletionHandler completionHandler =
137 ^(id<MTLLibrary> library, NSError* compileError) {
138 compileResult->set(library, compileError);
139 dispatch_semaphore_signal(semaphore);
140 compileResult->unref();
143 [
device newLibraryWithSource: mslCode
145 completionHandler: completionHandler];
148 constexpr auto kTimeoutNS = 1000000000UL;
149 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
151 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
152 NSString* description =
153 [NSString stringWithFormat:
@"Compilation took longer than %lu ms",
160 id<MTLLibrary> compiledLibrary;
161 std::tie(compiledLibrary, *
error) = compileResult->
get();
163 return compiledLibrary;
167 id<MTLDevice>
device, MTLRenderPipelineDescriptor* pipelineDescriptor, NSError**
error) {
168 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
171 compileResult->ref();
172 MTLNewRenderPipelineStateCompletionHandler completionHandler =
173 ^(id<MTLRenderPipelineState>
state, NSError* compileError) {
174 compileResult->set(
state, compileError);
175 dispatch_semaphore_signal(semaphore);
176 compileResult->unref();
179 [
device newRenderPipelineStateWithDescriptor: pipelineDescriptor
180 completionHandler: completionHandler];
183 constexpr auto kTimeoutNS = 1000000000UL;
184 if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, kTimeoutNS))) {
186 constexpr auto kTimeoutMS = kTimeoutNS/1000000UL;
187 NSString* description =
188 [NSString stringWithFormat:
@"Pipeline creation took longer than %lu ms",
195 id<MTLRenderPipelineState> pipelineState;
196 std::tie(pipelineState, *
error) = compileResult->
get();
198 return pipelineState;
202 id<MTLTexture> mtlTexture = nil;
218 mtlTexture =
texture->mtlTexture();
235 case MTLPixelFormatRGBA8Unorm:
237 case MTLPixelFormatR8Unorm:
239 case MTLPixelFormatA8Unorm:
241 case MTLPixelFormatBGRA8Unorm:
243 case MTLPixelFormatB5G6R5Unorm:
245 case MTLPixelFormatRGBA16Float:
247 case MTLPixelFormatR16Float:
249 case MTLPixelFormatRG8Unorm:
251 case MTLPixelFormatRGB10A2Unorm:
253 case MTLPixelFormatBGR10A2Unorm:
255 case MTLPixelFormatABGR4Unorm:
257 case MTLPixelFormatRGBA8Unorm_sRGB:
259 case MTLPixelFormatR16Unorm:
261 case MTLPixelFormatRG16Unorm:
263 case MTLPixelFormatRGBA16Unorm:
265 case MTLPixelFormatRG16Float:
270#ifdef SK_BUILD_FOR_MAC
285#ifdef SK_BUILD_FOR_MAC
304 case MTLPixelFormatStencil8:
311#if defined(SK_DEBUG) || defined(GR_TEST_UTILS)
313 return mtlFormat == MTLPixelFormatBGRA8Unorm;
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
unsigned int GrMTLPixelFormat
GR_NORETAIN_BEGIN SK_ALWAYS_INLINE id< MTLTexture > GrGetMTLTexture(const void *mtlTexture)
int GrMtlFormatStencilBits(MTLPixelFormat format)
MTLTextureDescriptor * GrGetMTLTextureDescriptor(id< MTLTexture > mtlTexture)
GR_NORETAIN_BEGIN NSError * GrCreateMtlError(NSString *description, GrMtlErrorCode errorCode)
void GrPrecompileMtlShaderLibrary(const GrMtlGpu *gpu, const std::string &msl)
id< MTLRenderPipelineState > GrMtlNewRenderPipelineStateWithDescriptor(id< MTLDevice > device, MTLRenderPipelineDescriptor *pipelineDescriptor, NSError **error)
SkTextureCompressionType GrMtlFormatToCompressionType(MTLPixelFormat format)
GrMTLPixelFormat GrGetMTLPixelFormatFromMtlTextureInfo(const GrMtlTextureInfo &info)
id< MTLLibrary > GrMtlNewLibraryWithSource(id< MTLDevice > device, NSString *mslCode, MTLCompileOptions *options, NSError **error)
int GrMtlTextureInfoSampleCount(const GrMtlTextureInfo &info)
GrColorFormatDesc GrMtlFormatDesc(MTLPixelFormat mtlFormat)
id< MTLTexture > GrGetMTLTextureFromSurface(GrSurface *surface)
id< MTLLibrary > GrCompileMtlShaderLibrary(const GrMtlGpu *gpu, const std::string &msl, GrContextOptions::ShaderErrorHandler *errorHandler)
id< MTLDevice > device() const
id< MTLTexture > colorMTLTexture() const
GrMtlAttachment * resolveAttachment() const
void set(id compiledObject, NSError *error)
std::pair< id, NSError * > get()
virtual void compileError(const char *shader, const char *errors)
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
#define TRACE_EVENT0(category_group, name)