blob: b9039e43d14bd74de4dfefbfc1cf77f925ca348c [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 Wallez1152bba2019-05-01 13:48:47 +000087 ResultOrError<ComputePipelineBase*> GetOrCreateComputePipeline(
88 const ComputePipelineDescriptor* descriptor);
89 void UncacheComputePipeline(ComputePipelineBase* obj);
90
Corentin Wallez0ee98592019-05-01 12:57:27 +000091 ResultOrError<PipelineLayoutBase*> GetOrCreatePipelineLayout(
92 const PipelineLayoutDescriptor* descriptor);
93 void UncachePipelineLayout(PipelineLayoutBase* obj);
94
Corentin Wallez94274b62019-05-02 15:30:56 +000095 ResultOrError<RenderPipelineBase*> GetOrCreateRenderPipeline(
96 const RenderPipelineDescriptor* descriptor);
97 void UncacheRenderPipeline(RenderPipelineBase* obj);
98
Corentin Wallezc5351982019-05-01 13:27:07 +000099 ResultOrError<ShaderModuleBase*> GetOrCreateShaderModule(
100 const ShaderModuleDescriptor* descriptor);
101 void UncacheShaderModule(ShaderModuleBase* obj);
102
Corentin Wallez0927ea52018-07-18 15:20:28 +0200103 // Dawn API
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000104 BindGroupBase* CreateBindGroup(const BindGroupDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200105 BindGroupLayoutBase* CreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez82b65732018-08-22 15:37:29 +0200106 BufferBase* CreateBuffer(const BufferDescriptor* descriptor);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000107 CommandEncoderBase* CreateCommandEncoder();
Corentin Wallez8e335a52018-08-27 23:12:56 +0200108 ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200109 PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor);
Corentin Wallezb703def2018-06-14 20:26:27 -0400110 QueueBase* CreateQueue();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000111 RenderPipelineBase* CreateRenderPipeline(const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200112 SamplerBase* CreateSampler(const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200113 ShaderModuleBase* CreateShaderModule(const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000114 SwapChainBase* CreateSwapChain(const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800115 TextureBase* CreateTexture(const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000116 TextureViewBase* CreateTextureView(TextureBase* texture,
117 const TextureViewDescriptor* descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400118
Corentin Wallezc1400f02017-11-24 13:59:42 -0500119 void Tick();
Austin Eng8b07e432018-12-01 03:20:19 +0000120
Corentin Wallez226110f2018-07-18 11:38:11 +0200121 void SetErrorCallback(dawn::DeviceErrorCallback callback, dawn::CallbackUserdata userdata);
Corentin Wallezc1400f02017-11-24 13:59:42 -0500122 void Reference();
123 void Release();
Austin Eng376f1c62017-05-30 20:03:44 -0400124
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000125 virtual ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(
126 size_t size) = 0;
127 virtual MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
Austin Engcf52d712019-04-05 20:51:29 +0000128 uint64_t sourceOffset,
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000129 BufferBase* destination,
Austin Engcf52d712019-04-05 20:51:29 +0000130 uint64_t destinationOffset,
131 uint64_t size) = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000132
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000133 ResultOrError<DynamicUploader*> GetDynamicUploader() const;
134
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000135 std::vector<const char*> GetTogglesUsed() const;
136 bool IsToggleEnabled(Toggle toggle) const;
137
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000138 protected:
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000139 void SetToggle(Toggle toggle, bool isEnabled);
140 void ApplyToggleOverrides(const DeviceDescriptor* deviceDescriptor);
141
Bryan Bernhart67a73bd2019-02-15 21:18:40 +0000142 std::unique_ptr<DynamicUploader> mDynamicUploader;
143
Corentin Wallezc1400f02017-11-24 13:59:42 -0500144 private:
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000145 virtual ResultOrError<BindGroupBase*> CreateBindGroupImpl(
146 const BindGroupDescriptor* descriptor) = 0;
Kai Ninomiya234becf2018-07-10 12:23:50 -0700147 virtual ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200148 const BindGroupLayoutDescriptor* descriptor) = 0;
Corentin Wallez82b65732018-08-22 15:37:29 +0200149 virtual ResultOrError<BufferBase*> CreateBufferImpl(const BufferDescriptor* descriptor) = 0;
Corentin Wallez8e335a52018-08-27 23:12:56 +0200150 virtual ResultOrError<ComputePipelineBase*> CreateComputePipelineImpl(
151 const ComputePipelineDescriptor* descriptor) = 0;
Kai Ninomiyaf53f98b2018-06-27 16:21:39 -0700152 virtual ResultOrError<PipelineLayoutBase*> CreatePipelineLayoutImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200153 const PipelineLayoutDescriptor* descriptor) = 0;
Corentin Wallezb703def2018-06-14 20:26:27 -0400154 virtual ResultOrError<QueueBase*> CreateQueueImpl() = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000155 virtual ResultOrError<RenderPipelineBase*> CreateRenderPipelineImpl(
156 const RenderPipelineDescriptor* descriptor) = 0;
Corentin Wallez79d9e162018-05-28 15:40:41 -0400157 virtual ResultOrError<SamplerBase*> CreateSamplerImpl(
Corentin Wallez36afbb62018-07-25 17:03:23 +0200158 const SamplerDescriptor* descriptor) = 0;
Corentin Wallezdf671032018-08-20 17:01:20 +0200159 virtual ResultOrError<ShaderModuleBase*> CreateShaderModuleImpl(
160 const ShaderModuleDescriptor* descriptor) = 0;
Corentin Wallez7be2a712019-02-15 11:15:58 +0000161 virtual ResultOrError<SwapChainBase*> CreateSwapChainImpl(
162 const SwapChainDescriptor* descriptor) = 0;
Jiawei Shao425428f2018-08-27 08:44:48 +0800163 virtual ResultOrError<TextureBase*> CreateTextureImpl(
164 const TextureDescriptor* descriptor) = 0;
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000165 virtual ResultOrError<TextureViewBase*> CreateTextureViewImpl(
166 TextureBase* texture,
167 const TextureViewDescriptor* descriptor) = 0;
Corentin Wallez1ae19e82018-05-17 17:09:07 -0400168
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000169 MaybeError CreateBindGroupInternal(BindGroupBase** result,
170 const BindGroupDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200171 MaybeError CreateBindGroupLayoutInternal(BindGroupLayoutBase** result,
Corentin Wallez36afbb62018-07-25 17:03:23 +0200172 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez82b65732018-08-22 15:37:29 +0200173 MaybeError CreateBufferInternal(BufferBase** result, const BufferDescriptor* descriptor);
Corentin Wallez8e335a52018-08-27 23:12:56 +0200174 MaybeError CreateComputePipelineInternal(ComputePipelineBase** result,
175 const ComputePipelineDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200176 MaybeError CreatePipelineLayoutInternal(PipelineLayoutBase** result,
Corentin Wallez36afbb62018-07-25 17:03:23 +0200177 const PipelineLayoutDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200178 MaybeError CreateQueueInternal(QueueBase** result);
Yan, Shaoboa4924272018-12-10 19:47:22 +0000179 MaybeError CreateRenderPipelineInternal(RenderPipelineBase** result,
180 const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200181 MaybeError CreateSamplerInternal(SamplerBase** result, const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200182 MaybeError CreateShaderModuleInternal(ShaderModuleBase** result,
183 const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000184 MaybeError CreateSwapChainInternal(SwapChainBase** result,
185 const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800186 MaybeError CreateTextureInternal(TextureBase** result, const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000187 MaybeError CreateTextureViewInternal(TextureViewBase** result,
188 TextureBase* texture,
189 const TextureViewDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200190
191 void ConsumeError(ErrorData* error);
192
Corentin Wallezec18f962019-01-10 10:50:54 +0000193 AdapterBase* mAdapter = nullptr;
194
Corentin Wallezc1400f02017-11-24 13:59:42 -0500195 // The object caches aren't exposed in the header as they would require a lot of
196 // additional includes.
197 struct Caches;
Corentin Wallezcca9c692018-09-06 15:26:48 +0200198 std::unique_ptr<Caches> mCaches;
Austin Eng376f1c62017-05-30 20:03:44 -0400199
Austin Engf0b761f2018-12-03 16:57:34 +0000200 std::unique_ptr<FenceSignalTracker> mFenceSignalTracker;
201
Corentin Wallez226110f2018-07-18 11:38:11 +0200202 dawn::DeviceErrorCallback mErrorCallback = nullptr;
203 dawn::CallbackUserdata mErrorUserdata = 0;
Corentin Wallezc1400f02017-11-24 13:59:42 -0500204 uint32_t mRefCount = 1;
Jiawei Shao15d4c2e2019-04-26 07:52:57 +0000205
206 TogglesSet mTogglesSet;
Austin Eng376f1c62017-05-30 20:03:44 -0400207 };
208
Corentin Wallez49a65d02018-07-24 16:45:45 +0200209} // namespace dawn_native
Austin Eng376f1c62017-05-30 20:03:44 -0400210
Corentin Wallezac67fec2019-01-04 10:30:40 +0000211#endif // DAWNNATIVE_DEVICE_H_