1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
use std::{
collections::hash_map::DefaultHasher,
convert::TryFrom,
hash::{Hash, Hasher},
};
type UnknownOpcodeError = TryFromPrimitiveError<Opcode>;
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u32)]
pub enum Opcode {
Plane = 0,
Sphere = 1,
Capsule = 2,
TaperedCapsule = 3,
Material = 4,
Union = 5,
UnionSmooth = 6,
Subtract = 7,
SubtractSmooth = 8,
Intersect = 9,
IntersectSmooth = 10,
PushTranslation = 11,
PushRotation = 12,
PopTransform = 13,
PushScale = 14,
PopScale = 15,
End = 16,
RoundedBox = 17,
BiconvexLens = 18,
RoundedCylinder = 19,
Torus = 20,
TorusSector = 21,
Cone = 22,
}
pub fn constants_hash(constants: &[f32]) -> u64 {
let mut s = DefaultHasher::new();
for &c in constants {
let v: u32 = c.to_bits();
v.hash(&mut s);
}
s.finish()
}
pub fn opcodes_hash(opcodes: &[Opcode]) -> u64 {
let mut s = DefaultHasher::new();
opcodes.hash(&mut s);
s.finish()
}
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Program {
pub constants: Vec<f32>,
pub opcodes: Vec<Opcode>,
}
impl Program {
pub fn with_constants(&self, constants: Vec<f32>) -> Self {
Self {
constants,
opcodes: self.opcodes.clone(),
}
}
pub fn constant_hash(&self) -> u64 {
constants_hash(&self.constants)
}
pub fn program_hash(&self) -> u64 {
opcodes_hash(&self.opcodes)
}
pub fn full_hash(&self) -> u64 {
self.program_hash() ^ self.constant_hash()
}
#[cfg(feature = "with_bincode")]
pub fn as_bytes(&self) -> Result<Vec<u8>, std::boxed::Box<bincode::ErrorKind>> {
bincode::serialize(self)
}
#[cfg(feature = "with_bincode")]
pub fn from_bytes(bytes: &[u8]) -> Result<Self, std::boxed::Box<bincode::ErrorKind>> {
bincode::deserialize(bytes)
}
pub fn from_raw(opcodes: &[u32], constants: &[f32]) -> Result<Self, UnknownOpcodeError> {
let opcodes = opcodes
.iter()
.map(|opcode| Opcode::try_from(*opcode))
.collect::<Result<Vec<Opcode>, _>>()?;
Ok(Self {
opcodes,
constants: constants.to_vec(),
})
}
pub fn as_raw(&self) -> (Vec<u32>, Vec<f32>) {
let opcodes = self
.opcodes
.iter()
.map(|&opcode| opcode.into())
.collect::<Vec<u32>>();
(opcodes, self.constants.clone())
}
pub(crate) fn constant_push_vec2(&mut self, v: impl Into<[f32; 2]>) {
self.constants.extend(&v.into());
}
pub(crate) fn constant_push_vec3(&mut self, v: impl Into<[f32; 3]>) {
self.constants.extend(&v.into());
}
pub(crate) fn constant_push_vec4(&mut self, v: impl Into<[f32; 4]>) {
self.constants.extend(&v.into());
}
pub fn disassemble(&self) -> String {
crate::compiler::disassemble(&self.opcodes, &self.constants)
.unwrap_or_else(|e| format!("(failed to disassemble: {:?}", e))
}
}