Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from novorender/wasm_bindgen_parser
Browse files Browse the repository at this point in the history
Full wasm bindgen parser
  • Loading branch information
arturoc authored Oct 19, 2023
2 parents 1b415f6 + 28fd32d commit a0fe134
Show file tree
Hide file tree
Showing 12 changed files with 1,675 additions and 537 deletions.
13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm-parser"
version = "0.1.2"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -16,13 +16,22 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
paste = "1.0.14"
wasm-parser-derive = { path = "wasm-parser-derive" }
# densevec = { version = "0.5.1", features = [], optional = true }
# once_cell = { version = "1.18.0", features = [] }
# bumpalo = { version = "3.14.0", optional = true }
# gl = "0.14.0"
bitflags = { version = "2.4.0", features = ["serde", "bytemuck"] }
half = { version = "2.3.1", features = ["bytemuck", "serde"] }
bytemuck = { version = "1.14.0", features = [ "derive" ] }
# bincode = "1"
# serde = { version = "1", default-features = false, features = ["derive", "serde_derive"] }
wasm-bindgen = { version = "0.2.87", features = [] }
wee_alloc = { version = "0.4.5", optional = true }
web-sys = { version = "0.3.64", features = ["console"], optional = true }
js-sys = "0.3.64"
# serde-wasm-bindgen = "0.6.0"
# serde_tuple = "0.5.0"
hashbrown = "0.14.1"
gl = "0.14.0"

[target.'cfg(not(target_family="wasm"))'.dependencies]
log = "0.4.20"
Expand Down
44 changes: 26 additions & 18 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,23 @@ mod benches {
#[bench]
fn parse(b: &mut test::Bencher) {
use std::io::Read;
use wasm_parser::parser::{Version, Schema};

let mut file = std::fs::File::open("8AC1B48A77DC9D0E0AE8DDC366379FFF").unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
let mut schema = Schema::parse(&data, Version::_2_1);
let mut schema = wasm_parser::types_2_1::Schema::parse(&data);
b.iter(|| {
schema = Schema::parse(&data, Version::_2_1);
schema = wasm_parser::types_2_1::Schema::parse(&data);
});

let sub_mesh_projection = match &schema {
Schema::Schema2_1(schema) => &schema.sub_mesh_projection,
_ => unreachable!()
};

for p in sub_mesh_projection.primitive_type() {
for p in unsafe{ schema.sub_mesh_projection.primitive_type.as_slice(schema.sub_mesh_projection.len) } {
assert!((*p as u8) < 7);
}
for p in sub_mesh_projection.attributes() {
for p in unsafe{ schema.sub_mesh_projection.attributes.as_slice(schema.sub_mesh_projection.len) } {
let mut p = *p;
p.remove(wasm_parser::types::OptionalVertexAttribute::NORMAL);
p.remove(wasm_parser::types::OptionalVertexAttribute::COLOR);
p.remove(wasm_parser::types::OptionalVertexAttribute::TEX_COORD);
p.remove(wasm_parser::types::OptionalVertexAttribute::PROJECTED_POS);
p.remove(wasm_parser::types_2_1::OptionalVertexAttribute::NORMAL);
p.remove(wasm_parser::types_2_1::OptionalVertexAttribute::COLOR);
p.remove(wasm_parser::types_2_1::OptionalVertexAttribute::TEX_COORD);
p.remove(wasm_parser::types_2_1::OptionalVertexAttribute::PROJECTED_POS);
assert!(p.is_empty());
}
}
Expand All @@ -62,14 +55,29 @@ mod benches {
#[bench]
fn children(b: &mut test::Bencher) {
use std::io::Read;
use wasm_parser::parser::{Version, Schema};
let mut file = std::fs::File::open("8AC1B48A77DC9D0E0AE8DDC366379FFF").unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
let schema = wasm_parser::types_2_1::Schema::parse(&data);
b.iter(|| {
let _ = schema.children(|_| true).collect::<Vec<_>>();
});
}


#[bench]
fn geometry(b: &mut test::Bencher) {
use std::io::Read;
let mut file = std::fs::File::open("8AC1B48A77DC9D0E0AE8DDC366379FFF").unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
let mut schema = wasm_parser::parser::Schema::parse(&data, Version::_2_1);
let schema = wasm_parser::types_2_1::Schema::parse(&data);
b.iter(|| {
let _ = schema.children(|_| true);
let _ = schema.geometry(
false,
wasm_parser::parser::Highlights{ indices: &[] },
|_| true
);
});
}
}
49 changes: 49 additions & 0 deletions src/interleaved.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::mem::size_of;

pub fn copy_to_interleaved_array<T: Copy + Send + Sync>(dst: &mut [T], src: &[T], byte_offset: usize, byte_stride: usize, begin: usize, end: usize) {
debug_assert_eq!(byte_offset % size_of::<T>(), 0);
debug_assert_eq!(byte_stride % size_of::<T>(), 0);

let offset = byte_offset / size_of::<T>();
let stride = byte_stride / size_of::<T>();

for (dst, src) in dst[offset..].iter_mut().step_by(stride).zip(&src[begin..end]) {
*dst = *src
}
}


pub fn fill_to_interleaved_array<T: Copy + Send + Sync>(dst: &mut [T], src: T, byte_offset: usize, byte_stride: usize, begin: usize, end: usize) {
debug_assert_eq!(byte_offset % size_of::<T>(), 0);
debug_assert_eq!(byte_stride % size_of::<T>(), 0);

let offset = byte_offset / size_of::<T>();
let stride = byte_stride / size_of::<T>();

let end = (offset + stride * (end - begin)).min(dst.len());

for dst in dst[offset..end].iter_mut().step_by(stride) {
*dst = src;
}
}

#[cfg(test)]
mod test {
use std::mem::size_of;

#[test]
fn test_copy_interleaved() {
let src = [0f32, 1., 2., 3., 4., 5., 6.];
let mut dst = [0.; 14];
super::copy_to_interleaved_array(&mut dst, &src, size_of::<f32>() * 2, size_of::<f32>() * 2, 1, 7);
assert_eq!(dst, [0., 0., 1., 0., 2., 0., 3., 0., 4., 0., 5., 0., 6., 0.]);
}

#[test]
fn test_fill_interleaved() {
let src = 1.;
let mut dst = [0f32; 14];
super::fill_to_interleaved_array(&mut dst, src, size_of::<f32>() * 2, size_of::<f32>() * 2, 1, 7);
assert_eq!(dst, [0., 0., 1., 0., 1., 0., 1., 0., 1., 0., 1., 0., 1., 0.]);
}
}
29 changes: 29 additions & 0 deletions src/ktx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone)]
pub struct TextureParameters {
#[wasm_bindgen(skip)]
pub kind: &'static str,
#[wasm_bindgen(skip)]
pub internal_format: &'static str,
pub ty: u32,
pub width: u32,
pub height: u32,
pub depth: u32,
#[wasm_bindgen(skip)]
pub image_data: Vec<u8>,
}

pub fn parse_ktx(ktx: &[u8]) -> TextureParameters {
// TODO:
TextureParameters {
kind: "TEXTURE_2D",
internal_format: "GL_RGBA8",
ty: gl::BYTE,
width: 1,
height: 1,
depth: 1,
image_data: vec![0, 0, 0, 1],
}
}
Loading

0 comments on commit a0fe134

Please sign in to comment.