2024-04-02 18:36:04 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2023-10-04 04:25:56 +00:00
|
|
|
use deno_core::op2;
|
2022-03-19 05:31:10 +00:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
2021-09-03 17:23:35 +00:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::borrow::Cow;
|
2023-03-03 17:18:01 +00:00
|
|
|
use std::rc::Rc;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
|
2023-03-03 17:18:01 +00:00
|
|
|
pub(crate) struct WebGpuBindGroupLayout(
|
|
|
|
pub(crate) crate::Instance,
|
|
|
|
pub(crate) wgpu_core::id::BindGroupLayoutId,
|
|
|
|
);
|
2021-09-03 17:23:35 +00:00
|
|
|
impl Resource for WebGpuBindGroupLayout {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBindGroupLayout".into()
|
|
|
|
}
|
2023-03-03 17:18:01 +00:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
2024-08-06 21:17:47 +00:00
|
|
|
self.0.bind_group_layout_drop(self.1);
|
2023-03-03 17:18:01 +00:00
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 17:18:01 +00:00
|
|
|
pub(crate) struct WebGpuBindGroup(
|
|
|
|
pub(crate) crate::Instance,
|
|
|
|
pub(crate) wgpu_core::id::BindGroupId,
|
|
|
|
);
|
2021-09-03 17:23:35 +00:00
|
|
|
impl Resource for WebGpuBindGroup {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUBindGroup".into()
|
|
|
|
}
|
2023-03-03 17:18:01 +00:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
2024-08-06 21:17:47 +00:00
|
|
|
self.0.bind_group_drop(self.1);
|
2023-03-03 17:18:01 +00:00
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GpuBufferBindingLayout {
|
|
|
|
r#type: GpuBufferBindingType,
|
|
|
|
has_dynamic_offset: bool,
|
|
|
|
min_binding_size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
enum GpuBufferBindingType {
|
|
|
|
Uniform,
|
|
|
|
Storage,
|
|
|
|
ReadOnlyStorage,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuBufferBindingType> for wgpu_types::BufferBindingType {
|
|
|
|
fn from(binding_type: GpuBufferBindingType) -> Self {
|
|
|
|
match binding_type {
|
|
|
|
GpuBufferBindingType::Uniform => wgpu_types::BufferBindingType::Uniform,
|
|
|
|
GpuBufferBindingType::Storage => {
|
|
|
|
wgpu_types::BufferBindingType::Storage { read_only: false }
|
|
|
|
}
|
|
|
|
GpuBufferBindingType::ReadOnlyStorage => {
|
|
|
|
wgpu_types::BufferBindingType::Storage { read_only: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GpuSamplerBindingLayout {
|
2021-11-17 04:08:03 +00:00
|
|
|
r#type: wgpu_types::SamplerBindingType,
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GpuTextureBindingLayout {
|
|
|
|
sample_type: GpuTextureSampleType,
|
|
|
|
view_dimension: wgpu_types::TextureViewDimension,
|
|
|
|
multisampled: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
enum GpuTextureSampleType {
|
|
|
|
Float,
|
|
|
|
UnfilterableFloat,
|
|
|
|
Depth,
|
|
|
|
Sint,
|
|
|
|
Uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuTextureSampleType> for wgpu_types::TextureSampleType {
|
|
|
|
fn from(sample_type: GpuTextureSampleType) -> Self {
|
|
|
|
match sample_type {
|
|
|
|
GpuTextureSampleType::Float => {
|
|
|
|
wgpu_types::TextureSampleType::Float { filterable: true }
|
|
|
|
}
|
|
|
|
GpuTextureSampleType::UnfilterableFloat => {
|
|
|
|
wgpu_types::TextureSampleType::Float { filterable: false }
|
|
|
|
}
|
|
|
|
GpuTextureSampleType::Depth => wgpu_types::TextureSampleType::Depth,
|
|
|
|
GpuTextureSampleType::Sint => wgpu_types::TextureSampleType::Sint,
|
|
|
|
GpuTextureSampleType::Uint => wgpu_types::TextureSampleType::Uint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GpuStorageTextureBindingLayout {
|
2024-04-02 18:36:04 +00:00
|
|
|
access: wgpu_types::StorageTextureAccess,
|
2021-09-03 17:23:35 +00:00
|
|
|
format: wgpu_types::TextureFormat,
|
|
|
|
view_dimension: wgpu_types::TextureViewDimension,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub struct GpuBindGroupLayoutEntry {
|
2021-09-03 17:23:35 +00:00
|
|
|
binding: u32,
|
|
|
|
visibility: u32,
|
|
|
|
#[serde(flatten)]
|
|
|
|
binding_type: GpuBindingType,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
enum GpuBindingType {
|
|
|
|
Buffer(GpuBufferBindingLayout),
|
|
|
|
Sampler(GpuSamplerBindingLayout),
|
|
|
|
Texture(GpuTextureBindingLayout),
|
|
|
|
StorageTexture(GpuStorageTextureBindingLayout),
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
impl From<GpuBindingType> for wgpu_types::BindingType {
|
|
|
|
fn from(binding_type: GpuBindingType) -> wgpu_types::BindingType {
|
|
|
|
match binding_type {
|
2021-09-03 17:23:35 +00:00
|
|
|
GpuBindingType::Buffer(buffer) => wgpu_types::BindingType::Buffer {
|
|
|
|
ty: buffer.r#type.into(),
|
|
|
|
has_dynamic_offset: buffer.has_dynamic_offset,
|
|
|
|
min_binding_size: std::num::NonZeroU64::new(buffer.min_binding_size),
|
|
|
|
},
|
2021-11-17 04:08:03 +00:00
|
|
|
GpuBindingType::Sampler(sampler) => wgpu_types::BindingType::Sampler(sampler.r#type),
|
2021-09-03 17:23:35 +00:00
|
|
|
GpuBindingType::Texture(texture) => wgpu_types::BindingType::Texture {
|
|
|
|
sample_type: texture.sample_type.into(),
|
|
|
|
view_dimension: texture.view_dimension,
|
|
|
|
multisampled: texture.multisampled,
|
|
|
|
},
|
|
|
|
GpuBindingType::StorageTexture(storage_texture) => {
|
|
|
|
wgpu_types::BindingType::StorageTexture {
|
2024-04-02 18:36:04 +00:00
|
|
|
access: storage_texture.access,
|
2021-09-03 17:23:35 +00:00
|
|
|
format: storage_texture.format,
|
|
|
|
view_dimension: storage_texture.view_dimension,
|
|
|
|
}
|
|
|
|
}
|
2022-03-24 17:20:10 +00:00
|
|
|
}
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_create_bind_group_layout(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] device_rid: ResourceId,
|
|
|
|
#[string] label: Cow<str>,
|
|
|
|
#[serde] entries: Vec<GpuBindGroupLayoutEntry>,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuDevice>(device_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let device = device_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let entries = entries
|
|
|
|
.into_iter()
|
|
|
|
.map(|entry| {
|
|
|
|
wgpu_types::BindGroupLayoutEntry {
|
|
|
|
binding: entry.binding,
|
|
|
|
visibility: wgpu_types::ShaderStages::from_bits(entry.visibility).unwrap(),
|
|
|
|
ty: entry.binding_type.into(),
|
|
|
|
count: None, // native-only
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let descriptor = wgpu_core::binding_model::BindGroupLayoutDescriptor {
|
2023-10-04 04:25:56 +00:00
|
|
|
label: Some(label),
|
2021-09-03 17:23:35 +00:00
|
|
|
entries: Cow::from(entries),
|
|
|
|
};
|
|
|
|
|
2024-08-06 21:17:47 +00:00
|
|
|
gfx_put!(instance.device_create_bind_group_layout(
|
2021-09-03 17:23:35 +00:00
|
|
|
device,
|
|
|
|
&descriptor,
|
2024-01-29 14:37:57 +00:00
|
|
|
None
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuBindGroupLayout)
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_create_pipeline_layout(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] device_rid: ResourceId,
|
|
|
|
#[string] label: Cow<str>,
|
|
|
|
#[serde] bind_group_layouts: Vec<u32>,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuDevice>(device_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let device = device_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let bind_group_layouts = bind_group_layouts
|
|
|
|
.into_iter()
|
|
|
|
.map(|rid| {
|
|
|
|
let bind_group_layout = state.resource_table.get::<WebGpuBindGroupLayout>(rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
Ok(bind_group_layout.1)
|
2022-03-24 17:20:10 +00:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let descriptor = wgpu_core::binding_model::PipelineLayoutDescriptor {
|
2023-10-04 04:25:56 +00:00
|
|
|
label: Some(label),
|
2021-09-03 17:23:35 +00:00
|
|
|
bind_group_layouts: Cow::from(bind_group_layouts),
|
|
|
|
push_constant_ranges: Default::default(),
|
|
|
|
};
|
|
|
|
|
2024-08-06 21:17:47 +00:00
|
|
|
gfx_put!(instance.device_create_pipeline_layout(
|
2021-09-03 17:23:35 +00:00
|
|
|
device,
|
|
|
|
&descriptor,
|
2024-01-29 14:37:57 +00:00
|
|
|
None
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, super::pipeline::WebGpuPipelineLayout)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub struct GpuBindGroupEntry {
|
2021-09-03 17:23:35 +00:00
|
|
|
binding: u32,
|
|
|
|
kind: String,
|
|
|
|
resource: ResourceId,
|
|
|
|
offset: Option<u64>,
|
|
|
|
size: Option<u64>,
|
|
|
|
}
|
|
|
|
|
2023-10-04 04:25:56 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-24 17:20:10 +00:00
|
|
|
pub fn op_webgpu_create_bind_group(
|
|
|
|
state: &mut OpState,
|
2023-10-04 04:25:56 +00:00
|
|
|
#[smi] device_rid: ResourceId,
|
|
|
|
#[string] label: Cow<str>,
|
|
|
|
#[smi] layout: ResourceId,
|
|
|
|
#[serde] entries: Vec<GpuBindGroupEntry>,
|
2021-09-03 17:23:35 +00:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::WebGpuDevice>(device_rid)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
let device = device_resource.1;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
2022-03-24 17:20:10 +00:00
|
|
|
let entries = entries
|
|
|
|
.into_iter()
|
|
|
|
.map(|entry| {
|
|
|
|
Ok(wgpu_core::binding_model::BindGroupEntry {
|
|
|
|
binding: entry.binding,
|
|
|
|
resource: match entry.kind.as_str() {
|
|
|
|
"GPUSampler" => {
|
|
|
|
let sampler_resource = state
|
2021-09-03 17:23:35 +00:00
|
|
|
.resource_table
|
2022-03-24 17:20:10 +00:00
|
|
|
.get::<super::sampler::WebGpuSampler>(entry.resource)?;
|
2023-03-03 17:18:01 +00:00
|
|
|
wgpu_core::binding_model::BindingResource::Sampler(sampler_resource.1)
|
2022-03-24 17:20:10 +00:00
|
|
|
}
|
|
|
|
"GPUTextureView" => {
|
|
|
|
let texture_view_resource =
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::texture::WebGpuTextureView>(entry.resource)?;
|
|
|
|
wgpu_core::binding_model::BindingResource::TextureView(
|
2023-03-03 17:18:01 +00:00
|
|
|
texture_view_resource.1,
|
2022-03-24 17:20:10 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
"GPUBufferBinding" => {
|
|
|
|
let buffer_resource = state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::buffer::WebGpuBuffer>(entry.resource)?;
|
|
|
|
wgpu_core::binding_model::BindingResource::Buffer(
|
|
|
|
wgpu_core::binding_model::BufferBinding {
|
2023-03-03 17:18:01 +00:00
|
|
|
buffer_id: buffer_resource.1,
|
2022-03-24 17:20:10 +00:00
|
|
|
offset: entry.offset.unwrap_or(0),
|
|
|
|
size: std::num::NonZeroU64::new(entry.size.unwrap_or(0)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
|
|
|
|
|
|
|
let bind_group_layout = state.resource_table.get::<WebGpuBindGroupLayout>(layout)?;
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
let descriptor = wgpu_core::binding_model::BindGroupDescriptor {
|
2023-10-04 04:25:56 +00:00
|
|
|
label: Some(label),
|
2023-03-03 17:18:01 +00:00
|
|
|
layout: bind_group_layout.1,
|
2021-09-03 17:23:35 +00:00
|
|
|
entries: Cow::from(entries),
|
|
|
|
};
|
|
|
|
|
2024-08-06 21:17:47 +00:00
|
|
|
gfx_put!(instance.device_create_bind_group(
|
2021-09-03 17:23:35 +00:00
|
|
|
device,
|
|
|
|
&descriptor,
|
2024-01-29 14:37:57 +00:00
|
|
|
None
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuBindGroup)
|
|
|
|
}
|