About Social Code
aboutsummaryrefslogtreecommitdiff
path: root/src/frygon/vulkan/fgvk_device.c
blob: 6f36e725070697dfb6c590e4b2d81b5931f58f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright © 2025 Lucas Francisco Fryzek
 * SPDX-License-Identifier: MIT
 */
#include "fgvk_device.h"

#include "fgvk_physical_device.h"
#include "fgvk_cmd_buffer.h"
#include "fgvk_shader.h"
#include "fgvk_queue.h"

VKAPI_ATTR VkResult VKAPI_CALL
fgvk_CreateDevice(VkPhysicalDevice physicalDevice,
                  const VkDeviceCreateInfo *pCreateInfo,
                  const VkAllocationCallbacks *pAllocator,
                  VkDevice *pDevice)
{
   VK_FROM_HANDLE(fgvk_physical_device, pdev, physicalDevice);
   VkResult result = VK_ERROR_OUT_OF_HOST_MEMORY;
   struct fgvk_device *dev;

   dev = vk_zalloc2(&pdev->vk.instance->alloc, pAllocator,
                    sizeof(*dev), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);

   if (!dev)
      return vk_error(pdev, VK_ERROR_OUT_OF_HOST_MEMORY);

   struct vk_device_dispatch_table dispatch_table;
   vk_device_dispatch_table_from_entrypoints(&dispatch_table,
                                             &fgvk_device_entrypoints, true);
   //vk_device_dispatch_table_from_entrypoints(&dispatch_table,
   //                                          &wsi_device_entrypoints, false);

   result = vk_device_init(&dev->vk, &pdev->vk, &dispatch_table,
                           pCreateInfo, pAllocator);

   if (result != VK_SUCCESS)
      goto fail_alloc;

   dev->vk.shader_ops = &fgvk_device_shader_ops;
   dev->vk.command_buffer_ops = &fgvk_cmd_buffer_ops;

   for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
      for (unsigned q = 0; q < pCreateInfo->pQueueCreateInfos[i].queueCount; q++) {
         result = fgvk_queue_create(dev, &pCreateInfo->pQueueCreateInfos[i], q);
         if (result != VK_SUCCESS)
            goto fail_queues;
      }
   }

   *pDevice = fgvk_device_to_handle(dev);

   return result;

fail_queues:
   vk_foreach_queue_safe(iter, &dev->vk) {
      struct fgvk_queue *queue = container_of(iter, struct fgvk_queue, vk);
      fgvk_queue_destroy(dev, queue);
   }
fail_alloc:
   vk_free(&dev->vk.alloc, dev);
   return result;
}

VKAPI_ATTR void VKAPI_CALL
fgvk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
{
   VK_FROM_HANDLE(fgvk_device, dev, _device);

   if (!dev)
      return;

   // TODO we can't finish cause sync doesn't work
   //vk_device_finish(&dev->vk);
   vk_free(&dev->vk.alloc, dev);
}