Uses element names instead of indices for the parameters of multivector constructors in rust.
This commit is contained in:
parent
8a05c34009
commit
ca88a2981c
2 changed files with 30 additions and 9 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
use crate::{algebra::BasisElement, ast::AstNode, glsl, rust};
|
||||||
|
|
||||||
pub fn camel_to_snake_case<W: std::io::Write>(collector: &mut W, name: &str) -> std::io::Result<()> {
|
pub fn camel_to_snake_case<W: std::io::Write>(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();
|
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() {
|
for (i, c) in name.to_lowercase().bytes().enumerate() {
|
||||||
|
|
@ -21,7 +23,21 @@ pub fn emit_indentation<W: std::io::Write>(collector: &mut W, indentation: usize
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::{ast::AstNode, glsl, rust};
|
pub fn emit_element_name<W: std::io::Write>(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::<String>()
|
||||||
|
.as_bytes(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Emitter<W: std::io::Write> {
|
pub struct Emitter<W: std::io::Write> {
|
||||||
pub rust_collector: W,
|
pub rust_collector: W,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{AstNode, DataType, Expression, ExpressionContent, Parameter},
|
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<W: std::io::Write>(collector: &mut W, data_type: &DataType) -> std::io::Result<()> {
|
fn emit_data_type<W: std::io::Write>(collector: &mut W, data_type: &DataType) -> std::io::Result<()> {
|
||||||
|
|
@ -245,22 +245,27 @@ pub fn emit_code<W: std::io::Write>(collector: &mut W, ast_node: &AstNode, inden
|
||||||
collector.write_all(b"#[allow(clippy::too_many_arguments)]\n")?;
|
collector.write_all(b"#[allow(clippy::too_many_arguments)]\n")?;
|
||||||
emit_indentation(collector, indentation + 1)?;
|
emit_indentation(collector, indentation + 1)?;
|
||||||
collector.write_all(b"pub const fn new(")?;
|
collector.write_all(b"pub const fn new(")?;
|
||||||
for i in 0..element_count {
|
let mut element_index = 0;
|
||||||
if i > 0 {
|
for group in class.grouped_basis.iter() {
|
||||||
collector.write_all(b", ")?;
|
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")?;
|
collector.write_all(b") -> Self {\n")?;
|
||||||
emit_indentation(collector, indentation + 2)?;
|
emit_indentation(collector, indentation + 2)?;
|
||||||
collector.write_all(b"Self { elements: [")?;
|
collector.write_all(b"Self { elements: [")?;
|
||||||
let mut element_index = 0;
|
element_index = 0;
|
||||||
for (j, group) in class.grouped_basis.iter().enumerate() {
|
for (j, group) in class.grouped_basis.iter().enumerate() {
|
||||||
for _ in 0..group.len() {
|
for element in group.iter() {
|
||||||
if element_index > 0 {
|
if element_index > 0 {
|
||||||
collector.write_all(b", ")?;
|
collector.write_all(b", ")?;
|
||||||
}
|
}
|
||||||
collector.write_fmt(format_args!("element{}", element_index))?;
|
emit_element_name(collector, element)?;
|
||||||
element_index += 1;
|
element_index += 1;
|
||||||
}
|
}
|
||||||
for _ in group.len()..simd_widths[j] {
|
for _ in group.len()..simd_widths[j] {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue