annotate semiconginev2/old/engine.nim @ 1230:51221494caeb

fix: uncomment all tests
author sam <sam@basx.dev>
date Fri, 19 Jul 2024 04:49:18 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
996
d91a810db920 add: default application name
sam <sam@basx.dev>
parents: 993
diff changeset
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
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 1009
diff changeset
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
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
24 import ./panel
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
26 import ./steam
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
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
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
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:
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
53 engine.renderer.get.Destroy()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
54 engine.device.Destroy()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
55 if engine.debugger.messenger.Valid:
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
56 engine.debugger.Destroy()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
57 engine.window.Destroy()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
58 engine.instance.Destroy()
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
59 if SteamAvailable():
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
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
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
63 proc InitEngine*(
996
d91a810db920 add: default application name
sam <sam@basx.dev>
parents: 993
diff changeset
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
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
72 TrySteamInit()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
73 if SteamAvailable():
993
f9b82b0740cb did: mini adjustment of log output
sam <sam@basx.dev>
parents: 973
diff changeset
74 echo "Starting with Steam"
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
75 else:
993
f9b82b0740cb did: mini adjustment of log output
sam <sam@basx.dev>
parents: 973
diff changeset
76 echo "Starting without Steam"
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents: 963
diff changeset
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
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
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
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:
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
100 result.debugger = result.instance.CreateDebugMessenger()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 # create devices
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
102 let selectedPhysicalDevice = result.instance.GetPhysicalDevices().FilterBestGraphics()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
103 result.device = result.instance.CreateDevice(
840
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 = @[],
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
106 selectedPhysicalDevice.FilterForGraphicsPresentationQueues()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
107 )
1023
73db28170930 did: change style for public symbols
sam <sam@basx.dev>
parents: 1022
diff changeset
108 StartMixerThread()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
109
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
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
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1023
diff changeset
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,
1167
cdf7ec8d04d2 add: MSAA, yipieee
sam <sam@basx.dev>
parents: 1157
diff changeset
117 samples = VK_SAMPLE_COUNT_1_BIT,
949
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
118 ) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
119
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 assert not engine.renderer.isSome
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121 var allShaders = @shaders
909
f4bc149a385c add: few string-ify methods, small fix for default-shaders
Sam <sam@basx.dev>
parents: 905
diff changeset
122 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
123 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
124 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
125 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
126 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
127 allShaders.add (TEXT_MATERIAL_TYPE, TEXT_SHADER)
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
128 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
129 shaders = allShaders,
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
130 clearColor = clearColor,
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
131 backFaceCulling = backFaceCulling,
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
132 vSync = vSync,
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
133 inFlightFrames = inFlightFrames,
1167
cdf7ec8d04d2 add: MSAA, yipieee
sam <sam@basx.dev>
parents: 1157
diff changeset
134 samples = samples,
949
0cf68e7336e8 add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents: 943
diff changeset
135 ))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
136
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
137 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
138 checkVkResult engine.device.vk.vkDeviceWaitIdle()
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
139 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
140 checkVkResult engine.device.vk.vkDeviceWaitIdle()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
141
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
142 proc LoadScene*(engine: var Engine, scene: var Scene) =
963
9d3f6d742e65 add: some more debug log
sam <sam@basx.dev>
parents: 956
diff changeset
143 debug &"start loading scene '{scene.name}'"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
144 assert engine.renderer.isSome
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
145 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
146 checkVkResult engine.device.vk.vkDeviceWaitIdle()
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
147 scene.AddShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
148 engine.renderer.get.SetupDrawableBuffers(scene)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
149 engine.renderer.get.UpdateMeshData(scene, forceAll = true)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
150 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
151 checkVkResult engine.device.vk.vkDeviceWaitIdle()
963
9d3f6d742e65 add: some more debug log
sam <sam@basx.dev>
parents: 956
diff changeset
152 debug &"done loading scene '{scene.name}'"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
153
1142
bf9dedd36bed fix: a few things
sam <sam@basx.dev>
parents: 1139
diff changeset
154 proc UnloadScene*(engine: var Engine, scene: Scene) =
963
9d3f6d742e65 add: some more debug log
sam <sam@basx.dev>
parents: 956
diff changeset
155 debug &"unload scene '{scene.name}'"
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
156 engine.renderer.get.Destroy(scene)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
157
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
158 proc RenderScene*(engine: var Engine, scene: var Scene) =
1157
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
159 if WindowIsMinimized():
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
160 return
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
161 assert engine.renderer.isSome, "Renderer has not yet been initialized, call 'engine.InitRenderer' first"
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
162 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
163 let t0 = getMonoTime()
951
c6213a794ab0 fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents: 950
diff changeset
164
1157
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
165 if engine.renderer.get.StartNewFrame():
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
166 scene.SetShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio)
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
167 engine.renderer.get.UpdateMeshData(scene)
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
168 engine.renderer.get.UpdateUniformData(scene)
dd757eb5ca86 fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents: 1153
diff changeset
169 engine.renderer.get.Render(scene)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
170
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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 max = float(engine.lastNRenderTimes[^1]) / 1_000_000
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
182 engine.window.SetTitle(&"{engine.applicationName} ({min:.2}, {median:.2}, {max:.2})")
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
183
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
184
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
185 # 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
186 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
187 func GetWindow*(engine: Engine): auto = engine.window
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
188 func GetAspectRatio*(engine: Engine): float32 = engine.GetWindow().Size[0] / engine.GetWindow().Size[1]
1153
9bdab0729b99 fix: not a function
sam@sambook-windows.localdomain
parents: 1142
diff changeset
189 proc ShowSystemCursor*(engine: Engine) = engine.window.ShowSystemCursor()
9bdab0729b99 fix: not a function
sam@sambook-windows.localdomain
parents: 1142
diff changeset
190 proc HideSystemCursor*(engine: Engine) = engine.window.HideSystemCursor()
1142
bf9dedd36bed fix: a few things
sam <sam@basx.dev>
parents: 1139
diff changeset
191 func Fullscreen*(engine: Engine): bool = engine.fullscreen
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
192 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
193 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
194 engine.fullscreen = enable
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
195 engine.window.Fullscreen(engine.fullscreen)
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 862
diff changeset
196
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
197 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
198 engine.device.physicalDevice.properties.limits
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
199
1168
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
200 func MaxFramebufferSampleCount*(engine: Engine, maxSamples = VK_SAMPLE_COUNT_8_BIT): VkSampleCountFlagBits =
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
201 let available = VkSampleCountFlags(
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
202 engine.Limits.framebufferColorSampleCounts.uint32 and engine.Limits.framebufferDepthSampleCounts.uint32
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
203 ).toEnums
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
204 return min(max(available), maxSamples)
73eaec7e1690 add: function to get max supported sampling count
sam <sam@basx.dev>
parents: 1167
diff changeset
205
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
206 proc UpdateInputs*(engine: Engine): bool =
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
207 UpdateInputs(engine.window.PendingEvents())
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 1009
diff changeset
208
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
209 proc ProcessEvents*(engine: Engine, panel: var Panel) =
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
210 let hasMouseNow = panel.Contains(MousePositionNormalized(engine.window.Size), engine.GetAspectRatio)
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
211
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
212 # enter/leave events
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
213 if hasMouseNow:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
214 if panel.hasMouse:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
215 if panel.onMouseMove != nil: panel.onMouseMove(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
216 else:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
217 if panel.onMouseEnter != nil: panel.onMouseEnter(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
218 else:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
219 if panel.hasMouse:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
220 if panel.onMouseLeave != nil: panel.onMouseLeave(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
221
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
222 # button events
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
223 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
224 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
225 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
226 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
227 if panel.onMouseUp != nil: panel.onMouseUp(panel, MouseReleasedButtons())
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
228
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
229 panel.hasMouse = hasMouseNow