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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Compiled SDF: Cache-efficient linearized evaluation
//!
//! This module provides a "compiled" representation of SDF trees that
//! trades flexibility for performance. The recursive Arc-based tree
//! is converted to a flat instruction array, eliminating:
//!
//! - Pointer chasing (Arc indirection → cache misses)
//! - Branch misprediction (deep recursion → hard to predict)
//! - Function call overhead (recursive eval → stack manipulation)
//!
//! # Performance Characteristics
//!
//! | Aspect | Interpreted | Compiled | Compiled+SIMD |
//! |--------|-------------|----------|---------------|
//! | Memory layout | Scattered (heap) | Contiguous (Vec) | Contiguous |
//! | Cache efficiency | Poor | Excellent | Excellent |
//! | Parallelism | 1 point | 1 point | 8 points |
//! | Best for | Editing | Single eval | Batch eval |
//!
//! # Usage
//!
//! ```rust
//! use alice_sdf::prelude::*;
//! use alice_sdf::compiled::{CompiledSdf, eval_compiled};
//!
//! // Create an SDF tree
//! let node = SdfNode::sphere(1.0)
//! .smooth_union(SdfNode::box3d(0.5, 0.5, 0.5), 0.2);
//!
//! // Compile it once
//! let compiled = CompiledSdf::compile(&node);
//!
//! // Evaluate many times (fast!)
//! for _ in 0..1000000 {
//! let d = eval_compiled(&compiled, glam::Vec3::new(0.5, 0.5, 0.5));
//! }
//! ```
//!
//! # SIMD Batch Evaluation (Phase 2)
//!
//! For batch evaluation, use the SIMD-accelerated functions:
//!
//! ```rust
//! use alice_sdf::prelude::*;
//! use alice_sdf::compiled::{CompiledSdf, eval_compiled_batch_simd_parallel};
//!
//! let node = SdfNode::sphere(1.0);
//! let compiled = CompiledSdf::compile(&node);
//!
//! // Generate many points
//! let points: Vec<Vec3> = (0..100000)
//! .map(|i| Vec3::new(i as f32 * 0.01, 0.0, 0.0))
//! .collect();
//!
//! // Evaluate all points using SIMD + multi-threading
//! let distances = eval_compiled_batch_simd_parallel(&compiled, &points);
//! ```
//!
//! # Architecture
//!
//! The compiled representation uses a stack-based virtual machine:
//!
//! - **Value Stack**: Holds intermediate SDF distance values
//! - **Coordinate Stack**: Holds saved coordinate frames for transforms
//! - **Instruction Array**: Sequential opcodes with inline parameters
//!
//! ## Instruction Format
//!
//! Each instruction is 64 bytes (one full cache line):
//!
//! ```text
//! ┌─────────────────────────────────────────────────┐
//! │ params[7]: f32[7] (28 bytes) │
//! │ opcode: u8 (1 byte) │
//! │ flags: u8 (1 byte) │
//! │ child_count: u16 (2 bytes) │
//! │ skip_offset: u32 (4 bytes) │
//! │ padding (28 bytes) │
//! └─────────────────────────────────────────────────┘
//! ```
//!
//! ## Evaluation Order
//!
//! - **Primitives**: Push distance to value stack
//! - **Binary ops**: Pop 2 values, push result
//! - **Transforms**: Push coord frame, modify point
//! - **PopTransform**: Restore coord frame, apply post-processing
//!
//! ## SIMD Evaluation (8-wide)
//!
//! The SIMD evaluator processes 8 points simultaneously using AVX2/NEON:
//!
//! - `Vec3x8`: 8 3D vectors in SoA layout
//! - `f32x8`: 8 scalar values for distances
//! - `Quatx8`: 8 quaternions for rotations
//!
//! ## BVH Acceleration (Phase 3)
//!
//! The BVH evaluator attaches AABBs to each instruction for spatial pruning:
//!
//! - `AabbPacked`: 32-byte aligned AABB for cache efficiency
//! - `CompiledSdfBvh`: Compiled SDF with BVH data
//! - `get_scene_aabb()`: Get the bounding box of the entire scene
//!
//! BVH is most effective for:
//! - Sparse scenes with distant objects
//! - Raymarching (each step can skip distant geometry)
//! - Broad-phase collision detection
//!
//! ## JIT Compilation (Phase 4) - Feature: `jit`
//!
//! The JIT compiler converts SDF trees directly to native machine code:
//!
//! ```rust,ignore
//! use alice_sdf::prelude::*;
//! use alice_sdf::compiled::jit::JitCompiledSdf;
//!
//! let shape = SdfNode::sphere(1.0)
//! .smooth_union(SdfNode::box3d(0.5, 0.5, 0.5), 0.2);
//!
//! // JIT compile once
//! let jit = JitCompiledSdf::compile(&shape).unwrap();
//!
//! // Evaluate with zero interpreter overhead
//! let d = jit.eval(Vec3::new(0.5, 0.0, 0.0));
//! ```
//!
//! Enable with: `cargo build --features jit`
//!
//! ## SoA Memory Layout (Phase 5)
//!
//! For maximum SIMD throughput, use Structure of Arrays layout:
//!
//! ```
//! use alice_sdf::prelude::*;
//! use alice_sdf::soa::SoAPoints;
//!
//! let shape = SdfNode::sphere(1.0);
//! let compiled = CompiledSdf::compile(&shape);
//!
//! // Convert to SoA layout (AoS → SoA)
//! let points: SoAPoints = (0..64)
//! .map(|i| Vec3::new(i as f32 * 0.1, 0.0, 0.0))
//! .collect();
//!
//! // Direct SIMD loads without shuffle
//! let distances = eval_compiled_batch_soa_parallel(&compiled, &points);
//! assert_eq!(distances.len(), 64);
//! ```
//!
//! SoA benefits:
//! - No gather/shuffle overhead for SIMD loads
//! - Better cache line utilization
//! - CPU prefetch-friendly sequential access
//!
//! Author: Moroya Sakamoto
/// Instanced SDF rendering (1 shape × N transforms)
/// Bytecode-driven BVH AABB refit (case B P1 / P2).
/// Shared shader transpiler framework (ShaderLang trait + GenericTranspiler)
pub use ;
pub use ;
pub use ;
pub use ;
pub use AabbPacked;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use InstancedSdf;
pub use Instruction;
pub use ;
pub use RefitError;
pub use ;