import vulkan
, ../utils/lets
, vkTypes
, vulkan_record 

proc createLogicalDevice*( rec: var Vulkan_Record
                         ) =
 rec.qfi = QueueFamily( graphics: 0
                      , transfer: 0
                      , compute: 0
                      , sparse: 0
                      , usingSparse: false
                      , usingCompute: false
                      )

 var
  queuePriority = 0f
  # qTypes = bitor( VK_QUEUE_GRAPHICS_BIT.int
  #               , VK_QUEUE_TRANSFER_BIT.int
  #               , VK_QUEUE_COMPUTE_BIT.int
  #               , VK_QUEUE_SPARSE_BINDING_BIT.int
  #               )
  queueCreateInfos = newSeq[VkDeviceQueueCreateInfo]()
 
 let deviceQueueCreateInfo = newVkDeviceQueueCreateInfo( sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO
                                                        , queueFamilyIndex = rec.qfi.graphics
                                                        , queueCount = 1
                                                        , pQueuePriorities = queuePriority.addr
                                                        )
 queueCreateInfos.add deviceQueueCreateInfo
  
 var
  deviceFeatures: VkPhysicalDeviceFeatures
  deviceExts = allocCStringArray(coreDeviceExts)

 vkGetPhysicalDeviceFeatures(rec.gpu.handle, addr deviceFeatures)
 var deviceCreateInfo = newVkDeviceCreateInfo( pQueueCreateInfos = queueCreateInfos[0].addr
                                          , queueCreateInfoCount = queueCreateInfos.len.uint32
                                          , pEnabledFeatures = deviceFeatures.addr
                                          , enabledExtensionCount = 1 #coreDeviceExts.len
                                          , enabledLayerCount = 0
                                          , ppEnabledLayerNames = nil
                                          , ppEnabledExtensionNames = deviceExts
                                        )

 if vkCreateDevice( rec.gpu.handle
                  , deviceCreateInfo.addr
                  , nil
                  , addr rec.logical_device
                  ) != VKSuccess: quit "failed to create logical device"

proc create_logical_command_pool*(rec: var Vulkan_Record) =
 var
  poolInfo = VkCommandPoolCreateInfo( sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
                                    , queueFamilyIndex: rec.qfi.graphics
                                   # , flags: VkCommandPoolCreateFlags VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT # optional
                                    )

 if vkCreateCommandPool( rec.logical_device
                       , poolInfo.addr
                       , nil
                       , addr rec.command_pool
                       ) != VK_SUCCESS: 
  quit("failed to create command pool")