LibreSoc Vulkan Driver Initial Commit
[mesa.git] / src / libre-soc / vulkan / libresoc_device.c
diff --git a/src/libre-soc/vulkan/libresoc_device.c b/src/libre-soc/vulkan/libresoc_device.c
new file mode 100644 (file)
index 0000000..089aa36
--- /dev/null
@@ -0,0 +1,324 @@
+/*
+ * Copyright © 2019 Raspberry Pi
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/sysinfo.h>
+#include <unistd.h>
+
+#include "libresoc_private.h"
+#include "vk_util.h"
+
+VkResult
+libresoc_EnumerateInstanceExtensionProperties(const char *pLayerName,
+                                          uint32_t *pPropertyCount,
+                                          VkExtensionProperties *pProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "EnumerateInstanceExtensionProperties called for: %s \n", pLayerName);
+       }
+   VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
+
+   for (int i = 0; i < LIBRESOC_INSTANCE_EXTENSION_COUNT; i++) {
+      if (libresoc_instance_extensions_supported.extensions[i]) {
+         vk_outarray_append(&out, prop) {
+            *prop = libresoc_instance_extensions[i];
+         }
+      }
+   }
+
+   return vk_outarray_status(&out);
+}
+
+VkResult
+libresoc_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
+                    const VkAllocationCallbacks *pAllocator,
+                    VkInstance *pInstance)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "CreateInstance called. \n");
+       }
+   /* FIXME: stub */
+   return VK_SUCCESS;
+}
+
+void
+libresoc_DestroyInstance(VkInstance _instance,
+                     const VkAllocationCallbacks *pAllocator)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "DestroyInstance called. \n");
+       }
+   /* FIXME: stub */
+}
+
+VkResult
+libresoc_EnumeratePhysicalDevices(VkInstance _instance,
+                              uint32_t *pPhysicalDeviceCount,
+                              VkPhysicalDevice *pPhysicalDevices)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "EnumeratePhysicalDevices called\n");
+       }
+   /* FIXME: stub */
+   return VK_SUCCESS;
+}
+
+void
+libresoc_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
+                               VkPhysicalDeviceFeatures *pFeatures)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDeviceFeatures called. \n");
+       }
+   /* FIXME: stub */
+}
+
+void
+libresoc_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
+                                 VkPhysicalDeviceProperties *pProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDeviceProperties called. \n");
+       }
+   /* FIXME: stub */
+}
+
+void
+libresoc_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
+                                            uint32_t *pCount,
+                                            VkQueueFamilyProperties *pQueueFamilyProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDeviceQueueFamilyProperites called. \n");
+       }
+   /* FIXME: stub */
+}
+
+void
+libresoc_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
+                                       VkPhysicalDeviceMemoryProperties *pMemoryProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDEviceMemoryProperties called. \n");
+       }
+   /* FIXME: stub */
+}
+
+void
+libresoc_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties *pFormatProperties) {
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDeviceFormatProperties called. \n");
+       }
+   /* FIXME: stub */
+}
+
+VkResult
+      libresoc_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* pImageFormatProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDEviceImageFormatProperties called. \n");
+       }
+      
+   /* FIXME: stub */
+   return VK_SUCCESS;
+}
+      void 
+      libresoc_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling, uint32_t* pPropertyCount, VkSparseImageFormatProperties* pProperties)
+      {
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetPhysicalDeviceSparseImageFormatProperties called. \n");
+       }
+   /* FIXME: stub */
+      }
+PFN_vkVoidFunction
+libresoc_GetInstanceProcAddr(VkInstance _instance,
+                         const char *pName)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetInstanceProcAddr called for: %s \n", pName);
+       }
+   LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
+
+   /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
+    * when we have to return valid function pointers, NULL, or it's left
+    * undefined.  See the table for exact details.
+    */
+   if (pName == NULL)
+      return NULL;
+
+#define LOOKUP_LIBRESOC_ENTRYPOINT(entrypoint)              \
+   if (strcmp(pName, "vk" #entrypoint) == 0)            \
+      return (PFN_vkVoidFunction)libresoc_##entrypoint
+
+   LOOKUP_LIBRESOC_ENTRYPOINT(EnumerateInstanceExtensionProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(CreateInstance);
+   LOOKUP_LIBRESOC_ENTRYPOINT(DestroyInstance);
+   LOOKUP_LIBRESOC_ENTRYPOINT(EnumeratePhysicalDevices);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceFeatures);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceFormatProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceImageFormatProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceQueueFamilyProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceMemoryProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetDeviceProcAddr);
+   LOOKUP_LIBRESOC_ENTRYPOINT(CreateDevice);
+   LOOKUP_LIBRESOC_ENTRYPOINT(EnumerateDeviceExtensionProperties);
+   LOOKUP_LIBRESOC_ENTRYPOINT(GetPhysicalDeviceSparseImageFormatProperties);
+
+
+#undef LOOKUP_LIBRESOC_ENTRYPOINT
+
+   if (instance == NULL)
+      return NULL;
+
+   int idx = libresoc_get_instance_entrypoint_index(pName);
+   if (idx >= 0)
+      return instance->dispatch.entrypoints[idx];
+
+   idx = libresoc_get_physical_device_entrypoint_index(pName);
+   if (idx >= 0)
+      return instance->physicalDevice.dispatch.entrypoints[idx];
+
+   idx = libresoc_get_device_entrypoint_index(pName);
+   if (idx >= 0)
+      return instance->device_dispatch.entrypoints[idx];
+
+   return NULL;
+}
+
+/* With version 1+ of the loader interface the ICD should expose
+ * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
+ */
+PUBLIC
+VKAPI_ATTR PFN_vkVoidFunction
+VKAPI_CALL vk_icdGetInstanceProcAddr(VkInstance instance,
+                                     const char *pName);
+
+PUBLIC
+VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
+vk_icdGetInstanceProcAddr(VkInstance instance,
+                          const char*                                 pName)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "vk_icdGetInstanceProcAddr called for: %s \n", pName);
+       }
+   return libresoc_GetInstanceProcAddr(instance, pName);
+}
+
+PFN_vkVoidFunction
+libresoc_GetDeviceProcAddr(VkDevice _device,
+                       const char *pName)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetDeviceProcAddr called for: %s \n", pName);
+       }
+   LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
+
+   if (!device || !pName)
+      return NULL;
+
+   int idx = libresoc_get_device_entrypoint_index(pName);
+   if (idx < 0)
+      return NULL;
+
+   return device->dispatch.entrypoints[idx];
+}
+
+/* With version 4+ of the loader interface the ICD should expose
+ * vk_icdGetPhysicalDeviceProcAddr()
+ */
+PUBLIC
+VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
+vk_icdGetPhysicalDeviceProcAddr(VkInstance  _instance,
+                                const char* pName);
+
+PFN_vkVoidFunction
+vk_icdGetPhysicalDeviceProcAddr(VkInstance  _instance,
+                                const char* pName)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "vk_icdGetPhysicalDeviceProcAddr called for: %s \n", pName);
+       }
+   LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
+
+   if (!pName || !instance)
+      return NULL;
+
+   int idx = libresoc_get_physical_device_entrypoint_index(pName);
+   if (idx < 0)
+      return NULL;
+
+   return instance->physicalDevice.dispatch.entrypoints[idx];
+}
+
+VkResult
+libresoc_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
+                                        const char *pLayerName,
+                                        uint32_t *pPropertyCount,
+                                        VkExtensionProperties *pProperties)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "EnumerateDeviceExtensionProperties called for layer: %s \n", pLayerName);
+       }
+   /* FIXME: stub */
+   return VK_SUCCESS;
+}
+
+VkResult
+libresoc_CreateDevice(VkPhysicalDevice physicalDevice,
+                  const VkDeviceCreateInfo *pCreateInfo,
+                  const VkAllocationCallbacks *pAllocator,
+                  VkDevice *pDevice)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "CreateDevice called \n");
+       }
+   /* FIXME: stub */
+   return VK_SUCCESS;
+}
+
+void
+libresoc_DestroyDevice(VkDevice _device,
+                   const VkAllocationCallbacks *pAllocator)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "DestroyDevice called. \n");
+       }
+   /* FIXME: stub */
+}
+
+void
+libresoc_GetDeviceQueue(VkDevice _device,
+                    uint32_t queueNodeIndex,
+                    uint32_t queueIndex,
+                    VkQueue *pQueue)
+{
+       if (getenv("LIBRESOC_TRACE")) {
+               fprintf(stderr, "GetDeviceQueue called. \n");
+       }
+   /* FIXME: stub */
+}