Skip to content

Commit 44e99f2

Browse files
committed
use drawable.texture.width
1 parent 3817c71 commit 44e99f2

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ trim_trailing_whitespace = true
1010

1111
[Makefile]
1212
indent_style = tab
13+
14+
[*.swift]
15+
indent_size = 4

swift-playgrounds/Metal.playground/Contents.swift

+14-15
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ public class MetalView: NSObject, MTKViewDelegate {
4141
let device: MTLDevice!
4242
let cps: MTLComputePipelineState!
4343
private var startDate: Date = Date()
44-
44+
4545
public init?(mtkView: MTKView) {
4646
view = mtkView
4747
view.clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1)
4848
view.colorPixelFormat = .bgra8Unorm
4949
device = MTLCreateSystemDefaultDevice()!
5050
commandQueue = device.makeCommandQueue()
5151
let library = try! device.makeLibrary(source: source, options: nil)
52-
let function = library.makeFunction(name:"focusedWindowChangedEffect")!
52+
let function = library.makeFunction(name: "focusedWindowChangedEffect")!
5353
cps = try! device.makeComputePipelineState(function: function)
54-
54+
5555
super.init()
5656
view.delegate = self
5757
view.device = device
5858
}
59-
60-
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
61-
59+
60+
public func mtkView(_: MTKView, drawableSizeWillChange _: CGSize) {}
61+
6262
public func draw(in view: MTKView) {
6363
var time = Float(Date().timeIntervalSince(startDate))
64-
65-
if (time > 1) {
64+
65+
if time > 1.0 {
6666
startDate = Date()
6767
}
68-
68+
6969
var color = vector_float3(0.3, 0.2, 1.0) // rgba
7070

7171
if let drawable = view.currentDrawable,
@@ -78,13 +78,13 @@ public class MetalView: NSObject, MTKViewDelegate {
7878
commandEncoder.setBytes(&color, length: MemoryLayout<Float>.size * 3, index: 1)
7979

8080
let w = cps.threadExecutionWidth
81-
let h = cps.maxTotalThreadsPerThreadgroup / w;
81+
let h = cps.maxTotalThreadsPerThreadgroup / w
8282
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)
8686
commandEncoder.dispatchThreads(threadsPerGrid,
87-
threadsPerThreadgroup:threadsPerThreadgroup)
87+
threadsPerThreadgroup: threadsPerThreadgroup)
8888
commandEncoder.endEncoding()
8989
commandBuffer.present(drawable)
9090
commandBuffer.commit()
@@ -97,4 +97,3 @@ let view = MTKView(frame: frame)
9797
let delegate = MetalView(mtkView: view)
9898
view.delegate = delegate
9999
PlaygroundPage.current.liveView = view
100-

0 commit comments

Comments
 (0)