use std::simd::{ Select, StdFloat, cmp::{SimdPartialEq, SimdPartialOrd}, num::SimdFloat, }; use crate::{ interpreters::{VALUE_0, VALUE_1, VALUE_05, VALUE_M1, VALUE_NAN, Value, glfract}, ssa::{SSAInput, SSAInstruction, SSAOpcode, SSATape}, }; #[derive(Clone, Debug)] pub struct PointInterpreter<'csg> { value_map: Vec, csg: &'csg SSATape, } impl PointInterpreter<'_> { fn load(&self, consider: SSAInput) -> Value { match consider { SSAInput::Register(r) => self.value_map[r as usize], SSAInput::Constant(c) => Value::splat(c), } } fn store(&mut self, location: u32, value: Value) { self.value_map[location as usize] = value; } } impl<'csg> PointInterpreter<'csg> { pub fn new(csg: &'csg SSATape) -> Self { PointInterpreter { value_map: vec![VALUE_0; csg.last_output as usize], csg, } } fn clear_stacks(&mut self) { self.value_map = vec![VALUE_0; self.csg.last_output as usize]; } fn param_one(&mut self, instruction: &SSAInstruction, func: impl Fn(Value) -> Value) { for i in 0..instruction.opcode.size as usize { let val_a = self.load(instruction.inputs[i]); self.store(instruction.outputs[i], func(val_a)); } } fn param_two(&mut self, instruction: &SSAInstruction, func: impl Fn(Value, Value) -> Value) { for i in 0..instruction.opcode.size as usize { let val_a = self.load(instruction.inputs[i]); let val_b = self.load(instruction.inputs[i + instruction.opcode.size as usize]); self.store(instruction.outputs[i], func(val_a, val_b)); } } fn param_three( &mut self, instruction: &SSAInstruction, func: impl Fn(Value, Value, Value) -> Value, ) { for i in 0..instruction.opcode.size as usize { let val_a = self.load(instruction.inputs[i]); let val_b = self.load(instruction.inputs[i + instruction.opcode.size as usize]); let val_c = self.load(instruction.inputs[i + (instruction.opcode.size * 2) as usize]); self.store(instruction.outputs[i], func(val_a, val_b, val_c)); } } // cargo asm "tape-drive::interpreter::PointInterpreter::scene" --no-color // --rust > scene.asm pub fn scene(&mut self, px: Value, py: Value, pz: Value, time: Value) -> Value { self.clear_stacks(); for instruction in &self.csg.tape { use SSAOpcode::*; match instruction.opcode.opcode { SSAReturn => { return self.load(instruction.inputs[0]); } SSAPosition => { self.store(instruction.outputs[0], px); self.store(instruction.outputs[1], py); self.store(instruction.outputs[2], pz); self.store(instruction.outputs[3], time); } SSAAdd => { self.param_two(instruction, |val_a, val_b| val_a + val_b); } SSASub => { self.param_two(instruction, |val_a, val_b| val_a - val_b); } SSAMul => { self.param_two(instruction, |val_a, val_b| val_a * val_b); } SSADiv => { self.param_two(instruction, |val_a, val_b| val_a / val_b); } SSAMod => { self.param_two(instruction, |val_a, val_b| val_a % val_b); } SSAAtan2 => self.param_two(instruction, |val_a, val_b| { let mut val_a = val_a.to_array(); let val_b = val_b.to_array(); for i in 0..Value::LEN { val_a[i] = val_a[i].atan2(val_b[i]); } Value::from_array(val_a) }), SSAMin => { self.param_two(instruction, |val_a, val_b| val_a.simd_min(val_b)); } SSAMinMaterial => todo!(), SSAMax => { self.param_two(instruction, |val_a, val_b| val_a.simd_max(val_b)); } SSAMaxMaterial => todo!(), SSADot => { let val_a = (0..instruction.opcode.size) .map(|i| self.load(instruction.inputs[i as usize])) .collect::>(); let val_b = (instruction.opcode.size..(instruction.opcode.size * 2)) .map(|i| self.load(instruction.inputs[i as usize])) .collect::>(); self.store( instruction.outputs[0], match instruction.opcode.size { 1 => val_a[0] * val_b[0], 2 => (val_a[0] * val_b[0]) + (val_a[1] * val_b[1]), 3 => { (val_a[0] * val_b[0]) + (val_a[1] * val_b[1]) + (val_a[2] * val_b[2]) } 4 => { (val_a[0] * val_b[0]) + (val_a[1] * val_b[1]) + (val_a[2] * val_b[2]) + (val_a[3] * val_b[3]) } _ => unreachable!(), }, ); } SSALength => { let val_a = (0..instruction.opcode.size) .map(|i| self.load(instruction.inputs[i as usize])) .collect::>(); self.store( instruction.outputs[0], match instruction.opcode.size { 1 => val_a[0], 2 => ((val_a[0] * val_a[0]) + (val_a[1] * val_a[1])).sqrt(), 3 => ((val_a[0] * val_a[0]) + (val_a[1] * val_a[1]) + (val_a[2] * val_a[2])) .sqrt(), 4 => ((val_a[0] * val_a[0]) + (val_a[1] * val_a[1]) + (val_a[2] * val_a[2]) + (val_a[3] * val_a[3])) .sqrt(), _ => unreachable!(), }, ); } SSADistance => { let val_a = (0..instruction.opcode.size) .map(|i| self.load(instruction.inputs[i as usize])) .collect::>(); let val_b = (instruction.opcode.size..(instruction.opcode.size * 2)) .map(|i| self.load(instruction.inputs[i as usize])) .collect::>(); self.store( instruction.outputs[0], match instruction.opcode.size { 1 => val_b[0] - val_a[0], 2 => (((val_a[0] - val_b[0]) * (val_a[0] - val_b[0])) + ((val_a[1] - val_b[1]) * (val_a[1] - val_b[1]))) .sqrt(), 3 => (((val_a[0] - val_b[0]) * (val_a[0] - val_b[0])) + ((val_a[1] - val_b[1]) * (val_a[1] - val_b[1])) + ((val_a[2] - val_b[2]) * (val_a[2] - val_b[2])) + ((val_a[3] - val_b[3]) * (val_a[3] - val_b[3]))) .sqrt(), 4 => (((val_a[0] - val_b[0]) * (val_a[0] - val_b[0])) + ((val_a[1] - val_b[1]) * (val_a[1] - val_b[1])) + ((val_a[2] - val_b[2]) * (val_a[2] - val_b[2])) + ((val_a[3] - val_b[3]) * (val_a[3] - val_b[3])) + ((val_a[4] - val_b[4]) * (val_a[4] - val_b[4]))) .sqrt(), _ => unreachable!(), }, ); } SSANegate => { self.param_one(instruction, |val_a| -val_a); } SSARound => { self.param_one(instruction, |val_a| val_a.round()); } SSAAbs => { self.param_one(instruction, |val_a| val_a.abs()); } SSAFloor => { self.param_one(instruction, |val_a| val_a.floor()); } SSACeil => { self.param_one(instruction, |val_a| val_a.ceil()); } SSAFract => { self.param_one(instruction, glfract); } SSASin => { self.param_one(instruction, |val_a| val_a.sin()); } SSACos => { self.param_one(instruction, |val_a| val_a.cos()); } SSATan => { self.param_one(instruction, |val_a| { Value::from_array(val_a.to_array().map(|f| f.tan())) }); } SSAAsin => { self.param_one(instruction, |val_a| { Value::from_array(val_a.to_array().map(|f| f.asin())) }); } SSAAcos => { self.param_one(instruction, |val_a| { Value::from_array(val_a.to_array().map(|f| f.acos())) }); } SSAAtan => { self.param_one(instruction, |val_a| { Value::from_array(val_a.to_array().map(|f| f.atan())) }); } SSAExp => { self.param_one(instruction, |val_a| val_a.exp()); } SSALog => { self.param_one(instruction, |val_a| val_a.ln()); } SSASqrt => { self.param_one(instruction, |val_a| val_a.sqrt()); } SSASquare => { self.param_one(instruction, |val_a| val_a * val_a); } SSACube => { self.param_one(instruction, |val_a| val_a * val_a * val_a); } SSASmoothMin => { self.param_three(instruction, |d1, d2, k| { let h = (VALUE_05 + (VALUE_05 * (d2 - d1) / k)).simd_clamp(VALUE_0, VALUE_1); ((d2 * (VALUE_1 - h)) + (d1 * h)) - k * h * (VALUE_1 - h) }); } SSASmoothMax => { self.param_three(instruction, |d1, d2, k| { let h = (VALUE_05 - (VALUE_05 * (d2 + d1) / k)).simd_clamp(VALUE_0, VALUE_1); ((d2 * (VALUE_1 - h)) + (-d1 * h)) + k * h * (VALUE_1 - h) }); } SSASmoothMinMaterial => todo!(), SSASmoothMaxMaterial => todo!(), SSAClamp => { self.param_three(instruction, |val_a, val_b, val_c| { val_a.simd_clamp(val_b, val_c) }); } SSAMix => { self.param_three(instruction, |val_a, val_b, val_c| { (val_a * (VALUE_1 - val_c)) + (val_b * val_c) }); } SSAFMA => { self.param_three(instruction, |val_a, val_b, val_c| { val_a.mul_add(val_b, val_c) }); } SSASDFSphere => { let pos_x = self.load(instruction.inputs[0]); let pos_y = self.load(instruction.inputs[1]); let pos_z = self.load(instruction.inputs[2]); let radius = self.load(instruction.inputs[3]); self.store( instruction.outputs[0], ((pos_x * pos_x) + (pos_y * pos_y) + (pos_z * pos_z)).sqrt() - radius, ); } SSASDFBox => { let pos_x = self.load(instruction.inputs[0]); let pos_y = self.load(instruction.inputs[1]); let pos_z = self.load(instruction.inputs[2]); let rad_x = self.load(instruction.inputs[3]); let rad_y = self.load(instruction.inputs[4]); let rad_z = self.load(instruction.inputs[5]); let qx = pos_x.abs() - rad_x; let qy = pos_y.abs() - rad_y; let qz = pos_z.abs() - rad_z; let qxmax = qx.simd_max(VALUE_0); let qymax = qy.simd_max(VALUE_0); let qzmax = qz.simd_max(VALUE_0); self.store( instruction.outputs[0], ((qxmax * qxmax) + (qymax * qymax) + (qzmax * qzmax)).sqrt() + qx.simd_max(qy.simd_max(qz)).simd_min(VALUE_0), ); } SSASDFTorus => { let pos_x = self.load(instruction.inputs[0]); let pos_y = self.load(instruction.inputs[1]); let pos_z = self.load(instruction.inputs[2]); let radius1 = self.load(instruction.inputs[3]); let radius2 = self.load(instruction.inputs[4]); let q = ((pos_x * pos_x) + (pos_z * pos_z)).sqrt() - radius1; self.store( instruction.outputs[0], ((q * q) + (pos_y * pos_y)).sqrt() - radius2, ); } SSACompare => { self.param_two(instruction, |val_a, val_b| { val_a .simd_gt(val_b) .select(VALUE_1, val_a.simd_lt(val_b).select(VALUE_M1, VALUE_0)) }); } SSAAnd => { self.param_two(instruction, |val_a, val_b| { val_a.simd_eq(VALUE_0).select(val_a, val_b) }); } SSAOr => { self.param_two(instruction, |val_a, val_b| { val_a.simd_eq(VALUE_0).select(val_b, val_a) }); } SSARecip => { self.param_one(instruction, |val_a| val_a.recip()); } SSANot => { self.param_one(instruction, |val_a| { val_a.simd_eq(VALUE_0).select(VALUE_1, VALUE_0) }); } SSAStop => return VALUE_0, } } VALUE_NAN } }