use super::*;
impl Texture2DDescriptor {
pub fn default_for(width: u32, height: u32, format: GpuTextureFormat) -> Self {
Self {
width,
height,
format,
mip_level_count: 1,
sample_count: 1,
}
}
}
impl TextureViewDescriptor {
pub fn full() -> Self {
Self {
format: None,
dimension: None,
base_mip_level: 0,
mip_level_count: 0,
base_array_layer: 0,
array_layer_count: 0,
aspect: None,
}
}
pub(crate) fn effective_dimension(&self) -> &'static str {
self.try_get_dimension()
.unwrap_or(WEBGPU_TEXTURE_VIEW_DIMENSION_2D)
}
pub(crate) fn effective_aspect(&self) -> &'static str {
self.try_get_aspect().unwrap_or(WEBGPU_TEXTURE_ASPECT_ALL)
}
pub fn mip(level: u32) -> Self {
Self {
format: None,
dimension: None,
base_mip_level: level,
mip_level_count: 1,
base_array_layer: 0,
array_layer_count: 0,
aspect: None,
}
}
pub fn depth_only() -> Self {
Self {
format: None,
dimension: None,
base_mip_level: 0,
mip_level_count: 0,
base_array_layer: 0,
array_layer_count: 0,
aspect: Some(WEBGPU_TEXTURE_ASPECT_DEPTH_ONLY),
}
}
}
impl TextureWriteDescriptor {
pub fn for_2d(data: Vec<u8>, bytes_per_row: u32, texture: JsValue) -> Self {
Self {
data,
bytes_per_row,
rows_per_image: 0,
mip_level: 0,
texture,
origin: None,
flip_y: false,
}
}
}
impl BindGroupEntry {
pub fn binding(&self) -> u32 {
match self {
Self::Buffer { binding, .. }
| Self::Texture { binding, .. }
| Self::StorageTexture { binding, .. }
| Self::Sampler { binding, .. } => *binding,
}
}
}
impl BindGroupLayoutEntry {
pub fn uniform(binding: u32, visibility: ShaderStage) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::UniformBuffer,
}
}
pub fn storage(binding: u32, visibility: ShaderStage, read_only: bool) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::StorageBuffer { read_only },
}
}
pub fn texture(binding: u32, visibility: ShaderStage, sample_type: &str) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::SampledTexture {
sample_type: sample_type.to_string(),
multisampled: false,
},
}
}
pub fn texture_multisampled(binding: u32, visibility: ShaderStage, sample_type: &str) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::SampledTexture {
sample_type: sample_type.to_string(),
multisampled: true,
},
}
}
pub fn storage_texture(
binding: u32,
visibility: ShaderStage,
format: &str,
read_only: bool,
) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::StorageTexture {
read_only,
format: format.to_string(),
},
}
}
pub fn sampler(binding: u32, visibility: ShaderStage) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::Sampler {
filtering: true,
comparison: false,
},
}
}
pub fn sampler_non_filtering(binding: u32, visibility: ShaderStage) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::Sampler {
filtering: false,
comparison: false,
},
}
}
pub fn sampler_comparison(binding: u32, visibility: ShaderStage) -> Self {
Self {
binding,
visibility,
ty: BindGroupEntryType::Sampler {
filtering: false,
comparison: true,
},
}
}
}
impl ColorTargetState {
pub fn for_format(format: GpuTextureFormat) -> Self {
Self {
format,
blend: None,
write_mask: WEBGPU_WRITE_MASK_ALL_CHANNELS,
}
}
}
impl MultisampleState {
pub fn with_sample_count(count: u32) -> Self {
Self {
count,
mask: WEBGPU_MULTISAMPLE_MASK_ALL,
}
}
}
impl DepthStencilState {
pub fn writing_depth(format: GpuTextureFormat) -> Self {
Self {
format,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
}
}
}
impl BlendComponent {
pub fn source_alpha_over() -> Self {
Self {
operation: BlendOperation::Add,
source: BlendFactor::SourceAlpha,
destination: BlendFactor::OneMinusSourceAlpha,
}
}
}
impl BlendState {
pub fn alpha_over() -> Self {
Self {
color: BlendComponent::source_alpha_over(),
alpha: BlendComponent::source_alpha_over(),
}
}
}
impl SamplerDescriptor {
pub fn nearest_clamp() -> Self {
Self {
filter: FilterMode::Nearest,
mipmap_filter: MipmapFilter::Nearest,
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
compare: None,
}
}
}
impl DrawArgs {
pub fn whole_stream(vertex_count: u32, instance_count: u32) -> Self {
Self {
vertex_count,
instance_count,
first_vertex: 0,
first_instance: 0,
}
}
}
impl DrawIndexedArgs {
pub fn whole_buffer(index_count: u32, instance_count: u32) -> Self {
Self {
index_count,
instance_count,
first_index: 0,
base_vertex: 0,
first_instance: 0,
}
}
}
impl DispatchArgs {
pub fn grid(x: u32, y: u32, z: u32) -> Self {
Self { x, y, z }
}
}