@@ -41,31 +41,31 @@ public class MetalView: NSObject, MTKViewDelegate {
41
41
let device : MTLDevice !
42
42
let cps : MTLComputePipelineState !
43
43
private var startDate : Date = Date ( )
44
-
44
+
45
45
public init ? ( mtkView: MTKView ) {
46
46
view = mtkView
47
47
view. clearColor = MTLClearColorMake ( 0.5 , 0.5 , 0.5 , 1 )
48
48
view. colorPixelFormat = . bgra8Unorm
49
49
device = MTLCreateSystemDefaultDevice ( ) !
50
50
commandQueue = device. makeCommandQueue ( )
51
51
let library = try ! device. makeLibrary ( source: source, options: nil )
52
- let function = library. makeFunction ( name: " focusedWindowChangedEffect " ) !
52
+ let function = library. makeFunction ( name: " focusedWindowChangedEffect " ) !
53
53
cps = try ! device. makeComputePipelineState ( function: function)
54
-
54
+
55
55
super. init ( )
56
56
view. delegate = self
57
57
view. device = device
58
58
}
59
-
60
- public func mtkView( _ view : MTKView , drawableSizeWillChange size : CGSize ) { }
61
-
59
+
60
+ public func mtkView( _: MTKView , drawableSizeWillChange _ : CGSize ) { }
61
+
62
62
public func draw( in view: MTKView ) {
63
63
var time = Float ( Date ( ) . timeIntervalSince ( startDate) )
64
-
65
- if ( time > 1 ) {
64
+
65
+ if time > 1.0 {
66
66
startDate = Date ( )
67
67
}
68
-
68
+
69
69
var color = vector_float3 ( 0.3 , 0.2 , 1.0 ) // rgba
70
70
71
71
if let drawable = view. currentDrawable,
@@ -78,13 +78,13 @@ public class MetalView: NSObject, MTKViewDelegate {
78
78
commandEncoder. setBytes ( & color, length: MemoryLayout < Float > . size * 3 , index: 1 )
79
79
80
80
let w = cps. threadExecutionWidth
81
- let h = cps. maxTotalThreadsPerThreadgroup / w;
81
+ let h = cps. maxTotalThreadsPerThreadgroup / w
82
82
let threadsPerThreadgroup = MTLSize ( width: w, height: h, depth: 1 )
83
- let threadsPerGrid = MTLSize ( width: Int ( view . frame . width) * 2 ,
84
- height: Int ( view . frame . height) * 2 ,
85
- depth: 1 ) ;
83
+ let threadsPerGrid = MTLSize ( width: drawable . texture . width,
84
+ height: drawable . texture . height,
85
+ depth: 1 )
86
86
commandEncoder. dispatchThreads ( threadsPerGrid,
87
- threadsPerThreadgroup: threadsPerThreadgroup)
87
+ threadsPerThreadgroup: threadsPerThreadgroup)
88
88
commandEncoder. endEncoding ( )
89
89
commandBuffer. present ( drawable)
90
90
commandBuffer. commit ( )
@@ -97,4 +97,3 @@ let view = MTKView(frame: frame)
97
97
let delegate = MetalView ( mtkView: view)
98
98
view. delegate = delegate
99
99
PlaygroundPage . current. liveView = view
100
-
0 commit comments