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 | |
Austin Eng | 8b07e43 | 2018-12-01 03:20:19 +0000 | [diff] [blame] | 18 | #include "common/Serial.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 19 | #include "dawn_native/Error.h" |
| 20 | #include "dawn_native/Forward.h" |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 21 | #include "dawn_native/ObjectBase.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 22 | |
Jiawei Shao | 58809d4 | 2018-09-19 00:32:52 +0000 | [diff] [blame] | 23 | #include "dawn_native/DawnNative.h" |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 24 | #include "dawn_native/dawn_platform.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 25 | |
Corentin Wallez | cca9c69 | 2018-09-06 15:26:48 +0200 | [diff] [blame] | 26 | #include <memory> |
| 27 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 28 | namespace dawn_native { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 29 | |
| 30 | using ErrorCallback = void (*)(const char* errorMessage, void* userData); |
| 31 | |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 32 | class AdapterBase; |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame^] | 33 | class BufferBuilder; |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 34 | class FenceSignalTracker; |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 35 | class DynamicUploader; |
| 36 | class StagingBufferBase; |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 37 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 38 | class DeviceBase { |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 39 | public: |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 40 | DeviceBase(AdapterBase* adapter); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 41 | virtual ~DeviceBase(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 42 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 43 | void HandleError(const char* message); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 44 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 45 | bool ConsumedError(MaybeError maybeError) { |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 46 | if (DAWN_UNLIKELY(maybeError.IsError())) { |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 47 | ConsumeError(maybeError.AcquireError()); |
| 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 53 | MaybeError ValidateObject(const ObjectBase* object) const; |
| 54 | |
Corentin Wallez | d77fd5f | 2019-01-30 16:07:48 +0000 | [diff] [blame] | 55 | AdapterBase* GetAdapter() const; |
| 56 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 57 | // Used by autogenerated code, returns itself |
| 58 | DeviceBase* GetDevice(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 59 | |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 60 | FenceSignalTracker* GetFenceSignalTracker() const; |
| 61 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame^] | 62 | virtual CommandBufferBase* CreateCommandBuffer(CommandEncoderBase* encoder) = 0; |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 63 | virtual InputStateBase* CreateInputState(InputStateBuilder* builder) = 0; |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 64 | virtual RenderPassDescriptorBase* CreateRenderPassDescriptor( |
| 65 | RenderPassDescriptorBuilder* builder) = 0; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 66 | |
Austin Eng | 8b07e43 | 2018-12-01 03:20:19 +0000 | [diff] [blame] | 67 | virtual Serial GetCompletedCommandSerial() const = 0; |
| 68 | virtual Serial GetLastSubmittedCommandSerial() const = 0; |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 69 | virtual Serial GetPendingCommandSerial() const = 0; |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 70 | virtual void TickImpl() = 0; |
Corentin Wallez | dbb5729 | 2017-06-14 13:33:45 -0400 | [diff] [blame] | 71 | |
Corentin Wallez | 9fc6534 | 2018-07-18 15:28:38 +0200 | [diff] [blame] | 72 | // Many Dawn objects are completely immutable once created which means that if two |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 73 | // builders are given the same arguments, they can return the same object. Reusing |
| 74 | // objects will help make comparisons between objects by a single pointer comparison. |
| 75 | // |
| 76 | // Technically no object is immutable as they have a reference count, and an |
| 77 | // application with reference-counting issues could "see" that objects are reused. |
| 78 | // This is solved by automatic-reference counting, and also the fact that when using |
| 79 | // the client-server wire every creation will get a different proxy object, with a |
| 80 | // different reference count. |
| 81 | // |
| 82 | // When trying to create an object, we give both the builder and an example of what |
| 83 | // the built object will be, the "blueprint". The blueprint is just a FooBase object |
| 84 | // instead of a backend Foo object. If the blueprint doesn't match an object in the |
| 85 | // cache, then the builder is used to make a new object. |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 86 | ResultOrError<BindGroupLayoutBase*> GetOrCreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 87 | const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 88 | void UncacheBindGroupLayout(BindGroupLayoutBase* obj); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 89 | |
Corentin Wallez | 0927ea5 | 2018-07-18 15:20:28 +0200 | [diff] [blame] | 90 | // Dawn API |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 91 | BindGroupBase* CreateBindGroup(const BindGroupDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 92 | BindGroupLayoutBase* CreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 93 | BufferBase* CreateBuffer(const BufferDescriptor* descriptor); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 94 | CommandEncoderBase* CreateCommandEncoder(); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 95 | ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor); |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 96 | FenceBase* CreateFence(const FenceDescriptor* descriptor); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 97 | InputStateBuilder* CreateInputStateBuilder(); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 98 | PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor); |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 99 | QueueBase* CreateQueue(); |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 100 | RenderPassDescriptorBuilder* CreateRenderPassDescriptorBuilder(); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 101 | RenderPipelineBase* CreateRenderPipeline(const RenderPipelineDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 102 | SamplerBase* CreateSampler(const SamplerDescriptor* descriptor); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 103 | ShaderModuleBase* CreateShaderModule(const ShaderModuleDescriptor* descriptor); |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 104 | SwapChainBase* CreateSwapChain(const SwapChainDescriptor* descriptor); |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 105 | TextureBase* CreateTexture(const TextureDescriptor* descriptor); |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 106 | TextureViewBase* CreateTextureView(TextureBase* texture, |
| 107 | const TextureViewDescriptor* descriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 108 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 109 | void Tick(); |
Austin Eng | 8b07e43 | 2018-12-01 03:20:19 +0000 | [diff] [blame] | 110 | |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 111 | void SetErrorCallback(dawn::DeviceErrorCallback callback, dawn::CallbackUserdata userdata); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 112 | void Reference(); |
| 113 | void Release(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 114 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 115 | BufferBuilder* CreateBufferBuilderForTesting() { |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
Bryan Bernhart | 74e95ff | 2019-01-29 00:10:07 +0000 | [diff] [blame] | 119 | virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer( |
| 120 | size_t size) = 0; |
| 121 | virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source, |
| 122 | uint32_t sourceOffset, |
| 123 | BufferBase* destination, |
| 124 | uint32_t destinationOffset, |
| 125 | uint32_t size) = 0; |
| 126 | |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 127 | ResultOrError<DynamicUploader*> GetDynamicUploader() const; |
| 128 | |
| 129 | protected: |
| 130 | std::unique_ptr<DynamicUploader> mDynamicUploader; |
| 131 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 132 | private: |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 133 | virtual ResultOrError<BindGroupBase*> CreateBindGroupImpl( |
| 134 | const BindGroupDescriptor* descriptor) = 0; |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 135 | virtual ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 136 | const BindGroupLayoutDescriptor* descriptor) = 0; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 137 | virtual ResultOrError<BufferBase*> CreateBufferImpl(const BufferDescriptor* descriptor) = 0; |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 138 | virtual ResultOrError<ComputePipelineBase*> CreateComputePipelineImpl( |
| 139 | const ComputePipelineDescriptor* descriptor) = 0; |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 140 | virtual ResultOrError<PipelineLayoutBase*> CreatePipelineLayoutImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 141 | const PipelineLayoutDescriptor* descriptor) = 0; |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 142 | virtual ResultOrError<QueueBase*> CreateQueueImpl() = 0; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 143 | virtual ResultOrError<RenderPipelineBase*> CreateRenderPipelineImpl( |
| 144 | const RenderPipelineDescriptor* descriptor) = 0; |
Corentin Wallez | 79d9e16 | 2018-05-28 15:40:41 -0400 | [diff] [blame] | 145 | virtual ResultOrError<SamplerBase*> CreateSamplerImpl( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 146 | const SamplerDescriptor* descriptor) = 0; |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 147 | virtual ResultOrError<ShaderModuleBase*> CreateShaderModuleImpl( |
| 148 | const ShaderModuleDescriptor* descriptor) = 0; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 149 | virtual ResultOrError<SwapChainBase*> CreateSwapChainImpl( |
| 150 | const SwapChainDescriptor* descriptor) = 0; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 151 | virtual ResultOrError<TextureBase*> CreateTextureImpl( |
| 152 | const TextureDescriptor* descriptor) = 0; |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 153 | virtual ResultOrError<TextureViewBase*> CreateTextureViewImpl( |
| 154 | TextureBase* texture, |
| 155 | const TextureViewDescriptor* descriptor) = 0; |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 156 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 157 | MaybeError CreateBindGroupInternal(BindGroupBase** result, |
| 158 | const BindGroupDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 159 | MaybeError CreateBindGroupLayoutInternal(BindGroupLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 160 | const BindGroupLayoutDescriptor* descriptor); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 161 | MaybeError CreateBufferInternal(BufferBase** result, const BufferDescriptor* descriptor); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 162 | MaybeError CreateComputePipelineInternal(ComputePipelineBase** result, |
| 163 | const ComputePipelineDescriptor* descriptor); |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 164 | MaybeError CreateFenceInternal(FenceBase** result, const FenceDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 165 | MaybeError CreatePipelineLayoutInternal(PipelineLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 166 | const PipelineLayoutDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 167 | MaybeError CreateQueueInternal(QueueBase** result); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 168 | MaybeError CreateRenderPipelineInternal(RenderPipelineBase** result, |
| 169 | const RenderPipelineDescriptor* descriptor); |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 170 | MaybeError CreateSamplerInternal(SamplerBase** result, const SamplerDescriptor* descriptor); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 171 | MaybeError CreateShaderModuleInternal(ShaderModuleBase** result, |
| 172 | const ShaderModuleDescriptor* descriptor); |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 173 | MaybeError CreateSwapChainInternal(SwapChainBase** result, |
| 174 | const SwapChainDescriptor* descriptor); |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 175 | MaybeError CreateTextureInternal(TextureBase** result, const TextureDescriptor* descriptor); |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 176 | MaybeError CreateTextureViewInternal(TextureViewBase** result, |
| 177 | TextureBase* texture, |
| 178 | const TextureViewDescriptor* descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 179 | |
| 180 | void ConsumeError(ErrorData* error); |
| 181 | |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 182 | AdapterBase* mAdapter = nullptr; |
| 183 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 184 | // The object caches aren't exposed in the header as they would require a lot of |
| 185 | // additional includes. |
| 186 | struct Caches; |
Corentin Wallez | cca9c69 | 2018-09-06 15:26:48 +0200 | [diff] [blame] | 187 | std::unique_ptr<Caches> mCaches; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 188 | |
Austin Eng | f0b761f | 2018-12-03 16:57:34 +0000 | [diff] [blame] | 189 | std::unique_ptr<FenceSignalTracker> mFenceSignalTracker; |
| 190 | |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 191 | dawn::DeviceErrorCallback mErrorCallback = nullptr; |
| 192 | dawn::CallbackUserdata mErrorUserdata = 0; |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 193 | uint32_t mRefCount = 1; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 194 | }; |
| 195 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 196 | } // namespace dawn_native |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 197 | |
Corentin Wallez | ac67fec | 2019-01-04 10:30:40 +0000 | [diff] [blame] | 198 | #endif // DAWNNATIVE_DEVICE_H_ |