Create very primitive physical device and instance.
authorVivek Pandya <vivekvpandya@gmail.com>
Fri, 28 Aug 2020 16:37:54 +0000 (22:07 +0530)
committerVivek Pandya <vivekvpandya@gmail.com>
Sat, 5 Sep 2020 11:50:32 +0000 (17:20 +0530)
src/libre-soc/vulkan/libresoc_device.c
src/libre-soc/vulkan/libresoc_private.h

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
index 3b497a368bf5250b814512d5cfebdd1105b74183..5aba3eb4142e058bd67c20e23a7d980ec52e601b 100644 (file)
@@ -40,6 +40,7 @@
 #include "vk_debug_report.h"
 #include "util/xmlconfig.h"
 
+#include "vk_object.h"
 #include "libresoc_entrypoints.h"
 #include "libresoc_extensions.h"
 
@@ -79,6 +80,7 @@ struct libresoc_app_info {
 };
 
 struct libresoc_instance {
+   struct vk_object_base                       base;
    VK_LOADER_DATA _loader_data;
 
    VkAllocationCallbacks alloc;
@@ -89,8 +91,8 @@ struct libresoc_instance {
    struct libresoc_instance_dispatch_table dispatch;
    struct libresoc_device_dispatch_table device_dispatch;
 
-   int physicalDeviceCount;
-   struct libresoc_physical_device physicalDevice;
+   int physical_device_count;
+   struct libresoc_physical_device physical_device;
 
    struct vk_debug_report_instance debug_report_callbacks;
 };