2023-01-14 16:07:46 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-09-03 17:23:35 +00:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-19 05:31:10 +00:00
|
|
|
use deno_core::op;
|
|
|
|
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;
|
|
|
|
|
|
|
|
use super::error::WebGpuResult;
|
|
|
|
pub(crate) struct WebGpuTexture(pub(crate) wgpu_core::id::TextureId);
|
|
|
|
impl Resource for WebGpuTexture {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUTexture".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct WebGpuTextureView(pub(crate) wgpu_core::id::TextureViewId);
|
|
|
|
impl Resource for WebGpuTextureView {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUTextureView".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreateTextureArgs {
|
|
|
|
device_rid: ResourceId,
|
|
|
|
label: Option<String>,
|
|
|
|
size: wgpu_types::Extent3d,
|
|
|
|
mip_level_count: u32,
|
|
|
|
sample_count: u32,
|
|
|
|
dimension: wgpu_types::TextureDimension,
|
|
|
|
format: wgpu_types::TextureFormat,
|
|
|
|
usage: u32,
|
2023-01-18 21:03:56 +00:00
|
|
|
view_formats: Vec<wgpu_types::TextureFormat>,
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_create_texture(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: CreateTextureArgs,
|
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
|
|
|
.get::<super::WebGpuDevice>(args.device_rid)?;
|
|
|
|
let device = device_resource.0;
|
|
|
|
|
|
|
|
let descriptor = wgpu_core::resource::TextureDescriptor {
|
|
|
|
label: args.label.map(Cow::from),
|
|
|
|
size: args.size,
|
|
|
|
mip_level_count: args.mip_level_count,
|
|
|
|
sample_count: args.sample_count,
|
|
|
|
dimension: args.dimension,
|
|
|
|
format: args.format,
|
|
|
|
usage: wgpu_types::TextureUsages::from_bits_truncate(args.usage),
|
2023-01-18 21:03:56 +00:00
|
|
|
view_formats: args.view_formats,
|
2021-09-03 17:23:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gfx_put!(device => instance.device_create_texture(
|
|
|
|
device,
|
|
|
|
&descriptor,
|
2022-09-02 22:37:21 +00:00
|
|
|
()
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuTexture)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreateTextureViewArgs {
|
|
|
|
texture_rid: ResourceId,
|
|
|
|
label: Option<String>,
|
|
|
|
format: Option<wgpu_types::TextureFormat>,
|
|
|
|
dimension: Option<wgpu_types::TextureViewDimension>,
|
2023-02-03 14:03:34 +00:00
|
|
|
#[serde(flatten)]
|
|
|
|
range: wgpu_types::ImageSubresourceRange,
|
2021-09-03 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 05:31:10 +00:00
|
|
|
#[op]
|
2021-09-03 17:23:35 +00:00
|
|
|
pub fn op_webgpu_create_texture_view(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: CreateTextureViewArgs,
|
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let texture_resource = state
|
|
|
|
.resource_table
|
|
|
|
.get::<WebGpuTexture>(args.texture_rid)?;
|
|
|
|
let texture = texture_resource.0;
|
|
|
|
|
|
|
|
let descriptor = wgpu_core::resource::TextureViewDescriptor {
|
|
|
|
label: args.label.map(Cow::from),
|
|
|
|
format: args.format,
|
|
|
|
dimension: args.dimension,
|
2023-02-03 14:03:34 +00:00
|
|
|
range: args.range,
|
2021-09-03 17:23:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gfx_put!(texture => instance.texture_create_view(
|
|
|
|
texture,
|
|
|
|
&descriptor,
|
2022-09-02 22:37:21 +00:00
|
|
|
()
|
2021-09-03 17:23:35 +00:00
|
|
|
) => state, WebGpuTextureView)
|
|
|
|
}
|