-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 59634ea
Showing
55 changed files
with
2,928 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Xcode | ||
# | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## User settings | ||
xcuserdata/ | ||
|
||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) | ||
*.xcscmblueprint | ||
*.xccheckout | ||
|
||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) | ||
build/ | ||
DerivedData/ | ||
*.moved-aside | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
|
||
## Obj-C/Swift specific | ||
*.hmap | ||
|
||
## App packaging | ||
*.ipa | ||
*.dSYM.zip | ||
*.dSYM | ||
|
||
## Playgrounds | ||
timeline.xctimeline | ||
playground.xcworkspace | ||
|
||
# Swift Package Manager | ||
# | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
# Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
# *.xcodeproj | ||
# | ||
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata | ||
# hence it is not needed unless you have added a package configuration file to your project | ||
# .swiftpm | ||
|
||
.build/ | ||
|
||
# CocoaPods | ||
# | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# | ||
# Pods/ | ||
# | ||
# Add this line if you want to avoid checking in source code from the Xcode workspace | ||
# *.xcworkspace | ||
|
||
# Carthage | ||
# | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
|
||
Carthage/Build/ | ||
|
||
# Accio dependency management | ||
Dependencies/ | ||
.accio/ | ||
|
||
# fastlane | ||
# | ||
# It is recommended to not store the screenshots in the git repo. | ||
# Instead, use fastlane to re-generate the screenshots whenever they are needed. | ||
# For more information about the recommended setup visit: | ||
# https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
||
fastlane/report.xml | ||
fastlane/Preview.html | ||
fastlane/screenshots/**/*.png | ||
fastlane/test_output | ||
|
||
# Code Injection | ||
# | ||
# After new code Injection tools there's a generated folder /iOSInjectionProject | ||
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ | ||
.swiftpm | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Eugene Bokhan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// swift-tools-version:5.3 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "metal-tools", | ||
platforms: [ | ||
.iOS(.v11), | ||
.macOS(.v10_13) | ||
], | ||
products: [ | ||
.library(name: "MetalTools", | ||
targets: ["MetalTools"]), | ||
], | ||
targets: [ | ||
.target(name: "MetalTools") | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# metal-tools |
104 changes: 104 additions & 0 deletions
104
Sources/MetalTools/CodableContainer/MTLTextureCodableContainer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Metal | ||
|
||
public class MTLTextureCodableContainer: Codable { | ||
|
||
private let descriptor: MTLTextureDescriptorCodableContainer | ||
private var data: Data | ||
|
||
public init(texture: MTLTexture) throws { | ||
let descriptor = texture.descriptor | ||
self.descriptor = .init(descriptor: descriptor) | ||
|
||
let sizeAndAlign = texture.device.heapTextureSizeAndAlign(descriptor: descriptor) | ||
|
||
var data = Data(count: sizeAndAlign.size) | ||
try data.withUnsafeMutableBytes { p in | ||
let pointer = p.baseAddress! | ||
|
||
guard let pixelFormatSize = texture.pixelFormat.size | ||
else { throw MetalError.MTLTextureSerializationError.unsupportedPixelFormat } | ||
|
||
var offset = 0 | ||
|
||
for slice in 0 ..< texture.arrayLength { | ||
for mipMaplevel in 0 ..< texture.mipmapLevelCount { | ||
guard let textureView = texture.makeTextureView(pixelFormat: texture.pixelFormat, | ||
textureType: texture.textureType, | ||
levels: mipMaplevel ..< mipMaplevel + 1, | ||
slices: slice ..< slice + 1) | ||
else { throw MetalError.MTLTextureSerializationError.dataAccessFailure } | ||
|
||
var bytesPerRow = pixelFormatSize * textureView.width | ||
let bytesPerImage = bytesPerRow * textureView.height | ||
|
||
// This comes from docs | ||
// > When you copy pixels from a MTLTextureType1D or MTLTextureType1DArray texture, use 0. | ||
if texture.textureType == .type1D || texture.textureType == .type1DArray { | ||
bytesPerRow = 0 | ||
} | ||
|
||
textureView.getBytes(pointer.advanced(by: offset), | ||
bytesPerRow: bytesPerRow, | ||
bytesPerImage: bytesPerImage, | ||
from: textureView.region, | ||
mipmapLevel: 0, | ||
slice: 0) | ||
|
||
offset += bytesPerImage | ||
} | ||
} | ||
} | ||
|
||
self.data = data | ||
} | ||
|
||
public func texture(device: MTLDevice) throws -> MTLTexture { | ||
|
||
enum Error: Swift.Error { | ||
case missingBaseAddress | ||
} | ||
|
||
guard let texture = device.makeTexture(descriptor: self.descriptor.descriptor) | ||
else { throw MetalError.MTLTextureSerializationError.allocationFailed } | ||
|
||
try self.data.withUnsafeMutableBytes { p in | ||
guard let pointer = p.baseAddress | ||
else { throw Error.missingBaseAddress } | ||
|
||
guard let pixelFormatSize = texture.pixelFormat.size | ||
else { throw MetalError.MTLTextureSerializationError.unsupportedPixelFormat } | ||
|
||
var offset = 0 | ||
|
||
for slice in 0 ..< texture.arrayLength { | ||
for mipMaplevel in 0 ..< texture.mipmapLevelCount { | ||
guard let textureView = texture.makeTextureView(pixelFormat: texture.pixelFormat, | ||
textureType: texture.textureType, | ||
levels: mipMaplevel ..< mipMaplevel + 1, | ||
slices: slice ..< slice + 1) | ||
else { throw MetalError.MTLTextureSerializationError.dataAccessFailure } | ||
|
||
var bytesPerRow = pixelFormatSize * textureView.width | ||
let bytesPerImage = bytesPerRow * textureView.height | ||
|
||
// This comes from docs | ||
// > When you copy pixels from a MTLTextureType1D or MTLTextureType1DArray texture, use 0. | ||
if texture.textureType == .type1D || texture.textureType == .type1DArray { | ||
bytesPerRow = 0 | ||
} | ||
|
||
textureView.replace(region: textureView.region, | ||
mipmapLevel: 0, | ||
slice: 0, | ||
withBytes: pointer.advanced(by: offset), | ||
bytesPerRow: bytesPerRow, | ||
bytesPerImage: bytesPerImage) | ||
|
||
offset += bytesPerImage | ||
} | ||
} | ||
} | ||
|
||
return texture | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Sources/MetalTools/CodableContainer/MTLTextureDescriptorCodableContainer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Metal | ||
|
||
final class MTLTextureDescriptorCodableContainer: Codable { | ||
|
||
let descriptor: MTLTextureDescriptor | ||
|
||
init(descriptor: MTLTextureDescriptor) { | ||
self.descriptor = descriptor | ||
} | ||
|
||
required init(from decoder: Decoder) throws { | ||
self.descriptor = try MTLTextureDescriptor(from: decoder) | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try self.descriptor.encode(to: encoder) | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
Sources/MetalTools/Extensions/CoreVideo/CVPixelBuffer+MTLTexture.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Metal | ||
import CoreVideo.CVPixelBuffer | ||
|
||
public extension CVPixelBuffer { | ||
|
||
func metalTexture(using cache: CVMetalTextureCache, | ||
pixelFormat: MTLPixelFormat, | ||
planeIndex: Int = 0) -> MTLTexture? { | ||
let width = CVPixelBufferGetWidthOfPlane(self, planeIndex) | ||
let height = CVPixelBufferGetHeightOfPlane(self, planeIndex) | ||
|
||
var texture: CVMetalTexture? = nil | ||
let status = CVMetalTextureCacheCreateTextureFromImage(nil, | ||
cache, | ||
self, | ||
nil, | ||
pixelFormat, | ||
width, | ||
height, | ||
planeIndex, | ||
&texture) | ||
|
||
var retVal: MTLTexture? = nil | ||
if status == kCVReturnSuccess { | ||
retVal = CVMetalTextureGetTexture(texture!) | ||
} | ||
|
||
return retVal | ||
} | ||
|
||
} | ||
|
||
public extension MTLContext { | ||
|
||
func textureCache(textureAge: Float = 1.0) throws -> CVMetalTextureCache { | ||
let textureAgeKey = kCVMetalTextureCacheMaximumTextureAgeKey as NSString | ||
let textureAgeValue = NSNumber(value: textureAge) | ||
let options = [textureAgeKey: textureAgeValue] as NSDictionary | ||
|
||
var videoTextureCache: CVMetalTextureCache! = nil | ||
let status = CVMetalTextureCacheCreate(kCFAllocatorDefault, | ||
options, | ||
self.device, | ||
nil, | ||
&videoTextureCache) | ||
if status != kCVReturnSuccess { | ||
throw MetalError.MTLContextError.textureCacheCreationFailed | ||
} | ||
|
||
return videoTextureCache | ||
} | ||
|
||
} | ||
|
||
public extension MTLTexture { | ||
|
||
var pixelBuffer: CVPixelBuffer? { | ||
guard let cvPixelFormat = self.pixelFormat | ||
.compatibleCVPixelFormat | ||
else { return nil } | ||
|
||
var pb: CVPixelBuffer? = nil | ||
var status = CVPixelBufferCreate(nil, | ||
self.width, | ||
self.height, | ||
cvPixelFormat, | ||
nil, | ||
&pb) | ||
guard status == kCVReturnSuccess, | ||
let pixelBuffer = pb | ||
else { return nil } | ||
|
||
status = CVPixelBufferLockBaseAddress(pixelBuffer, []) | ||
guard status == kCVReturnSuccess, | ||
let pixelBufferBaseAdress = CVPixelBufferGetBaseAddress(pixelBuffer) | ||
else { return nil } | ||
|
||
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) | ||
|
||
self.getBytes(pixelBufferBaseAdress, | ||
bytesPerRow: bytesPerRow, | ||
from: self.region, | ||
mipmapLevel: 0) | ||
|
||
status = CVPixelBufferUnlockBaseAddress(pixelBuffer, []) | ||
guard status == kCVReturnSuccess | ||
else { return nil } | ||
|
||
return pixelBuffer | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
Sources/MetalTools/Extensions/Metal/MTLBlitCommandEncoder/MTLBlitCommandEncoder+Copy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Metal | ||
|
||
public extension MTLBlitCommandEncoder { | ||
func copy(region: MTLRegion, | ||
from source: MTLTexture, | ||
to targetOrigin: MTLOrigin, | ||
of target: MTLTexture, | ||
sourceSlice: Int = 0, | ||
sourceLevel: Int = 0, | ||
destinationSlice: Int = 0, | ||
destionationLevel: Int = 0) { | ||
self.copy(from: source, | ||
sourceSlice: sourceSlice, | ||
sourceLevel: sourceLevel, | ||
sourceOrigin: region.origin, | ||
sourceSize: region.size, | ||
to: target, | ||
destinationSlice: destinationSlice, | ||
destinationLevel: destionationLevel, | ||
destinationOrigin: targetOrigin) | ||
} | ||
|
||
func copy(texture: MTLTexture, | ||
to targetOrigin: MTLOrigin, | ||
of target: MTLTexture, | ||
sourceSlice: Int = 0, | ||
sourceLevel: Int = 0, | ||
destinationSlice: Int = 0, | ||
destionationLevel: Int = 0) { | ||
let region = texture.region | ||
self.copy(from: texture, | ||
sourceSlice: sourceSlice, | ||
sourceLevel: sourceLevel, | ||
sourceOrigin: region.origin, | ||
sourceSize: region.size, | ||
to: target, | ||
destinationSlice: destinationSlice, | ||
destinationLevel: destionationLevel, | ||
destinationOrigin: targetOrigin) | ||
} | ||
} |
Oops, something went wrong.