1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/ext/webgpu/bundle.rs

396 lines
11 KiB
Rust
Raw Normal View History

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2023-01-30 09:14:16 -05:00
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
2022-07-19 20:22:26 -04:00
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::rc::Rc;
use super::error::WebGpuResult;
2021-03-25 14:17:37 -04:00
struct WebGpuRenderBundleEncoder(
RefCell<wgpu_core::command::RenderBundleEncoder>,
);
2021-03-25 14:17:37 -04:00
impl Resource for WebGpuRenderBundleEncoder {
fn name(&self) -> Cow<str> {
"webGPURenderBundleEncoder".into()
}
}
2021-03-25 14:17:37 -04:00
pub(crate) struct WebGpuRenderBundle(pub(crate) wgpu_core::id::RenderBundleId);
impl Resource for WebGpuRenderBundle {
fn name(&self) -> Cow<str> {
"webGPURenderBundle".into()
}
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateRenderBundleEncoderArgs {
device_rid: ResourceId,
label: Option<String>,
2022-07-19 20:22:26 -04:00
color_formats: Vec<Option<wgpu_types::TextureFormat>>,
2022-01-24 17:47:05 -05:00
depth_stencil_format: Option<wgpu_types::TextureFormat>,
sample_count: u32,
depth_read_only: bool,
stencil_read_only: bool,
}
#[op]
pub fn op_webgpu_create_render_bundle_encoder(
state: &mut OpState,
args: CreateRenderBundleEncoderArgs,
) -> Result<WebGpuResult, AnyError> {
let device_resource = state
.resource_table
.get::<super::WebGpuDevice>(args.device_rid)?;
let device = device_resource.0;
2022-07-19 20:22:26 -04:00
let depth_stencil = args.depth_stencil_format.map(|format| {
wgpu_types::RenderBundleDepthStencil {
2022-01-24 17:47:05 -05:00
format,
depth_read_only: args.depth_read_only,
stencil_read_only: args.stencil_read_only,
2022-07-19 20:22:26 -04:00
}
});
let descriptor = wgpu_core::command::RenderBundleEncoderDescriptor {
label: args.label.map(Cow::from),
2022-07-19 20:22:26 -04:00
color_formats: Cow::from(args.color_formats),
sample_count: args.sample_count,
depth_stencil,
2022-01-24 17:47:05 -05:00
multiview: None,
};
let res =
wgpu_core::command::RenderBundleEncoder::new(&descriptor, device, None);
let (render_bundle_encoder, maybe_err) = match res {
Ok(encoder) => (encoder, None),
Err(e) => (
wgpu_core::command::RenderBundleEncoder::dummy(device),
Some(e),
),
};
let rid = state
.resource_table
2021-03-25 14:17:37 -04:00
.add(WebGpuRenderBundleEncoder(RefCell::new(
render_bundle_encoder,
)));
Ok(WebGpuResult::rid_err(rid, maybe_err))
}
#[op]
pub fn op_webgpu_render_bundle_encoder_finish(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
label: Option<String>,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.take::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
let render_bundle_encoder = Rc::try_unwrap(render_bundle_encoder_resource)
.ok()
.expect("unwrapping render_bundle_encoder_resource should succeed")
.0
.into_inner();
let instance = state.borrow::<super::Instance>();
gfx_put!(render_bundle_encoder.parent() => instance.render_bundle_encoder_finish(
render_bundle_encoder,
&wgpu_core::command::RenderBundleDescriptor {
2022-07-19 20:22:26 -04:00
label: label.map(Cow::from),
},
2023-01-30 09:14:16 -05:00
()
) => state, WebGpuRenderBundle)
}
2022-07-19 20:22:26 -04:00
#[op]
pub fn op_webgpu_render_bundle_encoder_set_bind_group(
state: &mut OpState,
render_bundle_encoder_rid: ResourceId,
index: u32,
bind_group: ResourceId,
dynamic_offsets_data: ZeroCopyBuf,
dynamic_offsets_data_start: usize,
dynamic_offsets_data_length: usize,
) -> Result<WebGpuResult, AnyError> {
let bind_group_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<super::binding::WebGpuBindGroup>(bind_group)?;
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
// Align the data
2022-07-19 20:22:26 -04:00
assert!(dynamic_offsets_data.len() % std::mem::size_of::<u32>() == 0);
let (prefix, dynamic_offsets_data, suffix) =
2023-01-30 09:14:16 -05:00
// SAFETY: A u8 to u32 cast is safe because we asserted that the length is a
// multiple of 4.
2022-07-19 20:22:26 -04:00
unsafe { dynamic_offsets_data.align_to::<u32>() };
assert!(prefix.is_empty());
assert!(suffix.is_empty());
2022-07-19 20:22:26 -04:00
let start = dynamic_offsets_data_start;
let len = dynamic_offsets_data_length;
// Assert that length and start are both in bounds
assert!(start <= dynamic_offsets_data.len());
assert!(len <= dynamic_offsets_data.len() - start);
let dynamic_offsets_data: &[u32] = &dynamic_offsets_data[start..start + len];
// SAFETY: the raw pointer and length are of the same slice, and that slice
// lives longer than the below function invocation.
unsafe {
wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_bind_group(
&mut render_bundle_encoder_resource.0.borrow_mut(),
2022-07-19 20:22:26 -04:00
index,
bind_group_resource.0,
dynamic_offsets_data.as_ptr(),
dynamic_offsets_data.len(),
);
}
Ok(WebGpuResult::empty())
}
#[op]
pub fn op_webgpu_render_bundle_encoder_push_debug_group(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
group_label: String,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
2022-07-19 20:22:26 -04:00
let label = std::ffi::CString::new(group_label).unwrap();
// SAFETY: the string the raw pointer points to lives longer than the below
// function invocation.
unsafe {
wgpu_core::command::bundle_ffi::wgpu_render_bundle_push_debug_group(
&mut render_bundle_encoder_resource.0.borrow_mut(),
label.as_ptr(),
);
}
Ok(WebGpuResult::empty())
}
#[op]
pub fn op_webgpu_render_bundle_encoder_pop_debug_group(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
wgpu_core::command::bundle_ffi::wgpu_render_bundle_pop_debug_group(
&mut render_bundle_encoder_resource.0.borrow_mut(),
);
Ok(WebGpuResult::empty())
}
#[op]
pub fn op_webgpu_render_bundle_encoder_insert_debug_marker(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
marker_label: String,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
2022-07-19 20:22:26 -04:00
let label = std::ffi::CString::new(marker_label).unwrap();
// SAFETY: the string the raw pointer points to lives longer than the below
// function invocation.
unsafe {
wgpu_core::command::bundle_ffi::wgpu_render_bundle_insert_debug_marker(
&mut render_bundle_encoder_resource.0.borrow_mut(),
label.as_ptr(),
);
}
Ok(WebGpuResult::empty())
}
#[op]
pub fn op_webgpu_render_bundle_encoder_set_pipeline(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
pipeline: ResourceId,
) -> Result<WebGpuResult, AnyError> {
let render_pipeline_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<super::pipeline::WebGpuRenderPipeline>(pipeline)?;
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_pipeline(
&mut render_bundle_encoder_resource.0.borrow_mut(),
render_pipeline_resource.0,
);
Ok(WebGpuResult::empty())
}
2022-07-19 20:22:26 -04:00
#[op]
pub fn op_webgpu_render_bundle_encoder_set_index_buffer(
state: &mut OpState,
render_bundle_encoder_rid: ResourceId,
buffer: ResourceId,
2022-01-24 17:47:05 -05:00
index_format: wgpu_types::IndexFormat,
offset: u64,
size: u64,
) -> Result<WebGpuResult, AnyError> {
let buffer_resource = state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<super::buffer::WebGpuBuffer>(buffer)?;
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
2023-01-30 09:14:16 -05:00
let size = Some(
std::num::NonZeroU64::new(size)
.ok_or_else(|| type_error("size must be larger than 0"))?,
);
render_bundle_encoder_resource
.0
.borrow_mut()
2023-01-30 09:14:16 -05:00
.set_index_buffer(buffer_resource.0, index_format, offset, size);
Ok(WebGpuResult::empty())
}
2022-07-19 20:22:26 -04:00
#[op]
pub fn op_webgpu_render_bundle_encoder_set_vertex_buffer(
state: &mut OpState,
render_bundle_encoder_rid: ResourceId,
slot: u32,
buffer: ResourceId,
offset: u64,
size: Option<u64>,
) -> Result<WebGpuResult, AnyError> {
let buffer_resource = state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<super::buffer::WebGpuBuffer>(buffer)?;
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
let size = if let Some(size) = size {
Some(
std::num::NonZeroU64::new(size)
.ok_or_else(|| type_error("size must be larger than 0"))?,
)
} else {
None
};
wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_vertex_buffer(
&mut render_bundle_encoder_resource.0.borrow_mut(),
2022-07-19 20:22:26 -04:00
slot,
buffer_resource.0,
2022-07-19 20:22:26 -04:00
offset,
2023-01-30 09:14:16 -05:00
size,
);
Ok(WebGpuResult::empty())
}
2022-07-19 20:22:26 -04:00
#[op]
pub fn op_webgpu_render_bundle_encoder_draw(
state: &mut OpState,
render_bundle_encoder_rid: ResourceId,
vertex_count: u32,
instance_count: u32,
first_vertex: u32,
first_instance: u32,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw(
&mut render_bundle_encoder_resource.0.borrow_mut(),
2022-07-19 20:22:26 -04:00
vertex_count,
instance_count,
first_vertex,
first_instance,
);
Ok(WebGpuResult::empty())
}
2022-07-19 20:22:26 -04:00
#[op]
pub fn op_webgpu_render_bundle_encoder_draw_indexed(
state: &mut OpState,
render_bundle_encoder_rid: ResourceId,
index_count: u32,
instance_count: u32,
first_index: u32,
base_vertex: i32,
first_instance: u32,
) -> Result<WebGpuResult, AnyError> {
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw_indexed(
&mut render_bundle_encoder_resource.0.borrow_mut(),
2022-07-19 20:22:26 -04:00
index_count,
instance_count,
first_index,
base_vertex,
first_instance,
);
Ok(WebGpuResult::empty())
}
#[op]
pub fn op_webgpu_render_bundle_encoder_draw_indirect(
state: &mut OpState,
2022-07-19 20:22:26 -04:00
render_bundle_encoder_rid: ResourceId,
indirect_buffer: ResourceId,
indirect_offset: u64,
) -> Result<WebGpuResult, AnyError> {
let buffer_resource = state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<super::buffer::WebGpuBuffer>(indirect_buffer)?;
let render_bundle_encoder_resource =
state
.resource_table
2022-07-19 20:22:26 -04:00
.get::<WebGpuRenderBundleEncoder>(render_bundle_encoder_rid)?;
wgpu_core::command::bundle_ffi::wgpu_render_bundle_draw_indirect(
&mut render_bundle_encoder_resource.0.borrow_mut(),
buffer_resource.0,
2022-07-19 20:22:26 -04:00
indirect_offset,
);
Ok(WebGpuResult::empty())
}