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 | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 15 | #include "dawn_native/Buffer.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" |
Jiawei Shao | c11a191 | 2020-07-28 01:58:50 +0000 | [diff] [blame] | 18 | #include "dawn_native/Commands.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 19 | #include "dawn_native/Device.h" |
Bryan Bernhart | 67a73bd | 2019-02-15 21:18:40 +0000 | [diff] [blame] | 20 | #include "dawn_native/DynamicUploader.h" |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 21 | #include "dawn_native/ErrorData.h" |
Natasha Lee | 949f1e4 | 2020-05-19 01:29:32 +0000 | [diff] [blame] | 22 | #include "dawn_native/MapRequestTracker.h" |
Corentin Wallez | 47a3341 | 2020-06-02 09:24:39 +0000 | [diff] [blame] | 23 | #include "dawn_native/Queue.h" |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 24 | #include "dawn_native/ValidationUtils_autogen.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 25 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 26 | #include <cstdio> |
Stephen White | 68d97ad | 2019-07-23 17:04:34 +0000 | [diff] [blame] | 27 | #include <cstring> |
Corentin Wallez | c1400f0 | 2017-11-24 13:59:42 -0500 | [diff] [blame] | 28 | #include <utility> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 29 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 30 | namespace dawn_native { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
Rafael Cintron | c64242d | 2020-04-06 18:20:02 +0000 | [diff] [blame] | 34 | class ErrorBuffer final : public BufferBase { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 35 | public: |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 36 | ErrorBuffer(DeviceBase* device, const BufferDescriptor* descriptor) |
| 37 | : BufferBase(device, descriptor, ObjectBase::kError) { |
| 38 | if (descriptor->mappedAtCreation) { |
| 39 | // Check that the size can be used to allocate an mFakeMappedData. A malloc(0) |
| 40 | // is invalid, and on 32bit systems we should avoid a narrowing conversion that |
| 41 | // would make size = 1 << 32 + 1 allocate one byte. |
| 42 | bool isValidSize = |
| 43 | descriptor->size != 0 && |
| 44 | descriptor->size < uint64_t(std::numeric_limits<size_t>::max()); |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 45 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 46 | if (isValidSize) { |
| 47 | mFakeMappedData = std::unique_ptr<uint8_t[]>(new (std::nothrow) |
| 48 | uint8_t[descriptor->size]); |
| 49 | } |
| 50 | } |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 53 | void ClearMappedData() { |
| 54 | mFakeMappedData.reset(); |
| 55 | } |
| 56 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 57 | private: |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 58 | bool IsCPUWritableAtCreation() const override { |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 59 | UNREACHABLE(); |
| 60 | return false; |
| 61 | } |
| 62 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 63 | MaybeError MapAtCreationImpl() override { |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 64 | UNREACHABLE(); |
| 65 | return {}; |
| 66 | } |
| 67 | |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 68 | MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) override { |
| 69 | UNREACHABLE(); |
| 70 | return {}; |
| 71 | } |
Natasha Lee | 949f1e4 | 2020-05-19 01:29:32 +0000 | [diff] [blame] | 72 | void* GetMappedPointerImpl() override { |
| 73 | return mFakeMappedData.get(); |
| 74 | } |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 75 | void UnmapImpl() override { |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 76 | UNREACHABLE(); |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 77 | } |
Natasha Lee | 718e1db | 2019-03-11 17:05:22 +0000 | [diff] [blame] | 78 | void DestroyImpl() override { |
| 79 | UNREACHABLE(); |
| 80 | } |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 81 | |
| 82 | std::unique_ptr<uint8_t[]> mFakeMappedData; |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // anonymous namespace |
| 86 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 87 | MaybeError ValidateBufferDescriptor(DeviceBase*, const BufferDescriptor* descriptor) { |
Corentin Wallez | 6fee61c | 2018-09-10 16:17:24 +0200 | [diff] [blame] | 88 | if (descriptor->nextInChain != nullptr) { |
| 89 | return DAWN_VALIDATION_ERROR("nextInChain must be nullptr"); |
| 90 | } |
| 91 | |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 92 | DAWN_TRY(ValidateBufferUsage(descriptor->usage)); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 93 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 94 | wgpu::BufferUsage usage = descriptor->usage; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 95 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 96 | const wgpu::BufferUsage kMapWriteAllowedUsages = |
| 97 | wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc; |
| 98 | if (usage & wgpu::BufferUsage::MapWrite && (usage & kMapWriteAllowedUsages) != usage) { |
Corentin Wallez | ec05355 | 2019-07-08 10:05:46 +0000 | [diff] [blame] | 99 | return DAWN_VALIDATION_ERROR("Only CopySrc is allowed with MapWrite"); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 102 | const wgpu::BufferUsage kMapReadAllowedUsages = |
| 103 | wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; |
| 104 | if (usage & wgpu::BufferUsage::MapRead && (usage & kMapReadAllowedUsages) != usage) { |
Corentin Wallez | ec05355 | 2019-07-08 10:05:46 +0000 | [diff] [blame] | 105 | return DAWN_VALIDATION_ERROR("Only CopyDst is allowed with MapRead"); |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 108 | if (descriptor->mappedAtCreation && descriptor->size % 4 != 0) { |
| 109 | return DAWN_VALIDATION_ERROR("size must be aligned to 4 when mappedAtCreation is true"); |
| 110 | } |
| 111 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 112 | return {}; |
| 113 | } |
| 114 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 115 | // Buffer |
| 116 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 117 | BufferBase::BufferBase(DeviceBase* device, const BufferDescriptor* descriptor) |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 118 | : ObjectBase(device), |
| 119 | mSize(descriptor->size), |
| 120 | mUsage(descriptor->usage), |
| 121 | mState(BufferState::Unmapped) { |
Yunchao He | 64cfaea | 2019-11-07 07:09:07 +0000 | [diff] [blame] | 122 | // Add readonly storage usage if the buffer has a storage usage. The validation rules in |
Austin Eng | 4b0b7a5 | 2019-11-21 22:09:41 +0000 | [diff] [blame] | 123 | // ValidatePassResourceUsage will make sure we don't use both at the same |
Yunchao He | 64cfaea | 2019-11-07 07:09:07 +0000 | [diff] [blame] | 124 | // time. |
| 125 | if (mUsage & wgpu::BufferUsage::Storage) { |
Jiawei Shao | e89b487 | 2020-04-21 00:48:10 +0000 | [diff] [blame] | 126 | mUsage |= kReadOnlyStorageBuffer; |
Yunchao He | 64cfaea | 2019-11-07 07:09:07 +0000 | [diff] [blame] | 127 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 130 | BufferBase::BufferBase(DeviceBase* device, |
| 131 | const BufferDescriptor* descriptor, |
| 132 | ObjectBase::ErrorTag tag) |
| 133 | : ObjectBase(device, tag), mSize(descriptor->size), mState(BufferState::Unmapped) { |
| 134 | if (descriptor->mappedAtCreation) { |
| 135 | mState = BufferState::MappedAtCreation; |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 136 | mMapOffset = 0; |
| 137 | mMapSize = mSize; |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 138 | } |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 141 | BufferBase::~BufferBase() { |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 142 | if (mState == BufferState::Mapped) { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 143 | ASSERT(!IsError()); |
Corentin Wallez | 2088cde | 2020-08-12 19:32:25 +0000 | [diff] [blame] | 144 | CallMapCallback(mMapSerial, WGPUBufferMapAsyncStatus_Unknown); |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 148 | // static |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 149 | BufferBase* BufferBase::MakeError(DeviceBase* device, const BufferDescriptor* descriptor) { |
| 150 | return new ErrorBuffer(device, descriptor); |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 153 | uint64_t BufferBase::GetSize() const { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 154 | ASSERT(!IsError()); |
Corentin Wallez | fbecc28 | 2017-11-23 10:32:51 -0800 | [diff] [blame] | 155 | return mSize; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 158 | wgpu::BufferUsage BufferBase::GetUsage() const { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 159 | ASSERT(!IsError()); |
Corentin Wallez | 62c7743 | 2018-08-22 15:08:02 +0200 | [diff] [blame] | 160 | return mUsage; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 163 | MaybeError BufferBase::MapAtCreation() { |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 164 | DAWN_TRY(MapAtCreationInternal()); |
| 165 | |
| 166 | // TODO(jiawei.shao@intel.com): check Toggle::LazyClearResourceOnFirstUse instead when |
| 167 | // buffer lazy initialization is completely supported. |
| 168 | DeviceBase* device = GetDevice(); |
| 169 | if (device->IsToggleEnabled(Toggle::LazyClearBufferOnFirstUse)) { |
| 170 | memset(GetMappedRange(0, mSize), uint8_t(0u), mSize); |
| 171 | SetIsDataInitialized(); |
| 172 | device->IncrementLazyClearCountForTesting(); |
| 173 | } else if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) { |
| 174 | memset(GetMappedRange(0, mSize), uint8_t(1u), mSize); |
| 175 | } |
| 176 | |
| 177 | return {}; |
| 178 | } |
| 179 | |
| 180 | MaybeError BufferBase::MapAtCreationInternal() { |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 181 | ASSERT(!IsError()); |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 182 | mState = BufferState::MappedAtCreation; |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 183 | mMapOffset = 0; |
| 184 | mMapSize = mSize; |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 185 | |
Corentin Wallez | 91904cd | 2020-06-30 12:21:54 +0000 | [diff] [blame] | 186 | // 0-sized buffers are not supposed to be written to, Return back any non-null pointer. |
| 187 | // Handle 0-sized buffers first so we don't try to map them in the backend. |
| 188 | if (mSize == 0) { |
Corentin Wallez | 91904cd | 2020-06-30 12:21:54 +0000 | [diff] [blame] | 189 | return {}; |
| 190 | } |
| 191 | |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 192 | // Mappable buffers don't use a staging buffer and are just as if mapped through MapAsync. |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 193 | if (IsCPUWritableAtCreation()) { |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 194 | DAWN_TRY(MapAtCreationImpl()); |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 195 | } else { |
| 196 | // If any of these fail, the buffer will be deleted and replaced with an |
| 197 | // error buffer. |
| 198 | // TODO(enga): Suballocate and reuse memory from a larger staging buffer so we don't |
| 199 | // create many small buffers. |
| 200 | DAWN_TRY_ASSIGN(mStagingBuffer, GetDevice()->CreateStagingBuffer(GetSize())); |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 203 | return {}; |
| 204 | } |
| 205 | |
Corentin Wallez | 47a3341 | 2020-06-02 09:24:39 +0000 | [diff] [blame] | 206 | MaybeError BufferBase::ValidateCanUseOnQueueNow() const { |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 207 | ASSERT(!IsError()); |
| 208 | |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 209 | switch (mState) { |
| 210 | case BufferState::Destroyed: |
| 211 | return DAWN_VALIDATION_ERROR("Destroyed buffer used in a submit"); |
| 212 | case BufferState::Mapped: |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 213 | case BufferState::MappedAtCreation: |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 214 | return DAWN_VALIDATION_ERROR("Buffer used in a submit while mapped"); |
| 215 | case BufferState::Unmapped: |
| 216 | return {}; |
Corentin Wallez | e51f8dd | 2020-02-18 02:44:05 +0000 | [diff] [blame] | 217 | default: |
| 218 | UNREACHABLE(); |
Corentin Wallez | 679ff4e | 2018-11-07 10:02:43 +0000 | [diff] [blame] | 219 | } |
Corentin Wallez | 679ff4e | 2018-11-07 10:02:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 222 | void BufferBase::CallMapCallback(uint32_t serial, WGPUBufferMapAsyncStatus status) { |
| 223 | ASSERT(!IsError()); |
| 224 | if (mMapCallback != nullptr && serial == mMapSerial) { |
| 225 | // Tag the callback as fired before firing it, otherwise it could fire a second time if |
| 226 | // for example buffer.Unmap() is called inside the application-provided callback. |
| 227 | WGPUBufferMapCallback callback = mMapCallback; |
| 228 | mMapCallback = nullptr; |
| 229 | |
| 230 | if (GetDevice()->IsLost()) { |
| 231 | callback(WGPUBufferMapAsyncStatus_DeviceLost, mMapUserdata); |
| 232 | } else { |
| 233 | callback(status, mMapUserdata); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 238 | void BufferBase::MapAsync(wgpu::MapMode mode, |
| 239 | size_t offset, |
| 240 | size_t size, |
| 241 | WGPUBufferMapCallback callback, |
| 242 | void* userdata) { |
| 243 | // Handle the defaulting of size required by WebGPU, even if in webgpu_cpp.h it is not |
| 244 | // possible to default the function argument (because there is the callback later in the |
| 245 | // argument list) |
| 246 | if (size == 0 && offset < mSize) { |
| 247 | size = mSize - offset; |
| 248 | } |
| 249 | |
| 250 | WGPUBufferMapAsyncStatus status; |
| 251 | if (GetDevice()->ConsumedError(ValidateMapAsync(mode, offset, size, &status))) { |
| 252 | if (callback) { |
| 253 | callback(status, userdata); |
| 254 | } |
| 255 | return; |
| 256 | } |
| 257 | ASSERT(!IsError()); |
| 258 | |
| 259 | // TODO(cwallez@chromium.org): what to do on wraparound? Could cause crashes. |
| 260 | mMapSerial++; |
| 261 | mMapMode = mode; |
| 262 | mMapOffset = offset; |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 263 | mMapSize = size; |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 264 | mMapCallback = callback; |
| 265 | mMapUserdata = userdata; |
| 266 | mState = BufferState::Mapped; |
| 267 | |
| 268 | if (GetDevice()->ConsumedError(MapAsyncImpl(mode, offset, size))) { |
| 269 | CallMapCallback(mMapSerial, WGPUBufferMapAsyncStatus_DeviceLost); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | MapRequestTracker* tracker = GetDevice()->GetMapRequestTracker(); |
Corentin Wallez | f7123d7 | 2020-08-20 14:22:29 +0000 | [diff] [blame] | 274 | tracker->Track(this, mMapSerial); |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 277 | void* BufferBase::GetMappedRange(size_t offset, size_t size) { |
| 278 | return GetMappedRangeInternal(true, offset, size); |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 281 | const void* BufferBase::GetConstMappedRange(size_t offset, size_t size) { |
| 282 | return GetMappedRangeInternal(false, offset, size); |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 285 | void* BufferBase::GetMappedRangeInternal(bool writable, size_t offset, size_t size) { |
| 286 | if (!CanGetMappedRange(writable, offset, size)) { |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 287 | return nullptr; |
| 288 | } |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 289 | |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 290 | if (mStagingBuffer != nullptr) { |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 291 | return static_cast<uint8_t*>(mStagingBuffer->GetMappedPointer()) + offset; |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 292 | } |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 293 | if (mSize == 0) { |
| 294 | return reinterpret_cast<uint8_t*>(intptr_t(0xCAFED00D)); |
| 295 | } |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 296 | return static_cast<uint8_t*>(GetMappedPointerImpl()) + offset; |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 299 | void BufferBase::Destroy() { |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 300 | if (IsError()) { |
| 301 | // It is an error to call Destroy() on an ErrorBuffer, but we still need to reclaim the |
| 302 | // fake mapped staging data. |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 303 | static_cast<ErrorBuffer*>(this)->ClearMappedData(); |
| 304 | mState = BufferState::Destroyed; |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 305 | } |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 306 | if (GetDevice()->ConsumedError(ValidateDestroy())) { |
| 307 | return; |
| 308 | } |
| 309 | ASSERT(!IsError()); |
| 310 | |
| 311 | if (mState == BufferState::Mapped) { |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 312 | Unmap(); |
| 313 | } else if (mState == BufferState::MappedAtCreation) { |
| 314 | if (mStagingBuffer != nullptr) { |
| 315 | mStagingBuffer.reset(); |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 316 | } else if (mSize != 0) { |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 317 | ASSERT(IsCPUWritableAtCreation()); |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 318 | Unmap(); |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 319 | } |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 320 | } |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 321 | |
Natasha Lee | 20b0c33 | 2019-04-01 19:49:04 +0000 | [diff] [blame] | 322 | DestroyInternal(); |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 325 | MaybeError BufferBase::CopyFromStagingBuffer() { |
| 326 | ASSERT(mStagingBuffer); |
Corentin Wallez | 45aed83 | 2020-06-05 15:44:03 +0000 | [diff] [blame] | 327 | if (GetSize() == 0) { |
| 328 | return {}; |
| 329 | } |
| 330 | |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 331 | DAWN_TRY(GetDevice()->CopyFromStagingToBuffer(mStagingBuffer.get(), 0, this, 0, GetSize())); |
| 332 | |
Bryan Bernhart | 450e212 | 2019-09-18 22:06:41 +0000 | [diff] [blame] | 333 | DynamicUploader* uploader = GetDevice()->GetDynamicUploader(); |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 334 | uploader->ReleaseStagingBuffer(std::move(mStagingBuffer)); |
| 335 | |
| 336 | return {}; |
| 337 | } |
| 338 | |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 339 | void BufferBase::Unmap() { |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 340 | if (IsError()) { |
| 341 | // It is an error to call Unmap() on an ErrorBuffer, but we still need to reclaim the |
| 342 | // fake mapped staging data. |
Corentin Wallez | b2ea191 | 2020-07-07 11:21:51 +0000 | [diff] [blame] | 343 | static_cast<ErrorBuffer*>(this)->ClearMappedData(); |
| 344 | mState = BufferState::Unmapped; |
Austin Eng | 740995c | 2019-05-15 18:55:22 +0000 | [diff] [blame] | 345 | } |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 346 | if (GetDevice()->ConsumedError(ValidateUnmap())) { |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 347 | return; |
| 348 | } |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 349 | ASSERT(!IsError()); |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 350 | |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 351 | if (mState == BufferState::Mapped) { |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 352 | // A map request can only be called once, so this will fire only if the request wasn't |
| 353 | // completed before the Unmap. |
| 354 | // Callbacks are not fired if there is no callback registered, so this is correct for |
Corentin Wallez | f7123d7 | 2020-08-20 14:22:29 +0000 | [diff] [blame] | 355 | // mappedAtCreation = true. |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 356 | CallMapCallback(mMapSerial, WGPUBufferMapAsyncStatus_Unknown); |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 357 | UnmapImpl(); |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 358 | |
Corentin Wallez | 2088cde | 2020-08-12 19:32:25 +0000 | [diff] [blame] | 359 | mMapCallback = nullptr; |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 360 | mMapUserdata = 0; |
| 361 | |
| 362 | } else if (mState == BufferState::MappedAtCreation) { |
| 363 | if (mStagingBuffer != nullptr) { |
| 364 | GetDevice()->ConsumedError(CopyFromStagingBuffer()); |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 365 | } else if (mSize != 0) { |
Jiawei Shao | 1c4a7f7 | 2020-09-01 08:08:57 +0000 | [diff] [blame^] | 366 | ASSERT(IsCPUWritableAtCreation()); |
Corentin Wallez | 2008d15 | 2020-06-30 11:05:44 +0000 | [diff] [blame] | 367 | UnmapImpl(); |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 368 | } |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 369 | } |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 370 | |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 371 | mState = BufferState::Unmapped; |
Corentin Wallez | b1c19ee | 2017-06-09 10:51:29 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Natasha Lee | 74f5054 | 2020-01-28 22:18:58 +0000 | [diff] [blame] | 374 | MaybeError BufferBase::ValidateMap(wgpu::BufferUsage requiredUsage, |
| 375 | WGPUBufferMapAsyncStatus* status) const { |
| 376 | *status = WGPUBufferMapAsyncStatus_DeviceLost; |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 377 | DAWN_TRY(GetDevice()->ValidateIsAlive()); |
Natasha Lee | 74f5054 | 2020-01-28 22:18:58 +0000 | [diff] [blame] | 378 | |
| 379 | *status = WGPUBufferMapAsyncStatus_Error; |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 380 | DAWN_TRY(GetDevice()->ValidateObject(this)); |
| 381 | |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 382 | switch (mState) { |
| 383 | case BufferState::Mapped: |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 384 | case BufferState::MappedAtCreation: |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 385 | return DAWN_VALIDATION_ERROR("Buffer is already mapped"); |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 386 | case BufferState::Destroyed: |
| 387 | return DAWN_VALIDATION_ERROR("Buffer is destroyed"); |
| 388 | case BufferState::Unmapped: |
| 389 | break; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 390 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 391 | |
Corentin Wallez | 62c7743 | 2018-08-22 15:08:02 +0200 | [diff] [blame] | 392 | if (!(mUsage & requiredUsage)) { |
Corentin Wallez | 6fee61c | 2018-09-10 16:17:24 +0200 | [diff] [blame] | 393 | return DAWN_VALIDATION_ERROR("Buffer needs the correct map usage bit"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 394 | } |
Corentin Wallez | d8c068f | 2018-07-09 15:15:07 +0200 | [diff] [blame] | 395 | |
Natasha Lee | 74f5054 | 2020-01-28 22:18:58 +0000 | [diff] [blame] | 396 | *status = WGPUBufferMapAsyncStatus_Success; |
Corentin Wallez | 7914958 | 2018-07-18 21:20:07 +0200 | [diff] [blame] | 397 | return {}; |
| 398 | } |
| 399 | |
Corentin Wallez | 0d52f80 | 2020-07-14 12:30:14 +0000 | [diff] [blame] | 400 | MaybeError BufferBase::ValidateMapAsync(wgpu::MapMode mode, |
| 401 | size_t offset, |
| 402 | size_t size, |
| 403 | WGPUBufferMapAsyncStatus* status) const { |
| 404 | *status = WGPUBufferMapAsyncStatus_DeviceLost; |
| 405 | DAWN_TRY(GetDevice()->ValidateIsAlive()); |
| 406 | |
| 407 | *status = WGPUBufferMapAsyncStatus_Error; |
| 408 | DAWN_TRY(GetDevice()->ValidateObject(this)); |
| 409 | |
| 410 | if (offset % 4 != 0) { |
| 411 | return DAWN_VALIDATION_ERROR("offset must be a multiple of 4"); |
| 412 | } |
| 413 | |
| 414 | if (size % 4 != 0) { |
| 415 | return DAWN_VALIDATION_ERROR("size must be a multiple of 4"); |
| 416 | } |
| 417 | |
| 418 | if (uint64_t(offset) > mSize || uint64_t(size) > mSize - uint64_t(offset)) { |
| 419 | return DAWN_VALIDATION_ERROR("size + offset must fit in the buffer"); |
| 420 | } |
| 421 | |
| 422 | switch (mState) { |
| 423 | case BufferState::Mapped: |
| 424 | case BufferState::MappedAtCreation: |
| 425 | return DAWN_VALIDATION_ERROR("Buffer is already mapped"); |
| 426 | case BufferState::Destroyed: |
| 427 | return DAWN_VALIDATION_ERROR("Buffer is destroyed"); |
| 428 | case BufferState::Unmapped: |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | bool isReadMode = mode & wgpu::MapMode::Read; |
| 433 | bool isWriteMode = mode & wgpu::MapMode::Write; |
| 434 | if (!(isReadMode ^ isWriteMode)) { |
| 435 | return DAWN_VALIDATION_ERROR("Exactly one of Read or Write mode must be set"); |
| 436 | } |
| 437 | |
| 438 | if (mode & wgpu::MapMode::Read) { |
| 439 | if (!(mUsage & wgpu::BufferUsage::MapRead)) { |
| 440 | return DAWN_VALIDATION_ERROR("The buffer must have the MapRead usage"); |
| 441 | } |
| 442 | } else { |
| 443 | ASSERT(mode & wgpu::MapMode::Write); |
| 444 | |
| 445 | if (!(mUsage & wgpu::BufferUsage::MapWrite)) { |
| 446 | return DAWN_VALIDATION_ERROR("The buffer must have the MapWrite usage"); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | *status = WGPUBufferMapAsyncStatus_Success; |
| 451 | return {}; |
| 452 | } |
| 453 | |
Corentin Wallez | f6e7044 | 2020-07-17 18:50:37 +0000 | [diff] [blame] | 454 | bool BufferBase::CanGetMappedRange(bool writable, size_t offset, size_t size) const { |
| 455 | if (size > mMapSize || offset < mMapOffset) { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | size_t offsetInMappedRange = offset - mMapOffset; |
| 460 | if (offsetInMappedRange > mMapSize - size) { |
| 461 | return false; |
| 462 | } |
| 463 | |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 464 | // Note that: |
| 465 | // |
| 466 | // - We don't check that the device is alive because the application can ask for the |
| 467 | // mapped pointer before it knows, and even Dawn knows, that the device was lost, and |
| 468 | // still needs to work properly. |
| 469 | // - We don't check that the object is alive because we need to return mapped pointers |
| 470 | // for error buffers too. |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 471 | |
| 472 | switch (mState) { |
| 473 | // Writeable Buffer::GetMappedRange is always allowed when mapped at creation. |
| 474 | case BufferState::MappedAtCreation: |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 475 | return true; |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 476 | |
| 477 | case BufferState::Mapped: |
Corentin Wallez | ce78ce2 | 2020-08-22 11:08:34 +0000 | [diff] [blame] | 478 | ASSERT(bool(mMapMode & wgpu::MapMode::Read) ^ |
| 479 | bool(mMapMode & wgpu::MapMode::Write)); |
| 480 | return !writable || (mMapMode & wgpu::MapMode::Write); |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 481 | |
| 482 | case BufferState::Unmapped: |
| 483 | case BufferState::Destroyed: |
Corentin Wallez | dbf805f | 2020-07-06 18:08:10 +0000 | [diff] [blame] | 484 | return false; |
| 485 | |
Corentin Wallez | 1325ab1 | 2020-06-30 11:51:14 +0000 | [diff] [blame] | 486 | default: |
| 487 | UNREACHABLE(); |
| 488 | } |
| 489 | } |
| 490 | |
Corentin Wallez | 7914958 | 2018-07-18 21:20:07 +0200 | [diff] [blame] | 491 | MaybeError BufferBase::ValidateUnmap() const { |
Natasha Lee | 0ecc48e | 2020-01-15 19:02:13 +0000 | [diff] [blame] | 492 | DAWN_TRY(GetDevice()->ValidateIsAlive()); |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 493 | DAWN_TRY(GetDevice()->ValidateObject(this)); |
| 494 | |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 495 | switch (mState) { |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 496 | case BufferState::Mapped: |
Corentin Wallez | 13f4650 | 2020-06-11 11:07:05 +0000 | [diff] [blame] | 497 | case BufferState::MappedAtCreation: |
Corentin Wallez | f7123d7 | 2020-08-20 14:22:29 +0000 | [diff] [blame] | 498 | // A buffer may be in the Mapped state if it was created with mappedAtCreation |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 499 | // even if it did not have a mappable usage. |
| 500 | return {}; |
| 501 | case BufferState::Unmapped: |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 502 | if ((mUsage & (wgpu::BufferUsage::MapRead | wgpu::BufferUsage::MapWrite)) == 0) { |
Austin Eng | 9cd21f1 | 2019-06-05 18:35:31 +0000 | [diff] [blame] | 503 | return DAWN_VALIDATION_ERROR("Buffer does not have map usage"); |
| 504 | } |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 505 | return {}; |
| 506 | case BufferState::Destroyed: |
| 507 | return DAWN_VALIDATION_ERROR("Buffer is destroyed"); |
Corentin Wallez | e51f8dd | 2020-02-18 02:44:05 +0000 | [diff] [blame] | 508 | default: |
| 509 | UNREACHABLE(); |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
Corentin Wallez | 7914958 | 2018-07-18 21:20:07 +0200 | [diff] [blame] | 512 | |
Austin Eng | 446ab44 | 2019-02-13 21:26:48 +0000 | [diff] [blame] | 513 | MaybeError BufferBase::ValidateDestroy() const { |
| 514 | DAWN_TRY(GetDevice()->ValidateObject(this)); |
Corentin Wallez | 7914958 | 2018-07-18 21:20:07 +0200 | [diff] [blame] | 515 | return {}; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Natasha Lee | 20b0c33 | 2019-04-01 19:49:04 +0000 | [diff] [blame] | 518 | void BufferBase::DestroyInternal() { |
| 519 | if (mState != BufferState::Destroyed) { |
| 520 | DestroyImpl(); |
| 521 | } |
| 522 | mState = BufferState::Destroyed; |
| 523 | } |
| 524 | |
Corentin Wallez | f7123d7 | 2020-08-20 14:22:29 +0000 | [diff] [blame] | 525 | void BufferBase::OnMapCommandSerialFinished(uint32_t mapSerial) { |
| 526 | CallMapCallback(mapSerial, WGPUBufferMapAsyncStatus_Success); |
Natasha Lee | 949f1e4 | 2020-05-19 01:29:32 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Jiawei Shao | 80f927d | 2020-07-06 08:24:30 +0000 | [diff] [blame] | 529 | bool BufferBase::IsDataInitialized() const { |
| 530 | return mIsDataInitialized; |
| 531 | } |
| 532 | |
| 533 | void BufferBase::SetIsDataInitialized() { |
| 534 | mIsDataInitialized = true; |
| 535 | } |
| 536 | |
| 537 | bool BufferBase::IsFullBufferRange(uint64_t offset, uint64_t size) const { |
| 538 | return offset == 0 && size == GetSize(); |
| 539 | } |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 540 | } // namespace dawn_native |