Skip to content

Commit

Permalink
chore: More refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcarnie committed Jul 27, 2022
1 parent 09dc737 commit fe002ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
67 changes: 34 additions & 33 deletions Source/FilterChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final public class FilterChain: ScreenshotSource {
}
private var outputFrame: OutputFrame = .init()

fileprivate struct Pass {
private struct Pass {
var format = MTLPixelFormat.bgra8Unorm
var buffers = [MTLBuffer?](repeating: nil, count: Constants.maxConstantBuffers)
var vBuffers = [MTLBuffer?](repeating: nil, count: Constants.maxConstantBuffers) // array used for vertex binding
Expand Down Expand Up @@ -332,6 +332,7 @@ final public class FilterChain: ScreenshotSource {
if historyNeedsInit {
initHistory()
} else {
// shift history and move last texture into first position
let tmp = historyTextures[historyCount]
for k in (1...historyCount).reversed() {
historyTextures[k] = historyTextures[k - 1]
Expand Down Expand Up @@ -372,7 +373,7 @@ final public class FilterChain: ScreenshotSource {

private func resize() {
let bounds = Self.fitAspectRectIntoRect(aspectSize: aspectSize, size: drawableSize)
if outputBounds.origin == bounds.origin && outputBounds.size == bounds.size {
if outputBounds == bounds {
return
}

Expand Down Expand Up @@ -442,42 +443,42 @@ final public class FilterChain: ScreenshotSource {

defer { _clearTextures.removeAll(keepingCapacity: true) }

if #available(macOS 10.15, *) {
guard #available(macOS 10.15, *) else { return }

/**
Find the size of the largest texture, in order to allocate a buffer with at least enough space for all textures.
*/
var sizeMax = 0
for t in _clearTextures {
let bytesPerPixel = t.pixelFormat.bytesPerPixel
precondition(bytesPerPixel > 0, "Unable to determine bytes per pixel for pixel format \(t.pixelFormat)")

let bytesPerRow = t.width * bytesPerPixel
let bytesPerImage = t.height * bytesPerRow
if bytesPerImage > sizeMax {
sizeMax = bytesPerImage
}
}

/**
Allocate a buffer over the entire heap and fill it with zeros
*/
if let bce = commandBuffer.makeBlitCommandEncoder(),
let buf = device.makeBuffer(length: sizeMax, options: [.storageModePrivate]) {
bce.fill(buffer: buf, range: 0..<sizeMax, value: 0)

/**
Find the size of the largest texture, in order to allocate a buffer with at least enough space for all textures.
Use the cleared buffer to clear the destination texture.
*/
var sizeMax = 0
for t in _clearTextures {
let bytesPerPixel = t.pixelFormat.bytesPerPixel
precondition(bytesPerPixel > 0, "Unable to determine bytes per pixel for pixel format \(t.pixelFormat)")

let bytesPerRow = t.width * bytesPerPixel
let bytesPerImage = t.height * bytesPerRow
if bytesPerImage > sizeMax {
sizeMax = bytesPerImage
}
}

/**
Allocate a buffer over the entire heap and fill it with zeros
*/
if let bce = commandBuffer.makeBlitCommandEncoder(),
let buf = device.makeBuffer(length: sizeMax, options: [.storageModePrivate]) {
bce.fill(buffer: buf, range: 0..<sizeMax, value: 0)

/**
Use the cleared buffer to clear the destination texture.
*/
for t in _clearTextures {
let bytesPerPixel = t.pixelFormat.bytesPerPixel
let bytesPerRow = t.width * bytesPerPixel
let bytesPerImage = t.height * bytesPerRow
let sourceSize = MTLSize(width: t.width, height: t.height, depth: 1)
bce.copy(from: buf, sourceOffset: 0, sourceBytesPerRow: bytesPerRow, sourceBytesPerImage: bytesPerImage, sourceSize: sourceSize,
to: t, destinationSlice: 0, destinationLevel: 0, destinationOrigin: .init())
}
bce.endEncoding()
let sourceSize = MTLSize(width: t.width, height: t.height, depth: 1)
bce.copy(from: buf, sourceOffset: 0, sourceBytesPerRow: bytesPerRow, sourceBytesPerImage: bytesPerImage, sourceSize: sourceSize,
to: t, destinationSlice: 0, destinationLevel: 0, destinationOrigin: .init())
}
bce.endEncoding()
}
}

Expand Down Expand Up @@ -537,7 +538,7 @@ final public class FilterChain: ScreenshotSource {

private func prepareSourceTexture(_ sourceTexture: MTLTexture, commandBuffer: MTLCommandBuffer) {
if historyCount == 0 {
// save a copy by setting the sourceTexture to Original / OriginalHistory0
// save a copy by setting the sourceTexture to Original / OriginalHistory0 semantic
initTexture(&historyTextures[0], withTexture: sourceTexture)
} else {
let texture = fetchNextHistoryTexture()
Expand Down Expand Up @@ -902,7 +903,7 @@ final public class FilterChain: ScreenshotSource {
}
}

let bindings = ShaderPassBindings(index: passNumber)
let bindings = ShaderPassBindings()
let pass = ss.passes[passNumber]
updateBindings(passBindings: bindings,
forPassNumber: passNumber,
Expand Down
10 changes: 1 addition & 9 deletions Source/PixelBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public class PixelBuffer {
}
}

public enum OEMTLPixelFormat: Int, RawRepresentable, CaseIterable {
case invalid

public enum OEMTLPixelFormat: Int, CaseIterable {
// 16-bit formats
case bgra4Unorm
case b5g6r5Unorm
Expand All @@ -198,9 +196,6 @@ public enum OEMTLPixelFormat: Int, RawRepresentable, CaseIterable {

case .bgra8Unorm, .bgrx8Unorm:
return true

default:
return false
}
}

Expand All @@ -212,9 +207,6 @@ public enum OEMTLPixelFormat: Int, RawRepresentable, CaseIterable {

case .b5g6r5Unorm, .r5g5b5a1Unorm, .bgra4Unorm:
return 2

default:
return 4
}
}
}
5 changes: 0 additions & 5 deletions Source/ShaderPassBindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,9 @@ class ShaderPassTextureBinding {
}

class ShaderPassBindings {
public let index: Int
public private(set) var buffers = [ShaderPassBufferBinding(), ShaderPassBufferBinding()] // equivalent to Constants.maxConstantBuffers
public private(set) var textures: [ShaderPassTextureBinding] = []

init(index: Int) {
self.index = index
}

func addTexture(_ texture: UnsafeRawPointer, binding: Int, name: String) -> ShaderPassTextureBinding {
let t = ShaderPassTextureBinding(texture: texture, binding: binding, name: name)
textures.append(t)
Expand Down

0 comments on commit fe002ec

Please sign in to comment.