-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetal+Extensions.swift
629 lines (559 loc) · 20 KB
/
Metal+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import CoreVideo
import Metal
import MetalPerformanceShaders
class MTLContext {
static let shared = MTLContext()
let device = MTLCreateSystemDefaultDevice()!
lazy var library = device.makeDefaultLibrary()!
lazy var queue = device.makeCommandQueue()!
func makeComputePipelineState(functionName: String) throws -> MTLComputePipelineState {
guard let function = library.makeFunction(name: functionName) else {
throw Error.noFunction(name: functionName)
}
return try device.makeComputePipelineState(function: function)
}
func makeRenderPipelineState(
pixelFormat: MTLPixelFormat,
vertexFunction: String,
fragmentFunction: String
) throws -> MTLRenderPipelineState {
let descriptor = MTLRenderPipelineDescriptor(
vertexFunction: vertexFunction,
fragmentFunction: fragmentFunction,
pixelFormat: pixelFormat,
library: library
)
return try device.makeRenderPipelineState(descriptor: descriptor)
}
func makeRenderPipelineState(
pixelFormat: MTLPixelFormat,
fragmentFunction: String
) throws -> MTLRenderPipelineState {
let descriptor = MTLRenderPipelineDescriptor(
vertexFunction: "passthroughVertexFunction",
fragmentFunction: fragmentFunction,
pixelFormat: pixelFormat,
library: library
)
return try device.makeRenderPipelineState(descriptor: descriptor)
}
@discardableResult
func schedule(_ work: (_ commandBuffer: MTLCommandBuffer) -> Void) -> MTLCommandBuffer? {
guard let commandBuffer = queue.makeCommandBuffer() else {
print(Self.self, #function, "no command buffer")
return nil
}
work(commandBuffer)
commandBuffer.commit()
return commandBuffer
}
@discardableResult
func scheduleAndWait<T>(_ work: (_ commandBuffer: MTLCommandBuffer) -> T?) -> T? {
guard let commandBuffer = queue.makeCommandBuffer() else {
print(Self.self, #function, "no command buffer")
return nil
}
let result = work(commandBuffer)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
return result
}
}
extension MTLContext {
enum Error: Swift.Error {
case noFunction(name: String)
}
}
extension MTLRenderPipelineDescriptor {
convenience init(
vertexFunction: String,
fragmentFunction: String,
pixelFormat: MTLPixelFormat,
library: MTLLibrary
) {
self.init()
colorAttachments[0].pixelFormat = pixelFormat
colorAttachments[0].isBlendingEnabled = true
colorAttachments[0].rgbBlendOperation = .add
colorAttachments[0].alphaBlendOperation = .add
colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
self.vertexFunction = library.makeFunction(name: vertexFunction)!
self.fragmentFunction = library.makeFunction(name: fragmentFunction)!
}
}
public extension MTLTexture {
var cgSize: CGSize {
CGSize(width: width, height: height)
}
var size: MTLSize {
MTLSize(width: width, height: height, depth: depth)
}
var descriptor: MTLTextureDescriptor {
let output = MTLTextureDescriptor()
output.width = width
output.height = height
output.depth = depth
output.arrayLength = arrayLength
output.storageMode = storageMode
output.cpuCacheMode = cpuCacheMode
output.usage = usage
output.textureType = textureType
output.sampleCount = sampleCount
output.mipmapLevelCount = mipmapLevelCount
output.pixelFormat = pixelFormat
output.allowGPUOptimizedContents = allowGPUOptimizedContents
return output
}
var temporaryTextureDescriptor: MTLTextureDescriptor {
let descriptor = descriptor
descriptor.storageMode = .private
return descriptor
}
func allChannelsSwizzled(as swizzle: MTLTextureSwizzle) -> MTLTexture? {
makeTextureView(
pixelFormat: pixelFormat,
textureType: textureType,
levels: 0 ..< 1,
slices: 0 ..< 1,
swizzle: MTLTextureSwizzleChannels(
red: swizzle, green: swizzle, blue: swizzle, alpha: swizzle
)
)
}
}
public extension MTLBlitCommandEncoder {
func fillWith0(_ buffer: MTLBuffer) {
fill(buffer: buffer, range: 0 ..< buffer.length, value: 0)
}
}
public extension MTLRenderCommandEncoder {
func set<T>(vertexValue: inout T, index: Int) {
setVertexBytes(&vertexValue, length: MemoryLayout<T>.stride, index: index)
}
func set<T>(fragmentValue: inout T, index: Int) {
setFragmentBytes(&fragmentValue, length: MemoryLayout<T>.stride, index: index)
}
}
public extension MTLTextureDescriptor {
static func texture2DDescriptor(
pixelFormat: MTLPixelFormat,
size: CGSize,
usage: MTLTextureUsage = [.shaderRead, .shaderWrite, .renderTarget]
) -> MTLTextureDescriptor {
let descriptor = MTLTextureDescriptor.texture2DDescriptor(
pixelFormat: pixelFormat,
width: Int(size.width),
height: Int(size.height),
mipmapped: false
)
descriptor.usage = usage
return descriptor
}
}
public extension MPSTemporaryImage {
convenience init(
commandBuffer: MTLCommandBuffer,
pixelFormat: MTLPixelFormat = .bgra8Unorm,
size: CGSize
) {
let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: pixelFormat, size: size)
descriptor.storageMode = .private
self.init(commandBuffer: commandBuffer, textureDescriptor: descriptor)
}
}
public extension MTLComputeCommandEncoder {
func set<T>(value: inout T, index: Int) {
setBytes(&value, length: MemoryLayout<T>.stride, index: index)
}
func dispatch2d(state: MTLComputePipelineState, size: MTLSize) {
if device.supports(feature: .nonUniformThreadgroups) {
dispatch2d(state: state, exactly: size)
} else {
dispatch2d(state: state, covering: size)
}
}
func dispatch2d(
state: MTLComputePipelineState,
covering size: MTLSize,
threadgroupSize: MTLSize? = nil
) {
let tgSize = threadgroupSize ?? state.max2dThreadgroupSize
let count = MTLSize(
width: (size.width + tgSize.width - 1) / tgSize.width,
height: (size.height + tgSize.height - 1) / tgSize.height,
depth: 1
)
setComputePipelineState(state)
dispatchThreadgroups(count, threadsPerThreadgroup: tgSize)
}
func dispatch2d(
state: MTLComputePipelineState,
exactly size: MTLSize,
threadgroupSize: MTLSize? = nil
) {
let tgSize = threadgroupSize ?? state.max2dThreadgroupSize
setComputePipelineState(state)
dispatchThreads(size, threadsPerThreadgroup: tgSize)
}
func dispatch1d(
state: MTLComputePipelineState,
exactly width: Int,
threadgroupWidth: Int? = nil
) {
let tgWidth = threadgroupWidth ?? state.threadExecutionWidth
setComputePipelineState(state)
dispatchThreads(MTLSize(width: width, height: 1, depth: 1), threadsPerThreadgroup: MTLSize(width: tgWidth, height: 1, depth: 1))
}
func set(textures: [MTLTexture?]) {
setTextures(textures, range: textures.indices)
}
}
public enum MTLFeature {
case nonUniformThreadgroups
case readWriteTextures(MTLPixelFormat)
}
public enum MTLDeviceError: Swift.Error {
case noTexture
}
public extension MTLDevice {
func supports(feature: MTLFeature) -> Bool {
switch feature {
case .nonUniformThreadgroups:
#if targetEnvironment(macCatalyst)
return supportsFamily(.common3)
#elseif os(iOS)
return supportsFeatureSet(.iOS_GPUFamily4_v1)
#elseif os(macOS)
return supportsFeatureSet(.macOS_GPUFamily1_v3)
#elseif os(visionOS)
return true
#endif
case let .readWriteTextures(pixelFormat):
let tierOneSupportedPixelFormats: Set<MTLPixelFormat> = [
.r32Float, .r32Uint, .r32Sint,
]
let tierTwoSupportedPixelFormats: Set<MTLPixelFormat> = tierOneSupportedPixelFormats.union([
.rgba32Float, .rgba32Uint, .rgba32Sint, .rgba16Float,
.rgba16Uint, .rgba16Sint, .rgba8Unorm, .rgba8Uint,
.rgba8Sint, .r16Float, .r16Uint, .r16Sint,
.r8Unorm, .r8Uint, .r8Sint,
])
switch readWriteTextureSupport {
case .tier1: return tierOneSupportedPixelFormats.contains(pixelFormat)
case .tier2: return tierTwoSupportedPixelFormats.contains(pixelFormat)
case .tierNone: return false
@unknown default: return false
}
}
}
func makeTextureThrows(descriptor: MTLTextureDescriptor) throws -> MTLTexture {
guard let texture = makeTexture(descriptor: descriptor) else {
throw MTLDeviceError.noTexture
}
return texture
}
func makeBufferBackedTexture(descriptor: MTLTextureDescriptor) -> MTLTexture? {
guard let pixelSizeInBytes = descriptor.pixelFormat.size else {
return nil
}
let height = descriptor.height
let bytesPerRow = (descriptor.width * pixelSizeInBytes).aligned(
in: minimumTextureBufferAlignment(for: descriptor.pixelFormat)
)
let length = height * bytesPerRow
let storageMode: MTLResourceOptions = {
switch descriptor.storageMode {
case .private:
return .storageModePrivate
case .shared:
return .storageModeShared
case .memoryless:
return .storageModeMemoryless
@unknown default:
return .storageModeShared
}
}()
guard let buffer = makeBuffer(length: length, options: storageMode) else {
return nil
}
guard let texture = buffer.makeTexture(
descriptor: descriptor, offset: 0, bytesPerRow: bytesPerRow
) else {
return nil
}
return texture
}
func makePixelBufferBackedTexture(
descriptor: MTLTextureDescriptor
) -> (MTLTexture, CVPixelBuffer)? {
let attributes = [
kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferMetalCompatibilityKey: kCFBooleanTrue,
] as CFDictionary
var pixelBuffer: CVPixelBuffer?
let status = CVPixelBufferCreate(
nil,
descriptor.width,
descriptor.height,
descriptor.pixelFormat.compatibleCVPixelFormat,
attributes,
&pixelBuffer
)
guard status == kCVReturnSuccess,
let pixelBuffer,
let texture = makePixelBufferBackedTexture(
descriptor: descriptor,
pixelBuffer: pixelBuffer
)
else {
return nil
}
return (texture, pixelBuffer)
}
func makePixelBufferBackedTexture(
descriptor: MTLTextureDescriptor,
pixelBuffer: CVPixelBuffer
) -> MTLTexture? {
guard let ioSurface = CVPixelBufferGetIOSurface(pixelBuffer),
let texture = makeTexture(
descriptor: descriptor,
iosurface: ioSurface.takeUnretainedValue(),
plane: 0
)
else {
return nil
}
return texture
}
}
public extension MTLComputePipelineState {
var max2dThreadgroupSize: MTLSize {
let width = threadExecutionWidth
let height = maxTotalThreadsPerThreadgroup / width
return MTLSize(width: width, height: height, depth: 1)
}
}
extension MTLSize: Equatable {
public static func == (_ lhs: MTLSize, _ rhs: MTLSize) -> Bool {
lhs.width == rhs.width &&
lhs.height == rhs.height &&
lhs.depth == rhs.depth
}
}
public extension MTLCommandBuffer {
var gpuExecutionTime: CFTimeInterval {
gpuEndTime - gpuStartTime
}
var kernelExecutionTime: CFTimeInterval {
kernelEndTime - kernelStartTime
}
func compute(_ work: (_ encoder: MTLComputeCommandEncoder) -> Void) {
guard let encoder = makeComputeCommandEncoder() else {
print(Self.self, #function, "no compute encoder")
return
}
work(encoder)
encoder.endEncoding()
}
func blit(_ work: (_ encoder: MTLBlitCommandEncoder) -> Void) {
guard let encoder = makeBlitCommandEncoder() else {
print(Self.self, #function, "no blit encoder")
return
}
work(encoder)
encoder.endEncoding()
}
func copy(from sourceTexture: MTLTexture, to destinationTexture: MTLTexture) {
blit { encoder in
encoder.copy(from: sourceTexture, to: destinationTexture)
}
}
func render(descriptor: MTLRenderPassDescriptor, _ work: (_ encoder: MTLRenderCommandEncoder) -> Void) {
guard let encoder = makeRenderCommandEncoder(descriptor: descriptor) else {
print(Self.self, #function, "no render encoder")
return
}
work(encoder)
encoder.endEncoding()
}
func render(to texture: MTLTexture, _ work: (_ encoder: MTLRenderCommandEncoder) -> Void) {
let descriptor = MTLRenderPassDescriptor(texture: texture)
render(descriptor: descriptor, work)
}
func clear(texture: MTLTexture, color: MTLClearColor = .init(red: 0, green: 0, blue: 0, alpha: 0)) {
let descriptor = MTLRenderPassDescriptor()
let attachDescriptor = MTLRenderPassColorAttachmentDescriptor()
attachDescriptor.texture = texture
attachDescriptor.loadAction = .clear
attachDescriptor.storeAction = .store
attachDescriptor.clearColor = color
descriptor.colorAttachments[0] = attachDescriptor
render(descriptor: descriptor) { _ in }
}
}
public extension MPSScaleTransform {
init(from fromSize: CGSize, to toSize: CGSize) {
self.init(
scaleX: toSize.width / fromSize.width,
scaleY: toSize.height / fromSize.height,
translateX: 0,
translateY: 0
)
}
}
public extension MTLCommandBuffer {
func resize(
texture: MTLTexture,
toTexture: MTLTexture,
scaleTransformP: UnsafePointer<MPSScaleTransform>
) {
let resizeKernel = MPSImageBilinearScale(device: device)
resizeKernel.scaleTransform = scaleTransformP
resizeKernel.encode(
commandBuffer: self,
sourceTexture: texture,
destinationTexture: toTexture
)
}
func resize(texture: MTLTexture, toSize: MTLSize) -> (MTLTexture, (() -> Void)?) {
guard texture.size != toSize else {
return (texture, nil)
}
let descriptor = texture.temporaryTextureDescriptor
descriptor.width = toSize.width
descriptor.height = toSize.height
descriptor.depth = toSize.depth
let outputImage = MPSTemporaryImage(
commandBuffer: self, textureDescriptor: descriptor
)
var scale = MPSScaleTransform(from: texture.cgSize, to: outputImage.texture.cgSize)
resize(texture: texture, toTexture: outputImage.texture, scaleTransformP: &scale)
return (outputImage.texture, { outputImage.readCount = 0 })
}
}
extension MTLRenderPassDescriptor {
convenience init(texture: MTLTexture) {
self.init()
let descriptor = MTLRenderPassColorAttachmentDescriptor()
descriptor.texture = texture
descriptor.loadAction = .clear
descriptor.storeAction = .store
descriptor.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
colorAttachments[0] = descriptor
}
}
public extension MTLPixelFormat {
var components: Int? {
switch self {
case .a8Unorm,
.r8Unorm,
.r8Snorm,
.r8Uint,
.r8Sint,
.stencil8,
.r8Unorm_srgb,
.r16Unorm,
.r16Snorm,
.r16Uint,
.r16Sint,
.r16Float,
.r32Uint,
.r32Sint,
.r32Float:
return 1
case .rg8Unorm,
.rg8Unorm_srgb,
.rg8Snorm,
.rg8Uint,
.rg8Sint,
.rg16Unorm,
.rg16Snorm,
.rg16Uint,
.rg16Sint,
.rg16Float,
.rg32Uint,
.rg32Sint,
.rg32Float:
return 2
case .rgba8Unorm,
.rgba8Unorm_srgb,
.rgba8Snorm,
.rgba8Uint,
.rgba8Sint,
.bgra8Unorm,
.bgra8Unorm_srgb,
.rgba16Unorm,
.rgba16Snorm,
.rgba16Uint,
.rgba16Sint,
.rgba16Float,
.rgba32Uint,
.rgba32Sint,
.rgba32Float:
return 4
default:
return nil
}
}
var size: Int? {
switch self {
case .a8Unorm, .r8Unorm, .r8Snorm,
.r8Uint, .r8Sint, .stencil8, .r8Unorm_srgb: return 1
case .r16Unorm, .r16Snorm, .r16Uint,
.r16Sint, .r16Float, .rg8Unorm,
.rg8Snorm, .rg8Uint, .rg8Sint,
.depth16Unorm, .rg8Unorm_srgb: return 2
case .r32Uint, .r32Sint, .r32Float,
.rg16Unorm, .rg16Snorm, .rg16Uint,
.rg16Sint, .rg16Float, .rgba8Unorm,
.rgba8Unorm_srgb, .rgba8Snorm, .rgba8Uint,
.rgba8Sint, .bgra8Unorm, .bgra8Unorm_srgb,
.rgb10a2Unorm, .rgb10a2Uint, .rg11b10Float,
.rgb9e5Float, .bgr10a2Unorm, .gbgr422,
.bgrg422, .depth32Float, .depth24Unorm_stencil8,
.x24_stencil8, .bgr10_xr_srgb, .bgr10_xr: return 4
case .rg32Uint, .rg32Sint, .rg32Float,
.rgba16Unorm, .rgba16Snorm, .rgba16Uint,
.rgba16Sint, .rgba16Float, .bc1_rgba,
.bc1_rgba_srgb, .depth32Float_stencil8, .x32_stencil8,
.bgra10_xr, .bgra10_xr_srgb: return 8
case .rgba32Uint, .rgba32Sint, .rgba32Float,
.bc2_rgba, .bc2_rgba_srgb, .bc3_rgba,
.bc3_rgba_srgb: return 16
default:
// TODO: Finish bc4-bc7
return nil
}
}
}
public extension MTLPixelFormat {
var compatibleCVPixelFormat: OSType {
switch self {
case .r8Unorm, .r8Unorm_srgb: return kCVPixelFormatType_OneComponent8
case .r16Float: return kCVPixelFormatType_OneComponent16Half
case .r32Float: return kCVPixelFormatType_OneComponent32Float
case .rg8Unorm, .rg8Unorm_srgb: return kCVPixelFormatType_TwoComponent8
case .rg16Float: return kCVPixelFormatType_TwoComponent16Half
case .rg32Float: return kCVPixelFormatType_TwoComponent32Float
case .bgra8Unorm, .bgra8Unorm_srgb: return kCVPixelFormatType_32BGRA
case .rgba8Unorm, .rgba8Unorm_srgb: return kCVPixelFormatType_32RGBA
case .rgba16Float: return kCVPixelFormatType_64RGBAHalf
case .rgba32Float: return kCVPixelFormatType_128RGBAFloat
case .depth32Float: return kCVPixelFormatType_DepthFloat32
default: return 0
}
}
}
public extension Int {
fileprivate static let pageSize = Int(getpagesize())
func aligned(in align: Int) -> Int {
Int(ceil(Double(self) / Double(align))) * align
}
var pageAligned: Int {
aligned(in: .pageSize)
}
}