2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2021-03-01 05:31:13 -05:00
|
|
|
use deno_core::{OpState, Resource};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
use super::error::WebGpuResult;
|
2021-03-01 05:31:13 -05:00
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
pub(crate) struct WebGpuSampler(pub(crate) wgpu_core::id::SamplerId);
|
|
|
|
impl Resource for WebGpuSampler {
|
2021-03-01 05:31:13 -05:00
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"webGPUSampler".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
enum GpuAddressMode {
|
|
|
|
ClampToEdge,
|
|
|
|
Repeat,
|
|
|
|
MirrorRepeat,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuAddressMode> for wgpu_types::AddressMode {
|
|
|
|
fn from(value: GpuAddressMode) -> wgpu_types::AddressMode {
|
|
|
|
match value {
|
|
|
|
GpuAddressMode::ClampToEdge => wgpu_types::AddressMode::ClampToEdge,
|
|
|
|
GpuAddressMode::Repeat => wgpu_types::AddressMode::Repeat,
|
|
|
|
GpuAddressMode::MirrorRepeat => wgpu_types::AddressMode::MirrorRepeat,
|
|
|
|
}
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
enum GpuFilterMode {
|
|
|
|
Nearest,
|
|
|
|
Linear,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuFilterMode> for wgpu_types::FilterMode {
|
|
|
|
fn from(value: GpuFilterMode) -> wgpu_types::FilterMode {
|
|
|
|
match value {
|
|
|
|
GpuFilterMode::Nearest => wgpu_types::FilterMode::Nearest,
|
|
|
|
GpuFilterMode::Linear => wgpu_types::FilterMode::Linear,
|
|
|
|
}
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:32:25 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
pub enum GpuCompareFunction {
|
|
|
|
Never,
|
|
|
|
Less,
|
|
|
|
Equal,
|
|
|
|
LessEqual,
|
|
|
|
Greater,
|
|
|
|
NotEqual,
|
|
|
|
GreaterEqual,
|
|
|
|
Always,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GpuCompareFunction> for wgpu_types::CompareFunction {
|
|
|
|
fn from(value: GpuCompareFunction) -> wgpu_types::CompareFunction {
|
|
|
|
match value {
|
|
|
|
GpuCompareFunction::Never => wgpu_types::CompareFunction::Never,
|
|
|
|
GpuCompareFunction::Less => wgpu_types::CompareFunction::Less,
|
|
|
|
GpuCompareFunction::Equal => wgpu_types::CompareFunction::Equal,
|
|
|
|
GpuCompareFunction::LessEqual => wgpu_types::CompareFunction::LessEqual,
|
|
|
|
GpuCompareFunction::Greater => wgpu_types::CompareFunction::Greater,
|
|
|
|
GpuCompareFunction::NotEqual => wgpu_types::CompareFunction::NotEqual,
|
|
|
|
GpuCompareFunction::GreaterEqual => {
|
|
|
|
wgpu_types::CompareFunction::GreaterEqual
|
|
|
|
}
|
|
|
|
GpuCompareFunction::Always => wgpu_types::CompareFunction::Always,
|
|
|
|
}
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CreateSamplerArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
device_rid: ResourceId,
|
2021-03-01 05:31:13 -05:00
|
|
|
label: Option<String>,
|
2021-08-24 14:32:25 -04:00
|
|
|
address_mode_u: GpuAddressMode,
|
|
|
|
address_mode_v: GpuAddressMode,
|
|
|
|
address_mode_w: GpuAddressMode,
|
|
|
|
mag_filter: GpuFilterMode,
|
|
|
|
min_filter: GpuFilterMode,
|
|
|
|
mipmap_filter: GpuFilterMode,
|
|
|
|
lod_min_clamp: f32,
|
|
|
|
lod_max_clamp: f32,
|
|
|
|
compare: Option<GpuCompareFunction>,
|
|
|
|
max_anisotropy: u8,
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_webgpu_create_sampler(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: CreateSamplerArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<WebGpuResult, AnyError> {
|
2021-03-01 05:31:13 -05:00
|
|
|
let instance = state.borrow::<super::Instance>();
|
|
|
|
let device_resource = state
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<super::WebGpuDevice>(args.device_rid)?;
|
2021-03-01 05:31:13 -05:00
|
|
|
let device = device_resource.0;
|
|
|
|
|
|
|
|
let descriptor = wgpu_core::resource::SamplerDescriptor {
|
|
|
|
label: args.label.map(Cow::from),
|
|
|
|
address_modes: [
|
2021-08-24 14:32:25 -04:00
|
|
|
args.address_mode_u.into(),
|
|
|
|
args.address_mode_v.into(),
|
|
|
|
args.address_mode_w.into(),
|
2021-03-01 05:31:13 -05:00
|
|
|
],
|
2021-08-24 14:32:25 -04:00
|
|
|
mag_filter: args.mag_filter.into(),
|
|
|
|
min_filter: args.min_filter.into(),
|
|
|
|
mipmap_filter: args.mipmap_filter.into(),
|
|
|
|
lod_min_clamp: args.lod_min_clamp,
|
|
|
|
lod_max_clamp: args.lod_max_clamp,
|
|
|
|
compare: args.compare.map(Into::into),
|
|
|
|
anisotropy_clamp: std::num::NonZeroU8::new(args.max_anisotropy),
|
2021-03-01 05:31:13 -05:00
|
|
|
border_color: None, // native-only
|
|
|
|
};
|
|
|
|
|
2021-05-03 10:42:59 -04:00
|
|
|
gfx_put!(device => instance.device_create_sampler(
|
2021-03-01 05:31:13 -05:00
|
|
|
device,
|
|
|
|
&descriptor,
|
|
|
|
std::marker::PhantomData
|
2021-05-03 10:42:59 -04:00
|
|
|
) => state, WebGpuSampler)
|
2021-03-01 05:31:13 -05:00
|
|
|
}
|