webgpu/dawn/include/dawn/native/D3DBackend.h
1// Copyright 2023 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_D3DBACKEND_H_
16#define INCLUDE_DAWN_NATIVE_D3DBACKEND_H_
17
18#include <dxgi1_4.h>
19#include <windows.h>
20#include <wrl/client.h>
21
22#include <memory>
23#include <vector>
24
25#include "dawn/native/DawnNative.h"
26#include "dawn/webgpu_cpp_chained_struct.h"
27
28namespace dawn::native::d3d {
29
30class ExternalImageDXGIImpl;
31
32DAWN_NATIVE_EXPORT Microsoft::WRL::ComPtr<IDXGIAdapter> GetDXGIAdapter(WGPUAdapter adapter);
33
34// Can be chained in WGPURequestAdapterOptions
35struct DAWN_NATIVE_EXPORT RequestAdapterOptionsLUID : wgpu::ChainedStruct {
36 RequestAdapterOptionsLUID();
37
38 ::LUID adapterLUID;
39};
40
41struct DAWN_NATIVE_EXPORT PhysicalDeviceDiscoveryOptions
42 : public PhysicalDeviceDiscoveryOptionsBase {
43 PhysicalDeviceDiscoveryOptions(WGPUBackendType type,
44 Microsoft::WRL::ComPtr<IDXGIAdapter> adapter);
45 Microsoft::WRL::ComPtr<IDXGIAdapter> dxgiAdapter;
46};
47
48// TODO(dawn:1774): Deprecated.
49using AdapterDiscoveryOptions = PhysicalDeviceDiscoveryOptions;
50
51struct DAWN_NATIVE_EXPORT ExternalImageDescriptorDXGISharedHandle : ExternalImageDescriptor {
52 public:
53 ExternalImageDescriptorDXGISharedHandle();
54
55 // Note: SharedHandle must be a handle to a texture object.
56 HANDLE sharedHandle = nullptr;
57};
58
59struct DAWN_NATIVE_EXPORT ExternalImageDescriptorD3D11Texture : ExternalImageDescriptor {
60 public:
61 ExternalImageDescriptorD3D11Texture();
62
63 // Texture is used for creating ExternalImageDXGI with d3d11 backend. It must be an
64 // ID3D11Texture2D object and created from the same ID3D11Device used in the WGPUDevice.
65 Microsoft::WRL::ComPtr<IUnknown> texture;
66};
67
68struct DAWN_NATIVE_EXPORT ExternalImageDXGIFenceDescriptor {
69 // Shared handle for the fence. This never passes ownership to the callee (when used as an input
70 // parameter) or to the caller (when used as a return value or output parameter).
71 HANDLE fenceHandle = nullptr;
72
73 // The value that was previously signaled on this fence and should be waited on.
74 uint64_t fenceValue = 0;
75};
76
77struct DAWN_NATIVE_EXPORT ExternalImageDXGIBeginAccessDescriptor {
78 bool isInitialized = false; // Whether the texture is initialized on import
79 WGPUTextureUsageFlags usage = WGPUTextureUsage_None;
80
81 // A list of fences to wait on before accessing the texture.
82 std::vector<ExternalImageDXGIFenceDescriptor> waitFences;
83
84 // Whether the texture is for a WebGPU swap chain.
85 bool isSwapChainTexture = false;
86};
87
88class DAWN_NATIVE_EXPORT ExternalImageDXGI {
89 public:
90 ~ExternalImageDXGI();
91
92 static std::unique_ptr<ExternalImageDXGI> Create(WGPUDevice device,
93 const ExternalImageDescriptor* descriptor);
94
95 // Returns true if the external image resources are still valid, otherwise BeginAccess() is
96 // guaranteed to fail e.g. after device destruction.
97 bool IsValid() const;
98
99 // Creates WGPUTexture wrapping the DXGI shared handle. The provided wait fences will be
100 // synchronized before using the texture in any command lists. Empty fences (nullptr handle) are
101 // ignored for convenience (EndAccess can return such fences).
102 WGPUTexture BeginAccess(const ExternalImageDXGIBeginAccessDescriptor* descriptor);
103
104 // Returns the signalFence that the client must wait on for correct synchronization. Can return
105 // an empty fence (nullptr handle) if the texture wasn't accessed by Dawn.
106 // Note that merely calling Destroy() on the WGPUTexture does not ensure synchronization.
107 void EndAccess(WGPUTexture texture, ExternalImageDXGIFenceDescriptor* signalFence);
108
109 private:
110 explicit ExternalImageDXGI(std::unique_ptr<ExternalImageDXGIImpl> impl);
111
112 std::unique_ptr<ExternalImageDXGIImpl> mImpl;
113};
114
115// May be chained on SharedTextureMemoryDescriptor
116struct DAWN_NATIVE_EXPORT SharedTextureMemoryID3D11Texture2DDescriptor : wgpu::ChainedStruct {
117 SharedTextureMemoryID3D11Texture2DDescriptor() {
118 sType = static_cast<wgpu::SType>(WGPUSType_SharedTextureMemoryD3D11Texture2DDescriptor);
119 }
120
121 // This ID3D11Texture2D object must be created from the same ID3D11Device used in the
122 // WGPUDevice.
123 Microsoft::WRL::ComPtr<IUnknown> texture;
124};
125
126} // namespace dawn::native::d3d
127
128#endif // INCLUDE_DAWN_NATIVE_D3DBACKEND_H_