1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_BUFFER_H_ 16 #define DAWNNATIVE_BUFFER_H_ 17 18 #include "dawn_native/Error.h" 19 #include "dawn_native/Forward.h" 20 #include "dawn_native/IntegerTypes.h" 21 #include "dawn_native/ObjectBase.h" 22 23 #include "dawn_native/dawn_platform.h" 24 25 #include <memory> 26 27 namespace dawn_native { 28 29 struct CopyTextureToBufferCmd; 30 31 enum class MapType : uint32_t; 32 33 MaybeError ValidateBufferDescriptor(DeviceBase* device, const BufferDescriptor* descriptor); 34 35 static constexpr wgpu::BufferUsage kReadOnlyBufferUsages = 36 wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Index | 37 wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Uniform | kReadOnlyStorageBuffer | 38 wgpu::BufferUsage::Indirect; 39 40 static constexpr wgpu::BufferUsage kMappableBufferUsages = 41 wgpu::BufferUsage::MapRead | wgpu::BufferUsage::MapWrite; 42 43 class BufferBase : public ApiObjectBase { 44 public: 45 enum class BufferState { 46 Unmapped, 47 Mapped, 48 MappedAtCreation, 49 Destroyed, 50 }; 51 BufferBase(DeviceBase* device, const BufferDescriptor* descriptor); 52 53 static BufferBase* MakeError(DeviceBase* device, const BufferDescriptor* descriptor); 54 55 ObjectType GetType() const override; 56 57 uint64_t GetSize() const; 58 uint64_t GetAllocatedSize() const; 59 wgpu::BufferUsage GetUsage() const; 60 61 MaybeError MapAtCreation(); 62 void OnMapRequestCompleted(MapRequestID mapID, WGPUBufferMapAsyncStatus status); 63 64 MaybeError ValidateCanUseOnQueueNow() const; 65 66 bool IsFullBufferRange(uint64_t offset, uint64_t size) const; 67 bool NeedsInitialization() const; 68 bool IsDataInitialized() const; 69 void SetIsDataInitialized(); 70 71 void* GetMappedRange(size_t offset, size_t size, bool writable = true); 72 void Unmap(); 73 74 // Dawn API 75 void APIMapAsync(wgpu::MapMode mode, 76 size_t offset, 77 size_t size, 78 WGPUBufferMapCallback callback, 79 void* userdata); 80 void* APIGetMappedRange(size_t offset, size_t size); 81 const void* APIGetConstMappedRange(size_t offset, size_t size); 82 void APIUnmap(); 83 void APIDestroy(); 84 85 protected: 86 BufferBase(DeviceBase* device, 87 const BufferDescriptor* descriptor, 88 ObjectBase::ErrorTag tag); 89 90 // Constructor used only for mocking and testing. 91 BufferBase(DeviceBase* device, BufferState state); 92 void DestroyImpl() override; 93 94 ~BufferBase() override; 95 96 MaybeError MapAtCreationInternal(); 97 98 uint64_t mAllocatedSize = 0; 99 100 private: 101 virtual MaybeError MapAtCreationImpl() = 0; 102 virtual MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) = 0; 103 virtual void UnmapImpl() = 0; 104 virtual void* GetMappedPointerImpl() = 0; 105 106 virtual bool IsCPUWritableAtCreation() const = 0; 107 MaybeError CopyFromStagingBuffer(); 108 void CallMapCallback(MapRequestID mapID, WGPUBufferMapAsyncStatus status); 109 110 MaybeError ValidateMapAsync(wgpu::MapMode mode, 111 size_t offset, 112 size_t size, 113 WGPUBufferMapAsyncStatus* status) const; 114 MaybeError ValidateUnmap() const; 115 bool CanGetMappedRange(bool writable, size_t offset, size_t size) const; 116 void UnmapInternal(WGPUBufferMapAsyncStatus callbackStatus); 117 118 uint64_t mSize = 0; 119 wgpu::BufferUsage mUsage = wgpu::BufferUsage::None; 120 BufferState mState; 121 bool mIsDataInitialized = false; 122 123 std::unique_ptr<StagingBufferBase> mStagingBuffer; 124 125 WGPUBufferMapCallback mMapCallback = nullptr; 126 void* mMapUserdata = 0; 127 MapRequestID mLastMapID = MapRequestID(0); 128 wgpu::MapMode mMapMode = wgpu::MapMode::None; 129 size_t mMapOffset = 0; 130 size_t mMapSize = 0; 131 }; 132 133 } // namespace dawn_native 134 135 #endif // DAWNNATIVE_BUFFER_H_ 136