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