Add Buffer::MapReadAsync state-tracking
This commit is contained in:
parent
f45bdb89c6
commit
b1c19eeb4f
|
@ -37,6 +37,7 @@
|
|||
typedef uint64_t nxtCallbackUserdata;
|
||||
typedef void (*nxtDeviceErrorCallback)(const char* message, nxtCallbackUserdata userdata);
|
||||
typedef void (*nxtBuilderErrorCallback)(nxtBuilderErrorStatus status, const char* message, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2);
|
||||
typedef void (*nxtBufferMapReadCallback)(nxtBufferMapReadStatus status, const void* data, nxtCallbackUserdata userdata);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
|
@ -56,6 +56,14 @@ void ProcTableAsClass::DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCall
|
|||
this->OnDeviceSetErrorCallback(self, callback, userdata);
|
||||
}
|
||||
|
||||
void ProcTableAsClass::BufferMapReadAsync(nxtBuffer self, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) {
|
||||
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
|
||||
object->mapReadCallback = callback;
|
||||
object->userdata1 = userdata;
|
||||
|
||||
this->OnBufferMapReadAsyncCallback(self, start, size, callback, userdata);
|
||||
}
|
||||
|
||||
void ProcTableAsClass::CallDeviceErrorCallback(nxtDevice device, const char* message) {
|
||||
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
|
||||
object->deviceErrorCallback(message, object->userdata1);
|
||||
|
@ -64,6 +72,10 @@ void ProcTableAsClass::CallBuilderErrorCallback(void* builder , nxtBuilderErrorS
|
|||
auto object = reinterpret_cast<ProcTableAsClass::Object*>(builder);
|
||||
object->builderErrorCallback(status, message, object->userdata1, object->userdata2);
|
||||
}
|
||||
void ProcTableAsClass::CallMapReadCallback(nxtBuffer buffer, nxtBufferMapReadStatus status, const void* data) {
|
||||
auto object = reinterpret_cast<ProcTableAsClass::Object*>(buffer);
|
||||
object->mapReadCallback(status, data, object->userdata1);
|
||||
}
|
||||
|
||||
{% for type in by_category["object"] if type.is_builder %}
|
||||
void ProcTableAsClass::{{as_MethodSuffix(type.name, Name("set error callback"))}}({{as_cType(type.name)}} self, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) {
|
||||
|
|
|
@ -55,21 +55,25 @@ class ProcTableAsClass {
|
|||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
// Stores callback and userdata and calls OnDeviceSetErrorCallback
|
||||
// Stores callback and userdata and calls the On* methods
|
||||
void DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata);
|
||||
void BufferMapReadAsync(nxtBuffer self, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata);
|
||||
|
||||
// Special cased mockable methods
|
||||
virtual void OnDeviceSetErrorCallback(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata) = 0;
|
||||
virtual void OnBuilderSetErrorCallback(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) = 0;
|
||||
virtual void OnBufferMapReadAsyncCallback(nxtBuffer buffer, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) = 0;
|
||||
|
||||
// Calls the stored callbacks
|
||||
void CallDeviceErrorCallback(nxtDevice device, const char* message);
|
||||
void CallBuilderErrorCallback(void* builder , nxtBuilderErrorStatus status, const char* message);
|
||||
void CallMapReadCallback(nxtBuffer buffer, nxtBufferMapReadStatus status, const void* data);
|
||||
|
||||
struct Object {
|
||||
ProcTableAsClass* procs = nullptr;
|
||||
nxtDeviceErrorCallback deviceErrorCallback = nullptr;
|
||||
nxtBuilderErrorCallback builderErrorCallback = nullptr;
|
||||
nxtBufferMapReadCallback mapReadCallback = nullptr;
|
||||
nxtCallbackUserdata userdata1 = 0;
|
||||
nxtCallbackUserdata userdata2 = 0;
|
||||
};
|
||||
|
@ -99,6 +103,7 @@ class MockProcTable : public ProcTableAsClass {
|
|||
|
||||
MOCK_METHOD3(OnDeviceSetErrorCallback, void(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata));
|
||||
MOCK_METHOD4(OnBuilderSetErrorCallback, void(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2));
|
||||
MOCK_METHOD5(OnBufferMapReadAsyncCallback, void(nxtBuffer buffer, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata));
|
||||
};
|
||||
|
||||
#endif // MOCK_NXT_H
|
||||
|
|
|
@ -289,6 +289,9 @@ namespace wire {
|
|||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
void ClientBufferMapReadAsync(Buffer* buffer, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) {
|
||||
}
|
||||
|
||||
void ClientDeviceReference(Device* self) {
|
||||
}
|
||||
|
||||
|
|
25
next.json
25
next.json
|
@ -132,6 +132,19 @@
|
|||
{"name": "data", "type": "uint32_t", "annotation": "const*", "length": "count"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"_comment": "Contrary to set sub data, this is in char size",
|
||||
"name": "map read async",
|
||||
"args": [
|
||||
{"name": "start", "type": "uint32_t"},
|
||||
{"name": "size", "type": "uint32_t"},
|
||||
{"name": "callback", "type": "buffer map read callback"},
|
||||
{"name": "userdata", "type": "callback userdata"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "unmap"
|
||||
},
|
||||
{
|
||||
"name": "transition usage",
|
||||
"args": [
|
||||
|
@ -173,6 +186,18 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"buffer map read callback": {
|
||||
"category": "natively defined"
|
||||
},
|
||||
"buffer map read status": {
|
||||
"category": "enum",
|
||||
"values": [
|
||||
{"value": 0, "name": "success"},
|
||||
{"value": 1, "name": "error"},
|
||||
{"value": 2, "name": "unknown"},
|
||||
{"value": 3, "name": "context lost"}
|
||||
]
|
||||
},
|
||||
"buffer usage bit": {
|
||||
"category": "bitmask",
|
||||
"values": [
|
||||
|
|
|
@ -30,6 +30,12 @@ namespace backend {
|
|||
currentUsage(builder->currentUsage) {
|
||||
}
|
||||
|
||||
BufferBase::~BufferBase() {
|
||||
if (mapped) {
|
||||
CallMapReadCallback(mapReadSerial, NXT_BUFFER_MAP_READ_STATUS_UNKNOWN, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
BufferViewBuilder* BufferBase::CreateBufferViewBuilder() {
|
||||
return new BufferViewBuilder(device, this);
|
||||
}
|
||||
|
@ -50,6 +56,13 @@ namespace backend {
|
|||
return currentUsage;
|
||||
}
|
||||
|
||||
void BufferBase::CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer) {
|
||||
if (mapReadCallback && serial == mapReadSerial) {
|
||||
mapReadCallback(status, pointer, mapReadUserdata);
|
||||
mapReadCallback = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void BufferBase::SetSubData(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||
if ((start + count) * sizeof(uint32_t) > GetSize()) {
|
||||
device->HandleError("Buffer subdata out of range");
|
||||
|
@ -64,6 +77,46 @@ namespace backend {
|
|||
SetSubDataImpl(start, count, data);
|
||||
}
|
||||
|
||||
void BufferBase::MapReadAsync(uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) {
|
||||
if (start + size > GetSize()) {
|
||||
device->HandleError("Buffer map read out of range");
|
||||
callback(NXT_BUFFER_MAP_READ_STATUS_ERROR, nullptr, userdata);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(currentUsage & nxt::BufferUsageBit::MapRead)) {
|
||||
device->HandleError("Buffer needs the map read usage bit");
|
||||
callback(NXT_BUFFER_MAP_READ_STATUS_ERROR, nullptr, userdata);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mapped) {
|
||||
device->HandleError("Buffer already mapped");
|
||||
callback(NXT_BUFFER_MAP_READ_STATUS_ERROR, nullptr, userdata);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(cwallez@chromium.org): what to do on wraparound? Could cause crashes.
|
||||
mapReadSerial ++;
|
||||
mapReadCallback = callback;
|
||||
mapReadUserdata = userdata;
|
||||
MapReadAsyncImpl(mapReadSerial, start, size);
|
||||
mapped = true;
|
||||
}
|
||||
|
||||
void BufferBase::Unmap() {
|
||||
if (!mapped) {
|
||||
device->HandleError("Buffer wasn't mapped");
|
||||
return;
|
||||
}
|
||||
|
||||
// A map request can only be called once, so this will fire only if the request wasn't
|
||||
// completed before the Unmap
|
||||
CallMapReadCallback(mapReadSerial, NXT_BUFFER_MAP_READ_STATUS_UNKNOWN, nullptr);
|
||||
UnmapImpl();
|
||||
mapped = false;
|
||||
}
|
||||
|
||||
bool BufferBase::IsFrozen() const {
|
||||
return frozen;
|
||||
}
|
||||
|
@ -86,7 +139,7 @@ namespace backend {
|
|||
}
|
||||
|
||||
bool BufferBase::IsTransitionPossible(nxt::BufferUsageBit usage) const {
|
||||
if (frozen) {
|
||||
if (frozen || mapped) {
|
||||
return false;
|
||||
}
|
||||
return IsUsagePossible(allowedUsage, usage);
|
||||
|
@ -138,6 +191,7 @@ namespace backend {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
// TODO(cwallez@chromium.org) disallow using MapRead with anything else than TransferDst
|
||||
return device->CreateBuffer(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace backend {
|
|||
class BufferBase : public RefCounted {
|
||||
public:
|
||||
BufferBase(BufferBuilder* builder);
|
||||
~BufferBase();
|
||||
|
||||
uint32_t GetSize() const;
|
||||
nxt::BufferUsageBit GetAllowedUsage() const;
|
||||
|
@ -41,17 +42,30 @@ namespace backend {
|
|||
// NXT API
|
||||
BufferViewBuilder* CreateBufferViewBuilder();
|
||||
void SetSubData(uint32_t start, uint32_t count, const uint32_t* data);
|
||||
void MapReadAsync(uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata);
|
||||
void Unmap();
|
||||
void TransitionUsage(nxt::BufferUsageBit usage);
|
||||
void FreezeUsage(nxt::BufferUsageBit usage);
|
||||
|
||||
protected:
|
||||
void CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer);
|
||||
|
||||
private:
|
||||
virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) = 0;
|
||||
virtual void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0;
|
||||
virtual void UnmapImpl() = 0;
|
||||
|
||||
DeviceBase* device;
|
||||
uint32_t size;
|
||||
nxt::BufferUsageBit allowedUsage = nxt::BufferUsageBit::None;
|
||||
nxt::BufferUsageBit currentUsage = nxt::BufferUsageBit::None;
|
||||
|
||||
nxtBufferMapReadCallback mapReadCallback = nullptr;
|
||||
nxtCallbackUserdata mapReadUserdata = 0;
|
||||
uint32_t mapReadSerial = 0;
|
||||
|
||||
bool frozen = false;
|
||||
bool mapped = false;
|
||||
};
|
||||
|
||||
class BufferBuilder : public Builder<BufferBase> {
|
||||
|
|
|
@ -148,6 +148,14 @@ namespace d3d12 {
|
|||
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||
}
|
||||
|
||||
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the null backend
|
||||
}
|
||||
|
||||
void Buffer::UnmapImpl() {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the null backend
|
||||
}
|
||||
|
||||
// BufferView
|
||||
|
||||
BufferView::BufferView(Device* device, BufferViewBuilder* builder)
|
||||
|
|
|
@ -141,6 +141,8 @@ namespace d3d12 {
|
|||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override;
|
||||
void UnmapImpl() override;
|
||||
|
||||
Device* device;
|
||||
};
|
||||
|
|
|
@ -158,6 +158,8 @@ namespace metal {
|
|||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override;
|
||||
void UnmapImpl() override;
|
||||
|
||||
Device* device;
|
||||
std::mutex mutex;
|
||||
|
|
|
@ -225,6 +225,14 @@ namespace metal {
|
|||
[mtlBuffer didModifyRange:NSMakeRange(start * sizeof(uint32_t), count * sizeof(uint32_t))];
|
||||
}
|
||||
|
||||
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the metal backend
|
||||
}
|
||||
|
||||
void Buffer::UnmapImpl() {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the metal backend
|
||||
}
|
||||
|
||||
// BufferView
|
||||
|
||||
BufferView::BufferView(Device* device, BufferViewBuilder* builder)
|
||||
|
|
|
@ -107,6 +107,14 @@ namespace null {
|
|||
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||
}
|
||||
|
||||
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the null backend
|
||||
}
|
||||
|
||||
void Buffer::UnmapImpl() {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the null backend
|
||||
}
|
||||
|
||||
// Queue
|
||||
|
||||
Queue::Queue(QueueBuilder* builder)
|
||||
|
|
|
@ -112,6 +112,8 @@ namespace null {
|
|||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override;
|
||||
void UnmapImpl() override;
|
||||
};
|
||||
|
||||
class Queue : public QueueBase {
|
||||
|
|
|
@ -133,6 +133,14 @@ namespace opengl {
|
|||
glBufferSubData(GL_ARRAY_BUFFER, start * sizeof(uint32_t), count * sizeof(uint32_t), data);
|
||||
}
|
||||
|
||||
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the GL backend
|
||||
}
|
||||
|
||||
void Buffer::UnmapImpl() {
|
||||
// TODO(cwallez@chromium.org): Implement Map Read for the GL backend
|
||||
}
|
||||
|
||||
// BufferView
|
||||
|
||||
BufferView::BufferView(Device* device, BufferViewBuilder* builder)
|
||||
|
|
|
@ -124,6 +124,8 @@ namespace opengl {
|
|||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override;
|
||||
void UnmapImpl() override;
|
||||
|
||||
Device* device;
|
||||
GLuint buffer = 0;
|
||||
|
|
Loading…
Reference in New Issue