Skip to content

Commit

Permalink
Blit Biffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gadirom committed Jul 27, 2022
1 parent 802816f commit 70089e6
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 23 deletions.
2 changes: 0 additions & 2 deletions Example/Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ struct ContentView: View {
MPSUnary{ [self] in MPSImageGaussianBlur(device: $0, sigma: blurRadius)}
.source(targetTexture)
.toDrawable()
// Blit()
// .source(targetTexture)

//let _ = print("compile")
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/MetalBuilder/MetalBuilderRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public final class MetalBuilderRenderer{
addTextures(newTexs: [mpsUnaryComponent.inTexture, mpsUnaryComponent.outTexture])
passes.append(MPSUnaryPass(mpsUnaryComponent))
}
if let blitComponent = component as? Blit{
addTextures(newTexs: [blitComponent.inTexture, blitComponent.outTexture])
passes.append(BlitPass(blitComponent))
if let blitTextureComponent = component as? BlitTexture{
addTextures(newTexs: [blitTextureComponent.inTexture, blitTextureComponent.outTexture])
passes.append(BlitTexturePass(blitTextureComponent))
}
}
//setup passes
Expand Down
43 changes: 43 additions & 0 deletions Sources/MetalBuilder/MetalPasses/BlitBufferPass.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import MetalKit
import SwiftUI

// BlitBuffer pass
class BlitBufferPass: MetalPass{
let component: BlitBuffer

init(_ component: BlitBuffer){
self.component = component
}
func setup(device: MTLDevice, library: MTLLibrary){
}
func encode(_ commandBuffer: MTLCommandBuffer,_ drawable: CAMetalDrawable?) {

guard let inBuffer = component.inBuffer
else{
print("BlitBuffer: no inBuffer!")
return
}
guard let outBuffer = component.outBuffer
else{
print("BlitBuffer: no outBuffer!")
return
}
let elementSize = inBuffer.elementSize!
let count: Int
if let c = component.count{
count = c
}else{
count = inBuffer.count
}

let size = count*elementSize
let inOffset = component.sourceOffset*size
let outOffset = component.destinationOffset*size

let blitBufferEncoder = commandBuffer.makeBlitCommandEncoder()
blitBufferEncoder?.copy(from: inBuffer.buffer!, sourceOffset: inOffset,
to: outBuffer.buffer!, destinationOffset: outOffset,
size: size)
blitBufferEncoder?.endEncoding()
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import MetalKit
import SwiftUI

// Blit pass
class BlitPass: MetalPass{
let component: Blit
// BlitTexture pass
class BlitTexturePass: MetalPass{
let component: BlitTexture

init(_ component: Blit){
init(_ component: BlitTexture){
self.component = component
}
func setup(device: MTLDevice, library: MTLLibrary){
}
func encode(_ commandBuffer: MTLCommandBuffer,_ drawable: CAMetalDrawable?) {
let blitEncoder = commandBuffer.makeBlitCommandEncoder()
if let inTexture = component.inTexture?.texture{
var size: MTLSize
if let s = component.size?.wrappedValue{
Expand All @@ -30,11 +29,12 @@ class BlitPass: MetalPass{
}
outTexture = t
}
blitEncoder?.copy(from: inTexture,
let blitTextureEncoder = commandBuffer.makeBlitCommandEncoder()
blitTextureEncoder?.copy(from: inTexture,
sourceSlice: 0, sourceLevel: 0, sourceOrigin: MTLOriginMake(0, 0, 0), sourceSize: size,

to: outTexture, destinationSlice: 0, destinationLevel: 0, destinationOrigin: MTLOriginMake(0, 0, 0))
blitEncoder?.endEncoding()
blitTextureEncoder?.endEncoding()
}
}
}
15 changes: 11 additions & 4 deletions Sources/MetalBuilder/PropertyWrappers/MetalBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ enum MetalBuilderBufferError: Error {
case bufferNotCreated
}

public final class MTLBufferContainer<T>{
public var buffer: MTLBuffer?
public var pointer: UnsafeMutablePointer<T>?
public class BufferContainer{
var buffer: MTLBuffer?
public let count: Int
public var elementSize: Int?

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

public final class MTLBufferContainer<T>: BufferContainer{
//public var buffer: MTLBuffer?
public var pointer: UnsafeMutablePointer<T>?

func create(device: MTLDevice) throws{
let length = MemoryLayout<T>.stride*count
elementSize = MemoryLayout<T>.stride
let length = elementSize!*count
buffer = device.makeBuffer(length: length)
if let buffer = buffer{
pointer = buffer.contents().bindMemory(to: T.self, capacity: length)
Expand Down
37 changes: 37 additions & 0 deletions Sources/MetalBuilder/ResultBuilderComponents/BlitBuffer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import MetalKit
import SwiftUI

/// BlitBuffer Component
///
/// initializes a blit pass
public struct BlitBuffer: MetalBuilderComponent{
var inBuffer: BufferContainer?
var outBuffer: BufferContainer?

var sourceOffset: Int = 0
var destinationOffset: Int = 0
var count: Int?

public init(){
}
}

// chaining dunctions
public extension BlitBuffer{
func source<T>(_ container: MTLBufferContainer<T>)->BlitBuffer{
var b = self
b.inBuffer = container
return b
}
func destination<T>(_ container: MTLBufferContainer<T>)->BlitBuffer{
var b = self
b.outBuffer = container
return b
}
func count(count: Int)->BlitBuffer{
var b = self
b.count = count
return b
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
import MetalKit
import SwiftUI

/// Blit Component
/// BlitTexture Component
///
/// initializes a blit pass
/// if no destination is set tries to copy to drawable
public struct Blit: MetalBuilderComponent{
var outTexture: MTLTextureContainer?
public struct BlitTexture: MetalBuilderComponent{
var inTexture: MTLTextureContainer?
var outTexture: MTLTextureContainer?

var size: Binding<MTLSize>?
public init(){
}
}

// chaining dunctions
public extension Blit{
func source(_ container: MTLTextureContainer)->Blit{
public extension BlitTexture{
func source(_ container: MTLTextureContainer)->BlitTexture{
var b = self
b.inTexture = container
return b
}
func destination(_ container: MTLTextureContainer)->Blit{
func destination(_ container: MTLTextureContainer)->BlitTexture{
var b = self
b.outTexture = container
return b
}
func size(size: Binding<MTLSize>)->Blit{
func size(size: Binding<MTLSize>)->BlitTexture{
var b = self
b.size = size
return b
Expand Down

0 comments on commit 70089e6

Please sign in to comment.