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 | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 15 | #include "dawn_native/Device.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 17 | #include "dawn_native/BindGroup.h" |
| 18 | #include "dawn_native/BindGroupLayout.h" |
| 19 | #include "dawn_native/BlendState.h" |
| 20 | #include "dawn_native/Buffer.h" |
| 21 | #include "dawn_native/CommandBuffer.h" |
| 22 | #include "dawn_native/ComputePipeline.h" |
| 23 | #include "dawn_native/DepthStencilState.h" |
| 24 | #include "dawn_native/ErrorData.h" |
| 25 | #include "dawn_native/InputState.h" |
| 26 | #include "dawn_native/PipelineLayout.h" |
| 27 | #include "dawn_native/Queue.h" |
| 28 | #include "dawn_native/RenderPassDescriptor.h" |
| 29 | #include "dawn_native/RenderPipeline.h" |
| 30 | #include "dawn_native/Sampler.h" |
| 31 | #include "dawn_native/ShaderModule.h" |
| 32 | #include "dawn_native/SwapChain.h" |
| 33 | #include "dawn_native/Texture.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 34 | |
| 35 | #include <unordered_set> |
| 36 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 37 | namespace dawn_native { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 38 | |
| 39 | // DeviceBase::Caches |
| 40 | |
| 41 | // The caches are unordered_sets of pointers with special hash and compare functions |
| 42 | // to compare the value of the objects, instead of the pointers. |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 43 | using BindGroupLayoutCache = std:: |
| 44 | unordered_set<BindGroupLayoutBase*, BindGroupLayoutCacheFuncs, BindGroupLayoutCacheFuncs>; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 45 | |
| 46 | struct DeviceBase::Caches { |
| 47 | BindGroupLayoutCache bindGroupLayouts; |
| 48 | }; |
| 49 | |
| 50 | // DeviceBase |
| 51 | |
| 52 | DeviceBase::DeviceBase() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 53 | mCaches = new DeviceBase::Caches(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | DeviceBase::~DeviceBase() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 57 | delete mCaches; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void DeviceBase::HandleError(const char* message) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 61 | if (mErrorCallback) { |
| 62 | mErrorCallback(message, mErrorUserdata); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 66 | void DeviceBase::SetErrorCallback(dawn::DeviceErrorCallback callback, |
| 67 | dawn::CallbackUserdata userdata) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 68 | mErrorCallback = callback; |
| 69 | mErrorUserdata = userdata; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | DeviceBase* DeviceBase::GetDevice() { |
| 73 | return this; |
| 74 | } |
| 75 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 76 | ResultOrError<BindGroupLayoutBase*> DeviceBase::GetOrCreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 77 | const BindGroupLayoutDescriptor* descriptor) { |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 78 | BindGroupLayoutBase blueprint(this, descriptor, true); |
| 79 | |
| 80 | auto iter = mCaches->bindGroupLayouts.find(&blueprint); |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 81 | if (iter != mCaches->bindGroupLayouts.end()) { |
Corentin Wallez | df5a18d | 2018-04-17 16:35:23 -0400 | [diff] [blame] | 82 | (*iter)->Reference(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 83 | return *iter; |
| 84 | } |
| 85 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 86 | BindGroupLayoutBase* backendObj; |
Corentin Wallez | 33ca496 | 2018-07-18 11:45:17 +0200 | [diff] [blame] | 87 | DAWN_TRY_ASSIGN(backendObj, CreateBindGroupLayoutImpl(descriptor)); |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 88 | mCaches->bindGroupLayouts.insert(backendObj); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 89 | return backendObj; |
| 90 | } |
| 91 | |
| 92 | void DeviceBase::UncacheBindGroupLayout(BindGroupLayoutBase* obj) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 93 | mCaches->bindGroupLayouts.erase(obj); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 96 | // Object creation API methods |
| 97 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 98 | BindGroupBuilder* DeviceBase::CreateBindGroupBuilder() { |
| 99 | return new BindGroupBuilder(this); |
| 100 | } |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 101 | BindGroupLayoutBase* DeviceBase::CreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 102 | const BindGroupLayoutDescriptor* descriptor) { |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 103 | BindGroupLayoutBase* result = nullptr; |
| 104 | |
| 105 | if (ConsumedError(CreateBindGroupLayoutInternal(&result, descriptor))) { |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 106 | return nullptr; |
| 107 | } |
| 108 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 109 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 110 | } |
Austin Eng | 94bebe5 | 2017-07-26 16:59:53 -0400 | [diff] [blame] | 111 | BlendStateBuilder* DeviceBase::CreateBlendStateBuilder() { |
| 112 | return new BlendStateBuilder(this); |
| 113 | } |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 114 | BufferBase* DeviceBase::CreateBuffer(const BufferDescriptor* descriptor) { |
| 115 | BufferBase* result = nullptr; |
| 116 | |
| 117 | if (ConsumedError(CreateBufferInternal(&result, descriptor))) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 122 | } |
| 123 | CommandBufferBuilder* DeviceBase::CreateCommandBufferBuilder() { |
| 124 | return new CommandBufferBuilder(this); |
| 125 | } |
Corentin Wallez | 29ced28 | 2017-07-14 10:58:07 -0400 | [diff] [blame] | 126 | ComputePipelineBuilder* DeviceBase::CreateComputePipelineBuilder() { |
| 127 | return new ComputePipelineBuilder(this); |
| 128 | } |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 129 | DepthStencilStateBuilder* DeviceBase::CreateDepthStencilStateBuilder() { |
| 130 | return new DepthStencilStateBuilder(this); |
| 131 | } |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 132 | InputStateBuilder* DeviceBase::CreateInputStateBuilder() { |
| 133 | return new InputStateBuilder(this); |
| 134 | } |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 135 | PipelineLayoutBase* DeviceBase::CreatePipelineLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 136 | const PipelineLayoutDescriptor* descriptor) { |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 137 | PipelineLayoutBase* result = nullptr; |
| 138 | |
| 139 | if (ConsumedError(CreatePipelineLayoutInternal(&result, descriptor))) { |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 140 | return nullptr; |
| 141 | } |
| 142 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 143 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 144 | } |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 145 | QueueBase* DeviceBase::CreateQueue() { |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 146 | QueueBase* result = nullptr; |
| 147 | |
| 148 | if (ConsumedError(CreateQueueInternal(&result))) { |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 149 | return nullptr; |
| 150 | } |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 151 | |
| 152 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 153 | } |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 154 | RenderPassDescriptorBuilder* DeviceBase::CreateRenderPassDescriptorBuilder() { |
| 155 | return new RenderPassDescriptorBuilder(this); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 156 | } |
Corentin Wallez | 29ced28 | 2017-07-14 10:58:07 -0400 | [diff] [blame] | 157 | RenderPipelineBuilder* DeviceBase::CreateRenderPipelineBuilder() { |
| 158 | return new RenderPipelineBuilder(this); |
| 159 | } |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 160 | SamplerBase* DeviceBase::CreateSampler(const SamplerDescriptor* descriptor) { |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 161 | SamplerBase* result = nullptr; |
| 162 | |
| 163 | if (ConsumedError(CreateSamplerInternal(&result, descriptor))) { |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 164 | return nullptr; |
| 165 | } |
Corentin Wallez | 50e0986 | 2018-05-28 14:26:40 -0400 | [diff] [blame] | 166 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 167 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 168 | } |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 169 | ShaderModuleBase* DeviceBase::CreateShaderModule(const ShaderModuleDescriptor* descriptor) { |
| 170 | ShaderModuleBase* result = nullptr; |
| 171 | |
| 172 | if (ConsumedError(CreateShaderModuleInternal(&result, descriptor))) { |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 177 | } |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 178 | SwapChainBuilder* DeviceBase::CreateSwapChainBuilder() { |
| 179 | return new SwapChainBuilder(this); |
| 180 | } |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 181 | TextureBase* DeviceBase::CreateTexture(const TextureDescriptor* descriptor) { |
| 182 | TextureBase* result = nullptr; |
| 183 | |
| 184 | if (ConsumedError(CreateTextureInternal(&result, descriptor))) { |
| 185 | return nullptr; |
| 186 | } |
| 187 | return result; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 190 | // Other Device API methods |
| 191 | |
Corentin Wallez | dbb5729 | 2017-06-14 13:33:45 -0400 | [diff] [blame] | 192 | void DeviceBase::Tick() { |
| 193 | TickImpl(); |
| 194 | } |
| 195 | |
Austin Eng | f9c39d0 | 2017-07-10 11:16:51 -0400 | [diff] [blame] | 196 | void DeviceBase::Reference() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 197 | ASSERT(mRefCount != 0); |
| 198 | mRefCount++; |
Austin Eng | f9c39d0 | 2017-07-10 11:16:51 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void DeviceBase::Release() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 202 | ASSERT(mRefCount != 0); |
| 203 | mRefCount--; |
| 204 | if (mRefCount == 0) { |
Austin Eng | f9c39d0 | 2017-07-10 11:16:51 -0400 | [diff] [blame] | 205 | delete this; |
| 206 | } |
| 207 | } |
| 208 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 209 | // Implementation details of object creation |
| 210 | |
| 211 | MaybeError DeviceBase::CreateBindGroupLayoutInternal( |
| 212 | BindGroupLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 213 | const BindGroupLayoutDescriptor* descriptor) { |
Corentin Wallez | 33ca496 | 2018-07-18 11:45:17 +0200 | [diff] [blame] | 214 | DAWN_TRY(ValidateBindGroupLayoutDescriptor(this, descriptor)); |
| 215 | DAWN_TRY_ASSIGN(*result, GetOrCreateBindGroupLayout(descriptor)); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 216 | return {}; |
| 217 | } |
| 218 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 219 | MaybeError DeviceBase::CreateBufferInternal(BufferBase** result, |
| 220 | const BufferDescriptor* descriptor) { |
| 221 | DAWN_TRY(ValidateBufferDescriptor(this, descriptor)); |
| 222 | DAWN_TRY_ASSIGN(*result, CreateBufferImpl(descriptor)); |
| 223 | return {}; |
| 224 | } |
| 225 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 226 | MaybeError DeviceBase::CreatePipelineLayoutInternal( |
| 227 | PipelineLayoutBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 228 | const PipelineLayoutDescriptor* descriptor) { |
Corentin Wallez | 33ca496 | 2018-07-18 11:45:17 +0200 | [diff] [blame] | 229 | DAWN_TRY(ValidatePipelineLayoutDescriptor(this, descriptor)); |
| 230 | DAWN_TRY_ASSIGN(*result, CreatePipelineLayoutImpl(descriptor)); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 231 | return {}; |
| 232 | } |
| 233 | |
| 234 | MaybeError DeviceBase::CreateQueueInternal(QueueBase** result) { |
Corentin Wallez | 33ca496 | 2018-07-18 11:45:17 +0200 | [diff] [blame] | 235 | DAWN_TRY_ASSIGN(*result, CreateQueueImpl()); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 236 | return {}; |
| 237 | } |
| 238 | |
| 239 | MaybeError DeviceBase::CreateSamplerInternal(SamplerBase** result, |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 240 | const SamplerDescriptor* descriptor) { |
Corentin Wallez | 33ca496 | 2018-07-18 11:45:17 +0200 | [diff] [blame] | 241 | DAWN_TRY(ValidateSamplerDescriptor(this, descriptor)); |
| 242 | DAWN_TRY_ASSIGN(*result, CreateSamplerImpl(descriptor)); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 243 | return {}; |
| 244 | } |
| 245 | |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 246 | MaybeError DeviceBase::CreateShaderModuleInternal(ShaderModuleBase** result, |
| 247 | const ShaderModuleDescriptor* descriptor) { |
| 248 | DAWN_TRY(ValidateShaderModuleDescriptor(this, descriptor)); |
| 249 | DAWN_TRY_ASSIGN(*result, CreateShaderModuleImpl(descriptor)); |
| 250 | return {}; |
| 251 | } |
| 252 | |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 253 | MaybeError DeviceBase::CreateTextureInternal(TextureBase** result, |
| 254 | const TextureDescriptor* descriptor) { |
| 255 | DAWN_TRY(ValidateTextureDescriptor(this, descriptor)); |
| 256 | DAWN_TRY_ASSIGN(*result, CreateTextureImpl(descriptor)); |
| 257 | return {}; |
| 258 | } |
| 259 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 260 | // Other implementation details |
| 261 | |
| 262 | void DeviceBase::ConsumeError(ErrorData* error) { |
| 263 | ASSERT(error != nullptr); |
| 264 | HandleError(error->GetMessage().c_str()); |
| 265 | delete error; |
| 266 | } |
| 267 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 268 | } // namespace dawn_native |