Add Buffer::MapReadAsync state-tracking

This commit is contained in:
Corentin Wallez 2017-06-09 10:51:29 -04:00 committed by Corentin Wallez
parent f45bdb89c6
commit b1c19eeb4f
15 changed files with 156 additions and 2 deletions

View File

@ -37,6 +37,7 @@
typedef uint64_t nxtCallbackUserdata; typedef uint64_t nxtCallbackUserdata;
typedef void (*nxtDeviceErrorCallback)(const char* message, nxtCallbackUserdata userdata); typedef void (*nxtDeviceErrorCallback)(const char* message, nxtCallbackUserdata userdata);
typedef void (*nxtBuilderErrorCallback)(nxtBuilderErrorStatus status, const char* message, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2); typedef void (*nxtBuilderErrorCallback)(nxtBuilderErrorStatus status, const char* message, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2);
typedef void (*nxtBufferMapReadCallback)(nxtBufferMapReadStatus status, const void* data, nxtCallbackUserdata userdata);
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -56,6 +56,14 @@ void ProcTableAsClass::DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCall
this->OnDeviceSetErrorCallback(self, callback, userdata); 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) { void ProcTableAsClass::CallDeviceErrorCallback(nxtDevice device, const char* message) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device); auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
object->deviceErrorCallback(message, object->userdata1); object->deviceErrorCallback(message, object->userdata1);
@ -64,6 +72,10 @@ void ProcTableAsClass::CallBuilderErrorCallback(void* builder , nxtBuilderErrorS
auto object = reinterpret_cast<ProcTableAsClass::Object*>(builder); auto object = reinterpret_cast<ProcTableAsClass::Object*>(builder);
object->builderErrorCallback(status, message, object->userdata1, object->userdata2); 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 %} {% 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) { void ProcTableAsClass::{{as_MethodSuffix(type.name, Name("set error callback"))}}({{as_cType(type.name)}} self, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) {

View File

@ -55,21 +55,25 @@ class ProcTableAsClass {
{% endif %} {% endif %}
{% endfor %} {% 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 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 // Special cased mockable methods
virtual void OnDeviceSetErrorCallback(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata) = 0; virtual void OnDeviceSetErrorCallback(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata) = 0;
virtual void OnBuilderSetErrorCallback(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) = 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 // Calls the stored callbacks
void CallDeviceErrorCallback(nxtDevice device, const char* message); void CallDeviceErrorCallback(nxtDevice device, const char* message);
void CallBuilderErrorCallback(void* builder , nxtBuilderErrorStatus status, const char* message); void CallBuilderErrorCallback(void* builder , nxtBuilderErrorStatus status, const char* message);
void CallMapReadCallback(nxtBuffer buffer, nxtBufferMapReadStatus status, const void* data);
struct Object { struct Object {
ProcTableAsClass* procs = nullptr; ProcTableAsClass* procs = nullptr;
nxtDeviceErrorCallback deviceErrorCallback = nullptr; nxtDeviceErrorCallback deviceErrorCallback = nullptr;
nxtBuilderErrorCallback builderErrorCallback = nullptr; nxtBuilderErrorCallback builderErrorCallback = nullptr;
nxtBufferMapReadCallback mapReadCallback = nullptr;
nxtCallbackUserdata userdata1 = 0; nxtCallbackUserdata userdata1 = 0;
nxtCallbackUserdata userdata2 = 0; nxtCallbackUserdata userdata2 = 0;
}; };
@ -99,6 +103,7 @@ class MockProcTable : public ProcTableAsClass {
MOCK_METHOD3(OnDeviceSetErrorCallback, void(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata)); MOCK_METHOD3(OnDeviceSetErrorCallback, void(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata));
MOCK_METHOD4(OnBuilderSetErrorCallback, void(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2)); 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 #endif // MOCK_NXT_H

View File

@ -289,6 +289,9 @@ namespace wire {
{% endif %} {% endif %}
{% endfor %} {% endfor %}
void ClientBufferMapReadAsync(Buffer* buffer, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) {
}
void ClientDeviceReference(Device* self) { void ClientDeviceReference(Device* self) {
} }

View File

@ -132,6 +132,19 @@
{"name": "data", "type": "uint32_t", "annotation": "const*", "length": "count"} {"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", "name": "transition usage",
"args": [ "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": { "buffer usage bit": {
"category": "bitmask", "category": "bitmask",
"values": [ "values": [

View File

@ -30,6 +30,12 @@ namespace backend {
currentUsage(builder->currentUsage) { currentUsage(builder->currentUsage) {
} }
BufferBase::~BufferBase() {
if (mapped) {
CallMapReadCallback(mapReadSerial, NXT_BUFFER_MAP_READ_STATUS_UNKNOWN, nullptr);
}
}
BufferViewBuilder* BufferBase::CreateBufferViewBuilder() { BufferViewBuilder* BufferBase::CreateBufferViewBuilder() {
return new BufferViewBuilder(device, this); return new BufferViewBuilder(device, this);
} }
@ -50,6 +56,13 @@ namespace backend {
return currentUsage; 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) { void BufferBase::SetSubData(uint32_t start, uint32_t count, const uint32_t* data) {
if ((start + count) * sizeof(uint32_t) > GetSize()) { if ((start + count) * sizeof(uint32_t) > GetSize()) {
device->HandleError("Buffer subdata out of range"); device->HandleError("Buffer subdata out of range");
@ -64,6 +77,46 @@ namespace backend {
SetSubDataImpl(start, count, data); 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 { bool BufferBase::IsFrozen() const {
return frozen; return frozen;
} }
@ -86,7 +139,7 @@ namespace backend {
} }
bool BufferBase::IsTransitionPossible(nxt::BufferUsageBit usage) const { bool BufferBase::IsTransitionPossible(nxt::BufferUsageBit usage) const {
if (frozen) { if (frozen || mapped) {
return false; return false;
} }
return IsUsagePossible(allowedUsage, usage); return IsUsagePossible(allowedUsage, usage);
@ -138,6 +191,7 @@ namespace backend {
return nullptr; return nullptr;
} }
// TODO(cwallez@chromium.org) disallow using MapRead with anything else than TransferDst
return device->CreateBuffer(this); return device->CreateBuffer(this);
} }

View File

@ -26,6 +26,7 @@ namespace backend {
class BufferBase : public RefCounted { class BufferBase : public RefCounted {
public: public:
BufferBase(BufferBuilder* builder); BufferBase(BufferBuilder* builder);
~BufferBase();
uint32_t GetSize() const; uint32_t GetSize() const;
nxt::BufferUsageBit GetAllowedUsage() const; nxt::BufferUsageBit GetAllowedUsage() const;
@ -41,17 +42,30 @@ namespace backend {
// NXT API // NXT API
BufferViewBuilder* CreateBufferViewBuilder(); BufferViewBuilder* CreateBufferViewBuilder();
void SetSubData(uint32_t start, uint32_t count, const uint32_t* data); 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 TransitionUsage(nxt::BufferUsageBit usage);
void FreezeUsage(nxt::BufferUsageBit usage); void FreezeUsage(nxt::BufferUsageBit usage);
protected:
void CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer);
private: private:
virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) = 0; 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; DeviceBase* device;
uint32_t size; uint32_t size;
nxt::BufferUsageBit allowedUsage = nxt::BufferUsageBit::None; nxt::BufferUsageBit allowedUsage = nxt::BufferUsageBit::None;
nxt::BufferUsageBit currentUsage = nxt::BufferUsageBit::None; nxt::BufferUsageBit currentUsage = nxt::BufferUsageBit::None;
nxtBufferMapReadCallback mapReadCallback = nullptr;
nxtCallbackUserdata mapReadUserdata = 0;
uint32_t mapReadSerial = 0;
bool frozen = false; bool frozen = false;
bool mapped = false;
}; };
class BufferBuilder : public Builder<BufferBase> { class BufferBuilder : public Builder<BufferBase> {

View File

@ -148,6 +148,14 @@ namespace d3d12 {
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) { 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::BufferView(Device* device, BufferViewBuilder* builder) BufferView::BufferView(Device* device, BufferViewBuilder* builder)

View File

@ -141,6 +141,8 @@ namespace d3d12 {
private: private:
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; 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; Device* device;
}; };

View File

@ -158,6 +158,8 @@ namespace metal {
private: private:
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; 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; Device* device;
std::mutex mutex; std::mutex mutex;

View File

@ -225,6 +225,14 @@ namespace metal {
[mtlBuffer didModifyRange:NSMakeRange(start * sizeof(uint32_t), count * sizeof(uint32_t))]; [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::BufferView(Device* device, BufferViewBuilder* builder) BufferView::BufferView(Device* device, BufferViewBuilder* builder)

View File

@ -107,6 +107,14 @@ namespace null {
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) { 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::Queue(QueueBuilder* builder) Queue::Queue(QueueBuilder* builder)

View File

@ -112,6 +112,8 @@ namespace null {
private: private:
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; 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 { class Queue : public QueueBase {

View File

@ -133,6 +133,14 @@ namespace opengl {
glBufferSubData(GL_ARRAY_BUFFER, start * sizeof(uint32_t), count * sizeof(uint32_t), data); 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::BufferView(Device* device, BufferViewBuilder* builder) BufferView::BufferView(Device* device, BufferViewBuilder* builder)

View File

@ -124,6 +124,8 @@ namespace opengl {
private: private:
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; 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; Device* device;
GLuint buffer = 0; GLuint buffer = 0;