1
Fork 0

added rayon support

This commit is contained in:
Andy Killorin 2023-09-16 15:42:03 -05:00
parent 41d8def875
commit 21db4f155e
Signed by: ank
GPG key ID: B6241CA3B552BCA4
5 changed files with 134 additions and 24 deletions

99
Cargo.lock generated
View file

@ -81,6 +81,49 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"crossbeam-utils",
"memoffset 0.9.0",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if 1.0.0",
]
[[package]] [[package]]
name = "cty" name = "cty"
version = "0.2.2" version = "0.2.2"
@ -102,6 +145,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.3" version = "0.3.3"
@ -257,6 +306,12 @@ dependencies = [
"windows", "windows",
] ]
[[package]]
name = "hermit-abi"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -372,6 +427,15 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "memory_units" name = "memory_units"
version = "0.4.0" version = "0.4.0"
@ -433,7 +497,7 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
"memoffset", "memoffset 0.6.5",
] ]
[[package]] [[package]]
@ -497,6 +561,16 @@ dependencies = [
"libm", "libm",
] ]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.18.0" version = "1.18.0"
@ -539,6 +613,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"libm", "libm",
"nalgebra", "nalgebra",
"rayon",
"spin", "spin",
"wee_alloc", "wee_alloc",
] ]
@ -576,6 +651,28 @@ dependencies = [
"cty", "cty",
] ]
[[package]]
name = "rayon"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"

View file

@ -11,7 +11,11 @@ gamepad = ["gilrs"]
[dependencies] [dependencies]
gilrs = { version = "0.10.2", optional = true } gilrs = { version = "0.10.2", optional = true }
minifb = "0.25.0" minifb = "0.25.0"
pirates = { path = "../pirates", default_features = false }
[dependencies.pirates]
path = "../pirates"
default_features = false
features = ["rayon"]
[package.metadata.bundle] [package.metadata.bundle]
name = "Simple Sailing Simulator" name = "Simple Sailing Simulator"

View file

@ -21,9 +21,6 @@ function keyboard_callback(e) {
const value = new Uint8ClampedArray(exports.memory.buffer, keycode_address, 2); const value = new Uint8ClampedArray(exports.memory.buffer, keycode_address, 2);
value[0] = e.keyCode; value[0] = e.keyCode;
console.log("thing" + value[0]);
console.log(keycode_address);
console.log(value[0]);
exports.keyboard_input(); exports.keyboard_input();
} }

View file

@ -18,6 +18,10 @@ crate-type = ["lib", "cdylib"]
libm = "0.2.7" libm = "0.2.7"
spin = "0.9.8" spin = "0.9.8"
[dependencies.rayon]
version = "1.7.0"
optional = true
[dependencies.wee_alloc] [dependencies.wee_alloc]
version = "0.4.5" version = "0.4.5"
optional = true optional = true

View file

@ -18,6 +18,8 @@ use libm;
extern crate nalgebra as na; extern crate nalgebra as na;
use nalgebra::Vector2; use nalgebra::Vector2;
use spin::Mutex; use spin::Mutex;
#[cfg(feature = "rayon")]
use rayon::prelude::*;
mod sampler; mod sampler;
mod noise; mod noise;
@ -135,26 +137,32 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
// draw sea // draw sea
const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0); const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0);
for y in 0..HEIGHT {
for x in 0..WIDTH { #[cfg(feature = "rayon")]
let mut point = Vector2::new(x as f32, y as f32); let mut buffer_iter = buffer.par_iter_mut();
point -= HALF; #[cfg(not(feature = "rayon"))]
point *= camera[2]; let mut buffer_iter = buffer.iter_mut();
point += HALF;
let n = sample_world(point+camera_vec+boat_pos, rand); buffer_iter.enumerate().for_each(|pix| {
buffer[y*WIDTH + x] = let y = pix.0 / WIDTH;
if n > 0.1 { let x = pix.0 % WIDTH;
let n = (n+0.1) * 300.0; let mut point = Vector2::new(x as f32, y as f32);
0xFF00FF00 + (n as u32 + (n as u32) << 8) point -= HALF;
} else if n > 0.04 { point *= camera[2];
let n = (0.1-n) * -300.0; point += HALF;
0xFF44FF44 + (n as u32 + (n as u32) << 8) let n = sample_world(point+camera_vec+boat_pos, rand);
} else { *pix.1 =
let n = (n+0.1) * 300.0; if n > 0.1 {
0xFFFF3333 + (n as u32 + (n as u32) << 8) let n = (n+0.1) * 300.0;
} 0xFF00FF00 + (n as u32 + (n as u32) << 8)
} else if n > 0.04 {
let n = (0.1-n) * -300.0;
0xFF44FF44 + (n as u32 + (n as u32) << 8)
} else {
let n = (n+0.1) * 300.0;
0xFFFF3333 + (n as u32 + (n as u32) << 8)
} }
} });
// draw boat // draw boat