-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCore.swift
572 lines (440 loc) · 18 KB
/
Core.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//
// Core.swift
// ShaderMania
//
// Created by Markus Moenig on 25/8/20.
//
import MetalKit
import Combine
import AVFoundation
public class Core : ObservableObject
{
enum State {
case Idle, Running, Paused
}
var state : State = .Idle
var view : DMTKView!
var device : MTLDevice!
var nodesView : DMTKView!
var texture : Texture2D? = nil
var metalStates : MetalStates!
var file : File? = nil
var viewportSize : vector_uint2
var scaleFactor : Float
var assetFolder : AssetFolder!
var screenWidth : Float = 0
var screenHeight : Float = 0
var coreCmdQueue : MTLCommandQueue? = nil
var coreCmdBuffer : MTLCommandBuffer? = nil
var coreScissorRect : MTLScissorRect? = nil
var scriptEditor : ScriptEditor? = nil
var shaderCompiler : ShaderCompiler!
var textureLoader : MTKTextureLoader!
var resources : [AnyObject] = []
var availableFonts : [String] = ["OpenSans", "Square", "SourceCodePro"]
var fonts : [Font] = []
var _Time = Float1(0)
var _Aspect = Float2(1,1)
var _Frame = UInt32(0)
var targetFPS : Float = 60
var coreAsset : Asset? = nil
// Preview Size, UI only
var previewFactor : CGFloat = 4
var previewOpacity : Double = 0.5
let updateUI = PassthroughSubject<Void, Never>()
var didSend = false
let timeChanged = PassthroughSubject<Float, Never>()
let createPreview = PassthroughSubject<Void, Never>()
var helpText : String = ""
let helpTextChanged = PassthroughSubject<Void, Never>()
let contentChanged = PassthroughSubject<Void, Never>()
let selectionChanged = PassthroughSubject<Asset?, Never>()
let libraryChanged = PassthroughSubject<LibraryShaderList?, Never>()
var assetError = CompileError()
let coreError = PassthroughSubject<Void, Never>()
var localAudioPlayers: [String:AVAudioPlayer] = [:]
var globalAudioPlayers: [String:AVAudioPlayer] = [:]
var showingHelp : Bool = false
var frameworkId : String? = nil
var project : Project? = nil
var nodesWidget : NodesWidget!
var library : Library!
public init(_ frameworkId: String? = nil)
{
self.frameworkId = frameworkId
viewportSize = vector_uint2( 0, 0 )
#if os(OSX)
scaleFactor = Float(NSScreen.main!.backingScaleFactor)
#else
scaleFactor = Float(UIScreen.main.scale)
#endif
file = File()
assetFolder = AssetFolder()
assetFolder.setup(self)
shaderCompiler = ShaderCompiler(self)
#if os(iOS)
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error {
print(error.localizedDescription)
}
#endif
project = Project()
library = Library(self)
}
public func setupView(_ view: DMTKView)
{
self.view = view
if let metalDevice = MTLCreateSystemDefaultDevice() {
device = metalDevice
if frameworkId != nil {
view.device = device
}
} else {
print("Cannot initialize Metal!")
}
view.core = self
metalStates = MetalStates(self)
textureLoader = MTKTextureLoader(device: device)
/*
for fontName in availableFonts {
let font = Font(name: fontName, core: self)
fonts.append(font)
}*/
view.platformInit()
checkTexture()
library.requestShaders()
}
public func setupNodesView(_ view: DMTKView)
{
view.platformInit()
nodesView = view
view.core = self
nodesWidget = NodesWidget(self)
}
public func load(_ data: Data)
{
if let folder = try? JSONDecoder().decode(AssetFolder.self, from: data) {
assetFolder = folder
}
}
public func start()
{
clearLocalAudio()
clearGlobalAudio()
view.reset()
assetError.error = nil
state = .Running
_Aspect.x = 1
_Aspect.y = 1
state = .Running
view.enableSetNeedsDisplay = false
view.isPaused = false
_Time.x = 0
targetFPS = 60
_Frame = 0
//if let scriptEditor = scriptEditor {
// scriptEditor.setSilentMode(true)
//}
}
func stop()
{
clearLocalAudio()
clearGlobalAudio()
//if let scriptEditor = scriptEditor {
// scriptEditor.setSilentMode(false)
//}
coreAsset = nil
if let scriptEditor = scriptEditor, assetError.error == nil {
scriptEditor.clearAnnotations()
}
state = .Idle
view.isPaused = true
_Time.x = 0
_Frame = 0
//timeChanged.send(_Time.x)
}
@discardableResult func checkTexture() -> Bool
{
if texture == nil || texture!.texture.width != Int(view.frame.width) || texture!.texture.height != Int(view.frame.height) {
if texture == nil {
texture = Texture2D(self)
} else {
texture?.allocateTexture(width: Int(view.frame.width), height: Int(view.frame.height))
}
viewportSize.x = UInt32(texture!.width)
viewportSize.y = UInt32(texture!.height)
screenWidth = Float(texture!.width)
screenHeight = Float(texture!.height)
coreScissorRect = MTLScissorRect(x: 0, y: 0, width: texture!.texture.width, height: texture!.texture.height)
//if let map = currentMap?.map {
// map.setup(core: self)
//}
return true
}
return false
}
public func draw()
{
guard let drawable = view.currentDrawable else {
return
}
if state == .Idle {
if let asset = assetFolder.current {
if asset.type == .Texture {
startDrawing()
if checkTexture() {
createPreview(asset, false)
}
let renderPassDescriptor = view.currentRenderPassDescriptor
renderPassDescriptor?.colorAttachments[0].loadAction = .load
let renderEncoder = coreCmdBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!)
drawTexture(texture!.texture!, renderEncoder: renderEncoder!)
renderEncoder?.endEncoding()
coreCmdBuffer?.present(drawable)
stopDrawing()
return
}
}
} else {
//_Time.x += 1.0 / targetFPS
//timeChanged.send(_Time.x)
}
if let asset = nodesWidget.currentNode {
if let texture = project?.render(assetFolder: assetFolder, device: device, time: _Time.x, frame: _Frame, viewSize: SIMD2<Int>(Int(view.frame.width), Int(view.frame.height)), forAsset: asset) {
project?.stopDrawing()
startDrawing()
let renderPassDescriptor = view.currentRenderPassDescriptor
renderPassDescriptor?.colorAttachments[0].loadAction = .clear
renderPassDescriptor?.colorAttachments[0].clearColor = MTLClearColorMake(0,0,0,0)
let renderEncoder = coreCmdBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!)
drawTexture(texture, renderEncoder: renderEncoder!)
renderEncoder?.endEncoding()
coreCmdBuffer?.present(drawable)
stopDrawing()
if project!.resChanged {
if didSend == false {
didSend = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.updateUI.send()
self.didSend = false
}
}
}
}
}
if state == .Running {
_Time.x += 1.0 / targetFPS
_Frame += 1
}
}
func updateNodePreview()
{
if let asset = nodesWidget.currentNode {
if asset.type == .Shader {
project?.startDrawing(device)
project?.render(assetFolder: assetFolder, device: device, time: _Time.x, frame: _Frame, viewSize: SIMD2<Int>(80, 80), forAsset: asset, preview: true)
project?.stopDrawing()
} else
if asset.type == .Image {
if asset.previewTexture == nil {
project?.createImageTexture(asset, preview: true, device: device)
}
}
}
}
func startDrawing()
{
if coreCmdQueue == nil {
coreCmdQueue = view.device!.makeCommandQueue()
}
coreCmdBuffer = coreCmdQueue!.makeCommandBuffer()
}
func stopDrawing(deleteQueue: Bool = false)
{
coreCmdBuffer?.commit()
coreCmdBuffer?.waitUntilCompleted()
if deleteQueue {
self.coreCmdQueue = nil
}
self.coreCmdBuffer = nil
}
/// Create a preview for the current asset
func createPreview(_ asset: Asset,_ update: Bool = true)
{
if state == .Idle {
//clearLocalAudio()
if asset.type == .Shader {
updateOnce()
} else
if asset.type == .Audio {
do {
let player = try AVAudioPlayer(data: asset.data[0])
localAudioPlayers[asset.name] = player
player.play()
} catch let error {
print(error.localizedDescription)
}
} else if asset.type == .Image {
if asset.dataIndex < asset.data.count {
if asset.texture == nil {
project?.createImageTexture(asset, preview: false, device: device)
}
if let texture = asset.texture {
let texture2D = Texture2D(self, texture: texture)
self.startDrawing()
var options : [String:Any] = [:]
options["texture"] = texture2D
//let width : Float = texture2D.width * Float(asset.dataScale)
//let height : Float = texture2D.height * Float(asset.dataScale)
options["width"] = view.frame.width
options["height"] = view.frame.height
self.texture?.clear()
self.texture?.drawTexture(options)
self.stopDrawing()
if update {
self.updateOnce()
}
if let scriptEditor = self.scriptEditor {
let text = """
Displaying image \"\(asset.name)\"
Image resolution \(Int(texture2D.width))x\(Int(texture2D.height))
"""
scriptEditor.setAssetValue(asset, value: text)
}
}
}
} else if asset.type == .Texture {
checkTexture()
if asset.data.count == 0 {
if let scriptEditor = self.scriptEditor {
let text = """
This texture will be automatically set to the output resolution.
"""
scriptEditor.setAssetValue(asset, value: text)
if update {
startDrawing()
}
self.texture?.clear()
if update {
stopDrawing()
updateOnce()
}
}
} else {
let data = asset.data[0]
let texOptions: [MTKTextureLoader.Option : Any] = [.generateMipmaps: false, .SRGB: false]
if let texture = try? textureLoader.newTexture(data: data, options: texOptions) {
let texture2D = Texture2D(self, texture: texture)
if update {
startDrawing()
}
var options : [String:Any] = [:]
options["texture"] = texture2D
let width : Float = texture2D.width * Float(asset.dataScale)
let height : Float = texture2D.height * Float(asset.dataScale)
options["width"] = width
options["height"] = height
//self.texture?.clear()
self.texture?.drawTexture(options)
if update {
stopDrawing()
updateOnce()
}
if let scriptEditor = self.scriptEditor {
let text = """
Displaying image for texture \(asset.name)
Image resolution \(Int(texture2D.width)) x \(Int(texture2D.height))
"""
scriptEditor.setAssetValue(asset, value: text)
}
}
}
}
}
}
/// Clears all local audio
func clearLocalAudio()
{
for (_, a) in localAudioPlayers {
a.stop()
}
localAudioPlayers = [:]
}
/// Clears all global audio
func clearGlobalAudio()
{
for (_, a) in globalAudioPlayers {
a.stop()
}
globalAudioPlayers = [:]
}
/// Updates the display once
func updateOnce()
{
self.view.enableSetNeedsDisplay = true
#if os(OSX)
let nsrect : NSRect = NSRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.setNeedsDisplay(nsrect)
#else
self.view.setNeedsDisplay()
#endif
}
func drawTexture(_ texture: MTLTexture, renderEncoder: MTLRenderCommandEncoder)
{
let width : Float = Float(texture.width)
let height: Float = Float(texture.height)
var settings = TextureUniform()
settings.screenSize.x = Float(texture.width)//screenWidth
settings.screenSize.y = Float(texture.height)//screenHeight
settings.pos.x = 0
settings.pos.y = 0
settings.size.x = width * scaleFactor
settings.size.y = height * scaleFactor
settings.globalAlpha = 1
let rect = MMRect( 0, 0, width, height, scale: scaleFactor )
let vertexData = createVertexData(texture: texture, rect: rect)
var viewportSize = vector_uint2( UInt32(texture.width), UInt32(texture.height))
renderEncoder.setVertexBytes(vertexData, length: vertexData.count * MemoryLayout<Float>.stride, index: 0)
renderEncoder.setVertexBytes(&viewportSize, length: MemoryLayout<vector_uint2>.stride, index: 1)
renderEncoder.setFragmentBytes(&settings, length: MemoryLayout<TextureUniform>.stride, index: 0)
renderEncoder.setFragmentTexture(texture, index: 1)
renderEncoder.setRenderPipelineState(metalStates.getState(state: .CopyTexture))
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
}
/// Creates vertex data for the given rectangle
func createVertexData(texture: Texture2D, rect: MMRect) -> [Float]
{
let left: Float = -texture.width / 2.0 + rect.x
let right: Float = left + rect.width//self.width / 2 - x
let top: Float = texture.height / 2.0 - rect.y
let bottom: Float = top - rect.height
let quadVertices: [Float] = [
right, bottom, 1.0, 0.0,
left, bottom, 0.0, 0.0,
left, top, 0.0, 1.0,
right, bottom, 1.0, 0.0,
left, top, 0.0, 1.0,
right, top, 1.0, 1.0,
]
return quadVertices
}
/// Creates vertex data for the given rectangle
func createVertexData(texture: MTLTexture, rect: MMRect) -> [Float]
{
let left: Float = -Float(texture.width) / 2.0 + rect.x
let right: Float = left + rect.width//self.width / 2 - x
let top: Float = Float(texture.height) / 2.0 - rect.y
let bottom: Float = top - rect.height
let quadVertices: [Float] = [
right, bottom, 1.0, 0.0,
left, bottom, 0.0, 0.0,
left, top, 0.0, 1.0,
right, bottom, 1.0, 0.0,
left, top, 0.0, 1.0,
right, top, 1.0, 1.0,
]
return quadVertices
}
}