import vulkan
, strutils
, bitops
, ../utils/lets

proc checkValidationLayers*() =
 var layerCount: uint32 = 0
 discard vkEnumerateInstanceLayerProperties(layerCount.addr, nil)
 var layers = newSeq[VkLayerProperties](layerCount)
 discard vkEnumerateInstanceLayerProperties(layerCount.addr, layers[0].addr)

 for validate in validationLayers:
  var found = false
  for layer in layers:
   if cstring(layer.layerName.addr) == validate:
    found = true
    break
   if not found: echo layer.layerName," layer is not supported"

proc debugUtilsMessengerCallback*( messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT
                                 , messageTypes: VkDebugUtilsMessageTypeFlagsEXT
                                 , pCallbackData: VkDebugUtilsMessengerCallbackDataEXT
                                 , userData: pointer
                                 ): VkBool32{.cdecl.} =

 if bitand( messageSeverity.int
          , VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT.int
          ) != 0: 
  discard 
  #echo "VERBOSE: ", "[", pCallbackData.messageIdNumber, "][", pCallbackData.pMessageIdName, "] : ", pCallbackData.pMessage

 elif bitand( messageSeverity.int
            , VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT.int
            ) != 0: discard
  #[ if contains($pCallbackData.pMessage, "/usr")                            or
     contains($pCallbackData.pMessage, "libVkLayer")                      or
     contains($pCallbackData.pMessage, "VUID-NONE")                       or
     pCallbackData.messageIdNumber == 0    #[ [Loader Message] ]#         or
      contains($pCallbackData.pMessage, "Inserted device layer"):           discard
  else:  echo "INFO: "
              , "[",pCallbackData.messageIdNumber
              , "]["
              , pCallbackData.pMessageIdName
              , "] : "
              , pCallbackData.pMessage ]#

 elif bitand( messageSeverity.int
            , VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT.int
            ) != 0: 
  if contains($pCallbackData.pMessage, "small-dedicated-allocation") or
     contains($pCallbackData.pMessage, "CHASSIS") or
     contains($pCallbackData.pMessage, "command-buffer-reset") or
     contains($pCallbackData.pMessage, "small-allocation") or
      # Attempting to enable extension VK_EXT_debug_utils, 
      #but this extension is intended to support use by applications when debugging and it is strongly recommended that it be otherwise avoided.
      pCallbackData.messageIdNumber == 284711830: 
       discard# echo "small allocation warning"
  else: discard#echo "WARNING: ", "[",pCallbackData.messageIdNumber, "][", pCallbackData.pMessageIdName, "] : ", pCallbackData.pMessage

 elif bitand( messageSeverity.int
            , VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT.int
            ) != 0: 
  echo "ERROR: ", "[",pCallbackData.messageIdNumber, "][", pCallbackData.pMessageIdName, "] : ", pCallbackData.pMessage