-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
590 lines (473 loc) · 18.5 KB
/
main.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// MIT License
//
// Copyright (C) 2018-2024, Tellusim Technologies Inc. https://tellusim.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Tellusim
/*
*/
func create_mesh(_ steps: Vector2u,_ radius: Vector2f,_ texcoord: Float32) -> Mesh {
// create mesh
let mesh = Mesh()
// create vertices
let num_vertices = (steps.x + 1) * (steps.y + 1)
let positions = MeshAttribute(MeshAttribute.Kind.Position, Format.RGBf32, num_vertices)
let normals = MeshAttribute(MeshAttribute.Kind.Normal, Format.RGBf32, num_vertices)
let tangents = MeshAttribute(MeshAttribute.Kind.Tangent, Format.RGBAf32, num_vertices)
let texcoords = MeshAttribute(MeshAttribute.Kind.TexCoord, Format.RGf32, num_vertices)
var vertex = UInt32(0)
let isteps = Vector2f(1.0) / Vector2f(steps)
let aspect = radius.x / radius.y
for j in 0 ... steps.y {
let ty = Float32(j) * isteps.y
let z = -cos(ty * Pi2 - Pi05)
let r = sin(ty * Pi2 - Pi05)
for i in 0 ... steps.x {
let tx = Float32(i) * isteps.x
let x = -sin(tx * Pi2)
let y = cos(tx * Pi2)
positions.set(vertex, Vector3f(x * (r * radius.y + radius.x), y * (r * radius.y + radius.x), z * radius.y))
normals.set(vertex, Vector3f(x * r, y * r, z))
tangents.set(vertex, Vector4f(-y, x, 0.0, 1.0))
texcoords.set(vertex, Vector2f(tx * aspect, ty) * texcoord)
vertex += 1
}
}
let basis = MeshAttribute(MeshAttribute.Kind.Basis, Format.RGBAu32, num_vertices)
basis.packAttributes(normals, tangents, Format.RGBAf16)
// create indices
let num_indices = steps.x * steps.y * 4
let indices_format = (num_vertices < Maxu16) ? Format.Ru16 : Format.Ru32
let indices = MeshIndices(MeshIndices.Kind.Quadrilateral, indices_format, num_indices)
var index = UInt32(0)
for j in 0 ..< steps.y {
for i in 0 ..< steps.x {
vertex = (steps.x + 1) * j + i
indices.set(index, vertex, vertex + 1, vertex + steps.x + 2, vertex + steps.x + 1)
index += 4
}
}
// create geometry
let geometry = MeshGeometry(mesh)
geometry.addAttribute(positions, indices)
geometry.addAttribute(basis, indices)
geometry.addAttribute(normals, indices)
geometry.addAttribute(texcoords, indices)
// geometry bounds
let hsize = Vector3f(Vector2f(radius.x + radius.y), radius.y)
geometry.setBoundBox(BoundBoxf(min: -hsize, max: hsize))
// create bounds
mesh.createBounds()
return mesh
}
/*
*/
func create_image(_ size: UInt32,_ frame: UInt32) -> Image {
// create image
let image = Image()
image.create2D(Format.RGBAu8n, size, Image.Flags.None)
// create sampler
let sampler = ImageSampler(image)
// fill image
for y in 0 ..< size {
for x in 0 ..< size {
let v = Float32(((Int32(x) - Int32(frame ^ y)) ^ (Int32(y) + Int32(frame ^ x))) & 255) / 63.0
let r = UInt32(cos(Pi * 1.0 + v) * 127.5 + 127.5)
let g = UInt32(cos(Pi * 0.5 + v) * 127.5 + 127.5)
let b = UInt32(cos(Pi * 0.0 + v) * 127.5 + 127.5)
sampler.set2D(x, y, ImageColor(r, g, b, 255))
}
}
return image
}
/*
*/
func main_() -> Int32 {
// create app
let app = App()
if !app.create(Platform.Any, UInt32(App.Values.Version.rawValue)) { return 1 }
// create window
let window = Window(app.getPlatform(), app.getDevice())
if !window { return 1 }
window.setSize(app.getWidth(), app.getHeight())
window.setCloseClickedCallback(Window.CloseClickedFunction({() in window.stop() }))
window.setKeyboardPressedCallback(Window.KeyboardPressedFunction({ (key: UInt32, code: UInt32) in
if key == Window.Key.Esc.rawValue { window.stop() }
if key == Window.Key.F12.rawValue {
let image = Image()
if window.grab(image) && image.save("screenshot.png") {
Log.print(Log.Level.Message, "Screenshot\n")
}
}
}))
let title = window.getPlatformName() + " Tellusim::Swift"
if !window.create(title) || !window.setHidden(false) { return 1 }
// create device
let device = Device(window)
if !device { return 1 }
// device features
Log.print(Log.Level.Message, "Features: \(device.getFeatures())\n")
Log.print(Log.Level.Message, "Device: \(device.getName())\n")
// build info
Log.print(Log.Level.Message, "Build: \(App.getBuildInfo())\n")
// create target
let target = device.createTarget(window)
if !target{ return 1 }
////////////////////////////////
// core test
////////////////////////////////
let blob = Blob()
blob.writeString(title)
blob.seek(0)
Log.print(Log.Level.Message, "Stream: \(blob.readString())\n")
////////////////////////////////
// bounds test
////////////////////////////////
// 32-bit floating point
var bound_boxf = BoundBoxf(-Vector3f.one, Vector3f.one)
var bound_spheref = BoundSpheref(bound_boxf)
let bound_frustumf = BoundFrustumf(Matrix4x4f.perspective(60.0, 1.0, 0.1, 1000.0), Matrix4x4f.lookAt(Vector3f.one, Vector3f.zero, Vector3f(0.0, 0.0, 1.0)))
Log.print(Log.Level.Message, "\(bound_boxf) \(bound_frustumf.inside(bound_boxf)) \(bound_spheref) \(bound_frustumf.inside(bound_spheref))\n")
bound_boxf = Matrix4x3f.translate(10.0, 0.0, 0.0) * bound_boxf
bound_spheref = Matrix4x4f.translate(10.0, 0.0, 0.0) * bound_spheref
Log.print(Log.Level.Message, "\(bound_boxf) \(bound_frustumf.inside(bound_boxf)) \(bound_spheref) \(bound_frustumf.inside(bound_spheref))\n")
// 64-bit floating point
var bound_boxd = BoundBoxd(-Vector3d.one, Vector3d.one)
var bound_sphered = BoundSphered(bound_boxd)
let bound_frustumd = BoundFrustumd(Matrix4x4d.perspective(60.0, 1.0, 0.1, 1000.0), Matrix4x4d.lookAt(Vector3d.one, Vector3d.zero, Vector3d(0.0, 0.0, 1.0)))
Log.print(Log.Level.Message, "\(bound_boxd) \(bound_frustumd.inside(bound_boxd)) \(bound_sphered) \(bound_frustumd.inside(bound_sphered))\n")
bound_boxd = Matrix4x3d.translate(10.0, 0.0, 0.0) * bound_boxd
bound_sphered = Matrix4x4d.translate(10.0, 0.0, 0.0) * bound_sphered
Log.print(Log.Level.Message, "\(bound_boxd) \(bound_frustumd.inside(bound_boxd)) \(bound_sphered) \(bound_frustumd.inside(bound_sphered))\n")
////////////////////////////////
// platform test
////////////////////////////////
// create pipeline
let pipeline = device.createPipeline()
pipeline.setUniformMask(0, Shader.Mask.Fragment)
pipeline.setColorFormat(window.getColorFormat())
pipeline.setDepthFormat(window.getDepthFormat())
pipeline.addAttribute(Pipeline.Attribute.Position, Format.RGf32, 0, 0, 8)
if !pipeline.loadShaderGLSL(Shader.Kind.Vertex, "main.shader", "VERTEX_SHADER=1") { return 1 }
if !pipeline.loadShaderGLSL(Shader.Kind.Fragment, "main.shader", "FRAGMENT_SHADER=1") { return 1 }
if !pipeline.create() { return 1 }
// geometry vertices
var vertices: [Vector2f] = [ Vector2f(3.0, -1.0), Vector2f(-1.0, -1.0), Vector2f(-1.0, 3.0) ]
// geometry indices
var indices: [UInt16] = [ 0, 1, 2 ]
////////////////////////////////
// interface test
////////////////////////////////
// create canvas
let canvas = Canvas()
// create root controls
let root = ControlRoot(canvas, true)
root.setFontSize(24)
// create rect
let rect = ControlRect(root, CanvasElement.Mode.Texture)
rect.setAlign(Control.Align.Expand)
rect.setFullscreen(true)
// create dialog
let dialog = ControlDialog(root, 1, 8.0, 8.0)
dialog.setUpdatedCallback(ControlDialog.UpdatedFunction({ (dialog: ControlDialog) in
let x = dialog.getPositionX()
let y = dialog.getPositionY()
let width = dialog.getWidth()
let height = dialog.getHeight()
Log.printf(Log.Level.Message, "Dialog Updated %.0f %.0f %.0fx%.0f\n", x, y, width, height)
}))
dialog.setAlign(Control.Align.Center)
dialog.setSize(240.0, 180.0)
// create text
let text = ControlText(dialog, title)
text.setAlign(Control.Align.CenterX)
// create button
let button = ControlButton(dialog, "Button")
button.setClickedCallback(ControlButton.ClickedFunction({ (button: ControlButton) in
Log.print(Log.Level.Message, "\(button.getText()) Clicked\n")
}))
button.setAlign(Control.Align.Expand)
button.setMargin(0.0, 0.0, 0.0, 16.0)
// common parameters
struct CommonParameters {
var transform: Matrix4x4f
var color: Color
var time: Float32
}
var parameters = CommonParameters(
transform: Matrix4x4f.identity,
color: Color(1.0),
time: 0.0
)
// create sliders
let slider_r = ControlSlider(dialog, "R", 2, Float64(parameters.color.r), 0.0, 1.0)
let slider_g = ControlSlider(dialog, "G", 2, Float64(parameters.color.g), 0.0, 1.0)
let slider_b = ControlSlider(dialog, "B", 2, Float64(parameters.color.b), 0.0, 1.0)
slider_r.setChangedCallback(ControlSlider.ChangedFunction({ (slider: ControlSlider) in parameters.color.r = slider.getValuef32() }))
slider_g.setChangedCallback(ControlSlider.ChangedFunction({ (slider: ControlSlider) in parameters.color.g = slider.getValuef32() }))
slider_b.setChangedCallback(ControlSlider.ChangedFunction({ (slider: ControlSlider) in parameters.color.b = slider.getValuef32() }))
slider_r.setAlign(Control.Align.ExpandX)
slider_g.setAlign(Control.Align.ExpandX)
slider_b.setAlign(Control.Align.ExpandX)
////////////////////////////////
// scene test
////////////////////////////////
// main async
var main_async = Async()
if !main_async.initialize() { return 1 }
// process async
let process_async = Async()
if !process_async.initialize() { return 1 }
// manager cache
SceneManager.setShaderCache("shader.cache")
SceneManager.setTextureCache("texture.cache")
// create scene manager
let scene_manager = SceneManager()
if !scene_manager.create(device, SceneManager.Flags.DefaultFlags, SceneManager.CreateFunction({ (progress: UInt32) in
Log.printf(Log.Level.Message, "SceneManager %u%% \r", progress)
})) { return 1 }
Log.print("\n")
// process thread
class ProcessThread : Thread {
init(_ manager: SceneManager, _ async : Async) {
self.manager = manager
self.async = async
super.init()
}
override func main() {
while manager.isValidPtr() && !manager.isTerminated() {
if !manager.process(async) { Time.sleep(1000) }
}
Log.print(Log.Level.Message, "Thread Done\n")
}
var manager: SceneManager
var async: Async
}
let thread = ProcessThread(scene_manager, process_async)
thread.start()
////////////////////////////////
// render test
////////////////////////////////
// create render manager
let render_manager = RenderManager(scene_manager)
render_manager.setDrawParameters(device, window.getColorFormat(), window.getDepthFormat(), window.getMultisample())
if !render_manager.create(device, RenderManager.Flags.DefaultFlags, RenderManager.CreateFunction({ (progress: UInt32) in
Log.printf(Log.Level.Message, "RenderManager %u%% \r", progress)
})) { return 1 }
Log.print("\n")
// create render frame
let render_frame = RenderFrame(render_manager)
// render resources
let render_renderer = render_manager.getRenderer()
let render_spatial = render_manager.getSpatial()
////////////////////////////////
// scene test
////////////////////////////////
// create scene
let scene = Scene(scene_manager, render_renderer.getSceneRender())
// create graph
let graph = Graph(scene)
// create camera
let camera_position = Vector3d(12.0, 12.0, 6.0)
let camera = CameraPerspective(scene)
let node_camera = NodeCamera(graph, camera)
node_camera.setGlobalTransform(Matrix4x3d.placeTo(camera_position, Vector3d(0.0), Vector3d(0.0, 0.0, 1.0)))
render_frame.setCamera(node_camera)
// create light
let light = LightPoint(scene)
let node_light = NodeLight(graph, light)
node_light.setGlobalTransform(Matrix4x3d.translate(camera_position))
light.setIntensity(100.0)
light.setRadius(1000.0)
// create material
let metallic_material = MaterialMetallic(scene)
var material = Material(metallic_material)
material.setUniform("roughness_scale", 0.2)
material.setUniform("metallic_scale", 0.0)
// create object
let object_mesh = ObjectMesh(scene)
let node_object = NodeObject(graph, object_mesh)
let mesh = create_mesh(Vector2u(64, 32), Vector2f(8.0, 2.0), 2.0)
object_mesh.create(mesh, &material)
mesh.destroyPtr()
////////////////////////////////
// main loop
////////////////////////////////
var texture_frame = UInt32(0)
let texture_ifps = 1.0 / 30.0
var texture_time = Time.seconds()
// main loop
window.run(Window.MainLoopFunction({ () -> Bool in
// update window
Window.update()
// render window
if !window.render() { return false }
// update scene
if true {
// update render manager
render_manager.update()
// resize frame
if render_frame.getWidth() != window.getWidth() || render_frame.getHeight() != window.getHeight() {
if !render_frame.create(device, render_renderer, window.getWidth(), window.getHeight()) { return false }
Log.printf(Log.Level.Message, "Frame Resized %ux%u\n", window.getWidth(), window.getHeight())
}
// update diffuse texture
if Time.seconds() - texture_time > texture_ifps {
texture_time += texture_ifps
let image = create_image(256, texture_frame)
material.setTexture("diffuse", "procedural", image)
material.updateScene()
texture_frame += 1
}
// update graph
let time = Time.seconds()
node_object.setGlobalTransform(Matrix4x3d.rotateZ(time * 24.0) * Matrix4x3d.rotateX(time * 16.0))
node_object.updateScene()
graph.updateSpatial()
graph.updateScene()
// update scene
if !scene.create(device, &main_async) { return false }
scene.setTime(time)
scene.update(device)
// update scene manager
if !scene_manager.update(device, main_async) { return false }
}
// dispatch
if true {
let compute = device.createCompute()
// dispatch scene
scene_manager.dispatch(device, compute)
scene.dispatch(device, compute)
// dispatch render (multi-frame test)
render_spatial.dispatchFrame(compute, render_frame)
render_spatial.dispatchObjects(compute, render_frame)
render_renderer.dispatchFrame(compute, render_frame)
}
// draw
if true {
// flush buffers
scene_manager.flush(device)
render_manager.flush(device)
render_frame.flush(device)
// draw deferred
render_renderer.drawDeferred(device, render_frame)
}
// dispatch
if true {
let compute = device.createCompute()
// dispatch render
render_renderer.dispatchLight(device, compute, render_frame)
render_renderer.dispatchOccluder(device, compute, render_frame)
render_renderer.dispatchLuminance(device, compute, render_frame)
render_renderer.dispatchComposite(device, compute, render_frame)
}
// update interface
if true {
// window size
let height = Float32(app.getHeight())
let width = floor(height * Float32(window.getWidth()) / Float32(window.getHeight()))
let mouse_x = width * Float32(window.getMouseX()) / Float32(window.getWidth())
let mouse_y = height * Float32(window.getMouseY()) / Float32(window.getHeight())
// mouse button
var buttons = Control.Button.None
if (window.getMouseButtons() & Window.Button.Left) != Window.Button.None { buttons |= Control.Button.Left }
if (window.getMouseButtons() & Window.Button.Left2) != Window.Button.None { buttons |= Control.Button.Left }
// render texture
rect.setTexture(render_frame.getCompositeTexture(), true)
rect.setTextureScale(width / Float32(window.getWidth()), height / Float32(window.getHeight()))
rect.setTextureFlip(false, render_renderer.isTargetFlipped())
// update controls
root.setViewport(width, height)
root.setMouse(mouse_x, mouse_y, buttons)
while root.update(canvas.getScale(target)) { }
// create canvas
canvas.create(device, target)
}
// window target
if target.begin() {
let command = device.createCommand(target)
// current time
let time = Float32(Time.seconds())
// common parameters
parameters.transform = Matrix4x4f.rotateZ(time * 16.0)
parameters.time = time
// draw background
command.setPipeline(pipeline)
command.setUniform(0, ¶meters)
command.setVertices(0, &vertices)
command.setIndices(&indices)
command.drawElements(3)
// draw canvas
canvas.draw(command, target)
}
target.end()
// present window
if !window.present() { return false }
// check device
if !device.check() { return false }
return true
}))
// stop process thread
scene_manager.terminate()
// finish context
window.finish()
// clear scene
scene.clear()
scene_manager.update(device, main_async)
window.finish()
// destroy resources
scene.destroyPtr()
render_frame.destroyPtr()
render_manager.destroyPtr()
scene_manager.destroyPtr()
root.destroyPtr()
// done
Log.print(Log.Level.Message, "Done\n")
return 0
}
/*
*/
#if IOS
import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Log.print(Log.Level.Verbose, "didFinishLaunchingWithOptions(): is called\n")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
chdir(Bundle.main.bundlePath)
Log.printf(Log.Level.Message, "main: %d\n", main_())
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
Log.print(Log.Level.Verbose, "applicationWillResignActive(): is called\n")
}
func applicationDidEnterBackground(_ application: UIApplication) {
Log.print(Log.Level.Verbose, "applicationDidEnterBackground(): is called\n")
}
func applicationWillEnterForeground(_ application: UIApplication) {
Log.print(Log.Level.Verbose, "applicationWillEnterForeground(): is called\n")
}
func applicationDidBecomeActive(_ application: UIApplication) {
Log.print(Log.Level.Verbose, "applicationDidBecomeActive(): is called\n")
}
}
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
#else
Log.printf(Log.Level.Message, "main: %d\n", main_())
#endif