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 | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 17 | #include "common/Log.h" |
Corentin Wallez | ec18f96 | 2019-01-10 10:50:54 +0000 | [diff] [blame] | 18 | #include "dawn_native/Adapter.h" |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 19 | #include "dawn_native/AttachmentState.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 20 | #include "dawn_native/BindGroup.h" |
| 21 | #include "dawn_native/BindGroupLayout.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 22 | #include "dawn_native/Buffer.h" |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 23 | #include "dawn_native/CallbackTaskManager.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 24 | #include "dawn_native/CommandBuffer.h" |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 25 | #include "dawn_native/CommandEncoder.h" |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 26 | #include "dawn_native/CompilationMessages.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 27 | #include "dawn_native/ComputePipeline.h" |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 28 | #include "dawn_native/CreatePipelineAsyncTask.h" |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 29 | #include "dawn_native/DynamicUploader.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 30 | #include "dawn_native/ErrorData.h" |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 31 | #include "dawn_native/ErrorScope.h" |
Brandon Jones | 0e92e9b | 2021-04-01 20:46:42 +0000 | [diff] [blame] | 32 | #include "dawn_native/ExternalTexture.h" |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 33 | #include "dawn_native/Instance.h" |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 34 | #include "dawn_native/InternalPipelineStore.h" |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 35 | #include "dawn_native/PersistentCache.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 36 | #include "dawn_native/PipelineLayout.h" |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 37 | #include "dawn_native/QuerySet.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 38 | #include "dawn_native/Queue.h" |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 39 | #include "dawn_native/RenderBundleEncoder.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 40 | #include "dawn_native/RenderPipeline.h" |
| 41 | #include "dawn_native/Sampler.h" |
| 42 | #include "dawn_native/ShaderModule.h" |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 43 | #include "dawn_native/Surface.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 44 | #include "dawn_native/SwapChain.h" |
| 45 | #include "dawn_native/Texture.h" |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 46 | #include "dawn_native/ValidationUtils_autogen.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 47 | |
| 48 | #include <unordered_set> |
| 49 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 50 | namespace dawn_native { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 51 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 52 | // DeviceBase sub-structures |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 53 | |
| 54 | // The caches are unordered_sets of pointers with special hash and compare functions |
| 55 | // to compare the value of the objects, instead of the pointers. |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 56 | template <typename Object> |
| 57 | using ContentLessObjectCache = |
| 58 | std::unordered_set<Object*, typename Object::HashFunc, typename Object::EqualityFunc>; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 59 | |
| 60 | struct DeviceBase::Caches { |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 61 | ~Caches() { |
| 62 | ASSERT(attachmentStates.empty()); |
| 63 | ASSERT(bindGroupLayouts.empty()); |
| 64 | ASSERT(computePipelines.empty()); |
| 65 | ASSERT(pipelineLayouts.empty()); |
| 66 | ASSERT(renderPipelines.empty()); |
| 67 | ASSERT(samplers.empty()); |
| 68 | ASSERT(shaderModules.empty()); |
| 69 | } |
| 70 | |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 71 | ContentLessObjectCache<AttachmentStateBlueprint> attachmentStates; |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 72 | ContentLessObjectCache<BindGroupLayoutBase> bindGroupLayouts; |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 73 | ContentLessObjectCache<ComputePipelineBase> computePipelines; |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 74 | ContentLessObjectCache<PipelineLayoutBase> pipelineLayouts; |
Corentin Wallez | 94274b6 | 2019-05-02 15:30:56 +0000 | [diff] [blame] | 75 | ContentLessObjectCache<RenderPipelineBase> renderPipelines; |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 76 | ContentLessObjectCache<SamplerBase> samplers; |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 77 | ContentLessObjectCache<ShaderModuleBase> shaderModules; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 78 | }; |
| 79 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 80 | struct DeviceBase::DeprecationWarnings { |
Corentin Wallez | dbec2fa | 2020-05-04 16:04:40 +0000 | [diff] [blame] | 81 | std::unordered_set<std::string> emitted; |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 82 | size_t count = 0; |
| 83 | }; |
| 84 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 85 | // DeviceBase |
| 86 | |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 87 | DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor) |
Austin Eng | f6ef753 | 2021-01-13 17:54:37 +0000 | [diff] [blame] | 88 | : mInstance(adapter->GetInstance()), mAdapter(adapter) { |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 89 | if (descriptor != nullptr) { |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 90 | ApplyToggleOverrides(descriptor); |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 91 | ApplyExtensions(descriptor); |
| 92 | } |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 93 | |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 94 | mFormatTable = BuildFormatTable(this); |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 95 | SetDefaultToggles(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Austin Eng | f6ef753 | 2021-01-13 17:54:37 +0000 | [diff] [blame] | 98 | DeviceBase::~DeviceBase() = default; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 99 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 100 | MaybeError DeviceBase::Initialize(QueueBase* defaultQueue) { |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 101 | mQueue = AcquireRef(defaultQueue); |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 102 | |
Austin Eng | f2bc3b3 | 2020-11-16 23:32:36 +0000 | [diff] [blame] | 103 | #if defined(DAWN_ENABLE_ASSERTS) |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 104 | mUncapturedErrorCallback = [](WGPUErrorType, char const*, void*) { |
| 105 | static bool calledOnce = false; |
| 106 | if (!calledOnce) { |
| 107 | calledOnce = true; |
| 108 | dawn::WarningLog() << "No Dawn device uncaptured error callback was set. This is " |
| 109 | "probably not intended. If you really want to ignore errors " |
| 110 | "and suppress this message, set the callback to null."; |
| 111 | } |
| 112 | }; |
Austin Eng | f2bc3b3 | 2020-11-16 23:32:36 +0000 | [diff] [blame] | 113 | |
| 114 | mDeviceLostCallback = [](char const*, void*) { |
| 115 | static bool calledOnce = false; |
| 116 | if (!calledOnce) { |
| 117 | calledOnce = true; |
| 118 | dawn::WarningLog() << "No Dawn device lost callback was set. This is probably not " |
| 119 | "intended. If you really want to ignore device lost " |
| 120 | "and suppress this message, set the callback to null."; |
| 121 | } |
| 122 | }; |
| 123 | #endif // DAWN_ENABLE_ASSERTS |
| 124 | |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 125 | mCaches = std::make_unique<DeviceBase::Caches>(); |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 126 | mErrorScopeStack = std::make_unique<ErrorScopeStack>(); |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 127 | mDynamicUploader = std::make_unique<DynamicUploader>(this); |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 128 | mCallbackTaskManager = std::make_unique<CallbackTaskManager>(); |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 129 | mDeprecationWarnings = std::make_unique<DeprecationWarnings>(); |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 130 | mInternalPipelineStore = std::make_unique<InternalPipelineStore>(); |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 131 | mPersistentCache = std::make_unique<PersistentCache>(this); |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 132 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 133 | // Starting from now the backend can start doing reentrant calls so the device is marked as |
| 134 | // alive. |
| 135 | mState = State::Alive; |
| 136 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 137 | DAWN_TRY_ASSIGN(mEmptyBindGroupLayout, CreateEmptyBindGroupLayout()); |
| 138 | |
Corentin Wallez | 09ee5eb | 2020-04-07 15:10:17 +0000 | [diff] [blame] | 139 | return {}; |
| 140 | } |
| 141 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 142 | void DeviceBase::ShutDownBase() { |
Austin Eng | a75b230 | 2021-02-04 20:38:02 +0000 | [diff] [blame] | 143 | // Skip handling device facilities if they haven't even been created (or failed doing so) |
| 144 | if (mState != State::BeingCreated) { |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 145 | // Call all the callbacks immediately as the device is about to shut down. |
| 146 | auto callbackTasks = mCallbackTaskManager->AcquireCallbackTasks(); |
| 147 | for (std::unique_ptr<CallbackTask>& callbackTask : callbackTasks) { |
| 148 | callbackTask->HandleShutDown(); |
| 149 | } |
Austin Eng | a75b230 | 2021-02-04 20:38:02 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 152 | // Disconnect the device, depending on which state we are currently in. |
| 153 | switch (mState) { |
| 154 | case State::BeingCreated: |
| 155 | // The GPU timeline was never started so we don't have to wait. |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 156 | break; |
| 157 | |
| 158 | case State::Alive: |
| 159 | // Alive is the only state which can have GPU work happening. Wait for all of it to |
| 160 | // complete before proceeding with destruction. |
Natasha Lee | 3fe84b5 | 2020-06-16 17:53:38 +0000 | [diff] [blame] | 161 | // Ignore errors so that we can continue with destruction |
| 162 | IgnoreErrors(WaitForIdleForDestruction()); |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 163 | AssumeCommandsComplete(); |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 164 | break; |
| 165 | |
| 166 | case State::BeingDisconnected: |
| 167 | // Getting disconnected is a transient state happening in a single API call so there |
| 168 | // is always an external reference keeping the Device alive, which means the |
| 169 | // destructor cannot run while BeingDisconnected. |
| 170 | UNREACHABLE(); |
| 171 | break; |
| 172 | |
| 173 | case State::Disconnected: |
| 174 | break; |
Natasha Lee | 2c8a17e | 2019-12-16 23:36:16 +0000 | [diff] [blame] | 175 | } |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 176 | ASSERT(mCompletedSerial == mLastSubmittedSerial); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 177 | ASSERT(mFutureSerial <= mCompletedSerial); |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 178 | |
Corentin Wallez | e3f44e3 | 2020-04-15 10:07:25 +0000 | [diff] [blame] | 179 | if (mState != State::BeingCreated) { |
Austin Eng | a75b230 | 2021-02-04 20:38:02 +0000 | [diff] [blame] | 180 | // The GPU timeline is finished. |
| 181 | // Tick the queue-related tasks since they should be complete. This must be done before |
| 182 | // ShutDownImpl() it may relinquish resources that will be freed by backends in the |
| 183 | // ShutDownImpl() call. |
Corentin Wallez | e91975c | 2021-03-29 15:40:55 +0000 | [diff] [blame] | 184 | mQueue->Tick(GetCompletedCommandSerial()); |
Austin Eng | a75b230 | 2021-02-04 20:38:02 +0000 | [diff] [blame] | 185 | // Call TickImpl once last time to clean up resources |
Natasha Lee | 3fe84b5 | 2020-06-16 17:53:38 +0000 | [diff] [blame] | 186 | // Ignore errors so that we can continue with destruction |
| 187 | IgnoreErrors(TickImpl()); |
Corentin Wallez | e3f44e3 | 2020-04-15 10:07:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // At this point GPU operations are always finished, so we are in the disconnected state. |
| 191 | mState = State::Disconnected; |
| 192 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 193 | mDynamicUploader = nullptr; |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 194 | mCallbackTaskManager = nullptr; |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 195 | mPersistentCache = nullptr; |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 196 | |
Tomek Ponitka | bf27bd7 | 2020-06-29 11:13:43 +0000 | [diff] [blame] | 197 | mEmptyBindGroupLayout = nullptr; |
| 198 | |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 199 | mInternalPipelineStore = nullptr; |
| 200 | |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 201 | AssumeCommandsComplete(); |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 202 | // Tell the backend that it can free all the objects now that the GPU timeline is empty. |
| 203 | ShutDownImpl(); |
| 204 | |
| 205 | mCaches = nullptr; |
Natasha Lee | 2c8a17e | 2019-12-16 23:36:16 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 208 | void DeviceBase::HandleError(InternalErrorType type, const char* message) { |
Austin Eng | 8ef94f1 | 2021-01-13 17:53:29 +0000 | [diff] [blame] | 209 | if (type == InternalErrorType::DeviceLost) { |
| 210 | // A real device lost happened. Set the state to disconnected as the device cannot be |
| 211 | // used. |
| 212 | mState = State::Disconnected; |
| 213 | } else if (type == InternalErrorType::Internal) { |
| 214 | // If we receive an internal error, assume the backend can't recover and proceed with |
| 215 | // device destruction. We first wait for all previous commands to be completed so that |
| 216 | // backend objects can be freed immediately, before handling the loss. |
| 217 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 218 | // Move away from the Alive state so that the application cannot use this device |
| 219 | // anymore. |
| 220 | // TODO(cwallez@chromium.org): Do we need atomics for this to become visible to other |
| 221 | // threads in a multithreaded scenario? |
| 222 | mState = State::BeingDisconnected; |
| 223 | |
Natasha Lee | 3fe84b5 | 2020-06-16 17:53:38 +0000 | [diff] [blame] | 224 | // Ignore errors so that we can continue with destruction |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 225 | // Assume all commands are complete after WaitForIdleForDestruction (because they were) |
Natasha Lee | 3fe84b5 | 2020-06-16 17:53:38 +0000 | [diff] [blame] | 226 | IgnoreErrors(WaitForIdleForDestruction()); |
| 227 | IgnoreErrors(TickImpl()); |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 228 | AssumeCommandsComplete(); |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 229 | ASSERT(mFutureSerial <= mCompletedSerial); |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 230 | mState = State::Disconnected; |
| 231 | |
| 232 | // Now everything is as if the device was lost. |
| 233 | type = InternalErrorType::DeviceLost; |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 234 | } |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 235 | |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 236 | if (type == InternalErrorType::DeviceLost) { |
| 237 | // The device was lost, call the application callback. |
| 238 | if (mDeviceLostCallback != nullptr) { |
| 239 | mDeviceLostCallback(message, mDeviceLostUserdata); |
| 240 | mDeviceLostCallback = nullptr; |
| 241 | } |
| 242 | |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 243 | mQueue->HandleDeviceLoss(); |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 244 | auto callbackTasks = mCallbackTaskManager->AcquireCallbackTasks(); |
| 245 | for (std::unique_ptr<CallbackTask>& callbackTask : callbackTasks) { |
| 246 | callbackTask->HandleDeviceLoss(); |
| 247 | } |
Corentin Wallez | c1d3a66 | 2021-01-27 15:54:12 +0000 | [diff] [blame] | 248 | |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 249 | // Still forward device loss errors to the error scopes so they all reject. |
| 250 | mErrorScopeStack->HandleError(ToWGPUErrorType(type), message); |
| 251 | } else { |
| 252 | // Pass the error to the error scope stack and call the uncaptured error callback |
| 253 | // if it isn't handled. DeviceLost is not handled here because it should be |
| 254 | // handled by the lost callback. |
| 255 | bool captured = mErrorScopeStack->HandleError(ToWGPUErrorType(type), message); |
| 256 | if (!captured && mUncapturedErrorCallback != nullptr) { |
| 257 | mUncapturedErrorCallback(static_cast<WGPUErrorType>(ToWGPUErrorType(type)), message, |
| 258 | mUncapturedErrorUserdata); |
| 259 | } |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 260 | } |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 263 | void DeviceBase::APIInjectError(wgpu::ErrorType type, const char* message) { |
Austin Eng | e06f01b | 2019-09-30 22:50:59 +0000 | [diff] [blame] | 264 | if (ConsumedError(ValidateErrorType(type))) { |
| 265 | return; |
| 266 | } |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 267 | |
| 268 | // This method should only be used to make error scope reject. For DeviceLost there is the |
| 269 | // LoseForTesting function that can be used instead. |
| 270 | if (type != wgpu::ErrorType::Validation && type != wgpu::ErrorType::OutOfMemory) { |
| 271 | HandleError(InternalErrorType::Validation, |
| 272 | "Invalid injected error, must be Validation or OutOfMemory"); |
Austin Eng | 3fd022e | 2019-10-03 14:40:09 +0000 | [diff] [blame] | 273 | return; |
| 274 | } |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 275 | |
| 276 | HandleError(FromWGPUErrorType(type), message); |
Austin Eng | e06f01b | 2019-09-30 22:50:59 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Rafael Cintron | 69c68d0 | 2020-01-10 17:58:28 +0000 | [diff] [blame] | 279 | void DeviceBase::ConsumeError(std::unique_ptr<ErrorData> error) { |
Austin Eng | b11bd2d | 2019-09-18 21:03:41 +0000 | [diff] [blame] | 280 | ASSERT(error != nullptr); |
Austin Eng | 75ef596 | 2020-05-13 23:10:36 +0000 | [diff] [blame] | 281 | std::ostringstream ss; |
| 282 | ss << error->GetMessage(); |
| 283 | for (const auto& callsite : error->GetBacktrace()) { |
| 284 | ss << "\n at " << callsite.function << " (" << callsite.file << ":" << callsite.line |
| 285 | << ")"; |
| 286 | } |
| 287 | HandleError(error->GetType(), ss.str().c_str()); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 290 | void DeviceBase::APISetUncapturedErrorCallback(wgpu::ErrorCallback callback, void* userdata) { |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 291 | mUncapturedErrorCallback = callback; |
| 292 | mUncapturedErrorUserdata = userdata; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 295 | void DeviceBase::APISetDeviceLostCallback(wgpu::DeviceLostCallback callback, void* userdata) { |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 296 | mDeviceLostCallback = callback; |
| 297 | mDeviceLostUserdata = userdata; |
| 298 | } |
| 299 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 300 | void DeviceBase::APIPushErrorScope(wgpu::ErrorFilter filter) { |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 301 | if (ConsumedError(ValidateErrorFilter(filter))) { |
| 302 | return; |
| 303 | } |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 304 | mErrorScopeStack->Push(filter); |
Austin Eng | 45238d7 | 2019-09-04 22:54:03 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 307 | bool DeviceBase::APIPopErrorScope(wgpu::ErrorCallback callback, void* userdata) { |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 308 | if (mErrorScopeStack->Empty()) { |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
Austin Eng | ef9e441 | 2021-02-19 18:17:22 +0000 | [diff] [blame] | 311 | ErrorScope scope = mErrorScopeStack->Pop(); |
| 312 | if (callback != nullptr) { |
| 313 | callback(static_cast<WGPUErrorType>(scope.GetErrorType()), scope.GetErrorMessage(), |
| 314 | userdata); |
| 315 | } |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 316 | |
| 317 | return true; |
Austin Eng | 45238d7 | 2019-09-04 22:54:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Bryan Bernhart | 41b3f9c | 2020-11-20 20:38:37 +0000 | [diff] [blame] | 320 | PersistentCache* DeviceBase::GetPersistentCache() { |
| 321 | ASSERT(mPersistentCache.get() != nullptr); |
| 322 | return mPersistentCache.get(); |
| 323 | } |
| 324 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 325 | MaybeError DeviceBase::ValidateObject(const ObjectBase* object) const { |
Jiawei Shao | 91fbfc3 | 2019-10-15 00:17:35 +0000 | [diff] [blame] | 326 | ASSERT(object != nullptr); |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 327 | if (DAWN_UNLIKELY(object->GetDevice() != this)) { |
| 328 | return DAWN_VALIDATION_ERROR("Object from a different device."); |
| 329 | } |
| 330 | if (DAWN_UNLIKELY(object->IsError())) { |
| 331 | return DAWN_VALIDATION_ERROR("Object is an error."); |
| 332 | } |
| 333 | return {}; |
| 334 | } |
| 335 | |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 336 | MaybeError DeviceBase::ValidateIsAlive() const { |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 337 | if (DAWN_LIKELY(mState == State::Alive)) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 338 | return {}; |
| 339 | } |
Austin Eng | 8ef94f1 | 2021-01-13 17:53:29 +0000 | [diff] [blame] | 340 | return DAWN_VALIDATION_ERROR("Device is lost"); |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 343 | void DeviceBase::APILoseForTesting() { |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 344 | if (mState != State::Alive) { |
Natasha Lee | 31eacb9 | 2020-03-17 15:03:18 +0000 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
Corentin Wallez | a0afd31 | 2020-04-01 12:07:43 +0000 | [diff] [blame] | 348 | HandleError(InternalErrorType::Internal, "Device lost for testing"); |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 351 | DeviceBase::State DeviceBase::GetState() const { |
| 352 | return mState; |
| 353 | } |
| 354 | |
Natasha Lee | 74f5054 | 2020-01-28 22:18:58 +0000 | [diff] [blame] | 355 | bool DeviceBase::IsLost() const { |
Corentin Wallez | 73ea1f1 | 2020-04-07 16:19:47 +0000 | [diff] [blame] | 356 | ASSERT(mState != State::BeingCreated); |
| 357 | return mState != State::Alive; |
Natasha Lee | 74f5054 | 2020-01-28 22:18:58 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Corentin Wallez | d77fd5f | 2019-01-30 16:07:48 +0000 | [diff] [blame] | 360 | AdapterBase* DeviceBase::GetAdapter() const { |
| 361 | return mAdapter; |
| 362 | } |
| 363 | |
Austin Eng | d4ce736 | 2019-08-13 19:00:34 +0000 | [diff] [blame] | 364 | dawn_platform::Platform* DeviceBase::GetPlatform() const { |
| 365 | return GetAdapter()->GetInstance()->GetPlatform(); |
| 366 | } |
| 367 | |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 368 | ExecutionSerial DeviceBase::GetCompletedCommandSerial() const { |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 369 | return mCompletedSerial; |
| 370 | } |
| 371 | |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 372 | ExecutionSerial DeviceBase::GetLastSubmittedCommandSerial() const { |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 373 | return mLastSubmittedSerial; |
| 374 | } |
| 375 | |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 376 | ExecutionSerial DeviceBase::GetFutureSerial() const { |
| 377 | return mFutureSerial; |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Yan, Shaobo | db8766b | 2020-11-04 02:30:16 +0000 | [diff] [blame] | 380 | InternalPipelineStore* DeviceBase::GetInternalPipelineStore() { |
| 381 | return mInternalPipelineStore.get(); |
| 382 | } |
| 383 | |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 384 | void DeviceBase::IncrementLastSubmittedCommandSerial() { |
| 385 | mLastSubmittedSerial++; |
| 386 | } |
| 387 | |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 388 | void DeviceBase::AssumeCommandsComplete() { |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 389 | ExecutionSerial maxSerial = |
| 390 | ExecutionSerial(std::max(mLastSubmittedSerial + ExecutionSerial(1), mFutureSerial)); |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 391 | mLastSubmittedSerial = maxSerial; |
| 392 | mCompletedSerial = maxSerial; |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 395 | bool DeviceBase::IsDeviceIdle() { |
| 396 | ExecutionSerial maxSerial = std::max(mLastSubmittedSerial, mFutureSerial); |
| 397 | if (mCompletedSerial == maxSerial) { |
| 398 | return true; |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 403 | ExecutionSerial DeviceBase::GetPendingCommandSerial() const { |
| 404 | return mLastSubmittedSerial + ExecutionSerial(1); |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 407 | void DeviceBase::AddFutureSerial(ExecutionSerial serial) { |
| 408 | if (serial > mFutureSerial) { |
| 409 | mFutureSerial = serial; |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Corentin Wallez | 6870e6d | 2021-04-07 18:09:21 +0000 | [diff] [blame] | 413 | MaybeError DeviceBase::CheckPassedSerials() { |
| 414 | ExecutionSerial completedSerial; |
| 415 | DAWN_TRY_ASSIGN(completedSerial, CheckAndUpdateCompletedSerials()); |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 416 | |
| 417 | ASSERT(completedSerial <= mLastSubmittedSerial); |
| 418 | // completedSerial should not be less than mCompletedSerial unless it is 0. |
| 419 | // It can be 0 when there's no fences to check. |
Corentin Wallez | 62139fc | 2020-09-28 19:35:14 +0000 | [diff] [blame] | 420 | ASSERT(completedSerial >= mCompletedSerial || completedSerial == ExecutionSerial(0)); |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 421 | |
| 422 | if (completedSerial > mCompletedSerial) { |
| 423 | mCompletedSerial = completedSerial; |
| 424 | } |
Corentin Wallez | 6870e6d | 2021-04-07 18:09:21 +0000 | [diff] [blame] | 425 | |
| 426 | return {}; |
Natasha Lee | 351c95a | 2020-05-07 21:52:54 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 429 | ResultOrError<const Format*> DeviceBase::GetInternalFormat(wgpu::TextureFormat format) const { |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 430 | size_t index = ComputeFormatIndex(format); |
| 431 | if (index >= mFormatTable.size()) { |
| 432 | return DAWN_VALIDATION_ERROR("Unknown texture format"); |
| 433 | } |
| 434 | |
| 435 | const Format* internalFormat = &mFormatTable[index]; |
| 436 | if (!internalFormat->isSupported) { |
| 437 | return DAWN_VALIDATION_ERROR("Unsupported texture format"); |
| 438 | } |
| 439 | |
| 440 | return internalFormat; |
| 441 | } |
| 442 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 443 | const Format& DeviceBase::GetValidInternalFormat(wgpu::TextureFormat format) const { |
Corentin Wallez | 1546bfb | 2019-07-18 09:25:04 +0000 | [diff] [blame] | 444 | size_t index = ComputeFormatIndex(format); |
| 445 | ASSERT(index < mFormatTable.size()); |
| 446 | ASSERT(mFormatTable[index].isSupported); |
| 447 | return mFormatTable[index]; |
| 448 | } |
| 449 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 450 | ResultOrError<Ref<BindGroupLayoutBase>> DeviceBase::GetOrCreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 451 | const BindGroupLayoutDescriptor* descriptor) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 452 | BindGroupLayoutBase blueprint(this, descriptor); |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 453 | |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 454 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 455 | blueprint.SetContentHash(blueprintHash); |
| 456 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 457 | Ref<BindGroupLayoutBase> result; |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 458 | auto iter = mCaches->bindGroupLayouts.find(&blueprint); |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 459 | if (iter != mCaches->bindGroupLayouts.end()) { |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 460 | result = *iter; |
| 461 | } else { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 462 | DAWN_TRY_ASSIGN(result, CreateBindGroupLayoutImpl(descriptor)); |
| 463 | result->SetIsCachedReference(); |
| 464 | result->SetContentHash(blueprintHash); |
| 465 | mCaches->bindGroupLayouts.insert(result.Get()); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 466 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 467 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 468 | return std::move(result); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void DeviceBase::UncacheBindGroupLayout(BindGroupLayoutBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 472 | ASSERT(obj->IsCachedReference()); |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 473 | size_t removedCount = mCaches->bindGroupLayouts.erase(obj); |
| 474 | ASSERT(removedCount == 1); |
| 475 | } |
| 476 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 477 | // Private function used at initialization |
| 478 | ResultOrError<Ref<BindGroupLayoutBase>> DeviceBase::CreateEmptyBindGroupLayout() { |
| 479 | BindGroupLayoutDescriptor desc = {}; |
| 480 | desc.entryCount = 0; |
| 481 | desc.entries = nullptr; |
Tomek Ponitka | bf27bd7 | 2020-06-29 11:13:43 +0000 | [diff] [blame] | 482 | |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 483 | return GetOrCreateBindGroupLayout(&desc); |
| 484 | } |
| 485 | |
| 486 | BindGroupLayoutBase* DeviceBase::GetEmptyBindGroupLayout() { |
Corentin Wallez | 55f251d | 2020-11-18 09:10:22 +0000 | [diff] [blame] | 487 | ASSERT(mEmptyBindGroupLayout != nullptr); |
Tomek Ponitka | 5fb974c | 2020-07-01 13:09:46 +0000 | [diff] [blame] | 488 | return mEmptyBindGroupLayout.Get(); |
Tomek Ponitka | bf27bd7 | 2020-06-29 11:13:43 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 491 | std::pair<Ref<ComputePipelineBase>, size_t> DeviceBase::GetCachedComputePipeline( |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 492 | const ComputePipelineDescriptor* descriptor) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 493 | ComputePipelineBase blueprint(this, descriptor); |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 494 | |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 495 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 496 | blueprint.SetContentHash(blueprintHash); |
| 497 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 498 | Ref<ComputePipelineBase> result; |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 499 | auto iter = mCaches->computePipelines.find(&blueprint); |
| 500 | if (iter != mCaches->computePipelines.end()) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 501 | result = *iter; |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 504 | return std::make_pair(result, blueprintHash); |
| 505 | } |
| 506 | |
| 507 | Ref<ComputePipelineBase> DeviceBase::AddOrGetCachedPipeline( |
| 508 | Ref<ComputePipelineBase> computePipeline, |
| 509 | size_t blueprintHash) { |
| 510 | computePipeline->SetContentHash(blueprintHash); |
| 511 | auto insertion = mCaches->computePipelines.insert(computePipeline.Get()); |
| 512 | if (insertion.second) { |
| 513 | computePipeline->SetIsCachedReference(); |
| 514 | return computePipeline; |
| 515 | } else { |
| 516 | return *(insertion.first); |
| 517 | } |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | void DeviceBase::UncacheComputePipeline(ComputePipelineBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 521 | ASSERT(obj->IsCachedReference()); |
Corentin Wallez | 1152bba | 2019-05-01 13:48:47 +0000 | [diff] [blame] | 522 | size_t removedCount = mCaches->computePipelines.erase(obj); |
| 523 | ASSERT(removedCount == 1); |
| 524 | } |
| 525 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 526 | ResultOrError<Ref<PipelineLayoutBase>> DeviceBase::GetOrCreatePipelineLayout( |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 527 | const PipelineLayoutDescriptor* descriptor) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 528 | PipelineLayoutBase blueprint(this, descriptor); |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 529 | |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 530 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 531 | blueprint.SetContentHash(blueprintHash); |
| 532 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 533 | Ref<PipelineLayoutBase> result; |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 534 | auto iter = mCaches->pipelineLayouts.find(&blueprint); |
| 535 | if (iter != mCaches->pipelineLayouts.end()) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 536 | result = *iter; |
| 537 | } else { |
| 538 | DAWN_TRY_ASSIGN(result, CreatePipelineLayoutImpl(descriptor)); |
| 539 | result->SetIsCachedReference(); |
| 540 | result->SetContentHash(blueprintHash); |
| 541 | mCaches->pipelineLayouts.insert(result.Get()); |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 544 | return std::move(result); |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void DeviceBase::UncachePipelineLayout(PipelineLayoutBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 548 | ASSERT(obj->IsCachedReference()); |
Corentin Wallez | 0ee9859 | 2019-05-01 12:57:27 +0000 | [diff] [blame] | 549 | size_t removedCount = mCaches->pipelineLayouts.erase(obj); |
| 550 | ASSERT(removedCount == 1); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 553 | ResultOrError<Ref<RenderPipelineBase>> DeviceBase::GetOrCreateRenderPipeline( |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 554 | const RenderPipelineDescriptor* descriptor) { |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 555 | RenderPipelineBase blueprint(this, descriptor); |
| 556 | |
| 557 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 558 | blueprint.SetContentHash(blueprintHash); |
| 559 | |
| 560 | Ref<RenderPipelineBase> result; |
| 561 | auto iter = mCaches->renderPipelines.find(&blueprint); |
| 562 | if (iter != mCaches->renderPipelines.end()) { |
| 563 | result = *iter; |
| 564 | } else { |
Brandon Jones | f759264 | 2021-04-02 19:42:28 +0000 | [diff] [blame] | 565 | DAWN_TRY_ASSIGN(result, CreateRenderPipelineImpl(descriptor)); |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 566 | result->SetIsCachedReference(); |
| 567 | result->SetContentHash(blueprintHash); |
| 568 | mCaches->renderPipelines.insert(result.Get()); |
| 569 | } |
| 570 | |
| 571 | return std::move(result); |
| 572 | } |
| 573 | |
Corentin Wallez | 94274b6 | 2019-05-02 15:30:56 +0000 | [diff] [blame] | 574 | void DeviceBase::UncacheRenderPipeline(RenderPipelineBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 575 | ASSERT(obj->IsCachedReference()); |
Corentin Wallez | 94274b6 | 2019-05-02 15:30:56 +0000 | [diff] [blame] | 576 | size_t removedCount = mCaches->renderPipelines.erase(obj); |
| 577 | ASSERT(removedCount == 1); |
| 578 | } |
| 579 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 580 | ResultOrError<Ref<SamplerBase>> DeviceBase::GetOrCreateSampler( |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 581 | const SamplerDescriptor* descriptor) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 582 | SamplerBase blueprint(this, descriptor); |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 583 | |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 584 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 585 | blueprint.SetContentHash(blueprintHash); |
| 586 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 587 | Ref<SamplerBase> result; |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 588 | auto iter = mCaches->samplers.find(&blueprint); |
| 589 | if (iter != mCaches->samplers.end()) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 590 | result = *iter; |
| 591 | } else { |
| 592 | DAWN_TRY_ASSIGN(result, CreateSamplerImpl(descriptor)); |
| 593 | result->SetIsCachedReference(); |
| 594 | result->SetContentHash(blueprintHash); |
| 595 | mCaches->samplers.insert(result.Get()); |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 598 | return std::move(result); |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void DeviceBase::UncacheSampler(SamplerBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 602 | ASSERT(obj->IsCachedReference()); |
Idan Raiter | 2bc3169 | 2019-05-20 18:16:34 +0000 | [diff] [blame] | 603 | size_t removedCount = mCaches->samplers.erase(obj); |
| 604 | ASSERT(removedCount == 1); |
| 605 | } |
| 606 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 607 | ResultOrError<Ref<ShaderModuleBase>> DeviceBase::GetOrCreateShaderModule( |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 608 | const ShaderModuleDescriptor* descriptor, |
| 609 | ShaderModuleParseResult* parseResult) { |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 610 | ASSERT(parseResult != nullptr); |
| 611 | |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 612 | ShaderModuleBase blueprint(this, descriptor); |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 613 | |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 614 | const size_t blueprintHash = blueprint.ComputeContentHash(); |
| 615 | blueprint.SetContentHash(blueprintHash); |
| 616 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 617 | Ref<ShaderModuleBase> result; |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 618 | auto iter = mCaches->shaderModules.find(&blueprint); |
| 619 | if (iter != mCaches->shaderModules.end()) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 620 | result = *iter; |
| 621 | } else { |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 622 | if (!parseResult->HasParsedShader()) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 623 | // We skip the parse on creation if validation isn't enabled which let's us quickly |
| 624 | // lookup in the cache without validating and parsing. We need the parsed module |
| 625 | // now, so call validate. Most of |ValidateShaderModuleDescriptor| is parsing, but |
| 626 | // we can consider splitting it if additional validation is added. |
| 627 | ASSERT(!IsValidationEnabled()); |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 628 | DAWN_TRY(ValidateShaderModuleDescriptor(this, descriptor, parseResult)); |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 629 | } |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 630 | DAWN_TRY_ASSIGN(result, CreateShaderModuleImpl(descriptor, parseResult)); |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 631 | result->SetIsCachedReference(); |
| 632 | result->SetContentHash(blueprintHash); |
| 633 | mCaches->shaderModules.insert(result.Get()); |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 636 | return std::move(result); |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void DeviceBase::UncacheShaderModule(ShaderModuleBase* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 640 | ASSERT(obj->IsCachedReference()); |
Corentin Wallez | c535198 | 2019-05-01 13:27:07 +0000 | [diff] [blame] | 641 | size_t removedCount = mCaches->shaderModules.erase(obj); |
| 642 | ASSERT(removedCount == 1); |
| 643 | } |
| 644 | |
Austin Eng | bb10a91 | 2019-08-07 21:47:24 +0000 | [diff] [blame] | 645 | Ref<AttachmentState> DeviceBase::GetOrCreateAttachmentState( |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 646 | AttachmentStateBlueprint* blueprint) { |
| 647 | auto iter = mCaches->attachmentStates.find(blueprint); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 648 | if (iter != mCaches->attachmentStates.end()) { |
Austin Eng | bb10a91 | 2019-08-07 21:47:24 +0000 | [diff] [blame] | 649 | return static_cast<AttachmentState*>(*iter); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Austin Eng | f35dcfe | 2019-09-10 23:19:11 +0000 | [diff] [blame] | 652 | Ref<AttachmentState> attachmentState = AcquireRef(new AttachmentState(this, *blueprint)); |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 653 | attachmentState->SetIsCachedReference(); |
Bryan Bernhart | 24bf7a4 | 2020-12-03 18:42:13 +0000 | [diff] [blame] | 654 | attachmentState->SetContentHash(attachmentState->ComputeContentHash()); |
Austin Eng | bb10a91 | 2019-08-07 21:47:24 +0000 | [diff] [blame] | 655 | mCaches->attachmentStates.insert(attachmentState.Get()); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 656 | return attachmentState; |
| 657 | } |
| 658 | |
Austin Eng | bb10a91 | 2019-08-07 21:47:24 +0000 | [diff] [blame] | 659 | Ref<AttachmentState> DeviceBase::GetOrCreateAttachmentState( |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 660 | const RenderBundleEncoderDescriptor* descriptor) { |
| 661 | AttachmentStateBlueprint blueprint(descriptor); |
| 662 | return GetOrCreateAttachmentState(&blueprint); |
| 663 | } |
| 664 | |
| 665 | Ref<AttachmentState> DeviceBase::GetOrCreateAttachmentState( |
| 666 | const RenderPipelineDescriptor* descriptor) { |
| 667 | AttachmentStateBlueprint blueprint(descriptor); |
| 668 | return GetOrCreateAttachmentState(&blueprint); |
| 669 | } |
| 670 | |
| 671 | Ref<AttachmentState> DeviceBase::GetOrCreateAttachmentState( |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 672 | const RenderPassDescriptor* descriptor) { |
| 673 | AttachmentStateBlueprint blueprint(descriptor); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 674 | return GetOrCreateAttachmentState(&blueprint); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void DeviceBase::UncacheAttachmentState(AttachmentState* obj) { |
Austin Eng | 84bcf44 | 2019-10-30 00:20:03 +0000 | [diff] [blame] | 678 | ASSERT(obj->IsCachedReference()); |
Austin Eng | b98f0fa | 2019-07-26 19:08:18 +0000 | [diff] [blame] | 679 | size_t removedCount = mCaches->attachmentStates.erase(obj); |
| 680 | ASSERT(removedCount == 1); |
| 681 | } |
| 682 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 683 | // Object creation API methods |
| 684 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 685 | BindGroupBase* DeviceBase::APICreateBindGroup(const BindGroupDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 686 | Ref<BindGroupBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 687 | if (ConsumedError(CreateBindGroup(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 688 | return BindGroupBase::MakeError(this); |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 689 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 690 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 691 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 692 | BindGroupLayoutBase* DeviceBase::APICreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 693 | const BindGroupLayoutDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 694 | Ref<BindGroupLayoutBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 695 | if (ConsumedError(CreateBindGroupLayout(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 696 | return BindGroupLayoutBase::MakeError(this); |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 697 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 698 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 699 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 700 | BufferBase* DeviceBase::APICreateBuffer(const BufferDescriptor* descriptor) { |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 701 | Ref<BufferBase> result = nullptr; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 702 | if (ConsumedError(CreateBuffer(descriptor), &result)) { |
Corentin Wallez | 55f251d | 2020-11-18 09:10:22 +0000 | [diff] [blame] | 703 | ASSERT(result == nullptr); |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 704 | return BufferBase::MakeError(this, descriptor); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 705 | } |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 706 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 707 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 708 | CommandEncoder* DeviceBase::APICreateCommandEncoder( |
| 709 | const CommandEncoderDescriptor* descriptor) { |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 710 | return new CommandEncoder(this, descriptor); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 711 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 712 | ComputePipelineBase* DeviceBase::APICreateComputePipeline( |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 713 | const ComputePipelineDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 714 | Ref<ComputePipelineBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 715 | if (ConsumedError(CreateComputePipeline(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 716 | return ComputePipelineBase::MakeError(this); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 717 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 718 | return result.Detach(); |
Corentin Wallez | 29ced28 | 2017-07-14 10:58:07 -0400 | [diff] [blame] | 719 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 720 | void DeviceBase::APICreateComputePipelineAsync(const ComputePipelineDescriptor* descriptor, |
| 721 | WGPUCreateComputePipelineAsyncCallback callback, |
| 722 | void* userdata) { |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 723 | MaybeError maybeResult = CreateComputePipelineAsync(descriptor, callback, userdata); |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 724 | |
| 725 | // Call the callback directly when a validation error has been found in the front-end |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 726 | // validations. If there is no error, then CreateComputePipelineAsync will call the |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 727 | // callback. |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 728 | if (maybeResult.IsError()) { |
| 729 | std::unique_ptr<ErrorData> error = maybeResult.AcquireError(); |
Corentin Wallez | 2d3c2e3 | 2021-02-22 18:27:36 +0000 | [diff] [blame] | 730 | callback(WGPUCreatePipelineAsyncStatus_Error, nullptr, error->GetMessage().c_str(), |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 731 | userdata); |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 732 | } |
Jiawei Shao | ae5f950 | 2020-10-19 01:56:08 +0000 | [diff] [blame] | 733 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 734 | PipelineLayoutBase* DeviceBase::APICreatePipelineLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 735 | const PipelineLayoutDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 736 | Ref<PipelineLayoutBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 737 | if (ConsumedError(CreatePipelineLayout(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 738 | return PipelineLayoutBase::MakeError(this); |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 739 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 740 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 741 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 742 | QuerySetBase* DeviceBase::APICreateQuerySet(const QuerySetDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 743 | Ref<QuerySetBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 744 | if (ConsumedError(CreateQuerySet(descriptor), &result)) { |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 745 | return QuerySetBase::MakeError(this); |
| 746 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 747 | return result.Detach(); |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 748 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 749 | SamplerBase* DeviceBase::APICreateSampler(const SamplerDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 750 | Ref<SamplerBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 751 | if (ConsumedError(CreateSampler(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 752 | return SamplerBase::MakeError(this); |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 753 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 754 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 755 | } |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 756 | void DeviceBase::APICreateRenderPipelineAsync(const RenderPipelineDescriptor* descriptor, |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 757 | WGPUCreateRenderPipelineAsyncCallback callback, |
| 758 | void* userdata) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 759 | ResultOrError<Ref<RenderPipelineBase>> maybeResult = |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 760 | CreateRenderPipeline(descriptor); |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 761 | if (maybeResult.IsError()) { |
| 762 | std::unique_ptr<ErrorData> error = maybeResult.AcquireError(); |
Corentin Wallez | 2d3c2e3 | 2021-02-22 18:27:36 +0000 | [diff] [blame] | 763 | callback(WGPUCreatePipelineAsyncStatus_Error, nullptr, error->GetMessage().c_str(), |
Jiawei Shao | 03e1400 | 2020-10-21 04:37:41 +0000 | [diff] [blame] | 764 | userdata); |
| 765 | return; |
| 766 | } |
| 767 | |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 768 | Ref<RenderPipelineBase> result = maybeResult.AcquireSuccess(); |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 769 | std::unique_ptr<CreateRenderPipelineAsyncCallbackTask> callbackTask = |
| 770 | std::make_unique<CreateRenderPipelineAsyncCallbackTask>(std::move(result), "", callback, |
| 771 | userdata); |
| 772 | mCallbackTaskManager->AddCallbackTask(std::move(callbackTask)); |
Jiawei Shao | 03e1400 | 2020-10-21 04:37:41 +0000 | [diff] [blame] | 773 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 774 | RenderBundleEncoder* DeviceBase::APICreateRenderBundleEncoder( |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 775 | const RenderBundleEncoderDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 776 | Ref<RenderBundleEncoder> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 777 | if (ConsumedError(CreateRenderBundleEncoder(descriptor), &result)) { |
Corentin Wallez | 321c122 | 2019-11-13 17:00:37 +0000 | [diff] [blame] | 778 | return RenderBundleEncoder::MakeError(this); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 779 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 780 | return result.Detach(); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 781 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 782 | RenderPipelineBase* DeviceBase::APICreateRenderPipeline( |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 783 | const RenderPipelineDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 784 | Ref<RenderPipelineBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 785 | if (ConsumedError(CreateRenderPipeline(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 786 | return RenderPipelineBase::MakeError(this); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 787 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 788 | return result.Detach(); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 789 | } |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 790 | RenderPipelineBase* DeviceBase::APICreateRenderPipeline2( |
| 791 | const RenderPipelineDescriptor* descriptor) { |
| 792 | EmitDeprecationWarning( |
| 793 | "CreateRenderPipeline2() has been deprecated. Please begin using " |
| 794 | "CreateRenderPipeline() instead."); |
| 795 | return APICreateRenderPipeline(descriptor); |
| 796 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 797 | ShaderModuleBase* DeviceBase::APICreateShaderModule(const ShaderModuleDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 798 | Ref<ShaderModuleBase> result; |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 799 | ShaderModuleParseResult parseResult = {}; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 800 | if (ConsumedError(CreateShaderModule(descriptor, &parseResult), &result)) { |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 801 | return ShaderModuleBase::MakeError(this, std::move(parseResult.compilationMessages)); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 802 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 803 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 804 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 805 | SwapChainBase* DeviceBase::APICreateSwapChain(Surface* surface, |
| 806 | const SwapChainDescriptor* descriptor) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 807 | Ref<SwapChainBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 808 | if (ConsumedError(CreateSwapChain(surface, descriptor), &result)) { |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 809 | return SwapChainBase::MakeError(this); |
| 810 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 811 | return result.Detach(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 812 | } |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 813 | TextureBase* DeviceBase::APICreateTexture(const TextureDescriptor* descriptor) { |
Rafael Cintron | 0e9320b | 2020-04-23 19:47:12 +0000 | [diff] [blame] | 814 | Ref<TextureBase> result; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 815 | if (ConsumedError(CreateTexture(descriptor), &result)) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 816 | return TextureBase::MakeError(this); |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 817 | } |
Rafael Cintron | 0e9320b | 2020-04-23 19:47:12 +0000 | [diff] [blame] | 818 | return result.Detach(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 819 | } |
| 820 | |
Austin Eng | cf1fdf4 | 2020-06-15 23:42:13 +0000 | [diff] [blame] | 821 | // For Dawn Wire |
| 822 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 823 | BufferBase* DeviceBase::APICreateErrorBuffer() { |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 824 | BufferDescriptor desc = {}; |
| 825 | return BufferBase::MakeError(this, &desc); |
Austin Eng | cf1fdf4 | 2020-06-15 23:42:13 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 828 | // Other Device API methods |
| 829 | |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 830 | // Returns true if future ticking is needed. |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 831 | bool DeviceBase::APITick() { |
Corentin Wallez | 1bc1ed4 | 2021-03-30 10:21:36 +0000 | [diff] [blame] | 832 | if (ConsumedError(Tick())) { |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 833 | return false; |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 834 | } |
Corentin Wallez | 1bc1ed4 | 2021-03-30 10:21:36 +0000 | [diff] [blame] | 835 | return !IsDeviceIdle(); |
| 836 | } |
| 837 | |
| 838 | MaybeError DeviceBase::Tick() { |
| 839 | DAWN_TRY(ValidateIsAlive()); |
| 840 | |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 841 | // to avoid overly ticking, we only want to tick when: |
| 842 | // 1. the last submitted serial has moved beyond the completed serial |
| 843 | // 2. or the completed serial has not reached the future serial set by the trackers |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 844 | if (mLastSubmittedSerial > mCompletedSerial || mCompletedSerial < mFutureSerial) { |
Corentin Wallez | 6870e6d | 2021-04-07 18:09:21 +0000 | [diff] [blame] | 845 | DAWN_TRY(CheckPassedSerials()); |
Corentin Wallez | 1bc1ed4 | 2021-03-30 10:21:36 +0000 | [diff] [blame] | 846 | DAWN_TRY(TickImpl()); |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 847 | |
| 848 | // There is no GPU work in flight, we need to move the serials forward so that |
| 849 | // so that CPU operations waiting on GPU completion can know they don't have to wait. |
| 850 | // AssumeCommandsComplete will assign the max serial we must tick to in order to |
| 851 | // fire the awaiting callbacks. |
| 852 | if (mCompletedSerial == mLastSubmittedSerial) { |
| 853 | AssumeCommandsComplete(); |
| 854 | } |
| 855 | |
| 856 | // TODO(cwallez@chromium.org): decouple TickImpl from updating the serial so that we can |
| 857 | // tick the dynamic uploader before the backend resource allocators. This would allow |
| 858 | // reclaiming resources one tick earlier. |
| 859 | mDynamicUploader->Deallocate(mCompletedSerial); |
Corentin Wallez | e91975c | 2021-03-29 15:40:55 +0000 | [diff] [blame] | 860 | mQueue->Tick(mCompletedSerial); |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 861 | } |
Jiawei Shao | 42103bc | 2020-10-24 03:11:43 +0000 | [diff] [blame] | 862 | |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 863 | // We have to check mCallbackTaskManager in every Tick because it is not related to any |
| 864 | // global serials. |
| 865 | if (!mCallbackTaskManager->IsEmpty()) { |
| 866 | // If a user calls Queue::Submit inside the callback, then the device will be ticked, |
| 867 | // which in turns ticks the tracker, causing reentrance and dead lock here. To prevent |
| 868 | // such reentrant call, we remove all the callback tasks from mCallbackTaskManager, |
| 869 | // update mCallbackTaskManager, then call all the callbacks. |
| 870 | auto callbackTasks = mCallbackTaskManager->AcquireCallbackTasks(); |
| 871 | for (std::unique_ptr<CallbackTask>& callbackTask : callbackTasks) { |
| 872 | callbackTask->Finish(); |
| 873 | } |
Natasha Lee | 783cd5a | 2020-06-04 02:26:46 +0000 | [diff] [blame] | 874 | } |
Brandon Jones | 4ad3586 | 2020-10-15 16:21:03 +0000 | [diff] [blame] | 875 | |
Corentin Wallez | 1bc1ed4 | 2021-03-30 10:21:36 +0000 | [diff] [blame] | 876 | return {}; |
Corentin Wallez | dbb5729 | 2017-06-14 13:33:45 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Corentin Wallez | 2ce4b90 | 2021-03-29 14:02:05 +0000 | [diff] [blame] | 879 | QueueBase* DeviceBase::APIGetQueue() { |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 880 | // Backends gave the primary queue during initialization. |
| 881 | ASSERT(mQueue != nullptr); |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 882 | |
| 883 | // Returns a new reference to the queue. |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 884 | mQueue->Reference(); |
| 885 | return mQueue.Get(); |
| 886 | } |
| 887 | |
Brandon Jones | 0e92e9b | 2021-04-01 20:46:42 +0000 | [diff] [blame] | 888 | ExternalTextureBase* DeviceBase::APICreateExternalTexture( |
| 889 | const ExternalTextureDescriptor* descriptor) { |
| 890 | Ref<ExternalTextureBase> result = nullptr; |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 891 | if (ConsumedError(CreateExternalTexture(descriptor), &result)) { |
Brandon Jones | 0e92e9b | 2021-04-01 20:46:42 +0000 | [diff] [blame] | 892 | return ExternalTextureBase::MakeError(this); |
| 893 | } |
| 894 | |
| 895 | return result.Detach(); |
| 896 | } |
| 897 | |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 898 | void DeviceBase::ApplyExtensions(const DeviceDescriptor* deviceDescriptor) { |
| 899 | ASSERT(deviceDescriptor); |
| 900 | ASSERT(GetAdapter()->SupportsAllRequestedExtensions(deviceDescriptor->requiredExtensions)); |
| 901 | |
| 902 | mEnabledExtensions = GetAdapter()->GetInstance()->ExtensionNamesToExtensionsSet( |
| 903 | deviceDescriptor->requiredExtensions); |
| 904 | } |
| 905 | |
| 906 | std::vector<const char*> DeviceBase::GetEnabledExtensions() const { |
| 907 | return mEnabledExtensions.GetEnabledExtensionNames(); |
| 908 | } |
| 909 | |
Jiawei Shao | 574b951 | 2019-08-02 00:06:38 +0000 | [diff] [blame] | 910 | bool DeviceBase::IsExtensionEnabled(Extension extension) const { |
| 911 | return mEnabledExtensions.IsEnabled(extension); |
| 912 | } |
| 913 | |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 914 | bool DeviceBase::IsValidationEnabled() const { |
| 915 | return !IsToggleEnabled(Toggle::SkipValidation); |
| 916 | } |
| 917 | |
Idan Raiter | bcc65f2 | 2020-07-01 21:37:57 +0000 | [diff] [blame] | 918 | bool DeviceBase::IsRobustnessEnabled() const { |
| 919 | return !IsToggleEnabled(Toggle::DisableRobustness); |
| 920 | } |
| 921 | |
Natasha Lee | e69627f | 2019-07-26 17:54:48 +0000 | [diff] [blame] | 922 | size_t DeviceBase::GetLazyClearCountForTesting() { |
| 923 | return mLazyClearCountForTesting; |
| 924 | } |
| 925 | |
| 926 | void DeviceBase::IncrementLazyClearCountForTesting() { |
| 927 | ++mLazyClearCountForTesting; |
| 928 | } |
| 929 | |
Corentin Wallez | 8a43794 | 2020-04-17 16:45:17 +0000 | [diff] [blame] | 930 | size_t DeviceBase::GetDeprecationWarningCountForTesting() { |
| 931 | return mDeprecationWarnings->count; |
| 932 | } |
| 933 | |
| 934 | void DeviceBase::EmitDeprecationWarning(const char* warning) { |
| 935 | mDeprecationWarnings->count++; |
| 936 | if (mDeprecationWarnings->emitted.insert(warning).second) { |
| 937 | dawn::WarningLog() << warning; |
| 938 | } |
| 939 | } |
| 940 | |
Corentin Wallez | e91975c | 2021-03-29 15:40:55 +0000 | [diff] [blame] | 941 | QueueBase* DeviceBase::GetQueue() const { |
| 942 | return mQueue.Get(); |
| 943 | } |
| 944 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 945 | // Implementation details of object creation |
| 946 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 947 | ResultOrError<Ref<BindGroupBase>> DeviceBase::CreateBindGroup( |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 948 | const BindGroupDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 949 | DAWN_TRY(ValidateIsAlive()); |
Corentin Wallez | 3966eb1 | 2020-04-21 07:36:30 +0000 | [diff] [blame] | 950 | if (IsValidationEnabled()) { |
Corentin Wallez | 84ae2bf | 2020-05-13 17:05:55 +0000 | [diff] [blame] | 951 | DAWN_TRY(ValidateBindGroupDescriptor(this, descriptor)); |
Corentin Wallez | 3966eb1 | 2020-04-21 07:36:30 +0000 | [diff] [blame] | 952 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 953 | return CreateBindGroupImpl(descriptor); |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 956 | ResultOrError<Ref<BindGroupLayoutBase>> DeviceBase::CreateBindGroupLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 957 | const BindGroupLayoutDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 958 | DAWN_TRY(ValidateIsAlive()); |
Corentin Wallez | 3966eb1 | 2020-04-21 07:36:30 +0000 | [diff] [blame] | 959 | if (IsValidationEnabled()) { |
Corentin Wallez | 84ae2bf | 2020-05-13 17:05:55 +0000 | [diff] [blame] | 960 | DAWN_TRY(ValidateBindGroupLayoutDescriptor(this, descriptor)); |
Corentin Wallez | 3966eb1 | 2020-04-21 07:36:30 +0000 | [diff] [blame] | 961 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 962 | return GetOrCreateBindGroupLayout(descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 965 | ResultOrError<Ref<BufferBase>> DeviceBase::CreateBuffer(const BufferDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 966 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 967 | if (IsValidationEnabled()) { |
| 968 | DAWN_TRY(ValidateBufferDescriptor(this, descriptor)); |
| 969 | } |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 970 | |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 971 | Ref<BufferBase> buffer; |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 972 | DAWN_TRY_ASSIGN(buffer, CreateBufferImpl(descriptor)); |
| 973 | |
| 974 | if (descriptor->mappedAtCreation) { |
| 975 | DAWN_TRY(buffer->MapAtCreation()); |
| 976 | } |
| 977 | |
Corentin Wallez | 8a99199 | 2020-07-08 19:45:40 +0000 | [diff] [blame] | 978 | return std::move(buffer); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 981 | ResultOrError<Ref<ComputePipelineBase>> DeviceBase::CreateComputePipeline( |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 982 | const ComputePipelineDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 983 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 984 | if (IsValidationEnabled()) { |
| 985 | DAWN_TRY(ValidateComputePipelineDescriptor(this, descriptor)); |
| 986 | } |
Austin Eng | f6eb890 | 2019-11-22 17:02:22 +0000 | [diff] [blame] | 987 | |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 988 | // Ref will keep the pipeline layout alive until the end of the function where |
| 989 | // the pipeline will take another reference. |
| 990 | Ref<PipelineLayoutBase> layoutRef; |
| 991 | ComputePipelineDescriptor appliedDescriptor; |
| 992 | DAWN_TRY_ASSIGN(layoutRef, ValidateAndGetComputePipelineDescriptorWithDefaults( |
| 993 | *descriptor, &appliedDescriptor)); |
Austin Eng | f6eb890 | 2019-11-22 17:02:22 +0000 | [diff] [blame] | 994 | |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 995 | auto pipelineAndBlueprintFromCache = GetCachedComputePipeline(&appliedDescriptor); |
| 996 | if (pipelineAndBlueprintFromCache.first.Get() != nullptr) { |
| 997 | return std::move(pipelineAndBlueprintFromCache.first); |
Austin Eng | f6eb890 | 2019-11-22 17:02:22 +0000 | [diff] [blame] | 998 | } |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 999 | |
| 1000 | Ref<ComputePipelineBase> backendObj; |
| 1001 | DAWN_TRY_ASSIGN(backendObj, CreateComputePipelineImpl(&appliedDescriptor)); |
| 1002 | size_t blueprintHash = pipelineAndBlueprintFromCache.second; |
| 1003 | return AddOrGetCachedPipeline(backendObj, blueprintHash); |
| 1004 | } |
| 1005 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1006 | MaybeError DeviceBase::CreateComputePipelineAsync( |
Jiawei Shao | c243f67 | 2021-04-08 01:22:22 +0000 | [diff] [blame] | 1007 | const ComputePipelineDescriptor* descriptor, |
| 1008 | WGPUCreateComputePipelineAsyncCallback callback, |
| 1009 | void* userdata) { |
| 1010 | DAWN_TRY(ValidateIsAlive()); |
| 1011 | if (IsValidationEnabled()) { |
| 1012 | DAWN_TRY(ValidateComputePipelineDescriptor(this, descriptor)); |
| 1013 | } |
| 1014 | |
| 1015 | // Ref will keep the pipeline layout alive until the end of the function where |
| 1016 | // the pipeline will take another reference. |
| 1017 | Ref<PipelineLayoutBase> layoutRef; |
| 1018 | ComputePipelineDescriptor appliedDescriptor; |
| 1019 | DAWN_TRY_ASSIGN(layoutRef, ValidateAndGetComputePipelineDescriptorWithDefaults( |
| 1020 | *descriptor, &appliedDescriptor)); |
| 1021 | |
| 1022 | // Call the callback directly when we can get a cached compute pipeline object. |
| 1023 | auto pipelineAndBlueprintFromCache = GetCachedComputePipeline(&appliedDescriptor); |
| 1024 | if (pipelineAndBlueprintFromCache.first.Get() != nullptr) { |
| 1025 | Ref<ComputePipelineBase> result = std::move(pipelineAndBlueprintFromCache.first); |
| 1026 | callback(WGPUCreatePipelineAsyncStatus_Success, |
| 1027 | reinterpret_cast<WGPUComputePipeline>(result.Detach()), "", userdata); |
| 1028 | } else { |
| 1029 | // Otherwise we will create the pipeline object in CreateComputePipelineAsyncImpl(), |
| 1030 | // where the pipeline object may be created asynchronously and the result will be saved |
| 1031 | // to mCreatePipelineAsyncTracker. |
| 1032 | const size_t blueprintHash = pipelineAndBlueprintFromCache.second; |
| 1033 | CreateComputePipelineAsyncImpl(&appliedDescriptor, blueprintHash, callback, userdata); |
| 1034 | } |
| 1035 | |
| 1036 | return {}; |
| 1037 | } |
| 1038 | |
| 1039 | ResultOrError<Ref<PipelineLayoutBase>> |
| 1040 | DeviceBase::ValidateAndGetComputePipelineDescriptorWithDefaults( |
| 1041 | const ComputePipelineDescriptor& descriptor, |
| 1042 | ComputePipelineDescriptor* outDescriptor) { |
| 1043 | Ref<PipelineLayoutBase> layoutRef; |
| 1044 | *outDescriptor = descriptor; |
| 1045 | if (outDescriptor->layout == nullptr) { |
| 1046 | DAWN_TRY_ASSIGN(layoutRef, PipelineLayoutBase::CreateDefault( |
| 1047 | this, {{SingleShaderStage::Compute, |
| 1048 | outDescriptor->computeStage.module, |
| 1049 | outDescriptor->computeStage.entryPoint}})); |
| 1050 | outDescriptor->layout = layoutRef.Get(); |
| 1051 | } |
| 1052 | |
| 1053 | return layoutRef; |
| 1054 | } |
| 1055 | |
| 1056 | // TODO(jiawei.shao@intel.com): override this function with the async version on the backends |
| 1057 | // that supports creating compute pipeline asynchronously |
| 1058 | void DeviceBase::CreateComputePipelineAsyncImpl(const ComputePipelineDescriptor* descriptor, |
| 1059 | size_t blueprintHash, |
| 1060 | WGPUCreateComputePipelineAsyncCallback callback, |
| 1061 | void* userdata) { |
| 1062 | Ref<ComputePipelineBase> result; |
| 1063 | std::string errorMessage; |
| 1064 | |
| 1065 | auto resultOrError = CreateComputePipelineImpl(descriptor); |
| 1066 | if (resultOrError.IsError()) { |
| 1067 | std::unique_ptr<ErrorData> error = resultOrError.AcquireError(); |
| 1068 | errorMessage = error->GetMessage(); |
| 1069 | } else { |
| 1070 | result = AddOrGetCachedPipeline(resultOrError.AcquireSuccess(), blueprintHash); |
| 1071 | } |
| 1072 | |
Jiawei Shao | c74af703 | 2021-05-02 03:22:30 +0000 | [diff] [blame] | 1073 | std::unique_ptr<CreateComputePipelineAsyncCallbackTask> callbackTask = |
| 1074 | std::make_unique<CreateComputePipelineAsyncCallbackTask>( |
| 1075 | std::move(result), errorMessage, callback, userdata); |
| 1076 | mCallbackTaskManager->AddCallbackTask(std::move(callbackTask)); |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1079 | ResultOrError<Ref<PipelineLayoutBase>> DeviceBase::CreatePipelineLayout( |
Corentin Wallez | 36afbb6 | 2018-07-25 17:03:23 +0200 | [diff] [blame] | 1080 | const PipelineLayoutDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1081 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1082 | if (IsValidationEnabled()) { |
| 1083 | DAWN_TRY(ValidatePipelineLayoutDescriptor(this, descriptor)); |
| 1084 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1085 | return GetOrCreatePipelineLayout(descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1088 | ResultOrError<Ref<ExternalTextureBase>> DeviceBase::CreateExternalTexture( |
Brandon Jones | 0e92e9b | 2021-04-01 20:46:42 +0000 | [diff] [blame] | 1089 | const ExternalTextureDescriptor* descriptor) { |
| 1090 | if (IsValidationEnabled()) { |
| 1091 | DAWN_TRY(ValidateExternalTextureDescriptor(this, descriptor)); |
| 1092 | } |
| 1093 | |
| 1094 | return ExternalTextureBase::Create(this, descriptor); |
| 1095 | } |
| 1096 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1097 | ResultOrError<Ref<QuerySetBase>> DeviceBase::CreateQuerySet( |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1098 | const QuerySetDescriptor* descriptor) { |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 1099 | DAWN_TRY(ValidateIsAlive()); |
| 1100 | if (IsValidationEnabled()) { |
| 1101 | DAWN_TRY(ValidateQuerySetDescriptor(this, descriptor)); |
| 1102 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1103 | return CreateQuerySetImpl(descriptor); |
Hao Li | b6eff5a | 2020-06-11 00:34:14 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1106 | ResultOrError<Ref<RenderBundleEncoder>> DeviceBase::CreateRenderBundleEncoder( |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 1107 | const RenderBundleEncoderDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1108 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1109 | if (IsValidationEnabled()) { |
| 1110 | DAWN_TRY(ValidateRenderBundleEncoderDescriptor(this, descriptor)); |
| 1111 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1112 | return RenderBundleEncoder::Create(this, descriptor); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1115 | ResultOrError<Ref<RenderPipelineBase>> DeviceBase::CreateRenderPipeline( |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 1116 | const RenderPipelineDescriptor* descriptor) { |
Brandon Jones | 7f77cfd | 2021-03-31 08:56:02 +0000 | [diff] [blame] | 1117 | DAWN_TRY(ValidateIsAlive()); |
| 1118 | if (IsValidationEnabled()) { |
| 1119 | DAWN_TRY(ValidateRenderPipelineDescriptor(this, descriptor)); |
| 1120 | } |
| 1121 | |
Brandon Jones | 7f77cfd | 2021-03-31 08:56:02 +0000 | [diff] [blame] | 1122 | if (descriptor->layout == nullptr) { |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 1123 | RenderPipelineDescriptor descriptorWithDefaultLayout = *descriptor; |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 1124 | |
Brandon Jones | 7f77cfd | 2021-03-31 08:56:02 +0000 | [diff] [blame] | 1125 | // Ref will keep the pipeline layout alive until the end of the function where |
| 1126 | // the pipeline will take another reference. |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 1127 | Ref<PipelineLayoutBase> layoutRef; |
Brandon Jones | 7f77cfd | 2021-03-31 08:56:02 +0000 | [diff] [blame] | 1128 | DAWN_TRY_ASSIGN(layoutRef, |
| 1129 | PipelineLayoutBase::CreateDefault(this, GetStages(descriptor))); |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 1130 | descriptorWithDefaultLayout.layout = layoutRef.Get(); |
Brandon Jones | 7f77cfd | 2021-03-31 08:56:02 +0000 | [diff] [blame] | 1131 | |
Brandon Jones | 3ceb654 | 2021-04-01 01:28:52 +0000 | [diff] [blame] | 1132 | return GetOrCreateRenderPipeline(&descriptorWithDefaultLayout); |
| 1133 | } else { |
| 1134 | return GetOrCreateRenderPipeline(descriptor); |
| 1135 | } |
Brandon Jones | 0702b70 | 2021-03-11 21:19:00 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1138 | ResultOrError<Ref<SamplerBase>> DeviceBase::CreateSampler( |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1139 | const SamplerDescriptor* descriptor) { |
shrekshao | b3177d4 | 2021-01-26 02:22:58 +0000 | [diff] [blame] | 1140 | const SamplerDescriptor defaultDescriptor = {}; |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1141 | DAWN_TRY(ValidateIsAlive()); |
shrekshao | b3177d4 | 2021-01-26 02:22:58 +0000 | [diff] [blame] | 1142 | descriptor = descriptor != nullptr ? descriptor : &defaultDescriptor; |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1143 | if (IsValidationEnabled()) { |
| 1144 | DAWN_TRY(ValidateSamplerDescriptor(this, descriptor)); |
| 1145 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1146 | return GetOrCreateSampler(descriptor); |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1149 | ResultOrError<Ref<ShaderModuleBase>> DeviceBase::CreateShaderModule( |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 1150 | const ShaderModuleDescriptor* descriptor, |
| 1151 | ShaderModuleParseResult* parseResult) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1152 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 1153 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1154 | // ShaderModule can be called from inside dawn_native. If that's the case handle the error |
| 1155 | // directly in Dawn and don't need the parse results since there should be no validation |
| 1156 | // errors. |
| 1157 | ShaderModuleParseResult ignoredResults; |
| 1158 | if (parseResult == nullptr) { |
| 1159 | parseResult = &ignoredResults; |
| 1160 | } |
| 1161 | |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1162 | if (IsValidationEnabled()) { |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 1163 | DAWN_TRY(ValidateShaderModuleDescriptor(this, descriptor, parseResult)); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1164 | } |
Austin Eng | 0d948f7 | 2020-12-07 18:12:13 +0000 | [diff] [blame] | 1165 | |
Brandon Jones | 47b6b68 | 2021-04-08 04:25:11 +0000 | [diff] [blame] | 1166 | return GetOrCreateShaderModule(descriptor, parseResult); |
Corentin Wallez | df67103 | 2018-08-20 17:01:20 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1169 | ResultOrError<Ref<SwapChainBase>> DeviceBase::CreateSwapChain( |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1170 | Surface* surface, |
| 1171 | const SwapChainDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1172 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1173 | if (IsValidationEnabled()) { |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 1174 | DAWN_TRY(ValidateSwapChainDescriptor(this, surface, descriptor)); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1175 | } |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 1176 | |
Corentin Wallez | 25eeaa3 | 2020-10-27 11:31:26 +0000 | [diff] [blame] | 1177 | // TODO(dawn:269): Remove this code path once implementation-based swapchains are removed. |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 1178 | if (surface == nullptr) { |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1179 | return CreateSwapChainImpl(descriptor); |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 1180 | } else { |
| 1181 | ASSERT(descriptor->implementation == 0); |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 1182 | |
| 1183 | NewSwapChainBase* previousSwapChain = surface->GetAttachedSwapChain(); |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1184 | ResultOrError<Ref<NewSwapChainBase>> maybeNewSwapChain = |
Corentin Wallez | 25eeaa3 | 2020-10-27 11:31:26 +0000 | [diff] [blame] | 1185 | CreateSwapChainImpl(surface, previousSwapChain, descriptor); |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (previousSwapChain != nullptr) { |
Corentin Wallez | 25eeaa3 | 2020-10-27 11:31:26 +0000 | [diff] [blame] | 1188 | previousSwapChain->DetachFromSurface(); |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 1189 | } |
Corentin Wallez | d26ee85 | 2020-01-25 10:05:40 +0000 | [diff] [blame] | 1190 | |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1191 | Ref<NewSwapChainBase> newSwapChain; |
Corentin Wallez | 25eeaa3 | 2020-10-27 11:31:26 +0000 | [diff] [blame] | 1192 | DAWN_TRY_ASSIGN(newSwapChain, std::move(maybeNewSwapChain)); |
| 1193 | |
| 1194 | newSwapChain->SetIsAttached(); |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1195 | surface->SetAttachedSwapChain(newSwapChain.Get()); |
| 1196 | return newSwapChain; |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 1197 | } |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1200 | ResultOrError<Ref<TextureBase>> DeviceBase::CreateTexture(const TextureDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1201 | DAWN_TRY(ValidateIsAlive()); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1202 | if (IsValidationEnabled()) { |
Brandon Jones | 76e5a9f | 2021-05-13 17:51:23 +0000 | [diff] [blame] | 1203 | DAWN_TRY(ValidateTextureDescriptor(this, descriptor)); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1204 | } |
Brandon Jones | 76e5a9f | 2021-05-13 17:51:23 +0000 | [diff] [blame] | 1205 | return CreateTextureImpl(descriptor); |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Corentin Wallez | 0af4a83 | 2021-04-19 08:52:35 +0000 | [diff] [blame] | 1208 | ResultOrError<Ref<TextureViewBase>> DeviceBase::CreateTextureView( |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1209 | TextureBase* texture, |
| 1210 | const TextureViewDescriptor* descriptor) { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 1211 | DAWN_TRY(ValidateIsAlive()); |
Kai Ninomiya | 93196db | 2019-08-26 22:51:19 +0000 | [diff] [blame] | 1212 | DAWN_TRY(ValidateObject(texture)); |
| 1213 | TextureViewDescriptor desc = GetTextureViewDescriptorWithDefaults(texture, descriptor); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1214 | if (IsValidationEnabled()) { |
Yunchao He | 1e45c5e | 2021-04-08 18:32:17 +0000 | [diff] [blame] | 1215 | DAWN_TRY(ValidateTextureViewDescriptor(this, texture, &desc)); |
Austin Eng | 4d15609 | 2019-11-21 00:48:39 +0000 | [diff] [blame] | 1216 | } |
Corentin Wallez | 50f9958 | 2021-03-31 18:36:32 +0000 | [diff] [blame] | 1217 | return CreateTextureViewImpl(texture, &desc); |
Jiawei Shao | 6329e5a | 2018-10-12 08:32:58 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Corentin Wallez | 52f2383 | 2018-07-16 17:40:08 +0200 | [diff] [blame] | 1220 | // Other implementation details |
| 1221 | |
Bryan Bernhart | 450e212 | 2019-09-18 22:06:41 +0000 | [diff] [blame] | 1222 | DynamicUploader* DeviceBase::GetDynamicUploader() const { |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 1223 | return mDynamicUploader.get(); |
| 1224 | } |
| 1225 | |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 1226 | // The Toggle device facility |
| 1227 | |
| 1228 | std::vector<const char*> DeviceBase::GetTogglesUsed() const { |
| 1229 | return mEnabledToggles.GetContainedToggleNames(); |
| 1230 | } |
| 1231 | |
| 1232 | bool DeviceBase::IsToggleEnabled(Toggle toggle) const { |
| 1233 | return mEnabledToggles.Has(toggle); |
| 1234 | } |
| 1235 | |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 1236 | void DeviceBase::SetToggle(Toggle toggle, bool isEnabled) { |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 1237 | if (!mOverridenToggles.Has(toggle)) { |
| 1238 | mEnabledToggles.Set(toggle, isEnabled); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | void DeviceBase::ForceSetToggle(Toggle toggle, bool isEnabled) { |
| 1243 | if (!mOverridenToggles.Has(toggle) && mEnabledToggles.Has(toggle) != isEnabled) { |
| 1244 | dawn::WarningLog() << "Forcing toggle \"" << ToggleEnumToName(toggle) << "\" to " |
Jiawei Shao | b6e141a | 2020-11-20 08:27:29 +0000 | [diff] [blame] | 1245 | << isEnabled << " when it was overriden to be " << !isEnabled; |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 1246 | } |
Corentin Wallez | cb84c79 | 2020-04-06 08:32:01 +0000 | [diff] [blame] | 1247 | mEnabledToggles.Set(toggle, isEnabled); |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 1250 | void DeviceBase::SetDefaultToggles() { |
| 1251 | SetToggle(Toggle::LazyClearResourceOnFirstUse, true); |
Corentin Wallez | 07987ed | 2021-02-01 16:22:08 +0000 | [diff] [blame] | 1252 | SetToggle(Toggle::DisallowUnsafeAPIs, true); |
Corentin Wallez | 022d074 | 2020-04-06 16:55:22 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | void DeviceBase::ApplyToggleOverrides(const DeviceDescriptor* deviceDescriptor) { |
| 1256 | ASSERT(deviceDescriptor); |
| 1257 | |
| 1258 | for (const char* toggleName : deviceDescriptor->forceEnabledToggles) { |
| 1259 | Toggle toggle = GetAdapter()->GetInstance()->ToggleNameToEnum(toggleName); |
| 1260 | if (toggle != Toggle::InvalidEnum) { |
| 1261 | mEnabledToggles.Set(toggle, true); |
| 1262 | mOverridenToggles.Set(toggle, true); |
| 1263 | } |
| 1264 | } |
| 1265 | for (const char* toggleName : deviceDescriptor->forceDisabledToggles) { |
| 1266 | Toggle toggle = GetAdapter()->GetInstance()->ToggleNameToEnum(toggleName); |
| 1267 | if (toggle != Toggle::InvalidEnum) { |
| 1268 | mEnabledToggles.Set(toggle, false); |
| 1269 | mOverridenToggles.Set(toggle, true); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 1274 | } // namespace dawn_native |