Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get wasm build working #14

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Check wasm build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install wasm target
run: rustup target add wasm32-unknown-unknown
- name: Check
run: cargo check --target=wasm32-unknown-unknown --release -pfidget --no-default-features --features="rhai,render,mesh"
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fidget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ nalgebra = { version = "0.31" }
crossbeam-deque = { version = "0.8", optional = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[features]
default = ["jit", "rhai", "render", "mesh"]

Expand Down
2 changes: 2 additions & 0 deletions fidget/src/mesh/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl MeshBuilder {
}

impl DcBuilder for MeshBuilder {
type VertexIndex = usize;

fn cell(&mut self, octree: &Octree, cell: CellIndex) {
dc::dc_cell(octree, cell, self);
}
Expand Down
32 changes: 15 additions & 17 deletions fidget/src/mesh/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ impl std::fmt::Debug for CellData {
}
}

static_assertions::const_assert_eq!(
std::mem::size_of::<usize>(),
std::mem::size_of::<u64>()
);

/// Unpacked form of [`CellData`]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Cell {
Expand Down Expand Up @@ -74,22 +69,25 @@ impl Cell {

impl From<CellData> for Cell {
fn from(c: CellData) -> Self {
let i = c.0 as usize;
let i = c.0;
match i {
0 => Cell::Invalid,
1 => Cell::Empty,
2 => Cell::Full,
_ => match (i >> 62) & 0b11 {
0b10 => Cell::Branch {
index: i & ((1 << 54) - 1),
thread: (i >> 54) as u8,
},
0b11 => Cell::Leaf(Leaf {
mask: (i >> 54) as u8,
index: i & ((1 << 54) - 1),
}),
_ => panic!("invalid cell encoding"),
},
_ => {
let index = (i & ((1 << 54) - 1)).try_into().unwrap();
match (i >> 62) & 0b11 {
0b10 => Cell::Branch {
thread: (i >> 54) as u8,
index,
},
0b11 => Cell::Leaf(Leaf {
mask: (i >> 54) as u8,
index,
}),
_ => panic!("invalid cell encoding"),
}
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions fidget/src/mesh/dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use crate::mesh::{
};

pub trait DcBuilder {
/// Type for vertex indexes
///
/// This is typically a `usize`, but we'll sometimes explicitly force it to
/// be a `u64` if we're planning to use upper bits for flags.
type VertexIndex: Copy + Clone;

fn cell(&mut self, octree: &Octree, cell: CellIndex);
fn face<F: Frame>(&mut self, octree: &Octree, a: CellIndex, b: CellIndex);

Expand Down Expand Up @@ -49,7 +55,12 @@ pub trait DcBuilder {
///
/// The vertices are given in a clockwise winding with the intersection
/// vertex (i.e. the one on the edge) always last.
fn triangle(&mut self, a: usize, b: usize, c: usize);
fn triangle(
&mut self,
a: Self::VertexIndex,
b: Self::VertexIndex,
c: Self::VertexIndex,
);

/// Looks up the given vertex, localizing it within a cell
///
Expand All @@ -60,7 +71,7 @@ pub trait DcBuilder {
v: usize,
cell: CellIndex,
verts: &[CellVertex],
) -> usize;
) -> Self::VertexIndex;
}

pub fn dc_cell<B: DcBuilder>(octree: &Octree, cell: CellIndex, out: &mut B) {
Expand Down
2 changes: 2 additions & 0 deletions fidget/src/mesh/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ impl DcFixup {
}

impl DcBuilder for DcFixup {
type VertexIndex = usize;

fn cell(&mut self, octree: &Octree, cell: CellIndex) {
if let Cell::Leaf(Leaf { index, mask }) =
octree.cells[cell.index].into()
Expand Down
27 changes: 16 additions & 11 deletions fidget/src/mesh/mt/dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::mesh::{
types::{X, Y, Z},
Mesh, Octree,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicU64, Ordering};

#[derive(Debug)]
enum Task {
Expand Down Expand Up @@ -42,8 +42,8 @@ pub struct DcWorker<'a> {
/// - The top bit is always 1 (so 0 is not a valid claimed index)
/// - The next 8 bits indicate which thread has claimed the vertex
/// - The remaining bits are an index into that thread's [`DcWorker::verts`]
map: &'a [AtomicUsize],
tris: Vec<nalgebra::Vector3<usize>>,
map: &'a [AtomicU64],
tris: Vec<nalgebra::Vector3<u64>>,
verts: Vec<nalgebra::Vector3<f32>>,

/// Our personal queue of tasks to complete, along with references to other
Expand All @@ -58,7 +58,7 @@ impl<'a> DcWorker<'a> {
let map = octree
.verts
.iter()
.map(|_| AtomicUsize::new(0))
.map(|_| AtomicU64::new(0))
.collect::<Vec<_>>();

let mut workers = queues
Expand Down Expand Up @@ -132,8 +132,9 @@ impl<'a> DcWorker<'a> {
.iter_mut()
.zip(tris.iter().map(|t| {
t.map(|v| {
let thread = (v >> 55) & 0xFF;
let i = v & ((1 << 55) - 1);
let thread = ((v >> 55) & 0xFF) as usize;
let i: usize =
(v & ((1 << 55) - 1)).try_into().unwrap();
vert_offsets_ref[thread] + i
})
}))
Expand All @@ -152,7 +153,7 @@ impl<'a> DcWorker<'a> {
pub fn run(
mut self,
pool: &ThreadPool,
) -> (Vec<nalgebra::Vector3<usize>>, Vec<nalgebra::Vector3<f32>>) {
) -> (Vec<nalgebra::Vector3<u64>>, Vec<nalgebra::Vector3<f32>>) {
let mut ctx = pool.start(self.thread_index);

loop {
Expand Down Expand Up @@ -202,6 +203,10 @@ impl<'a> DcWorker<'a> {

#[allow(clippy::modulo_one, clippy::identity_op, unused_parens)]
impl DcBuilder for DcWorker<'_> {
// We need to reserve 1 byte for multithreading info, so we'll force the use
// of a u64 here (rather than a usize, which is 32-bit on some platforms)
type VertexIndex = u64;

fn cell(&mut self, _octree: &Octree, cell: CellIndex) {
self.queue.push(Task::Cell(cell));
}
Expand Down Expand Up @@ -231,7 +236,7 @@ impl DcBuilder for DcWorker<'_> {
}
}

fn triangle(&mut self, a: usize, b: usize, c: usize) {
fn triangle(&mut self, a: u64, b: u64, c: u64) {
self.tris.push(nalgebra::Vector3::new(a, b, c))
}

Expand All @@ -245,12 +250,12 @@ impl DcBuilder for DcWorker<'_> {
i: usize,
_cell: CellIndex,
verts: &[CellVertex],
) -> usize {
) -> u64 {
// Build our thread + vertex index
let mut next = self.verts.len();
let mut next = self.verts.len() as u64;
assert!(next < (1 << 55));
next |= 1 << 63;
next |= self.thread_index << 55;
next |= (self.thread_index as u64) << 55;

match self.map[i].compare_exchange(
0,
Expand Down
Loading