diff --git a/dom/webgpu/ipc/PWebGPU.ipdl b/dom/webgpu/ipc/PWebGPU.ipdl index f17e3a390973..ed480a244fdf 100644 --- a/dom/webgpu/ipc/PWebGPU.ipdl +++ b/dom/webgpu/ipc/PWebGPU.ipdl @@ -10,6 +10,7 @@ using layers::CompositableHandle from "mozilla/layers/LayersTypes.h"; using RawId from "mozilla/webgpu/WebGPUTypes.h"; using dom::GPURequestAdapterOptions from "mozilla/dom/WebGPUBinding.h"; using dom::GPUCommandBufferDescriptor from "mozilla/dom/WebGPUBinding.h"; +using dom::GPUBufferDescriptor from "mozilla/dom/WebGPUBinding.h"; using webgpu::ffi::WGPUHostMap from "mozilla/webgpu/ffi/wgpu.h"; using MaybeScopedError from "mozilla/webgpu/WebGPUTypes.h"; @@ -38,6 +39,8 @@ parent: async CommandEncoderAction(RawId selfId, RawId aDeviceId, ByteBuf buf); async BumpImplicitBindGroupLayout(RawId pipelineId, bool isCompute, uint32_t index, RawId assignId); + async CreateBuffer(RawId deviceId, RawId bufferId, GPUBufferDescriptor desc); + async InstanceRequestAdapter(GPURequestAdapterOptions options, RawId[] ids) returns (ByteBuf byteBuf); async AdapterRequestDevice(RawId selfId, ByteBuf buf, RawId newId) returns (bool success); async AdapterDestroy(RawId selfId); diff --git a/dom/webgpu/ipc/WebGPUChild.cpp b/dom/webgpu/ipc/WebGPUChild.cpp index 8b3816ffc1cb..cc60188d3c53 100644 --- a/dom/webgpu/ipc/WebGPUChild.cpp +++ b/dom/webgpu/ipc/WebGPUChild.cpp @@ -352,23 +352,11 @@ Maybe WebGPUChild::AdapterRequestDevice( RawId WebGPUChild::DeviceCreateBuffer(RawId aSelfId, const dom::GPUBufferDescriptor& aDesc) { - ffi::WGPUBufferDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } - desc.size = aDesc.mSize; - desc.usage = aDesc.mUsage; - desc.mapped_at_creation = aDesc.mMappedAtCreation; - - ByteBuf bb; - RawId id = - ffi::wgpu_client_create_buffer(mClient.get(), aSelfId, &desc, ToFFI(&bb)); - if (!SendDeviceAction(aSelfId, std::move(bb))) { + RawId bufferId = ffi::wgpu_client_make_buffer_id(mClient.get(), aSelfId); + if (!SendCreateBuffer(aSelfId, bufferId, aDesc)) { MOZ_CRASH("IPC failure"); } - return id; + return bufferId; } RawId WebGPUChild::DeviceCreateTexture(RawId aSelfId, diff --git a/dom/webgpu/ipc/WebGPUParent.cpp b/dom/webgpu/ipc/WebGPUParent.cpp index f0a9618b94bf..525a2416ec5f 100644 --- a/dom/webgpu/ipc/WebGPUParent.cpp +++ b/dom/webgpu/ipc/WebGPUParent.cpp @@ -334,6 +334,22 @@ ipc::IPCResult WebGPUParent::RecvDeviceDestroy(RawId aSelfId) { return IPC_OK(); } +ipc::IPCResult WebGPUParent::RecvCreateBuffer( + RawId aSelfId, RawId aBufferId, dom::GPUBufferDescriptor&& aDesc) { + nsCString label; + const char* labelOrNull = nullptr; + if (aDesc.mLabel.WasPassed()) { + LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); + labelOrNull = label.get(); + } + ErrorBuffer error; + ffi::wgpu_server_device_create_buffer(mContext.get(), aSelfId, aBufferId, + labelOrNull, aDesc.mSize, aDesc.mUsage, + aDesc.mMappedAtCreation, error.ToFFI()); + ForwardError(aSelfId, error); + return IPC_OK(); +} + ipc::IPCResult WebGPUParent::RecvBufferReturnShmem(RawId aSelfId, Shmem&& aShmem) { MOZ_LOG(sLogger, LogLevel::Info, @@ -744,13 +760,11 @@ ipc::IPCResult WebGPUParent::RecvSwapChainPresent( ffi::WGPUBufferUsages usage = WGPUBufferUsages_COPY_DST | WGPUBufferUsages_MAP_READ; - ffi::WGPUBufferDescriptor desc = {}; - desc.size = bufferSize; - desc.usage = usage; ErrorBuffer error; ffi::wgpu_server_device_create_buffer(mContext.get(), data->mDeviceId, - &desc, bufferId, error.ToFFI()); + bufferId, nullptr, bufferSize, + usage, false, error.ToFFI()); if (ForwardError(data->mDeviceId, error)) { return IPC_OK(); } diff --git a/dom/webgpu/ipc/WebGPUParent.h b/dom/webgpu/ipc/WebGPUParent.h index 54c24f65d809..09d7c83d0e9e 100644 --- a/dom/webgpu/ipc/WebGPUParent.h +++ b/dom/webgpu/ipc/WebGPUParent.h @@ -34,6 +34,8 @@ class WebGPUParent final : public PWebGPUParent { AdapterRequestDeviceResolver&& resolver); ipc::IPCResult RecvAdapterDestroy(RawId aSelfId); ipc::IPCResult RecvDeviceDestroy(RawId aSelfId); + ipc::IPCResult RecvCreateBuffer(RawId aSelfId, RawId aBufferId, + dom::GPUBufferDescriptor&& aDesc); ipc::IPCResult RecvBufferReturnShmem(RawId aSelfId, Shmem&& aShmem); ipc::IPCResult RecvBufferMap(RawId aSelfId, ffi::WGPUHostMap aHostMap, uint64_t aOffset, uint64_t size, diff --git a/dom/webgpu/ipc/WebGPUSerialize.h b/dom/webgpu/ipc/WebGPUSerialize.h index 18ed6f885369..b1052332365f 100644 --- a/dom/webgpu/ipc/WebGPUSerialize.h +++ b/dom/webgpu/ipc/WebGPUSerialize.h @@ -33,6 +33,9 @@ DEFINE_IPC_SERIALIZER_WITHOUT_FIELDS(mozilla::dom::GPUCommandBufferDescriptor); DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::GPURequestAdapterOptions, mPowerPreference, mForceFallbackAdapter); +DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::GPUBufferDescriptor, mSize, + mUsage, mMappedAtCreation); + DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::webgpu::ScopedError, operationError, validationMessage); diff --git a/gfx/wgpu_bindings/cbindgen.toml b/gfx/wgpu_bindings/cbindgen.toml index 8f02507dbf44..beda4228946b 100644 --- a/gfx/wgpu_bindings/cbindgen.toml +++ b/gfx/wgpu_bindings/cbindgen.toml @@ -43,6 +43,7 @@ exclude = [ "Option_BufferSize", "Option_NonZeroU32", "Option_NonZeroU8", "ANativeWindow_setBuffersGeometry", ] +include = ["BufferUsages"] [export.rename] "BufferDescriptor_RawString" = "BufferDescriptor" diff --git a/gfx/wgpu_bindings/src/client.rs b/gfx/wgpu_bindings/src/client.rs index 395b96dbfeaf..3e5d196890d5 100644 --- a/gfx/wgpu_bindings/src/client.rs +++ b/gfx/wgpu_bindings/src/client.rs @@ -475,26 +475,6 @@ pub extern "C" fn wgpu_client_make_buffer_id( .alloc(backend) } -#[no_mangle] -pub extern "C" fn wgpu_client_create_buffer( - client: &Client, - device_id: id::DeviceId, - desc: &wgt::BufferDescriptor, - bb: &mut ByteBuf, -) -> id::BufferId { - let backend = device_id.backend(); - let id = client - .identities - .lock() - .select(backend) - .buffers - .alloc(backend); - - let action = DeviceAction::CreateBuffer(id, desc.map_label(cow_label)); - *bb = make_byte_buf(&action); - id -} - #[no_mangle] pub extern "C" fn wgpu_client_create_texture( client: &Client, diff --git a/gfx/wgpu_bindings/src/lib.rs b/gfx/wgpu_bindings/src/lib.rs index 5146c7c729d1..71e587566189 100644 --- a/gfx/wgpu_bindings/src/lib.rs +++ b/gfx/wgpu_bindings/src/lib.rs @@ -75,7 +75,6 @@ struct ImplicitLayout<'a> { #[derive(serde::Serialize, serde::Deserialize)] enum DeviceAction<'a> { - CreateBuffer(id::BufferId, wgc::resource::BufferDescriptor<'a>), CreateTexture(id::TextureId, wgc::resource::TextureDescriptor<'a>), CreateSampler(id::SamplerId, wgc::resource::SamplerDescriptor<'a>), CreateBindGroupLayout( diff --git a/gfx/wgpu_bindings/src/server.rs b/gfx/wgpu_bindings/src/server.rs index 590145e77275..20691547df05 100644 --- a/gfx/wgpu_bindings/src/server.rs +++ b/gfx/wgpu_bindings/src/server.rs @@ -211,12 +211,30 @@ pub extern "C" fn wgpu_server_device_drop(global: &Global, self_id: id::DeviceId pub extern "C" fn wgpu_server_device_create_buffer( global: &Global, self_id: id::DeviceId, - desc: &wgt::BufferDescriptor, - new_id: id::BufferId, + buffer_id: id::BufferId, + label_or_null: RawString, + size: wgt::BufferAddress, + usage: u32, + mapped_at_creation: bool, mut error_buf: ErrorBuffer, ) { - let desc = desc.map_label(cow_label); - let (_, error) = gfx_select!(self_id => global.device_create_buffer(self_id, &desc, new_id)); + let label = cow_label(&label_or_null); + let usage = match wgt::BufferUsages::from_bits(usage) { + Some(usage) => usage, + None => { + error_buf.init_str("GPUBufferDescriptor's 'usage' includes invalid unimplemented bits \ + or unimplemented usages"); + gfx_select!(self_id => global.create_buffer_error(buffer_id, label)); + return; + } + }; + let desc = wgc::resource::BufferDescriptor { + label, + size, + usage, + mapped_at_creation, + }; + let (_, error) = gfx_select!(self_id => global.device_create_buffer(self_id, &desc, buffer_id)); if let Some(err) = error { error_buf.init(err); } @@ -285,12 +303,6 @@ impl Global { mut error_buf: ErrorBuffer, ) { match action { - DeviceAction::CreateBuffer(id, desc) => { - let (_, error) = self.device_create_buffer::(self_id, &desc, id); - if let Some(err) = error { - error_buf.init(err); - } - } DeviceAction::CreateTexture(id, desc) => { let (_, error) = self.device_create_texture::(self_id, &desc, id); if let Some(err) = error {