Skip to content

Commit

Permalink
Compiled wasm interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosan committed Jan 25, 2020
1 parent 0c7c681 commit cef6ece
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion rendering/src/raytracer/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::raytracer::Image;
use ndarray::arr1;
use std::time::Instant;

type Frame = Image;
pub type Frame = Image;

#[no_mangle]
pub extern "C" fn get_frame() -> Box<Frame> {
Expand Down
47 changes: 42 additions & 5 deletions rendering_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate rendering;

//use rendering::raytracer::external::{get_frame, Frame};
use rendering::raytracer::external::get_frame;
use wasm_bindgen::prelude::*;

mod utils;
Expand All @@ -21,7 +21,44 @@ pub fn greet() {
alert("Hello, {{project-name}}!");
}

//#[wasm_bindgen]
//pub fn render() -> Box<Frame> {
// get_frame()
//}
#[wasm_bindgen]
pub fn render() -> ByteStream {
let frame = get_frame();
ByteStream::new(&frame.data, frame.width, frame.height)
}

#[wasm_bindgen]
pub struct ByteStream {
data: *const u8,
width: u32,
height: u32,
size: usize,
}

#[wasm_bindgen]
impl ByteStream {
pub fn new(bytes: &[u8], width: u32, height: u32) -> ByteStream {
ByteStream {
data: bytes.as_ptr(),
size: bytes.len(),
width,
height,
}
}

pub fn data(&self) -> *const u8 {
self.data
}

pub fn size(&self) -> usize {
self.size
}

pub fn width(&self) -> u32 {
self.width
}

pub fn height(&self) -> u32 {
self.height
}
}

0 comments on commit cef6ece

Please sign in to comment.