1 // Copyright 2018 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_VULKANBACKEND_H_ 16 #define DAWNNATIVE_VULKANBACKEND_H_ 17 18 #include <dawn/dawn_wsi.h> 19 #include <dawn_native/DawnNative.h> 20 21 #include <vulkan/vulkan.h> 22 23 #include <vector> 24 25 namespace dawn_native { namespace vulkan { 26 27 DAWN_NATIVE_EXPORT VkInstance GetInstance(WGPUDevice device); 28 29 DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName); 30 31 DAWN_NATIVE_EXPORT DawnSwapChainImplementation 32 CreateNativeSwapChainImpl(WGPUDevice device, ::VkSurfaceKHR surface); 33 DAWN_NATIVE_EXPORT WGPUTextureFormat 34 GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain); 35 36 struct DAWN_NATIVE_EXPORT AdapterDiscoveryOptions : public AdapterDiscoveryOptionsBase { 37 AdapterDiscoveryOptions(); 38 39 bool forceSwiftShader = false; 40 }; 41 42 struct DAWN_NATIVE_EXPORT ExternalImageDescriptorVk : ExternalImageDescriptor { 43 public: 44 // The following members may be ignored if |ExternalImageDescriptor::isInitialized| is false 45 // since the import does not need to preserve texture contents. 46 47 // See https://www.khronos.org/registry/vulkan/specs/1.1/html/chap7.html. The acquire 48 // operation old/new layouts must match exactly the layouts in the release operation. So 49 // we may need to issue two barriers releasedOldLayout -> releasedNewLayout -> 50 // cTextureDescriptor.usage if the new layout is not compatible with the desired usage. 51 // The first barrier is the queue transfer, the second is the layout transition to our 52 // desired usage. 53 VkImageLayout releasedOldLayout = VK_IMAGE_LAYOUT_GENERAL; 54 VkImageLayout releasedNewLayout = VK_IMAGE_LAYOUT_GENERAL; 55 56 protected: 57 using ExternalImageDescriptor::ExternalImageDescriptor; 58 }; 59 60 struct ExternalImageExportInfoVk : ExternalImageExportInfo { 61 public: 62 // See comments in |ExternalImageDescriptorVk| 63 // Contains the old/new layouts used in the queue release operation. 64 VkImageLayout releasedOldLayout; 65 VkImageLayout releasedNewLayout; 66 67 protected: 68 using ExternalImageExportInfo::ExternalImageExportInfo; 69 }; 70 71 // Can't use DAWN_PLATFORM_LINUX since header included in both Dawn and Chrome 72 #ifdef __linux__ 73 74 // Common properties of external images represented by FDs. On successful import the file 75 // descriptor's ownership is transferred to the Dawn implementation and they shouldn't be 76 // used outside of Dawn again. TODO(enga): Also transfer ownership in the error case so the 77 // caller can assume the FD is always consumed. 78 struct DAWN_NATIVE_EXPORT ExternalImageDescriptorFD : ExternalImageDescriptorVk { 79 public: 80 int memoryFD; // A file descriptor from an export of the memory of the image 81 std::vector<int> waitFDs; // File descriptors of semaphores which will be waited on 82 83 protected: 84 using ExternalImageDescriptorVk::ExternalImageDescriptorVk; 85 }; 86 87 // Descriptor for opaque file descriptor image import 88 struct DAWN_NATIVE_EXPORT ExternalImageDescriptorOpaqueFD : ExternalImageDescriptorFD { 89 ExternalImageDescriptorOpaqueFD(); 90 91 VkDeviceSize allocationSize; // Must match VkMemoryAllocateInfo from image creation 92 uint32_t memoryTypeIndex; // Must match VkMemoryAllocateInfo from image creation 93 }; 94 95 // Descriptor for dma-buf file descriptor image import 96 struct DAWN_NATIVE_EXPORT ExternalImageDescriptorDmaBuf : ExternalImageDescriptorFD { 97 ExternalImageDescriptorDmaBuf(); 98 99 uint32_t stride; // Stride of the buffer in bytes 100 uint64_t drmModifier; // DRM modifier of the buffer 101 }; 102 103 // Info struct that is written to in |ExportVulkanImage|. 104 struct DAWN_NATIVE_EXPORT ExternalImageExportInfoFD : ExternalImageExportInfoVk { 105 public: 106 // Contains the exported semaphore handles. 107 std::vector<int> semaphoreHandles; 108 109 protected: 110 using ExternalImageExportInfoVk::ExternalImageExportInfoVk; 111 }; 112 113 struct DAWN_NATIVE_EXPORT ExternalImageExportInfoOpaqueFD : ExternalImageExportInfoFD { 114 ExternalImageExportInfoOpaqueFD(); 115 }; 116 117 struct DAWN_NATIVE_EXPORT ExternalImageExportInfoDmaBuf : ExternalImageExportInfoFD { 118 ExternalImageExportInfoDmaBuf(); 119 }; 120 121 #endif // __linux__ 122 123 // Imports external memory into a Vulkan image. Internally, this uses external memory / 124 // semaphore extensions to import the image and wait on the provided synchronizaton 125 // primitives before the texture can be used. 126 // On failure, returns a nullptr. 127 DAWN_NATIVE_EXPORT WGPUTexture WrapVulkanImage(WGPUDevice device, 128 const ExternalImageDescriptorVk* descriptor); 129 130 // Exports external memory from a Vulkan image. This must be called on wrapped textures 131 // before they are destroyed. It writes the semaphore to wait on and the old/new image 132 // layouts to |info|. Pass VK_IMAGE_LAYOUT_UNDEFINED as |desiredLayout| if you don't want to 133 // perform a layout transition. 134 DAWN_NATIVE_EXPORT bool ExportVulkanImage(WGPUTexture texture, 135 VkImageLayout desiredLayout, 136 ExternalImageExportInfoVk* info); 137 138 }} // namespace dawn_native::vulkan 139 140 #endif // DAWNNATIVE_VULKANBACKEND_H_ 141