2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-24 14:42:33 +00:00
|
|
|
#ifndef DAWNNATIVE_BUFFER_H_
|
|
|
|
#define DAWNNATIVE_BUFFER_H_
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Error.h"
|
|
|
|
#include "dawn_native/Forward.h"
|
2018-10-15 12:54:30 +00:00
|
|
|
#include "dawn_native/ObjectBase.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-25 15:03:23 +00:00
|
|
|
#include "dawn_native/dawn_platform.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-07-19 21:51:09 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-07-28 01:58:50 +00:00
|
|
|
struct CopyTextureToBufferCmd;
|
|
|
|
|
2020-07-14 12:30:14 +00:00
|
|
|
enum class MapType : uint32_t;
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
MaybeError ValidateBufferDescriptor(DeviceBase* device, const BufferDescriptor* descriptor);
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
static constexpr wgpu::BufferUsage kReadOnlyBufferUsages =
|
|
|
|
wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Index |
|
2020-04-21 00:48:10 +00:00
|
|
|
wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Uniform | kReadOnlyStorageBuffer;
|
2018-07-05 14:29:12 +00:00
|
|
|
|
2018-10-15 12:54:30 +00:00
|
|
|
class BufferBase : public ObjectBase {
|
2019-02-13 21:26:48 +00:00
|
|
|
enum class BufferState {
|
|
|
|
Unmapped,
|
|
|
|
Mapped,
|
2020-06-11 11:07:05 +00:00
|
|
|
MappedAtCreation,
|
2019-02-13 21:26:48 +00:00
|
|
|
Destroyed,
|
|
|
|
};
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
public:
|
2018-08-22 13:37:29 +00:00
|
|
|
BufferBase(DeviceBase* device, const BufferDescriptor* descriptor);
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-07-07 11:21:51 +00:00
|
|
|
static BufferBase* MakeError(DeviceBase* device, const BufferDescriptor* descriptor);
|
2019-02-13 13:09:18 +00:00
|
|
|
|
2019-06-05 18:35:31 +00:00
|
|
|
uint64_t GetSize() const;
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BufferUsage GetUsage() const;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-07-07 11:21:51 +00:00
|
|
|
MaybeError MapAtCreation();
|
2020-08-20 14:22:29 +00:00
|
|
|
void OnMapCommandSerialFinished(uint32_t mapSerial);
|
2019-05-15 18:55:22 +00:00
|
|
|
|
2020-06-02 09:24:39 +00:00
|
|
|
MaybeError ValidateCanUseOnQueueNow() const;
|
2018-11-07 10:02:43 +00:00
|
|
|
|
2020-07-06 08:24:30 +00:00
|
|
|
bool IsFullBufferRange(uint64_t offset, uint64_t size) const;
|
|
|
|
bool IsDataInitialized() const;
|
|
|
|
void SetIsDataInitialized();
|
|
|
|
|
2018-07-18 13:20:28 +00:00
|
|
|
// Dawn API
|
2020-07-14 12:30:14 +00:00
|
|
|
void MapAsync(wgpu::MapMode mode,
|
|
|
|
size_t offset,
|
|
|
|
size_t size,
|
|
|
|
WGPUBufferMapCallback callback,
|
|
|
|
void* userdata);
|
2020-07-17 18:50:37 +00:00
|
|
|
void* GetMappedRange(size_t offset, size_t size);
|
|
|
|
const void* GetConstMappedRange(size_t offset, size_t size);
|
2017-11-24 18:59:42 +00:00
|
|
|
void Unmap();
|
2019-02-13 21:26:48 +00:00
|
|
|
void Destroy();
|
2017-11-24 18:59:42 +00:00
|
|
|
|
|
|
|
protected:
|
2020-07-07 11:21:51 +00:00
|
|
|
BufferBase(DeviceBase* device,
|
|
|
|
const BufferDescriptor* descriptor,
|
|
|
|
ObjectBase::ErrorTag tag);
|
2020-04-06 18:20:02 +00:00
|
|
|
~BufferBase() override;
|
2019-02-13 13:09:18 +00:00
|
|
|
|
2019-04-01 19:49:04 +00:00
|
|
|
void DestroyInternal();
|
|
|
|
|
2020-07-06 08:24:30 +00:00
|
|
|
bool IsMapped() const;
|
|
|
|
|
2020-09-01 08:08:57 +00:00
|
|
|
MaybeError MapAtCreationInternal();
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
private:
|
2020-07-07 11:21:51 +00:00
|
|
|
virtual MaybeError MapAtCreationImpl() = 0;
|
2020-07-14 12:30:14 +00:00
|
|
|
virtual MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) = 0;
|
2017-11-24 18:59:42 +00:00
|
|
|
virtual void UnmapImpl() = 0;
|
2019-03-11 17:05:22 +00:00
|
|
|
virtual void DestroyImpl() = 0;
|
2020-05-19 01:29:32 +00:00
|
|
|
virtual void* GetMappedPointerImpl() = 0;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-09-01 08:08:57 +00:00
|
|
|
virtual bool IsCPUWritableAtCreation() const = 0;
|
2019-06-05 18:35:31 +00:00
|
|
|
MaybeError CopyFromStagingBuffer();
|
2020-07-17 18:50:37 +00:00
|
|
|
void* GetMappedRangeInternal(bool writable, size_t offset, size_t size);
|
2020-07-14 12:30:14 +00:00
|
|
|
void CallMapCallback(uint32_t serial, WGPUBufferMapAsyncStatus status);
|
2019-06-05 18:35:31 +00:00
|
|
|
|
2020-01-28 22:18:58 +00:00
|
|
|
MaybeError ValidateMap(wgpu::BufferUsage requiredUsage,
|
|
|
|
WGPUBufferMapAsyncStatus* status) const;
|
2020-07-14 12:30:14 +00:00
|
|
|
MaybeError ValidateMapAsync(wgpu::MapMode mode,
|
|
|
|
size_t offset,
|
|
|
|
size_t size,
|
|
|
|
WGPUBufferMapAsyncStatus* status) const;
|
2018-07-18 19:20:07 +00:00
|
|
|
MaybeError ValidateUnmap() const;
|
2019-02-13 21:26:48 +00:00
|
|
|
MaybeError ValidateDestroy() const;
|
2020-07-17 18:50:37 +00:00
|
|
|
bool CanGetMappedRange(bool writable, size_t offset, size_t size) const;
|
2018-03-21 00:56:39 +00:00
|
|
|
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t mSize = 0;
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BufferUsage mUsage = wgpu::BufferUsage::None;
|
2020-07-14 12:30:14 +00:00
|
|
|
BufferState mState;
|
|
|
|
bool mIsDataInitialized = false;
|
|
|
|
|
|
|
|
std::unique_ptr<StagingBufferBase> mStagingBuffer;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-07-14 12:30:14 +00:00
|
|
|
WGPUBufferMapCallback mMapCallback = nullptr;
|
2019-05-29 13:03:50 +00:00
|
|
|
void* mMapUserdata = 0;
|
2018-03-21 00:56:39 +00:00
|
|
|
uint32_t mMapSerial = 0;
|
2020-07-14 12:30:14 +00:00
|
|
|
wgpu::MapMode mMapMode = wgpu::MapMode::None;
|
|
|
|
size_t mMapOffset = 0;
|
2020-07-17 18:50:37 +00:00
|
|
|
size_t mMapSize = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:42:33 +00:00
|
|
|
#endif // DAWNNATIVE_BUFFER_H_
|