import vulkan
, vulkan_record
, pipeline_record

, ../scene/[ scene_record
               , scene_controller 
               ]

proc prepare_frame*(rec: var Vulkan_Record
               , scene_record: var Scene_Record
               , pipeline_record: Pipeline_Record
               ) = 
 
 discard vkAcquireNextImageKHR( rec.vk_device
                              , rec.swapChain.handle
                              , uint64.high
                              , rec.presentCompleteSemaphore
                              , VkFence 0
                              , addr rec.currentFrameBuffer
                              )

 discard vkWaitForFences(rec.vk_device, 1, addr rec.fences[rec.currentFrameBuffer], VKBool32 true, uint64.high)
 discard vkResetFences(rec.vk_device, 1, addr rec.fences[rec.currentFrameBuffer])
 #
 

proc submit_frame*( current_frame_buffer: uint32
                 , render_complete_semaphore: VkSemaphore
                 , queue: VkQueue 
                 #, scene: var Scene
                 , present_info: var VkPresentInfoKHR
                 ) = 
 
 #for shape in scene.shapes.mitems: 
  #if shape.changed:  rec.vk_device.updateCamera shape

#[  for text in scene.texts.mitems: 
  if text.changed: 
   text.updateCamera ]#

 # THE M A G I C (presenting) queuePresent
 #[ var presentInfo: VkPresentInfoKHR
 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR
 presentInfo.pNext = nil
 presentInfo.swapchainCount = 1
 presentInfo.pSwapchains = addr rec.swapChain.swapChainKHR ]#
 present_info.pImageIndices = addr current_frame_buffer #rec.currentFrameBuffer
 
 # Check if a wait semaphore has been specified to wait for 
 # before presenting the image
 if (render_complete_semaphore.int != 0):
  presentInfo.pWaitSemaphores = addr render_complete_semaphore
  presentInfo.waitSemaphoreCount = 1
 
  #TODO: WHY DOES THIS MEMORY LEAK EVERY FRAME 
 var preres = vkQueuePresentKHR( queue
                               , addr presentInfo
                               )
 #if preres == VK_SUBOPTIMAL_KHR: quit "bad vkQueuePresentKHR"
  # rec.windowResize scene
  # scene.rebuild rec
  # discard vkDeviceWaitIdle rec.vk_device
  # rec.readyFrame = true

proc draw*(rec: var Vulkan_Record
                  , scene_record: var Scene_Record
                  , pipeline_record: Pipeline_Record
                  ) = 
 
 if rec.readyFrame:
  rec.prepare_frame scene_record, pipeline_record
  scene_record.draw_current_scene rec, pipeline_record
  
  var
   waitStageMask = VkPipelineStageFlags VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
   submitInfo = VkSubmitInfo( sType: VK_STRUCTURE_TYPE_SUBMIT_INFO
                                             , pWaitDstStageMask: addr waitStageMask
                                             , pWaitSemaphores: addr rec.presentCompleteSemaphore
                                             , waitSemaphoreCount: 1
                                             , pSignalSemaphores: addr rec.renderCompleteSemaphore
                                             , commandBufferCount: 1
                                             , pCommandBuffers: addr rec.draw_command_buffers[rec.currentFrameBuffer]
                                             , signalSemaphoreCount: 1
                                             )  
 
  discard vkQueueSubmit( rec.queue
                                         , 1
                                         , submitInfo.addr
                                         , rec.fences[rec.currentFrameBuffer]
                                         )
  
  submit_frame( rec.currentFrameBuffer
                        , rec.renderCompleteSemaphore
                        , rec.queue
                        , scene_record.current_scene[].present_info
                        )
  #rec.readyFrame = false