| 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 "include/core/SkStream.h" |
| 9 |
#include "include/gpu/ShaderErrorHandler.h" |
| 10 |
#include "include/gpu/graphite/Context.h" |
| 11 |
#include "include/gpu/graphite/mtl/MtlBackendContext.h" |
| 12 |
#include "src/core/SkTraceEvent.h" |
| 13 |
#include "src/gpu/graphite/ContextPriv.h" |
| 14 |
#include "src/gpu/graphite/TextureFormat.h" |
| 15 |
#include "src/gpu/graphite/mtl/MtlGraphiteUtils.h" |
| 16 |
#include "src/gpu/graphite/mtl/MtlQueueManager.h" |
| 17 |
#include "src/gpu/graphite/mtl/MtlSharedContext.h" |
| 18 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 19 |
|
| 20 |
#import <Metal/Metal.h> |
| 21 |
|
| 22 |
namespace skgpu::graphite { |
| 23 |
|
| 24 |
namespace ContextFactory { |
| 25 |
|
| 26 |
std::unique_ptr<Context> MakeMetal(const MtlBackendContext& backendContext, |
| 27 |
const ContextOptions& options) { |
| 28 |
sk_sp<SharedContext> sharedContext = MtlSharedContext::Make(backendContext, options); |
| 29 |
if (!sharedContext) { |
| 30 |
return nullptr; |
| 31 |
} |
| 32 |
|
| 33 |
sk_cfp<id<MTLCommandQueue>> queue = |
| 34 |
sk_ret_cfp((id<MTLCommandQueue>)(backendContext.fQueue.get())); |
| 35 |
auto queueManager = std::make_unique<MtlQueueManager>(std::move(queue), sharedContext.get()); |
| 36 |
if (!queueManager) { |
| 37 |
return nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
return ContextCtorAccessor::MakeContext(std::move(sharedContext), |
| 41 |
std::move(queueManager), |
| 42 |
options); |
| 43 |
} |
| 44 |
|
| 45 |
} // namespace ContextFactory |
| 46 |
|
| 47 |
sk_cfp<id<MTLLibrary>> MtlCompileShaderLibrary(const MtlSharedContext* sharedContext, |
| 48 |
std::string_view label, |
| 49 |
std::string_view msl, |
| 50 |
ShaderErrorHandler* errorHandler) { |
| 51 |
TRACE_EVENT0("skia.shaders", "driver_compile_shader"); |
| 52 |
NSString* nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(msl.data()) |
| 53 |
length:msl.size() |
| 54 |
encoding:NSUTF8StringEncoding |
| 55 |
freeWhenDone:NO]; |
| 56 |
if (!nsSource) { |
| 57 |
return nil; |
| 58 |
} |
| 59 |
MTLCompileOptions* options = [[MTLCompileOptions alloc] init]; |
| 60 |
|
| 61 |
// Framebuffer fetch is supported in MSL 2.3 in MacOS 11+. |
| 62 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 63 |
options.languageVersion = MTLLanguageVersion2_3; |
| 64 |
|
| 65 |
// array<> is supported in MSL 2.0 on MacOS 10.13+ and iOS 11+, |
| 66 |
// and in MSL 1.2 on iOS 10+ (but not MacOS). |
| 67 |
} else if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 68 |
options.languageVersion = MTLLanguageVersion2_0; |
| 69 |
#if defined(SK_BUILD_FOR_IOS) |
| 70 |
} else if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) { |
| 71 |
options.languageVersion = MTLLanguageVersion1_2; |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
NSError* error = nil; |
| 76 |
// TODO: do we need a version with a timeout? |
| 77 |
sk_cfp<id<MTLLibrary>> compiledLibrary( |
| 78 |
[sharedContext->device() newLibraryWithSource:(NSString* _Nonnull)nsSource |
| 79 |
options:options |
| 80 |
error:&error]); |
| 81 |
if (!compiledLibrary) { |
| 82 |
std::string mslStr(msl); |
| 83 |
errorHandler->compileError( |
| 84 |
mslStr.c_str(), error.debugDescription.UTF8String, /*shaderWasCached=*/false); |
| 85 |
return nil; |
| 86 |
} |
| 87 |
|
| 88 |
NSString* nsLabel = [[NSString alloc] initWithBytesNoCopy:const_cast<char*>(label.data()) |
| 89 |
length:label.size() |
| 90 |
encoding:NSUTF8StringEncoding |
| 91 |
freeWhenDone:NO]; |
| 92 |
compiledLibrary.get().label = nsLabel; |
| 93 |
return compiledLibrary; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
// Most formats in MTL_FORMAT_MAPPING for TextureFormat are always available for Graphite's |
| 99 |
// supported Mac and iOS versions. These formats are not. To simplify the mapping, these constants |
| 100 |
// are extracted from Frameworks/Metal/MTLPixelFormat.h to avoid guarding with an @available check, |
| 101 |
// although MtlCaps must still do so when enabling a particular format. |
| 102 |
|
| 103 |
#define MTL_PIXEL_FORMAT(name, value) \ |
| 104 |
static constexpr MTLPixelFormat name = static_cast<MTLPixelFormat>(value) |
| 105 |
|
| 106 |
// Either unsupported on iOS or high min iOS, so considered "mac-only" |
| 107 |
MTL_PIXEL_FORMAT(MTLPixelFormatBC1_RGBA_, 130); |
| 108 |
MTL_PIXEL_FORMAT(MTLPixelFormatBC1_RGBA_sRGB_, 131); |
| 109 |
MTL_PIXEL_FORMAT(MTLPixelFormatDepth24Unorm_Stencil8_, 255); |
| 110 |
// Requires Mac 11.0+, while we support 10.15+. All meet Graphite's minimum supported iOS version |
| 111 |
MTL_PIXEL_FORMAT(MTLPixelFormatB5G6R5Unorm_, 40); |
| 112 |
MTL_PIXEL_FORMAT(MTLPixelFormatABGR4Unorm_, 42); |
| 113 |
MTL_PIXEL_FORMAT(MTLPixelFormatBGR10_XR_, 554); |
| 114 |
MTL_PIXEL_FORMAT(MTLPixelFormatBGRA10_XR_, 552); |
| 115 |
MTL_PIXEL_FORMAT(MTLPixelFormatETC2_RGB8_, 180); |
| 116 |
MTL_PIXEL_FORMAT(MTLPixelFormatETC2_RGB8_sRGB_, 181); |
| 117 |
|
| 118 |
#if defined(SK_BUILD_FOR_MAC) |
| 119 |
static_assert(MTLPixelFormatBC1_RGBA_ == MTLPixelFormatBC1_RGBA); |
| 120 |
static_assert(MTLPixelFormatBC1_RGBA_sRGB_ == MTLPixelFormatBC1_RGBA_sRGB); |
| 121 |
static_assert(MTLPixelFormatDepth24Unorm_Stencil8_ == MTLPixelFormatDepth24Unorm_Stencil8); |
| 122 |
#endif |
| 123 |
|
| 124 |
static void validate_mtl_pixelformats() { |
| 125 |
if (@available(macOS 11.0, iOS 10.0, *)) { |
| 126 |
SkASSERT(MTLPixelFormatB5G6R5Unorm_ == MTLPixelFormatB5G6R5Unorm); |
| 127 |
SkASSERT(MTLPixelFormatABGR4Unorm_ == MTLPixelFormatABGR4Unorm); |
| 128 |
SkASSERT(MTLPixelFormatBGR10_XR_ == MTLPixelFormatBGR10_XR); |
| 129 |
SkASSERT(MTLPixelFormatBGRA10_XR_ == MTLPixelFormatBGRA10_XR); |
| 130 |
SkASSERT(MTLPixelFormatETC2_RGB8_ == MTLPixelFormatETC2_RGB8); |
| 131 |
SkASSERT(MTLPixelFormatETC2_RGB8_sRGB_ == MTLPixelFormatETC2_RGB8_sRGB); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#define MTL_FORMAT_MAPPING(M) \ |
| 136 |
M(TextureFormat::kR8, MTLPixelFormatR8Unorm) \ |
| 137 |
M(TextureFormat::kR16, MTLPixelFormatR16Unorm) \ |
| 138 |
M(TextureFormat::kR16F, MTLPixelFormatR16Float) \ |
| 139 |
M(TextureFormat::kR32F, MTLPixelFormatR32Float) \ |
| 140 |
M(TextureFormat::kA8, MTLPixelFormatA8Unorm) \ |
| 141 |
M(TextureFormat::kRG8, MTLPixelFormatRG8Unorm) \ |
| 142 |
M(TextureFormat::kRG16, MTLPixelFormatRG16Unorm) \ |
| 143 |
M(TextureFormat::kRG16F, MTLPixelFormatRG16Float) \ |
| 144 |
M(TextureFormat::kRG32F, MTLPixelFormatRG32Float) \ |
| 145 |
/*TextureFormat::kRGB8, unsupported */ \ |
| 146 |
/*TextureFormat::kBGR8, unsupported */ \ |
| 147 |
M(TextureFormat::kB5_G6_R5, MTLPixelFormatB5G6R5Unorm_) \ |
| 148 |
/*TextureFormat::kR5_G6_B5, unsupported */ \ |
| 149 |
/*TextureFormat::kRGB16, unsupported */ \ |
| 150 |
/*TextureFormat::kRGB16F, unsupported */ \ |
| 151 |
/*TextureFormat::kRGB32F, unsupported */ \ |
| 152 |
/*TextureFormat::kRGB8_sRGB, unsupported */ \ |
| 153 |
M(TextureFormat::kBGR10_XR, MTLPixelFormatBGR10_XR_) \ |
| 154 |
M(TextureFormat::kRGBA8, MTLPixelFormatRGBA8Unorm) \ |
| 155 |
M(TextureFormat::kRGBA16, MTLPixelFormatRGBA16Unorm) \ |
| 156 |
M(TextureFormat::kRGBA16F, MTLPixelFormatRGBA16Float) \ |
| 157 |
M(TextureFormat::kRGBA32F, MTLPixelFormatRGBA32Float) \ |
| 158 |
M(TextureFormat::kRGB10_A2, MTLPixelFormatRGB10A2Unorm) \ |
| 159 |
/*TextureFormat::kRGBA10x6, unsupported */ \ |
| 160 |
M(TextureFormat::kRGBA8_sRGB, MTLPixelFormatRGBA8Unorm_sRGB) \ |
| 161 |
M(TextureFormat::kBGRA8, MTLPixelFormatBGRA8Unorm) \ |
| 162 |
M(TextureFormat::kBGR10_A2, MTLPixelFormatBGR10A2Unorm) \ |
| 163 |
M(TextureFormat::kBGRA8_sRGB, MTLPixelFormatBGRA8Unorm_sRGB) \ |
| 164 |
M(TextureFormat::kABGR4, MTLPixelFormatABGR4Unorm_) \ |
| 165 |
/*TextureFormat::kARGB4, unsupported */ \ |
| 166 |
M(TextureFormat::kBGRA10x6_XR, MTLPixelFormatBGRA10_XR_) \ |
| 167 |
M(TextureFormat::kRGB8_ETC2, MTLPixelFormatETC2_RGB8_) \ |
| 168 |
M(TextureFormat::kRGB8_ETC2_sRGB, MTLPixelFormatETC2_RGB8_sRGB_) \ |
| 169 |
/*TextureFormat::kRGB8_BC1, unsupported */ \ |
| 170 |
M(TextureFormat::kRGBA8_BC1, MTLPixelFormatBC1_RGBA_) \ |
| 171 |
M(TextureFormat::kRGBA8_BC1_sRGB, MTLPixelFormatBC1_RGBA_sRGB_) \ |
| 172 |
/*TextureFormat::kYUV8_P2_420, unsupported */ \ |
| 173 |
/*TextureFormat::kYUV8_P3_420, unsupported */ \ |
| 174 |
/*TextureFormat::kYUV10x6_P2_420, unsupported */ \ |
| 175 |
/*TextureFormat::kExternal, unsupported */ \ |
| 176 |
M(TextureFormat::kS8, MTLPixelFormatStencil8) \ |
| 177 |
M(TextureFormat::kD16, MTLPixelFormatDepth16Unorm) \ |
| 178 |
M(TextureFormat::kD32F, MTLPixelFormatDepth32Float) \ |
| 179 |
M(TextureFormat::kD24_S8, MTLPixelFormatDepth24Unorm_Stencil8_) \ |
| 180 |
M(TextureFormat::kD32F_S8, MTLPixelFormatDepth32Float_Stencil8) |
| 181 |
|
| 182 |
TextureFormat MTLPixelFormatToTextureFormat(MTLPixelFormat format) { |
| 183 |
#define M(TF, MTL) case MTL: return TF; |
| 184 |
switch(format) { |
| 185 |
MTL_FORMAT_MAPPING(M) |
| 186 |
default: return TextureFormat::kUnsupported; |
| 187 |
} |
| 188 |
#undef M |
| 189 |
} |
| 190 |
MTLPixelFormat TextureFormatToMTLPixelFormat(TextureFormat format) { |
| 191 |
// Validate constants that can't be statically validated due to @available |
| 192 |
validate_mtl_pixelformats(); |
| 193 |
|
| 194 |
#define M(TF, MTL) case TF: return MTL; |
| 195 |
switch(format) { |
| 196 |
MTL_FORMAT_MAPPING(M) |
| 197 |
default: return MTLPixelFormatInvalid; |
| 198 |
} |
| 199 |
#undef M |
| 200 |
} |
| 201 |
|
| 202 |
} // namespace skgpu::graphite |