changeset 11:3415afff1d1a

Merge /mnt/Users/sam/git/vulkan
author Sam <sam@basx.dev>
date Sat, 24 Dec 2022 22:38:09 +0700
parents 227e53d1a853 (current diff) 0660ba9d1930 (diff)
children 9e5fe647ff91
files src/engine.nim
diffstat 6 files changed, 103 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Dec 24 22:38:03 2022 +0700
+++ b/Makefile	Sat Dec 24 22:38:09 2022 +0700
@@ -1,8 +1,9 @@
 SOURCES := $(shell find src -name '*.nim')
-COMPILE_OPTIONS := --path:src --mm:orc --experimental:strictEffects --threads:on
+COMPILE_OPTIONS := --path:src --mm:orc --experimental:strictEffects --threads:on --app:gui
 DEBUG_OPTIONS := --debugger:native --checks:on --assertions:on
 RELEASE_OPTIONS := -d:release --checks:off --assertions:off
-WINDOWS_OPTIONS := -d:mingw
+WINDOWS_DEBUG_OPTIONS := --cc:vcc --passC:'/MDd' --passL:'ucrtd.lib' 
+WINDOWS_RELEASE_OPTIONS := --cc:vcc --passC:'/MD' --passL:'ucrt.lib' 
 
 # build
 build/debug/linux/test: ${SOURCES}
@@ -11,15 +12,15 @@
 build/release/linux/test: ${SOURCES}
 	mkdir -p $$( dirname $@ )
 	nim c ${COMPILE_OPTIONS} ${RELEASE_OPTIONS} -o:$@ examples/test.nim
-build/debug/windows/test:  ${SOURCES}
+build/debug/windows/test.exe:  ${SOURCES}
 	mkdir -p $$( dirname $@ )
-	nim c ${COMPILE_OPTIONS} ${DEBUG_OPTIONS} ${WINDOWS_OPTIONS} -o:$@ examples/test.nim
-build/release/windows/test: ${SOURCES}
+	nim c ${COMPILE_OPTIONS} ${DEBUG_OPTIONS} ${WINDOWS_DEBUG_OPTIONS} -o:$@ examples/test.nim
+build/release/windows/test.exe: ${SOURCES}
 	mkdir -p $$( dirname $@ )
-	nim c ${COMPILE_OPTIONS} ${RELEASE_OPTIONS} ${WINDOWS_OPTIONS} -o:$@ examples/test.nim
+	nim c ${COMPILE_OPTIONS} ${RELEASE_OPTIONS} ${WINDOWS_RELEASE_OPTIONS} -o:$@ examples/test.nim
 
 build_all_linux: build/debug/linux/test build/release/linux/test
-build_all_windows: build/debug/windows/test build/release/windows/test
+build_all_windows: build/debug/windows/test.exe build/release/windows/test.exe
 
 build_all: build_all_linux build_all_windows
 
--- a/src/engine.nim	Sat Dec 24 22:38:03 2022 +0700
+++ b/src/engine.nim	Sat Dec 24 22:38:09 2022 +0700
@@ -12,6 +12,7 @@
 import ./glslang/glslang
 
 const MAX_FRAMES_IN_FLIGHT = 2
+const DEBUG_LOG = not defined(release)
 
 var logger = newConsoleLogger()
 addHandler(logger)
@@ -137,7 +138,7 @@
       height: min(max(uint32(height), capabilities.minImageExtent.height), capabilities.maxImageExtent.height),
     )
 
-when not defined(release):
+when DEBUG_LOG:
   proc setupDebugLog(instance: VkInstance): VkDebugUtilsMessengerEXT =
     var createInfo = VkDebugUtilsMessengerCreateInfoEXT(
       sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
@@ -422,8 +423,6 @@
     vulkan.frameDimension
   )
 
-  # createFramebuffers();
-
 
 proc setupCommandBuffers(device: VkDevice, graphicsQueueFamily: uint32): (VkCommandPool, array[MAX_FRAMES_IN_FLIGHT, VkCommandBuffer]) =
   # set up command buffer
@@ -465,11 +464,12 @@
   vkLoad1_0()
   vkLoad1_1()
   vkLoad1_2()
+
   checkGlslangResult glslang_initialize_process()
 
   # create vulkan instance
   result.vulkan.instance = createVulkanInstance(VULKAN_VERSION)
-  when not defined(release):
+  when DEBUG_LOG:
     result.vulkan.debugMessenger = result.vulkan.instance.setupDebugLog()
   result.vulkan.surface = result.vulkan.instance.createVulkanSurface(result.window)
   result.vulkan.device = result.vulkan.instance.setupVulkanDeviceAndQueues(result.vulkan.surface)
@@ -638,7 +638,7 @@
 
   engine.vulkan.instance.vkDestroySurfaceKHR(engine.vulkan.surface, nil)
   engine.vulkan.device.device.vkDestroyDevice(nil)
-  when ENABLEVULKANVALIDATIONLAYERS:
+  when DEBUG_LOG:
     engine.vulkan.instance.vkDestroyDebugUtilsMessengerEXT(engine.vulkan.debugMessenger, nil)
   glslang_finalize_process()
   engine.window.trash()
--- a/src/glslang/glslang_c_interface.nim	Sat Dec 24 22:38:03 2022 +0700
+++ b/src/glslang/glslang_c_interface.nim	Sat Dec 24 22:38:09 2022 +0700
@@ -13,28 +13,55 @@
 
 
 # required to link the GLSL compiler
-{.passl: &"-Lthirdparty/lib/glslang/{platform}_{libversion}" .}
-{.passl: &"-Lthirdparty/lib/spirv-tools/{platform}_{libversion}" .}
+when defined(linux):
+  {.passl: &"-Lthirdparty/lib/glslang/{platform}_{libversion}" .}
+  {.passl: &"-Lthirdparty/lib/spirv-tools/{platform}_{libversion}" .}
+  {.passl: "-lglslang" .}
+  {.passl: "-lglslang-default-resource-limits" .}
+  {.passl: "-lHLSL" .}
+  {.passl: "-lMachineIndependent" .}
+  {.passl: "-lGenericCodeGen" .}
+  {.passl: "-lOSDependent" .}
+  {.passl: "-lOGLCompiler" .}
+  {.passl: "-lSPIRV" .}
+  {.passl: "-lSPIRV-Tools-opt" .}
+  {.passl: "-lSPIRV-Tools" .}
+  {.passl: "-lSPIRV-Tools-diff" .}
+  {.passl: "-lSPIRV-Tools-fuzz" .}
+  {.passl: "-lSPIRV-Tools-link" .}
+  {.passl: "-lSPIRV-Tools-lint" .}
+  {.passl: "-lSPIRV-Tools-opt" .}
+  {.passl: "-lSPIRV-Tools-reduce" .}
 
-{.passl: "-lglslang" .}
-{.passl: "-lglslang-default-resource-limits" .}
-{.passl: "-lHLSL" .}
-{.passl: "-lMachineIndependent" .}
-{.passl: "-lGenericCodeGen" .}
-{.passl: "-lOSDependent" .}
-{.passl: "-lOGLCompiler" .}
-{.passl: "-lSPIRV" .}
-{.passl: "-lSPIRV-Tools-opt" .}
-{.passl: "-lSPIRV-Tools" .}
-{.passl: "-lSPIRV-Tools-diff" .}
-{.passl: "-lSPIRV-Tools-fuzz" .}
-{.passl: "-lSPIRV-Tools-link" .}
-{.passl: "-lSPIRV-Tools-lint" .}
-{.passl: "-lSPIRV-Tools-opt" .}
-{.passl: "-lSPIRV-Tools-reduce" .}
+  {.passl: "-lstdc++" .}
+  {.passl: "-lm" .}
+when defined(windows):
+  when libversion == "release":
+    const LIB_POSTFIX = ".lib"
+  when libversion == "debug":
+    const LIB_POSTFIX = "d.lib"
+  
+  {.passl: "/link" .}
+  {.passl: &"/LIBPATH:./thirdparty/lib/glslang/{platform}_{libversion}" .}
+  {.passl: &"/LIBPATH:./thirdparty/lib/spirv-tools/{platform}_{libversion}" .}
+  {.passl: "glslang" & LIB_POSTFIX .}
+  {.passl: "glslang-default-resource-limits" & LIB_POSTFIX .}
+  {.passl: "HLSL" & LIB_POSTFIX .}
+  {.passl: "MachineIndependent" & LIB_POSTFIX .}
+  {.passl: "GenericCodeGen" & LIB_POSTFIX .}
+  {.passl: "OSDependent" & LIB_POSTFIX .}
+  {.passl: "OGLCompiler" & LIB_POSTFIX .}
+  {.passl: "SPIRV" & LIB_POSTFIX .}
+  {.passl: "SPIRV-Tools-opt.lib" .}
+  {.passl: "SPIRV-Tools.lib" .}
+  {.passl: "SPIRV-Tools-diff.lib" .}
+  {.passl: "SPIRV-Tools-fuzz.lib" .}
+  {.passl: "SPIRV-Tools-link.lib" .}
+  {.passl: "SPIRV-Tools-lint.lib" .}
+  {.passl: "SPIRV-Tools-opt.lib" .}
+  {.passl: "SPIRV-Tools-reduce.lib" .}
 
-{.passl: "-lstdc++" .}
-{.passl: "-lm" .}
+
 
 import
   glslang_c_shader_types
--- a/src/platform/windows/win32.nim	Sat Dec 24 22:38:03 2022 +0700
+++ b/src/platform/windows/win32.nim	Sat Dec 24 22:38:09 2022 +0700
@@ -7,6 +7,8 @@
     hinstance*: HINSTANCE
     hwnd*: HWND
 
+var currentEvents: seq[Event]
+
 template checkWin32Result*(call: untyped) =
   let value = call
   if value != 0:
@@ -15,7 +17,7 @@
 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
   case uMsg
   of WM_DESTROY:
-    discard
+    currentEvents.add(Event(eventType: events.EventType.Quit))
   else:
     return DefWindowProc(hwnd, uMsg, wParam, lParam)
 
@@ -25,12 +27,15 @@
   var
     windowClassName = T"EngineWindowClass"
     windowName = T(title)
-    windowClass = WNDCLASS(
+    windowClass = WNDCLASSEX(
+      cbSize: UINT(WNDCLASSEX.sizeof),
       lpfnWndProc: WindowHandler,
       hInstance: result.hInstance,
       lpszClassName: windowClassName,
     )
-  RegisterClass(addr(windowClass))
+  
+  if(RegisterClassEx(addr(windowClass)) == 0):
+    raise newException(Exception, "Unable to register window class")
 
   result.hwnd = CreateWindowEx(
       DWORD(0),
@@ -44,10 +49,10 @@
       nil
     )
 
-  discard ShowWindow(result.hwnd, 0)
+  discard ShowWindow(result.hwnd, 1)
 
 proc trash*(window: NativeWindow) =
-  PostQuitMessage(0)
+  discard
 
 proc size*(window: NativeWindow): (int, int) =
   var rect: RECT
@@ -55,4 +60,8 @@
   (int(rect.right - rect.left), int(rect.bottom - rect.top))
 
 proc pendingEvents*(window: NativeWindow): seq[Event] =
-  result
+  currentEvents = newSeq[Event]()
+  var msg: MSG
+  while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
+    DispatchMessage(addr(msg))
+  return currentEvents
\ No newline at end of file
--- a/src/vulkan.nim	Sat Dec 24 22:38:03 2022 +0700
+++ b/src/vulkan.nim	Sat Dec 24 22:38:09 2022 +0700
@@ -30,15 +30,7 @@
 if isNil(vkHandleDLL):
   quit("could not load: " & vkDLL)
 
-let vkGetProcAddress = cast[proc(s: cstring): pointer {.stdcall.}](symAddr(vkHandleDLL, "vkGetInstanceProcAddr"))
-if vkGetProcAddress == nil:
-  quit("failed to load `vkGetInstanceProcAddr` from " & vkDLL)
-
 vkGetProc = proc(procName: cstring): pointer {.cdecl.} =
-  when defined(windows):
-    result = vkGetProcAddress(procName)
-    if result != nil:
-      return
   result = symAddr(vkHandleDLL, procName)
   if result == nil:
     raiseInvalidLibrary(procName)
--- a/src/vulkan_helpers.nim	Sat Dec 24 22:38:03 2022 +0700
+++ b/src/vulkan_helpers.nim	Sat Dec 24 22:38:09 2022 +0700
@@ -2,24 +2,24 @@
 import std/strutils
 import std/strformat
 import std/logging
+import std/macros
 
 import ./glslang/glslang
 import ./vulkan
 import ./window
 
-when defined(release):
-  const ENABLEVULKANVALIDATIONLAYERS* = false
-else:
-  const ENABLEVULKANVALIDATIONLAYERS* = true
+
+const ENABLEVULKANVALIDATIONLAYERS* = not defined(release)
 
 
 template checkVkResult*(call: untyped) =
   when defined(release):
     discard call
   else:
-    debug(&"CALLING vulkan: {astToStr(call)}")
+    debug "CALLING vulkan: ", astToStr(call)
     let value = call
     if value != VK_SUCCESS:
+      error "Vulkan error: ",  astToStr(call),  " returned ", $value
       raise newException(Exception, "Vulkan error: " & astToStr(call) & " returned " & $value)
 
 func addrOrNil[T](obj: var openArray[T]): ptr T =
@@ -128,17 +128,27 @@
 
 
 proc createVulkanInstance*(vulkanVersion: uint32): VkInstance =
-  var requiredExtensions = [
-    "VK_EXT_acquire_xlib_display".cstring,
-    "VK_EXT_direct_mode_display".cstring,
-    "VK_KHR_display".cstring,
-    "VK_KHR_surface".cstring,
-    "VK_KHR_xlib_surface".cstring,
-    "VK_EXT_debug_utils".cstring,
-  ]
+
+  when defined(linux):
+    var requiredExtensions = [
+      # "VK_EXT_acquire_xlib_display".cstring,
+      # "VK_EXT_direct_mode_display".cstring,
+      # "VK_KHR_display".cstring,
+      "VK_KHR_surface".cstring,
+      "VK_KHR_xlib_surface".cstring,
+      "VK_EXT_debug_utils".cstring,
+    ]
+  when defined(windows):
+    var requiredExtensions = [
+      "VK_KHR_win32_surface".cstring,
+      #"VK_KHR_display".cstring,
+      "VK_KHR_surface".cstring,
+      "VK_EXT_debug_utils".cstring,
+    ]
+  
   let availableExtensions = getInstanceExtensions()
   for extension in requiredExtensions:
-    assert $extension in availableExtensions
+    assert $extension in availableExtensions, $extension
 
   let desiredLayers = ["VK_LAYER_KHRONOS_validation".cstring, "VK_LAYER_MESA_overlay".cstring]
   let availableLayers = getValidationLayers()
@@ -171,7 +181,10 @@
   checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result))
 
   loadVK_KHR_surface()
-  loadVK_KHR_xlib_surface()
+  when defined(linux):
+    loadVK_KHR_xlib_surface()
+  when defined(windows):
+    loadVK_KHR_win32_surface()
   loadVK_KHR_swapchain()
   when ENABLEVULKANVALIDATIONLAYERS:
     loadVK_EXT_debug_utils(result)