Create very primitive physical device and instance.
[mesa.git] / src / libre-soc / vulkan / libresoc_device.c
index 089aa36cee0e27b829fc2fbaf48d41246210bf16..8c0f576a151e6658ef99c1319700b0df41eacaf5 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "libresoc_private.h"
 #include "vk_util.h"
+#include "vk_alloc.h"
 
 VkResult
 libresoc_EnumerateInstanceExtensionProperties(const char *pLayerName,
@@ -52,6 +53,33 @@ libresoc_EnumerateInstanceExtensionProperties(const char *pLayerName,
    return vk_outarray_status(&out);
 }
 
+static void *
+default_alloc_func(void *pUserData, size_t size, size_t align,
+                   VkSystemAllocationScope allocationScope)
+{
+       return malloc(size);
+}
+
+static void *
+default_realloc_func(void *pUserData, void *pOriginal, size_t size,
+                     size_t align, VkSystemAllocationScope allocationScope)
+{
+       return realloc(pOriginal, size);
+}
+
+static void
+default_free_func(void *pUserData, void *pMemory)
+{
+       free(pMemory);
+}
+
+static const VkAllocationCallbacks default_alloc = {
+       .pUserData = NULL,
+       .pfnAllocation = default_alloc_func,
+       .pfnReallocation = default_realloc_func,
+       .pfnFree = default_free_func,
+};
+
 VkResult
 libresoc_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
                     const VkAllocationCallbacks *pAllocator,
@@ -60,8 +88,23 @@ libresoc_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
        if (getenv("LIBRESOC_TRACE")) {
                fprintf(stderr, "CreateInstance called. \n");
        }
-   /* FIXME: stub */
-   return VK_SUCCESS;
+       struct libresoc_instance *instance;
+
+       instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
+                             VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+       if (!instance)
+               return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
+
+       vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE);
+
+       if (pAllocator)
+               instance->alloc = *pAllocator;
+       else
+               instance->alloc = default_alloc;
+       /*TODO : enable extensions*/
+       *pInstance = libresoc_instance_to_handle(instance);
+
+       return VK_SUCCESS;
 }
 
 void
@@ -74,6 +117,43 @@ libresoc_DestroyInstance(VkInstance _instance,
    /* FIXME: stub */
 }
 
+static VkResult
+libresoc_physical_device_try_create(struct libresoc_instance *instance,
+                               struct libresoc_physical_device **device_out)
+{
+       VkResult result;
+
+       struct libresoc_physical_device *device =
+               vk_zalloc2(&instance->alloc, NULL, sizeof(*device), 8,
+                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+       if (!device) {
+               result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
+               return result;
+       }
+
+       device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
+       device->instance = instance;
+
+       *device_out = device;
+
+       return VK_SUCCESS;
+
+}
+static VkResult
+libresoc_enumerate_physical_devices(struct libresoc_instance *instance)
+{
+
+       VkResult result = VK_SUCCESS;
+       /* the driver creates a null
+        * device that allows to test the compiler without having a physical device
+        */
+       struct libresoc_physical_device *pdevice;
+
+       result = libresoc_physical_device_try_create(instance,  &pdevice);
+       return result;
+
+}
+
 VkResult
 libresoc_EnumeratePhysicalDevices(VkInstance _instance,
                               uint32_t *pPhysicalDeviceCount,
@@ -82,6 +162,18 @@ libresoc_EnumeratePhysicalDevices(VkInstance _instance,
        if (getenv("LIBRESOC_TRACE")) {
                fprintf(stderr, "EnumeratePhysicalDevices called\n");
        }
+       LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
+       VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
+
+       VkResult result = libresoc_enumerate_physical_devices(instance);
+       if (result != VK_SUCCESS)
+               return result;
+
+       vk_outarray_append(&out, i) {
+               *i = libresoc_physical_device_to_handle(&instance->physical_device);
+       }
+
+       return vk_outarray_status(&out);
    /* FIXME: stub */
    return VK_SUCCESS;
 }
@@ -201,7 +293,7 @@ libresoc_GetInstanceProcAddr(VkInstance _instance,
 
    idx = libresoc_get_physical_device_entrypoint_index(pName);
    if (idx >= 0)
-      return instance->physicalDevice.dispatch.entrypoints[idx];
+      return instance->physical_device.dispatch.entrypoints[idx];
 
    idx = libresoc_get_device_entrypoint_index(pName);
    if (idx >= 0)
@@ -272,7 +364,7 @@ vk_icdGetPhysicalDeviceProcAddr(VkInstance  _instance,
    if (idx < 0)
       return NULL;
 
-   return instance->physicalDevice.dispatch.entrypoints[idx];
+   return instance->physical_device.dispatch.entrypoints[idx];
 }
 
 VkResult