Mercurial > games > semicongine
annotate semicongine/engine.nim @ 1138:02e1d2658ff5
did: more renaming
author | sam <sam@basx.dev> |
---|---|
date | Tue, 04 Jun 2024 22:08:48 +0700 |
parents | 71315636ba82 |
children | 114f395b9144 |
rev | line source |
---|---|
996 | 1 import std/compilesettings |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
2 import std/algorithm |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
3 import std/monotimes |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/options |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
5 import std/strformat |
909
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
6 import std/sequtils |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import std/logging |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 import std/os |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 import ./platform/window |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 import ./core |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 import ./vulkan/instance |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 import ./vulkan/device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 import ./vulkan/physicaldevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 import ./vulkan/shader |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 import ./scene |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 import ./material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 import ./renderer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 import ./audio |
1021 | 22 import ./input |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 import ./text |
894 | 24 import ./panel |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 |
973 | 26 import ./steam |
27 | |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
951
diff
changeset
|
28 const COUNT_N_RENDERTIMES = 199 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
29 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 EngineState* = enum |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 Starting |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 Running |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 Shutdown |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 Engine* = object |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
36 applicationName: string |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
37 showFps: bool |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 device: Device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 debugger: Debugger |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 instance: Instance |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 window: NativeWindow |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 renderer: Option[Renderer] |
859
a55410a08c63
add: better api for fullscreen management
sam <sam@basx.dev>
parents:
840
diff
changeset
|
43 fullscreen: bool |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
44 lastNRenderTimes: array[COUNT_N_RENDERTIMES, int64] |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
45 currentRenderTimeI: int = 0 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 |
941
e3ef8ebdda46
did: cleanup unnecessasry annotations, comments
sam <sam@basx.dev>
parents:
940
diff
changeset
|
47 # forward declarations |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
48 func GetAspectRatio*(engine: Engine): float32 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 |
1138 | 50 proc Destroy*(engine: var Engine) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 if engine.renderer.isSome: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 engine.renderer.get.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 engine.device.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 if engine.debugger.messenger.valid: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 engine.debugger.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 engine.window.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 engine.instance.destroy() |
973 | 59 if SteamAvailable(): |
60 SteamShutdown() | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 |
1138 | 63 proc InitEngine*( |
996 | 64 applicationName = querySetting(projectName), |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
65 showFps = DEBUG, |
937
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
66 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), |
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
67 vulkanLayers: openArray[string] = [], |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 ): Engine = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 echo "Set log level to ", ENGINE_LOGLEVEL |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 setLogFilter(ENGINE_LOGLEVEL) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 |
973 | 72 TrySteamInit() |
73 if SteamAvailable(): | |
993 | 74 echo "Starting with Steam" |
973 | 75 else: |
993 | 76 echo "Starting without Steam" |
973 | 77 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
78 result.applicationName = applicationName |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
79 result.showFps = showFps |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
80 result.window = createWindow(result.applicationName) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 var |
937
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
83 layers = @vulkanLayers |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 instanceExtensions: seq[string] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
86 if DEBUG: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 instanceExtensions.add "VK_EXT_debug_utils" |
937
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
88 layers.add "VK_LAYER_KHRONOS_validation" |
941
e3ef8ebdda46
did: cleanup unnecessasry annotations, comments
sam <sam@basx.dev>
parents:
940
diff
changeset
|
89 # This stuff might be usefull if we one day to smart GPU memory allocation, |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
90 # but right now it just clobbers up the console log: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 # putEnv("VK_LAYER_ENABLES", "VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT") |
940
459066429a72
fix: last commit was actually only relevant for ARM, also adjust validation outputs
sam <sam@basx.dev>
parents:
937
diff
changeset
|
92 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT") |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 result.instance = result.window.createInstance( |
937
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
95 vulkanVersion = vulkanVersion, |
894 | 96 instanceExtensions = instanceExtensions, |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
97 layers = layers.deduplicate(), |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 ) |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
99 if DEBUG: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 result.debugger = result.instance.createDebugMessenger() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 # create devices |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 let selectedPhysicalDevice = result.instance.getPhysicalDevices().filterBestGraphics() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 result.device = result.instance.createDevice( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 selectedPhysicalDevice, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 enabledExtensions = @[], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 selectedPhysicalDevice.filterForGraphicsPresentationQueues() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 ) |
1023 | 108 StartMixerThread() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 |
1138 | 110 proc InitRenderer*( |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
111 engine: var Engine, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
112 shaders: openArray[(MaterialType, ShaderConfiguration)], |
1136 | 113 clearColor = NewVec4f(0, 0, 0, 0), |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
114 backFaceCulling = true, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
115 vSync = false, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
116 inFlightFrames = 2, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
117 ) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
118 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 assert not engine.renderer.isSome |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 var allShaders = @shaders |
909
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
121 if not shaders.mapIt(it[0]).contains(EMPTY_MATERIAL): |
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
122 allShaders.add (EMPTY_MATERIAL, EMPTY_SHADER) |
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
123 if not shaders.mapIt(it[0]).contains(PANEL_MATERIAL_TYPE): |
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
124 allShaders.add (PANEL_MATERIAL_TYPE, PANEL_SHADER) |
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
125 if not shaders.mapIt(it[0]).contains(TEXT_MATERIAL_TYPE): |
f4bc149a385c
add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents:
905
diff
changeset
|
126 allShaders.add (TEXT_MATERIAL_TYPE, TEXT_SHADER) |
1138 | 127 engine.renderer = some(engine.device.InitRenderer( |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
128 shaders = allShaders, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
129 clearColor = clearColor, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
130 backFaceCulling = backFaceCulling, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
131 vSync = vSync, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
132 inFlightFrames = inFlightFrames, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
943
diff
changeset
|
133 )) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
134 |
1138 | 135 proc InitRenderer*(engine: var Engine, clearColor = NewVec4f(0, 0, 0, 0), vSync = false) = |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
951
diff
changeset
|
136 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
1138 | 137 engine.InitRenderer(@[], clearColor, vSync = vSync) |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
951
diff
changeset
|
138 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 |
1138 | 140 proc LoadScene*(engine: var Engine, scene: var Scene) = |
963 | 141 debug &"start loading scene '{scene.name}'" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
142 assert engine.renderer.isSome |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
143 assert not scene.loaded |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
951
diff
changeset
|
144 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
145 scene.addShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
146 engine.renderer.get.setupDrawableBuffers(scene) |
894 | 147 engine.renderer.get.updateMeshData(scene, forceAll = true) |
148 engine.renderer.get.updateUniformData(scene, forceAll = true) | |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
951
diff
changeset
|
149 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
963 | 150 debug &"done loading scene '{scene.name}'" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 |
1138 | 152 proc UnLoadScene*(engine: var Engine, scene: Scene) = |
963 | 153 debug &"unload scene '{scene.name}'" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 engine.renderer.get.destroy(scene) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
155 |
1138 | 156 proc RenderScene*(engine: var Engine, scene: var Scene) = |
157 assert engine.renderer.isSome, "Renderer has not yet been initialized, call 'engine.InitRenderer' first" | |
1009 | 158 assert engine.renderer.get.hasScene(scene), &"Scene '{scene.name}' has not been loaded yet" |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
159 let t0 = getMonoTime() |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
160 |
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
161 engine.renderer.get.startNewFrame() |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
162 scene.setShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 engine.renderer.get.updateMeshData(scene) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
164 engine.renderer.get.updateUniformData(scene) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
165 engine.renderer.get.render(scene) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
166 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
167 if engine.showFps: |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
168 let nanoSecs = getMonoTime().ticks - t0.ticks |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
169 engine.lastNRenderTimes[engine.currentRenderTimeI] = nanoSecs |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
170 inc engine.currentRenderTimeI |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
171 if engine.currentRenderTimeI >= engine.lastNRenderTimes.len: |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
172 engine.currentRenderTimeI = 0 |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
173 engine.lastNRenderTimes.sort |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
174 let |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
175 min = float(engine.lastNRenderTimes[0]) / 1_000_000 |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
176 median = float(engine.lastNRenderTimes[engine.lastNRenderTimes.len div 2]) / 1_000_000 |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
177 max = float(engine.lastNRenderTimes[^1]) / 1_000_000 |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
178 engine.window.setTitle(&"{engine.applicationName} ({min:.2}, {median:.2}, {max:.2})") |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
179 |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
180 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
181 # wrappers for internal things |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
182 func GpuDevice*(engine: Engine): Device = engine.device |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
183 func GetWindow*(engine: Engine): auto = engine.window |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
184 func GetAspectRatio*(engine: Engine): float32 = engine.GetWindow().size[0] / engine.GetWindow().size[1] |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
185 func ShowSystemCursor*(engine: Engine) = engine.window.showSystemCursor() |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
186 func HideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor() |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
187 func Fullscreen*(engine: Engine): bool = engine.fullscreen |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
188 proc `Fullscreen=`*(engine: var Engine, enable: bool) = |
862
e5d7e3f6826e
fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents:
859
diff
changeset
|
189 if enable != engine.fullscreen: |
e5d7e3f6826e
fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents:
859
diff
changeset
|
190 engine.fullscreen = enable |
e5d7e3f6826e
fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents:
859
diff
changeset
|
191 engine.window.fullscreen(engine.fullscreen) |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
862
diff
changeset
|
192 |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
193 func Limits*(engine: Engine): VkPhysicalDeviceLimits = |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
194 engine.device.physicalDevice.properties.limits |
895 | 195 |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
196 proc UpdateInputs*(engine: Engine): bool = |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
197 UpdateInputs(engine.window.pendingEvents()) |
1021 | 198 |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
199 proc ProcessEvents*(engine: Engine, panel: var Panel) = |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
200 let hasMouseNow = panel.contains(MousePositionNormalized(engine.window.size), engine.GetAspectRatio) |
895 | 201 |
202 # enter/leave events | |
203 if hasMouseNow: | |
204 if panel.hasMouse: | |
205 if panel.onMouseMove != nil: panel.onMouseMove(panel) | |
206 else: | |
207 if panel.onMouseEnter != nil: panel.onMouseEnter(panel) | |
208 else: | |
209 if panel.hasMouse: | |
210 if panel.onMouseLeave != nil: panel.onMouseLeave(panel) | |
211 | |
212 # button events | |
213 if hasMouseNow: | |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
214 if MouseWasPressed(): |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
215 if panel.onMouseDown != nil: panel.onMouseDown(panel, MousePressedButtons()) |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
216 if MouseWasReleased(): |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1021
diff
changeset
|
217 if panel.onMouseUp != nil: panel.onMouseUp(panel, MouseReleasedButtons()) |
895 | 218 |
219 panel.hasMouse = hasMouseNow |