2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT 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_COMMON_BUFFER_H_
|
|
|
|
#define BACKEND_COMMON_BUFFER_H_
|
|
|
|
|
|
|
|
#include "Forward.h"
|
2017-05-08 08:52:11 +00:00
|
|
|
#include "Builder.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
#include "RefCounted.h"
|
|
|
|
|
|
|
|
#include "nxt/nxtcpp.h"
|
|
|
|
|
|
|
|
namespace backend {
|
|
|
|
|
|
|
|
class BufferBase : public RefCounted {
|
|
|
|
public:
|
|
|
|
BufferBase(BufferBuilder* builder);
|
2017-06-09 14:51:29 +00:00
|
|
|
~BufferBase();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
uint32_t GetSize() const;
|
|
|
|
nxt::BufferUsageBit GetAllowedUsage() const;
|
|
|
|
nxt::BufferUsageBit GetUsage() const;
|
|
|
|
static bool IsUsagePossible(nxt::BufferUsageBit allowedUsage, nxt::BufferUsageBit usage);
|
|
|
|
bool IsTransitionPossible(nxt::BufferUsageBit usage) const;
|
|
|
|
bool IsFrozen() const;
|
|
|
|
bool HasFrozenUsage(nxt::BufferUsageBit usage) const;
|
2017-06-12 22:31:10 +00:00
|
|
|
void UpdateUsageInternal(nxt::BufferUsageBit usage);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-05-10 13:30:05 +00:00
|
|
|
DeviceBase* GetDevice();
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
// NXT API
|
|
|
|
BufferViewBuilder* CreateBufferViewBuilder();
|
|
|
|
void SetSubData(uint32_t start, uint32_t count, const uint32_t* data);
|
2017-06-09 14:51:29 +00:00
|
|
|
void MapReadAsync(uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata);
|
|
|
|
void Unmap();
|
2017-04-20 18:38:20 +00:00
|
|
|
void TransitionUsage(nxt::BufferUsageBit usage);
|
|
|
|
void FreezeUsage(nxt::BufferUsageBit usage);
|
|
|
|
|
2017-06-09 14:51:29 +00:00
|
|
|
protected:
|
|
|
|
void CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer);
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
private:
|
|
|
|
virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) = 0;
|
2017-06-09 14:51:29 +00:00
|
|
|
virtual void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0;
|
|
|
|
virtual void UnmapImpl() = 0;
|
2017-06-12 22:31:10 +00:00
|
|
|
virtual void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
DeviceBase* device;
|
|
|
|
uint32_t size;
|
|
|
|
nxt::BufferUsageBit allowedUsage = nxt::BufferUsageBit::None;
|
|
|
|
nxt::BufferUsageBit currentUsage = nxt::BufferUsageBit::None;
|
2017-06-09 14:51:29 +00:00
|
|
|
|
|
|
|
nxtBufferMapReadCallback mapReadCallback = nullptr;
|
|
|
|
nxtCallbackUserdata mapReadUserdata = 0;
|
|
|
|
uint32_t mapReadSerial = 0;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
bool frozen = false;
|
2017-06-09 14:51:29 +00:00
|
|
|
bool mapped = false;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
class BufferBuilder : public Builder<BufferBase> {
|
2017-04-20 18:38:20 +00:00
|
|
|
public:
|
|
|
|
BufferBuilder(DeviceBase* device);
|
|
|
|
|
|
|
|
// NXT API
|
|
|
|
void SetAllowedUsage(nxt::BufferUsageBit usage);
|
|
|
|
void SetInitialUsage(nxt::BufferUsageBit usage);
|
|
|
|
void SetSize(uint32_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BufferBase;
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
BufferBase* GetResultImpl() override;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
uint32_t size;
|
|
|
|
nxt::BufferUsageBit allowedUsage = nxt::BufferUsageBit::None;
|
|
|
|
nxt::BufferUsageBit currentUsage = nxt::BufferUsageBit::None;
|
|
|
|
int propertiesSet = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BufferViewBase : public RefCounted {
|
|
|
|
public:
|
|
|
|
BufferViewBase(BufferViewBuilder* builder);
|
|
|
|
|
|
|
|
BufferBase* GetBuffer();
|
|
|
|
uint32_t GetSize() const;
|
|
|
|
uint32_t GetOffset() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Ref<BufferBase> buffer;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t offset;
|
|
|
|
};
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
class BufferViewBuilder : public Builder<BufferViewBase> {
|
2017-04-20 18:38:20 +00:00
|
|
|
public:
|
|
|
|
BufferViewBuilder(DeviceBase* device, BufferBase* buffer);
|
|
|
|
|
|
|
|
// NXT API
|
|
|
|
void SetExtent(uint32_t offset, uint32_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BufferViewBase;
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
BufferViewBase* GetResultImpl() override;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
Ref<BufferBase> buffer;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
int propertiesSet = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BACKEND_COMMON_BUFFER_H_
|