changeset 1193:5aa1184fa5eb compiletime-tests

did: a few more things
author sam <sam@basx.dev>
date Mon, 08 Jul 2024 16:15:33 +0700
parents 90bf0cab1d02
children 397c681f9c0c
files semicongine.nim semicongine/rendering.nim semicongine/rendering/vulkan_wrappers.nim test1.nim
diffstat 4 files changed, 111 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine.nim	Mon Jul 08 16:05:06 2024 +0700
+++ b/semicongine.nim	Mon Jul 08 16:15:33 2024 +0700
@@ -18,5 +18,3 @@
 include semicongine/core
 include semicongine/events
 include semicongine/rendering
-
-initVulkan(REQUIRED_PLATFORM_EXTENSIONS)
--- a/semicongine/rendering.nim	Mon Jul 08 16:05:06 2024 +0700
+++ b/semicongine/rendering.nim	Mon Jul 08 16:15:33 2024 +0700
@@ -19,6 +19,25 @@
 template PassFlat* {.pragma.}
 template ShaderOutput* {.pragma.}
 
+# there is a big, bad global vulkan object
+# believe me, this makes everything much, much easier
+
+include ./platform/window # for NativeWindow
+include ./platform/surface # For CreateNativeSurface
+
+type
+  VulkanGlobals* = object
+    instance*: VkInstance
+    device*: VkDevice
+    physicalDevice*: VkPhysicalDevice
+    surface: VkSurfaceKHR
+    window: NativeWindow
+    graphicsQueueFamily*: uint32
+    graphicsQueue*: VkQueue
+    anisotropy*: float32 = 0 # needs to be enable during device creation
+
+var vulkan*: VulkanGlobals
+
 type
   # type aliases
   SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64]
@@ -121,3 +140,94 @@
 include ./rendering/vulkan_wrappers
 include ./rendering/shaders
 include ./rendering/renderer
+
+
+proc initVulkan(appName: string = "semicongine app") =
+
+  include ./platform/vulkan_extensions # for REQUIRED_PLATFORM_EXTENSIONS
+
+  # instance creation
+  #
+  when not defined(release):
+    let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface", "VK_EXT_debug_utils"]
+    let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[]
+  else:
+    let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"]
+    let layers: seq[string]
+
+  var
+    layersC = allocCStringArray(layers)
+    instanceExtensionsC = allocCStringArray(requiredExtensions)
+  defer:
+    deallocCStringArray(layersC)
+    deallocCStringArray(instanceExtensionsC)
+
+  var
+    appinfo = VkApplicationInfo(
+      sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
+      pApplicationName: appName,
+      pEngineName: "semicongine",
+      apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0),
+    )
+    createinfo = VkInstanceCreateInfo(
+      sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
+      pApplicationInfo: addr(appinfo),
+      enabledLayerCount: layers.len.uint32,
+      ppEnabledLayerNames: layersC,
+      enabledExtensionCount: requiredExtensions.len.uint32,
+      ppEnabledExtensionNames: instanceExtensionsC
+    )
+  checkVkResult vkCreateInstance(addr(createinfo), nil, addr(vulkan.instance))
+  loadVulkan(vulkan.instance)
+
+  # load extensions
+  #
+  for extension in requiredExtensions:
+    loadExtension(vulkan.instance, $extension)
+  vulkan.window = CreateWindow(appName)
+  vulkan.surface = CreateNativeSurface(vulkan.instance, vulkan.window)
+
+  # logical device creation
+
+  # TODO: allowing support for physical devices without hasUniformBufferStandardLayout
+  # would require us to ship different shaders, so we don't support standard layout
+  # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly
+  # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions()
+  # var deviceExtensions  = @["VK_KHR_swapchain", "VK_KHR_uniform_buffer_standard_layout"]
+  var deviceExtensions = @["VK_KHR_swapchain"]
+  for extension in deviceExtensions:
+    loadExtension(vulkan.instance, extension)
+
+  # get physical device and graphics queue family
+  vulkan.physicalDevice = GetBestPhysicalDevice(vulkan.instance)
+  vulkan.graphicsQueueFamily = GetQueueFamily(vulkan.physicalDevice, VK_QUEUE_GRAPHICS_BIT)
+
+  let
+    priority = cfloat(1)
+    queueInfo = VkDeviceQueueCreateInfo(
+      sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
+      queueFamilyIndex: vulkan.graphicsQueueFamily,
+      queueCount: 1,
+      pQueuePriorities: addr(priority),
+    )
+    deviceExtensionsC = allocCStringArray(deviceExtensions)
+  defer: deallocCStringArray(deviceExtensionsC)
+  var createDeviceInfo = VkDeviceCreateInfo(
+    sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
+    queueCreateInfoCount: 1,
+    pQueueCreateInfos: addr(queueInfo),
+    enabledLayerCount: 0,
+    ppEnabledLayerNames: nil,
+    enabledExtensionCount: uint32(deviceExtensions.len),
+    ppEnabledExtensionNames: deviceExtensionsC,
+    pEnabledFeatures: nil,
+  )
+  checkVkResult vkCreateDevice(
+    physicalDevice = vulkan.physicalDevice,
+    pCreateInfo = addr createDeviceInfo,
+    pAllocator = nil,
+    pDevice = addr vulkan.device
+  )
+  vulkan.graphicsQueue = svkGetDeviceQueue(vulkan.device, vulkan.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT)
+
+initVulkan()
--- a/semicongine/rendering/vulkan_wrappers.nim	Mon Jul 08 16:05:06 2024 +0700
+++ b/semicongine/rendering/vulkan_wrappers.nim	Mon Jul 08 16:15:33 2024 +0700
@@ -1,20 +1,3 @@
-include ../platform/vulkan_extensions
-include ../platform/window
-include ../platform/surface
-
-type
-  VulkanGlobals* = object
-    instance*: VkInstance
-    device*: VkDevice
-    physicalDevice*: VkPhysicalDevice
-    surface: VkSurfaceKHR
-    window: NativeWindow
-    graphicsQueueFamily*: uint32
-    graphicsQueue*: VkQueue
-    anisotropy*: float32 = 0 # needs to be enable during device creation
-
-var vulkan*: VulkanGlobals
-
 proc GetBestPhysicalDevice(instance: VkInstance): VkPhysicalDevice =
   var nDevices: uint32
   checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil)
@@ -70,99 +53,6 @@
         return true
   return false
 
-proc initVulkan*(platformLayers: seq[string], appName: string = "semicongine app") =
-
-  # instance creation
-  #
-  when not defined(release):
-    let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface", "VK_EXT_debug_utils"]
-    let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[]
-  else:
-    let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"]
-    let layers: seq[string]
-
-  var
-    layersC = allocCStringArray(layers)
-    instanceExtensionsC = allocCStringArray(requiredExtensions)
-  defer:
-    deallocCStringArray(layersC)
-    deallocCStringArray(instanceExtensionsC)
-
-  var
-    appinfo = VkApplicationInfo(
-      sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
-      pApplicationName: appName,
-      pEngineName: "semicongine",
-      apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0),
-    )
-    createinfo = VkInstanceCreateInfo(
-      sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
-      pApplicationInfo: addr(appinfo),
-      enabledLayerCount: layers.len.uint32,
-      ppEnabledLayerNames: layersC,
-      enabledExtensionCount: requiredExtensions.len.uint32,
-      ppEnabledExtensionNames: instanceExtensionsC
-    )
-  checkVkResult vkCreateInstance(addr(createinfo), nil, addr(vulkan.instance))
-  loadVulkan(vulkan.instance)
-
-  # load extensions
-  #
-  for extension in requiredExtensions:
-    loadExtension(vulkan.instance, $extension)
-  vulkan.window = CreateWindow(appName)
-  vulkan.surface = CreateNativeSurface(vulkan.instance, vulkan.window)
-
-
-
-
-  # logical device creation
-
-
-
-
-  # TODO: allowing support for physical devices without hasUniformBufferStandardLayout
-  # would require us to ship different shaders, so we don't support standard layout
-  # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly
-  # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions()
-  # var deviceExtensions  = @["VK_KHR_swapchain", "VK_KHR_uniform_buffer_standard_layout"]
-  var deviceExtensions = @["VK_KHR_swapchain"]
-  for extension in deviceExtensions:
-    loadExtension(vulkan.instance, extension)
-
-  # get physical device and graphics queue family
-  vulkan.physicalDevice = GetBestPhysicalDevice(vulkan.instance)
-  vulkan.graphicsQueueFamily = GetQueueFamily(vulkan.physicalDevice, VK_QUEUE_GRAPHICS_BIT)
-
-  let
-    priority = cfloat(1)
-    queueInfo = VkDeviceQueueCreateInfo(
-      sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
-      queueFamilyIndex: vulkan.graphicsQueueFamily,
-      queueCount: 1,
-      pQueuePriorities: addr(priority),
-    )
-    deviceExtensionsC = allocCStringArray(deviceExtensions)
-  defer: deallocCStringArray(deviceExtensionsC)
-  var createDeviceInfo = VkDeviceCreateInfo(
-    sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
-    queueCreateInfoCount: 1,
-    pQueueCreateInfos: addr(queueInfo),
-    enabledLayerCount: 0,
-    ppEnabledLayerNames: nil,
-    enabledExtensionCount: uint32(deviceExtensions.len),
-    ppEnabledExtensionNames: deviceExtensionsC,
-    pEnabledFeatures: nil,
-  )
-  checkVkResult vkCreateDevice(
-    physicalDevice = vulkan.physicalDevice,
-    pCreateInfo = addr createDeviceInfo,
-    pAllocator = nil,
-    pDevice = addr vulkan.device
-  )
-  vulkan.graphicsQueue = svkGetDeviceQueue(vulkan.device, vulkan.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT)
-
-
 proc svkGetPhysicalDeviceProperties*(): VkPhysicalDeviceProperties =
   vkGetPhysicalDeviceProperties(vulkan.physicalDevice, addr(result))
 
--- a/test1.nim	Mon Jul 08 16:05:06 2024 +0700
+++ b/test1.nim	Mon Jul 08 16:15:33 2024 +0700
@@ -113,7 +113,7 @@
   createInfo = VkCommandPoolCreateInfo(
     sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
     flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
-    queueFamilyIndex: vulkan.queueFamilyIndex,
+    queueFamilyIndex: vulkan.graphicsQueueFamily,
   )
 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool)
 var