From b5ba17012a7d7c27a90dc53e6e98c693ec26f347 Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Thu, 20 Feb 2020 15:49:32 -0500 Subject: [PATCH] Update WebGPU to mozilla-central from hg rev 0f1a8e4c6a76b3b0b16902c7fdfe2356c60ca351 --- ffi/wgpu.h | 6 +++--- wgpu-core/src/command/compute.rs | 2 +- wgpu-core/src/command/mod.rs | 34 -------------------------------- wgpu-core/src/command/render.rs | 33 ++++++++++++++++++++++++------- wgpu-native/src/command.rs | 20 ++++++++++++++++++- wgpu-remote/src/lib.rs | 28 +++++++++++++++++++++++++- wgpu-remote/src/server.rs | 13 ++++++++++++ 7 files changed, 89 insertions(+), 47 deletions(-) diff --git a/ffi/wgpu.h b/ffi/wgpu.h index aa588a8216..06ee444f6b 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -726,9 +726,9 @@ WGPURawPass *wgpu_command_encoder_begin_compute_pass(WGPUCommandEncoderId encode /** * # Safety * - * This function is unsafe as there is no guarantee that the given pointer - * (`RenderPassDescriptor::color_attachments`) is valid for - * `RenderPassDescriptor::color_attachments_length` elements. + * This function is unsafe because improper use may lead to memory + * problems. For example, a double-free may occur if the function is called + * twice on the same raw pointer. */ WGPURawPass *wgpu_command_encoder_begin_render_pass(WGPUCommandEncoderId encoder_id, const WGPURenderPassDescriptor *desc); diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index eb30757335..4b299f7cc0 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -40,7 +40,7 @@ enum ComputeCommand { } impl super::RawPass { - pub fn new_compute(parent: id::CommandEncoderId) -> Self { + pub unsafe fn new_compute(parent: id::CommandEncoderId) -> Self { Self::from_vec(Vec::::with_capacity(1), parent) } diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 79f6e25212..6f9a17744e 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -218,40 +218,6 @@ pub struct RawRenderTargets { pub depth_stencil: RenderPassDepthStencilAttachmentDescriptor, } -/// # Safety -/// -/// This function is unsafe as there is no guarantee that the given pointer -/// (`RenderPassDescriptor::color_attachments`) is valid for -/// `RenderPassDescriptor::color_attachments_length` elements. -#[no_mangle] -pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( - encoder_id: id::CommandEncoderId, - desc: &RenderPassDescriptor, -) -> *mut RawPass { - let mut targets = RawRenderTargets { - depth_stencil: desc.depth_stencil_attachment - .cloned() - .unwrap_or_else(|| mem::zeroed()), - colors: mem::zeroed(), - }; - for (color, at) in targets.colors - .iter_mut() - .zip(slice::from_raw_parts(desc.color_attachments, desc.color_attachments_length)) - { - *color = RawRenderPassColorAttachmentDescriptor { - attachment: at.attachment, - resolve_target: at.resolve_target.map_or(id::TextureViewId::ERROR, |rt| *rt), - load_op: at.load_op, - store_op: at.store_op, - clear_color: at.clear_color, - }; - } - - let mut pass = RawPass::new_render(encoder_id); - pass.encode(&targets); - Box::into_raw(Box::new(pass)) -} - impl Global { pub fn command_encoder_finish( &self, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 5dd4157c4b..3d3a9611a8 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -6,6 +6,7 @@ use crate::{ command::{ bind::{Binder, LayoutChange}, PhantomSlice, + RawRenderPassColorAttachmentDescriptor, RawRenderTargets, }, conv, @@ -39,6 +40,7 @@ use std::{ marker::PhantomData, mem, ops::Range, + slice, }; @@ -155,8 +157,30 @@ enum RenderCommand { } impl super::RawPass { - pub fn new_render(parent_id: id::CommandEncoderId) -> Self { - Self::from_vec(Vec::::with_capacity(1), parent_id) + pub unsafe fn new_render(parent_id: id::CommandEncoderId, desc: &RenderPassDescriptor) -> Self { + let mut pass = Self::from_vec(Vec::::with_capacity(1), parent_id); + + let mut targets = RawRenderTargets { + depth_stencil: desc.depth_stencil_attachment + .cloned() + .unwrap_or_else(|| mem::zeroed()), + colors: mem::zeroed(), + }; + for (color, at) in targets.colors + .iter_mut() + .zip(slice::from_raw_parts(desc.color_attachments, desc.color_attachments_length)) + { + *color = RawRenderPassColorAttachmentDescriptor { + attachment: at.attachment, + resolve_target: at.resolve_target.map_or(id::TextureViewId::ERROR, |rt| *rt), + load_op: at.load_op, + store_op: at.store_op, + clear_color: at.clear_color, + }; + } + + pass.encode(&targets); + pass } pub unsafe fn finish_render(mut self) -> (Vec, id::CommandEncoderId) { @@ -1365,9 +1389,4 @@ pub mod render_ffi { *length = pass.size(); pass.base } - - #[no_mangle] - pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut RawPass) { - let _ = Box::from_raw(pass).into_vec(); - } } diff --git a/wgpu-native/src/command.rs b/wgpu-native/src/command.rs index 7a6084cf48..71ac36121a 100644 --- a/wgpu-native/src/command.rs +++ b/wgpu-native/src/command.rs @@ -5,7 +5,6 @@ use crate::GLOBAL; pub use core::command::{ - wgpu_command_encoder_begin_render_pass, compute_ffi::*, render_ffi::*, }; @@ -82,6 +81,20 @@ pub extern "C" fn wgpu_command_encoder_copy_texture_to_texture( } +/// # Safety +/// +/// This function is unsafe because improper use may lead to memory +/// problems. For example, a double-free may occur if the function is called +/// twice on the same raw pointer. +#[no_mangle] +pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( + encoder_id: id::CommandEncoderId, + desc: &core::command::RenderPassDescriptor, +) -> *mut core::command::RawPass { + let pass = core::command::RawPass::new_render(encoder_id, desc); + Box::into_raw(Box::new(pass)) +} + /// # Safety /// /// This function is unsafe because improper use may lead to memory @@ -93,6 +106,11 @@ pub unsafe extern "C" fn wgpu_render_pass_end_pass(pass_id: id::RenderPassId) { gfx_select!(encoder_id => GLOBAL.command_encoder_run_render_pass(encoder_id, &pass_data)) } +#[no_mangle] +pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut core::command::RawPass) { + let _ = Box::from_raw(pass).into_vec(); +} + /// # Safety /// /// This function is unsafe because improper use may lead to memory diff --git a/wgpu-remote/src/lib.rs b/wgpu-remote/src/lib.rs index b839bba27d..d48f1327ef 100644 --- a/wgpu-remote/src/lib.rs +++ b/wgpu-remote/src/lib.rs @@ -9,7 +9,6 @@ use core::{ }; pub use core::command::{ - wgpu_command_encoder_begin_render_pass, compute_ffi::*, render_ffi::*, }; @@ -32,6 +31,7 @@ struct IdentityHub { bind_groups: IdentityManager, shader_modules: IdentityManager, compute_pipelines: IdentityManager, + render_pipelines: IdentityManager, } #[derive(Debug, Default)] @@ -219,6 +219,19 @@ pub unsafe extern "C" fn wgpu_compute_pass_destroy(pass: core::command::RawPass) let _ = pass.into_vec(); } +#[no_mangle] +pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass( + encoder_id: id::CommandEncoderId, + desc: &core::command::RenderPassDescriptor, +) -> core::command::RawPass { + core::command::RawPass::new_render(encoder_id, desc) +} + +#[no_mangle] +pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: core::command::RawPass) { + let _ = pass.into_vec(); +} + #[no_mangle] pub extern "C" fn wgpu_client_make_bind_group_layout_id( client: &Client, @@ -353,3 +366,16 @@ pub extern "C" fn wgpu_client_kill_compute_pipeline_id( .compute_pipelines .free(id) } + +#[no_mangle] +pub extern "C" fn wgpu_client_kill_render_pipeline_id( + client: &Client, + id: id::RenderPipelineId, +) { + client + .identities + .lock() + .select(id.backend()) + .render_pipelines + .free(id) +} diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index c8065b518d..d69c03e7c1 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -190,6 +190,11 @@ pub unsafe extern "C" fn wgpu_server_encoder_copy_buffer_to_buffer( gfx_select!(self_id => global.command_encoder_copy_buffer_to_buffer(self_id, source_id, source_offset, destination_id, destination_offset, size)); } +/// # Safety +/// +/// This function is unsafe as there is no guarantee that the given pointers are +/// valid for `color_attachments_length` and `command_length` elements, +/// respectively. #[no_mangle] pub unsafe extern "C" fn wgpu_server_encode_compute_pass( global: &Global, @@ -321,3 +326,11 @@ pub extern "C" fn wgpu_server_compute_pipeline_destroy( ) { gfx_select!(self_id => global.compute_pipeline_destroy(self_id)); } + +#[no_mangle] +pub extern "C" fn wgpu_server_render_pipeline_destroy( + global: &Global, + self_id: id::RenderPipelineId, +) { + gfx_select!(self_id => global.render_pipeline_destroy(self_id)); +}