Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (c) 2015-2016 The Khronos Group Inc.
      3  * Copyright (c) 2015-2016 Valve Corporation
      4  * Copyright (c) 2015-2016 LunarG, Inc.
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  *
     18  * Author: Mark Lobodzinski <mark (at) lunarg.com>
     19  */
     20 
     21 #define _GNU_SOURCE
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include "vk_loader_platform.h"
     26 #include "loader.h"
     27 #include "extensions.h"
     28 #include <vulkan/vk_icd.h>
     29 
     30 // Definitions for the VK_NV_external_memory_capabilities extension
     31 
     32 LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
     33 vkGetPhysicalDeviceExternalImageFormatPropertiesNV(
     34     VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
     35     VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
     36     VkExternalMemoryHandleTypeFlagsNV externalHandleType,
     37     VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
     38     const VkLayerInstanceDispatchTable *disp;
     39     VkPhysicalDevice unwrapped_phys_dev =
     40         loader_unwrap_physical_device(physicalDevice);
     41     disp = loader_get_instance_dispatch(physicalDevice);
     42 
     43     return disp->GetPhysicalDeviceExternalImageFormatPropertiesNV(
     44         unwrapped_phys_dev, format, type, tiling, usage, flags,
     45         externalHandleType, pExternalImageFormatProperties);
     46 }
     47 
     48 VKAPI_ATTR VkResult VKAPI_CALL
     49 terminator_GetPhysicalDeviceExternalImageFormatPropertiesNV(
     50     VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type,
     51     VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
     52     VkExternalMemoryHandleTypeFlagsNV externalHandleType,
     53     VkExternalImageFormatPropertiesNV *pExternalImageFormatProperties) {
     54     struct loader_physical_device *phys_dev =
     55         (struct loader_physical_device *)physicalDevice;
     56     struct loader_icd *icd = phys_dev->this_icd;
     57 
     58     if (!icd->GetPhysicalDeviceExternalImageFormatPropertiesNV) {
     59         if (externalHandleType) {
     60             return VK_ERROR_FORMAT_NOT_SUPPORTED;
     61         }
     62 
     63         if (!icd->GetPhysicalDeviceImageFormatProperties) {
     64             return VK_ERROR_INITIALIZATION_FAILED;
     65         }
     66 
     67         pExternalImageFormatProperties->externalMemoryFeatures = 0;
     68         pExternalImageFormatProperties->exportFromImportedHandleTypes = 0;
     69         pExternalImageFormatProperties->compatibleHandleTypes = 0;
     70 
     71         return icd->GetPhysicalDeviceImageFormatProperties(
     72             phys_dev->phys_dev, format, type, tiling, usage, flags,
     73             &pExternalImageFormatProperties->imageFormatProperties);
     74     }
     75 
     76     return icd->GetPhysicalDeviceExternalImageFormatPropertiesNV(
     77         phys_dev->phys_dev, format, type, tiling, usage, flags,
     78         externalHandleType, pExternalImageFormatProperties);
     79 }
     80 
     81 // Definitions for the VK_AMD_draw_indirect_count extension
     82 
     83 VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndirectCountAMD(
     84     VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
     85     VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
     86     uint32_t stride) {
     87     const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
     88     disp->CmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer,
     89                                   countBufferOffset, maxDrawCount, stride);
     90 }
     91 
     92 VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexedIndirectCountAMD(
     93     VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
     94     VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
     95     uint32_t stride) {
     96     const VkLayerDispatchTable *disp = loader_get_dispatch(commandBuffer);
     97     disp->CmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset,
     98                                          countBuffer, countBufferOffset,
     99                                          maxDrawCount, stride);
    100 }
    101 
    102 #ifdef VK_USE_PLATFORM_WIN32_KHR
    103 
    104 // Definitions for the VK_NV_external_memory_win32 extension
    105 
    106 VKAPI_ATTR VkResult VKAPI_CALL vkGetMemoryWin32HandleNV(
    107     VkDevice device, VkDeviceMemory memory,
    108     VkExternalMemoryHandleTypeFlagsNV handleType, HANDLE *pHandle) {
    109     const VkLayerDispatchTable *disp = loader_get_dispatch(device);
    110     return disp->GetMemoryWin32HandleNV(device, memory, handleType, pHandle);
    111 }
    112 
    113 #endif // VK_USE_PLATFORM_WIN32_KHR
    114 
    115 // GPA helpers for non-KHR extensions
    116 
    117 bool extension_instance_gpa(struct loader_instance *ptr_instance,
    118                             const char *name, void **addr) {
    119     *addr = NULL;
    120 
    121     // Functions for the VK_NV_external_memory_capabilities extension
    122 
    123     if (!strcmp("vkGetPhysicalDeviceExternalImageFormatPropertiesNV", name)) {
    124         *addr = (ptr_instance->enabled_known_extensions
    125                      .nv_external_memory_capabilities == 1)
    126                     ? (void *)vkGetPhysicalDeviceExternalImageFormatPropertiesNV
    127                     : NULL;
    128         return true;
    129     }
    130 
    131     // Functions for the VK_AMD_draw_indirect_count extension
    132 
    133     if (!strcmp("vkCmdDrawIndirectCountAMD", name)) {
    134         *addr = (void *)vkCmdDrawIndirectCountAMD;
    135         return true;
    136     }
    137 
    138     if (!strcmp("vkCmdDrawIndexedIndirectCountAMD", name)) {
    139         *addr = (void *)vkCmdDrawIndexedIndirectCountAMD;
    140         return true;
    141     }
    142 
    143 #ifdef VK_USE_PLATFORM_WIN32_KHR
    144 
    145     // Functions for the VK_NV_external_memory_win32 extension
    146 
    147     if (!strcmp("vkGetMemoryWin32HandleNV", name)) {
    148         *addr = (void *)vkGetMemoryWin32HandleNV;
    149         return true;
    150     }
    151 
    152 #endif // VK_USE_PLATFORM_WIN32_KHR
    153 
    154     return false;
    155 }
    156 
    157 void extensions_create_instance(struct loader_instance *ptr_instance,
    158                                 const VkInstanceCreateInfo *pCreateInfo) {
    159     ptr_instance->enabled_known_extensions.nv_external_memory_capabilities = 0;
    160 
    161     for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
    162         if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
    163                    VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME) == 0) {
    164             ptr_instance->enabled_known_extensions
    165                 .nv_external_memory_capabilities = 1;
    166             return;
    167         }
    168     }
    169 }
    170