formatting: Using canonical formatting on whole project

This commit is contained in:
2026-11-22 17:16:32 +01:00
parent 2910314df3
commit dd0e102348
44 changed files with 1526 additions and 3376 deletions
+52 -104
View File
@@ -51,8 +51,7 @@ pub trait Attribute:
/// The layout of a structure is part of the cache contract: the order of the
/// attributes must match the order the shaders read, and every attribute size
/// must be a whole number of `u32` words.
pub trait Attributes: Attribute
{
pub trait Attributes: Attribute {
/// The attributes layout for this struct.
///
/// Built by `#[derive(Attributes)]`, but it can also be written by hand.
@@ -73,22 +72,19 @@ pub trait Attributes: Attribute
fn get_attr_mut(&mut self, index: usize) -> &mut [u8];
/// Gets the nth attribute's bytes, or [`None`] if the index is out of range.
fn try_get_attr(&self, index: usize) -> Option<&[u8]>
{
fn try_get_attr(&self, index: usize) -> Option<&[u8]> {
(index < Self::LAYOUT.len()).then(|| self.get_attr(index))
}
/// Gets the nth attribute's bytes mutably, or [`None`] if the index is out of range.
fn try_get_attr_mut(&mut self, index: usize) -> Option<&mut [u8]>
{
fn try_get_attr_mut(&mut self, index: usize) -> Option<&mut [u8]> {
(index < Self::LAYOUT.len()).then(|| self.get_attr_mut(index))
}
}
/// Describes one attribute of a voxel
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AttributeDescriptor
{
pub struct AttributeDescriptor {
/// A name for the attribute, used as the GPU buffer label
pub label: &'static str,
@@ -102,11 +98,9 @@ pub struct AttributeDescriptor
pub semantic: Option<&'static str>,
}
impl AttributeDescriptor
{
impl AttributeDescriptor {
/// Creates a descriptor without a semantic role
pub const fn new(label: &'static str, word_count: u32) -> Self
{
pub const fn new(label: &'static str, word_count: u32) -> Self {
Self {
label,
word_count,
@@ -115,15 +109,13 @@ impl AttributeDescriptor
}
/// Adds a semantic role to this descriptor
pub const fn with_semantic(mut self, semantic: &'static str) -> Self
{
pub const fn with_semantic(mut self, semantic: &'static str) -> Self {
self.semantic = Some(semantic);
self
}
/// The size of this attribute in bytes
pub const fn size_bytes(&self) -> u32
{
pub const fn size_bytes(&self) -> u32 {
self.word_count * size_of::<u32>() as u32
}
}
@@ -133,50 +125,42 @@ impl AttributeDescriptor
/// The layout is stored as a `'static` slice so it can live in an associated
/// constant. Runtime-built layouts can copy it into a `Vec`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AttributesLayout
{
pub struct AttributesLayout {
/// The attributes, in binding order
pub attributes: &'static [AttributeDescriptor],
}
impl AttributesLayout
{
impl AttributesLayout {
/// A layout with no attributes
pub const EMPTY: Self = Self { attributes: &[] };
/// Creates a layout from a static descriptor slice
pub const fn new(attributes: &'static [AttributeDescriptor]) -> Self
{
pub const fn new(attributes: &'static [AttributeDescriptor]) -> Self {
Self { attributes }
}
/// Number of attributes
pub const fn len(&self) -> usize
{
pub const fn len(&self) -> usize {
self.attributes.len()
}
/// Whether the layout has no attributes
pub const fn is_empty(&self) -> bool
{
pub const fn is_empty(&self) -> bool {
self.attributes.is_empty()
}
/// Gets the descriptor at `index`
pub fn get(&self, index: usize) -> Option<&AttributeDescriptor>
{
pub fn get(&self, index: usize) -> Option<&AttributeDescriptor> {
self.attributes.get(index)
}
/// Iterates over `(index, descriptor)` pairs
pub fn iter(&self) -> impl Iterator<Item = (usize, &AttributeDescriptor)> + '_
{
pub fn iter(&self) -> impl Iterator<Item = (usize, &AttributeDescriptor)> + '_ {
self.attributes.iter().enumerate()
}
/// Sum of every attribute's word count, i.e. the words used by one voxel
pub fn total_word_count(&self) -> u32
{
pub fn total_word_count(&self) -> u32 {
self.attributes
.iter()
.map(|attribute| attribute.word_count)
@@ -184,35 +168,28 @@ impl AttributesLayout
}
/// Finds an attribute by label
pub fn by_label(&self, label: &str) -> Option<(usize, &AttributeDescriptor)>
{
pub fn by_label(&self, label: &str) -> Option<(usize, &AttributeDescriptor)> {
self.iter().find(|(_, attribute)| attribute.label == label)
}
/// Finds an attribute by semantic role
pub fn by_semantic(&self, semantic: &str) -> Option<(usize, &AttributeDescriptor)>
{
pub fn by_semantic(&self, semantic: &str) -> Option<(usize, &AttributeDescriptor)> {
self.iter()
.find(|(_, attribute)| attribute.semantic == Some(semantic))
}
/// Checks that the layout can be used as a cache layout
pub fn validate(&self) -> Result<(), LayoutError>
{
if self.attributes.is_empty()
{
pub fn validate(&self) -> Result<(), LayoutError> {
if self.attributes.is_empty() {
return Err(LayoutError::Empty);
}
for (index, attribute) in self.attributes.iter().enumerate()
{
if attribute.label.is_empty()
{
for (index, attribute) in self.attributes.iter().enumerate() {
if attribute.label.is_empty() {
return Err(LayoutError::EmptyLabel { index });
}
if attribute.word_count == 0
{
if attribute.word_count == 0 {
return Err(LayoutError::ZeroWordCount { index });
}
@@ -249,19 +226,15 @@ impl AttributesLayout
/// A stable fingerprint of the layout, used to check that a producer, a
/// cache instance and a shader build agree on the attribute layout.
pub fn fingerprint(&self) -> u64
{
pub fn fingerprint(&self) -> u64 {
let mut hash = 0xcbf2_9ce4_8422_2325u64;
for attribute in self.attributes
{
for attribute in self.attributes {
fnv1a(&mut hash, attribute.label.as_bytes());
fnv1a(&mut hash, &attribute.word_count.to_le_bytes());
match attribute.semantic
{
Some(semantic) =>
{
match attribute.semantic {
Some(semantic) => {
fnv1a(&mut hash, &[1]);
fnv1a(&mut hash, semantic.as_bytes());
}
@@ -286,8 +259,7 @@ impl AttributesLayout
///
///
/// ```
pub fn wgsl_layout_decl(&self) -> String
{
pub fn wgsl_layout_decl(&self) -> String {
let mut decl = String::new();
decl += &format!("const ATTRIBUTE_COUNT: u32 = {};", self.attributes.len());
@@ -299,8 +271,7 @@ impl AttributesLayout
self.attributes.len()
);
for attribute in self.attributes.iter()
{
for attribute in self.attributes.iter() {
decl += &format!(" {},", attribute.word_count);
}
@@ -310,10 +281,8 @@ impl AttributesLayout
}
}
fn fnv1a(hash: &mut u64, bytes: &[u8])
{
for byte in bytes
{
fn fnv1a(hash: &mut u64, bytes: &[u8]) {
for byte in bytes {
*hash ^= *byte as u64;
*hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
@@ -321,33 +290,28 @@ fn fnv1a(hash: &mut u64, bytes: &[u8])
/// Errors reported by [`AttributesLayout::validate`]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayoutError
{
pub enum LayoutError {
/// The layout has no attributes, which a cache cannot represent
Empty,
/// An attribute has an empty label
EmptyLabel
{
EmptyLabel {
/// Index of the offending attribute
index: usize,
},
/// An attribute has a zero word count
ZeroWordCount
{
ZeroWordCount {
/// Index of the offending attribute
index: usize,
},
/// Two attributes share a label
DuplicateLabel
{
DuplicateLabel {
/// Index of the first attribute
first: usize,
/// Index of the duplicate attribute
second: usize,
},
/// Two attributes share a semantic role
DuplicateSemantic
{
DuplicateSemantic {
/// Index of the first attribute
first: usize,
/// Index of the duplicate attribute
@@ -355,30 +319,23 @@ pub enum LayoutError
},
}
impl std::fmt::Display for LayoutError
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
match self
{
impl std::fmt::Display for LayoutError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => write!(formatter, "attribute layout has no attributes"),
Self::EmptyLabel { index } =>
{
Self::EmptyLabel { index } => {
write!(formatter, "attribute {index} has an empty label")
}
Self::ZeroWordCount { index } =>
{
Self::ZeroWordCount { index } => {
write!(formatter, "attribute {index} has a zero word count")
}
Self::DuplicateLabel { first, second } =>
{
Self::DuplicateLabel { first, second } => {
write!(
formatter,
"attributes {first} and {second} share the same label"
)
}
Self::DuplicateSemantic { first, second } =>
{
Self::DuplicateSemantic { first, second } => {
write!(
formatter,
"attributes {first} and {second} share the same semantic role"
@@ -392,12 +349,9 @@ impl std::error::Error for LayoutError {}
// Primitive attributes
impl Attribute for u32
{
fn average(attributes: &[Self]) -> Self
{
if attributes.is_empty()
{
impl Attribute for u32 {
fn average(attributes: &[Self]) -> Self {
if attributes.is_empty() {
return Self::default();
}
@@ -405,12 +359,9 @@ impl Attribute for u32
}
}
impl Attribute for i32
{
fn average(attributes: &[Self]) -> Self
{
if attributes.is_empty()
{
impl Attribute for i32 {
fn average(attributes: &[Self]) -> Self {
if attributes.is_empty() {
return Self::default();
}
@@ -418,12 +369,9 @@ impl Attribute for i32
}
}
impl Attribute for f32
{
fn average(attributes: &[Self]) -> Self
{
if attributes.is_empty()
{
impl Attribute for f32 {
fn average(attributes: &[Self]) -> Self {
if attributes.is_empty() {
return Self::default();
}
+9 -13
View File
@@ -8,8 +8,7 @@ use vxls_attributes::LayoutError;
#[derive(Clone, Copy, PartialEq, Default, Pod, Zeroable, Attributes, Debug)]
#[repr(C)]
struct TestAttributes
{
struct TestAttributes {
#[attribute(label = "color", semantic = "color")]
color: u32,
@@ -20,8 +19,7 @@ struct TestAttributes
reserved: u32,
}
fn sample(color: u32, normal: [f32; 3]) -> TestAttributes
{
fn sample(color: u32, normal: [f32; 3]) -> TestAttributes {
TestAttributes {
color,
normal,
@@ -30,8 +28,7 @@ fn sample(color: u32, normal: [f32; 3]) -> TestAttributes
}
#[test]
fn layout_matches_the_declared_fields()
{
fn layout_matches_the_declared_fields() {
let layout = TestAttributes::LAYOUT;
assert_eq!(layout.len(), 2);
@@ -54,8 +51,7 @@ fn layout_matches_the_declared_fields()
}
#[test]
fn get_attr_returns_the_field_bytes()
{
fn get_attr_returns_the_field_bytes() {
let value = sample(0x1122_3344, [1.0, 2.0, 3.0]);
assert_eq!(value.get_attr(0), bytemuck::bytes_of(&value.color));
@@ -63,14 +59,15 @@ fn get_attr_returns_the_field_bytes()
assert!(value.try_get_attr(2).is_none());
let mut value = value;
value.get_attr_mut(0).copy_from_slice(&0xAABB_CCDDu32.to_ne_bytes());
value
.get_attr_mut(0)
.copy_from_slice(&0xAABB_CCDDu32.to_ne_bytes());
assert_eq!(value.color, 0xAABB_CCDD);
assert!(value.try_get_attr_mut(2).is_none());
}
#[test]
fn average_is_component_wise_and_skips_the_reserved_field()
{
fn average_is_component_wise_and_skips_the_reserved_field() {
let first = TestAttributes {
color: 2,
normal: [1.0, 2.0, 3.0],
@@ -92,8 +89,7 @@ fn average_is_component_wise_and_skips_the_reserved_field()
}
#[test]
fn validate_rejects_duplicate_labels_and_semantics()
{
fn validate_rejects_duplicate_labels_and_semantics() {
const DUPLICATE_LABELS: AttributesLayout = AttributesLayout {
attributes: &[
AttributeDescriptor {