dawn_wire/client: Add ToAPI and FromAPI helpers.
In the client code, we often need to translate between WGPUObject (the API type) to Object* (the internal client type). This added a bunch of reinterpret_casts that make the code less readable and more fragile. This CL adds FromAPI and ToAPI helpers in the autogenerated ApiObjects_autogen.h header, that convert between API and internal types in a type-safe way. Bug: dawn:445 Change-Id: Ia1bf624f0315ced496b95cb660adf88abd916d71 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/24063 Commit-Queue: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Stephen White <senorblanco@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
parent
2f616dd108
commit
00d6215be9
|
@ -16,10 +16,24 @@
|
||||||
#define DAWNWIRE_CLIENT_APIOBJECTS_AUTOGEN_H_
|
#define DAWNWIRE_CLIENT_APIOBJECTS_AUTOGEN_H_
|
||||||
|
|
||||||
namespace dawn_wire { namespace client {
|
namespace dawn_wire { namespace client {
|
||||||
{% for type in by_category["object"] if not type.name.CamelCase() in client_special_objects %}
|
|
||||||
struct {{type.name.CamelCase()}} : ObjectBase {
|
{% for type in by_category["object"] %}
|
||||||
using ObjectBase::ObjectBase;
|
{% set Type = type.name.CamelCase() %}
|
||||||
};
|
{% if type.name.CamelCase() in client_special_objects %}
|
||||||
|
class {{Type}};
|
||||||
|
{% else %}
|
||||||
|
struct {{type.name.CamelCase()}} : ObjectBase {
|
||||||
|
using ObjectBase::ObjectBase;
|
||||||
|
};
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
inline {{Type}}* FromAPI(WGPU{{Type}} obj) {
|
||||||
|
return reinterpret_cast<{{Type}}*>(obj);
|
||||||
|
}
|
||||||
|
inline WGPU{{Type}} ToAPI({{Type}}* obj) {
|
||||||
|
return reinterpret_cast<WGPU{{Type}}>(obj);
|
||||||
|
}
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
}} // namespace dawn_wire::client
|
}} // namespace dawn_wire::client
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,6 @@ namespace dawn_wire { namespace client {
|
||||||
|
|
||||||
// static
|
// static
|
||||||
WGPUBuffer Buffer::Create(Device* device_, const WGPUBufferDescriptor* descriptor) {
|
WGPUBuffer Buffer::Create(Device* device_, const WGPUBufferDescriptor* descriptor) {
|
||||||
WGPUDevice cDevice = reinterpret_cast<WGPUDevice>(device_);
|
|
||||||
Client* wireClient = device_->GetClient();
|
Client* wireClient = device_->GetClient();
|
||||||
|
|
||||||
if ((descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 &&
|
if ((descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 &&
|
||||||
|
@ -63,19 +62,18 @@ namespace dawn_wire { namespace client {
|
||||||
buffer->mSize = descriptor->size;
|
buffer->mSize = descriptor->size;
|
||||||
|
|
||||||
DeviceCreateBufferCmd cmd;
|
DeviceCreateBufferCmd cmd;
|
||||||
cmd.self = cDevice;
|
cmd.self = ToAPI(device_);
|
||||||
cmd.descriptor = descriptor;
|
cmd.descriptor = descriptor;
|
||||||
cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation};
|
cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation};
|
||||||
|
|
||||||
wireClient->SerializeCommand(cmd);
|
wireClient->SerializeCommand(cmd);
|
||||||
|
|
||||||
return reinterpret_cast<WGPUBuffer>(buffer);
|
return ToAPI(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
WGPUCreateBufferMappedResult Buffer::CreateMapped(Device* device_,
|
WGPUCreateBufferMappedResult Buffer::CreateMapped(Device* device_,
|
||||||
const WGPUBufferDescriptor* descriptor) {
|
const WGPUBufferDescriptor* descriptor) {
|
||||||
WGPUDevice cDevice = reinterpret_cast<WGPUDevice>(device_);
|
|
||||||
Client* wireClient = device_->GetClient();
|
Client* wireClient = device_->GetClient();
|
||||||
|
|
||||||
WGPUCreateBufferMappedResult result;
|
WGPUCreateBufferMappedResult result;
|
||||||
|
@ -119,13 +117,13 @@ namespace dawn_wire { namespace client {
|
||||||
buffer->mWriteHandle = std::move(writeHandle);
|
buffer->mWriteHandle = std::move(writeHandle);
|
||||||
buffer->mMappedData = result.data;
|
buffer->mMappedData = result.data;
|
||||||
|
|
||||||
result.buffer = reinterpret_cast<WGPUBuffer>(buffer);
|
result.buffer = ToAPI(buffer);
|
||||||
|
|
||||||
// Get the serialization size of the WriteHandle.
|
// Get the serialization size of the WriteHandle.
|
||||||
size_t handleCreateInfoLength = buffer->mWriteHandle->SerializeCreateSize();
|
size_t handleCreateInfoLength = buffer->mWriteHandle->SerializeCreateSize();
|
||||||
|
|
||||||
DeviceCreateBufferMappedCmd cmd;
|
DeviceCreateBufferMappedCmd cmd;
|
||||||
cmd.device = cDevice;
|
cmd.device = ToAPI(device_);
|
||||||
cmd.descriptor = descriptor;
|
cmd.descriptor = descriptor;
|
||||||
cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation};
|
cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation};
|
||||||
cmd.handleCreateInfoLength = handleCreateInfoLength;
|
cmd.handleCreateInfoLength = handleCreateInfoLength;
|
||||||
|
@ -145,11 +143,11 @@ namespace dawn_wire { namespace client {
|
||||||
auto* allocation = device_->GetClient()->BufferAllocator().New(device_);
|
auto* allocation = device_->GetClient()->BufferAllocator().New(device_);
|
||||||
|
|
||||||
DeviceCreateErrorBufferCmd cmd;
|
DeviceCreateErrorBufferCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUDevice>(device_);
|
cmd.self = ToAPI(device_);
|
||||||
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
||||||
device_->GetClient()->SerializeCommand(cmd);
|
device_->GetClient()->SerializeCommand(cmd);
|
||||||
|
|
||||||
return reinterpret_cast<WGPUBuffer>(allocation->object.get());
|
return ToAPI(allocation->object.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer::~Buffer() {
|
Buffer::~Buffer() {
|
||||||
|
@ -420,7 +418,7 @@ namespace dawn_wire { namespace client {
|
||||||
ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown);
|
ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown);
|
||||||
|
|
||||||
BufferUnmapCmd cmd;
|
BufferUnmapCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUBuffer>(this);
|
cmd.self = ToAPI(this);
|
||||||
device->GetClient()->SerializeCommand(cmd);
|
device->GetClient()->SerializeCommand(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,7 +430,7 @@ namespace dawn_wire { namespace client {
|
||||||
ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown);
|
ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown);
|
||||||
|
|
||||||
BufferDestroyCmd cmd;
|
BufferDestroyCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUBuffer>(this);
|
cmd.self = ToAPI(this);
|
||||||
device->GetClient()->SerializeCommand(cmd);
|
device->GetClient()->SerializeCommand(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,11 +44,11 @@ namespace dawn_wire { namespace client {
|
||||||
}
|
}
|
||||||
|
|
||||||
ReservedTexture Client::ReserveTexture(WGPUDevice cDevice) {
|
ReservedTexture Client::ReserveTexture(WGPUDevice cDevice) {
|
||||||
Device* device = reinterpret_cast<Device*>(cDevice);
|
Device* device = FromAPI(cDevice);
|
||||||
ObjectAllocator<Texture>::ObjectAndSerial* allocation = TextureAllocator().New(device);
|
ObjectAllocator<Texture>::ObjectAndSerial* allocation = TextureAllocator().New(device);
|
||||||
|
|
||||||
ReservedTexture result;
|
ReservedTexture result;
|
||||||
result.texture = reinterpret_cast<WGPUTexture>(allocation->object.get());
|
result.texture = ToAPI(allocation->object.get());
|
||||||
result.id = allocation->object->id;
|
result.id = allocation->object->id;
|
||||||
result.generation = allocation->generation;
|
result.generation = allocation->generation;
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace dawn_wire { namespace client {
|
||||||
mDefaultQueue = allocation->object.get();
|
mDefaultQueue = allocation->object.get();
|
||||||
|
|
||||||
DeviceGetDefaultQueueCmd cmd;
|
DeviceGetDefaultQueueCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUDevice>(this);
|
cmd.self = ToAPI(this);
|
||||||
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
||||||
|
|
||||||
mClient->SerializeCommand(cmd);
|
mClient->SerializeCommand(cmd);
|
||||||
|
@ -85,7 +85,7 @@ namespace dawn_wire { namespace client {
|
||||||
mErrorScopeStackSize++;
|
mErrorScopeStackSize++;
|
||||||
|
|
||||||
DevicePushErrorScopeCmd cmd;
|
DevicePushErrorScopeCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUDevice>(this);
|
cmd.self = ToAPI(this);
|
||||||
cmd.filter = filter;
|
cmd.filter = filter;
|
||||||
|
|
||||||
mClient->SerializeCommand(cmd);
|
mClient->SerializeCommand(cmd);
|
||||||
|
@ -103,7 +103,7 @@ namespace dawn_wire { namespace client {
|
||||||
mErrorScopes[serial] = {callback, userdata};
|
mErrorScopes[serial] = {callback, userdata};
|
||||||
|
|
||||||
DevicePopErrorScopeCmd cmd;
|
DevicePopErrorScopeCmd cmd;
|
||||||
cmd.device = reinterpret_cast<WGPUDevice>(this);
|
cmd.device = ToAPI(this);
|
||||||
cmd.requestSerial = serial;
|
cmd.requestSerial = serial;
|
||||||
|
|
||||||
mClient->SerializeCommand(cmd);
|
mClient->SerializeCommand(cmd);
|
||||||
|
@ -139,7 +139,7 @@ namespace dawn_wire { namespace client {
|
||||||
|
|
||||||
void Device::InjectError(WGPUErrorType type, const char* message) {
|
void Device::InjectError(WGPUErrorType type, const char* message) {
|
||||||
DeviceInjectErrorCmd cmd;
|
DeviceInjectErrorCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUDevice>(this);
|
cmd.self = ToAPI(this);
|
||||||
cmd.type = type;
|
cmd.type = type;
|
||||||
cmd.message = message;
|
cmd.message = message;
|
||||||
mClient->SerializeCommand(cmd);
|
mClient->SerializeCommand(cmd);
|
||||||
|
@ -160,7 +160,7 @@ namespace dawn_wire { namespace client {
|
||||||
|
|
||||||
WGPUQueue Device::GetDefaultQueue() {
|
WGPUQueue Device::GetDefaultQueue() {
|
||||||
mDefaultQueue->refcount++;
|
mDefaultQueue->refcount++;
|
||||||
return reinterpret_cast<WGPUQueue>(mDefaultQueue);
|
return ToAPI(mDefaultQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}} // namespace dawn_wire::client
|
}} // namespace dawn_wire::client
|
||||||
|
|
|
@ -23,18 +23,18 @@ namespace dawn_wire { namespace client {
|
||||||
auto* allocation = device->GetClient()->FenceAllocator().New(device);
|
auto* allocation = device->GetClient()->FenceAllocator().New(device);
|
||||||
|
|
||||||
QueueCreateFenceCmd cmd;
|
QueueCreateFenceCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUQueue>(this);
|
cmd.self = ToAPI(this);
|
||||||
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
||||||
cmd.descriptor = descriptor;
|
cmd.descriptor = descriptor;
|
||||||
device->GetClient()->SerializeCommand(cmd);
|
device->GetClient()->SerializeCommand(cmd);
|
||||||
|
|
||||||
Fence* fence = allocation->object.get();
|
Fence* fence = allocation->object.get();
|
||||||
fence->Initialize(this, descriptor);
|
fence->Initialize(this, descriptor);
|
||||||
return reinterpret_cast<WGPUFence>(fence);
|
return ToAPI(fence);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Queue::Signal(WGPUFence cFence, uint64_t signalValue) {
|
void Queue::Signal(WGPUFence cFence, uint64_t signalValue) {
|
||||||
Fence* fence = reinterpret_cast<Fence*>(cFence);
|
Fence* fence = FromAPI(cFence);
|
||||||
if (fence->GetQueue() != this) {
|
if (fence->GetQueue() != this) {
|
||||||
device->InjectError(WGPUErrorType_Validation,
|
device->InjectError(WGPUErrorType_Validation,
|
||||||
"Fence must be signaled on the queue on which it was created.");
|
"Fence must be signaled on the queue on which it was created.");
|
||||||
|
@ -49,7 +49,7 @@ namespace dawn_wire { namespace client {
|
||||||
fence->SetSignaledValue(signalValue);
|
fence->SetSignaledValue(signalValue);
|
||||||
|
|
||||||
QueueSignalCmd cmd;
|
QueueSignalCmd cmd;
|
||||||
cmd.self = reinterpret_cast<WGPUQueue>(this);
|
cmd.self = ToAPI(this);
|
||||||
cmd.fence = cFence;
|
cmd.fence = cFence;
|
||||||
cmd.signalValue = signalValue;
|
cmd.signalValue = signalValue;
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ namespace dawn_wire { namespace client {
|
||||||
uint64_t bufferOffset,
|
uint64_t bufferOffset,
|
||||||
const void* data,
|
const void* data,
|
||||||
size_t size) {
|
size_t size) {
|
||||||
Buffer* buffer = reinterpret_cast<Buffer*>(cBuffer);
|
Buffer* buffer = FromAPI(cBuffer);
|
||||||
|
|
||||||
QueueWriteBufferInternalCmd cmd;
|
QueueWriteBufferInternalCmd cmd;
|
||||||
cmd.queueId = id;
|
cmd.queueId = id;
|
||||||
|
|
Loading…
Reference in New Issue