.
diff --git a/sparse_strips/vello_hybrid/src/render/webgl/mod.rs b/sparse_strips/vello_hybrid/src/render/webgl/mod.rs index faae943..e9b3f8c 100644 --- a/sparse_strips/vello_hybrid/src/render/webgl/mod.rs +++ b/sparse_strips/vello_hybrid/src/render/webgl/mod.rs
@@ -2251,10 +2251,11 @@ fn initialize_packed_u32_attribs(gl: &WebGl2RenderingContext, count: u32, stride: i32) { for loc in 0..count { gl.enable_vertex_attrib_array(loc); - gl.vertex_attrib_i_pointer_with_i32( + gl.vertex_attrib_pointer_with_i32( loc, 1, - WebGl2RenderingContext::UNSIGNED_INT, + WebGl2RenderingContext::FLOAT, + false, stride, i32::try_from(loc).unwrap() * size_of::<u32>() as i32, ); @@ -2585,10 +2586,11 @@ let offset = i * 4; gl.enable_vertex_attrib_array(location); - gl.vertex_attrib_i_pointer_with_i32( + gl.vertex_attrib_pointer_with_i32( location, 1, - WebGl2RenderingContext::UNSIGNED_INT, + WebGl2RenderingContext::FLOAT, + false, STRIP_STRIDE, offset, ); @@ -2678,10 +2680,11 @@ fn set_strip_attrib_offset(&self, first_instance: i32) { let byte_offset = first_instance * STRIP_STRIDE; for i in 0..STRIP_ATTR_COUNT { - self.gl.vertex_attrib_i_pointer_with_i32( + self.gl.vertex_attrib_pointer_with_i32( i as u32, 1, - WebGl2RenderingContext::UNSIGNED_INT, + WebGl2RenderingContext::FLOAT, + false, STRIP_STRIDE, i * 4 + byte_offset, );
diff --git a/sparse_strips/vello_hybrid/src/render/wgpu/mod.rs b/sparse_strips/vello_hybrid/src/render/wgpu/mod.rs index b15b60b..d2e3bf6 100644 --- a/sparse_strips/vello_hybrid/src/render/wgpu/mod.rs +++ b/sparse_strips/vello_hybrid/src/render/wgpu/mod.rs
@@ -1093,12 +1093,12 @@ /// Vertex attributes for the strip pub fn vertex_attributes() -> [wgpu::VertexAttribute; 6] { wgpu::vertex_attr_array![ - 0 => Uint32, - 1 => Uint32, - 2 => Uint32, - 3 => Uint32, - 4 => Uint32, - 5 => Uint32, + 0 => Float32, + 1 => Float32, + 2 => Float32, + 3 => Float32, + 4 => Float32, + 5 => Float32, ] } } @@ -1319,9 +1319,9 @@ array_stride: size_of::<GpuClearInstance>() as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![ - 0 => Uint32x2, - 1 => Uint32x2, - 2 => Uint32x2, + 0 => Float32x2, + 1 => Float32x2, + 2 => Float32x2, ], }], compilation_options: PipelineCompilationOptions::default(), @@ -1453,15 +1453,15 @@ array_stride: size_of::<FilterInstanceData>() as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![ - 0 => Uint32, - 1 => Uint32, - 2 => Uint32, - 3 => Uint32, - 4 => Uint32, - 5 => Uint32, - 6 => Uint32, - 7 => Uint32, - 8 => Uint32, + 0 => Float32, + 1 => Float32, + 2 => Float32, + 3 => Float32, + 4 => Float32, + 5 => Float32, + 6 => Float32, + 7 => Float32, + 8 => Float32, ], }], compilation_options: PipelineCompilationOptions::default(), @@ -1560,24 +1560,24 @@ array_stride: size_of::<GpuBlendInstance>() as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![ - 0 => Uint32, - 1 => Uint32, - 2 => Uint32, - 3 => Uint32, - 4 => Uint32, - 5 => Uint32, - 6 => Uint32, - 7 => Uint32, + 0 => Float32, + 1 => Float32, + 2 => Float32, + 3 => Float32, + 4 => Float32, + 5 => Float32, + 6 => Float32, + 7 => Float32, ], }; let copy_vertex_state = wgpu::VertexBufferLayout { array_stride: size_of::<GpuCopyInstance>() as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array![ - 0 => Uint32, - 1 => Uint32, - 2 => Uint32, - 3 => Uint32, + 0 => Float32, + 1 => Float32, + 2 => Float32, + 3 => Float32, ], }; let create_texture_op_pipeline =
diff --git a/sparse_strips/vello_sparse_shaders/shaders/blend.wesl b/sparse_strips/vello_sparse_shaders/shaders/blend.wesl index 3c6b6a5..b6ecafc 100644 --- a/sparse_strips/vello_sparse_shaders/shaders/blend.wesl +++ b/sparse_strips/vello_sparse_shaders/shaders/blend.wesl
@@ -40,14 +40,14 @@ const BLEND_HAS_ALPHA = 1u << 26u; struct BlendInstance { - @location(0) geometry_origin: u32, - @location(1) geometry_size: u32, - @location(2) geometry_alpha_col_idx: u32, - @location(3) parent_texture_size: u32, - @location(4) child_texture_origin: u32, - @location(5) child_parent_origin: u32, - @location(6) child_rect_size: u32, - @location(7) blend_config: u32, + @location(0) geometry_origin: f32, + @location(1) geometry_size: f32, + @location(2) geometry_alpha_col_idx: f32, + @location(3) parent_texture_size: f32, + @location(4) child_texture_origin: f32, + @location(5) child_parent_origin: f32, + @location(6) child_rect_size: f32, + @location(7) blend_config: f32, } struct VertexOutput { @@ -76,11 +76,14 @@ @builtin(vertex_index) vertex_index: u32, instance: BlendInstance, ) -> VertexOutput { - let geometry_origin = unpack_u16_pair(instance.geometry_origin); - let geometry_size = vec2<f32>(unpack_u16_pair(instance.geometry_size)); - let parent_texture_size = unpack_u16_pair(instance.parent_texture_size); - let child_parent_origin = unpack_u16_pair(instance.child_parent_origin); - let child_texture_origin = unpack_u16_pair(instance.child_texture_origin); + let geometry_origin = unpack_u16_pair(bitcast<u32>(instance.geometry_origin)); + let geometry_size = vec2<f32>(unpack_u16_pair(bitcast<u32>(instance.geometry_size))); + let geometry_alpha_col_idx = bitcast<u32>(instance.geometry_alpha_col_idx); + let parent_texture_size = unpack_u16_pair(bitcast<u32>(instance.parent_texture_size)); + let child_parent_origin = unpack_u16_pair(bitcast<u32>(instance.child_parent_origin)); + let child_texture_origin = unpack_u16_pair(bitcast<u32>(instance.child_texture_origin)); + let child_rect_size = bitcast<u32>(instance.child_rect_size); + let blend_config = bitcast<u32>(instance.blend_config); let x = f32(vertex_index & 1u); let y = f32(vertex_index >> 1u); @@ -91,10 +94,10 @@ out.parent_texture_xy = parent_texture_xy; out.child_texture_xy = vec2<f32>(child_texture_origin) + child_local; out.child_local = child_local; - out.child_rect_size = instance.child_rect_size; - out.blend_config = instance.blend_config; + out.child_rect_size = child_rect_size; + out.blend_config = blend_config; out.tex_coord = vec2<f32>( - f32(instance.geometry_alpha_col_idx) + x * geometry_size.x, + f32(geometry_alpha_col_idx) + x * geometry_size.x, y * geometry_size.y, );
diff --git a/sparse_strips/vello_sparse_shaders/shaders/clear.wesl b/sparse_strips/vello_sparse_shaders/shaders/clear.wesl index 7353225..3de64c9 100644 --- a/sparse_strips/vello_sparse_shaders/shaders/clear.wesl +++ b/sparse_strips/vello_sparse_shaders/shaders/clear.wesl
@@ -4,10 +4,13 @@ @vertex fn vs_main( @builtin(vertex_index) vertex_index: u32, - @location(0) origin: vec2<u32>, - @location(1) size: vec2<u32>, - @location(2) target_size: vec2<u32>, + @location(0) origin_bits: vec2<f32>, + @location(1) size_bits: vec2<f32>, + @location(2) target_size_bits: vec2<f32>, ) -> @builtin(position) vec4<f32> { + let origin = bitcast<vec2<u32>>(origin_bits); + let size = bitcast<vec2<u32>>(size_bits); + let target_size = bitcast<vec2<u32>>(target_size_bits); let x = f32(vertex_index & 1u); let y = f32(vertex_index >> 1u);
diff --git a/sparse_strips/vello_sparse_shaders/shaders/copy.wesl b/sparse_strips/vello_sparse_shaders/shaders/copy.wesl index cca0ddc..92fffe2 100644 --- a/sparse_strips/vello_sparse_shaders/shaders/copy.wesl +++ b/sparse_strips/vello_sparse_shaders/shaders/copy.wesl
@@ -4,10 +4,10 @@ //! Copy rectangular regions between intermediate textures. struct CopyInstance { - @location(0) dest_texture_origin: u32, - @location(1) source_texture_origin: u32, - @location(2) copy_rect_size: u32, - @location(3) dest_texture_size: u32, + @location(0) dest_texture_origin: f32, + @location(1) source_texture_origin: f32, + @location(2) copy_rect_size: f32, + @location(3) dest_texture_size: f32, } struct VertexOutput { @@ -27,10 +27,10 @@ @builtin(vertex_index) vertex_index: u32, instance: CopyInstance, ) -> VertexOutput { - let dest_texture_origin = unpack_u16_pair(instance.dest_texture_origin); - let source_texture_origin = unpack_u16_pair(instance.source_texture_origin); - let copy_rect_size = unpack_u16_pair(instance.copy_rect_size); - let dest_texture_size = unpack_u16_pair(instance.dest_texture_size); + let dest_texture_origin = unpack_u16_pair(bitcast<u32>(instance.dest_texture_origin)); + let source_texture_origin = unpack_u16_pair(bitcast<u32>(instance.source_texture_origin)); + let copy_rect_size = unpack_u16_pair(bitcast<u32>(instance.copy_rect_size)); + let dest_texture_size = unpack_u16_pair(bitcast<u32>(instance.dest_texture_size)); let x = f32(vertex_index & 1u); let y = f32(vertex_index >> 1u);
diff --git a/sparse_strips/vello_sparse_shaders/shaders/filter.wesl b/sparse_strips/vello_sparse_shaders/shaders/filter.wesl index 5973d8f..e1daa7e 100644 --- a/sparse_strips/vello_sparse_shaders/shaders/filter.wesl +++ b/sparse_strips/vello_sparse_shaders/shaders/filter.wesl
@@ -99,15 +99,15 @@ fn get_drop_shadow_color(texel2: vec4<u32>) -> u32 { return texel2.z; } struct FilterInstanceData { - @location(0) source_origin: u32, - @location(1) source_size: u32, - @location(2) dest_origin: u32, - @location(3) dest_size: u32, - @location(4) dest_texture_size: u32, - @location(5) filter_data_offset: u32, - @location(6) original_origin: u32, - @location(7) original_size: u32, - @location(8) filter_pass_kind: u32, + @location(0) source_origin: f32, + @location(1) source_size: f32, + @location(2) dest_origin: f32, + @location(3) dest_size: f32, + @location(4) dest_texture_size: f32, + @location(5) filter_data_offset: f32, + @location(6) original_origin: f32, + @location(7) original_size: f32, + @location(8) filter_pass_kind: f32, } struct FilterVertexOutput { @@ -133,13 +133,15 @@ let quad_vertex = vertex_index % 4u; let x = f32((quad_vertex & 1u)); let y = f32((quad_vertex >> 1u)); - let source_origin = unpack_u16_pair(instance.source_origin); - let source_size = unpack_u16_pair(instance.source_size); - let dest_origin = unpack_u16_pair(instance.dest_origin); - let dest_size = unpack_u16_pair(instance.dest_size); - let dest_texture_size = vec2<f32>(unpack_u16_pair(instance.dest_texture_size)); - let original_origin = unpack_u16_pair(instance.original_origin); - let original_size = unpack_u16_pair(instance.original_size); + let source_origin = unpack_u16_pair(bitcast<u32>(instance.source_origin)); + let source_size = unpack_u16_pair(bitcast<u32>(instance.source_size)); + let dest_origin = unpack_u16_pair(bitcast<u32>(instance.dest_origin)); + let dest_size = unpack_u16_pair(bitcast<u32>(instance.dest_size)); + let dest_texture_size = vec2<f32>(unpack_u16_pair(bitcast<u32>(instance.dest_texture_size))); + let filter_data_offset = bitcast<u32>(instance.filter_data_offset); + let original_origin = unpack_u16_pair(bitcast<u32>(instance.original_origin)); + let original_size = unpack_u16_pair(bitcast<u32>(instance.original_size)); + let filter_pass_kind = bitcast<u32>(instance.filter_pass_kind); // Decimated passes only write `dest_size`, but must also clear the bottom/right border // of stale pixels to transparent so our bilinear taps don't accidentally sample stale pixels. @@ -152,13 +154,13 @@ var out: FilterVertexOutput; out.position = vec4<f32>(ndc_x, ndc_y, 0.0, 1.0); - out.filter_data_offset = instance.filter_data_offset; + out.filter_data_offset = filter_data_offset; out.source_origin = source_origin; out.source_size = source_size; out.dest_origin = dest_origin; out.dest_size = dest_size; out.original_origin = original_origin; - out.filter_pass_kind = instance.filter_pass_kind; + out.filter_pass_kind = filter_pass_kind; return out; }
diff --git a/sparse_strips/vello_sparse_shaders/shaders/render.wesl b/sparse_strips/vello_sparse_shaders/shaders/render.wesl index bc38799..09ec622 100644 --- a/sparse_strips/vello_sparse_shaders/shaders/render.wesl +++ b/sparse_strips/vello_sparse_shaders/shaders/render.wesl
@@ -150,7 +150,7 @@ struct StripInstance { // [x, y] packed as u16's // x, y — coordinates of the strip or rect - @location(0) xy: u32, + @location(0) xy: f32, // [width, dense_width] packed as u16's // width — width of the strip or rect // dense_width — width of the portion where alpha blending should be applied @@ -160,17 +160,17 @@ // TODO: In the future, this could be optimized such that `width` always represents the width and a simple // 1-bit flag is used to distinguish between sparse fill region and strip. This frees up 15 other bits. // Otherwise, it might also be possible to merge a strip and sparse fill command into a single strip instance. - @location(1) widths_or_rect_height: u32, + @location(1) widths_or_rect_height: f32, // For normal strips: alpha texture column index where this strip's alpha values begin. // There are [`Config::strip_height`] alpha values per column. // For rect strips: packed fractional edge offsets for AA. - @location(2) col_idx_or_rect_frac: u32, + @location(2) col_idx_or_rect_frac: f32, // See StripInstance documentation above. - @location(3) payload: u32, + @location(3) payload: f32, // See StripInstance documentation above. - @location(4) paint_and_rect_flag: u32, + @location(4) paint_and_rect_flag: f32, // Painter's-order index for z-depth computation. - @location(5) depth_index: u32, + @location(5) depth_index: f32, } struct VertexOutput { @@ -216,24 +216,30 @@ ) -> VertexOutput { var out: VertexOutput; out.sample_xy = vec2(0.0); + let xy = bitcast<u32>(instance.xy); + let widths_or_rect_height = bitcast<u32>(instance.widths_or_rect_height); + let col_idx_or_rect_frac = bitcast<u32>(instance.col_idx_or_rect_frac); + let payload = bitcast<u32>(instance.payload); + let paint_and_rect_flag = bitcast<u32>(instance.paint_and_rect_flag); + let depth_index = bitcast<u32>(instance.depth_index); // Map vertex_index (0-3) to quad corners: // 0 → (0,0), 1 → (1,0), 2 → (0,1), 3 → (1,1) let x = f32(in_vertex_index & 1u); let y = f32(in_vertex_index >> 1u); // Unpack the x and y coordinates from the packed u32 instance.xy - let x0 = instance.xy & 0xffffu; - let y0 = instance.xy >> 16u; - let width = instance.widths_or_rect_height & 0xffffu; - let dense_width = instance.widths_or_rect_height >> 16u; + let x0 = xy & 0xffffu; + let y0 = xy >> 16u; + let width = widths_or_rect_height & 0xffffu; + let dense_width = widths_or_rect_height >> 16u; - let is_rect = (instance.paint_and_rect_flag & RECT_STRIP_FLAG) != 0u; + let is_rect = (paint_and_rect_flag & RECT_STRIP_FLAG) != 0u; var height = config.strip_height; if is_rect { height = dense_width; out.dense_end_or_rect_size = width | (dense_width << 16u); - out.rect_frac = instance.col_idx_or_rect_frac; + out.rect_frac = col_idx_or_rect_frac; } else { - out.dense_end_or_rect_size = instance.col_idx_or_rect_frac + dense_width; + out.dense_end_or_rect_size = col_idx_or_rect_frac + dense_width; out.rect_frac = 0u; } // Calculate the pixel coordinates of the current vertex within the strip. @@ -245,15 +251,15 @@ let ndc_x = pix_x * 2.0 / f32(config.width) - 1.0; let ndc_y = 1.0 - pix_y * 2.0 / f32(config.height); - let color_source = (instance.paint_and_rect_flag >> 29u) & 0x3u; + let color_source = (paint_and_rect_flag >> 29u) & 0x3u; if color_source == COLOR_SOURCE_PAYLOAD { - let paint_type = (instance.paint_and_rect_flag >> 26u) & 0x7u; + let paint_type = (paint_and_rect_flag >> 26u) & 0x7u; // Unpack view coordinates for image sampling and gradient calculations - let scene_strip_x = instance.payload & 0xffffu; - let scene_strip_y = instance.payload >> 16u; + let scene_strip_x = payload & 0xffffu; + let scene_strip_y = payload >> 16u; if paint_type == PAINT_TYPE_IMAGE { - let paint_tex_idx = instance.paint_and_rect_flag & PAINT_TEXTURE_INDEX_MASK; + let paint_tex_idx = paint_and_rect_flag & PAINT_TEXTURE_INDEX_MASK; let image_texel0 = load_encoded_paint_texel(paint_tex_idx, 0u); let image_texel1 = load_encoded_paint_texel(paint_tex_idx, 1u); let image_texel2 = load_encoded_paint_texel(paint_tex_idx, 2u); @@ -270,25 +276,25 @@ ); } } else if color_source == COLOR_SOURCE_LAYER { - let src_x = instance.payload & 0xffffu; - let src_y = instance.payload >> 16u; + let src_x = payload & 0xffffu; + let src_y = payload >> 16u; out.sample_xy = vec2<f32>( f32(src_x) + x * f32(width), f32(src_y) + y * f32(height), ); } - let col_offset = select(f32(instance.col_idx_or_rect_frac), 0.0, is_rect); + let col_offset = select(f32(col_idx_or_rect_frac), 0.0, is_rect); out.tex_coord = vec2<f32>(col_offset + x * f32(width), y * f32(height)); // Divide by a power of 2 to ensure exact f32 arithmetic (and divide by the expected depth // buffer precision of 24 bits). - let z = 1.0 - f32(instance.depth_index) / f32(1u << 24u); + let z = 1.0 - f32(depth_index) / f32(1u << 24u); // Flip it based on the flag. let final_ndc_y = select(ndc_y, -ndc_y, config.ndc_y_negate != 0u); out.position = vec4<f32>(ndc_x, final_ndc_y, z, 1.0); - out.payload = instance.payload; - out.paint_and_rect_flag = instance.paint_and_rect_flag; + out.payload = payload; + out.paint_and_rect_flag = paint_and_rect_flag; return out; }
diff --git a/sparse_strips/vello_sparse_shaders/src/lint/mod.rs b/sparse_strips/vello_sparse_shaders/src/lint/mod.rs index f76bdf8..c5830d0 100644 --- a/sparse_strips/vello_sparse_shaders/src/lint/mod.rs +++ b/sparse_strips/vello_sparse_shaders/src/lint/mod.rs
@@ -8,6 +8,7 @@ use naga::Module; +mod no_integer_vertex_inputs; mod no_structs_in_fragment; /// Diagnostic produced by a single lint pass when it finds violations. @@ -23,10 +24,13 @@ /// Runs every WGSL shader lint over `module` and panics with a single aggregated /// message (prefixed by `shader_name`) if any lint reports violations. pub(crate) fn lint(shader_name: &str, module: &Module) { - let reports: Vec<LintReport> = [no_structs_in_fragment::check(module)] - .into_iter() - .flatten() - .collect(); + let reports: Vec<LintReport> = [ + no_structs_in_fragment::check(module), + no_integer_vertex_inputs::check(module), + ] + .into_iter() + .flatten() + .collect(); if reports.is_empty() { return;
diff --git a/sparse_strips/vello_sparse_shaders/src/lint/no_integer_vertex_inputs.rs b/sparse_strips/vello_sparse_shaders/src/lint/no_integer_vertex_inputs.rs new file mode 100644 index 0000000..45875a6 --- /dev/null +++ b/sparse_strips/vello_sparse_shaders/src/lint/no_integer_vertex_inputs.rs
@@ -0,0 +1,241 @@ +// Copyright 2026 the Vello Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! Lint pass: vertex inputs at user-defined locations must not use signed or +//! unsigned integer types. +//! +//! Some Intel HD Graphics 4000 macOS/ANGLE combinations silently route +//! buffer-backed integer vertex attributes through a floating-point conversion. +//! Observed affected Chrome versions include 104+ on Catalina, 100+ on Mojave, +//! and 100–103 on El Capitan; this list is not exhaustive. +//! WebGL still reports the attribute as integer, but the shader receives the +//! bit pattern of the numerically converted float. Fetch packed values through +//! floating-point attributes instead and reconstruct them in the vertex shader. + +use naga::{Binding, Module, ScalarKind, ShaderStage, TypeInner}; + +use super::LintReport; + +/// Detects integer-typed, user-defined inputs to all vertex entry points. +/// +/// Built-ins such as `@builtin(vertex_index) vertex_index: u32` are generated by +/// the API rather than fetched from a vertex buffer, so they are deliberately +/// allowed. +pub(super) fn check(module: &Module) -> Option<LintReport> { + let mut violations = Vec::new(); + + for entry_point in module + .entry_points + .iter() + .filter(|entry_point| entry_point.stage == ShaderStage::Vertex) + { + for (argument_index, argument) in entry_point.function.arguments.iter().enumerate() { + let argument_name = argument.name.as_deref().unwrap_or("<unnamed>"); + match &argument.binding { + Some(binding) => check_input( + module, + &entry_point.name, + &format!("argument #{argument_index} (`{argument_name}`)"), + binding, + argument.ty, + &mut violations, + ), + None => { + if let TypeInner::Struct { members, .. } = &module.types[argument.ty].inner { + for (member_index, member) in members.iter().enumerate() { + let Some(binding) = &member.binding else { + continue; + }; + let member_name = member.name.as_deref().unwrap_or("<unnamed>"); + check_input( + module, + &entry_point.name, + &format!( + "argument #{argument_index} (`{argument_name}`), member \ + #{member_index} (`{member_name}`)" + ), + binding, + member.ty, + &mut violations, + ); + } + } + } + } + } + } + + if violations.is_empty() { + return None; + } + + Some(LintReport { + summary: "uses signed or unsigned integer vertex inputs at user-defined locations.", + explanation: "Buffer-backed integer vertex attributes are corrupted on some Intel HD \ + Graphics 4000 macOS/ANGLE combinations: the values pass through a numeric \ + floating-point conversion despite being configured as integer attributes. \ + Observed affected Chrome versions include 104+ on Catalina, 100+ on \ + Mojave, and 100-103 on El Capitan (non-exhaustive). \ + Declare each vertex input as `f32` (or a float vector), fetch it through the \ + floating-point vertex-attribute path, and recover packed bits with \ + `bitcast<u32>`. Integer built-ins and integer values produced inside the \ + vertex shader remain safe.", + violations, + }) +} + +fn check_input( + module: &Module, + entry_point_name: &str, + input_label: &str, + binding: &Binding, + ty: naga::Handle<naga::Type>, + violations: &mut Vec<String>, +) { + let Binding::Location { location, .. } = binding else { + return; + }; + + let kind = match module.types[ty].inner { + TypeInner::Scalar(scalar) | TypeInner::Vector { scalar, .. } => scalar.kind, + _ => return, + }; + if !matches!(kind, ScalarKind::Sint | ScalarKind::Uint) { + return; + } + + violations.push(format!( + "vertex entry point `{entry_point_name}`: {input_label} at location {location} has {} type `{}`", + match kind { + ScalarKind::Sint => "signed integer", + ScalarKind::Uint => "unsigned integer", + _ => unreachable!(), + }, + type_name(module, ty), + )); +} + +fn type_name(module: &Module, ty: naga::Handle<naga::Type>) -> String { + if let Some(name) = &module.types[ty].name { + return name.clone(); + } + + match module.types[ty].inner { + TypeInner::Scalar(scalar) => scalar_name(scalar.kind, scalar.width), + TypeInner::Vector { size, scalar } => format!( + "vec{}<{}>", + u8::from(size), + scalar_name(scalar.kind, scalar.width), + ), + ref inner => format!("{inner:?}"), + } +} + +fn scalar_name(kind: ScalarKind, width: naga::Bytes) -> String { + let prefix = match kind { + ScalarKind::Sint => "i", + ScalarKind::Uint => "u", + ScalarKind::Float => "f", + ScalarKind::Bool => return "bool".to_string(), + ScalarKind::AbstractInt => "abstract-int-", + ScalarKind::AbstractFloat => "abstract-float-", + }; + format!("{prefix}{}", width * 8) +} + +#[cfg(test)] +mod tests { + use naga::{Module, front::wgsl}; + + use super::super::lint; + + const FLOAT_VERTEX_INPUTS: &str = r#" +struct VertexInput { + @location(0) scalar: f32, + @location(1) vector: vec2<f32>, +} + +@vertex +fn vs_main( + @builtin(vertex_index) vertex_index: u32, + input: VertexInput, +) -> @builtin(position) vec4<f32> { + return vec4<f32>(input.scalar, input.vector, f32(vertex_index)); +} + +@fragment +fn fs_main() -> @location(0) vec4<f32> { + return vec4<f32>(0.0); +} +"#; + + const DIRECT_UNSIGNED_INPUT: &str = r#" +@vertex +fn vs_main(@location(3) packed: u32) -> @builtin(position) vec4<f32> { + return vec4<f32>(f32(packed)); +} + +@fragment +fn fs_main() -> @location(0) vec4<f32> { + return vec4<f32>(0.0); +} +"#; + + const STRUCT_SIGNED_INPUT: &str = r#" +struct VertexInput { + @location(0) position: vec2<f32>, + @location(4) packed: vec2<i32>, +} + +@vertex +fn vs_main(input: VertexInput) -> @builtin(position) vec4<f32> { + return vec4<f32>(input.position, vec2<f32>(input.packed)); +} + +@fragment +fn fs_main() -> @location(0) vec4<f32> { + return vec4<f32>(0.0); +} +"#; + + const ALIASED_UNSIGNED_INPUT: &str = r#" +alias Packed = u32; + +@vertex +fn vs_main(@location(2) packed: Packed) -> @builtin(position) vec4<f32> { + return vec4<f32>(f32(packed)); +} + +@fragment +fn fs_main() -> @location(0) vec4<f32> { + return vec4<f32>(0.0); +} +"#; + + fn parse(source: &str) -> Module { + wgsl::parse_str(source).expect("WGSL parses") + } + + #[test] + fn accepts_float_vertex_inputs_and_integer_builtins() { + lint("float_inputs", &parse(FLOAT_VERTEX_INPUTS)); + } + + #[test] + #[should_panic(expected = "location 3 has unsigned integer type")] + fn rejects_direct_unsigned_integer_vertex_input() { + lint("unsigned_input", &parse(DIRECT_UNSIGNED_INPUT)); + } + + #[test] + #[should_panic(expected = "location 4 has signed integer type")] + fn rejects_signed_integer_vertex_input_in_struct() { + lint("signed_input", &parse(STRUCT_SIGNED_INPUT)); + } + + #[test] + #[should_panic(expected = "location 2 has unsigned integer type")] + fn rejects_aliased_unsigned_integer_vertex_input() { + lint("aliased_unsigned_input", &parse(ALIASED_UNSIGNED_INPUT)); + } +}