From ca88a2981c5299612381e275c9cfcecc3b895364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Mon, 24 Jul 2023 22:00:30 +0200 Subject: [PATCH] Uses element names instead of indices for the parameters of multivector constructors in rust. --- codegen/src/emit.rs | 18 +++++++++++++++++- codegen/src/rust.rs | 21 +++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/codegen/src/emit.rs b/codegen/src/emit.rs index e80f202..84c79cd 100644 --- a/codegen/src/emit.rs +++ b/codegen/src/emit.rs @@ -1,3 +1,5 @@ +use crate::{algebra::BasisElement, ast::AstNode, glsl, rust}; + pub fn camel_to_snake_case(collector: &mut W, name: &str) -> std::io::Result<()> { let mut underscores = name.chars().enumerate().filter(|(_i, c)| c.is_uppercase()).map(|(i, _c)| i).peekable(); for (i, c) in name.to_lowercase().bytes().enumerate() { @@ -21,7 +23,21 @@ pub fn emit_indentation(collector: &mut W, indentation: usize Ok(()) } -use crate::{ast::AstNode, glsl, rust}; +pub fn emit_element_name(collector: &mut W, element: &BasisElement) -> std::io::Result<()> { + debug_assert_ne!(element.scalar, 0); + if element.index == 0 { + collector.write_all(b"scalar") + } else { + collector.write_all(if element.scalar < 0 { b"_e" } else { b"e" })?; + collector.write_all( + element + .component_bits() + .map(|index| format!("{:X}", index)) + .collect::() + .as_bytes(), + ) + } +} pub struct Emitter { pub rust_collector: W, diff --git a/codegen/src/rust.rs b/codegen/src/rust.rs index 2e7c2ad..5dd19ef 100644 --- a/codegen/src/rust.rs +++ b/codegen/src/rust.rs @@ -1,6 +1,6 @@ use crate::{ ast::{AstNode, DataType, Expression, ExpressionContent, Parameter}, - emit::{camel_to_snake_case, emit_indentation}, + emit::{camel_to_snake_case, emit_element_name, emit_indentation}, }; fn emit_data_type(collector: &mut W, data_type: &DataType) -> std::io::Result<()> { @@ -245,22 +245,27 @@ pub fn emit_code(collector: &mut W, ast_node: &AstNode, inden collector.write_all(b"#[allow(clippy::too_many_arguments)]\n")?; emit_indentation(collector, indentation + 1)?; collector.write_all(b"pub const fn new(")?; - for i in 0..element_count { - if i > 0 { - collector.write_all(b", ")?; + let mut element_index = 0; + for group in class.grouped_basis.iter() { + for element in group.iter() { + if element_index > 0 { + collector.write_all(b", ")?; + } + emit_element_name(collector, element)?; + collector.write_all(b": f32")?; + element_index += 1; } - collector.write_fmt(format_args!("element{}: f32", i))?; } collector.write_all(b") -> Self {\n")?; emit_indentation(collector, indentation + 2)?; collector.write_all(b"Self { elements: [")?; - let mut element_index = 0; + element_index = 0; for (j, group) in class.grouped_basis.iter().enumerate() { - for _ in 0..group.len() { + for element in group.iter() { if element_index > 0 { collector.write_all(b", ")?; } - collector.write_fmt(format_args!("element{}", element_index))?; + emit_element_name(collector, element)?; element_index += 1; } for _ in group.len()..simd_widths[j] {