Skip to content

Commit 41278e9

Browse files
authored
Merge pull request #3 from yukiny0811/feat/sample-usage
add sample texture usage
2 parents 89692b6 + ef44839 commit 41278e9

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

Examples/ExampleMacOS/ExampleMacOS/Logic/MyRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MyRenderer: ShaderRenderer {
2626
override func draw(view: MTKView, drawable: CAMetalDrawable) {
2727
let dispatch = EMMetalDispatch()
2828
dispatch.compute { [self] encoder in
29-
compute.tex = EMMetalTexture(texture: drawable.texture)
29+
compute.tex = drawable.texture.emTexture
3030
compute.intensity = abs(sin(Float(Date().timeIntervalSince(date)))) * 100
3131
compute.dispatch(encoder, textureSizeReference: drawable.texture)
3232
}

Sources/EasyMetalShader/Models/EMMetalTexture.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class EMMetalTexture: NSObject {
1414
public var texture: MTLTexture?
1515
public var usage: EMMetalTextureUsage
1616

17-
public init(texture: MTLTexture?, usage: EMMetalTextureUsage = .read_write) {
17+
public init(texture: MTLTexture?, usage: EMMetalTextureUsage) {
1818
self.texture = texture
1919
self.usage = usage
2020
}
2121

22-
public static func create(width: Int, height: Int, pixelFormat: MTLPixelFormat, label: String?, usage: EMMetalTextureUsage) -> EMMetalTexture {
22+
public static func create(width: Int, height: Int, pixelFormat: MTLPixelFormat, label: String?) -> MTLTexture {
2323
let descriptor = MTLTextureDescriptor()
2424
descriptor.pixelFormat = pixelFormat
2525
descriptor.textureType = .type2D
@@ -29,10 +29,10 @@ public class EMMetalTexture: NSObject {
2929
descriptor.resourceOptions = .storageModePrivate
3030
let texture = ShaderCore.device.makeTexture(descriptor: descriptor)!
3131
texture.label = label
32-
return EMMetalTexture(texture: texture, usage: usage)
32+
return texture
3333
}
3434

35-
public static func createManaged(width: Int, height: Int, pixelFormat: MTLPixelFormat, label: String?, usage: EMMetalTextureUsage) -> EMMetalTexture {
35+
public static func createManaged(width: Int, height: Int, pixelFormat: MTLPixelFormat, label: String?) -> MTLTexture {
3636
let descriptor = MTLTextureDescriptor()
3737
descriptor.pixelFormat = pixelFormat
3838
descriptor.textureType = .type2D
@@ -46,6 +46,14 @@ public class EMMetalTexture: NSObject {
4646
#endif
4747
let texture = ShaderCore.device.makeTexture(descriptor: descriptor)!
4848
texture.label = label
49-
return EMMetalTexture(texture: texture, usage: usage)
49+
return texture
50+
}
51+
}
52+
53+
public extension MTLTexture {
54+
55+
// dont use this before shader compilation!
56+
var emTexture: EMMetalTexture {
57+
.init(texture: self, usage: .read)
5058
}
5159
}

Sources/EasyMetalShader/Models/EMMetalTextureUsage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public enum EMMetalTextureUsage {
99
case read
1010
case write
1111
case read_write
12+
case sample
1213
}

Sources/EasyMetalShader/Pipelines/EMMetalComputeFunction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ open class EMMetalComputeFunction: NSObject, EMMetalFunction {
6767
functionImpl += "texture2d<float, access::write> \(key) [[texture(\(i+1))]]"
6868
case .read_write:
6969
functionImpl += "texture2d<float, access::read_write> \(key) [[texture(\(i+1))]]"
70+
case .sample:
71+
functionImpl += "texture2d<float, access::sample> \(key) [[texture(\(i+1))]]"
7072
}
7173
case .none:
7274
break

Sources/EasyMetalShader/Pipelines/EMMetalRenderFunction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ open class EMMetalRenderFunction: NSObject, EMMetalFunction {
7070
functionImpl += "texture2d<float, access::write> \(key) [[texture(\(i+1))]]"
7171
case .read_write:
7272
functionImpl += "texture2d<float, access::read_write> \(key) [[texture(\(i+1))]]"
73+
case .sample:
74+
functionImpl += "texture2d<float, access::sample> \(key) [[texture(\(i+1))]]"
7375
}
7476
case .none:
7577
break
@@ -142,6 +144,8 @@ open class EMMetalRenderFunction: NSObject, EMMetalFunction {
142144
functionImpl += "texture2d<float, access::write> \(key) [[texture(\(i+1))]]"
143145
case .read_write:
144146
functionImpl += "texture2d<float, access::read_write> \(key) [[texture(\(i+1))]]"
147+
case .sample:
148+
functionImpl += "texture2d<float, access::sample> \(key) [[texture(\(i+1))]]"
145149
}
146150
case .none:
147151
break

0 commit comments

Comments
 (0)