rpu 0.3.0

RPU is a GLSL-compatible language for rendering procedural graphics on the CPU.
Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use crate::prelude::*;
use rand::prelude::*;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;
use wasmer::{imports, Function, Instance, Module, Store, Value};

#[derive(Debug, Clone, Copy)]
struct Tile {
    x: usize,
    y: usize,
    width: usize,
    height: usize,
}

pub struct RPU {}

impl Default for RPU {
    fn default() -> Self {
        Self::new()
    }
}

impl RPU {
    pub fn new() -> Self {
        Self {}
    }

    /// Compile the RPU source code (given by its path) to WAT source code.
    pub fn compile_to_wat_from_path(
        &self,
        path: PathBuf,
        high_precision: bool,
    ) -> Result<String, String> {
        if let Ok(main) = std::fs::read_to_string(path.clone()) {
            let mut pre = Preprocessor::default();
            let main = pre.process_module(&main);
            let scanner = Scanner::new(main);
            let mut parser = Parser::new();
            parser.set_high_precision(high_precision);

            parser.parse(scanner)
        } else {
            Err("Could not read file.".to_string())
        }
    }

    /// Compile the RPU source code to WAT source code.
    pub fn compile_to_wat(&self, rpu_source: String) -> Result<String, String> {
        let mut pre = Preprocessor::default();
        let rpu_source = pre.process_module(&rpu_source);
        let scanner = Scanner::new(rpu_source);
        let mut parser = Parser::new();

        parser.parse(scanner)
    }

    /// Compile the RPU source code and run the function with the given arguments.
    pub fn compile_and_run(
        &self,
        source: &str,
        func_name: &str,
        args: Vec<Value>,
        high_precision: bool,
    ) -> Result<Vec<Value>, String> {
        let rc = self.compile_to_wat(source.to_string());
        match rc {
            Ok(wat) => self.compile_wat_and_run(&wat, func_name, args, high_precision),
            Err(err) => Err(err.to_string()),
        }
    }

    /// Compile the WAT source code and run the function with the given arguments.
    pub fn compile_wat_and_run(
        &self,
        wat: &str,
        func_name: &str,
        args: Vec<Value>,
        high_precision: bool,
    ) -> Result<Vec<Value>, String> {
        let mut store = Store::default();
        let module_rc = Module::new(&store, wat);
        match module_rc {
            Ok(module) => {
                let import_object = RPU::create_imports(&mut store, high_precision);
                if let Ok(instance) = Instance::new(&mut store, &module, &import_object) {
                    if let Ok(func) = instance.exports.get_function(func_name) {
                        let _start = self.get_time();
                        match func.call(&mut store, &args) {
                            Ok(values) => {
                                let _stop = self.get_time();
                                println!("Execution time: {:?} ms.", _stop - _start);
                                return Ok(values.to_vec());
                            }
                            Err(err) => return Err(err.to_string()),
                        }
                    }
                }
            }
            Err(err) => return Err(err.to_string()),
        }

        Err("Unknown error".to_string())
    }

    /// Compile the WAT source code and run the shader with the given arguments. The shader will be executed on the given buffer.
    pub fn compile_wat_and_run_as_shader(
        &self,
        wat: &str,
        func_name: &str,
        buffer: &mut ColorBuffer,
        high_precision: bool,
    ) -> Result<Vec<Value>, String> {
        let mut store = Store::default();
        let module_rc = Module::new(&store, wat);
        match module_rc {
            Ok(module) => {
                let import_object = RPU::create_imports(&mut store, high_precision);
                if let Ok(instance) = Instance::new(&mut store, &module, &import_object) {
                    if let Ok(func) = instance.exports.get_function(func_name) {
                        let _start = self.get_time();
                        for y in 0..buffer.height {
                            for x in 0..buffer.width {
                                let args = if high_precision {
                                    vec![
                                        Value::F64(x as f64),
                                        Value::F64(buffer.height as f64 - y as f64),
                                        Value::F64(buffer.width as f64),
                                        Value::F64(buffer.height as f64),
                                    ]
                                } else {
                                    vec![
                                        Value::F32(x as f32),
                                        Value::F32(buffer.height as f32 - y as f32),
                                        Value::F32(buffer.width as f32),
                                        Value::F32(buffer.height as f32),
                                    ]
                                };

                                match func.call(&mut store, &args) {
                                    Ok(values) => {
                                        if high_precision {
                                            let r = values[0].f64().unwrap();
                                            let g = values[1].f64().unwrap();
                                            let b = values[2].f64().unwrap();
                                            let a = values[3].f64().unwrap();
                                            buffer.set(x, y, [r, g, b, a]);
                                        } else {
                                            let r = values[0].f32().unwrap();
                                            let g = values[1].f32().unwrap();
                                            let b = values[2].f32().unwrap();
                                            let a = values[3].f32().unwrap();
                                            buffer.set(
                                                x,
                                                y,
                                                [r as f64, g as f64, b as f64, a as f64],
                                            );
                                        }
                                    }
                                    Err(err) => return Err(err.to_string()),
                                }
                            }
                        }
                        let _stop = self.get_time();
                        println!("Shader execution time: {:?} ms.", _stop - _start);
                    }
                }
            }
            Err(err) => return Err(err.to_string()),
        }

        Ok(vec![])
    }

    /// Compile the WAT source code and run the shader with the given arguments. The shader will be executed on the given buffer.
    pub fn compile_wat_and_run_as_tiled_shader(
        &self,
        wat: &str,
        func_name: &str,
        buffer: &mut Arc<Mutex<ColorBuffer>>,
        tile_size: (usize, usize),
        iterations: usize,
        high_precision: bool,
    ) -> Result<Vec<Value>, String> {
        let width = buffer.lock().unwrap().width;
        let height = buffer.lock().unwrap().height;

        let tiles = self.create_tiles(width, height, tile_size.0, tile_size.1);

        let tiles_mutex = Arc::new(Mutex::new(tiles));

        let num_cpus = num_cpus::get();
        let _start = self.get_time();

        // Create threads
        let mut handles = vec![];
        for _ in 0..num_cpus {
            let tiles_mutex = Arc::clone(&tiles_mutex);
            let buffer_mutex = Arc::clone(buffer);
            let fname = func_name.to_string().clone();
            let wat = wat.to_string().clone();

            let handle = thread::spawn(move || {
                let mut store = Store::default();
                let module_rc = Module::new(&store, wat);
                match module_rc {
                    Ok(module) => {
                        let import_object = RPU::create_imports(&mut store, high_precision);
                        if let Ok(instance) = Instance::new(&mut store, &module, &import_object) {
                            if let Ok(func) = instance.exports.get_function(&fname) {
                                let mut tile_buffer = ColorBuffer::new(tile_size.0, tile_size.1);
                                loop {
                                    // Lock mutex to access tiles
                                    let mut tiles = tiles_mutex.lock().unwrap();

                                    // Check if there are remaining tiles
                                    if let Some(tile) = tiles.pop() {
                                        // Release mutex before processing tile
                                        drop(tiles);
                                        // Process tile
                                        for h in 0..tile.height {
                                            for w in 0..tile.width {
                                                let x = tile.x + w;
                                                let y = tile.y + h;

                                                if x >= width || y >= height {
                                                    continue;
                                                }

                                                let args = if high_precision {
                                                    vec![
                                                        Value::F64(x as f64),
                                                        Value::F64(height as f64 - y as f64),
                                                        Value::F64(width as f64),
                                                        Value::F64(height as f64),
                                                    ]
                                                } else {
                                                    vec![
                                                        Value::F32(x as f32),
                                                        Value::F32(height as f32 - y as f32),
                                                        Value::F32(width as f32),
                                                        Value::F32(height as f32),
                                                    ]
                                                };

                                                let mut fc = [0.0, 0.0, 0.0, 0.0];
                                                for i in 0..iterations {
                                                    if let Ok(gl) =
                                                        instance.exports.get_global("mem_ptr")
                                                    {
                                                        _ = gl.set(&mut store, Value::I32(32));
                                                    }
                                                    match func.call(&mut store, &args) {
                                                        Ok(values) => {
                                                            let rgba = if high_precision {
                                                                [
                                                                    values[0].f64().unwrap(),
                                                                    values[1].f64().unwrap(),
                                                                    values[2].f64().unwrap(),
                                                                    values[3].f64().unwrap(),
                                                                ]
                                                            } else {
                                                                [
                                                                    values[0].f32().unwrap() as f64,
                                                                    values[1].f32().unwrap() as f64,
                                                                    values[2].f32().unwrap() as f64,
                                                                    values[3].f32().unwrap() as f64,
                                                                ]
                                                            };
                                                            let f = 1.0 / (i as f64 + 1.0);
                                                            fc[0] = fc[0] * (1.0 - f) + rgba[0] * f;
                                                            fc[1] = fc[1] * (1.0 - f) + rgba[1] * f;
                                                            fc[2] = fc[2] * (1.0 - f) + rgba[2] * f;
                                                            fc[3] = fc[3] * (1.0 - f) + rgba[3] * f;
                                                        }
                                                        Err(err) => println!("{}", err),
                                                    }

                                                    // Set the final color into the local buffer
                                                    tile_buffer.set(w, h, fc);
                                                }
                                            }
                                        }
                                        // Save the tile buffer to the main buffer
                                        buffer_mutex.lock().unwrap().copy_from(
                                            tile.x,
                                            tile.y,
                                            &tile_buffer,
                                        );

                                        // Save thebuffer optionally to disk after each completed block.
                                        if let Ok(buffer) = buffer_mutex.lock() {
                                            if let Some(path) = &buffer.file_path {
                                                buffer.save(path.clone());
                                            }
                                        }
                                    } else {
                                        // No remaining tiles, exit loop
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Err(err) => println!("{}", err),
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to finish
        for handle in handles {
            handle.join().unwrap();
        }

        let _stop = self.get_time();
        println!("Shader execution time: {:?} ms.", _stop - _start);

        Ok(vec![])
    }

    /// Create the tiles as a spiral pattern starting from the center.
    fn create_tiles(
        &self,
        image_width: usize,
        image_height: usize,
        tile_width: usize,
        tile_height: usize,
    ) -> Vec<Tile> {
        // TODO: Generate the tiles in a nice spiral pattern

        let mut tiles = Vec::new();
        let mut x = 0;
        let mut y = 0;
        while x < image_width && y < image_height {
            let tile = Tile {
                x,
                y,
                width: tile_width,
                height: tile_height,
            };
            tiles.push(tile);
            x += tile_width;
            if x >= image_width {
                x = 0;
                y += tile_height;
            }
        }

        tiles
    }

    fn create_imports(store: &mut Store, high_precision: bool) -> wasmer::Imports {
        if high_precision {
            imports! {
                "env" => {
                    "_rpu_sin" => Function::new_typed(store, |x: f64| -> f64 { x.sin() }),
                    "_rpu_cos" => Function::new_typed(store, |x: f64| -> f64 { x.cos() }),
                    "_rpu_tan" => Function::new_typed(store, |x: f64| -> f64 { x.tan() }),
                    "_rpu_atan" => Function::new_typed(store, |x: f64| -> f64 { x.atan() }),
                    "_rpu_exp" => Function::new_typed(store, |x: f64| -> f64 { x.exp() }),
                    "_rpu_degrees" => Function::new_typed(store, |x: f64| -> f64 { x.to_degrees() }),
                    "_rpu_radians" => Function::new_typed(store, |x: f64| -> f64 { x.to_radians() }),
                    "_rpu_min" => Function::new_typed(store, |x: f64, y: f64| -> f64 { x.min(y) }),
                    "_rpu_max" => Function::new_typed(store, |x: f64, y: f64| -> f64 { x.max(y) }),
                    "_rpu_pow" => Function::new_typed(store, |x: f64, y: f64| -> f64 { x.powf(y) }),
                    "_rpu_mod" => Function::new_typed(store, |x: f64, y: f64| -> f64 { x - y * (x / y).floor() }),
                    "_rpu_step" => Function::new_typed(store, |edge: f64, x: f64| -> f64 { if x < edge {
                        0.0
                    } else {
                        1.0
                    }}),
                    "_rpu_rand" => Function::new_typed(store, || -> f64 {
                        let mut rng = rand::thread_rng();
                        rng.gen()
                    }),
                    "_rpu_sign" => Function::new_typed(store, |x: f64| -> f64 { x.signum() }),
                    "_rpu_clamp" => Function::new_typed(store, |x: f64, y: f64, z: f64| -> f64 { x.clamp(y, z) }),
                },
            }
        } else {
            imports! {
                "env" => {
                    "_rpu_sin" => Function::new_typed(store, |x: f32| -> f32 { x.sin() }),
                    "_rpu_cos" => Function::new_typed(store, |x: f32| -> f32 { x.cos() }),
                    "_rpu_tan" => Function::new_typed(store, |x: f32| -> f32 { x.tan() }),
                    "_rpu_atan" => Function::new_typed(store, |x: f32| -> f32 { x.atan() }),
                    "_rpu_exp" => Function::new_typed(store, |x: f32| -> f32 { x.exp() }),
                    "_rpu_degrees" => Function::new_typed(store, |x: f32| -> f32 { x.to_degrees() }),
                    "_rpu_radians" => Function::new_typed(store, |x: f32| -> f32 { x.to_radians() }),
                    "_rpu_min" => Function::new_typed(store, |x: f32, y: f32| -> f32 { x.min(y) }),
                    "_rpu_max" => Function::new_typed(store, |x: f32, y: f32| -> f32 { x.max(y) }),
                    "_rpu_pow" => Function::new_typed(store, |x: f32, y: f32| -> f32 { x.powf(y) }),
                    "_rpu_mod" => Function::new_typed(store, |x: f32, y: f32| -> f32 { x - y * (x / y).floor() }),
                    "_rpu_step" => Function::new_typed(store, |edge: f32, x: f32| -> f32 { if x < edge {
                        0.0
                    } else {
                        1.0
                    }}),
                    "_rpu_rand" => Function::new_typed(store, || -> f32 {
                        let mut rng = rand::thread_rng();
                        rng.gen()
                    }),
                    "_rpu_sign" => Function::new_typed(store, |x: f32| -> f32 { x.signum() }),
                    "_rpu_clamp" => Function::new_typed(store, |x: f32, y: f32, z: f32| -> f32 { x.clamp(y, z) }),
                },
            }
        }
    }

    /// Get the current time
    pub fn get_time(&self) -> u128 {
        #[cfg(target_arch = "wasm32")]
        {
            web_sys::window().unwrap().performance().unwrap().now() as u128
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            let stop = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("Time went backwards");
            stop.as_millis()
        }
    }
}