Skip to content

Commit

Permalink
Reformat repository and create linting job.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed Feb 18, 2024
1 parent 7fa19d5 commit 1e4d64b
Show file tree
Hide file tree
Showing 119 changed files with 1,955 additions and 1,448 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: lint

on:
push:
branches:
- main
pull_request:
branches:
- '*'

jobs:
lint:
name: lint
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Install
run: brew install swift-format
- name: Run linting
run: swift-format lint --recursive --parallel --strict --configuration .swift-format.json Package.swift Sources
69 changes: 69 additions & 0 deletions .swift-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentation" : {
"spaces" : 4
},
"indentConditionalCompilationBlocks" : true,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 100,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
"allowedFunctions" : [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoPlaygroundLiterals" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : false,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"TypeNamesShouldBeCapitalized" : true,
"UseEarlyExits" : false,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
},
"spacesAroundRangeFormationOperators" : false,
"tabWidth" : 4,
"version" : 1
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
products: [
.library(
name: "GPUImage",
targets: ["GPUImage"]),
targets: ["GPUImage"])
],
dependencies: [],
targets: [
Expand Down
108 changes: 67 additions & 41 deletions Sources/GPUImage/BasicOperation.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Metal

public func defaultVertexFunctionNameForInputs(_ inputCount:UInt) -> String {
public func defaultVertexFunctionNameForInputs(_ inputCount: UInt) -> String {
switch inputCount {
case 1:
return "oneInputVertex"
Expand All @@ -13,13 +13,13 @@ public func defaultVertexFunctionNameForInputs(_ inputCount:UInt) -> String {
}

open class BasicOperation: ImageProcessingOperation {

public let maximumInputs: UInt
public let targets = TargetContainer()
public let sources = SourceContainer()

public var activatePassthroughOnNextFrame: Bool = false
public var uniformSettings:ShaderUniformSettings
public var uniformSettings: ShaderUniformSettings
public var useMetalPerformanceShaders: Bool = false {
didSet {
if !sharedMetalRenderingDevice.metalPerformanceShadersAreSupported {
Expand All @@ -31,39 +31,46 @@ open class BasicOperation: ImageProcessingOperation {

let renderPipelineState: MTLRenderPipelineState
let operationName: String
var inputTextures = [UInt:Texture]()
let textureInputSemaphore = DispatchSemaphore(value:1)
var inputTextures = [UInt: Texture]()
let textureInputSemaphore = DispatchSemaphore(value: 1)
var useNormalizedTextureCoordinates = true
var metalPerformanceShaderPathway: ((MTLCommandBuffer, [UInt:Texture], Texture) -> ())?
var metalPerformanceShaderPathway: ((MTLCommandBuffer, [UInt: Texture], Texture) -> Void)?

public init(vertexFunctionName: String? = nil, fragmentFunctionName: String, numberOfInputs: UInt = 1, operationName: String = #file) {
public init(
vertexFunctionName: String? = nil, fragmentFunctionName: String, numberOfInputs: UInt = 1,
operationName: String = #file
) {
self.maximumInputs = numberOfInputs
self.operationName = operationName

let concreteVertexFunctionName = vertexFunctionName ?? defaultVertexFunctionNameForInputs(numberOfInputs)
let (pipelineState, lookupTable, bufferSize) = generateRenderPipelineState(device:sharedMetalRenderingDevice, vertexFunctionName:concreteVertexFunctionName, fragmentFunctionName:fragmentFunctionName, operationName:operationName)

let concreteVertexFunctionName =
vertexFunctionName ?? defaultVertexFunctionNameForInputs(numberOfInputs)
let (pipelineState, lookupTable, bufferSize) = generateRenderPipelineState(
device: sharedMetalRenderingDevice, vertexFunctionName: concreteVertexFunctionName,
fragmentFunctionName: fragmentFunctionName, operationName: operationName)
self.renderPipelineState = pipelineState
self.uniformSettings = ShaderUniformSettings(uniformLookupTable:lookupTable, bufferSize:bufferSize)
self.uniformSettings = ShaderUniformSettings(
uniformLookupTable: lookupTable, bufferSize: bufferSize)
}

public func transmitPreviousImage(to target: ImageConsumer, atIndex: UInt) {
// TODO: Finish implementation later
}

public func newTextureAvailable(_ texture: Texture, fromSourceIndex: UInt) {
let _ = textureInputSemaphore.wait(timeout:DispatchTime.distantFuture)
let _ = textureInputSemaphore.wait(timeout: DispatchTime.distantFuture)
defer {
textureInputSemaphore.signal()
}

inputTextures[fromSourceIndex] = texture

if (UInt(inputTextures.count) >= maximumInputs) || activatePassthroughOnNextFrame {
let outputWidth:Int
let outputHeight:Int
let outputWidth: Int
let outputHeight: Int

let firstInputTexture = inputTextures[0]!
if firstInputTexture.orientation.rotationNeeded(for:.portrait).flipsDimensions() {
if firstInputTexture.orientation.rotationNeeded(for: .portrait).flipsDimensions() {
outputWidth = firstInputTexture.texture.height
outputHeight = firstInputTexture.texture.width
} else {
Expand All @@ -72,32 +79,47 @@ open class BasicOperation: ImageProcessingOperation {
}

if uniformSettings.usesAspectRatio {
let outputRotation = firstInputTexture.orientation.rotationNeeded(for:.portrait)
let outputRotation = firstInputTexture.orientation.rotationNeeded(for: .portrait)
uniformSettings["aspectRatio"] = firstInputTexture.aspectRatio(for: outputRotation)
}

guard let commandBuffer = sharedMetalRenderingDevice.commandQueue.makeCommandBuffer() else {return}

let outputTexture = Texture(device:sharedMetalRenderingDevice.device, orientation: .portrait, width: outputWidth, height: outputHeight, timingStyle: firstInputTexture.timingStyle)

guard (!activatePassthroughOnNextFrame) else { // Use this to allow a bootstrap of cyclical processing, like with a low pass filter
guard let commandBuffer = sharedMetalRenderingDevice.commandQueue.makeCommandBuffer()
else { return }

let outputTexture = Texture(
device: sharedMetalRenderingDevice.device, orientation: .portrait,
width: outputWidth, height: outputHeight, timingStyle: firstInputTexture.timingStyle
)

guard !activatePassthroughOnNextFrame else { // Use this to allow a bootstrap of cyclical processing, like with a low pass filter
activatePassthroughOnNextFrame = false
// TODO: Render rotated passthrough image here

removeTransientInputs()
textureInputSemaphore.signal()
updateTargetsWithTexture(outputTexture)
let _ = textureInputSemaphore.wait(timeout:DispatchTime.distantFuture)
let _ = textureInputSemaphore.wait(timeout: DispatchTime.distantFuture)

return
}

if let alternateRenderingFunction = metalPerformanceShaderPathway, useMetalPerformanceShaders {
var rotatedInputTextures: [UInt:Texture]
if (firstInputTexture.orientation.rotationNeeded(for:.portrait) != .noRotation) {
let rotationOutputTexture = Texture(device:sharedMetalRenderingDevice.device, orientation: .portrait, width: outputWidth, height: outputHeight)
guard let rotationCommandBuffer = sharedMetalRenderingDevice.commandQueue.makeCommandBuffer() else {return}
rotationCommandBuffer.renderQuad(pipelineState: sharedMetalRenderingDevice.passthroughRenderState, uniformSettings: uniformSettings, inputTextures: inputTextures, useNormalizedTextureCoordinates: useNormalizedTextureCoordinates, outputTexture: rotationOutputTexture)

if let alternateRenderingFunction = metalPerformanceShaderPathway,
useMetalPerformanceShaders
{
var rotatedInputTextures: [UInt: Texture]
if firstInputTexture.orientation.rotationNeeded(for: .portrait) != .noRotation {
let rotationOutputTexture = Texture(
device: sharedMetalRenderingDevice.device, orientation: .portrait,
width: outputWidth, height: outputHeight)
guard
let rotationCommandBuffer = sharedMetalRenderingDevice.commandQueue
.makeCommandBuffer()
else { return }
rotationCommandBuffer.renderQuad(
pipelineState: sharedMetalRenderingDevice.passthroughRenderState,
uniformSettings: uniformSettings, inputTextures: inputTextures,
useNormalizedTextureCoordinates: useNormalizedTextureCoordinates,
outputTexture: rotationOutputTexture)
rotationCommandBuffer.commit()
rotatedInputTextures = inputTextures
rotatedInputTextures[0] = rotationOutputTexture
Expand All @@ -109,23 +131,27 @@ open class BasicOperation: ImageProcessingOperation {
internalRenderFunction(commandBuffer: commandBuffer, outputTexture: outputTexture)
}
commandBuffer.commit()

removeTransientInputs()
textureInputSemaphore.signal()
updateTargetsWithTexture(outputTexture)
let _ = textureInputSemaphore.wait(timeout:DispatchTime.distantFuture)
let _ = textureInputSemaphore.wait(timeout: DispatchTime.distantFuture)
}
}

func removeTransientInputs() {
for index in 0..<self.maximumInputs {
if let texture = inputTextures[index], texture.timingStyle.isTransient() {
inputTextures[index] = nil
}
}
}

func internalRenderFunction(commandBuffer: MTLCommandBuffer, outputTexture: Texture) {
commandBuffer.renderQuad(pipelineState: renderPipelineState, uniformSettings: uniformSettings, inputTextures: inputTextures, useNormalizedTextureCoordinates: useNormalizedTextureCoordinates, outputTexture: outputTexture)
commandBuffer.renderQuad(
pipelineState: renderPipelineState, uniformSettings: uniformSettings,
inputTextures: inputTextures,
useNormalizedTextureCoordinates: useNormalizedTextureCoordinates,
outputTexture: outputTexture)
}
}
Loading

0 comments on commit 1e4d64b

Please sign in to comment.