@@ -5,21 +5,18 @@ import MetalKit
5
5
let MaxBuffers = 3
6
6
7
7
public struct Constants {
8
- public init ( ) {
9
-
10
- }
8
+ public init ( ) { }
11
9
}
12
10
13
11
class Renderer : NSObject , MTKViewDelegate {
14
-
15
12
var device : MTLDevice !
16
13
var queue : MTLCommandQueue !
17
14
var pipeline : MTLRenderPipelineState !
18
15
var source = " "
19
16
public var constants = Constants ( )
20
17
21
18
private let inflightSemaphore = DispatchSemaphore ( value: MaxBuffers)
22
-
19
+
23
20
var minBuffers : [ MTLBuffer ] = [ ]
24
21
var maxBuffers : [ MTLBuffer ] = [ ]
25
22
@@ -30,9 +27,9 @@ class Renderer: NSObject, MTKViewDelegate {
30
27
init ( device: MTLDevice ) {
31
28
self . device = device
32
29
queue = device. makeCommandQueue ( )
33
-
30
+
34
31
let library = try ! device. makeDefaultLibrary ( bundle: Bundle . module)
35
-
32
+
36
33
let rpd = MTLRenderPipelineDescriptor ( )
37
34
rpd. vertexFunction = library. makeFunction ( name: " waveform_vert " )
38
35
rpd. fragmentFunction = library. makeFunction ( name: " waveform_frag " )
@@ -46,37 +43,34 @@ class Renderer: NSObject, MTKViewDelegate {
46
43
colorAttachment. destinationAlphaBlendFactor = . oneMinusSourceAlpha
47
44
48
45
pipeline = try ! device. makeRenderPipelineState ( descriptor: rpd)
49
-
46
+
50
47
super. init ( )
51
48
}
52
49
53
- func mtkView( _ view : MTKView , drawableSizeWillChange size : CGSize ) {
50
+ func mtkView( _: MTKView , drawableSizeWillChange _ : CGSize ) { }
54
51
55
- }
56
-
57
52
func selectBuffers( width: CGFloat ) -> ( MTLBuffer , MTLBuffer ) {
58
-
59
53
var level = 0
60
54
for (minBuffer, maxBuffer) in zip ( minBuffers, maxBuffers) {
61
55
if CGFloat ( minBuffer. length / MemoryLayout< Float> . size) < width {
62
56
return ( minBuffer, maxBuffer)
63
57
}
64
58
level += 1
65
59
}
66
-
60
+
67
61
return ( minBuffers. last!, maxBuffers. last!)
68
62
}
69
-
63
+
70
64
func encode( to commandBuffer: MTLCommandBuffer ,
71
65
pass: MTLRenderPassDescriptor ,
72
- width: CGFloat ) {
73
-
66
+ width: CGFloat )
67
+ {
74
68
pass. colorAttachments [ 0 ] . clearColor = MTLClearColorMake ( 0 , 0 , 0 , 0 )
75
69
76
70
let highestResolutionCount = Float ( lastSamples. samples. count)
77
71
let startFactor = Float ( start) / highestResolutionCount
78
72
let lengthFactor = Float ( length) / highestResolutionCount
79
-
73
+
80
74
let ( minBuffer, maxBuffer) = selectBuffers ( width: width / CGFloat( lengthFactor) )
81
75
let enc = commandBuffer. makeRenderCommandEncoder ( descriptor: pass) !
82
76
enc. setRenderPipelineState ( pipeline)
@@ -93,11 +87,9 @@ class Renderer: NSObject, MTKViewDelegate {
93
87
enc. setFragmentBytes ( c, length: MemoryLayout< Constants> . size, index: 3 )
94
88
enc. drawPrimitives ( type: . triangleStrip, vertexStart: 0 , vertexCount: 4 )
95
89
enc. endEncoding ( )
96
-
97
90
}
98
91
99
92
func draw( in view: MTKView ) {
100
-
101
93
let size = view. frame. size
102
94
let w = Float ( size. width)
103
95
let h = Float ( size. height)
@@ -117,15 +109,12 @@ class Renderer: NSObject, MTKViewDelegate {
117
109
semaphore. signal ( )
118
110
}
119
111
120
-
121
112
if let renderPassDescriptor = view. currentRenderPassDescriptor, let currentDrawable = view. currentDrawable {
122
-
123
113
encode ( to: commandBuffer, pass: renderPassDescriptor, width: size. width)
124
114
125
115
commandBuffer. present ( currentDrawable)
126
116
}
127
117
commandBuffer. commit ( )
128
-
129
118
}
130
119
131
120
func set( samples: SampleBuffer , start: Int , length: Int ) {
@@ -137,10 +126,10 @@ class Renderer: NSObject, MTKViewDelegate {
137
126
lastSamples = samples
138
127
minBuffers. removeAll ( )
139
128
maxBuffers. removeAll ( )
140
-
129
+
141
130
var minSamples = samples. samples
142
131
var maxSamples = samples. samples
143
-
132
+
144
133
var s = samples. samples. count
145
134
while s > 2 {
146
135
minBuffers. append ( device. makeBuffer ( minSamples) !)
0 commit comments