0

Problem:

My current computer only has 2 queue families available, and therefore, I'm trying to make it so that the vulkan program allows for shared queue families yet the program segfaults at the vkCreateDevice function call. I'm very new to Vulkan and am not sure what's wrong with the code.

The only thing that to me doesn't seem completely right would be the assignment of queue_create_infos[queue_quantity].pQueuePriorities but I don't think there would be any other way to do it.. What did I do wrong?

  M_DEBUG("Creating logical device..");

  /* avoid creating additional queues for shared indices */
  b8 present_shares_graphics_queue  = (context->device.graphics_queue_index == context->device.present_queue_index);
  b8 transfer_shares_graphics_queue = (context->device.graphics_queue_index == context->device.transfer_queue_index);
  u32 index_count = 1;

  if (!present_shares_graphics_queue) {
    index_count++;
  }

  if (!transfer_shares_graphics_queue) {
    index_count++;
  }

  u32 indices[32];
  u8 index = 0;

  indices[index++] = context->device.graphics_queue_index;
  if (!present_shares_graphics_queue) {
    indices[index++] = context->device.present_queue_index;
  }

  if (!transfer_shares_graphics_queue) {
    indices[index++] = context->device.transfer_queue_index;
  }
  /* * * * * * * * * * * * * * * * * * * * * * * * * * * */

  VkDeviceQueueCreateInfo queue_create_infos[32];
  u32 queue_quantity = 0;
  for (; queue_quantity < index_count;) {
    u32 shared_queues = 1;
    for (u32 j = queue_quantity; j < index_count; ++j) {
      if (indices[j] == indices[j + 1]) {
        ++shared_queues;
      }
    }
    queue_create_infos[queue_quantity].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    queue_create_infos[queue_quantity].queueFamilyIndex = indices[queue_quantity];
    queue_create_infos[queue_quantity].queueCount = shared_queues;

    queue_create_infos[queue_quantity].flags = 0;
    queue_create_infos[queue_quantity].pNext = 0;
    f32 queue_priority[32][32] = {0.0f};
    queue_create_infos[queue_quantity].pQueuePriorities = (const f32 *)queue_priority[queue_quantity];
    queue_quantity += shared_queues;
  }

  /* request device features */
  VkPhysicalDeviceFeatures device_features = {0};
  device_features.samplerAnisotropy        = VK_TRUE; /* request anisotropy */
  /* * * * * * * * * * * * * */

  /* create device */
  VkDeviceCreateInfo device_create_info      = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO};
  device_create_info.queueCreateInfoCount    = queue_quantity;
  device_create_info.pQueueCreateInfos       = queue_create_infos;
  device_create_info.pEnabledFeatures        = &device_features;
  device_create_info.enabledExtensionCount   = 1;

  const char * extension_names               = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
  device_create_info.ppEnabledExtensionNames = &extension_names;

  /* deprecated and ignored  */
  device_create_info.enabledLayerCount   = 0;
  device_create_info.ppEnabledLayerNames = 0;
  /* * * * * * * * * * * * * */

  /* segmentation fault here */

  VK_CHECK(vkCreateDevice(context->device.physical_device,
                          &device_create_info,
                          context->allocator,
                          &context->device.logical_device));
  M_INFO("Successfully created logical device.");
  /* * * * * * * * */

vulkaninfo queue family output:

VkQueueFamilyProperties:
========================
    queueProperties[0]:
    -------------------
        minImageTransferGranularity = (1,1,1)
        queueCount                  = 1
        queueFlags                  = QUEUE_GRAPHICS | QUEUE_COMPUTE | QUEUE_TRANSFER | QUEUE_SPARSE_BINDING
        timestampValidBits          = 64
        present support             = true

    queueProperties[1]:
    -------------------
        minImageTransferGranularity = (1,1,1)
        queueCount                  = 4
        queueFlags                  = QUEUE_COMPUTE | QUEUE_TRANSFER | QUEUE_SPARSE_BINDING
        timestampValidBits          = 64
        present support             = true
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
mango
  • 1
  • You should look hard at `queue_priority`. Also, why are you creating *thirty two* queues? – Nicol Bolas Oct 07 '22 at 19:30
  • Also, what is a "shared queue family"? – Nicol Bolas Oct 07 '22 at 19:31
  • I chose the number 32 randomly just so it's enough to store all floats. I'm not sure whether that could be causing the problem though. – mango Oct 07 '22 at 19:35
  • Also, I consider a "shared queue family" to be a normal queue family used by more than one queues. – mango Oct 07 '22 at 19:36
  • So... do you intend to *use* all of the queues that a queue family might support? If not, why ask for them? – Nicol Bolas Oct 07 '22 at 19:40
  • I suppose I could change `VkDeviceQueueCreateInfo queue_create_infos[32];` to `VkDeviceQueueCreateInfo queue_create_infos[index_count];` though it doesn't seem to make a difference. – mango Oct 07 '22 at 19:49
  • 2
    I think it has to do with your priorities... but I am not sure, either it might be undefined how an array behaves after the loop in which it was defined terminates, this I don't know. I'd also clarify that this line f32 queue_priority[32][32] = {0.0f}; is correct? After a very quick read on google I would think you should do this insteadf32 queue_priority[32][32] = { {0.0f} }; I am not sure though, worth a try? – Plegeus Oct 07 '22 at 20:56
  • Changing it to `f32 queue_priority[32][32] = { {0.0f} };` does not do anything at all.. – mango Oct 07 '22 at 21:39
  • @mango: I was trying to subtly hint that using a pointer to an array that no longer exists is not a good idea. – Nicol Bolas Oct 07 '22 at 21:53
  • My code seems like a mess and I don't doubt that could be one of the reasons, is there a better way to pick queues? If so, I'd really appreciate it if you could give me an example or point me towards one. Thanks. – mango Oct 07 '22 at 22:17
  • @mango No "could" about it. The `queue_priority` is clearly out of scope at the point the `vkCreateDevice` is called, leading to read after destruction. – krOoze Oct 10 '22 at 11:54
  • @mango If you have a new question, ask a new question, and don't increase the scope of this one. – krOoze Oct 10 '22 at 12:01

0 Answers0