webgpu/dawn/include/dawn/native/DawnNative.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_DAWNNATIVE_H_
16#define INCLUDE_DAWN_NATIVE_DAWNNATIVE_H_
17
18#include <string>
19#include <vector>
20
21#include "dawn/dawn_proc_table.h"
22#include "dawn/native/dawn_native_export.h"
23#include "dawn/webgpu.h"
24#include "dawn/webgpu_cpp_chained_struct.h"
25
26namespace dawn::platform {
27class Platform;
28} // namespace dawn::platform
29
30namespace wgpu {
31struct AdapterProperties;
32struct DeviceDescriptor;
33struct RequestAdapterOptions;
34} // namespace wgpu
35
36namespace dawn::native {
37
38class InstanceBase;
39class AdapterBase;
40
41// Each toggle is assigned with a TogglesStage, indicating the validation and earliest usage
42// time of the toggle.
43enum class ToggleStage { Instance, Adapter, Device };
44
45// A struct to record the information of a toggle. A toggle is a code path in Dawn device that
46// can be manually configured to run or not outside Dawn, including workarounds, special
47// features and optimizations.
48struct ToggleInfo {
49 const char* name;
50 const char* description;
51 const char* url;
52 ToggleStage stage;
53};
54
55// A struct to record the information of a feature. A feature is a GPU feature that is not
56// required to be supported by all Dawn backends and can only be used when it is enabled on the
57// creation of device.
58struct FeatureInfo {
59 const char* name;
60 const char* description;
61 const char* url;
62 // The enum of feature state, could be stable or experimental. Using an experimental feature
63 // requires the AllowUnsafeAPIs toggle to be enabled.
64 enum class FeatureState { Stable = 0, Experimental };
65 FeatureState featureState;
66};
67
68// An adapter is an object that represent on possibility of creating devices in the system.
69// Most of the time it will represent a combination of a physical GPU and an API. Not that the
70// same GPU can be represented by multiple adapters but on different APIs.
71//
72// The underlying Dawn adapter is owned by the Dawn instance so this class is not RAII but just
73// a reference to an underlying adapter.
74class DAWN_NATIVE_EXPORT Adapter {
75 public:
76 Adapter();
77 // NOLINTNEXTLINE(runtime/explicit)
78 Adapter(AdapterBase* impl);
79 ~Adapter();
80
81 Adapter(const Adapter& other);
82 Adapter& operator=(const Adapter& other);
83
84 // Essentially webgpu.h's wgpuAdapterGetProperties while we don't have WGPUAdapter in
85 // dawn.json
86 void GetProperties(wgpu::AdapterProperties* properties) const;
87 void GetProperties(WGPUAdapterProperties* properties) const;
88
89 std::vector<const char*> GetSupportedExtensions() const;
90 std::vector<const char*> GetSupportedFeatures() const;
91 bool GetLimits(WGPUSupportedLimits* limits) const;
92
93 void SetUseTieredLimits(bool useTieredLimits);
94
95 // Check that the Adapter is able to support importing external images. This is necessary
96 // to implement the swapchain and interop APIs in Chromium.
97 bool SupportsExternalImages() const;
98
99 explicit operator bool() const;
100
101 // Create a device on this adapter. On an error, nullptr is returned.
102 WGPUDevice CreateDevice(const wgpu::DeviceDescriptor* deviceDescriptor);
103 WGPUDevice CreateDevice(const WGPUDeviceDescriptor* deviceDescriptor = nullptr);
104
105 void RequestDevice(const wgpu::DeviceDescriptor* descriptor,
106 WGPURequestDeviceCallback callback,
107 void* userdata);
108 void RequestDevice(const WGPUDeviceDescriptor* descriptor,
109 WGPURequestDeviceCallback callback,
110 void* userdata);
111 void RequestDevice(std::nullptr_t descriptor,
112 WGPURequestDeviceCallback callback,
113 void* userdata) {
114 RequestDevice(static_cast<const wgpu::DeviceDescriptor*>(descriptor), callback, userdata);
115 }
116
117 // Returns the underlying WGPUAdapter object.
118 WGPUAdapter Get() const;
119
120 // Reset the backend device object for testing purposes.
121 void ResetInternalDeviceForTesting();
122
123 private:
124 AdapterBase* mImpl = nullptr;
125};
126
127// Base class for options passed to Instance::DiscoverPhysicalDevices.
128struct DAWN_NATIVE_EXPORT PhysicalDeviceDiscoveryOptionsBase {
129 public:
130 const WGPUBackendType backendType;
131
132 protected:
133 explicit PhysicalDeviceDiscoveryOptionsBase(WGPUBackendType type);
134};
135
136// Deprecated, use PhysicalDeviceDiscoveryOptionsBase instead.
137// TODO(dawn:1774): Remove this.
138using AdapterDiscoveryOptionsBase = PhysicalDeviceDiscoveryOptionsBase;
139
140enum BackendValidationLevel { Full, Partial, Disabled };
141
142// Can be chained in InstanceDescriptor
143struct DAWN_NATIVE_EXPORT DawnInstanceDescriptor : wgpu::ChainedStruct {
144 DawnInstanceDescriptor();
145 static constexpr size_t kFirstMemberAlignment =
146 wgpu::detail::ConstexprMax(alignof(wgpu::ChainedStruct), alignof(uint32_t));
147 alignas(kFirstMemberAlignment) uint32_t additionalRuntimeSearchPathsCount = 0;
148 const char* const* additionalRuntimeSearchPaths;
149 dawn::platform::Platform* platform = nullptr;
150
151 // Equality operators, mostly for testing. Note that this tests
152 // strict pointer-pointer equality if the struct contains member pointers.
153 bool operator==(const DawnInstanceDescriptor& rhs) const;
154};
155
156// Represents a connection to dawn_native and is used for dependency injection, discovering
157// system adapters and injecting custom adapters (like a Swiftshader Vulkan adapter).
158//
159// This is an RAII class for Dawn instances and also controls the lifetime of all adapters
160// for this instance.
161class DAWN_NATIVE_EXPORT Instance {
162 public:
163 explicit Instance(const WGPUInstanceDescriptor* desc = nullptr);
164 ~Instance();
165
166 Instance(const Instance& other) = delete;
167 Instance& operator=(const Instance& other) = delete;
168
169 // Gather all physical devices in the system that can be accessed with no special options.
170 void DiscoverDefaultPhysicalDevices();
171
172 // Adds physical devices that can be discovered with the options provided (like a
173 // getProcAddress). The backend is chosen based on the type of the options used. Returns true on
174 // success.
175 bool DiscoverPhysicalDevices(const PhysicalDeviceDiscoveryOptionsBase* options);
176
177 // Deprecated, use DiscoverDefaultPhysicalDevices and DiscoverPhysicalDevices instead.
178 // TODO(Dawn:1774): Remove these.
179 void DiscoverDefaultAdapters();
180 bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options);
181
182 // Discovers and returns a vector of adapters.
183 // All systems adapters that can be found are returned if no options are passed.
184 // Otherwise, returns adapters based on the `options`. Adapter toggles descriptor can chained
185 // after options.
186 std::vector<Adapter> EnumerateAdapters(const WGPURequestAdapterOptions* options) const;
187 std::vector<Adapter> EnumerateAdapters(
188 const wgpu::RequestAdapterOptions* options = nullptr) const;
189
190 // Deprecated. Call EnumerateAdapters instead.
191 std::vector<Adapter> GetAdapters() const;
192
193 const ToggleInfo* GetToggleInfo(const char* toggleName);
194 const FeatureInfo* GetFeatureInfo(WGPUFeatureName feature);
195
196 // Enables backend validation layers
197 void EnableBackendValidation(bool enableBackendValidation);
198 void SetBackendValidationLevel(BackendValidationLevel validationLevel);
199
200 // Enable debug capture on Dawn startup
201 void EnableBeginCaptureOnStartup(bool beginCaptureOnStartup);
202
203 // Enable / disable the adapter blocklist.
204 void EnableAdapterBlocklist(bool enable);
205
206 uint64_t GetDeviceCountForTesting() const;
207
208 // Returns the underlying WGPUInstance object.
209 WGPUInstance Get() const;
210
211 private:
212 InstanceBase* mImpl = nullptr;
213};
214
215// Backend-agnostic API for dawn_native
216DAWN_NATIVE_EXPORT const DawnProcTable& GetProcs();
217
218// Query the names of all the toggles that are enabled in device
219DAWN_NATIVE_EXPORT std::vector<const char*> GetTogglesUsed(WGPUDevice device);
220
221// Backdoor to get the number of lazy clears for testing
222DAWN_NATIVE_EXPORT size_t GetLazyClearCountForTesting(WGPUDevice device);
223
224// Backdoor to get the number of deprecation warnings for testing
225DAWN_NATIVE_EXPORT size_t GetDeprecationWarningCountForTesting(WGPUDevice device);
226
227// Backdoor to get the number of physical devices an instance knows about for testing
228DAWN_NATIVE_EXPORT size_t GetPhysicalDeviceCountForTesting(WGPUInstance instance);
229
230// Query if texture has been initialized
231DAWN_NATIVE_EXPORT bool IsTextureSubresourceInitialized(
232 WGPUTexture texture,
233 uint32_t baseMipLevel,
234 uint32_t levelCount,
235 uint32_t baseArrayLayer,
236 uint32_t layerCount,
237 WGPUTextureAspect aspect = WGPUTextureAspect_All);
238
239// Backdoor to get the order of the ProcMap for testing
240DAWN_NATIVE_EXPORT std::vector<const char*> GetProcMapNamesForTesting();
241
242DAWN_NATIVE_EXPORT bool DeviceTick(WGPUDevice device);
243
244DAWN_NATIVE_EXPORT bool InstanceProcessEvents(WGPUInstance instance);
245
246// ErrorInjector functions used for testing only. Defined in dawn_native/ErrorInjector.cpp
247DAWN_NATIVE_EXPORT void EnableErrorInjector();
248DAWN_NATIVE_EXPORT void DisableErrorInjector();
249DAWN_NATIVE_EXPORT void ClearErrorInjector();
250DAWN_NATIVE_EXPORT uint64_t AcquireErrorInjectorCallCount();
251DAWN_NATIVE_EXPORT void InjectErrorAt(uint64_t index);
252
253// The different types of external images
254enum ExternalImageType {
255 OpaqueFD,
256 DmaBuf,
257 IOSurface,
258 DXGISharedHandle,
259 D3D11Texture,
260 EGLImage,
261 GLTexture,
262 AHardwareBuffer,
263 Last = AHardwareBuffer,
264};
265
266// Common properties of external images
267struct DAWN_NATIVE_EXPORT ExternalImageDescriptor {
268 public:
269 const WGPUTextureDescriptor* cTextureDescriptor; // Must match image creation params
270 bool isInitialized; // Whether the texture is initialized on import
271 ExternalImageType GetType() const;
272
273 protected:
274 explicit ExternalImageDescriptor(ExternalImageType type);
275
276 private:
277 ExternalImageType mType;
278};
279
280struct DAWN_NATIVE_EXPORT ExternalImageExportInfo {
281 public:
282 bool isInitialized = false; // Whether the texture is initialized after export
283 ExternalImageType GetType() const;
284
285 protected:
286 explicit ExternalImageExportInfo(ExternalImageType type);
287
288 private:
289 ExternalImageType mType;
290};
291
292DAWN_NATIVE_EXPORT bool CheckIsErrorForTesting(void* objectHandle);
293
294DAWN_NATIVE_EXPORT const char* GetObjectLabelForTesting(void* objectHandle);
295
296DAWN_NATIVE_EXPORT uint64_t GetAllocatedSizeForTesting(WGPUBuffer buffer);
297
298} // namespace dawn::native
299
300// Alias the DawnInstanceDescriptor up to wgpu.
301// TODO(dawn:1374) Remove this aliasing once the usages are updated.
302namespace wgpu {
303using dawn::native::DawnInstanceDescriptor;
304} // namespace wgpu
305
306#endif // INCLUDE_DAWN_NATIVE_DAWNNATIVE_H_