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