blob: 0eb9810223d5131fdaa3f1d7401ec6a330911113 [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"
Austin Eng376f1c62017-05-30 20:03:44 -040022
Jiawei Shao58809d42018-09-19 00:32:52 +000023#include "dawn_native/DawnNative.h"
Corentin Wallez36afbb62018-07-25 17:03:23 +020024#include "dawn_native/dawn_platform.h"
Austin Eng376f1c62017-05-30 20:03:44 -040025
Corentin Wallezcca9c692018-09-06 15:26:48 +020026#include <memory>
27
Corentin Wallez49a65d02018-07-24 16:45:45 +020028namespace dawn_native {
Austin Eng376f1c62017-05-30 20:03:44 -040029
30 using ErrorCallback = void (*)(const char* errorMessage, void* userData);
31
Corentin Wallezec18f962019-01-10 10:50:54 +000032 class AdapterBase;
Corentin Wallezf20f5b92019-02-20 11:46:16 +000033 class BufferBuilder;
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:
Corentin Wallezec18f962019-01-10 10:50:54 +000040 DeviceBase(AdapterBase* adapter);
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;
Corentin Wallezc1400f02017-11-24 13:59:42 -050063 virtual InputStateBase* CreateInputState(InputStateBuilder* builder) = 0;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -040064 virtual RenderPassDescriptorBase* CreateRenderPassDescriptor(
65 RenderPassDescriptorBuilder* builder) = 0;
Austin Eng376f1c62017-05-30 20:03:44 -040066
Austin Eng8b07e432018-12-01 03:20:19 +000067 virtual Serial GetCompletedCommandSerial() const = 0;
68 virtual Serial GetLastSubmittedCommandSerial() const = 0;
Bryan Bernhart74e95ff2019-01-29 00:10:07 +000069 virtual Serial GetPendingCommandSerial() const = 0;
Corentin Wallezc1400f02017-11-24 13:59:42 -050070 virtual void TickImpl() = 0;
Corentin Wallezdbb57292017-06-14 13:33:45 -040071
Corentin Wallez9fc65342018-07-18 15:28:38 +020072 // Many Dawn objects are completely immutable once created which means that if two
Corentin Wallezc1400f02017-11-24 13:59:42 -050073 // 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 Ninomiya234becf2018-07-10 12:23:50 -070086 ResultOrError<BindGroupLayoutBase*> GetOrCreateBindGroupLayout(
Corentin Wallez36afbb62018-07-25 17:03:23 +020087 const BindGroupLayoutDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050088 void UncacheBindGroupLayout(BindGroupLayoutBase* obj);
Austin Eng376f1c62017-05-30 20:03:44 -040089
Corentin Wallez0927ea52018-07-18 15:20:28 +020090 // Dawn API
Corentin Wallez6f9d21e2018-12-05 07:18:30 +000091 BindGroupBase* CreateBindGroup(const BindGroupDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +020092 BindGroupLayoutBase* CreateBindGroupLayout(const BindGroupLayoutDescriptor* descriptor);
Corentin Wallez82b65732018-08-22 15:37:29 +020093 BufferBase* CreateBuffer(const BufferDescriptor* descriptor);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000094 CommandEncoderBase* CreateCommandEncoder();
Corentin Wallez8e335a52018-08-27 23:12:56 +020095 ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor);
Austin Engf0b761f2018-12-03 16:57:34 +000096 FenceBase* CreateFence(const FenceDescriptor* descriptor);
Corentin Wallezc1400f02017-11-24 13:59:42 -050097 InputStateBuilder* CreateInputStateBuilder();
Corentin Wallez36afbb62018-07-25 17:03:23 +020098 PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor);
Corentin Wallezb703def2018-06-14 20:26:27 -040099 QueueBase* CreateQueue();
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400100 RenderPassDescriptorBuilder* CreateRenderPassDescriptorBuilder();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000101 RenderPipelineBase* CreateRenderPipeline(const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200102 SamplerBase* CreateSampler(const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200103 ShaderModuleBase* CreateShaderModule(const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000104 SwapChainBase* CreateSwapChain(const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800105 TextureBase* CreateTexture(const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000106 TextureViewBase* CreateTextureView(TextureBase* texture,
107 const TextureViewDescriptor* descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400108
Corentin Wallezc1400f02017-11-24 13:59:42 -0500109 void Tick();
Austin Eng8b07e432018-12-01 03:20:19 +0000110
Corentin Wallez226110f2018-07-18 11:38:11 +0200111 void SetErrorCallback(dawn::DeviceErrorCallback callback, dawn::CallbackUserdata userdata);
Corentin Wallezc1400f02017-11-24 13:59:42 -0500112 void Reference();
113 void Release();
Austin Eng376f1c62017-05-30 20:03:44 -0400114
Corentin Wallez82b65732018-08-22 15:37:29 +0200115 BufferBuilder* CreateBufferBuilderForTesting() {
116 return nullptr;
117 }
118
Bryan Bernhart74e95ff2019-01-29 00:10:07 +0000119 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 Bernhart67a73bd2019-02-15 21:18:40 +0000127 ResultOrError<DynamicUploader*> GetDynamicUploader() const;
128
129 protected:
130 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);
Austin Engf0b761f2018-12-03 16:57:34 +0000164 MaybeError CreateFenceInternal(FenceBase** result, const FenceDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200165 MaybeError CreatePipelineLayoutInternal(PipelineLayoutBase** result,
Corentin Wallez36afbb62018-07-25 17:03:23 +0200166 const PipelineLayoutDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200167 MaybeError CreateQueueInternal(QueueBase** result);
Yan, Shaoboa4924272018-12-10 19:47:22 +0000168 MaybeError CreateRenderPipelineInternal(RenderPipelineBase** result,
169 const RenderPipelineDescriptor* descriptor);
Corentin Wallez36afbb62018-07-25 17:03:23 +0200170 MaybeError CreateSamplerInternal(SamplerBase** result, const SamplerDescriptor* descriptor);
Corentin Wallezdf671032018-08-20 17:01:20 +0200171 MaybeError CreateShaderModuleInternal(ShaderModuleBase** result,
172 const ShaderModuleDescriptor* descriptor);
Corentin Wallez7be2a712019-02-15 11:15:58 +0000173 MaybeError CreateSwapChainInternal(SwapChainBase** result,
174 const SwapChainDescriptor* descriptor);
Jiawei Shao425428f2018-08-27 08:44:48 +0800175 MaybeError CreateTextureInternal(TextureBase** result, const TextureDescriptor* descriptor);
Jiawei Shao6329e5a2018-10-12 08:32:58 +0000176 MaybeError CreateTextureViewInternal(TextureViewBase** result,
177 TextureBase* texture,
178 const TextureViewDescriptor* descriptor);
Corentin Wallez52f23832018-07-16 17:40:08 +0200179
180 void ConsumeError(ErrorData* error);
181
Corentin Wallezec18f962019-01-10 10:50:54 +0000182 AdapterBase* mAdapter = nullptr;
183
Corentin Wallezc1400f02017-11-24 13:59:42 -0500184 // The object caches aren't exposed in the header as they would require a lot of
185 // additional includes.
186 struct Caches;
Corentin Wallezcca9c692018-09-06 15:26:48 +0200187 std::unique_ptr<Caches> mCaches;
Austin Eng376f1c62017-05-30 20:03:44 -0400188
Austin Engf0b761f2018-12-03 16:57:34 +0000189 std::unique_ptr<FenceSignalTracker> mFenceSignalTracker;
190
Corentin Wallez226110f2018-07-18 11:38:11 +0200191 dawn::DeviceErrorCallback mErrorCallback = nullptr;
192 dawn::CallbackUserdata mErrorUserdata = 0;
Corentin Wallezc1400f02017-11-24 13:59:42 -0500193 uint32_t mRefCount = 1;
Austin Eng376f1c62017-05-30 20:03:44 -0400194 };
195
Corentin Wallez49a65d02018-07-24 16:45:45 +0200196} // namespace dawn_native
Austin Eng376f1c62017-05-30 20:03:44 -0400197
Corentin Wallezac67fec2019-01-04 10:30:40 +0000198#endif // DAWNNATIVE_DEVICE_H_