blob: 5bb6f6b8bc926df554973e1538d15eedfc361914 [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"
Corentin Wallezd37523f2018-07-24 13:53:51 +020019#include "dawn_native/Error.h"
Jiawei Shao574b9512019-08-02 00:06:38 +000020#include "dawn_native/Extensions.h"
Corentin Wallez1546bfb2019-07-18 09:25:04 +000021#include "dawn_native/Format.h"
Corentin Wallezd37523f2018-07-24 13:53:51 +020022#include "dawn_native/Forward.h"
Corentin Wallez9e4518b2018-10-15 12:54:30 +000023#include "dawn_native/ObjectBase.h"
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000024#include "dawn_native/Toggles.h"
Austin Eng376f1c62017-05-30 20:03:44 -040025
Jiawei Shao58809d42018-09-19 00:32:52 +000026#include "dawn_native/DawnNative.h"
Corentin Wallez36afbb62018-07-25 17:03:23 +020027#include "dawn_native/dawn_platform.h"
Austin Eng376f1c62017-05-30 20:03:44 -040028
Corentin Wallezcca9c692018-09-06 15:26:48 +020029#include <memory>
Jiawei Shaoc243f672021-04-08 01:22:22 +000030#include <utility>
Corentin Wallezcca9c692018-09-06 15:26:48 +020031
Corentin Wallez49a65d02018-07-24 16:45:45 +020032namespace dawn_native {
Corentin Wallezec18f962019-01-10 10:50:54 +000033 class AdapterBase;
Austin Engb98f0fa2019-07-26 19:08:18 +000034 class AttachmentState;
Austin Eng8a488c12019-08-13 22:12:54 +000035 class AttachmentStateBlueprint;
Austin Enga80993d2020-03-20 21:56:30 +000036 class BindGroupLayoutBase;
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000037 class CreatePipelineAsyncTracker;
Austin Enga80993d2020-03-20 21:56:30 +000038 class DynamicUploader;
Austin Engef9e4412021-02-19 18:17:22 +000039 class ErrorScopeStack;
Brandon Jones0e92e9b2021-04-01 20:46:42 +000040 class ExternalTextureBase;
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +000041 class PersistentCache;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +000042 class StagingBufferBase;
Yan, Shaobodb8766b2020-11-04 02:30:16 +000043 struct InternalPipelineStore;
Austin Eng0d948f72020-12-07 18:12:13 +000044 struct ShaderModuleParseResult;
Austin Engf0b761f2018-12-03 16:57:34 +000045
Corentin Wallez12571522021-03-25 23:50:24 +000046 class DeviceBase : public RefCounted {
Corentin Wallezc1400f02017-11-24 13:59:42 -050047 public:
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000048 DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050049 virtual ~DeviceBase();
Austin Eng376f1c62017-05-30 20:03:44 -040050
Corentin Walleza0afd312020-04-01 12:07:43 +000051 void HandleError(InternalErrorType type, const char* message);
Austin Eng376f1c62017-05-30 20:03:44 -040052
Corentin Wallez52f23832018-07-16 17:40:08 +020053 bool ConsumedError(MaybeError maybeError) {
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020054 if (DAWN_UNLIKELY(maybeError.IsError())) {
Austin Engb11bd2d2019-09-18 21:03:41 +000055 ConsumeError(maybeError.AcquireError());
Corentin Wallez52f23832018-07-16 17:40:08 +020056 return true;
57 }
58 return false;
59 }
60
Corentin Walleze09869e2019-10-09 10:11:00 +000061 template <typename T>
62 bool ConsumedError(ResultOrError<T> resultOrError, T* result) {
63 if (DAWN_UNLIKELY(resultOrError.IsError())) {
64 ConsumeError(resultOrError.AcquireError());
65 return true;
66 }
67 *result = resultOrError.AcquireSuccess();
68 return false;
69 }
70
Corentin Walleza594f8f2019-02-13 13:09:18 +000071 MaybeError ValidateObject(const ObjectBase* object) const;
72
Corentin Wallezd77fd5f2019-01-30 16:07:48 +000073 AdapterBase* GetAdapter() const;
Austin Engd4ce7362019-08-13 19:00:34 +000074 dawn_platform::Platform* GetPlatform() const;
Corentin Wallezd77fd5f2019-01-30 16:07:48 +000075
Corentin Wallez1f6c8c42019-10-23 11:57:41 +000076 // Returns the Format corresponding to the wgpu::TextureFormat or an error if the format
77 // isn't a valid wgpu::TextureFormat or isn't supported by this device.
Corentin Wallez1546bfb2019-07-18 09:25:04 +000078 // The pointer returned has the same lifetime as the device.
Corentin Wallez1f6c8c42019-10-23 11:57:41 +000079 ResultOrError<const Format*> GetInternalFormat(wgpu::TextureFormat format) const;
Corentin Wallez1546bfb2019-07-18 09:25:04 +000080
Corentin Wallez1f6c8c42019-10-23 11:57:41 +000081 // Returns the Format corresponding to the wgpu::TextureFormat and assumes the format is
Corentin Wallez1546bfb2019-07-18 09:25:04 +000082 // valid and supported.
83 // The reference returned has the same lifetime as the device.
Corentin Wallez1f6c8c42019-10-23 11:57:41 +000084 const Format& GetValidInternalFormat(wgpu::TextureFormat format) const;
Corentin Wallez1546bfb2019-07-18 09:25:04 +000085
Corentin Wallez50f99582021-03-31 18:36:32 +000086 virtual ResultOrError<Ref<CommandBufferBase>> CreateCommandBuffer(
Corentin Wallez321c1222019-11-13 17:00:37 +000087 CommandEncoder* encoder,
Corentin Wallez4b90c472019-07-10 20:43:13 +000088 const CommandBufferDescriptor* descriptor) = 0;
Austin Eng376f1c62017-05-30 20:03:44 -040089
Corentin Wallez62139fc2020-09-28 19:35:14 +000090 ExecutionSerial GetCompletedCommandSerial() const;
91 ExecutionSerial GetLastSubmittedCommandSerial() const;
Brandon Jones4ad35862020-10-15 16:21:03 +000092 ExecutionSerial GetFutureSerial() const;
Corentin Wallez62139fc2020-09-28 19:35:14 +000093 ExecutionSerial GetPendingCommandSerial() const;
Corentin Wallezdbb57292017-06-14 13:33:45 -040094
Corentin Wallez9fc65342018-07-18 15:28:38 +020095 // Many Dawn objects are completely immutable once created which means that if two
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +000096 // creations are given the same arguments, they can return the same object. Reusing
Corentin Wallezc1400f02017-11-24 13:59:42 -050097 // objects will help make comparisons between objects by a single pointer comparison.
98 //
99 // Technically no object is immutable as they have a reference count, and an
100 // application with reference-counting issues could "see" that objects are reused.
101 // This is solved by automatic-reference counting, and also the fact that when using
102 // the client-server wire every creation will get a different proxy object, with a
103 // different reference count.
104 //
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000105 // When trying to create an object, we give both the descriptor and an example of what
106 // the created object will be, the "blueprint". The blueprint is just a FooBase object
Corentin Wallezc1400f02017-11-24 13:59:42 -0500107 // instead of a backend Foo object. If the blueprint doesn't match an object in the
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000108 // cache, then the descriptor is used to make a new object.
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000109 ResultOrError<Ref<BindGroupLayoutBase>> GetOrCreateBindGroupLayout(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200110 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -0500111 void UncacheBindGroupLayout(BindGroupLayoutBase* obj);
Austin Eng376f1c62017-05-30 20:03:44 -0400112
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000113 BindGroupLayoutBase* GetEmptyBindGroupLayout();
Tomek Ponitkabf27bd72020-06-29 11:13:43 +0000114
Corentin Wallez1152bba2019-05-01 13:48:47 +0000115 void UncacheComputePipeline(ComputePipelineBase* obj);
116
Corentin Wallez50f99582021-03-31 18:36:32 +0000117 ResultOrError<Ref<PipelineLayoutBase>> GetOrCreatePipelineLayout(
Corentin Wallez0ee98592019-05-01 12:57:27 +0000118 const PipelineLayoutDescriptor* descriptor);
119 void UncachePipelineLayout(PipelineLayoutBase* obj);
120
Corentin Wallez50f99582021-03-31 18:36:32 +0000121 ResultOrError<Ref<RenderPipelineBase>> GetOrCreateRenderPipeline(
Brandon Jones3ceb6542021-04-01 01:28:52 +0000122 const RenderPipelineDescriptor2* descriptor);
Corentin Wallez94274b62019-05-02 15:30:56 +0000123 void UncacheRenderPipeline(RenderPipelineBase* obj);
124
Corentin Wallez50f99582021-03-31 18:36:32 +0000125 ResultOrError<Ref<SamplerBase>> GetOrCreateSampler(const SamplerDescriptor* descriptor);
Idan Raiter2bc31692019-05-20 18:16:34 +0000126 void UncacheSampler(SamplerBase* obj);
127
Corentin Wallez50f99582021-03-31 18:36:32 +0000128 ResultOrError<Ref<ShaderModuleBase>> GetOrCreateShaderModule(
Austin Eng0d948f72020-12-07 18:12:13 +0000129 const ShaderModuleDescriptor* descriptor,
130 ShaderModuleParseResult* parseResult);
Corentin Wallezc5351982019-05-01 13:27:07 +0000131 void UncacheShaderModule(ShaderModuleBase* obj);
132
Austin Eng8a488c12019-08-13 22:12:54 +0000133 Ref<AttachmentState> GetOrCreateAttachmentState(AttachmentStateBlueprint* blueprint);
134 Ref<AttachmentState> GetOrCreateAttachmentState(
135 const RenderBundleEncoderDescriptor* descriptor);
Austin Engbb10a912019-08-07 21:47:24 +0000136 Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPipelineDescriptor* descriptor);
Brandon Jones3ceb6542021-04-01 01:28:52 +0000137 Ref<AttachmentState> GetOrCreateAttachmentState(
138 const RenderPipelineDescriptor2* descriptor);
Austin Engbb10a912019-08-07 21:47:24 +0000139 Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPassDescriptor* descriptor);
Austin Engb98f0fa2019-07-26 19:08:18 +0000140 void UncacheAttachmentState(AttachmentState* obj);
141
Corentin Wallez0927ea52018-07-18 15:20:28 +0200142 // Dawn API
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000143 BindGroupBase* APICreateBindGroup(const BindGroupDescriptor* descriptor);
144 BindGroupLayoutBase* APICreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor);
145 BufferBase* APICreateBuffer(const BufferDescriptor* descriptor);
146 CommandEncoder* APICreateCommandEncoder(const CommandEncoderDescriptor* descriptor);
147 ComputePipelineBase* APICreateComputePipeline(const ComputePipelineDescriptor* descriptor);
148 PipelineLayoutBase* APICreatePipelineLayout(const PipelineLayoutDescriptor* descriptor);
149 QuerySetBase* APICreateQuerySet(const QuerySetDescriptor* descriptor);
150 void APICreateComputePipelineAsync(const ComputePipelineDescriptor* descriptor,
151 WGPUCreateComputePipelineAsyncCallback callback,
152 void* userdata);
153 void APICreateRenderPipelineAsync(const RenderPipelineDescriptor2* descriptor,
154 WGPUCreateRenderPipelineAsyncCallback callback,
155 void* userdata);
156 RenderBundleEncoder* APICreateRenderBundleEncoder(
Austin Eng8a488c12019-08-13 22:12:54 +0000157 const RenderBundleEncoderDescriptor* descriptor);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000158 RenderPipelineBase* APICreateRenderPipeline(const RenderPipelineDescriptor* descriptor);
159 RenderPipelineBase* APICreateRenderPipeline2(const RenderPipelineDescriptor2* descriptor);
Brandon Jones0e92e9b2021-04-01 20:46:42 +0000160 ExternalTextureBase* APICreateExternalTexture(const ExternalTextureDescriptor* descriptor);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000161 SamplerBase* APICreateSampler(const SamplerDescriptor* descriptor);
162 ShaderModuleBase* APICreateShaderModule(const ShaderModuleDescriptor* descriptor);
163 SwapChainBase* APICreateSwapChain(Surface* surface, const SwapChainDescriptor* descriptor);
164 TextureBase* APICreateTexture(const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000165 TextureViewBase* CreateTextureView(TextureBase* texture,
166 const TextureViewDescriptor* descriptor);
Yan, Shaobodb8766b2020-11-04 02:30:16 +0000167 InternalPipelineStore* GetInternalPipelineStore();
Austin Eng376f1c62017-05-30 20:03:44 -0400168
Austin Engcf1fdf42020-06-15 23:42:13 +0000169 // For Dawn Wire
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000170 BufferBase* APICreateErrorBuffer();
Austin Engcf1fdf42020-06-15 23:42:13 +0000171
Corentin Wallez6d315da2021-02-04 15:33:42 +0000172 // TODO(dawn:22): Remove once the deprecation period is finished.
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000173 QueueBase* APIGetDefaultQueue();
174 QueueBase* APIGetQueue();
Corentin Wallez8a437942020-04-17 16:45:17 +0000175
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000176 void APIInjectError(wgpu::ErrorType type, const char* message);
177 bool APITick();
Austin Eng8b07e432018-12-01 03:20:19 +0000178
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000179 void APISetDeviceLostCallback(wgpu::DeviceLostCallback callback, void* userdata);
180 void APISetUncapturedErrorCallback(wgpu::ErrorCallback callback, void* userdata);
181 void APIPushErrorScope(wgpu::ErrorFilter filter);
182 bool APIPopErrorScope(wgpu::ErrorCallback callback, void* userdata);
Natasha Lee0ecc48e2020-01-15 19:02:13 +0000183
184 MaybeError ValidateIsAlive() const;
185
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +0000186 PersistentCache* GetPersistentCache();
187
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000188 virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(
189 size_t size) = 0;
190 virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
Austin Engcf52d712019-04-05 20:51:29 +0000191 uint64_t sourceOffset,
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000192 BufferBase* destination,
Austin Engcf52d712019-04-05 20:51:29 +0000193 uint64_t destinationOffset,
194 uint64_t size) = 0;
Tomek Ponitkad7207852020-08-20 13:29:39 +0000195 virtual MaybeError CopyFromStagingToTexture(const StagingBufferBase* source,
196 const TextureDataLayout& src,
197 TextureCopy* dst,
198 const Extent3D& copySizePixels) = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000199
Bryan Bernhart450e2122019-09-18 22:06:41 +0000200 DynamicUploader* GetDynamicUploader() const;
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000201
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000202 // The device state which is a combination of creation state and loss state.
203 //
204 // - BeingCreated: the device didn't finish creation yet and the frontend cannot be used
205 // (both for the application calling WebGPU, or re-entrant calls). No work exists on
206 // the GPU timeline.
207 // - Alive: the device is usable and might have work happening on the GPU timeline.
208 // - BeingDisconnected: the device is no longer usable because we are waiting for all
209 // work on the GPU timeline to finish. (this is to make validation prevent the
210 // application from adding more work during the transition from Available to
211 // Disconnected)
212 // - Disconnected: there is no longer work happening on the GPU timeline and the CPU data
213 // structures can be safely destroyed without additional synchronization.
214 enum class State {
215 BeingCreated,
216 Alive,
217 BeingDisconnected,
218 Disconnected,
219 };
220 State GetState() const;
221 bool IsLost() const;
222
Jiawei Shao574b9512019-08-02 00:06:38 +0000223 std::vector<const char*> GetEnabledExtensions() const;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000224 std::vector<const char*> GetTogglesUsed() const;
Jiawei Shao574b9512019-08-02 00:06:38 +0000225 bool IsExtensionEnabled(Extension extension) const;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000226 bool IsToggleEnabled(Toggle toggle) const;
Austin Eng4d156092019-11-21 00:48:39 +0000227 bool IsValidationEnabled() const;
Idan Raiterbcc65f22020-07-01 21:37:57 +0000228 bool IsRobustnessEnabled() const;
Natasha Leee69627f2019-07-26 17:54:48 +0000229 size_t GetLazyClearCountForTesting();
230 void IncrementLazyClearCountForTesting();
Corentin Wallez8a437942020-04-17 16:45:17 +0000231 size_t GetDeprecationWarningCountForTesting();
232 void EmitDeprecationWarning(const char* warning);
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000233 void APILoseForTesting();
Corentin Walleze91975c2021-03-29 15:40:55 +0000234 QueueBase* GetQueue() const;
Brandon Jones4ad35862020-10-15 16:21:03 +0000235
236 // AddFutureSerial is used to update the mFutureSerial with the max serial needed to be
237 // ticked in order to clean up all pending callback work or to execute asynchronous resource
238 // writes. It should be given the serial that a callback is tracked with, so that once that
239 // serial is completed, it can be resolved and cleaned up. This is so that when there is no
240 // gpu work (the last submitted serial has not moved beyond the completed serial), Tick can
241 // still check if we have pending work to take care of, rather than hanging and never
242 // reaching the serial the work will be executed on.
243 void AddFutureSerial(ExecutionSerial serial);
Brandon Jonesb6f4d532020-11-13 02:11:12 +0000244 // Check for passed fences and set the new completed serial
Corentin Wallez6870e6d2021-04-07 18:09:21 +0000245 MaybeError CheckPassedSerials();
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000246
Corentin Wallez1bc1ed42021-03-30 10:21:36 +0000247 MaybeError Tick();
248
Tomek Ponitkad7207852020-08-20 13:29:39 +0000249 virtual uint32_t GetOptimalBytesPerRowAlignment() const = 0;
250 virtual uint64_t GetOptimalBufferToTextureCopyOffsetAlignment() const = 0;
251
Hao Licdbd2952021-01-05 02:39:18 +0000252 virtual float GetTimestampPeriodInNS() const = 0;
253
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000254 protected:
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000255 void SetToggle(Toggle toggle, bool isEnabled);
Corentin Wallez022d0742020-04-06 16:55:22 +0000256 void ForceSetToggle(Toggle toggle, bool isEnabled);
Corentin Wallez09ee5eb2020-04-07 15:10:17 +0000257
Corentin Wallez8a437942020-04-17 16:45:17 +0000258 MaybeError Initialize(QueueBase* defaultQueue);
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000259 void ShutDownBase();
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000260
Natasha Lee351c95a2020-05-07 21:52:54 +0000261 // Incrememt mLastSubmittedSerial when we submit the next serial
262 void IncrementLastSubmittedCommandSerial();
Natasha Lee351c95a2020-05-07 21:52:54 +0000263
Corentin Wallezc1400f02017-11-24 13:59:42 -0500264 private:
Corentin Wallez50f99582021-03-31 18:36:32 +0000265 virtual ResultOrError<Ref<BindGroupBase>> CreateBindGroupImpl(
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000266 const BindGroupDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000267 virtual ResultOrError<Ref<BindGroupLayoutBase>> CreateBindGroupLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200268 const BindGroupLayoutDescriptor* descriptor) = 0;
Corentin Wallez8a991992020-07-08 19:45:40 +0000269 virtual ResultOrError<Ref<BufferBase>> CreateBufferImpl(
270 const BufferDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000271 virtual ResultOrError<Ref<ComputePipelineBase>> CreateComputePipelineImpl(
Corentin Wallez8e335a52018-08-27 23:12:56 +0200272 const ComputePipelineDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000273 virtual ResultOrError<Ref<PipelineLayoutBase>> CreatePipelineLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200274 const PipelineLayoutDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000275 virtual ResultOrError<Ref<QuerySetBase>> CreateQuerySetImpl(
Hao Lib6eff5a2020-06-11 00:34:14 +0000276 const QuerySetDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000277 virtual ResultOrError<Ref<RenderPipelineBase>> CreateRenderPipelineImpl(
Brandon Jonesf7592642021-04-02 19:42:28 +0000278 const RenderPipelineDescriptor2* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000279 virtual ResultOrError<Ref<SamplerBase>> CreateSamplerImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200280 const SamplerDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000281 virtual ResultOrError<Ref<ShaderModuleBase>> CreateShaderModuleImpl(
Austin Eng0d948f72020-12-07 18:12:13 +0000282 const ShaderModuleDescriptor* descriptor,
283 ShaderModuleParseResult* parseResult) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000284 virtual ResultOrError<Ref<SwapChainBase>> CreateSwapChainImpl(
Corentin Wallez7be2a712019-02-15 11:15:58 +0000285 const SwapChainDescriptor* descriptor) = 0;
Corentin Wallezd26ee852020-01-25 10:05:40 +0000286 // Note that previousSwapChain may be nullptr, or come from a different backend.
Corentin Wallez50f99582021-03-31 18:36:32 +0000287 virtual ResultOrError<Ref<NewSwapChainBase>> CreateSwapChainImpl(
Corentin Wallezd87e6762020-01-23 17:20:38 +0000288 Surface* surface,
Corentin Wallezd26ee852020-01-25 10:05:40 +0000289 NewSwapChainBase* previousSwapChain,
Corentin Wallezd87e6762020-01-23 17:20:38 +0000290 const SwapChainDescriptor* descriptor) = 0;
Rafael Cintron0e9320b2020-04-23 19:47:12 +0000291 virtual ResultOrError<Ref<TextureBase>> CreateTextureImpl(
Jiawei Shao425428f2018-08-27 08:44:48 +0800292 const TextureDescriptor* descriptor) = 0;
Corentin Wallez50f99582021-03-31 18:36:32 +0000293 virtual ResultOrError<Ref<TextureViewBase>> CreateTextureViewImpl(
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000294 TextureBase* texture,
295 const TextureViewDescriptor* descriptor) = 0;
Corentin Wallez1ae19e82018-05-17 17:09:07 -0400296
Corentin Wallez1bc1ed42021-03-30 10:21:36 +0000297 virtual MaybeError TickImpl() = 0;
298
Tomek Ponitka5fb974c2020-07-01 13:09:46 +0000299 ResultOrError<Ref<BindGroupLayoutBase>> CreateEmptyBindGroupLayout();
300
Corentin Wallez50f99582021-03-31 18:36:32 +0000301 ResultOrError<Ref<BindGroupBase>> CreateBindGroupInternal(
302 const BindGroupDescriptor* descriptor);
303 ResultOrError<Ref<BindGroupLayoutBase>> CreateBindGroupLayoutInternal(
304 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez8a991992020-07-08 19:45:40 +0000305 ResultOrError<Ref<BufferBase>> CreateBufferInternal(const BufferDescriptor* descriptor);
Jiawei Shaoc243f672021-04-08 01:22:22 +0000306 MaybeError CreateComputePipelineAsyncInternal(
307 const ComputePipelineDescriptor* descriptor,
308 WGPUCreateComputePipelineAsyncCallback callback,
309 void* userdata);
Corentin Wallez50f99582021-03-31 18:36:32 +0000310 ResultOrError<Ref<ComputePipelineBase>> CreateComputePipelineInternal(
311 const ComputePipelineDescriptor* descriptor);
312 ResultOrError<Ref<PipelineLayoutBase>> CreatePipelineLayoutInternal(
313 const PipelineLayoutDescriptor* descriptor);
Brandon Jones0e92e9b2021-04-01 20:46:42 +0000314 ResultOrError<Ref<ExternalTextureBase>> CreateExternalTextureInternal(
315 const ExternalTextureDescriptor* descriptor);
Corentin Wallez50f99582021-03-31 18:36:32 +0000316 ResultOrError<Ref<QuerySetBase>> CreateQuerySetInternal(
317 const QuerySetDescriptor* descriptor);
318 ResultOrError<Ref<RenderBundleEncoder>> CreateRenderBundleEncoderInternal(
Austin Eng8a488c12019-08-13 22:12:54 +0000319 const RenderBundleEncoderDescriptor* descriptor);
Corentin Wallez50f99582021-03-31 18:36:32 +0000320 ResultOrError<Ref<RenderPipelineBase>> CreateRenderPipelineInternal(
321 const RenderPipelineDescriptor2* descriptor);
Corentin Wallez50f99582021-03-31 18:36:32 +0000322 ResultOrError<Ref<SamplerBase>> CreateSamplerInternal(const SamplerDescriptor* descriptor);
323 ResultOrError<Ref<ShaderModuleBase>> CreateShaderModuleInternal(
324 const ShaderModuleDescriptor* descriptor);
325 ResultOrError<Ref<SwapChainBase>> CreateSwapChainInternal(
326 Surface* surface,
327 const SwapChainDescriptor* descriptor);
Rafael Cintron0e9320b2020-04-23 19:47:12 +0000328 ResultOrError<Ref<TextureBase>> CreateTextureInternal(const TextureDescriptor* descriptor);
Corentin Wallez50f99582021-03-31 18:36:32 +0000329 ResultOrError<Ref<TextureViewBase>> CreateTextureViewInternal(
330 TextureBase* texture,
331 const TextureViewDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200332
Jiawei Shaoc243f672021-04-08 01:22:22 +0000333 ResultOrError<Ref<PipelineLayoutBase>> ValidateAndGetComputePipelineDescriptorWithDefaults(
334 const ComputePipelineDescriptor& descriptor,
335 ComputePipelineDescriptor* outDescriptor);
336 std::pair<Ref<ComputePipelineBase>, size_t> GetCachedComputePipeline(
337 const ComputePipelineDescriptor* descriptor);
338 Ref<ComputePipelineBase> AddOrGetCachedPipeline(Ref<ComputePipelineBase> computePipeline,
339 size_t blueprintHash);
340 void CreateComputePipelineAsyncImpl(const ComputePipelineDescriptor* descriptor,
341 size_t blueprintHash,
342 WGPUCreateComputePipelineAsyncCallback callback,
343 void* userdata);
344
Corentin Wallez022d0742020-04-06 16:55:22 +0000345 void ApplyToggleOverrides(const DeviceDescriptor* deviceDescriptor);
Jiawei Shao574b9512019-08-02 00:06:38 +0000346 void ApplyExtensions(const DeviceDescriptor* deviceDescriptor);
347
Natasha Lee28232ce2019-06-11 18:11:05 +0000348 void SetDefaultToggles();
Corentin Wallez52f23832018-07-16 17:40:08 +0200349
Rafael Cintron69c68d02020-01-10 17:58:28 +0000350 void ConsumeError(std::unique_ptr<ErrorData> error);
Austin Engb11bd2d2019-09-18 21:03:41 +0000351
Natasha Lee351c95a2020-05-07 21:52:54 +0000352 // Each backend should implement to check their passed fences if there are any and return a
353 // completed serial. Return 0 should indicate no fences to check.
Corentin Wallez6870e6d2021-04-07 18:09:21 +0000354 virtual ResultOrError<ExecutionSerial> CheckAndUpdateCompletedSerials() = 0;
Natasha Lee783cd5a2020-06-04 02:26:46 +0000355 // During shut down of device, some operations might have been started since the last submit
356 // and waiting on a serial that doesn't have a corresponding fence enqueued. Fake serials to
357 // make all commands look completed.
358 void AssumeCommandsComplete();
Brandon Jones4ad35862020-10-15 16:21:03 +0000359 bool IsDeviceIdle();
360
Natasha Lee351c95a2020-05-07 21:52:54 +0000361 // mCompletedSerial tracks the last completed command serial that the fence has returned.
362 // mLastSubmittedSerial tracks the last submitted command serial.
363 // During device removal, the serials could be artificially incremented
364 // to make it appear as if commands have been compeleted. They can also be artificially
365 // incremented when no work is being done in the GPU so CPU operations don't have to wait on
366 // stale serials.
Brandon Jones4ad35862020-10-15 16:21:03 +0000367 // mFutureSerial tracks the largest serial we need to tick to for asynchronous commands or
368 // callbacks to fire
Corentin Wallez62139fc2020-09-28 19:35:14 +0000369 ExecutionSerial mCompletedSerial = ExecutionSerial(0);
370 ExecutionSerial mLastSubmittedSerial = ExecutionSerial(0);
Brandon Jones4ad35862020-10-15 16:21:03 +0000371 ExecutionSerial mFutureSerial = ExecutionSerial(0);
Natasha Lee351c95a2020-05-07 21:52:54 +0000372
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000373 // ShutDownImpl is used to clean up and release resources used by device, does not wait for
374 // GPU or check errors.
375 virtual void ShutDownImpl() = 0;
Natasha Lee2c8a17e2019-12-16 23:36:16 +0000376
377 // WaitForIdleForDestruction waits for GPU to finish, checks errors and gets ready for
378 // destruction. This is only used when properly destructing the device. For a real
379 // device loss, this function doesn't need to be called since the driver already closed all
380 // resources.
381 virtual MaybeError WaitForIdleForDestruction() = 0;
382
Austin Engef9e4412021-02-19 18:17:22 +0000383 wgpu::ErrorCallback mUncapturedErrorCallback = nullptr;
384 void* mUncapturedErrorUserdata = nullptr;
385
Natasha Lee9bba4a92019-12-18 18:59:20 +0000386 wgpu::DeviceLostCallback mDeviceLostCallback = nullptr;
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000387 void* mDeviceLostUserdata = nullptr;
Natasha Lee9bba4a92019-12-18 18:59:20 +0000388
Austin Engef9e4412021-02-19 18:17:22 +0000389 std::unique_ptr<ErrorScopeStack> mErrorScopeStack;
390
Austin Engf6ef7532021-01-13 17:54:37 +0000391 // The Device keeps a ref to the Instance so that any live Device keeps the Instance alive.
392 // The Instance shouldn't need to ref child objects so this shouldn't introduce ref cycles.
393 // The Device keeps a simple pointer to the Adapter because the Adapter is owned by the
394 // Instance.
395 Ref<InstanceBase> mInstance;
Corentin Wallezec18f962019-01-10 10:50:54 +0000396 AdapterBase* mAdapter = nullptr;
397
Corentin Wallezc1400f02017-11-24 13:59:42 -0500398 // The object caches aren't exposed in the header as they would require a lot of
399 // additional includes.
400 struct Caches;
Corentin Wallezcca9c692018-09-06 15:26:48 +0200401 std::unique_ptr<Caches> mCaches;
Austin Eng376f1c62017-05-30 20:03:44 -0400402
Tomek Ponitkabf27bd72020-06-29 11:13:43 +0000403 Ref<BindGroupLayoutBase> mEmptyBindGroupLayout;
404
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000405 std::unique_ptr<DynamicUploader> mDynamicUploader;
Corentin Wallez2d3c2e32021-02-22 18:27:36 +0000406 std::unique_ptr<CreatePipelineAsyncTracker> mCreatePipelineAsyncTracker;
Corentin Wallez6d315da2021-02-04 15:33:42 +0000407 Ref<QueueBase> mQueue;
Corentin Wallez8a437942020-04-17 16:45:17 +0000408
409 struct DeprecationWarnings;
410 std::unique_ptr<DeprecationWarnings> mDeprecationWarnings;
Austin Engf0b761f2018-12-03 16:57:34 +0000411
Corentin Wallez73ea1f12020-04-07 16:19:47 +0000412 State mState = State::BeingCreated;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000413
Corentin Wallez1546bfb2019-07-18 09:25:04 +0000414 FormatTable mFormatTable;
415
Corentin Wallezcb84c792020-04-06 08:32:01 +0000416 TogglesSet mEnabledToggles;
Corentin Wallez022d0742020-04-06 16:55:22 +0000417 TogglesSet mOverridenToggles;
Natasha Leee69627f2019-07-26 17:54:48 +0000418 size_t mLazyClearCountForTesting = 0;
Jiawei Shao574b9512019-08-02 00:06:38 +0000419
420 ExtensionsSet mEnabledExtensions;
Yan, Shaobodb8766b2020-11-04 02:30:16 +0000421
422 std::unique_ptr<InternalPipelineStore> mInternalPipelineStore;
Bryan Bernhart41b3f9c2020-11-20 20:38:37 +0000423
424 std::unique_ptr<PersistentCache> mPersistentCache;
Austin Eng376f1c62017-05-30 20:03:44 -0400425 };
426
Corentin Wallez49a65d02018-07-24 16:45:45 +0200427} // namespace dawn_native
Austin Eng376f1c62017-05-30 20:03:44 -0400428
Corentin Wallezac67fec2019-01-04 10:30:40 +0000429#endif // DAWNNATIVE_DEVICE_H_