The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
//! WebGPU enum strings ↔ wgpu types.
use wgpu::TextureFormat as F;
use wgpu::{AstcBlock, AstcChannel};
const FORMATS: &[(&str, F)] = &[
("r8unorm", F::R8Unorm),
("r8snorm", F::R8Snorm),
("r8uint", F::R8Uint),
("r8sint", F::R8Sint),
("r16uint", F::R16Uint),
("r16sint", F::R16Sint),
("r16unorm", F::R16Unorm),
("r16snorm", F::R16Snorm),
("r16float", F::R16Float),
("rg8unorm", F::Rg8Unorm),
("rg8snorm", F::Rg8Snorm),
("rg8uint", F::Rg8Uint),
("rg8sint", F::Rg8Sint),
("r32uint", F::R32Uint),
("r32sint", F::R32Sint),
("r32float", F::R32Float),
("rg16uint", F::Rg16Uint),
("rg16sint", F::Rg16Sint),
("rg16unorm", F::Rg16Unorm),
("rg16snorm", F::Rg16Snorm),
("rg16float", F::Rg16Float),
("rgba8unorm", F::Rgba8Unorm),
("rgba8unorm-srgb", F::Rgba8UnormSrgb),
("rgba8snorm", F::Rgba8Snorm),
("rgba8uint", F::Rgba8Uint),
("rgba8sint", F::Rgba8Sint),
("bgra8unorm", F::Bgra8Unorm),
("bgra8unorm-srgb", F::Bgra8UnormSrgb),
("rgb9e5ufloat", F::Rgb9e5Ufloat),
("rgb10a2uint", F::Rgb10a2Uint),
("rgb10a2unorm", F::Rgb10a2Unorm),
("rg11b10ufloat", F::Rg11b10Ufloat),
("rg32uint", F::Rg32Uint),
("rg32sint", F::Rg32Sint),
("rg32float", F::Rg32Float),
("rgba16uint", F::Rgba16Uint),
("rgba16sint", F::Rgba16Sint),
("rgba16unorm", F::Rgba16Unorm),
("rgba16snorm", F::Rgba16Snorm),
("rgba16float", F::Rgba16Float),
("rgba32uint", F::Rgba32Uint),
("rgba32sint", F::Rgba32Sint),
("rgba32float", F::Rgba32Float),
("stencil8", F::Stencil8),
("depth16unorm", F::Depth16Unorm),
("depth24plus", F::Depth24Plus),
("depth24plus-stencil8", F::Depth24PlusStencil8),
("depth32float", F::Depth32Float),
("depth32float-stencil8", F::Depth32FloatStencil8),
("bc1-rgba-unorm", F::Bc1RgbaUnorm),
("bc1-rgba-unorm-srgb", F::Bc1RgbaUnormSrgb),
("bc2-rgba-unorm", F::Bc2RgbaUnorm),
("bc2-rgba-unorm-srgb", F::Bc2RgbaUnormSrgb),
("bc3-rgba-unorm", F::Bc3RgbaUnorm),
("bc3-rgba-unorm-srgb", F::Bc3RgbaUnormSrgb),
("bc4-r-unorm", F::Bc4RUnorm),
("bc4-r-snorm", F::Bc4RSnorm),
("bc5-rg-unorm", F::Bc5RgUnorm),
("bc5-rg-snorm", F::Bc5RgSnorm),
("bc6h-rgb-ufloat", F::Bc6hRgbUfloat),
("bc6h-rgb-float", F::Bc6hRgbFloat),
("bc7-rgba-unorm", F::Bc7RgbaUnorm),
("bc7-rgba-unorm-srgb", F::Bc7RgbaUnormSrgb),
("etc2-rgb8unorm", F::Etc2Rgb8Unorm),
("etc2-rgb8unorm-srgb", F::Etc2Rgb8UnormSrgb),
("etc2-rgb8a1unorm", F::Etc2Rgb8A1Unorm),
("etc2-rgb8a1unorm-srgb", F::Etc2Rgb8A1UnormSrgb),
("etc2-rgba8unorm", F::Etc2Rgba8Unorm),
("etc2-rgba8unorm-srgb", F::Etc2Rgba8UnormSrgb),
("eac-r11unorm", F::EacR11Unorm),
("eac-r11snorm", F::EacR11Snorm),
("eac-rg11unorm", F::EacRg11Unorm),
("eac-rg11snorm", F::EacRg11Snorm),
];
const ASTC_BLOCKS: &[(&str, AstcBlock)] = &[
("4x4", AstcBlock::B4x4),
("5x4", AstcBlock::B5x4),
("5x5", AstcBlock::B5x5),
("6x5", AstcBlock::B6x5),
("6x6", AstcBlock::B6x6),
("8x5", AstcBlock::B8x5),
("8x6", AstcBlock::B8x6),
("8x8", AstcBlock::B8x8),
("10x5", AstcBlock::B10x5),
("10x6", AstcBlock::B10x6),
("10x8", AstcBlock::B10x8),
("10x10", AstcBlock::B10x10),
("12x10", AstcBlock::B12x10),
("12x12", AstcBlock::B12x12),
];
pub fn texture_format(s: &str) -> Option<F> {
if let Some(rest) = s.strip_prefix("astc-") {
let (block, channel) = if let Some(b) = rest.strip_suffix("-unorm-srgb") {
(b, AstcChannel::UnormSrgb)
} else if let Some(b) = rest.strip_suffix("-unorm") {
(b, AstcChannel::Unorm)
} else {
return None;
};
let block = ASTC_BLOCKS.iter().find(|(n, _)| *n == block)?.1;
return Some(F::Astc { block, channel });
}
FORMATS.iter().find(|(n, _)| *n == s).map(|(_, f)| *f)
}
pub fn texture_format_name(f: F) -> String {
if let F::Astc { block, channel } = f {
let b = ASTC_BLOCKS.iter().find(|(_, x)| *x == block).map(|(n, _)| *n).unwrap_or("4x4");
return format!("astc-{b}-{}", if channel == AstcChannel::UnormSrgb { "unorm-srgb" } else { "unorm" });
}
FORMATS.iter().find(|(_, x)| *x == f).map(|(n, _)| n.to_string()).unwrap_or_else(|| "rgba8unorm".into())
}
pub fn vertex_format(s: &str) -> Option<wgpu::VertexFormat> {
use wgpu::VertexFormat as V;
Some(match s {
"uint8" => V::Uint8,
"uint8x2" => V::Uint8x2,
"uint8x4" => V::Uint8x4,
"sint8" => V::Sint8,
"sint8x2" => V::Sint8x2,
"sint8x4" => V::Sint8x4,
"unorm8" => V::Unorm8,
"unorm8x2" => V::Unorm8x2,
"unorm8x4" => V::Unorm8x4,
"snorm8" => V::Snorm8,
"snorm8x2" => V::Snorm8x2,
"snorm8x4" => V::Snorm8x4,
"uint16" => V::Uint16,
"uint16x2" => V::Uint16x2,
"uint16x4" => V::Uint16x4,
"sint16" => V::Sint16,
"sint16x2" => V::Sint16x2,
"sint16x4" => V::Sint16x4,
"unorm16" => V::Unorm16,
"unorm16x2" => V::Unorm16x2,
"unorm16x4" => V::Unorm16x4,
"snorm16" => V::Snorm16,
"snorm16x2" => V::Snorm16x2,
"snorm16x4" => V::Snorm16x4,
"float16" => V::Float16,
"float16x2" => V::Float16x2,
"float16x4" => V::Float16x4,
"float32" => V::Float32,
"float32x2" => V::Float32x2,
"float32x3" => V::Float32x3,
"float32x4" => V::Float32x4,
"uint32" => V::Uint32,
"uint32x2" => V::Uint32x2,
"uint32x3" => V::Uint32x3,
"uint32x4" => V::Uint32x4,
"sint32" => V::Sint32,
"sint32x2" => V::Sint32x2,
"sint32x3" => V::Sint32x3,
"sint32x4" => V::Sint32x4,
"unorm10-10-10-2" => V::Unorm10_10_10_2,
"unorm8x4-bgra" => V::Unorm8x4Bgra,
_ => return None,
})
}
pub fn compare(s: &str) -> Option<wgpu::CompareFunction> {
use wgpu::CompareFunction as C;
Some(match s {
"never" => C::Never,
"less" => C::Less,
"equal" => C::Equal,
"less-equal" => C::LessEqual,
"greater" => C::Greater,
"not-equal" => C::NotEqual,
"greater-equal" => C::GreaterEqual,
"always" => C::Always,
_ => return None,
})
}
pub fn stencil_op(s: &str) -> Option<wgpu::StencilOperation> {
use wgpu::StencilOperation as S;
Some(match s {
"keep" => S::Keep,
"zero" => S::Zero,
"replace" => S::Replace,
"invert" => S::Invert,
"increment-clamp" => S::IncrementClamp,
"decrement-clamp" => S::DecrementClamp,
"increment-wrap" => S::IncrementWrap,
"decrement-wrap" => S::DecrementWrap,
_ => return None,
})
}
pub fn blend_factor(s: &str) -> Option<wgpu::BlendFactor> {
use wgpu::BlendFactor as B;
Some(match s {
"zero" => B::Zero,
"one" => B::One,
"src" => B::Src,
"one-minus-src" => B::OneMinusSrc,
"src-alpha" => B::SrcAlpha,
"one-minus-src-alpha" => B::OneMinusSrcAlpha,
"dst" => B::Dst,
"one-minus-dst" => B::OneMinusDst,
"dst-alpha" => B::DstAlpha,
"one-minus-dst-alpha" => B::OneMinusDstAlpha,
"src-alpha-saturated" => B::SrcAlphaSaturated,
"constant" => B::Constant,
"one-minus-constant" => B::OneMinusConstant,
"src1" => B::Src1,
"one-minus-src1" => B::OneMinusSrc1,
"src1-alpha" => B::Src1Alpha,
"one-minus-src1-alpha" => B::OneMinusSrc1Alpha,
_ => return None,
})
}
pub fn blend_op(s: &str) -> Option<wgpu::BlendOperation> {
use wgpu::BlendOperation as B;
Some(match s {
"add" => B::Add,
"subtract" => B::Subtract,
"reverse-subtract" => B::ReverseSubtract,
"min" => B::Min,
"max" => B::Max,
_ => return None,
})
}
pub fn topology(s: &str) -> Option<wgpu::PrimitiveTopology> {
use wgpu::PrimitiveTopology as P;
Some(match s {
"point-list" => P::PointList,
"line-list" => P::LineList,
"line-strip" => P::LineStrip,
"triangle-list" => P::TriangleList,
"triangle-strip" => P::TriangleStrip,
_ => return None,
})
}
pub fn index_format(s: &str) -> Option<wgpu::IndexFormat> {
match s {
"uint16" => Some(wgpu::IndexFormat::Uint16),
"uint32" => Some(wgpu::IndexFormat::Uint32),
_ => None,
}
}
pub fn address_mode(s: &str) -> Option<wgpu::AddressMode> {
match s {
"clamp-to-edge" => Some(wgpu::AddressMode::ClampToEdge),
"repeat" => Some(wgpu::AddressMode::Repeat),
"mirror-repeat" => Some(wgpu::AddressMode::MirrorRepeat),
_ => None,
}
}
pub fn filter(s: &str) -> Option<wgpu::FilterMode> {
match s {
"nearest" => Some(wgpu::FilterMode::Nearest),
"linear" => Some(wgpu::FilterMode::Linear),
_ => None,
}
}
pub fn view_dimension(s: &str) -> Option<wgpu::TextureViewDimension> {
use wgpu::TextureViewDimension as D;
Some(match s {
"1d" => D::D1,
"2d" => D::D2,
"2d-array" => D::D2Array,
"cube" => D::Cube,
"cube-array" => D::CubeArray,
"3d" => D::D3,
_ => return None,
})
}
pub fn texture_dimension(s: &str) -> Option<wgpu::TextureDimension> {
match s {
"1d" => Some(wgpu::TextureDimension::D1),
"2d" => Some(wgpu::TextureDimension::D2),
"3d" => Some(wgpu::TextureDimension::D3),
_ => None,
}
}
pub fn aspect(s: &str) -> Option<wgpu::TextureAspect> {
match s {
"all" => Some(wgpu::TextureAspect::All),
"stencil-only" => Some(wgpu::TextureAspect::StencilOnly),
"depth-only" => Some(wgpu::TextureAspect::DepthOnly),
_ => None,
}
}
/// Aspect codes used in the command stream (0 all, 1 stencil-only, 2 depth-only).
pub fn aspect_code(c: u32) -> wgpu::TextureAspect {
match c {
1 => wgpu::TextureAspect::StencilOnly,
2 => wgpu::TextureAspect::DepthOnly,
_ => wgpu::TextureAspect::All,
}
}
/// WebGPU feature name ↔ wgpu feature, for the features this binding exposes to pages.
/// Features that change the WGSL a page generates in ways naga may not accept (`shader-f16`,
/// `subgroups`, `clip-distances`, `dual-source-blending`) are deliberately not exposed, and so is
/// `timestamp-query`: wgpu 25's Metal backend samples pass timestamps at vertex/fragment stage
/// boundaries and the end-of-pass sample reads 0 on Apple GPUs here, so three.js's
/// `trackTimestamp` would report garbage (without the feature it turns tracking off).
pub const FEATURES: &[(&str, wgpu::Features)] = &[
("depth-clip-control", wgpu::Features::DEPTH_CLIP_CONTROL),
("depth32float-stencil8", wgpu::Features::DEPTH32FLOAT_STENCIL8),
("texture-compression-bc", wgpu::Features::TEXTURE_COMPRESSION_BC),
("texture-compression-etc2", wgpu::Features::TEXTURE_COMPRESSION_ETC2),
("texture-compression-astc", wgpu::Features::TEXTURE_COMPRESSION_ASTC),
("indirect-first-instance", wgpu::Features::INDIRECT_FIRST_INSTANCE),
("rg11b10ufloat-renderable", wgpu::Features::RG11B10UFLOAT_RENDERABLE),
("bgra8unorm-storage", wgpu::Features::BGRA8UNORM_STORAGE),
("float32-filterable", wgpu::Features::FLOAT32_FILTERABLE),
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_roundtrip() {
for (name, f) in FORMATS {
assert_eq!(texture_format(name), Some(*f));
assert_eq!(texture_format_name(*f), *name);
}
let astc = texture_format("astc-10x8-unorm-srgb").unwrap();
assert_eq!(astc, F::Astc { block: AstcBlock::B10x8, channel: AstcChannel::UnormSrgb });
assert_eq!(texture_format_name(astc), "astc-10x8-unorm-srgb");
assert_eq!(texture_format("rgba8"), None);
assert_eq!(texture_format("astc-3x3-unorm"), None);
}
}