webgpu/dawn/include/dawn/native/VulkanBackend.h
  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 INCLUDE_DAWN_NATIVE_VULKANBACKEND_H_
 16#define INCLUDE_DAWN_NATIVE_VULKANBACKEND_H_
 17
 18#include <vulkan/vulkan.h>
 19
 20#include <array>
 21#include <vector>
 22
 23#include "dawn/native/DawnNative.h"
 24
 25namespace dawn::native::vulkan {
 26
 27DAWN_NATIVE_EXPORT VkInstance GetInstance(WGPUDevice device);
 28
 29DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName);
 30
 31struct DAWN_NATIVE_EXPORT PhysicalDeviceDiscoveryOptions
 32    : public PhysicalDeviceDiscoveryOptionsBase {
 33    PhysicalDeviceDiscoveryOptions();
 34
 35    bool forceSwiftShader = false;
 36};
 37
 38// TODO(dawn:1774): Deprecated.
 39using AdapterDiscoveryOptions = PhysicalDeviceDiscoveryOptions;
 40
 41enum class NeedsDedicatedAllocation {
 42    Yes,
 43    No,
 44    // Use Vulkan reflection to detect whether a dedicated allocation is needed.
 45    Detect,
 46};
 47
 48struct DAWN_NATIVE_EXPORT ExternalImageDescriptorVk : ExternalImageDescriptor {
 49  public:
 50    // The following members may be ignored if |ExternalImageDescriptor::isInitialized| is false
 51    // since the import does not need to preserve texture contents.
 52
 53    // See https://www.khronos.org/registry/vulkan/specs/1.1/html/chap7.html. The acquire
 54    // operation old/new layouts must match exactly the layouts in the release operation. So
 55    // we may need to issue two barriers releasedOldLayout -> releasedNewLayout ->
 56    // cTextureDescriptor.usage if the new layout is not compatible with the desired usage.
 57    // The first barrier is the queue transfer, the second is the layout transition to our
 58    // desired usage.
 59    VkImageLayout releasedOldLayout = VK_IMAGE_LAYOUT_GENERAL;
 60    VkImageLayout releasedNewLayout = VK_IMAGE_LAYOUT_GENERAL;
 61
 62    // Try to detect the need to use a dedicated allocation for imported images by default but let
 63    // the application override this as drivers have bugs and forget to require a dedicated
 64    // allocation.
 65    NeedsDedicatedAllocation dedicatedAllocation = NeedsDedicatedAllocation::Detect;
 66
 67  protected:
 68    using ExternalImageDescriptor::ExternalImageDescriptor;
 69};
 70
 71struct ExternalImageExportInfoVk : ExternalImageExportInfo {
 72  public:
 73    // See comments in |ExternalImageDescriptorVk|
 74    // Contains the old/new layouts used in the queue release operation.
 75    VkImageLayout releasedOldLayout;
 76    VkImageLayout releasedNewLayout;
 77
 78  protected:
 79    using ExternalImageExportInfo::ExternalImageExportInfo;
 80};
 81
 82// Can't use DAWN_PLATFORM_IS(LINUX) since header included in both Dawn and Chrome
 83#ifdef __linux__
 84
 85// Common properties of external images represented by FDs. On successful import the file
 86// descriptor's ownership is transferred to the Dawn implementation and they shouldn't be
 87// used outside of Dawn again. TODO(enga): Also transfer ownership in the error case so the
 88// caller can assume the FD is always consumed.
 89struct DAWN_NATIVE_EXPORT ExternalImageDescriptorFD : ExternalImageDescriptorVk {
 90  public:
 91    int memoryFD;              // A file descriptor from an export of the memory of the image
 92    std::vector<int> waitFDs;  // File descriptors of semaphores which will be waited on
 93
 94  protected:
 95    using ExternalImageDescriptorVk::ExternalImageDescriptorVk;
 96};
 97
 98// Descriptor for opaque file descriptor image import
 99struct DAWN_NATIVE_EXPORT ExternalImageDescriptorOpaqueFD : ExternalImageDescriptorFD {
100    ExternalImageDescriptorOpaqueFD();
101
102    VkDeviceSize allocationSize;  // Must match VkMemoryAllocateInfo from image creation
103    uint32_t memoryTypeIndex;     // Must match VkMemoryAllocateInfo from image creation
104};
105
106// The plane-wise offset and stride.
107struct DAWN_NATIVE_EXPORT PlaneLayout {
108    uint64_t offset;
109    uint32_t stride;
110};
111
112// Descriptor for dma-buf file descriptor image import
113struct DAWN_NATIVE_EXPORT ExternalImageDescriptorDmaBuf : ExternalImageDescriptorFD {
114    ExternalImageDescriptorDmaBuf();
115
116    static constexpr uint32_t kMaxPlanes = 3;
117    std::array<PlaneLayout, kMaxPlanes> planeLayouts;
118    uint64_t drmModifier;  // DRM modifier of the buffer
119};
120
121// Info struct that is written to in |ExportVulkanImage|.
122struct DAWN_NATIVE_EXPORT ExternalImageExportInfoFD : ExternalImageExportInfoVk {
123  public:
124    // Contains the exported semaphore handles.
125    std::vector<int> semaphoreHandles;
126
127  protected:
128    using ExternalImageExportInfoVk::ExternalImageExportInfoVk;
129};
130
131struct DAWN_NATIVE_EXPORT ExternalImageExportInfoOpaqueFD : ExternalImageExportInfoFD {
132    ExternalImageExportInfoOpaqueFD();
133};
134
135struct DAWN_NATIVE_EXPORT ExternalImageExportInfoDmaBuf : ExternalImageExportInfoFD {
136    ExternalImageExportInfoDmaBuf();
137};
138
139#ifdef __ANDROID__
140
141// Descriptor for AHardwareBuffer image import
142struct DAWN_NATIVE_EXPORT ExternalImageDescriptorAHardwareBuffer : ExternalImageDescriptorVk {
143  public:
144    ExternalImageDescriptorAHardwareBuffer();
145
146    struct AHardwareBuffer* handle;  // The AHardwareBuffer which contains the memory of the image
147    std::vector<int> waitFDs;        // File descriptors of semaphores which will be waited on
148
149  protected:
150    using ExternalImageDescriptorVk::ExternalImageDescriptorVk;
151};
152
153struct DAWN_NATIVE_EXPORT ExternalImageExportInfoAHardwareBuffer : ExternalImageExportInfoFD {
154    ExternalImageExportInfoAHardwareBuffer();
155};
156
157#endif  // __ANDROID__
158
159#endif  // __linux__
160
161// Imports external memory into a Vulkan image. Internally, this uses external memory /
162// semaphore extensions to import the image and wait on the provided synchronizaton
163// primitives before the texture can be used.
164// On failure, returns a nullptr.
165DAWN_NATIVE_EXPORT WGPUTexture WrapVulkanImage(WGPUDevice device,
166                                               const ExternalImageDescriptorVk* descriptor);
167
168// Exports external memory from a Vulkan image. This must be called on wrapped textures
169// before they are destroyed. It writes the semaphore to wait on and the old/new image
170// layouts to |info|. Pass VK_IMAGE_LAYOUT_UNDEFINED as |desiredLayout| if you don't want to
171// perform a layout transition.
172DAWN_NATIVE_EXPORT bool ExportVulkanImage(WGPUTexture texture,
173                                          VkImageLayout desiredLayout,
174                                          ExternalImageExportInfoVk* info);
175// |ExportVulkanImage| with default desiredLayout of VK_IMAGE_LAYOUT_UNDEFINED.
176DAWN_NATIVE_EXPORT bool ExportVulkanImage(WGPUTexture texture, ExternalImageExportInfoVk* info);
177
178}  // namespace dawn::native::vulkan
179
180#endif  // INCLUDE_DAWN_NATIVE_VULKANBACKEND_H_