wasm: simplify down to just use MODE 3 (draw3).

draw3 was the pretty clear winner performance-wise.
This commit is contained in:
Joel Martin 2018-07-26 16:03:12 +09:00
parent af54d4b95e
commit 5372d169cc
3 changed files with 8 additions and 112 deletions

View File

@ -30,17 +30,3 @@ npm run serve # then visit localhost:8080 outside the container
Note that `run server` will automatically detect modification to
`index.js` and reload the page.
## Preliminary results:
* 2048x1024, draw1, release: 23 fps
* 2048x1024, draw2, release: 51 fps
* 2048x1024, draw3, release: 60 fps
* 1024x1024, draw1, release: 36 fps
* 1024x1024, draw2, release: 60 fps
* 1024x1024, draw3, release: 60 fps
* 1024x1024, draw1, debug: 3 fps
* 1024x1024, draw2, debug: 8 fps
* 1024x1024, draw3, debug: 9 fps

View File

@ -1,6 +1,5 @@
const WIDTH = 2048
const HEIGHT = 1024
const MODE = 3
import * as novnc from './novnc'
import { memory } from './novnc_bg'
@ -11,32 +10,22 @@ const ctx = canvas.getContext('2d')
canvas.width = WIDTH
canvas.height = HEIGHT
if (MODE === 2 || MODE === 3) {
let byteSize = WIDTH * HEIGHT * 4
var pointer = novnc.alloc( byteSize )
let byteSize = WIDTH * HEIGHT * 4
let pointer = novnc.alloc( byteSize )
var u8array = new Uint8ClampedArray(memory.buffer, pointer, byteSize)
var imgData = new ImageData(u8array, WIDTH, HEIGHT)
}
let u8array = new Uint8ClampedArray(memory.buffer, pointer, byteSize)
let imgData = new ImageData(u8array, WIDTH, HEIGHT)
let frame = -1
function renderLoop() {
const startMs = (new Date()).getTime()
//const startMs = (new Date()).getTime()
fps.render()
frame += 1
if (MODE === 1) {
novnc.draw1(ctx, WIDTH, HEIGHT, frame)
} else {
if (MODE === 2) {
novnc.draw2(pointer, WIDTH, HEIGHT, frame)
} else if (MODE === 3) {
novnc.draw3(pointer, WIDTH, HEIGHT, frame)
}
novnc.draw(pointer, WIDTH, HEIGHT, frame)
ctx.putImageData(imgData, 0, 0)
}
animationId = requestAnimationFrame(renderLoop)
//console.log("elapsed:", (new Date()).getTime() - startMs)
}

View File

@ -7,62 +7,6 @@ use std::slice;
use std::os::raw::c_void;
use wasm_bindgen::prelude::*;
//////////////////////////////////////////////////////////
/// draw1
#[wasm_bindgen]
extern "C" {
pub type ImageData;
#[wasm_bindgen(constructor)]
pub fn new(arr: &Uint8ClampedArray, width: u32, height: u32) -> ImageData;
}
#[wasm_bindgen]
extern "C" {
pub type Uint8ClampedArray;
#[wasm_bindgen(constructor)]
pub fn new(arr: &[u8]) -> Uint8ClampedArray;
}
#[wasm_bindgen]
extern "C" {
pub type CanvasRenderingContext2D;
#[wasm_bindgen(method, js_name = putImageData)]
pub fn put_image_data(this: &CanvasRenderingContext2D, image_data: &ImageData, p_1: i32, p_2: i32);
}
pub fn fill(width: u32, height: u32, frame: u32) -> Vec<u8> {
let mut data: Vec<u8> = vec![];
for x in 0..width {
for y in 0..height {
let r = if (x%512) < 256 {x%256} else {255-(x%256)};
let g = if (y%512) < 256 {y%256} else {255-(y%256)};
let b = if (frame%512) < 256 {frame%256} else {255-(frame%256)};
data.push(r as u8);
data.push(g as u8);
data.push(b as u8);
data.push(255);
}
}
data
}
#[wasm_bindgen]
pub fn draw1(ctx: &CanvasRenderingContext2D, width: u32, height: u32, frame: u32) {
let data = fill(width, height, frame);
let uint8_array = Uint8ClampedArray::new(&data);
ctx.put_image_data(&ImageData::new(&uint8_array, width, height), 0, 0);
}
//////////////////////////////////////////////////////////
/// draw2
// In order to work with the memory we expose allocation method
#[wasm_bindgen]
pub fn alloc(size: usize) -> *mut c_void {
@ -73,30 +17,7 @@ pub fn alloc(size: usize) -> *mut c_void {
}
#[wasm_bindgen]
pub fn draw2(mem: *mut u8, width: usize, height: usize, frame: u32) {
// pixels are stored in RGBA, so each pixel is 4 bytes
let sl = unsafe { slice::from_raw_parts_mut(mem, width * height * 4) };
for y in 0..height {
for x in 0..width {
let r = if (x%512) < 256 {x%256} else {255-(x%256)};
let g = if (y%512) < 256 {y%256} else {255-(y%256)};
let b = if (frame%512) < 256 {frame%256} else {255-(frame%256)};
let xy = x*4 + y*4*width;
sl[xy + 0] = r as u8;
sl[xy + 1] = g as u8;
sl[xy + 2] = b as u8;
sl[xy + 3] = 255;
}
}
}
//////////////////////////////////////////////////////////
/// draw3
#[wasm_bindgen]
pub fn draw3(mem: *mut u32, width: usize, height: usize, frame: u32) {
pub fn draw(mem: *mut u32, width: usize, height: usize, frame: u32) {
// pixels are stored in RGBA
let sl = unsafe { slice::from_raw_parts_mut(mem, width * height) };