Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -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 | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 15 | #include "backend/CommandAllocator.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 17 | #include "common/Assert.h" |
Corentin Wallez | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 18 | #include "common/Math.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 19 | |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 21 | #include <climits> |
| 22 | #include <cstdlib> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 23 | |
| 24 | namespace backend { |
| 25 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 26 | constexpr uint32_t EndOfBlock = UINT_MAX; // std::numeric_limits<uint32_t>::max(); |
| 27 | constexpr uint32_t AdditionalData = UINT_MAX - 1; // std::numeric_limits<uint32_t>::max() - 1; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 28 | |
| 29 | // TODO(cwallez@chromium.org): figure out a way to have more type safety for the iterator |
| 30 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 31 | CommandIterator::CommandIterator() : mEndOfBlock(EndOfBlock) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 32 | Reset(); |
| 33 | } |
| 34 | |
| 35 | CommandIterator::~CommandIterator() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 36 | ASSERT(mDataWasDestroyed); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 37 | |
| 38 | if (!IsEmpty()) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 39 | for (auto& block : mBlocks) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 40 | free(block.block); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 45 | CommandIterator::CommandIterator(CommandIterator&& other) : mEndOfBlock(EndOfBlock) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 46 | if (!other.IsEmpty()) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 47 | mBlocks = std::move(other.mBlocks); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | other.Reset(); |
| 49 | } |
| 50 | other.DataWasDestroyed(); |
| 51 | Reset(); |
| 52 | } |
| 53 | |
| 54 | CommandIterator& CommandIterator::operator=(CommandIterator&& other) { |
| 55 | if (!other.IsEmpty()) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 56 | mBlocks = std::move(other.mBlocks); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 57 | other.Reset(); |
| 58 | } else { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 59 | mBlocks.clear(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 60 | } |
| 61 | other.DataWasDestroyed(); |
| 62 | Reset(); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | CommandIterator::CommandIterator(CommandAllocator&& allocator) |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 67 | : mBlocks(allocator.AcquireBlocks()), mEndOfBlock(EndOfBlock) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | Reset(); |
| 69 | } |
| 70 | |
| 71 | CommandIterator& CommandIterator::operator=(CommandAllocator&& allocator) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 72 | mBlocks = allocator.AcquireBlocks(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 73 | Reset(); |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | void CommandIterator::Reset() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 78 | mCurrentBlock = 0; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 79 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 80 | if (mBlocks.empty()) { |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 81 | // This will case the first NextCommandId call to try to move to the next block and stop |
| 82 | // the iteration immediately, without special casing the initialization. |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 83 | mCurrentPtr = reinterpret_cast<uint8_t*>(&mEndOfBlock); |
| 84 | mBlocks.emplace_back(); |
| 85 | mBlocks[0].size = sizeof(mEndOfBlock); |
| 86 | mBlocks[0].block = mCurrentPtr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 87 | } else { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 88 | mCurrentPtr = AlignPtr(mBlocks[0].block, alignof(uint32_t)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | void CommandIterator::DataWasDestroyed() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 93 | mDataWasDestroyed = true; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool CommandIterator::IsEmpty() const { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 97 | return mBlocks[0].block == reinterpret_cast<const uint8_t*>(&mEndOfBlock); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool CommandIterator::NextCommandId(uint32_t* commandId) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 101 | uint8_t* idPtr = AlignPtr(mCurrentPtr, alignof(uint32_t)); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 102 | ASSERT(idPtr + sizeof(uint32_t) <= |
| 103 | mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 104 | |
| 105 | uint32_t id = *reinterpret_cast<uint32_t*>(idPtr); |
| 106 | |
| 107 | if (id == EndOfBlock) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 108 | mCurrentBlock++; |
| 109 | if (mCurrentBlock >= mBlocks.size()) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 110 | Reset(); |
Corentin Wallez | 770f25f | 2017-07-20 14:30:04 -0400 | [diff] [blame] | 111 | *commandId = EndOfBlock; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | return false; |
| 113 | } |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 114 | mCurrentPtr = AlignPtr(mBlocks[mCurrentBlock].block, alignof(uint32_t)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 115 | return NextCommandId(commandId); |
| 116 | } |
| 117 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 118 | mCurrentPtr = idPtr + sizeof(uint32_t); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 119 | *commandId = id; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | void* CommandIterator::NextCommand(size_t commandSize, size_t commandAlignment) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 124 | uint8_t* commandPtr = AlignPtr(mCurrentPtr, commandAlignment); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 125 | ASSERT(commandPtr + sizeof(commandSize) <= |
| 126 | mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 127 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 128 | mCurrentPtr = commandPtr + commandSize; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 129 | return commandPtr; |
| 130 | } |
| 131 | |
| 132 | void* CommandIterator::NextData(size_t dataSize, size_t dataAlignment) { |
| 133 | uint32_t id; |
| 134 | bool hasId = NextCommandId(&id); |
| 135 | ASSERT(hasId); |
| 136 | ASSERT(id == AdditionalData); |
| 137 | |
| 138 | return NextCommand(dataSize, dataAlignment); |
| 139 | } |
| 140 | |
| 141 | // Potential TODO(cwallez@chromium.org): |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 142 | // - Host the size and pointer to next block in the block itself to avoid having an allocation |
| 143 | // in the vector |
| 144 | // - Assume T's alignof is, say 64bits, static assert it, and make commandAlignment a constant |
| 145 | // in Allocate |
| 146 | // - Be able to optimize allocation to one block, for command buffers expected to live long to |
| 147 | // avoid cache misses |
Corentin Wallez | 9fc6534 | 2018-07-18 15:28:38 +0200 | [diff] [blame^] | 148 | // - Better block allocation, maybe have Dawn API to say command buffer is going to have size |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 149 | // close to another |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 150 | |
| 151 | CommandAllocator::CommandAllocator() |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 152 | : mCurrentPtr(reinterpret_cast<uint8_t*>(&mDummyEnum[0])), |
| 153 | mEndPtr(reinterpret_cast<uint8_t*>(&mDummyEnum[1])) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CommandAllocator::~CommandAllocator() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 157 | ASSERT(mBlocks.empty()); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | CommandBlocks&& CommandAllocator::AcquireBlocks() { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 161 | ASSERT(mCurrentPtr != nullptr && mEndPtr != nullptr); |
| 162 | ASSERT(IsPtrAligned(mCurrentPtr, alignof(uint32_t))); |
| 163 | ASSERT(mCurrentPtr + sizeof(uint32_t) <= mEndPtr); |
| 164 | *reinterpret_cast<uint32_t*>(mCurrentPtr) = EndOfBlock; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 165 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 166 | mCurrentPtr = nullptr; |
| 167 | mEndPtr = nullptr; |
| 168 | return std::move(mBlocks); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 171 | uint8_t* CommandAllocator::Allocate(uint32_t commandId, |
| 172 | size_t commandSize, |
| 173 | size_t commandAlignment) { |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 174 | ASSERT(mCurrentPtr != nullptr); |
| 175 | ASSERT(mEndPtr != nullptr); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 176 | ASSERT(commandId != EndOfBlock); |
| 177 | |
| 178 | // It should always be possible to allocate one id, for EndOfBlock tagging, |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 179 | ASSERT(IsPtrAligned(mCurrentPtr, alignof(uint32_t))); |
| 180 | ASSERT(mCurrentPtr + sizeof(uint32_t) <= mEndPtr); |
| 181 | uint32_t* idAlloc = reinterpret_cast<uint32_t*>(mCurrentPtr); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 182 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 183 | uint8_t* commandAlloc = AlignPtr(mCurrentPtr + sizeof(uint32_t), commandAlignment); |
Austin Eng | 8867e5d | 2017-07-14 18:53:07 -0400 | [diff] [blame] | 184 | uint8_t* nextPtr = AlignPtr(commandAlloc + commandSize, alignof(uint32_t)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 185 | |
| 186 | // When there is not enough space, we signal the EndOfBlock, so that the iterator nows to |
| 187 | // move to the next one. EndOfBlock on the last block means the end of the commands. |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 188 | if (nextPtr + sizeof(uint32_t) > mEndPtr) { |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 189 | // Even if we are not able to get another block, the list of commands will be |
| 190 | // well-formed and iterable as this block will be that last one. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 191 | *idAlloc = EndOfBlock; |
| 192 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 193 | // Make sure we have space for current allocation, plus end of block and alignment |
| 194 | // padding for the first id. |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 195 | ASSERT(nextPtr > mCurrentPtr); |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 196 | if (!GetNewBlock(static_cast<size_t>(nextPtr - mCurrentPtr) + sizeof(uint32_t) + |
| 197 | alignof(uint32_t))) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | return Allocate(commandId, commandSize, commandAlignment); |
| 201 | } |
| 202 | |
| 203 | *idAlloc = commandId; |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 204 | mCurrentPtr = nextPtr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 205 | return commandAlloc; |
| 206 | } |
| 207 | |
| 208 | uint8_t* CommandAllocator::AllocateData(size_t commandSize, size_t commandAlignment) { |
| 209 | return Allocate(AdditionalData, commandSize, commandAlignment); |
| 210 | } |
| 211 | |
| 212 | bool CommandAllocator::GetNewBlock(size_t minimumSize) { |
| 213 | // Allocate blocks doubling sizes each time, to a maximum of 16k (or at least minimumSize). |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 214 | mLastAllocationSize = |
| 215 | std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384))); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 216 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 217 | uint8_t* block = reinterpret_cast<uint8_t*>(malloc(mLastAllocationSize)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 218 | if (block == nullptr) { |
| 219 | return false; |
| 220 | } |
| 221 | |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 222 | mBlocks.push_back({mLastAllocationSize, block}); |
| 223 | mCurrentPtr = AlignPtr(block, alignof(uint32_t)); |
| 224 | mEndPtr = block + mLastAllocationSize; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 228 | } // namespace backend |