dawn-cmake/src/backend/Buffer.h

138 lines
4.5 KiB
C
Raw Normal View History

// Copyright 2017 The Dawn Authors
//
// 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.
#ifndef BACKEND_BUFFER_H_
#define BACKEND_BUFFER_H_
#include "backend/Builder.h"
2017-11-24 18:59:42 +00:00
#include "backend/Forward.h"
#include "backend/RefCounted.h"
#include "nxt/nxtcpp.h"
namespace backend {
static constexpr nxt::BufferUsageBit kReadOnlyBufferUsages =
nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferSrc |
nxt::BufferUsageBit::Index | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Uniform;
static constexpr nxt::BufferUsageBit kWritableBufferUsages = nxt::BufferUsageBit::MapWrite |
nxt::BufferUsageBit::TransferDst |
nxt::BufferUsageBit::Storage;
class BufferBase : public RefCounted {
2017-11-24 18:59:42 +00:00
public:
BufferBase(BufferBuilder* builder);
~BufferBase();
uint32_t GetSize() const;
nxt::BufferUsageBit GetAllowedUsage() const;
2017-12-04 23:06:02 +00:00
DeviceBase* GetDevice() const;
2017-11-24 18:59:42 +00:00
// NXT API
BufferViewBuilder* CreateBufferViewBuilder();
void SetSubData(uint32_t start, uint32_t count, const uint8_t* data);
2017-11-24 18:59:42 +00:00
void MapReadAsync(uint32_t start,
uint32_t size,
nxtBufferMapReadCallback callback,
nxtCallbackUserdata userdata);
void MapWriteAsync(uint32_t start,
uint32_t size,
nxtBufferMapWriteCallback callback,
nxtCallbackUserdata userdata);
2017-11-24 18:59:42 +00:00
void Unmap();
protected:
void CallMapReadCallback(uint32_t serial,
nxtBufferMapAsyncStatus status,
2017-11-24 18:59:42 +00:00
const void* pointer);
void CallMapWriteCallback(uint32_t serial, nxtBufferMapAsyncStatus status, void* pointer);
2017-11-24 18:59:42 +00:00
private:
virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint8_t* data) = 0;
2017-11-24 18:59:42 +00:00
virtual void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0;
virtual void MapWriteAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0;
2017-11-24 18:59:42 +00:00
virtual void UnmapImpl() = 0;
bool ValidateMapBase(uint32_t start, uint32_t size, nxt::BufferUsageBit requiredUsage);
2017-11-24 18:59:42 +00:00
DeviceBase* mDevice;
uint32_t mSize;
nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None;
nxtBufferMapReadCallback mMapReadCallback = nullptr;
nxtBufferMapWriteCallback mMapWriteCallback = nullptr;
nxtCallbackUserdata mMapUserdata = 0;
uint32_t mMapSerial = 0;
2017-11-24 18:59:42 +00:00
bool mIsMapped = false;
};
class BufferBuilder : public Builder<BufferBase> {
2017-11-24 18:59:42 +00:00
public:
BufferBuilder(DeviceBase* device);
2017-11-24 18:59:42 +00:00
// NXT API
void SetAllowedUsage(nxt::BufferUsageBit usage);
void SetInitialUsage(nxt::BufferUsageBit usage);
void SetSize(uint32_t size);
2017-11-24 18:59:42 +00:00
private:
friend class BufferBase;
2017-11-24 18:59:42 +00:00
BufferBase* GetResultImpl() override;
2017-11-24 18:59:42 +00:00
uint32_t mSize;
nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None;
nxt::BufferUsageBit mCurrentUsage = nxt::BufferUsageBit::None;
int mPropertiesSet = 0;
};
class BufferViewBase : public RefCounted {
2017-11-24 18:59:42 +00:00
public:
BufferViewBase(BufferViewBuilder* builder);
2017-11-24 18:59:42 +00:00
BufferBase* GetBuffer();
uint32_t GetSize() const;
uint32_t GetOffset() const;
2017-11-24 18:59:42 +00:00
private:
Ref<BufferBase> mBuffer;
uint32_t mSize;
uint32_t mOffset;
};
class BufferViewBuilder : public Builder<BufferViewBase> {
2017-11-24 18:59:42 +00:00
public:
BufferViewBuilder(DeviceBase* device, BufferBase* buffer);
2017-11-24 18:59:42 +00:00
// NXT API
void SetExtent(uint32_t offset, uint32_t size);
2017-11-24 18:59:42 +00:00
private:
friend class BufferViewBase;
2017-11-24 18:59:42 +00:00
BufferViewBase* GetResultImpl() override;
2017-11-24 18:59:42 +00:00
Ref<BufferBase> mBuffer;
uint32_t mOffset = 0;
uint32_t mSize = 0;
int mPropertiesSet = 0;
};
2017-11-24 18:59:42 +00:00
} // namespace backend
2017-11-24 18:59:42 +00:00
#endif // BACKEND_BUFFER_H_