diff --git a/dom/webgpu/CommandEncoder.cpp b/dom/webgpu/CommandEncoder.cpp index c3dc63bbaaf9..fb6d3c07b62b 100644 --- a/dom/webgpu/CommandEncoder.cpp +++ b/dom/webgpu/CommandEncoder.cpp @@ -171,8 +171,8 @@ void CommandEncoder::CopyTextureToTexture( void CommandEncoder::PushDebugGroup(const nsAString& aString) { if (mValid && mBridge->IsOpen()) { ipc::ByteBuf bb; - const NS_ConvertUTF16toUTF8 utf8(aString); - ffi::wgpu_command_encoder_push_debug_group(utf8.get(), ToFFI(&bb)); + NS_ConvertUTF16toUTF8 marker(aString); + ffi::wgpu_command_encoder_push_debug_group(&marker, ToFFI(&bb)); mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb)); } } @@ -186,8 +186,8 @@ void CommandEncoder::PopDebugGroup() { void CommandEncoder::InsertDebugMarker(const nsAString& aString) { if (mValid && mBridge->IsOpen()) { ipc::ByteBuf bb; - const NS_ConvertUTF16toUTF8 utf8(aString); - ffi::wgpu_command_encoder_insert_debug_marker(utf8.get(), ToFFI(&bb)); + NS_ConvertUTF16toUTF8 marker(aString); + ffi::wgpu_command_encoder_insert_debug_marker(&marker, ToFFI(&bb)); mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb)); } } diff --git a/dom/webgpu/ComputePassEncoder.cpp b/dom/webgpu/ComputePassEncoder.cpp index 0cbbb4e9a0f5..8172874c224e 100644 --- a/dom/webgpu/ComputePassEncoder.cpp +++ b/dom/webgpu/ComputePassEncoder.cpp @@ -28,7 +28,10 @@ void ScopedFfiComputeTraits::release(ffi::WGPUComputePass* raw) { ffi::WGPUComputePass* BeginComputePass( RawId aEncoderId, const dom::GPUComputePassDescriptor& aDesc) { ffi::WGPUComputePassDescriptor desc = {}; - Unused << aDesc; // no useful fields + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); + return ffi::wgpu_command_encoder_begin_compute_pass(aEncoderId, &desc); } diff --git a/dom/webgpu/RenderBundleEncoder.cpp b/dom/webgpu/RenderBundleEncoder.cpp index 523248968353..cb14db1dd08e 100644 --- a/dom/webgpu/RenderBundleEncoder.cpp +++ b/dom/webgpu/RenderBundleEncoder.cpp @@ -37,11 +37,8 @@ ffi::WGPURenderBundleEncoder* CreateRenderBundleEncoder( ffi::WGPURenderBundleEncoderDescriptor desc = {}; desc.sample_count = aDesc.mSampleCount; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); ffi::WGPUTextureFormat depthStencilFormat = {ffi::WGPUTextureFormat_Sentinel}; if (aDesc.mDepthStencilFormat.WasPassed()) { diff --git a/dom/webgpu/RenderPassEncoder.cpp b/dom/webgpu/RenderPassEncoder.cpp index c8612f0f87f2..4cc2b9fbf392 100644 --- a/dom/webgpu/RenderPassEncoder.cpp +++ b/dom/webgpu/RenderPassEncoder.cpp @@ -55,6 +55,9 @@ ffi::WGPURenderPass* BeginRenderPass( CommandEncoder* const aParent, const dom::GPURenderPassDescriptor& aDesc) { ffi::WGPURenderPassDescriptor desc = {}; + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); + ffi::WGPURenderPassDepthStencilAttachment dsDesc = {}; if (aDesc.mDepthStencilAttachment.WasPassed()) { const auto& dsa = aDesc.mDepthStencilAttachment.Value(); diff --git a/dom/webgpu/ipc/WebGPUChild.cpp b/dom/webgpu/ipc/WebGPUChild.cpp index 76428bcecb6e..f6d7c225fa3c 100644 --- a/dom/webgpu/ipc/WebGPUChild.cpp +++ b/dom/webgpu/ipc/WebGPUChild.cpp @@ -265,6 +265,9 @@ Maybe WebGPUChild::AdapterRequestDevice( ffi::WGPUDeviceDescriptor desc = {}; ffi::wgpu_client_fill_default_limits(&desc.limits); + // webgpu::StringHelper label(aDesc.mLabel); + // desc.label = label.Get(); + const auto featureBits = Adapter::MakeFeatureBits(aDesc.mRequiredFeatures); if (!featureBits) { return Nothing(); @@ -363,11 +366,10 @@ RawId WebGPUChild::DeviceCreateBuffer(RawId aSelfId, RawId WebGPUChild::DeviceCreateTexture(RawId aSelfId, const dom::GPUTextureDescriptor& aDesc) { ffi::WGPUTextureDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); + if (aDesc.mSize.IsRangeEnforcedUnsignedLongSequence()) { const auto& seq = aDesc.mSize.GetAsRangeEnforcedUnsignedLongSequence(); desc.size.width = seq.Length() > 0 ? seq[0] : 1; @@ -400,11 +402,9 @@ RawId WebGPUChild::TextureCreateView( RawId aSelfId, RawId aDeviceId, const dom::GPUTextureViewDescriptor& aDesc) { ffi::WGPUTextureViewDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); ffi::WGPUTextureFormat format = {ffi::WGPUTextureFormat_Sentinel}; if (aDesc.mFormat.WasPassed()) { @@ -438,12 +438,9 @@ RawId WebGPUChild::TextureCreateView( RawId WebGPUChild::DeviceCreateSampler(RawId aSelfId, const dom::GPUSamplerDescriptor& aDesc) { ffi::WGPUSamplerDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); desc.address_modes[0] = ffi::WGPUAddressMode(aDesc.mAddressModeU); desc.address_modes[1] = ffi::WGPUAddressMode(aDesc.mAddressModeV); desc.address_modes[2] = ffi::WGPUAddressMode(aDesc.mAddressModeW); @@ -471,11 +468,9 @@ RawId WebGPUChild::DeviceCreateSampler(RawId aSelfId, RawId WebGPUChild::DeviceCreateCommandEncoder( RawId aSelfId, const dom::GPUCommandEncoderDescriptor& aDesc) { ffi::WGPUCommandEncoderDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); ByteBuf bb; RawId id = ffi::wgpu_client_create_command_encoder(mClient.get(), aSelfId, @@ -503,11 +498,9 @@ RawId WebGPUChild::RenderBundleEncoderFinish( ffi::WGPURenderBundleEncoder& aEncoder, RawId aDeviceId, const dom::GPURenderBundleDescriptor& aDesc) { ffi::WGPURenderBundleDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); ipc::ByteBuf bb; RawId id = ffi::wgpu_client_create_render_bundle( @@ -616,11 +609,9 @@ RawId WebGPUChild::DeviceCreateBindGroupLayout( } ffi::WGPUBindGroupLayoutDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); desc.entries = entries.Elements(); desc.entries_length = entries.Length(); @@ -645,11 +636,9 @@ RawId WebGPUChild::DeviceCreatePipelineLayout( } ffi::WGPUPipelineLayoutDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); desc.bind_group_layouts = bindGroupLayouts.Elements(); desc.bind_group_layouts_length = bindGroupLayouts.Length(); @@ -688,11 +677,9 @@ RawId WebGPUChild::DeviceCreateBindGroup( } ffi::WGPUBindGroupDescriptor desc = {}; - nsCString label; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); desc.layout = aDesc.mLayout->mId; desc.entries = entries.Elements(); desc.entries_length = entries.Length(); @@ -846,7 +833,7 @@ RawId WebGPUChild::DeviceCreateRenderPipelineImpl( nsTArray vertexBuffers; nsTArray vertexAttributes; ffi::WGPURenderPipelineDescriptor desc = {}; - nsCString label, vsEntry, fsEntry; + nsCString vsEntry, fsEntry; ffi::WGPUIndexFormat stripIndexFormat = ffi::WGPUIndexFormat_Uint16; ffi::WGPUFace cullFace = ffi::WGPUFace_Front; ffi::WGPUVertexState vertexState = {}; @@ -854,10 +841,9 @@ RawId WebGPUChild::DeviceCreateRenderPipelineImpl( nsTArray colorStates; nsTArray blendStates; - if (aDesc.mLabel.WasPassed()) { - LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label); - desc.label = label.get(); - } + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); + if (aDesc.mLayout.WasPassed()) { desc.layout = aDesc.mLayout.Value().mId; } diff --git a/dom/webgpu/ipc/WebGPUParent.cpp b/dom/webgpu/ipc/WebGPUParent.cpp index 6c964197c42a..8ccd4b41a7dc 100644 --- a/dom/webgpu/ipc/WebGPUParent.cpp +++ b/dom/webgpu/ipc/WebGPUParent.cpp @@ -336,15 +336,11 @@ ipc::IPCResult WebGPUParent::RecvDeviceDestroy(RawId aSelfId) { 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(); - } + webgpu::StringHelper label(aDesc.mLabel); + ErrorBuffer error; ffi::wgpu_server_device_create_buffer(mContext.get(), aSelfId, aBufferId, - labelOrNull, aDesc.mSize, aDesc.mUsage, + label.Get(), aDesc.mSize, aDesc.mUsage, aDesc.mMappedAtCreation, error.ToFFI()); ForwardError(aSelfId, error); return IPC_OK(); @@ -473,6 +469,10 @@ ipc::IPCResult WebGPUParent::RecvCommandEncoderFinish( const dom::GPUCommandBufferDescriptor& aDesc) { Unused << aDesc; ffi::WGPUCommandBufferDescriptor desc = {}; + + webgpu::StringHelper label(aDesc.mLabel); + desc.label = label.Get(); + ErrorBuffer error; ffi::wgpu_server_encoder_finish(mContext.get(), aSelfId, &desc, error.ToFFI()); @@ -636,13 +636,17 @@ ipc::IPCResult WebGPUParent::RecvDeviceCreateSwapChain( ipc::IPCResult WebGPUParent::RecvDeviceCreateShaderModule( RawId aSelfId, RawId aBufferId, const nsString& aLabel, const nsCString& aCode, DeviceCreateShaderModuleResolver&& aOutMessage) { - NS_ConvertUTF16toUTF8 label(aLabel); + // TODO: this should probably be an optional label in the IPC message. + const nsACString* label = nullptr; + NS_ConvertUTF16toUTF8 utf8Label(aLabel); + if (!utf8Label.IsEmpty()) { + label = &utf8Label; + } ffi::WGPUShaderModuleCompilationMessage message; bool ok = ffi::wgpu_server_device_create_shader_module( - mContext.get(), aSelfId, aBufferId, label.get(), - reinterpret_cast(aCode.get()), aCode.Length(), &message); + mContext.get(), aSelfId, aBufferId, label, &aCode, &message); nsTArray messages; diff --git a/dom/webgpu/ipc/WebGPUTypes.h b/dom/webgpu/ipc/WebGPUTypes.h index befb88cc40fc..e607e03b9966 100644 --- a/dom/webgpu/ipc/WebGPUTypes.h +++ b/dom/webgpu/ipc/WebGPUTypes.h @@ -9,6 +9,7 @@ #include #include "mozilla/Maybe.h" #include "nsString.h" +#include "mozilla/dom/BindingDeclarations.h" namespace mozilla::webgpu { @@ -42,6 +43,27 @@ struct WebGPUCompilationMessage { WebGPUCompilationMessageType::Error; }; +/// A helper to reduce the boiler plate of turning the many Optional +/// we get from the dom to the nullable nsACString* we pass to the wgpu ffi. +class StringHelper { + public: + explicit StringHelper(const dom::Optional& aWide) { + if (aWide.WasPassed()) { + mNarrow = Some(NS_ConvertUTF16toUTF8(aWide.Value())); + } + } + + const nsACString* Get() const { + if (mNarrow.isSome()) { + return mNarrow.ptr(); + } + return nullptr; + } + + private: + Maybe mNarrow; +}; + } // namespace mozilla::webgpu #endif // WEBGPU_TYPES_H_ diff --git a/gfx/wgpu_bindings/cbindgen.toml b/gfx/wgpu_bindings/cbindgen.toml index c89ceb9e5654..79271aab14fc 100644 --- a/gfx/wgpu_bindings/cbindgen.toml +++ b/gfx/wgpu_bindings/cbindgen.toml @@ -29,7 +29,6 @@ typedef uint64_t WGPUOption_BindGroupLayoutId; typedef uint64_t WGPUOption_SamplerId; typedef uint64_t WGPUOption_SurfaceId; typedef uint64_t WGPUOption_TextureViewId; -typedef nsString WGPUnsString; """ include_version = true braces = "SameLine" @@ -40,6 +39,7 @@ style = "tag" [export] prefix = "WGPU" +renaming_overrides_prefixing = true exclude = [ "Option_AdapterId", "Option_BufferId", "Option_PipelineLayoutId", "Option_BindGroupLayoutId", "Option_SamplerId", "Option_SurfaceId", "Option_TextureViewId", @@ -49,13 +49,17 @@ exclude = [ include = ["BufferUsages"] [export.rename] -"BufferDescriptor_RawString" = "BufferDescriptor" -"CommandBufferDescriptor_RawString" = "CommandBufferDescriptor" -"CommandEncoderDescriptor_RawString" = "CommandEncoderDescriptor" -"RenderBundleDescriptor_RawString" = "RenderBundleDescriptor" -"DeviceDescriptor_RawString" = "DeviceDescriptor" -"TextureDescriptor_RawString" = "TextureDescriptor" -"SamplerDescriptor_RawString" = "SamplerDescriptor" +"nsString" = "nsString" +"nsCString" = "nsCString" +"nsAString" = "nsAString" +"nsACString" = "nsACString" +"BufferDescriptor______nsACString" = "WGPUBufferDescriptor" +"CommandBufferDescriptor______nsACString" = "WGPUCommandBufferDescriptor" +"CommandEncoderDescriptor______nsACString" = "WGPUCommandEncoderDescriptor" +"RenderBundleDescriptor______nsACString" = "WGPURenderBundleDescriptor" +"DeviceDescriptor______nsACString" = "WGPUDeviceDescriptor" +"TextureDescriptor______nsACString" = "WGPUTextureDescriptor" +"SamplerDescriptor______nsACString" = "WGPUSamplerDescriptor" [parse] parse_deps = true diff --git a/gfx/wgpu_bindings/src/client.rs b/gfx/wgpu_bindings/src/client.rs index 39095a30cdcb..278803adec27 100644 --- a/gfx/wgpu_bindings/src/client.rs +++ b/gfx/wgpu_bindings/src/client.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use crate::{ - cow_label, AdapterInformation, ByteBuf, CommandEncoderAction, DeviceAction, DropAction, + wgpu_string, cow_label, AdapterInformation, ByteBuf, CommandEncoderAction, DeviceAction, DropAction, ImplicitLayout, QueueWriteAction, RawString, TextureAction, }; @@ -14,6 +14,8 @@ pub use wgc::command::{compute_ffi::*, render_ffi::*}; use parking_lot::Mutex; +use nsstring::nsACString; + use std::{ borrow::Cow, num::{NonZeroU32, NonZeroU8}, @@ -146,7 +148,7 @@ impl PrimitiveState<'_> { #[repr(C)] pub struct RenderPipelineDescriptor<'a> { - label: RawString, + label: Option<&'a nsACString>, layout: Option, vertex: &'a VertexState, primitive: PrimitiveState<'a>, @@ -192,7 +194,7 @@ pub struct BindGroupLayoutEntry<'a> { #[repr(C)] pub struct BindGroupLayoutDescriptor<'a> { - label: RawString, + label: Option<&'a nsACString>, entries: *const BindGroupLayoutEntry<'a>, entries_length: usize, } @@ -209,23 +211,23 @@ pub struct BindGroupEntry { } #[repr(C)] -pub struct BindGroupDescriptor { - label: RawString, +pub struct BindGroupDescriptor<'a> { + label: Option<&'a nsACString>, layout: id::BindGroupLayoutId, entries: *const BindGroupEntry, entries_length: usize, } #[repr(C)] -pub struct PipelineLayoutDescriptor { - label: RawString, +pub struct PipelineLayoutDescriptor<'a> { + label: Option<&'a nsACString>, bind_group_layouts: *const id::BindGroupLayoutId, bind_group_layouts_length: usize, } #[repr(C)] pub struct SamplerDescriptor<'a> { - label: RawString, + label: Option<&'a nsACString>, address_modes: [wgt::AddressMode; 3], mag_filter: wgt::FilterMode, min_filter: wgt::FilterMode, @@ -238,7 +240,7 @@ pub struct SamplerDescriptor<'a> { #[repr(C)] pub struct TextureViewDescriptor<'a> { - label: RawString, + label: Option<&'a nsACString>, format: Option<&'a wgt::TextureFormat>, dimension: Option<&'a wgt::TextureViewDimension>, aspect: wgt::TextureAspect, @@ -250,7 +252,7 @@ pub struct TextureViewDescriptor<'a> { #[repr(C)] pub struct RenderBundleEncoderDescriptor<'a> { - label: RawString, + label: Option<&'a nsACString>, color_formats: *const wgt::TextureFormat, color_formats_length: usize, depth_stencil_format: Option<&'a wgt::TextureFormat>, @@ -434,10 +436,11 @@ pub extern "C" fn wgpu_client_adapter_extract_info( #[no_mangle] pub extern "C" fn wgpu_client_serialize_device_descriptor( - desc: &wgt::DeviceDescriptor, + desc: &wgt::DeviceDescriptor>, bb: &mut ByteBuf, ) { - *bb = make_byte_buf(&desc.map_label(cow_label)); + let label = wgpu_string(desc.label); + *bb = make_byte_buf(&desc.map_label(|_| label)); } #[no_mangle] @@ -472,9 +475,11 @@ pub extern "C" fn wgpu_client_make_buffer_id( pub extern "C" fn wgpu_client_create_texture( client: &Client, device_id: id::DeviceId, - desc: &wgt::TextureDescriptor, + desc: &wgt::TextureDescriptor>, bb: &mut ByteBuf, ) -> id::TextureId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -483,7 +488,7 @@ pub extern "C" fn wgpu_client_create_texture( .textures .alloc(backend); - let action = DeviceAction::CreateTexture(id, desc.map_label(cow_label)); + let action = DeviceAction::CreateTexture(id, desc.map_label(|_| label)); *bb = make_byte_buf(&action); id } @@ -495,6 +500,8 @@ pub extern "C" fn wgpu_client_create_texture_view( desc: &TextureViewDescriptor, bb: &mut ByteBuf, ) -> id::TextureViewId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -504,7 +511,7 @@ pub extern "C" fn wgpu_client_create_texture_view( .alloc(backend); let wgpu_desc = wgc::resource::TextureViewDescriptor { - label: cow_label(&desc.label), + label: label, format: desc.format.cloned(), dimension: desc.dimension.cloned(), range: wgt::ImageSubresourceRange { @@ -528,6 +535,8 @@ pub extern "C" fn wgpu_client_create_sampler( desc: &SamplerDescriptor, bb: &mut ByteBuf, ) -> id::SamplerId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -537,7 +546,7 @@ pub extern "C" fn wgpu_client_create_sampler( .alloc(backend); let wgpu_desc = wgc::resource::SamplerDescriptor { - label: cow_label(&desc.label), + label: label, address_modes: desc.address_modes, mag_filter: desc.mag_filter, min_filter: desc.min_filter, @@ -571,9 +580,11 @@ pub extern "C" fn wgpu_client_make_encoder_id( pub extern "C" fn wgpu_client_create_command_encoder( client: &Client, device_id: id::DeviceId, - desc: &wgt::CommandEncoderDescriptor, + desc: &wgt::CommandEncoderDescriptor>, bb: &mut ByteBuf, ) -> id::CommandEncoderId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -582,7 +593,7 @@ pub extern "C" fn wgpu_client_create_command_encoder( .command_buffers .alloc(backend); - let action = DeviceAction::CreateCommandEncoder(id, desc.map_label(cow_label)); + let action = DeviceAction::CreateCommandEncoder(id, desc.map_label(|_| label)); *bb = make_byte_buf(&action); id } @@ -593,12 +604,14 @@ pub extern "C" fn wgpu_device_create_render_bundle_encoder( desc: &RenderBundleEncoderDescriptor, bb: &mut ByteBuf, ) -> *mut wgc::command::RenderBundleEncoder { + let label = wgpu_string(desc.label); + let color_formats: Vec<_> = make_slice(desc.color_formats, desc.color_formats_length) .iter() .map(|format| Some(format.clone())) .collect(); let descriptor = wgc::command::RenderBundleEncoderDescriptor { - label: cow_label(&desc.label), + label: label, color_formats: Cow::Owned(color_formats), depth_stencil: desc .depth_stencil_format @@ -635,9 +648,11 @@ pub unsafe extern "C" fn wgpu_client_create_render_bundle( client: &Client, encoder: *mut wgc::command::RenderBundleEncoder, device_id: id::DeviceId, - desc: &wgt::RenderBundleDescriptor, + desc: &wgt::RenderBundleDescriptor>, bb: &mut ByteBuf, ) -> id::RenderBundleId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -647,14 +662,14 @@ pub unsafe extern "C" fn wgpu_client_create_render_bundle( .alloc(backend); let action = - DeviceAction::CreateRenderBundle(id, *Box::from_raw(encoder), desc.map_label(cow_label)); + DeviceAction::CreateRenderBundle(id, *Box::from_raw(encoder), desc.map_label(|_| label)); *bb = make_byte_buf(&action); id } #[repr(C)] -pub struct ComputePassDescriptor { - pub label: RawString, +pub struct ComputePassDescriptor<'a> { + pub label: Option<&'a nsACString>, } #[no_mangle] @@ -662,10 +677,12 @@ pub unsafe extern "C" fn wgpu_command_encoder_begin_compute_pass( encoder_id: id::CommandEncoderId, desc: &ComputePassDescriptor, ) -> *mut wgc::command::ComputePass { + let label = wgpu_string(desc.label); + let pass = wgc::command::ComputePass::new( encoder_id, &wgc::command::ComputePassDescriptor { - label: cow_label(&desc.label), + label: label, }, ); Box::into_raw(Box::new(pass)) @@ -686,8 +703,8 @@ pub unsafe extern "C" fn wgpu_compute_pass_destroy(pass: *mut wgc::command::Comp } #[repr(C)] -pub struct RenderPassDescriptor { - pub label: RawString, +pub struct RenderPassDescriptor<'a> { + pub label: Option<&'a nsACString>, pub color_attachments: *const wgc::command::RenderPassColorAttachment, pub color_attachments_length: usize, pub depth_stencil_attachment: *const wgc::command::RenderPassDepthStencilAttachment, @@ -698,6 +715,8 @@ pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( encoder_id: id::CommandEncoderId, desc: &RenderPassDescriptor, ) -> *mut wgc::command::RenderPass { + let label = wgpu_string(desc.label); + let color_attachments: Vec<_> = make_slice(desc.color_attachments, desc.color_attachments_length) .iter() .map(|format| Some(format.clone())) @@ -705,7 +724,7 @@ pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( let pass = wgc::command::RenderPass::new( encoder_id, &wgc::command::RenderPassDescriptor { - label: cow_label(&desc.label), + label: label, color_attachments: Cow::Owned(color_attachments), depth_stencil_attachment: desc.depth_stencil_attachment.as_ref(), }, @@ -734,6 +753,8 @@ pub unsafe extern "C" fn wgpu_client_create_bind_group_layout( desc: &BindGroupLayoutDescriptor, bb: &mut ByteBuf, ) -> id::BindGroupLayoutId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -803,7 +824,7 @@ pub unsafe extern "C" fn wgpu_client_create_bind_group_layout( }); } let wgpu_desc = wgc::binding_model::BindGroupLayoutDescriptor { - label: cow_label(&desc.label), + label: label, entries: Cow::Owned(entries), }; @@ -819,6 +840,8 @@ pub unsafe extern "C" fn wgpu_client_create_pipeline_layout( desc: &PipelineLayoutDescriptor, bb: &mut ByteBuf, ) -> id::PipelineLayoutId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -828,7 +851,7 @@ pub unsafe extern "C" fn wgpu_client_create_pipeline_layout( .alloc(backend); let wgpu_desc = wgc::binding_model::PipelineLayoutDescriptor { - label: cow_label(&desc.label), + label: label, bind_group_layouts: Cow::Borrowed(make_slice( desc.bind_group_layouts, desc.bind_group_layouts_length, @@ -848,6 +871,8 @@ pub unsafe extern "C" fn wgpu_client_create_bind_group( desc: &BindGroupDescriptor, bb: &mut ByteBuf, ) -> id::BindGroupId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let id = client .identities @@ -876,7 +901,7 @@ pub unsafe extern "C" fn wgpu_client_create_bind_group( }); } let wgpu_desc = wgc::binding_model::BindGroupDescriptor { - label: cow_label(&desc.label), + label: label, layout: desc.layout, entries: Cow::Owned(entries), }; @@ -945,12 +970,14 @@ pub unsafe extern "C" fn wgpu_client_create_render_pipeline( implicit_pipeline_layout_id: *mut Option, implicit_bind_group_layout_ids: *mut Option, ) -> id::RenderPipelineId { + let label = wgpu_string(desc.label); + let backend = device_id.backend(); let mut identities = client.identities.lock(); let id = identities.select(backend).render_pipelines.alloc(backend); let wgpu_desc = wgc::pipeline::RenderPipelineDescriptor { - label: cow_label(&desc.label), + label: label, layout: desc.layout, vertex: desc.vertex.to_wgpu(), fragment: desc.fragment.map(FragmentState::to_wgpu), @@ -1030,12 +1057,12 @@ pub unsafe extern "C" fn wgpu_command_encoder_copy_texture_to_texture( } #[no_mangle] -pub unsafe extern "C" fn wgpu_command_encoder_push_debug_group( - marker: RawString, +pub extern "C" fn wgpu_command_encoder_push_debug_group( + marker: &nsACString, bb: &mut ByteBuf, ) { - let cstr = std::ffi::CStr::from_ptr(marker); - let string = cstr.to_str().unwrap_or_default().to_string(); + + let string = marker.to_string(); let action = CommandEncoderAction::PushDebugGroup(string); *bb = make_byte_buf(&action); } @@ -1048,11 +1075,10 @@ pub unsafe extern "C" fn wgpu_command_encoder_pop_debug_group(bb: &mut ByteBuf) #[no_mangle] pub unsafe extern "C" fn wgpu_command_encoder_insert_debug_marker( - marker: RawString, + marker: &nsACString, bb: &mut ByteBuf, ) { - let cstr = std::ffi::CStr::from_ptr(marker); - let string = cstr.to_str().unwrap_or_default().to_string(); + let string = marker.to_string(); let action = CommandEncoderAction::InsertDebugMarker(string); *bb = make_byte_buf(&action); } diff --git a/gfx/wgpu_bindings/src/lib.rs b/gfx/wgpu_bindings/src/lib.rs index 71e587566189..877cfe9f287f 100644 --- a/gfx/wgpu_bindings/src/lib.rs +++ b/gfx/wgpu_bindings/src/lib.rs @@ -14,6 +14,8 @@ pub use wgc::device::trace::Command as CommandEncoderAction; use std::{borrow::Cow, mem, slice}; +use nsstring::nsACString; + type RawString = *const std::os::raw::c_char; //TODO: figure out why 'a and 'b have to be different here @@ -27,6 +29,12 @@ fn cow_label<'a, 'b>(raw: &'a RawString) -> Option> { } } +// Hides the repeated boilerplate of turning a `Option<&nsACString>` into a `Option`. +pub fn wgpu_string(gecko_string: Option<&nsACString>) -> Option> { + gecko_string.map(|s| s.to_utf8()) +} + + #[repr(C)] pub struct ByteBuf { data: *const u8, diff --git a/gfx/wgpu_bindings/src/server.rs b/gfx/wgpu_bindings/src/server.rs index 71465d0fd412..dad035911886 100644 --- a/gfx/wgpu_bindings/src/server.rs +++ b/gfx/wgpu_bindings/src/server.rs @@ -3,11 +3,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use crate::{ - cow_label, identity::IdentityRecyclerFactory, AdapterInformation, ByteBuf, - CommandEncoderAction, DeviceAction, DropAction, QueueWriteAction, RawString, TextureAction, + wgpu_string, identity::IdentityRecyclerFactory, AdapterInformation, ByteBuf, + CommandEncoderAction, DeviceAction, DropAction, QueueWriteAction, TextureAction, }; -use nsstring::nsString; +use nsstring::{nsString, nsACString, nsCString}; use wgc::{gfx_select, id}; use wgc::pipeline::CreateShaderModuleError; @@ -259,36 +259,35 @@ pub extern "C" fn wgpu_server_device_create_shader_module( global: &Global, self_id: id::DeviceId, module_id: id::ShaderModuleId, - label: RawString, - code: *const u8, - code_length: usize, + label: Option<&nsACString>, + code: &nsCString, out_message: &mut ShaderModuleCompilationMessage ) -> bool { - unsafe { - let label = cow_label(&label); + let utf8_label = label.map(|utf16| utf16.to_string()); + let label = utf8_label.as_ref().map(|s| Cow::from(&s[..])); - let source_str = std::str::from_utf8(std::slice::from_raw_parts(code, code_length)).unwrap(); - let source = wgc::pipeline::ShaderModuleSource::Wgsl(Cow::from(source_str)); + let source_str = code.to_utf8(); - let desc = wgc::pipeline::ShaderModuleDescriptor { - label, - shader_bound_checks: wgt::ShaderBoundChecks::new(), - }; + let source = wgc::pipeline::ShaderModuleSource::Wgsl(Cow::from(&source_str[..])); - let (_, error) = gfx_select!( - self_id => global.device_create_shader_module( - self_id, &desc, source, module_id - ) - ); + let desc = wgc::pipeline::ShaderModuleDescriptor { + label, + shader_bound_checks: wgt::ShaderBoundChecks::new(), + }; - if let Some(err) = error { - out_message.set_error(&err, source_str); - return false; - } + let (_, error) = gfx_select!( + self_id => global.device_create_shader_module( + self_id, &desc, source, module_id + ) + ); - // Avoid allocating the structure that holds errors in the common case (no errors). - return true; + if let Some(err) = error { + out_message.set_error(&err, &source_str[..]); + return false; } + + // Avoid allocating the structure that holds errors in the common case (no errors). + return true; } #[no_mangle] @@ -296,13 +295,15 @@ pub extern "C" fn wgpu_server_device_create_buffer( global: &Global, self_id: id::DeviceId, buffer_id: id::BufferId, - label_or_null: RawString, + label: Option<&nsACString>, size: wgt::BufferAddress, usage: u32, mapped_at_creation: bool, mut error_buf: ErrorBuffer, ) { - let label = cow_label(&label_or_null); + let utf8_label = label.map(|utf16| utf16.to_string()); + let label = utf8_label.as_ref().map(|s| Cow::from(&s[..])); + let usage = match wgt::BufferUsages::from_bits(usage) { Some(usage) => usage, None => { @@ -648,11 +649,14 @@ pub unsafe extern "C" fn wgpu_server_command_encoder_action( pub extern "C" fn wgpu_server_device_create_encoder( global: &Global, self_id: id::DeviceId, - desc: &wgt::CommandEncoderDescriptor, + desc: &wgt::CommandEncoderDescriptor>, new_id: id::CommandEncoderId, mut error_buf: ErrorBuffer, ) { - let desc = desc.map_label(cow_label); + let utf8_label = desc.label.map(|utf16| utf16.to_string()); + let label = utf8_label.as_ref().map(|s| Cow::from(&s[..])); + + let desc = desc.map_label(|_| label); let (_, error) = gfx_select!(self_id => global.device_create_command_encoder(self_id, &desc, new_id)); if let Some(err) = error { @@ -664,10 +668,11 @@ pub extern "C" fn wgpu_server_device_create_encoder( pub extern "C" fn wgpu_server_encoder_finish( global: &Global, self_id: id::CommandEncoderId, - desc: &wgt::CommandBufferDescriptor, + desc: &wgt::CommandBufferDescriptor>, mut error_buf: ErrorBuffer, ) { - let desc = desc.map_label(cow_label); + let label = wgpu_string(desc.label); + let desc = desc.map_label(|_| label); let (_, error) = gfx_select!(self_id => global.command_encoder_finish(self_id, &desc)); if let Some(err) = error { error_buf.init(err);