Home | History | Annotate | Download | only in vk
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "vk/GrVkInterface.h"
      9 #include "vk/GrVkBackendContext.h"
     10 #include "vk/GrVkUtil.h"
     11 
     12 GrVkInterface::GrVkInterface() {
     13 }
     14 
     15 #define GET_PROC_GLOBAL(F) functions->f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(NULL, "vk" #F)
     16 #define GET_PROC(F) functions->f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
     17 #define GET_PROC_LOCAL(inst, F) PFN_vk ## F F = (PFN_vk ## F) vkGetInstanceProcAddr(inst, "vk" #F)
     18 #define GET_DEV_PROC(F) functions->f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
     19 
     20 const GrVkInterface* GrVkCreateInterface(VkInstance instance, VkDevice device,
     21                                          uint32_t extensionFlags) {
     22 
     23     GrVkInterface* interface = new GrVkInterface();
     24     GrVkInterface::Functions* functions = &interface->fFunctions;
     25 
     26     GET_PROC_GLOBAL(CreateInstance);
     27     GET_PROC_GLOBAL(EnumerateInstanceExtensionProperties);
     28     GET_PROC_GLOBAL(EnumerateInstanceLayerProperties);
     29     GET_PROC(DestroyInstance);
     30     GET_PROC(EnumeratePhysicalDevices);
     31     GET_PROC(GetPhysicalDeviceFeatures);
     32     GET_PROC(GetPhysicalDeviceFormatProperties);
     33     GET_PROC(GetPhysicalDeviceImageFormatProperties);
     34     GET_PROC(GetPhysicalDeviceProperties);
     35     GET_PROC(GetPhysicalDeviceQueueFamilyProperties);
     36     GET_PROC(GetPhysicalDeviceMemoryProperties);
     37     GET_PROC(CreateDevice);
     38     GET_PROC(DestroyDevice);
     39     GET_PROC(EnumerateDeviceExtensionProperties);
     40     GET_PROC(EnumerateDeviceLayerProperties);
     41     GET_DEV_PROC(GetDeviceQueue);
     42     GET_DEV_PROC(QueueSubmit);
     43     GET_DEV_PROC(QueueWaitIdle);
     44     GET_DEV_PROC(DeviceWaitIdle);
     45     GET_DEV_PROC(AllocateMemory);
     46     GET_DEV_PROC(FreeMemory);
     47     GET_DEV_PROC(MapMemory);
     48     GET_DEV_PROC(UnmapMemory);
     49     GET_DEV_PROC(FlushMappedMemoryRanges);
     50     GET_DEV_PROC(InvalidateMappedMemoryRanges);
     51     GET_DEV_PROC(GetDeviceMemoryCommitment);
     52     GET_DEV_PROC(BindBufferMemory);
     53     GET_DEV_PROC(BindImageMemory);
     54     GET_DEV_PROC(GetBufferMemoryRequirements);
     55     GET_DEV_PROC(GetImageMemoryRequirements);
     56     GET_DEV_PROC(GetImageSparseMemoryRequirements);
     57     GET_PROC(GetPhysicalDeviceSparseImageFormatProperties);
     58     GET_DEV_PROC(QueueBindSparse);
     59     GET_DEV_PROC(CreateFence);
     60     GET_DEV_PROC(DestroyFence);
     61     GET_DEV_PROC(ResetFences);
     62     GET_DEV_PROC(GetFenceStatus);
     63     GET_DEV_PROC(WaitForFences);
     64     GET_DEV_PROC(CreateSemaphore);
     65     GET_DEV_PROC(DestroySemaphore);
     66     GET_DEV_PROC(CreateEvent);
     67     GET_DEV_PROC(DestroyEvent);
     68     GET_DEV_PROC(GetEventStatus);
     69     GET_DEV_PROC(SetEvent);
     70     GET_DEV_PROC(ResetEvent);
     71     GET_DEV_PROC(CreateQueryPool);
     72     GET_DEV_PROC(DestroyQueryPool);
     73     GET_DEV_PROC(GetQueryPoolResults);
     74     GET_DEV_PROC(CreateBuffer);
     75     GET_DEV_PROC(DestroyBuffer);
     76     GET_DEV_PROC(CreateBufferView);
     77     GET_DEV_PROC(DestroyBufferView);
     78     GET_DEV_PROC(CreateImage);
     79     GET_DEV_PROC(DestroyImage);
     80     GET_DEV_PROC(GetImageSubresourceLayout);
     81     GET_DEV_PROC(CreateImageView);
     82     GET_DEV_PROC(DestroyImageView);
     83     GET_DEV_PROC(CreateShaderModule);
     84     GET_DEV_PROC(DestroyShaderModule);
     85     GET_DEV_PROC(CreatePipelineCache);
     86     GET_DEV_PROC(DestroyPipelineCache);
     87     GET_DEV_PROC(GetPipelineCacheData);
     88     GET_DEV_PROC(MergePipelineCaches);
     89     GET_DEV_PROC(CreateGraphicsPipelines);
     90     GET_DEV_PROC(CreateComputePipelines);
     91     GET_DEV_PROC(DestroyPipeline);
     92     GET_DEV_PROC(CreatePipelineLayout);
     93     GET_DEV_PROC(DestroyPipelineLayout);
     94     GET_DEV_PROC(CreateSampler);
     95     GET_DEV_PROC(DestroySampler);
     96     GET_DEV_PROC(CreateDescriptorSetLayout);
     97     GET_DEV_PROC(DestroyDescriptorSetLayout);
     98     GET_DEV_PROC(CreateDescriptorPool);
     99     GET_DEV_PROC(DestroyDescriptorPool);
    100     GET_DEV_PROC(ResetDescriptorPool);
    101     GET_DEV_PROC(AllocateDescriptorSets);
    102     GET_DEV_PROC(FreeDescriptorSets);
    103     GET_DEV_PROC(UpdateDescriptorSets);
    104     GET_DEV_PROC(CreateFramebuffer);
    105     GET_DEV_PROC(DestroyFramebuffer);
    106     GET_DEV_PROC(CreateRenderPass);
    107     GET_DEV_PROC(DestroyRenderPass);
    108     GET_DEV_PROC(GetRenderAreaGranularity);
    109     GET_DEV_PROC(CreateCommandPool);
    110     GET_DEV_PROC(DestroyCommandPool);
    111     GET_DEV_PROC(ResetCommandPool);
    112     GET_DEV_PROC(AllocateCommandBuffers);
    113     GET_DEV_PROC(FreeCommandBuffers);
    114     GET_DEV_PROC(BeginCommandBuffer);
    115     GET_DEV_PROC(EndCommandBuffer);
    116     GET_DEV_PROC(ResetCommandBuffer);
    117     GET_DEV_PROC(CmdBindPipeline);
    118     GET_DEV_PROC(CmdSetViewport);
    119     GET_DEV_PROC(CmdSetScissor);
    120     GET_DEV_PROC(CmdSetLineWidth);
    121     GET_DEV_PROC(CmdSetDepthBias);
    122     GET_DEV_PROC(CmdSetBlendConstants);
    123     GET_DEV_PROC(CmdSetDepthBounds);
    124     GET_DEV_PROC(CmdSetStencilCompareMask);
    125     GET_DEV_PROC(CmdSetStencilWriteMask);
    126     GET_DEV_PROC(CmdSetStencilReference);
    127     GET_DEV_PROC(CmdBindDescriptorSets);
    128     GET_DEV_PROC(CmdBindIndexBuffer);
    129     GET_DEV_PROC(CmdBindVertexBuffers);
    130     GET_DEV_PROC(CmdDraw);
    131     GET_DEV_PROC(CmdDrawIndexed);
    132     GET_DEV_PROC(CmdDrawIndirect);
    133     GET_DEV_PROC(CmdDrawIndexedIndirect);
    134     GET_DEV_PROC(CmdDispatch);
    135     GET_DEV_PROC(CmdDispatchIndirect);
    136     GET_DEV_PROC(CmdCopyBuffer);
    137     GET_DEV_PROC(CmdCopyImage);
    138     GET_DEV_PROC(CmdBlitImage);
    139     GET_DEV_PROC(CmdCopyBufferToImage);
    140     GET_DEV_PROC(CmdCopyImageToBuffer);
    141     GET_DEV_PROC(CmdUpdateBuffer);
    142     GET_DEV_PROC(CmdFillBuffer);
    143     GET_DEV_PROC(CmdClearColorImage);
    144     GET_DEV_PROC(CmdClearDepthStencilImage);
    145     GET_DEV_PROC(CmdClearAttachments);
    146     GET_DEV_PROC(CmdResolveImage);
    147     GET_DEV_PROC(CmdSetEvent);
    148     GET_DEV_PROC(CmdResetEvent);
    149     GET_DEV_PROC(CmdWaitEvents);
    150     GET_DEV_PROC(CmdPipelineBarrier);
    151     GET_DEV_PROC(CmdBeginQuery);
    152     GET_DEV_PROC(CmdEndQuery);
    153     GET_DEV_PROC(CmdResetQueryPool);
    154     GET_DEV_PROC(CmdWriteTimestamp);
    155     GET_DEV_PROC(CmdCopyQueryPoolResults);
    156     GET_DEV_PROC(CmdPushConstants);
    157     GET_DEV_PROC(CmdBeginRenderPass);
    158     GET_DEV_PROC(CmdNextSubpass);
    159     GET_DEV_PROC(CmdEndRenderPass);
    160     GET_DEV_PROC(CmdExecuteCommands);
    161 
    162     if (extensionFlags & kEXT_debug_report_GrVkExtensionFlag) {
    163         GET_PROC(CreateDebugReportCallbackEXT);
    164         GET_PROC(DebugReportMessageEXT);
    165         GET_PROC(DestroyDebugReportCallbackEXT);
    166     }
    167 
    168     return interface;
    169 }
    170 
    171 #ifdef SK_DEBUG
    172     static int kIsDebug = 1;
    173 #else
    174     static int kIsDebug = 0;
    175 #endif
    176 
    177 #define RETURN_FALSE_INTERFACE                                                                   \
    178     if (kIsDebug) { SkDebugf("%s:%d GrVkInterface::validate() failed.\n", __FILE__, __LINE__); } \
    179     return false;
    180 
    181 bool GrVkInterface::validate(uint32_t extensionFlags) const {
    182     // functions that are always required
    183     if (NULL == fFunctions.fCreateInstance ||
    184         NULL == fFunctions.fDestroyInstance ||
    185         NULL == fFunctions.fEnumeratePhysicalDevices ||
    186         NULL == fFunctions.fGetPhysicalDeviceFeatures ||
    187         NULL == fFunctions.fGetPhysicalDeviceFormatProperties ||
    188         NULL == fFunctions.fGetPhysicalDeviceImageFormatProperties ||
    189         NULL == fFunctions.fGetPhysicalDeviceProperties ||
    190         NULL == fFunctions.fGetPhysicalDeviceQueueFamilyProperties ||
    191         NULL == fFunctions.fGetPhysicalDeviceMemoryProperties ||
    192         NULL == fFunctions.fCreateDevice ||
    193         NULL == fFunctions.fDestroyDevice ||
    194         NULL == fFunctions.fEnumerateInstanceExtensionProperties ||
    195         NULL == fFunctions.fEnumerateDeviceExtensionProperties ||
    196         NULL == fFunctions.fEnumerateInstanceLayerProperties ||
    197         NULL == fFunctions.fEnumerateDeviceLayerProperties ||
    198         NULL == fFunctions.fGetDeviceQueue ||
    199         NULL == fFunctions.fQueueSubmit ||
    200         NULL == fFunctions.fQueueWaitIdle ||
    201         NULL == fFunctions.fDeviceWaitIdle ||
    202         NULL == fFunctions.fAllocateMemory ||
    203         NULL == fFunctions.fFreeMemory ||
    204         NULL == fFunctions.fMapMemory ||
    205         NULL == fFunctions.fUnmapMemory ||
    206         NULL == fFunctions.fFlushMappedMemoryRanges ||
    207         NULL == fFunctions.fInvalidateMappedMemoryRanges ||
    208         NULL == fFunctions.fGetDeviceMemoryCommitment ||
    209         NULL == fFunctions.fBindBufferMemory ||
    210         NULL == fFunctions.fBindImageMemory ||
    211         NULL == fFunctions.fGetBufferMemoryRequirements ||
    212         NULL == fFunctions.fGetImageMemoryRequirements ||
    213         NULL == fFunctions.fGetImageSparseMemoryRequirements ||
    214         NULL == fFunctions.fGetPhysicalDeviceSparseImageFormatProperties ||
    215         NULL == fFunctions.fQueueBindSparse ||
    216         NULL == fFunctions.fCreateFence ||
    217         NULL == fFunctions.fDestroyFence ||
    218         NULL == fFunctions.fResetFences ||
    219         NULL == fFunctions.fGetFenceStatus ||
    220         NULL == fFunctions.fWaitForFences ||
    221         NULL == fFunctions.fCreateSemaphore ||
    222         NULL == fFunctions.fDestroySemaphore ||
    223         NULL == fFunctions.fCreateEvent ||
    224         NULL == fFunctions.fDestroyEvent ||
    225         NULL == fFunctions.fGetEventStatus ||
    226         NULL == fFunctions.fSetEvent ||
    227         NULL == fFunctions.fResetEvent ||
    228         NULL == fFunctions.fCreateQueryPool ||
    229         NULL == fFunctions.fDestroyQueryPool ||
    230         NULL == fFunctions.fGetQueryPoolResults ||
    231         NULL == fFunctions.fCreateBuffer ||
    232         NULL == fFunctions.fDestroyBuffer ||
    233         NULL == fFunctions.fCreateBufferView ||
    234         NULL == fFunctions.fDestroyBufferView ||
    235         NULL == fFunctions.fCreateImage ||
    236         NULL == fFunctions.fDestroyImage ||
    237         NULL == fFunctions.fGetImageSubresourceLayout ||
    238         NULL == fFunctions.fCreateImageView ||
    239         NULL == fFunctions.fDestroyImageView ||
    240         NULL == fFunctions.fCreateShaderModule ||
    241         NULL == fFunctions.fDestroyShaderModule ||
    242         NULL == fFunctions.fCreatePipelineCache ||
    243         NULL == fFunctions.fDestroyPipelineCache ||
    244         NULL == fFunctions.fGetPipelineCacheData ||
    245         NULL == fFunctions.fMergePipelineCaches ||
    246         NULL == fFunctions.fCreateGraphicsPipelines ||
    247         NULL == fFunctions.fCreateComputePipelines ||
    248         NULL == fFunctions.fDestroyPipeline ||
    249         NULL == fFunctions.fCreatePipelineLayout ||
    250         NULL == fFunctions.fDestroyPipelineLayout ||
    251         NULL == fFunctions.fCreateSampler ||
    252         NULL == fFunctions.fDestroySampler ||
    253         NULL == fFunctions.fCreateDescriptorSetLayout ||
    254         NULL == fFunctions.fDestroyDescriptorSetLayout ||
    255         NULL == fFunctions.fCreateDescriptorPool ||
    256         NULL == fFunctions.fDestroyDescriptorPool ||
    257         NULL == fFunctions.fResetDescriptorPool ||
    258         NULL == fFunctions.fAllocateDescriptorSets ||
    259         NULL == fFunctions.fFreeDescriptorSets ||
    260         NULL == fFunctions.fUpdateDescriptorSets ||
    261         NULL == fFunctions.fCreateFramebuffer ||
    262         NULL == fFunctions.fDestroyFramebuffer ||
    263         NULL == fFunctions.fCreateRenderPass ||
    264         NULL == fFunctions.fDestroyRenderPass ||
    265         NULL == fFunctions.fGetRenderAreaGranularity ||
    266         NULL == fFunctions.fCreateCommandPool ||
    267         NULL == fFunctions.fDestroyCommandPool ||
    268         NULL == fFunctions.fResetCommandPool ||
    269         NULL == fFunctions.fAllocateCommandBuffers ||
    270         NULL == fFunctions.fFreeCommandBuffers ||
    271         NULL == fFunctions.fBeginCommandBuffer ||
    272         NULL == fFunctions.fEndCommandBuffer ||
    273         NULL == fFunctions.fResetCommandBuffer ||
    274         NULL == fFunctions.fCmdBindPipeline ||
    275         NULL == fFunctions.fCmdSetViewport ||
    276         NULL == fFunctions.fCmdSetScissor ||
    277         NULL == fFunctions.fCmdSetLineWidth ||
    278         NULL == fFunctions.fCmdSetDepthBias ||
    279         NULL == fFunctions.fCmdSetBlendConstants ||
    280         NULL == fFunctions.fCmdSetDepthBounds ||
    281         NULL == fFunctions.fCmdSetStencilCompareMask ||
    282         NULL == fFunctions.fCmdSetStencilWriteMask ||
    283         NULL == fFunctions.fCmdSetStencilReference ||
    284         NULL == fFunctions.fCmdBindDescriptorSets ||
    285         NULL == fFunctions.fCmdBindIndexBuffer ||
    286         NULL == fFunctions.fCmdBindVertexBuffers ||
    287         NULL == fFunctions.fCmdDraw ||
    288         NULL == fFunctions.fCmdDrawIndexed ||
    289         NULL == fFunctions.fCmdDrawIndirect ||
    290         NULL == fFunctions.fCmdDrawIndexedIndirect ||
    291         NULL == fFunctions.fCmdDispatch ||
    292         NULL == fFunctions.fCmdDispatchIndirect ||
    293         NULL == fFunctions.fCmdCopyBuffer ||
    294         NULL == fFunctions.fCmdCopyImage ||
    295         NULL == fFunctions.fCmdBlitImage ||
    296         NULL == fFunctions.fCmdCopyBufferToImage ||
    297         NULL == fFunctions.fCmdCopyImageToBuffer ||
    298         NULL == fFunctions.fCmdUpdateBuffer ||
    299         NULL == fFunctions.fCmdFillBuffer ||
    300         NULL == fFunctions.fCmdClearColorImage ||
    301         NULL == fFunctions.fCmdClearDepthStencilImage ||
    302         NULL == fFunctions.fCmdClearAttachments ||
    303         NULL == fFunctions.fCmdResolveImage ||
    304         NULL == fFunctions.fCmdSetEvent ||
    305         NULL == fFunctions.fCmdResetEvent ||
    306         NULL == fFunctions.fCmdWaitEvents ||
    307         NULL == fFunctions.fCmdPipelineBarrier ||
    308         NULL == fFunctions.fCmdBeginQuery ||
    309         NULL == fFunctions.fCmdEndQuery ||
    310         NULL == fFunctions.fCmdResetQueryPool ||
    311         NULL == fFunctions.fCmdWriteTimestamp ||
    312         NULL == fFunctions.fCmdCopyQueryPoolResults ||
    313         NULL == fFunctions.fCmdPushConstants ||
    314         NULL == fFunctions.fCmdBeginRenderPass ||
    315         NULL == fFunctions.fCmdNextSubpass ||
    316         NULL == fFunctions.fCmdEndRenderPass ||
    317         NULL == fFunctions.fCmdExecuteCommands) {
    318         RETURN_FALSE_INTERFACE
    319     }
    320 
    321     if (extensionFlags & kEXT_debug_report_GrVkExtensionFlag) {
    322         if (NULL == fFunctions.fCreateDebugReportCallbackEXT ||
    323             NULL == fFunctions.fDebugReportMessageEXT ||
    324             NULL == fFunctions.fDestroyDebugReportCallbackEXT) {
    325             RETURN_FALSE_INTERFACE
    326         }
    327     }
    328     return true;
    329 }
    330 
    331