Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 15 | #ifndef DAWNNATIVE_DEVICE_H_ |
| 16 | #define DAWNNATIVE_DEVICE_H_ |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 17 | |
Tomek Ponitka | d720785 | 2020-08-20 13:29:39 +0000 | [diff] [blame] | 18 | #include "dawn_native/Commands.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 19 | #include "dawn_native/Error.h" |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 20 | #include "dawn_native/Extensions.h" |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 21 | #include "dawn_native/Format.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 22 | #include "dawn_native/Forward.h" |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 23 | #include "dawn_native/ObjectBase.h" |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 24 | #include "dawn_native/Toggles.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 25 | |
Jiawei Shao | 58809d4 | 2018-09-19 00:32:52 +0000 | [diff] [blame] | 26 | #include "dawn_native/DawnNative.h" |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 27 | #include "dawn_native/dawn_platform.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 28 | |
Corentin Wallez | cca9c69 | 2018-09-06 15:26:48 +0200 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 31 | namespace dawn_native { |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 32 | class AdapterBase; |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 33 | class AttachmentState; |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 34 | class AttachmentStateBlueprint; |
Austin Eng | a80993d | 2020-03-20 21:56:30 +0000 | [diff] [blame] | 35 | class BindGroupLayoutBase; |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 36 | class CreateReadyPipelineTracker; |
Austin Eng | a80993d | 2020-03-20 21:56:30 +0000 | [diff] [blame] | 37 | class DynamicUploader; |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 38 | class ErrorScope; |
Austin Eng | be99007 | 2019-09-17 18:24:07 +0000 | [diff] [blame] | 39 | class ErrorScopeTracker; |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 40 | class PersistentCache; |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 41 | class StagingBufferBase; |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 42 | struct InternalPipelineStore; |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 43 | struct ShaderModuleParseResult; |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 44 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 45 | class DeviceBase { |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 46 | public: |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 47 | DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 48 | virtual ~DeviceBase(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 49 | |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 50 | void HandleError(InternalErrorType type, const char* message); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 51 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 52 | bool ConsumedError(MaybeError maybeError) { |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 53 | if (DAWN_UNLIKELY(maybeError.IsError())) { |
Austin Eng | b11bd2d | 2019-09-18 21:03:41 +0000 | [diff] [blame] | 54 | ConsumeError(maybeError.AcquireError()); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
Corentin Wallez | e09869e | 2019-10-09 10:11:00 +0000 | [diff] [blame] | 60 | template <typename T> |
| 61 | bool ConsumedError(ResultOrError<T> resultOrError, T* result) { |
| 62 | if (DAWN_UNLIKELY(resultOrError.IsError())) { |
| 63 | ConsumeError(resultOrError.AcquireError()); |
| 64 | return true; |
| 65 | } |
| 66 | *result = resultOrError.AcquireSuccess(); |
| 67 | return false; |
| 68 | } |
| 69 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 70 | MaybeError ValidateObject(const ObjectBase* object) const; |
| 71 | |
Corentin Wallez | d77fd5f | 2019-01-30 16:07:48 +0000 | [diff] [blame] | 72 | AdapterBase* GetAdapter() const; |
Austin Eng | d4ce736 | 2019-08-13 19:00:34 +0000 | [diff] [blame] | 73 | dawn_platform::Platform* GetPlatform() const; |
Corentin Wallez | d77fd5f | 2019-01-30 16:07:48 +0000 | [diff] [blame] | 74 | |
Austin Eng | be99007 | 2019-09-17 18:24:07 +0000 | [diff] [blame] | 75 | ErrorScopeTracker* GetErrorScopeTracker() const; |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 76 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 77 | // Returns the Format corresponding to the wgpu::TextureFormat or an error if the format |
| 78 | // isn't a valid wgpu::TextureFormat or isn't supported by this device. |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 79 | // The pointer returned has the same lifetime as the device. |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 80 | ResultOrError<const Format*> GetInternalFormat(wgpu::TextureFormat format) const; |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 81 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 82 | // Returns the Format corresponding to the wgpu::TextureFormat and assumes the format is |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 83 | // valid and supported. |
| 84 | // The reference returned has the same lifetime as the device. |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 85 | const Format& GetValidInternalFormat(wgpu::TextureFormat format) const; |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 86 | |
Corentin Wallez | 4b90c47 | 2019-07-10 20:43:13 +0000 | [diff] [blame] | 87 | virtual CommandBufferBase* CreateCommandBuffer( |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 88 | CommandEncoder* encoder, |
Corentin Wallez | 4b90c47 | 2019-07-10 20:43:13 +0000 | [diff] [blame] | 89 | const CommandBufferDescriptor* descriptor) = 0; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 90 | |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 91 | ExecutionSerial GetCompletedCommandSerial() const; |
| 92 | ExecutionSerial GetLastSubmittedCommandSerial() const; |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 93 | ExecutionSerial GetFutureSerial() const; |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 94 | ExecutionSerial GetPendingCommandSerial() const; |
Rafael Cintron | 86d921e | 2019-10-07 15:32:10 +0000 | [diff] [blame] | 95 | virtual MaybeError TickImpl() = 0; |
Corentin Wallez | dbb5729 | 2017-06-14 13:33:45 -0400 | [diff] [blame] | 96 | |
Corentin Wallez | 9fc6534 | 2018-07-18 15:28:38 +0200 | [diff] [blame] | 97 | // Many Dawn objects are completely immutable once created which means that if two |
Corentin Wallez | 4f5fc2d | 2019-04-01 21:48:38 +0000 | [diff] [blame] | 98 | // creations are given the same arguments, they can return the same object. Reusing |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 99 | // objects will help make comparisons between objects by a single pointer comparison. |
| 100 | // |
| 101 | // Technically no object is immutable as they have a reference count, and an |
| 102 | // application with reference-counting issues could "see" that objects are reused. |
| 103 | // This is solved by automatic-reference counting, and also the fact that when using |
| 104 | // the client-server wire every creation will get a different proxy object, with a |
| 105 | // different reference count. |
| 106 | // |
Corentin Wallez | 4f5fc2d | 2019-04-01 21:48:38 +0000 | [diff] [blame] | 107 | // When trying to create an object, we give both the descriptor and an example of what |
| 108 | // the created object will be, the "blueprint". The blueprint is just a FooBase object |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 109 | // instead of a backend Foo object. If the blueprint doesn't match an object in the |
Corentin Wallez | 4f5fc2d | 2019-04-01 21:48:38 +0000 | [diff] [blame] | 110 | // cache, then the descriptor is used to make a new object. |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 111 | ResultOrError<Ref<BindGroupLayoutBase>> GetOrCreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 112 | const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 113 | void UncacheBindGroupLayout(BindGroupLayoutBase* obj); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 114 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 115 | BindGroupLayoutBase* GetEmptyBindGroupLayout(); |
Tomek Ponitka | bf27bd7 | 2020-06-29 11:13:43 +0000 | [diff] [blame] | 116 | |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 117 | ResultOrError<ComputePipelineBase*> GetOrCreateComputePipeline( |
| 118 | const ComputePipelineDescriptor* descriptor); |
| 119 | void UncacheComputePipeline(ComputePipelineBase* obj); |
| 120 | |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 121 | ResultOrError<PipelineLayoutBase*> GetOrCreatePipelineLayout( |
| 122 | const PipelineLayoutDescriptor* descriptor); |
| 123 | void UncachePipelineLayout(PipelineLayoutBase* obj); |
| 124 | |
Corentin Wallez | 94274b6 | 2019-05-02 15:30:56 +0000 | [diff] [blame] | 125 | ResultOrError<RenderPipelineBase*> GetOrCreateRenderPipeline( |
| 126 | const RenderPipelineDescriptor* descriptor); |
| 127 | void UncacheRenderPipeline(RenderPipelineBase* obj); |
| 128 | |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 129 | ResultOrError<SamplerBase*> GetOrCreateSampler(const SamplerDescriptor* descriptor); |
| 130 | void UncacheSampler(SamplerBase* obj); |
| 131 | |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 132 | ResultOrError<ShaderModuleBase*> GetOrCreateShaderModule( |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 133 | const ShaderModuleDescriptor* descriptor, |
| 134 | ShaderModuleParseResult* parseResult); |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 135 | void UncacheShaderModule(ShaderModuleBase* obj); |
| 136 | |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 137 | Ref<AttachmentState> GetOrCreateAttachmentState(AttachmentStateBlueprint* blueprint); |
| 138 | Ref<AttachmentState> GetOrCreateAttachmentState( |
| 139 | const RenderBundleEncoderDescriptor* descriptor); |
Austin Eng | bb10a91 | 2019-08-07 21:47:24 +0000 | [diff] [blame] | 140 | Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPipelineDescriptor* descriptor); |
| 141 | Ref<AttachmentState> GetOrCreateAttachmentState(const RenderPassDescriptor* descriptor); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 142 | void UncacheAttachmentState(AttachmentState* obj); |
| 143 | |
Corentin Wallez | 0927ea5 | 2018-07-18 15:20:28 +0200 | [diff] [blame] | 144 | // Dawn API |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 145 | BindGroupBase* CreateBindGroup(const BindGroupDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 146 | BindGroupLayoutBase* CreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 147 | BufferBase* CreateBuffer(const BufferDescriptor* descriptor); |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 148 | CommandEncoder* CreateCommandEncoder(const CommandEncoderDescriptor* descriptor); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 149 | ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 150 | PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor); |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 151 | QuerySetBase* CreateQuerySet(const QuerySetDescriptor* descriptor); |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 152 | void CreateReadyComputePipeline(const ComputePipelineDescriptor* descriptor, |
| 153 | WGPUCreateReadyComputePipelineCallback callback, |
| 154 | void* userdata); |
Jiawei Shao | 03e1400 | 2020-10-21 04:37:41 +0000 | [diff] [blame] | 155 | void CreateReadyRenderPipeline(const RenderPipelineDescriptor* descriptor, |
| 156 | WGPUCreateReadyRenderPipelineCallback callback, |
| 157 | void* userdata); |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 158 | RenderBundleEncoder* CreateRenderBundleEncoder( |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 159 | const RenderBundleEncoderDescriptor* descriptor); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 160 | RenderPipelineBase* CreateRenderPipeline(const RenderPipelineDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 161 | SamplerBase* CreateSampler(const SamplerDescriptor* descriptor); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 162 | ShaderModuleBase* CreateShaderModule(const ShaderModuleDescriptor* descriptor); |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 163 | SwapChainBase* CreateSwapChain(Surface* surface, const SwapChainDescriptor* descriptor); |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 164 | TextureBase* CreateTexture(const TextureDescriptor* descriptor); |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 165 | TextureViewBase* CreateTextureView(TextureBase* texture, |
| 166 | const TextureViewDescriptor* descriptor); |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 167 | InternalPipelineStore* GetInternalPipelineStore(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 168 | |
Austin Eng | cf1fdf4 | 2020-06-15 23:42:13 +0000 | [diff] [blame] | 169 | // For Dawn Wire |
| 170 | BufferBase* CreateErrorBuffer(); |
| 171 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 172 | QueueBase* GetDefaultQueue(); |
| 173 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 174 | void InjectError(wgpu::ErrorType type, const char* message); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 175 | bool Tick(); |
Austin Eng | 8b07e43 | 2018-12-01 03:20:19 +0000 | [diff] [blame] | 176 | |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 177 | void SetDeviceLostCallback(wgpu::DeviceLostCallback callback, void* userdata); |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 178 | void SetUncapturedErrorCallback(wgpu::ErrorCallback callback, void* userdata); |
| 179 | void PushErrorScope(wgpu::ErrorFilter filter); |
| 180 | bool PopErrorScope(wgpu::ErrorCallback callback, void* userdata); |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 181 | |
| 182 | MaybeError ValidateIsAlive() const; |
| 183 | |
Austin Eng | be99007 | 2019-09-17 18:24:07 +0000 | [diff] [blame] | 184 | ErrorScope* GetCurrentErrorScope(); |
Austin Eng | 45238d7 | 2019-09-04 22:54:03 +0000 | [diff] [blame] | 185 | |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 186 | PersistentCache* GetPersistentCache(); |
| 187 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 188 | void Reference(); |
| 189 | void Release(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 190 | |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 191 | virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer( |
| 192 | size_t size) = 0; |
| 193 | virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source, |
Austin Eng | cf52d71 | 2019-04-05 20:51:29 +0000 | [diff] [blame] | 194 | uint64_t sourceOffset, |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 195 | BufferBase* destination, |
Austin Eng | cf52d71 | 2019-04-05 20:51:29 +0000 | [diff] [blame] | 196 | uint64_t destinationOffset, |
| 197 | uint64_t size) = 0; |
Tomek Ponitka | d720785 | 2020-08-20 13:29:39 +0000 | [diff] [blame] | 198 | virtual MaybeError CopyFromStagingToTexture(const StagingBufferBase* source, |
| 199 | const TextureDataLayout& src, |
| 200 | TextureCopy* dst, |
| 201 | const Extent3D& copySizePixels) = 0; |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 202 | |
Bryan Bernhart | 450e212 | 2019-09-18 22:06:41 +0000 | [diff] [blame] | 203 | DynamicUploader* GetDynamicUploader() const; |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 204 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 205 | // The device state which is a combination of creation state and loss state. |
| 206 | // |
| 207 | // - BeingCreated: the device didn't finish creation yet and the frontend cannot be used |
| 208 | // (both for the application calling WebGPU, or re-entrant calls). No work exists on |
| 209 | // the GPU timeline. |
| 210 | // - Alive: the device is usable and might have work happening on the GPU timeline. |
| 211 | // - BeingDisconnected: the device is no longer usable because we are waiting for all |
| 212 | // work on the GPU timeline to finish. (this is to make validation prevent the |
| 213 | // application from adding more work during the transition from Available to |
| 214 | // Disconnected) |
| 215 | // - Disconnected: there is no longer work happening on the GPU timeline and the CPU data |
| 216 | // structures can be safely destroyed without additional synchronization. |
| 217 | enum class State { |
| 218 | BeingCreated, |
| 219 | Alive, |
| 220 | BeingDisconnected, |
| 221 | Disconnected, |
| 222 | }; |
| 223 | State GetState() const; |
| 224 | bool IsLost() const; |
| 225 | |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 226 | std::vector<const char*> GetEnabledExtensions() const; |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 227 | std::vector<const char*> GetTogglesUsed() const; |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 228 | bool IsExtensionEnabled(Extension extension) const; |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 229 | bool IsToggleEnabled(Toggle toggle) const; |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 230 | bool IsValidationEnabled() const; |
Idan Raiter | bcc65f2 | 2020-07-01 21:37:57 +0000 | [diff] [blame] | 231 | bool IsRobustnessEnabled() const; |
Natasha Lee | e69627f | 2019-07-26 17:54:48 +0000 | [diff] [blame] | 232 | size_t GetLazyClearCountForTesting(); |
| 233 | void IncrementLazyClearCountForTesting(); |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 234 | size_t GetDeprecationWarningCountForTesting(); |
| 235 | void EmitDeprecationWarning(const char* warning); |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 236 | void LoseForTesting(); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 237 | |
| 238 | // AddFutureSerial is used to update the mFutureSerial with the max serial needed to be |
| 239 | // ticked in order to clean up all pending callback work or to execute asynchronous resource |
| 240 | // writes. It should be given the serial that a callback is tracked with, so that once that |
| 241 | // serial is completed, it can be resolved and cleaned up. This is so that when there is no |
| 242 | // gpu work (the last submitted serial has not moved beyond the completed serial), Tick can |
| 243 | // still check if we have pending work to take care of, rather than hanging and never |
| 244 | // reaching the serial the work will be executed on. |
| 245 | void AddFutureSerial(ExecutionSerial serial); |
Brandon Jones | b6f4d53 | 2020-11-13 02:11:12 +0000 | [diff] [blame] | 246 | // Check for passed fences and set the new completed serial |
| 247 | void CheckPassedSerials(); |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 248 | |
Tomek Ponitka | d720785 | 2020-08-20 13:29:39 +0000 | [diff] [blame] | 249 | virtual uint32_t GetOptimalBytesPerRowAlignment() const = 0; |
| 250 | virtual uint64_t GetOptimalBufferToTextureCopyOffsetAlignment() const = 0; |
| 251 | |
Hao Li | cdbd295 | 2021-01-05 02:39:18 +0000 | [diff] [blame^] | 252 | virtual float GetTimestampPeriodInNS() const = 0; |
| 253 | |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 254 | protected: |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 255 | void SetToggle(Toggle toggle, bool isEnabled); |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 256 | void ForceSetToggle(Toggle toggle, bool isEnabled); |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 257 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 258 | MaybeError Initialize(QueueBase* defaultQueue); |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 259 | void ShutDownBase(); |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 260 | |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 261 | // Incrememt mLastSubmittedSerial when we submit the next serial |
| 262 | void IncrementLastSubmittedCommandSerial(); |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 263 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 264 | private: |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 265 | virtual ResultOrError<BindGroupBase*> CreateBindGroupImpl( |
| 266 | const BindGroupDescriptor* descriptor) = 0; |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 267 | virtual ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 268 | const BindGroupLayoutDescriptor* descriptor) = 0; |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 269 | virtual ResultOrError<Ref<BufferBase>> CreateBufferImpl( |
| 270 | const BufferDescriptor* descriptor) = 0; |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 271 | virtual ResultOrError<ComputePipelineBase*> CreateComputePipelineImpl( |
| 272 | const ComputePipelineDescriptor* descriptor) = 0; |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 273 | virtual ResultOrError<PipelineLayoutBase*> CreatePipelineLayoutImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 274 | const PipelineLayoutDescriptor* descriptor) = 0; |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 275 | virtual ResultOrError<QuerySetBase*> CreateQuerySetImpl( |
| 276 | const QuerySetDescriptor* descriptor) = 0; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 277 | virtual ResultOrError<RenderPipelineBase*> CreateRenderPipelineImpl( |
| 278 | const RenderPipelineDescriptor* descriptor) = 0; |
Corentin Wallez | 79d9e16 | 2018-05-28 15:40:41 -0400 | [diff] [blame] | 279 | virtual ResultOrError<SamplerBase*> CreateSamplerImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 280 | const SamplerDescriptor* descriptor) = 0; |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 281 | virtual ResultOrError<ShaderModuleBase*> CreateShaderModuleImpl( |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 282 | const ShaderModuleDescriptor* descriptor, |
| 283 | ShaderModuleParseResult* parseResult) = 0; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 284 | virtual ResultOrError<SwapChainBase*> CreateSwapChainImpl( |
| 285 | const SwapChainDescriptor* descriptor) = 0; |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 286 | // Note that previousSwapChain may be nullptr, or come from a different backend. |
| 287 | virtual ResultOrError<NewSwapChainBase*> CreateSwapChainImpl( |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 288 | Surface* surface, |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 289 | NewSwapChainBase* previousSwapChain, |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 290 | const SwapChainDescriptor* descriptor) = 0; |
Rafael Cintron | 0e9320b | 2020-04-23 19:47:12 +0000 | [diff] [blame] | 291 | virtual ResultOrError<Ref<TextureBase>> CreateTextureImpl( |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 292 | const TextureDescriptor* descriptor) = 0; |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 293 | virtual ResultOrError<TextureViewBase*> CreateTextureViewImpl( |
| 294 | TextureBase* texture, |
| 295 | const TextureViewDescriptor* descriptor) = 0; |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 296 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 297 | ResultOrError<Ref<BindGroupLayoutBase>> CreateEmptyBindGroupLayout(); |
| 298 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 299 | MaybeError CreateBindGroupInternal(BindGroupBase** result, |
| 300 | const BindGroupDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 301 | MaybeError CreateBindGroupLayoutInternal(BindGroupLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 302 | const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 303 | ResultOrError<Ref<BufferBase>> CreateBufferInternal(const BufferDescriptor* descriptor); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 304 | MaybeError CreateComputePipelineInternal(ComputePipelineBase** result, |
| 305 | const ComputePipelineDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 306 | MaybeError CreatePipelineLayoutInternal(PipelineLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 307 | const PipelineLayoutDescriptor* descriptor); |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 308 | MaybeError CreateQuerySetInternal(QuerySetBase** result, |
| 309 | const QuerySetDescriptor* descriptor); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 310 | MaybeError CreateRenderBundleEncoderInternal( |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 311 | RenderBundleEncoder** result, |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 312 | const RenderBundleEncoderDescriptor* descriptor); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 313 | MaybeError CreateRenderPipelineInternal(RenderPipelineBase** result, |
| 314 | const RenderPipelineDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 315 | MaybeError CreateSamplerInternal(SamplerBase** result, const SamplerDescriptor* descriptor); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 316 | MaybeError CreateShaderModuleInternal(ShaderModuleBase** result, |
| 317 | const ShaderModuleDescriptor* descriptor); |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 318 | MaybeError CreateSwapChainInternal(SwapChainBase** result, |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 319 | Surface* surface, |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 320 | const SwapChainDescriptor* descriptor); |
Rafael Cintron | 0e9320b | 2020-04-23 19:47:12 +0000 | [diff] [blame] | 321 | ResultOrError<Ref<TextureBase>> CreateTextureInternal(const TextureDescriptor* descriptor); |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 322 | MaybeError CreateTextureViewInternal(TextureViewBase** result, |
| 323 | TextureBase* texture, |
| 324 | const TextureViewDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 325 | |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 326 | void ApplyToggleOverrides(const DeviceDescriptor* deviceDescriptor); |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 327 | void ApplyExtensions(const DeviceDescriptor* deviceDescriptor); |
| 328 | |
Natasha Lee | 28232ce | 2019-06-11 18:11:05 +0000 | [diff] [blame] | 329 | void SetDefaultToggles(); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 330 | |
Rafael Cintron | 69c68d0 | 2020-01-10 17:58:28 +0000 | [diff] [blame] | 331 | void ConsumeError(std::unique_ptr<ErrorData> error); |
Austin Eng | b11bd2d | 2019-09-18 21:03:41 +0000 | [diff] [blame] | 332 | |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 333 | // Each backend should implement to check their passed fences if there are any and return a |
| 334 | // completed serial. Return 0 should indicate no fences to check. |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 335 | virtual ExecutionSerial CheckAndUpdateCompletedSerials() = 0; |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 336 | // During shut down of device, some operations might have been started since the last submit |
| 337 | // and waiting on a serial that doesn't have a corresponding fence enqueued. Fake serials to |
| 338 | // make all commands look completed. |
| 339 | void AssumeCommandsComplete(); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 340 | bool IsDeviceIdle(); |
| 341 | |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 342 | // mCompletedSerial tracks the last completed command serial that the fence has returned. |
| 343 | // mLastSubmittedSerial tracks the last submitted command serial. |
| 344 | // During device removal, the serials could be artificially incremented |
| 345 | // to make it appear as if commands have been compeleted. They can also be artificially |
| 346 | // incremented when no work is being done in the GPU so CPU operations don't have to wait on |
| 347 | // stale serials. |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 348 | // mFutureSerial tracks the largest serial we need to tick to for asynchronous commands or |
| 349 | // callbacks to fire |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 350 | ExecutionSerial mCompletedSerial = ExecutionSerial(0); |
| 351 | ExecutionSerial mLastSubmittedSerial = ExecutionSerial(0); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 352 | ExecutionSerial mFutureSerial = ExecutionSerial(0); |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 353 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 354 | // ShutDownImpl is used to clean up and release resources used by device, does not wait for |
| 355 | // GPU or check errors. |
| 356 | virtual void ShutDownImpl() = 0; |
Natasha Lee | 2c8a17e | 2019-12-16 23:36:16 +0000 | [diff] [blame] | 357 | |
| 358 | // WaitForIdleForDestruction waits for GPU to finish, checks errors and gets ready for |
| 359 | // destruction. This is only used when properly destructing the device. For a real |
| 360 | // device loss, this function doesn't need to be called since the driver already closed all |
| 361 | // resources. |
| 362 | virtual MaybeError WaitForIdleForDestruction() = 0; |
| 363 | |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 364 | wgpu::DeviceLostCallback mDeviceLostCallback = nullptr; |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 365 | void* mDeviceLostUserdata = nullptr; |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 366 | |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 367 | AdapterBase* mAdapter = nullptr; |
| 368 | |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 369 | Ref<ErrorScope> mRootErrorScope; |
| 370 | Ref<ErrorScope> mCurrentErrorScope; |
| 371 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 372 | // The object caches aren't exposed in the header as they would require a lot of |
| 373 | // additional includes. |
| 374 | struct Caches; |
Corentin Wallez | cca9c69 | 2018-09-06 15:26:48 +0200 | [diff] [blame] | 375 | std::unique_ptr<Caches> mCaches; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 376 | |
Tomek Ponitka | bf27bd7 | 2020-06-29 11:13:43 +0000 | [diff] [blame] | 377 | Ref<BindGroupLayoutBase> mEmptyBindGroupLayout; |
| 378 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 379 | std::unique_ptr<DynamicUploader> mDynamicUploader; |
Austin Eng | be99007 | 2019-09-17 18:24:07 +0000 | [diff] [blame] | 380 | std::unique_ptr<ErrorScopeTracker> mErrorScopeTracker; |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 381 | std::unique_ptr<CreateReadyPipelineTracker> mCreateReadyPipelineTracker; |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 382 | Ref<QueueBase> mDefaultQueue; |
| 383 | |
| 384 | struct DeprecationWarnings; |
| 385 | std::unique_ptr<DeprecationWarnings> mDeprecationWarnings; |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 386 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 387 | uint32_t mRefCount = 1; |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 388 | State mState = State::BeingCreated; |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 389 | |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 390 | FormatTable mFormatTable; |
| 391 | |
Corentin Wallez | cb84c79 | 2020-04-06 08:32:01 +0000 | [diff] [blame] | 392 | TogglesSet mEnabledToggles; |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 393 | TogglesSet mOverridenToggles; |
Natasha Lee | e69627f | 2019-07-26 17:54:48 +0000 | [diff] [blame] | 394 | size_t mLazyClearCountForTesting = 0; |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 395 | |
| 396 | ExtensionsSet mEnabledExtensions; |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 397 | |
| 398 | std::unique_ptr<InternalPipelineStore> mInternalPipelineStore; |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 399 | |
| 400 | std::unique_ptr<PersistentCache> mPersistentCache; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 401 | }; |
| 402 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 403 | } // namespace dawn_native |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 404 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 405 | #endif // DAWNNATIVE_DEVICE_H_ |