Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Dawn Authors |
| 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 | |
| 15 | #include "dawn_native/EncodingContext.h" |
| 16 | |
| 17 | #include "common/Assert.h" |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 18 | #include "dawn_native/CommandEncoder.h" |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 19 | #include "dawn_native/Commands.h" |
| 20 | #include "dawn_native/Device.h" |
| 21 | #include "dawn_native/ErrorData.h" |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 22 | #include "dawn_native/IndirectDrawValidationEncoder.h" |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 23 | #include "dawn_native/RenderBundleEncoder.h" |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 24 | |
| 25 | namespace dawn_native { |
| 26 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 27 | EncodingContext::EncodingContext(DeviceBase* device, const ApiObjectBase* initialEncoder) |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 28 | : mDevice(device), mTopLevelEncoder(initialEncoder), mCurrentEncoder(initialEncoder) { |
| 29 | } |
| 30 | |
| 31 | EncodingContext::~EncodingContext() { |
Loko Kung | 970739e | 2021-11-16 22:46:34 +0000 | [diff] [blame] | 32 | Destroy(); |
| 33 | } |
| 34 | |
| 35 | void EncodingContext::Destroy() { |
| 36 | if (mDestroyed) { |
| 37 | return; |
| 38 | } |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 39 | if (!mWereCommandsAcquired) { |
| 40 | FreeCommands(GetIterator()); |
| 41 | } |
Loko Kung | 970739e | 2021-11-16 22:46:34 +0000 | [diff] [blame] | 42 | // If we weren't already finished, then we want to handle an error here so that any calls |
| 43 | // to Finish after Destroy will return a meaningful error. |
| 44 | if (!IsFinished()) { |
| 45 | HandleError(DAWN_FORMAT_VALIDATION_ERROR("Destroyed encoder cannot be finished.")); |
| 46 | } |
| 47 | mDestroyed = true; |
| 48 | mCurrentEncoder = nullptr; |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CommandIterator EncodingContext::AcquireCommands() { |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 52 | MoveToIterator(); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 53 | ASSERT(!mWereCommandsAcquired); |
| 54 | mWereCommandsAcquired = true; |
| 55 | return std::move(mIterator); |
| 56 | } |
| 57 | |
| 58 | CommandIterator* EncodingContext::GetIterator() { |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 59 | MoveToIterator(); |
| 60 | ASSERT(!mWereCommandsAcquired); |
| 61 | return &mIterator; |
| 62 | } |
| 63 | |
| 64 | void EncodingContext::MoveToIterator() { |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 65 | CommitCommands(std::move(mPendingCommands)); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 66 | if (!mWasMovedToIterator) { |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 67 | mIterator.AcquireCommandBlocks(std::move(mAllocators)); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 68 | mWasMovedToIterator = true; |
| 69 | } |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Corentin Wallez | d98b307 | 2021-05-05 17:37:43 +0000 | [diff] [blame] | 72 | void EncodingContext::HandleError(std::unique_ptr<ErrorData> error) { |
Brandon Jones | d6d2584 | 2021-09-29 19:39:02 +0000 | [diff] [blame] | 73 | // Append in reverse so that the most recently set debug group is printed first, like a |
| 74 | // call stack. |
| 75 | for (auto iter = mDebugGroupLabels.rbegin(); iter != mDebugGroupLabels.rend(); ++iter) { |
| 76 | error->AppendDebugGroup(*iter); |
| 77 | } |
| 78 | |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 79 | if (!IsFinished()) { |
Austin Eng | 464aaeb | 2020-11-14 01:24:03 +0000 | [diff] [blame] | 80 | // Encoding should only generate validation errors. |
Corentin Wallez | d98b307 | 2021-05-05 17:37:43 +0000 | [diff] [blame] | 81 | ASSERT(error->GetType() == InternalErrorType::Validation); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 82 | // If the encoding context is not finished, errors are deferred until |
| 83 | // Finish() is called. |
Corentin Wallez | d98b307 | 2021-05-05 17:37:43 +0000 | [diff] [blame] | 84 | if (mError == nullptr) { |
| 85 | mError = std::move(error); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 86 | } |
| 87 | } else { |
Brandon Jones | d6d2584 | 2021-09-29 19:39:02 +0000 | [diff] [blame] | 88 | mDevice->HandleError(error->GetType(), error->GetFormattedMessage().c_str()); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 92 | void EncodingContext::WillBeginRenderPass() { |
| 93 | ASSERT(mCurrentEncoder == mTopLevelEncoder); |
| 94 | if (mDevice->IsValidationEnabled()) { |
| 95 | // When validation is enabled, we are going to want to capture all commands encoded |
| 96 | // between and including BeginRenderPassCmd and EndRenderPassCmd, and defer their |
| 97 | // sequencing util after we have a chance to insert any necessary validation |
| 98 | // commands. To support this we commit any current commands now, so that the |
| 99 | // impending BeginRenderPassCmd starts in a fresh CommandAllocator. |
| 100 | CommitCommands(std::move(mPendingCommands)); |
| 101 | } |
| 102 | } |
| 103 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 104 | void EncodingContext::EnterPass(const ApiObjectBase* passEncoder) { |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 105 | // Assert we're at the top level. |
| 106 | ASSERT(mCurrentEncoder == mTopLevelEncoder); |
| 107 | ASSERT(passEncoder != nullptr); |
| 108 | |
| 109 | mCurrentEncoder = passEncoder; |
| 110 | } |
| 111 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 112 | MaybeError EncodingContext::ExitRenderPass(const ApiObjectBase* passEncoder, |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 113 | RenderPassResourceUsageTracker usageTracker, |
| 114 | CommandEncoder* commandEncoder, |
| 115 | IndirectDrawMetadata indirectDrawMetadata) { |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 116 | ASSERT(mCurrentEncoder != mTopLevelEncoder); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 117 | ASSERT(mCurrentEncoder == passEncoder); |
| 118 | |
| 119 | mCurrentEncoder = mTopLevelEncoder; |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 120 | |
| 121 | if (mDevice->IsValidationEnabled()) { |
| 122 | // With validation enabled, commands were committed just before BeginRenderPassCmd was |
| 123 | // encoded by our RenderPassEncoder (see WillBeginRenderPass above). This means |
| 124 | // mPendingCommands contains only the commands from BeginRenderPassCmd to |
| 125 | // EndRenderPassCmd, inclusive. Now we swap out this allocator with a fresh one to give |
| 126 | // the validation encoder a chance to insert its commands first. |
| 127 | CommandAllocator renderCommands = std::move(mPendingCommands); |
| 128 | DAWN_TRY(EncodeIndirectDrawValidationCommands(mDevice, commandEncoder, &usageTracker, |
| 129 | &indirectDrawMetadata)); |
| 130 | CommitCommands(std::move(mPendingCommands)); |
| 131 | CommitCommands(std::move(renderCommands)); |
| 132 | } |
| 133 | |
| 134 | mRenderPassUsages.push_back(usageTracker.AcquireResourceUsage()); |
| 135 | return {}; |
Corentin Wallez | ec7ea6a | 2021-05-05 19:55:23 +0000 | [diff] [blame] | 136 | } |
Corentin Wallez | 2dd2d67 | 2021-05-05 15:41:13 +0000 | [diff] [blame] | 137 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 138 | void EncodingContext::ExitComputePass(const ApiObjectBase* passEncoder, |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 139 | ComputePassResourceUsage usages) { |
Corentin Wallez | ec7ea6a | 2021-05-05 19:55:23 +0000 | [diff] [blame] | 140 | ASSERT(mCurrentEncoder != mTopLevelEncoder); |
| 141 | ASSERT(mCurrentEncoder == passEncoder); |
| 142 | |
| 143 | mCurrentEncoder = mTopLevelEncoder; |
| 144 | mComputePassUsages.push_back(std::move(usages)); |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 147 | void EncodingContext::EnsurePassExited(const ApiObjectBase* passEncoder) { |
| 148 | if (mCurrentEncoder != mTopLevelEncoder && mCurrentEncoder == passEncoder) { |
| 149 | // The current pass encoder is being deleted. Implicitly end the pass with an error. |
| 150 | mCurrentEncoder = mTopLevelEncoder; |
| 151 | HandleError(DAWN_FORMAT_VALIDATION_ERROR( |
| 152 | "Command buffer recording ended before %s was ended.", passEncoder)); |
| 153 | } |
| 154 | } |
| 155 | |
Corentin Wallez | 2dd2d67 | 2021-05-05 15:41:13 +0000 | [diff] [blame] | 156 | const RenderPassUsages& EncodingContext::GetRenderPassUsages() const { |
| 157 | ASSERT(!mWereRenderPassUsagesAcquired); |
| 158 | return mRenderPassUsages; |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Corentin Wallez | 2dd2d67 | 2021-05-05 15:41:13 +0000 | [diff] [blame] | 161 | RenderPassUsages EncodingContext::AcquireRenderPassUsages() { |
| 162 | ASSERT(!mWereRenderPassUsagesAcquired); |
| 163 | mWereRenderPassUsagesAcquired = true; |
| 164 | return std::move(mRenderPassUsages); |
| 165 | } |
| 166 | |
| 167 | const ComputePassUsages& EncodingContext::GetComputePassUsages() const { |
| 168 | ASSERT(!mWereComputePassUsagesAcquired); |
| 169 | return mComputePassUsages; |
| 170 | } |
| 171 | |
| 172 | ComputePassUsages EncodingContext::AcquireComputePassUsages() { |
| 173 | ASSERT(!mWereComputePassUsagesAcquired); |
| 174 | mWereComputePassUsagesAcquired = true; |
| 175 | return std::move(mComputePassUsages); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Brandon Jones | d6d2584 | 2021-09-29 19:39:02 +0000 | [diff] [blame] | 178 | void EncodingContext::PushDebugGroupLabel(const char* groupLabel) { |
| 179 | mDebugGroupLabels.emplace_back(groupLabel); |
| 180 | } |
| 181 | |
| 182 | void EncodingContext::PopDebugGroupLabel() { |
| 183 | mDebugGroupLabels.pop_back(); |
| 184 | } |
| 185 | |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 186 | MaybeError EncodingContext::Finish() { |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 187 | DAWN_INVALID_IF(IsFinished(), "Command encoding already finished."); |
Austin Eng | 8a488c1 | 2019-08-13 22:12:54 +0000 | [diff] [blame] | 188 | |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 189 | const ApiObjectBase* currentEncoder = mCurrentEncoder; |
| 190 | const ApiObjectBase* topLevelEncoder = mTopLevelEncoder; |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 191 | |
| 192 | // Even if finish validation fails, it is now invalid to call any encoding commands, |
| 193 | // so we clear the encoders. Note: mTopLevelEncoder == nullptr is used as a flag for |
| 194 | // if Finish() has been called. |
| 195 | mCurrentEncoder = nullptr; |
| 196 | mTopLevelEncoder = nullptr; |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 197 | CommitCommands(std::move(mPendingCommands)); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 198 | |
Corentin Wallez | d98b307 | 2021-05-05 17:37:43 +0000 | [diff] [blame] | 199 | if (mError != nullptr) { |
| 200 | return std::move(mError); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 201 | } |
Brandon Jones | f33afcd | 2021-10-28 00:13:17 +0000 | [diff] [blame] | 202 | DAWN_INVALID_IF(currentEncoder != topLevelEncoder, |
| 203 | "Command buffer recording ended before %s was ended.", currentEncoder); |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 204 | return {}; |
| 205 | } |
| 206 | |
Ken Rockot | ebf183b | 2021-09-23 00:15:19 +0000 | [diff] [blame] | 207 | void EncodingContext::CommitCommands(CommandAllocator allocator) { |
| 208 | if (!allocator.IsEmpty()) { |
| 209 | mAllocators.push_back(std::move(allocator)); |
| 210 | } |
| 211 | } |
| 212 | |
Austin Eng | fde9490 | 2019-07-24 18:15:24 +0000 | [diff] [blame] | 213 | bool EncodingContext::IsFinished() const { |
| 214 | return mTopLevelEncoder == nullptr; |
| 215 | } |
| 216 | |
| 217 | } // namespace dawn_native |