Skip to content

Commit 2978f9e

Browse files
committed
MTLCommandBuffer extension
1 parent baa8739 commit 2978f9e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

EasyMetalShader.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/EasyMetalShader/Pipelines/EMMetalDispatch.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,27 @@ public class EMMetalDispatch {
5353
commandBuffer.waitUntilCompleted()
5454
}
5555
}
56+
57+
extension MTLCommandBuffer {
58+
59+
public func compute(_ process: @escaping (MTLComputeCommandEncoder) -> ()) {
60+
let encoder = self.makeComputeCommandEncoder()!
61+
process(encoder)
62+
encoder.endEncoding()
63+
}
64+
65+
public func render(renderTargetTexture: MTLTexture, needsClear: Bool, process: @escaping (MTLRenderCommandEncoder) -> ()) {
66+
let descriptor = MTLRenderPassDescriptor()
67+
descriptor.colorAttachments[0].texture = renderTargetTexture
68+
if needsClear {
69+
descriptor.colorAttachments[0].loadAction = .clear
70+
} else {
71+
descriptor.colorAttachments[0].loadAction = .load
72+
}
73+
descriptor.colorAttachments[0].storeAction = .store
74+
descriptor.colorAttachments[0].clearColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
75+
let encoder = self.makeRenderCommandEncoder(descriptor: descriptor)!
76+
process(encoder)
77+
encoder.endEncoding()
78+
}
79+
}

0 commit comments

Comments
 (0)