blob: ff9b7f099a220e9b65e42542e0533a016deebc32 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Austin Eng376f1c62017-05-30 20:03:44 -04002//
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
Corentin Wallezac67fec2019-01-04 10:30:40 +000015#ifndef DAWNNATIVE_DEVICE_H_
16#define DAWNNATIVE_DEVICE_H_
Austin Eng376f1c62017-05-30 20:03:44 -040017
Tomek Ponitkad7207852020-08-20 13:29:39 +000018#include "dawn_native/Commands.h"
Jiawei Shaoe0390b82021-10-13 04:19:26 +000019#include "dawn_native/ComputePipeline.h"
Corentin Wallezd37523f2018-07-24 13:53:51 +020020#include "dawn_native/Error.h"
François Beaufort3f689a42021-10-04 11:30:02 +000021#include "dawn_native/Features.h"
Corentin Wallez1546bfb2019-07-18 09:25:04 +000022#include "dawn_native/Format.h"
Corentin Wallezd37523f2018-07-24 13:53:51 +020023#include "dawn_native/Forward.h"
Austin Eng02fbf162021-09-13 18:49:09 +000024#include "dawn_native/Limits.h"
Corentin Wallez9e4518b2018-10-15 12:54:30 +000025#include "dawn_native/ObjectBase.h"
Loko Kung8d195d52021-09-28 15:40:01 +000026#include "dawn_native/ObjectType_autogen.h"
27#include "dawn_native/StagingBuffer.h"
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000028#include "dawn_native/Toggles.h"
Austin Eng376f1c62017-05-30 20:03:44 -040029
Jiawei Shao58809d42018-09-19 00:32:52 +000030#include "dawn_native/DawnNative.h"
Corentin Wallez36afbb62018-07-25 17:03:23 +020031#include "dawn_native/dawn_platform.h"
Austin Eng376f1c62017-05-30 20:03:44 -040032
Loko Kung8d195d52021-09-28 15:40:01 +000033#include <mutex>
Jiawei Shaoc243f672021-04-08 01:22:22 +000034#include <utility>
Corentin Wallezcca9c692018-09-06 15:26:48 +020035
Jiawei Shao5e1ca532021-06-04 05:12:06 +000036namespace dawn_platform {
37 class WorkerTaskPool;
38} // namespace dawn_platform
39
Corentin Wallez49a65d02018-07-24 16:45:45 +020040namespace dawn_native {
Corentin Wallezec18f962019-01-10 10:50:54 +000041 class AdapterBase;
Jiawei Shao5e1ca532021-06-04 05:12:06 +000042 class AsyncTaskManager;
Austin Engb98f0fa2019-07-26 19:08:18 +000043 class AttachmentState;
Austin Eng8a488c12019-08-13 22:12:54 +000044 class AttachmentStateBlueprint;
Austin Enga80993d2020-03-20 21:56:30 +000045 class BindGroupLayoutBase;
Jiawei Shaoc74af7032021-05-02 03:22:30 +000046 class CallbackTaskManager;
Austin Enga80993d2020-03-20 21:56:30 +000047 class DynamicUploader;
Austin Engef9e4412021-02-19 18:17:22 +000048 class ErrorScopeStack;
Brandon Jones0e92e9b2021-04-01 20:46:42 +000049 class ExternalTextureBase;
Brandon Jones47b6b682021-04-08 04:25:11 +000050 class OwnedCompilationMessages;
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +000051 class PersistentCache;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +000052 class StagingBufferBase;
Jiawei Shao5e1ca532021-06-04 05:12:06 +000053 struct CallbackTask;
Yan, Shaobodb8766b2020-11-04 02:30:16 +000054 struct InternalPipelineStore;
Austin Eng0d948f72020-12-07 18:12:13 +000055 struct ShaderModuleParseResult;
Austin Engf0b761f2018-12-03 16:57:34 +000056
Corentin Wallez12571522021-03-25 23:50:24 +000057 class DeviceBase : public RefCounted {
Corentin Wallezc1400f02017-11-24 13:59:42 -050058 public:
Austin Eng3482a802021-11-23 18:03:16 +000059 DeviceBase(AdapterBase* adapter, const DawnDeviceDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050060 virtual ~DeviceBase();
Austin Eng376f1c62017-05-30 20:03:44 -040061
Corentin Walleza0afd312020-04-01 12:07:43 +000062 void HandleError(InternalErrorType type, const char* message);
Austin Eng376f1c62017-05-30 20:03:44 -040063
Corentin Wallez52f23832018-07-16 17:40:08 +020064 bool ConsumedError(MaybeError maybeError) {
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020065 if (DAWN_UNLIKELY(maybeError.IsError())) {
Austin Engb11bd2d2019-09-18 21:03:41 +000066 ConsumeError(maybeError.AcquireError());
Corentin Wallez52f23832018-07-16 17:40:08 +020067 return true;
68 }
69 return false;
70 }
71
Corentin Walleze09869e2019-10-09 10:11:00 +000072 template <typename T>
73 bool ConsumedError(ResultOrError<T> resultOrError, T* result) {
74 if (DAWN_UNLIKELY(resultOrError.IsError())) {
75 ConsumeError(resultOrError.AcquireError());
76 return true;
77 }
78 *result = resultOrError.AcquireSuccess();
79 return false;
80 }
81
Corentin Wallez2bf51562021-10-01 18:15:40 +000082 template <typename... Args>
83 bool ConsumedError(MaybeError maybeError, const char* formatStr, const Args&... args) {
84 if (DAWN_UNLIKELY(maybeError.IsError())) {
85 std::unique_ptr<ErrorData> error = maybeError.AcquireError();
86 if (error->GetType() == InternalErrorType::Validation) {
87 std::string out;
88 absl::UntypedFormatSpec format(formatStr);
89 if (absl::FormatUntyped(&out, format, {absl::FormatArg(args)...})) {
90 error->AppendContext(std::move(out));
Brandon Jones25145662021-11-03 01:31:19 +000091 } else {
92 error->AppendContext(
93 absl::StrFormat("[Failed to format error: \"%s\"]", formatStr));
Corentin Wallez2bf51562021-10-01 18:15:40 +000094 }
95 }
96 ConsumeError(std::move(error));
97 return true;
98 }
99 return false;
100 }
101
Brandon Jones9b643b72021-09-29 18:39:23 +0000102 template <typename T, typename... Args>
103 bool ConsumedError(ResultOrError<T> resultOrError,
104 T* result,
105 const char* formatStr,
106 const Args&... args) {
107 if (DAWN_UNLIKELY(resultOrError.IsError())) {
108 std::unique_ptr<ErrorData> error = resultOrError.AcquireError();
109 if (error->GetType() == InternalErrorType::Validation) {
110 std::string out;
111 absl::UntypedFormatSpec format(formatStr);
112 if (absl::FormatUntyped(&out, format, {absl::FormatArg(args)...})) {
113 error->AppendContext(std::move(out));
Brandon Jones25145662021-11-03 01:31:19 +0000114 } else {
115 error->AppendContext(
116 absl::StrFormat("[Failed to format error: \"%s\"]", formatStr));
Brandon Jones9b643b72021-09-29 18:39:23 +0000117 }
118 }
119 ConsumeError(std::move(error));
120 return true;
121 }
122 *result = resultOrError.AcquireSuccess();
123 return false;
124 }
125
Loko Kung8d195d52021-09-28 15:40:01 +0000126 MaybeError ValidateObject(const ApiObjectBase* object) const;
Corentin Walleza594f8f2019-02-13 13:09:18 +0000127
Corentin Wallezd77fd5f2019-01-30 16:07:48 +0000128 AdapterBase* GetAdapter() const;
Austin Engd4ce7362019-08-13 19:00:34 +0000129 dawn_platform::Platform* GetPlatform() const;
Corentin Wallezd77fd5f2019-01-30 16:07:48 +0000130
Corentin Wallez1f6c8c42019-10-23 11:57:41 +0000131 // Returns the Format corresponding to the wgpu::TextureFormat or an error if the format
132 // isn't a valid wgpu::TextureFormat or isn't supported by this device.
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000133 // The pointer returned has the same lifetime as the device.
Corentin Wallez1f6c8c42019-10-23 11:57:41 +0000134 ResultOrError<const Format*> GetInternalFormat(wgpu::TextureFormat format) const;
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000135
Corentin Wallez1f6c8c42019-10-23 11:57:41 +0000136 // Returns the Format corresponding to the wgpu::TextureFormat and assumes the format is
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000137 // valid and supported.
138 // The reference returned has the same lifetime as the device.
Corentin Wallez1f6c8c42019-10-23 11:57:41 +0000139 const Format& GetValidInternalFormat(wgpu::TextureFormat format) const;
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000140
Corentin Wallez50f99582021-03-31 18:36:32 +0000141 virtual ResultOrError<Ref<CommandBufferBase>> CreateCommandBuffer(
Corentin Wallez321c1222019-11-13 17:00:37 +0000142 CommandEncoder* encoder,
Corentin Wallez4b90c472019-07-10 20:43:13 +0000143 const CommandBufferDescriptor* descriptor) = 0;
Austin Eng376f1c62017-05-30 20:03:44 -0400144
Corentin Wallez62139fc2020-09-28 19:35:14 +0000145 ExecutionSerial GetCompletedCommandSerial() const;
146 ExecutionSerial GetLastSubmittedCommandSerial() const;
Brandon Jones4ad35862020-10-15 16:21:03 +0000147 ExecutionSerial GetFutureSerial() const;
Corentin Wallez62139fc2020-09-28 19:35:14 +0000148 ExecutionSerial GetPendingCommandSerial() const;
Corentin Wallezdbb57292017-06-14 13:33:45 -0400149
Corentin Wallez9fc65342018-07-18 15:28:38 +0200150 // Many Dawn objects are completely immutable once created which means that if two
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000151 // creations are given the same arguments, they can return the same object. Reusing
Corentin Wallezc1400f02017-11-24 13:59:42 -0500152 // objects will help make comparisons between objects by a single pointer comparison.
153 //
154 // Technically no object is immutable as they have a reference count, and an
155 // application with reference-counting issues could "see" that objects are reused.
156 // This is solved by automatic-reference counting, and also the fact that when using
157 // the client-server wire every creation will get a different proxy object, with a
158 // different reference count.
159 //
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000160 // When trying to create an object, we give both the descriptor and an example of what
161 // the created object will be, the "blueprint". The blueprint is just a FooBase object
Corentin Wallezc1400f02017-11-24 13:59:42 -0500162 // instead of a backend Foo object. If the blueprint doesn't match an object in the
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000163 // cache, then the descriptor is used to make a new object.
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000164 ResultOrError<Ref<BindGroupLayoutBase>> GetOrCreateBindGroupLayout(
Brandon Jones1f48c972021-09-01 22:15:41 +0000165 const BindGroupLayoutDescriptor* descriptor,
166 PipelineCompatibilityToken pipelineCompatibilityToken = PipelineCompatibilityToken(0));
Corentin Wallezc1400f02017-11-24 13:59:42 -0500167 void UncacheBindGroupLayout(BindGroupLayoutBase* obj);
Austin Eng376f1c62017-05-30 20:03:44 -0400168
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000169 BindGroupLayoutBase* GetEmptyBindGroupLayout();
Tomek Ponitkabf27bd72020-06-29 11:13:43 +0000170
Corentin Wallez1152bba2019-05-01 13:48:47 +0000171 void UncacheComputePipeline(ComputePipelineBase* obj);
172
Corentin Wallez50f99582021-03-31 18:36:32 +0000173 ResultOrError<Ref<PipelineLayoutBase>> GetOrCreatePipelineLayout(
Corentin Wallez0ee98592019-05-01 12:57:27 +0000174 const PipelineLayoutDescriptor* descriptor);
175 void UncachePipelineLayout(PipelineLayoutBase* obj);
176
Corentin Wallez94274b62019-05-02 15:30:56 +0000177 void UncacheRenderPipeline(RenderPipelineBase* obj);
178
Corentin Wallez50f99582021-03-31 18:36:32 +0000179 ResultOrError<Ref<SamplerBase>> GetOrCreateSampler(const SamplerDescriptor* descriptor);
Idan Raiter2bc31692019-05-20 18:16:34 +0000180 void UncacheSampler(SamplerBase* obj);
181
Corentin Wallez50f99582021-03-31 18:36:32 +0000182 ResultOrError<Ref<ShaderModuleBase>> GetOrCreateShaderModule(
Austin Eng0d948f72020-12-07 18:12:13 +0000183 const ShaderModuleDescriptor* descriptor,
Zhaoming Jiangfdc35e62021-06-18 13:29:39 +0000184 ShaderModuleParseResult* parseResult,
185 OwnedCompilationMessages* compilationMessages);
Corentin Wallezc5351982019-05-01 13:27:07 +0000186 void UncacheShaderModule(ShaderModuleBase* obj);
187
Austin Eng8a488c12019-08-13 22:12:54 +0000188 Ref<AttachmentState> GetOrCreateAttachmentState(AttachmentStateBlueprint* blueprint);
189 Ref<AttachmentState> GetOrCreateAttachmentState(
190 const RenderBundleEncoderDescriptor* descriptor);
Austin Engbb10a912019-08-07 21:47:24 +0000191 Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPipelineDescriptor* descriptor);
192 Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPassDescriptor* descriptor);
Austin Engb98f0fa2019-07-26 19:08:18 +0000193 void UncacheAttachmentState(AttachmentState* obj);
194
Corentin Wallez0af4a832021-04-19 08:52:35 +0000195 // Object creation methods that be used in a reentrant manner.
196 ResultOrError<Ref<BindGroupBase>> CreateBindGroup(const BindGroupDescriptor* descriptor);
197 ResultOrError<Ref<BindGroupLayoutBase>> CreateBindGroupLayout(
Li Hao551e7a12021-07-02 09:51:18 +0000198 const BindGroupLayoutDescriptor* descriptor,
199 bool allowInternalBinding = false);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000200 ResultOrError<Ref<BufferBase>> CreateBuffer(const BufferDescriptor* descriptor);
201 ResultOrError<Ref<ComputePipelineBase>> CreateComputePipeline(
202 const ComputePipelineDescriptor* descriptor);
203 MaybeError CreateComputePipelineAsync(
204 const ComputePipelineDescriptor* descriptor,
205 WGPUCreateComputePipelineAsyncCallback callback,
206 void* userdata);
207 ResultOrError<Ref<ExternalTextureBase>> CreateExternalTexture(
208 const ExternalTextureDescriptor* descriptor);
209 ResultOrError<Ref<PipelineLayoutBase>> CreatePipelineLayout(
210 const PipelineLayoutDescriptor* descriptor);
211 ResultOrError<Ref<QuerySetBase>> CreateQuerySet(const QuerySetDescriptor* descriptor);
212 ResultOrError<Ref<RenderBundleEncoder>> CreateRenderBundleEncoder(
213 const RenderBundleEncoderDescriptor* descriptor);
214 ResultOrError<Ref<RenderPipelineBase>> CreateRenderPipeline(
Brandon Jones41c87d92021-05-21 05:01:38 +0000215 const RenderPipelineDescriptor* descriptor);
Jiawei Shao2aee6ee2021-08-26 00:39:14 +0000216 MaybeError CreateRenderPipelineAsync(const RenderPipelineDescriptor* descriptor,
217 WGPUCreateRenderPipelineAsyncCallback callback,
218 void* userdata);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000219 ResultOrError<Ref<SamplerBase>> CreateSampler(const SamplerDescriptor* descriptor);
220 ResultOrError<Ref<ShaderModuleBase>> CreateShaderModule(
221 const ShaderModuleDescriptor* descriptor,
Zhaoming Jiangfdc35e62021-06-18 13:29:39 +0000222 OwnedCompilationMessages* compilationMessages = nullptr);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000223 ResultOrError<Ref<SwapChainBase>> CreateSwapChain(Surface* surface,
224 const SwapChainDescriptor* descriptor);
225 ResultOrError<Ref<TextureBase>> CreateTexture(const TextureDescriptor* descriptor);
226 ResultOrError<Ref<TextureViewBase>> CreateTextureView(
227 TextureBase* texture,
228 const TextureViewDescriptor* descriptor);
229
230 // Implementation of API object creation methods. DO NOT use them in a reentrant manner.
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000231 BindGroupBase* APICreateBindGroup(const BindGroupDescriptor* descriptor);
232 BindGroupLayoutBase* APICreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor);
233 BufferBase* APICreateBuffer(const BufferDescriptor* descriptor);
234 CommandEncoder* APICreateCommandEncoder(const CommandEncoderDescriptor* descriptor);
235 ComputePipelineBase* APICreateComputePipeline(const ComputePipelineDescriptor* descriptor);
236 PipelineLayoutBase* APICreatePipelineLayout(const PipelineLayoutDescriptor* descriptor);
237 QuerySetBase* APICreateQuerySet(const QuerySetDescriptor* descriptor);
238 void APICreateComputePipelineAsync(const ComputePipelineDescriptor* descriptor,
239 WGPUCreateComputePipelineAsyncCallback callback,
240 void* userdata);
Brandon Jones41c87d92021-05-21 05:01:38 +0000241 void APICreateRenderPipelineAsync(const RenderPipelineDescriptor* descriptor,
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000242 WGPUCreateRenderPipelineAsyncCallback callback,
243 void* userdata);
244 RenderBundleEncoder* APICreateRenderBundleEncoder(
Austin Eng8a488c12019-08-13 22:12:54 +0000245 const RenderBundleEncoderDescriptor* descriptor);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000246 RenderPipelineBase* APICreateRenderPipeline(const RenderPipelineDescriptor* descriptor);
Brandon Jones0e92e9b2021-04-01 20:46:42 +0000247 ExternalTextureBase* APICreateExternalTexture(const ExternalTextureDescriptor* descriptor);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000248 SamplerBase* APICreateSampler(const SamplerDescriptor* descriptor);
249 ShaderModuleBase* APICreateShaderModule(const ShaderModuleDescriptor* descriptor);
250 SwapChainBase* APICreateSwapChain(Surface* surface, const SwapChainDescriptor* descriptor);
251 TextureBase* APICreateTexture(const TextureDescriptor* descriptor);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000252
Yan, Shaobodb8766b2020-11-04 02:30:16 +0000253 InternalPipelineStore* GetInternalPipelineStore();
Austin Eng376f1c62017-05-30 20:03:44 -0400254
Austin Engcf1fdf42020-06-15 23:42:13 +0000255 // For Dawn Wire
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000256 BufferBase* APICreateErrorBuffer();
Austin Engcf1fdf42020-06-15 23:42:13 +0000257
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000258 QueueBase* APIGetQueue();
Corentin Wallez8a437942020-04-17 16:45:17 +0000259
Austin Engbffc9662021-09-17 15:36:00 +0000260 bool APIGetLimits(SupportedLimits* limits);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000261 void APIInjectError(wgpu::ErrorType type, const char* message);
262 bool APITick();
Austin Eng8b07e432018-12-01 03:20:19 +0000263
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000264 void APISetDeviceLostCallback(wgpu::DeviceLostCallback callback, void* userdata);
265 void APISetUncapturedErrorCallback(wgpu::ErrorCallback callback, void* userdata);
Zhaoming Jiangb44000e2021-06-04 07:10:56 +0000266 void APISetLoggingCallback(wgpu::LoggingCallback callback, void* userdata);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000267 void APIPushErrorScope(wgpu::ErrorFilter filter);
268 bool APIPopErrorScope(wgpu::ErrorCallback callback, void* userdata);
Natasha Lee0ecc48e2020-01-15 19:02:13 +0000269
270 MaybeError ValidateIsAlive() const;
271
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +0000272 PersistentCache* GetPersistentCache();
273
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000274 virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(
275 size_t size) = 0;
276 virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
Austin Engcf52d712019-04-05 20:51:29 +0000277 uint64_t sourceOffset,
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000278 BufferBase* destination,
Austin Engcf52d712019-04-05 20:51:29 +0000279 uint64_t destinationOffset,
280 uint64_t size) = 0;
Tomek Ponitkad7207852020-08-20 13:29:39 +0000281 virtual MaybeError CopyFromStagingToTexture(const StagingBufferBase* source,
282 const TextureDataLayout& src,
283 TextureCopy* dst,
284 const Extent3D& copySizePixels) = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000285
Bryan Bernhart450e2122019-09-18 22:06:41 +0000286 DynamicUploader* GetDynamicUploader() const;
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000287
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000288 // The device state which is a combination of creation state and loss state.
289 //
290 // - BeingCreated: the device didn't finish creation yet and the frontend cannot be used
291 // (both for the application calling WebGPU, or re-entrant calls). No work exists on
292 // the GPU timeline.
293 // - Alive: the device is usable and might have work happening on the GPU timeline.
294 // - BeingDisconnected: the device is no longer usable because we are waiting for all
295 // work on the GPU timeline to finish. (this is to make validation prevent the
296 // application from adding more work during the transition from Available to
297 // Disconnected)
298 // - Disconnected: there is no longer work happening on the GPU timeline and the CPU data
299 // structures can be safely destroyed without additional synchronization.
Loko Kung2494c9b2021-12-01 17:55:30 +0000300 // - Destroyed: the device is disconnected and resources have been reclaimed.
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000301 enum class State {
302 BeingCreated,
303 Alive,
304 BeingDisconnected,
305 Disconnected,
Loko Kung2494c9b2021-12-01 17:55:30 +0000306 Destroyed,
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000307 };
308 State GetState() const;
309 bool IsLost() const;
Loko Kungfc5a7d42021-10-12 17:46:26 +0000310 void TrackObject(ApiObjectBase* object);
Loko Kung8d195d52021-09-28 15:40:01 +0000311 std::mutex* GetObjectListMutex(ObjectType type);
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000312
François Beaufort3f689a42021-10-04 11:30:02 +0000313 std::vector<const char*> GetEnabledFeatures() const;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000314 std::vector<const char*> GetTogglesUsed() const;
François Beaufort3f689a42021-10-04 11:30:02 +0000315 bool IsFeatureEnabled(Feature feature) const;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000316 bool IsToggleEnabled(Toggle toggle) const;
Austin Eng4d156092019-11-21 00:48:39 +0000317 bool IsValidationEnabled() const;
Idan Raiterbcc65f22020-07-01 21:37:57 +0000318 bool IsRobustnessEnabled() const;
Natasha Leee69627f2019-07-26 17:54:48 +0000319 size_t GetLazyClearCountForTesting();
320 void IncrementLazyClearCountForTesting();
Corentin Wallez8a437942020-04-17 16:45:17 +0000321 size_t GetDeprecationWarningCountForTesting();
322 void EmitDeprecationWarning(const char* warning);
Zhaoming Jiangb44000e2021-06-04 07:10:56 +0000323 void EmitLog(const char* message);
324 void EmitLog(WGPULoggingType loggingType, const char* message);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000325 void APILoseForTesting();
Corentin Walleze91975c2021-03-29 15:40:55 +0000326 QueueBase* GetQueue() const;
Brandon Jones4ad35862020-10-15 16:21:03 +0000327
328 // AddFutureSerial is used to update the mFutureSerial with the max serial needed to be
329 // ticked in order to clean up all pending callback work or to execute asynchronous resource
330 // writes. It should be given the serial that a callback is tracked with, so that once that
331 // serial is completed, it can be resolved and cleaned up. This is so that when there is no
332 // gpu work (the last submitted serial has not moved beyond the completed serial), Tick can
333 // still check if we have pending work to take care of, rather than hanging and never
334 // reaching the serial the work will be executed on.
335 void AddFutureSerial(ExecutionSerial serial);
Brandon Jonesb6f4d532020-11-13 02:11:12 +0000336 // Check for passed fences and set the new completed serial
Corentin Wallez6870e6d2021-04-07 18:09:21 +0000337 MaybeError CheckPassedSerials();
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000338
Corentin Wallez1bc1ed42021-03-30 10:21:36 +0000339 MaybeError Tick();
340
Jiawei Shao829d1652021-11-17 05:00:44 +0000341 // TODO(crbug.com/dawn/839): Organize the below backend-specific parameters into the struct
342 // BackendMetadata that we can query from the device.
Tomek Ponitkad7207852020-08-20 13:29:39 +0000343 virtual uint32_t GetOptimalBytesPerRowAlignment() const = 0;
344 virtual uint64_t GetOptimalBufferToTextureCopyOffsetAlignment() const = 0;
345
Hao Licdbd2952021-01-05 02:39:18 +0000346 virtual float GetTimestampPeriodInNS() const = 0;
347
Jiawei Shao829d1652021-11-17 05:00:44 +0000348 virtual bool ShouldDuplicateNumWorkgroupsForDispatchIndirect(
349 ComputePipelineBase* computePipeline) const;
350
Austin Eng91851e22021-10-13 18:57:18 +0000351 const CombinedLimits& GetLimits() const;
352
Jiawei Shao5e1ca532021-06-04 05:12:06 +0000353 AsyncTaskManager* GetAsyncTaskManager() const;
354 CallbackTaskManager* GetCallbackTaskManager() const;
355 dawn_platform::WorkerTaskPool* GetWorkerTaskPool() const;
356
357 void AddComputePipelineAsyncCallbackTask(Ref<ComputePipelineBase> pipeline,
358 std::string errorMessage,
359 WGPUCreateComputePipelineAsyncCallback callback,
Jiawei Shao8fd1eb52021-10-13 00:43:05 +0000360 void* userdata);
Jiawei Shao4ecfc582021-09-17 07:39:00 +0000361 void AddRenderPipelineAsyncCallbackTask(Ref<RenderPipelineBase> pipeline,
362 std::string errorMessage,
363 WGPUCreateRenderPipelineAsyncCallback callback,
Jiawei Shao3c0925b2021-09-29 00:49:01 +0000364 void* userdata);
Jiawei Shao5e1ca532021-06-04 05:12:06 +0000365
Brandon Jones1f48c972021-09-01 22:15:41 +0000366 PipelineCompatibilityToken GetNextPipelineCompatibilityToken();
367
Brandon Jonesba662952021-09-23 21:26:33 +0000368 const std::string& GetLabel() const;
369 void APISetLabel(const char* label);
Loko Kung2494c9b2021-12-01 17:55:30 +0000370 void APIDestroy();
Brandon Jonesba662952021-09-23 21:26:33 +0000371
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000372 protected:
Loko Kung2f3fe952021-10-12 18:53:57 +0000373 // Constructor used only for mocking and testing.
374 DeviceBase();
375
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000376 void SetToggle(Toggle toggle, bool isEnabled);
Corentin Wallez022d0742020-04-06 16:55:22 +0000377 void ForceSetToggle(Toggle toggle, bool isEnabled);
Corentin Wallez09ee5eb2020-04-07 15:10:17 +0000378
Corentin Wallez8a437942020-04-17 16:45:17 +0000379 MaybeError Initialize(QueueBase* defaultQueue);
Loko Kungfc5a7d42021-10-12 17:46:26 +0000380 void DestroyObjects();
381 void Destroy();
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000382
Natasha Lee351c95a2020-05-07 21:52:54 +0000383 // Incrememt mLastSubmittedSerial when we submit the next serial
384 void IncrementLastSubmittedCommandSerial();
Natasha Lee351c95a2020-05-07 21:52:54 +0000385
Corentin Wallezc1400f02017-11-24 13:59:42 -0500386 private:
Corentin Wallez50f99582021-03-31 18:36:32 +0000387 virtual ResultOrError<Ref<BindGroupBase>> CreateBindGroupImpl(
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000388 const BindGroupDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000389 virtual ResultOrError<Ref<BindGroupLayoutBase>> CreateBindGroupLayoutImpl(
Brandon Jones1f48c972021-09-01 22:15:41 +0000390 const BindGroupLayoutDescriptor* descriptor,
391 PipelineCompatibilityToken pipelineCompatibilityToken) = 0;
Corentin Wallez8a991992020-07-08 19:45:40 +0000392 virtual ResultOrError<Ref<BufferBase>> CreateBufferImpl(
393 const BufferDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000394 virtual ResultOrError<Ref<PipelineLayoutBase>> CreatePipelineLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200395 const PipelineLayoutDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000396 virtual ResultOrError<Ref<QuerySetBase>> CreateQuerySetImpl(
Hao Lib6eff5a2020-06-11 00:34:14 +0000397 const QuerySetDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000398 virtual ResultOrError<Ref<SamplerBase>> CreateSamplerImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200399 const SamplerDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000400 virtual ResultOrError<Ref<ShaderModuleBase>> CreateShaderModuleImpl(
Austin Eng0d948f72020-12-07 18:12:13 +0000401 const ShaderModuleDescriptor* descriptor,
402 ShaderModuleParseResult* parseResult) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000403 virtual ResultOrError<Ref<SwapChainBase>> CreateSwapChainImpl(
Corentin Wallez7be2a712019-02-15 11:15:58 +0000404 const SwapChainDescriptor* descriptor) = 0;
Corentin Wallezd26ee852020-01-25 10:05:40 +0000405 // Note that previousSwapChain may be nullptr, or come from a different backend.
Corentin Wallez50f99582021-03-31 18:36:32 +0000406 virtual ResultOrError<Ref<NewSwapChainBase>> CreateSwapChainImpl(
Corentin Wallezd87e6762020-01-23 17:20:38 +0000407 Surface* surface,
Corentin Wallezd26ee852020-01-25 10:05:40 +0000408 NewSwapChainBase* previousSwapChain,
Corentin Wallezd87e6762020-01-23 17:20:38 +0000409 const SwapChainDescriptor* descriptor) = 0;
Rafael Cintron0e9320b2020-04-23 19:47:12 +0000410 virtual ResultOrError<Ref<TextureBase>> CreateTextureImpl(
Jiawei Shao425428f2018-08-27 08:44:48 +0800411 const TextureDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000412 virtual ResultOrError<Ref<TextureViewBase>> CreateTextureViewImpl(
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000413 TextureBase* texture,
414 const TextureViewDescriptor* descriptor) = 0;
Jiawei Shao8fd1eb52021-10-13 00:43:05 +0000415 virtual Ref<ComputePipelineBase> CreateUninitializedComputePipelineImpl(
416 const ComputePipelineDescriptor* descriptor) = 0;
417 virtual Ref<RenderPipelineBase> CreateUninitializedRenderPipelineImpl(
418 const RenderPipelineDescriptor* descriptor) = 0;
Brandon Jonesba662952021-09-23 21:26:33 +0000419 virtual void SetLabelImpl();
Corentin Wallez1ae19e82018-05-17 17:09:07 -0400420
Corentin Wallez1bc1ed42021-03-30 10:21:36 +0000421 virtual MaybeError TickImpl() = 0;
Zhaoming Jiang28497122021-07-21 08:59:09 +0000422 void FlushCallbackTaskQueue();
Corentin Wallez1bc1ed42021-03-30 10:21:36 +0000423
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000424 ResultOrError<Ref<BindGroupLayoutBase>> CreateEmptyBindGroupLayout();
425
Jiawei Shao8fd1eb52021-10-13 00:43:05 +0000426 Ref<ComputePipelineBase> GetCachedComputePipeline(
427 ComputePipelineBase* uninitializedComputePipeline);
Jiawei Shao3c0925b2021-09-29 00:49:01 +0000428 Ref<RenderPipelineBase> GetCachedRenderPipeline(
429 RenderPipelineBase* uninitializedRenderPipeline);
Jiawei Shao2aee6ee2021-08-26 00:39:14 +0000430 Ref<ComputePipelineBase> AddOrGetCachedComputePipeline(
Jiawei Shao8fd1eb52021-10-13 00:43:05 +0000431 Ref<ComputePipelineBase> computePipeline);
Jiawei Shao3c0925b2021-09-29 00:49:01 +0000432 Ref<RenderPipelineBase> AddOrGetCachedRenderPipeline(
433 Ref<RenderPipelineBase> renderPipeline);
Jiawei Shao8fd1eb52021-10-13 00:43:05 +0000434 virtual void InitializeComputePipelineAsyncImpl(
435 Ref<ComputePipelineBase> computePipeline,
436 WGPUCreateComputePipelineAsyncCallback callback,
437 void* userdata);
Jiawei Shao3c0925b2021-09-29 00:49:01 +0000438 virtual void InitializeRenderPipelineAsyncImpl(
439 Ref<RenderPipelineBase> renderPipeline,
440 WGPUCreateRenderPipelineAsyncCallback callback,
441 void* userdata);
Jiawei Shaoc243f672021-04-08 01:22:22 +0000442
Austin Eng3482a802021-11-23 18:03:16 +0000443 void ApplyToggleOverrides(const DawnDeviceDescriptor* deviceDescriptor);
444 void ApplyFeatures(const DawnDeviceDescriptor* deviceDescriptor);
Jiawei Shao574b9512019-08-02 00:06:38 +0000445
Natasha Lee28232ce2019-06-11 18:11:05 +0000446 void SetDefaultToggles();
Corentin Wallez52f23832018-07-16 17:40:08 +0200447
Rafael Cintron69c68d02020-01-10 17:58:28 +0000448 void ConsumeError(std::unique_ptr<ErrorData> error);
Austin Engb11bd2d2019-09-18 21:03:41 +0000449
Natasha Lee351c95a2020-05-07 21:52:54 +0000450 // Each backend should implement to check their passed fences if there are any and return a
451 // completed serial. Return 0 should indicate no fences to check.
Corentin Wallez6870e6d2021-04-07 18:09:21 +0000452 virtual ResultOrError<ExecutionSerial> CheckAndUpdateCompletedSerials() = 0;
Natasha Lee783cd5a2020-06-04 02:26:46 +0000453 // During shut down of device, some operations might have been started since the last submit
454 // and waiting on a serial that doesn't have a corresponding fence enqueued. Fake serials to
455 // make all commands look completed.
456 void AssumeCommandsComplete();
Brandon Jones4ad35862020-10-15 16:21:03 +0000457 bool IsDeviceIdle();
458
Natasha Lee351c95a2020-05-07 21:52:54 +0000459 // mCompletedSerial tracks the last completed command serial that the fence has returned.
460 // mLastSubmittedSerial tracks the last submitted command serial.
461 // During device removal, the serials could be artificially incremented
462 // to make it appear as if commands have been compeleted. They can also be artificially
463 // incremented when no work is being done in the GPU so CPU operations don't have to wait on
464 // stale serials.
Brandon Jones4ad35862020-10-15 16:21:03 +0000465 // mFutureSerial tracks the largest serial we need to tick to for asynchronous commands or
466 // callbacks to fire
Corentin Wallez62139fc2020-09-28 19:35:14 +0000467 ExecutionSerial mCompletedSerial = ExecutionSerial(0);
468 ExecutionSerial mLastSubmittedSerial = ExecutionSerial(0);
Brandon Jones4ad35862020-10-15 16:21:03 +0000469 ExecutionSerial mFutureSerial = ExecutionSerial(0);
Natasha Lee351c95a2020-05-07 21:52:54 +0000470
Loko Kungfc5a7d42021-10-12 17:46:26 +0000471 // DestroyImpl is used to clean up and release resources used by device, does not wait for
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000472 // GPU or check errors.
Loko Kungfc5a7d42021-10-12 17:46:26 +0000473 virtual void DestroyImpl() = 0;
Natasha Lee2c8a17e2019-12-16 23:36:16 +0000474
475 // WaitForIdleForDestruction waits for GPU to finish, checks errors and gets ready for
476 // destruction. This is only used when properly destructing the device. For a real
477 // device loss, this function doesn't need to be called since the driver already closed all
478 // resources.
479 virtual MaybeError WaitForIdleForDestruction() = 0;
480
Austin Engef9e4412021-02-19 18:17:22 +0000481 wgpu::ErrorCallback mUncapturedErrorCallback = nullptr;
482 void* mUncapturedErrorUserdata = nullptr;
483
Zhaoming Jiangb44000e2021-06-04 07:10:56 +0000484 wgpu::LoggingCallback mLoggingCallback = nullptr;
485 void* mLoggingUserdata = nullptr;
486
Natasha Lee9bba4a92019-12-18 18:59:20 +0000487 wgpu::DeviceLostCallback mDeviceLostCallback = nullptr;
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000488 void* mDeviceLostUserdata = nullptr;
Natasha Lee9bba4a92019-12-18 18:59:20 +0000489
Austin Engef9e4412021-02-19 18:17:22 +0000490 std::unique_ptr<ErrorScopeStack> mErrorScopeStack;
491
Austin Engf6ef7532021-01-13 17:54:37 +0000492 // The Device keeps a ref to the Instance so that any live Device keeps the Instance alive.
493 // The Instance shouldn't need to ref child objects so this shouldn't introduce ref cycles.
494 // The Device keeps a simple pointer to the Adapter because the Adapter is owned by the
495 // Instance.
496 Ref<InstanceBase> mInstance;
Corentin Wallezec18f962019-01-10 10:50:54 +0000497 AdapterBase* mAdapter = nullptr;
498
Corentin Wallezc1400f02017-11-24 13:59:42 -0500499 // The object caches aren't exposed in the header as they would require a lot of
500 // additional includes.
501 struct Caches;
Corentin Wallezcca9c692018-09-06 15:26:48 +0200502 std::unique_ptr<Caches> mCaches;
Austin Eng376f1c62017-05-30 20:03:44 -0400503
Tomek Ponitkabf27bd72020-06-29 11:13:43 +0000504 Ref<BindGroupLayoutBase> mEmptyBindGroupLayout;
505
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000506 std::unique_ptr<DynamicUploader> mDynamicUploader;
Jiawei Shao5e1ca532021-06-04 05:12:06 +0000507 std::unique_ptr<AsyncTaskManager> mAsyncTaskManager;
Corentin Wallez6d315da2021-02-04 15:33:42 +0000508 Ref<QueueBase> mQueue;
Corentin Wallez8a437942020-04-17 16:45:17 +0000509
510 struct DeprecationWarnings;
511 std::unique_ptr<DeprecationWarnings> mDeprecationWarnings;
Austin Engf0b761f2018-12-03 16:57:34 +0000512
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000513 State mState = State::BeingCreated;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000514
Loko Kung8d195d52021-09-28 15:40:01 +0000515 // Encompasses the mutex and the actual list that contains all live objects "owned" by the
516 // device.
517 struct ApiObjectList {
518 std::mutex mutex;
519 LinkedList<ApiObjectBase> objects;
520 };
521 PerObjectType<ApiObjectList> mObjectLists;
522
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000523 FormatTable mFormatTable;
524
Corentin Wallezcb84c792020-04-06 08:32:01 +0000525 TogglesSet mEnabledToggles;
Corentin Wallez022d0742020-04-06 16:55:22 +0000526 TogglesSet mOverridenToggles;
Natasha Leee69627f2019-07-26 17:54:48 +0000527 size_t mLazyClearCountForTesting = 0;
Brandon Jones1f48c972021-09-01 22:15:41 +0000528 std::atomic_uint64_t mNextPipelineCompatibilityToken;
Jiawei Shao574b9512019-08-02 00:06:38 +0000529
Austin Eng02fbf162021-09-13 18:49:09 +0000530 CombinedLimits mLimits;
François Beaufort3f689a42021-10-04 11:30:02 +0000531 FeaturesSet mEnabledFeatures;
Yan, Shaobodb8766b2020-11-04 02:30:16 +0000532
533 std::unique_ptr<InternalPipelineStore> mInternalPipelineStore;
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +0000534
535 std::unique_ptr<PersistentCache> mPersistentCache;
Jiawei Shao5e1ca532021-06-04 05:12:06 +0000536
537 std::unique_ptr<CallbackTaskManager> mCallbackTaskManager;
538 std::unique_ptr<dawn_platform::WorkerTaskPool> mWorkerTaskPool;
Brandon Jonesba662952021-09-23 21:26:33 +0000539 std::string mLabel;
Austin Eng376f1c62017-05-30 20:03:44 -0400540 };
541
Corentin Wallez49a65d02018-07-24 16:45:45 +0200542} // namespace dawn_native
Austin Eng376f1c62017-05-30 20:03:44 -0400543
Corentin Wallezac67fec2019-01-04 10:30:40 +0000544#endif // DAWNNATIVE_DEVICE_H_