blob: 3f4a913ef1d9132bf8af62c27878049301d3658f [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
Austin Eng8b07e432018-12-01 03:20:19 +000018#include "common/Serial.h"
Corentin Wallezd37523f2018-07-24 13:53:51 +020019#include "dawn_native/Error.h"
20#include "dawn_native/Forward.h"
Corentin Wallez9e4518b2018-10-15 12:54:30 +000021#include "dawn_native/ObjectBase.h"
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000022#include "dawn_native/Toggles.h"
Austin Eng376f1c62017-05-30 20:03:44 -040023
Jiawei Shao58809d42018-09-19 00:32:52 +000024#include "dawn_native/DawnNative.h"
Corentin Wallez36afbb62018-07-25 17:03:23 +020025#include "dawn_native/dawn_platform.h"
Austin Eng376f1c62017-05-30 20:03:44 -040026
Corentin Wallezcca9c692018-09-06 15:26:48 +020027#include <memory>
28
Corentin Wallez49a65d02018-07-24 16:45:45 +020029namespace dawn_native {
Austin Eng376f1c62017-05-30 20:03:44 -040030
31 using ErrorCallback = void (*)(const char* errorMessage, void* userData);
32
Corentin Wallezec18f962019-01-10 10:50:54 +000033 class AdapterBase;
Austin Engf0b761f2018-12-03 16:57:34 +000034 class FenceSignalTracker;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +000035 class DynamicUploader;
36 class StagingBufferBase;
Austin Engf0b761f2018-12-03 16:57:34 +000037
Austin Eng376f1c62017-05-30 20:03:44 -040038 class DeviceBase {
Corentin Wallezc1400f02017-11-24 13:59:42 -050039 public:
Jiawei Shao15d4c2e2019-04-26 07:52:57 +000040 DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050041 virtual ~DeviceBase();
Austin Eng376f1c62017-05-30 20:03:44 -040042
Corentin Wallezc1400f02017-11-24 13:59:42 -050043 void HandleError(const char* message);
Austin Eng376f1c62017-05-30 20:03:44 -040044
Corentin Wallez52f23832018-07-16 17:40:08 +020045 bool ConsumedError(MaybeError maybeError) {
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020046 if (DAWN_UNLIKELY(maybeError.IsError())) {
Corentin Wallez52f23832018-07-16 17:40:08 +020047 ConsumeError(maybeError.AcquireError());
48 return true;
49 }
50 return false;
51 }
52
Corentin Walleza594f8f2019-02-13 13:09:18 +000053 MaybeError ValidateObject(const ObjectBase* object) const;
54
Corentin Wallezd77fd5f2019-01-30 16:07:48 +000055 AdapterBase* GetAdapter() const;
56
Corentin Wallezc1400f02017-11-24 13:59:42 -050057 // Used by autogenerated code, returns itself
58 DeviceBase* GetDevice();
Austin Eng376f1c62017-05-30 20:03:44 -040059
Austin Engf0b761f2018-12-03 16:57:34 +000060 FenceSignalTracker* GetFenceSignalTracker() const;
61
Corentin Wallezf20f5b92019-02-20 11:46:16 +000062 virtual CommandBufferBase* CreateCommandBuffer(CommandEncoderBase* encoder) = 0;
Austin Eng376f1c62017-05-30 20:03:44 -040063
Austin Eng8b07e432018-12-01 03:20:19 +000064 virtual Serial GetCompletedCommandSerial() const = 0;
65 virtual Serial GetLastSubmittedCommandSerial() const = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +000066 virtual Serial GetPendingCommandSerial() const = 0;
Corentin Wallezc1400f02017-11-24 13:59:42 -050067 virtual void TickImpl() = 0;
Corentin Wallezdbb57292017-06-14 13:33:45 -040068
Corentin Wallez9fc65342018-07-18 15:28:38 +020069 // Many Dawn objects are completely immutable once created which means that if two
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +000070 // creations are given the same arguments, they can return the same object. Reusing
Corentin Wallezc1400f02017-11-24 13:59:42 -050071 // objects will help make comparisons between objects by a single pointer comparison.
72 //
73 // Technically no object is immutable as they have a reference count, and an
74 // application with reference-counting issues could "see" that objects are reused.
75 // This is solved by automatic-reference counting, and also the fact that when using
76 // the client-server wire every creation will get a different proxy object, with a
77 // different reference count.
78 //
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +000079 // When trying to create an object, we give both the descriptor and an example of what
80 // the created object will be, the "blueprint". The blueprint is just a FooBase object
Corentin Wallezc1400f02017-11-24 13:59:42 -050081 // instead of a backend Foo object. If the blueprint doesn't match an object in the
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +000082 // cache, then the descriptor is used to make a new object.
Kai Ninomiya234becf2018-07-10 12:23:50 -070083 ResultOrError<BindGroupLayoutBase*> GetOrCreateBindGroupLayout(
Corentin Wallez36afbb62018-07-25 17:03:23 +020084 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050085 void UncacheBindGroupLayout(BindGroupLayoutBase* obj);
Austin Eng376f1c62017-05-30 20:03:44 -040086
Corentin Wallez0ee98592019-05-01 12:57:27 +000087 ResultOrError<PipelineLayoutBase*> GetOrCreatePipelineLayout(
88 const PipelineLayoutDescriptor* descriptor);
89 void UncachePipelineLayout(PipelineLayoutBase* obj);
90
Corentin Wallez0927ea52018-07-18 15:20:28 +020091 // Dawn API
Corentin Wallez6f9d21e2018-12-05 07:18:30 +000092 BindGroupBase* CreateBindGroup(const BindGroupDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +020093 BindGroupLayoutBase* CreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez82b65732018-08-22 15:37:29 +020094 BufferBase* CreateBuffer(const BufferDescriptor* descriptor);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000095 CommandEncoderBase* CreateCommandEncoder();
Corentin Wallez8e335a52018-08-27 23:12:56 +020096 ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +020097 PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor);
Corentin Wallezb703def2018-06-14 20:26:27 -040098 QueueBase* CreateQueue();
Yan, Shaoboa4924272018-12-10 19:47:22 +000099 RenderPipelineBase* CreateRenderPipeline(const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200100 SamplerBase* CreateSampler(const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200101 ShaderModuleBase* CreateShaderModule(const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000102 SwapChainBase* CreateSwapChain(const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800103 TextureBase* CreateTexture(const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000104 TextureViewBase* CreateTextureView(TextureBase* texture,
105 const TextureViewDescriptor* descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400106
Corentin Wallezc1400f02017-11-24 13:59:42 -0500107 void Tick();
Austin Eng8b07e432018-12-01 03:20:19 +0000108
Corentin Wallez226110f2018-07-18 11:38:11 +0200109 void SetErrorCallback(dawn::DeviceErrorCallback callback, dawn::CallbackUserdata userdata);
Corentin Wallezc1400f02017-11-24 13:59:42 -0500110 void Reference();
111 void Release();
Austin Eng376f1c62017-05-30 20:03:44 -0400112
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000113 virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(
114 size_t size) = 0;
115 virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
Austin Engcf52d712019-04-05 20:51:29 +0000116 uint64_t sourceOffset,
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000117 BufferBase* destination,
Austin Engcf52d712019-04-05 20:51:29 +0000118 uint64_t destinationOffset,
119 uint64_t size) = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000120
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000121 ResultOrError<DynamicUploader*> GetDynamicUploader() const;
122
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000123 std::vector<const char*> GetTogglesUsed() const;
124 bool IsToggleEnabled(Toggle toggle) const;
125
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000126 protected:
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000127 void SetToggle(Toggle toggle, bool isEnabled);
128 void ApplyToggleOverrides(const DeviceDescriptor* deviceDescriptor);
129
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000130 std::unique_ptr<DynamicUploader> mDynamicUploader;
131
Corentin Wallezc1400f02017-11-24 13:59:42 -0500132 private:
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000133 virtual ResultOrError<BindGroupBase*> CreateBindGroupImpl(
134 const BindGroupDescriptor* descriptor) = 0;
Kai Ninomiya234becf2018-07-10 12:23:50 -0700135 virtual ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200136 const BindGroupLayoutDescriptor* descriptor) = 0;
Corentin Wallez82b65732018-08-22 15:37:29 +0200137 virtual ResultOrError<BufferBase*> CreateBufferImpl(const BufferDescriptor* descriptor) = 0;
Corentin Wallez8e335a52018-08-27 23:12:56 +0200138 virtual ResultOrError<ComputePipelineBase*> CreateComputePipelineImpl(
139 const ComputePipelineDescriptor* descriptor) = 0;
Kai Ninomiyaf53f98b2018-06-27 16:21:39 -0700140 virtual ResultOrError<PipelineLayoutBase*> CreatePipelineLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200141 const PipelineLayoutDescriptor* descriptor) = 0;
Corentin Wallezb703def2018-06-14 20:26:27 -0400142 virtual ResultOrError<QueueBase*> CreateQueueImpl() = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000143 virtual ResultOrError<RenderPipelineBase*> CreateRenderPipelineImpl(
144 const RenderPipelineDescriptor* descriptor) = 0;
Corentin Wallez79d9e162018-05-28 15:40:41 -0400145 virtual ResultOrError<SamplerBase*> CreateSamplerImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200146 const SamplerDescriptor* descriptor) = 0;
Corentin Wallezdf671032018-08-20 17:01:20 +0200147 virtual ResultOrError<ShaderModuleBase*> CreateShaderModuleImpl(
148 const ShaderModuleDescriptor* descriptor) = 0;
Corentin Wallez7be2a712019-02-15 11:15:58 +0000149 virtual ResultOrError<SwapChainBase*> CreateSwapChainImpl(
150 const SwapChainDescriptor* descriptor) = 0;
Jiawei Shao425428f2018-08-27 08:44:48 +0800151 virtual ResultOrError<TextureBase*> CreateTextureImpl(
152 const TextureDescriptor* descriptor) = 0;
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000153 virtual ResultOrError<TextureViewBase*> CreateTextureViewImpl(
154 TextureBase* texture,
155 const TextureViewDescriptor* descriptor) = 0;
Corentin Wallez1ae19e82018-05-17 17:09:07 -0400156
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000157 MaybeError CreateBindGroupInternal(BindGroupBase** result,
158 const BindGroupDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200159 MaybeError CreateBindGroupLayoutInternal(BindGroupLayoutBase** result,
Corentin Wallez36afbb62018-07-25 17:03:23 +0200160 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez82b65732018-08-22 15:37:29 +0200161 MaybeError CreateBufferInternal(BufferBase** result, const BufferDescriptor* descriptor);
Corentin Wallez8e335a52018-08-27 23:12:56 +0200162 MaybeError CreateComputePipelineInternal(ComputePipelineBase** result,
163 const ComputePipelineDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200164 MaybeError CreatePipelineLayoutInternal(PipelineLayoutBase** result,
Corentin Wallez36afbb62018-07-25 17:03:23 +0200165 const PipelineLayoutDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200166 MaybeError CreateQueueInternal(QueueBase** result);
Yan, Shaoboa4924272018-12-10 19:47:22 +0000167 MaybeError CreateRenderPipelineInternal(RenderPipelineBase** result,
168 const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200169 MaybeError CreateSamplerInternal(SamplerBase** result, const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200170 MaybeError CreateShaderModuleInternal(ShaderModuleBase** result,
171 const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000172 MaybeError CreateSwapChainInternal(SwapChainBase** result,
173 const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800174 MaybeError CreateTextureInternal(TextureBase** result, const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000175 MaybeError CreateTextureViewInternal(TextureViewBase** result,
176 TextureBase* texture,
177 const TextureViewDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200178
179 void ConsumeError(ErrorData* error);
180
Corentin Wallezec18f962019-01-10 10:50:54 +0000181 AdapterBase* mAdapter = nullptr;
182
Corentin Wallezc1400f02017-11-24 13:59:42 -0500183 // The object caches aren't exposed in the header as they would require a lot of
184 // additional includes.
185 struct Caches;
Corentin Wallezcca9c692018-09-06 15:26:48 +0200186 std::unique_ptr<Caches> mCaches;
Austin Eng376f1c62017-05-30 20:03:44 -0400187
Austin Engf0b761f2018-12-03 16:57:34 +0000188 std::unique_ptr<FenceSignalTracker> mFenceSignalTracker;
189
Corentin Wallez226110f2018-07-18 11:38:11 +0200190 dawn::DeviceErrorCallback mErrorCallback = nullptr;
191 dawn::CallbackUserdata mErrorUserdata = 0;
Corentin Wallezc1400f02017-11-24 13:59:42 -0500192 uint32_t mRefCount = 1;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000193
194 TogglesSet mTogglesSet;
Austin Eng376f1c62017-05-30 20:03:44 -0400195 };
196
Corentin Wallez49a65d02018-07-24 16:45:45 +0200197} // namespace dawn_native
Austin Eng376f1c62017-05-30 20:03:44 -0400198
Corentin Wallezac67fec2019-01-04 10:30:40 +0000199#endif // DAWNNATIVE_DEVICE_H_