From d727f6e769ae6f3210a57f32f6c198469efaa9b8 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 25 May 2019 22:44:04 +0200 Subject: Run with lighttpd and remove deploy.py --- WebInterface/wasm/asm-paint/Cargo.toml | 1 - WebInterface/wasm/asm-paint/build.sh | 3 - WebInterface/wasm/asm-paint/deploy | 4 -- WebInterface/wasm/asm-paint/deploy.py | 102 ---------------------------- WebInterface/wasm/asm-paint/lighttpd.config | 29 ++++++++ WebInterface/wasm/asm-paint/loader.js | 4 +- WebInterface/wasm/asm-paint/run | 3 + WebInterface/wasm/asm-paint/src/lib.rs | 16 ++--- 8 files changed, 39 insertions(+), 123 deletions(-) delete mode 100755 WebInterface/wasm/asm-paint/build.sh delete mode 100755 WebInterface/wasm/asm-paint/deploy delete mode 100755 WebInterface/wasm/asm-paint/deploy.py create mode 100644 WebInterface/wasm/asm-paint/lighttpd.config create mode 100755 WebInterface/wasm/asm-paint/run diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 5713415..c0a7c68 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -9,4 +9,3 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" -web-sys = {version="0.3.22", features=["Window", "Document", "HtmlElement", "Node", "Element"]} diff --git a/WebInterface/wasm/asm-paint/build.sh b/WebInterface/wasm/asm-paint/build.sh deleted file mode 100755 index 61c1997..0000000 --- a/WebInterface/wasm/asm-paint/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -wasm-pack build --target web diff --git a/WebInterface/wasm/asm-paint/deploy b/WebInterface/wasm/asm-paint/deploy deleted file mode 100755 index 9f7f823..0000000 --- a/WebInterface/wasm/asm-paint/deploy +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -./build.sh -./deploy.py diff --git a/WebInterface/wasm/asm-paint/deploy.py b/WebInterface/wasm/asm-paint/deploy.py deleted file mode 100755 index b64c322..0000000 --- a/WebInterface/wasm/asm-paint/deploy.py +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env python3 - -from socket import (socket, AF_INET, SOCK_STREAM, IPPROTO_TCP, - SOL_SOCKET, SO_REUSEADDR) -from threading import Thread - -WASM_MIME = 'application/wasm' -JS_MIME = 'application/javascript' -PLAIN_MIME = 'text/plain' -HTML_MIME = 'text/html' - -REQUESTS = { - '/': ('index.html', HTML_MIME), - '/loader.js': ('loader.js', JS_MIME), - '/asm_paint_rs.js': ('pkg/asm_paint_rs.js', JS_MIME), - '/asm_paint_rs_bg.wasm': ('pkg/asm_paint_rs_bg.wasm', WASM_MIME), -} - -PAGE_404 = ''' - -

Request '404 Not Found'

- resource
'{}'
not found
- -''' - -def header_line_to_entry(line): - key, value = line.decode('utf-8').split(': ') - return key, value - - -class Server: - def __init__(self): - self.s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) - self.s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) - self.threads = [] - - def rec_http(self, client): - headers = b'' - while not headers.endswith(b'\r\n' * 2): - headers += client.recv(1) - headers = headers.split(b'\r\n') - head, headers = headers[0], headers[1:] - method, url, _ = head.split(b' ') - url = url.decode('utf-8') - headers = dict(header_line_to_entry(v) for v in headers if v) - if 'Content-Length' in headers: - client.recv(int(headers['Content-Length'])) - return method, url, headers - - def sen_http(self, client, status='200 OK', payload=b'', mime=PLAIN_MIME): - print('sende...') - client.send((f'HTTP/1.1 {status}\r\n' - f'Content-Length: {len(payload)}\r\n' - f'Content-Type: {mime}\r\n\r\n').encode('utf-8') - + payload) - print('gesendet') - - def run_client(self, client, addr): - while True: - print('wait for receive') - method, url, headers = self.rec_http(client) - print('got receive') - if method == b'GET': - if not url.startswith('/'): - url += '/' - print(f'got request for "{url}"') - if url in REQUESTS: - path, mime = REQUESTS[url] - f = open(path, 'rb') - payload = f.read() - f.close() - self.sen_http(client, '200 OK', payload, mime) - elif url == '/close': - client.close() - self.kill() - exit() - else: - self.sen_http(client, '404 Not Found', - PAGE_404.format(url).encode('utf-8'), - HTML_MIME) - else: - self.sen_http(client, '400 Bad Request', b'only supporting GET') - - def deploy(self, host='localhost', port=8080): - self.s.bind((host, port)) - self.s.listen(1) - while True: - client, addr = self.s.accept() - thread = Thread(target=self.run_client, args=(client,addr)) - self.threads.append(thread) - thread.run() - - def kill(self): - self.s.close() - - -if __name__ == '__main__': - try: - server = Server() - server.deploy() - finally: - server.kill() diff --git a/WebInterface/wasm/asm-paint/lighttpd.config b/WebInterface/wasm/asm-paint/lighttpd.config new file mode 100644 index 0000000..5fae32d --- /dev/null +++ b/WebInterface/wasm/asm-paint/lighttpd.config @@ -0,0 +1,29 @@ +server.http-parseopts = ( + "header-strict" => "enable", + "host-strict" => "enable", + "host-normalize" => "enable", + "url-normalize" => "enable", + "url-normalize-unreserved" => "enable", + "url-normalize-required" => "enable", + "url-ctrls-reject" => "enable", + "url-path-2f-decode" => "enable", + "url-path-dotseg-remove" => "enable", + "url-query-20-plus" => "enable" +) + +server.document-root = "/home/jan/projects/DiscoBot/WebInterface/wasm/asm-paint" +server.port = 8080 +dir-listing.activate = "enable" +index-file.names = ( "index.html" ) +mimetype.assign = ( + ".html" => "text/html", + ".txt" => "text/plain", + ".css" => "text/css", + ".js" => "application/x-javascript", + ".jpg" => "image/jpeg", + ".jpeg" => "image/jpeg", + ".gif" => "image/gif", + ".png" => "image/png", + ".wasm" => "application/wasm", + "" => "application/octet-stream" +) diff --git a/WebInterface/wasm/asm-paint/loader.js b/WebInterface/wasm/asm-paint/loader.js index f1d15ef..4566ee1 100644 --- a/WebInterface/wasm/asm-paint/loader.js +++ b/WebInterface/wasm/asm-paint/loader.js @@ -1,2 +1,2 @@ -import {default as init} from './asm_paint_rs.js' -init('asm_paint_rs_bg.wasm'); +import {default as init} from './pkg/asm_paint_rs.js' +init('./pkg/asm_paint_rs_bg.wasm'); diff --git a/WebInterface/wasm/asm-paint/run b/WebInterface/wasm/asm-paint/run new file mode 100755 index 0000000..61c1997 --- /dev/null +++ b/WebInterface/wasm/asm-paint/run @@ -0,0 +1,3 @@ +#!/bin/sh + +wasm-pack build --target web diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 462a89d..b6a5840 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -8,20 +8,14 @@ macro_rules! console_log { extern "C" { #[wasm_bindgen(js_namespace = console)] fn log(s: &str); + + #[wasm_bindgen(js_namespace = document)] + fn write(s: &str); } #[wasm_bindgen(start)] pub fn entry() { - use web_sys; - console_log!("hello {} wasm", 42); - - let window = web_sys::window().unwrap(); - - let document = window.document().unwrap(); - - let body = document.body().unwrap(); - - //body.set_inner_html("

Hello from WASM

"); + console_log!("hello {} wasm", 44); - body.set_inner_html("oho"); + write("gooo"); } -- cgit v1.2.3-54-g00ecf From e2e4f0f698b95ca979e78ac12fa2395635321dd3 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 25 May 2019 23:47:02 +0200 Subject: Add colored logging with fern --- WebInterface/wasm/asm-paint/Cargo.toml | 20 +++++++++++++- WebInterface/wasm/asm-paint/index.html | 1 + WebInterface/wasm/asm-paint/src/client_logger.rs | 34 ++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/lib.rs | 20 +++++--------- 4 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/client_logger.rs diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index c0a7c68..e8f5acb 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "asm-paint-rs" version = "0.1.0" -authors = ["Dennis Kobert "] +authors = [ + "natrixaeria", + "Dennis Kobert " +] edition = "2018" [lib] @@ -9,3 +12,18 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" +log = "0.4" +fern = "0.5" + +[dependencies.web-sys] +version = "0.3" +features = [ + 'Document', + 'Element', + 'HtmlCanvasElement', + 'WebGlBuffer', + 'WebGlRenderingContext', + 'WebGlProgram', + 'WebGlShader', + 'Window' +] diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 46be213..631122d 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -6,5 +6,6 @@ + diff --git a/WebInterface/wasm/asm-paint/src/client_logger.rs b/WebInterface/wasm/asm-paint/src/client_logger.rs new file mode 100644 index 0000000..a8765c6 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/client_logger.rs @@ -0,0 +1,34 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace=console, js_name=log)] + fn __console_log_colored2(f: &str, c1: &str, c2: &str); +} + +fn log(rec: &log::Record) { + __console_log_colored2(&format!("{}", rec.args()), + &format!("color: {}", match rec.level() { + log::Level::Trace => "violet", + log::Level::Debug => "blue", + log::Level::Info => "green", + log::Level::Warn => "orange", + log::Level::Error => "red" + }), ""); +} + +pub fn init_logger() { + fern::Dispatch::new().format(|out, message, record|{ + out.finish(format_args!( + "%c{}%c {} > {}", + record.level(), + record.target(), + message + ) + ) + }) + .level(log::LevelFilter::Debug) + .chain(fern::Output::call(log)) + .apply().unwrap(); +} + diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index b6a5840..fea9d0f 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,21 +1,13 @@ -use wasm_bindgen::prelude::*; - -macro_rules! console_log { - ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) -} +mod client_logger; -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); +use wasm_bindgen::prelude::*; - #[wasm_bindgen(js_namespace = document)] - fn write(s: &str); -} +#[macro_use] +extern crate log; #[wasm_bindgen(start)] pub fn entry() { - console_log!("hello {} wasm", 44); + client_logger::init_logger(); - write("gooo"); + info!("{}", 42); } -- cgit v1.2.3-54-g00ecf From fb31751fab32c8b67affd75603084e4f143bc758 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 16:09:03 +0200 Subject: Add basic project structure --- WebInterface/wasm/asm-paint/Cargo.toml | 2 +- WebInterface/wasm/asm-paint/run | 2 +- WebInterface/wasm/asm-paint/src/app.rs | 16 ++++++++++++++++ WebInterface/wasm/asm-paint/src/lib.rs | 7 ++++++- WebInterface/wasm/asm-paint/src/site.rs | 18 ++++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/app.rs create mode 100644 WebInterface/wasm/asm-paint/src/site.rs diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index e8f5acb..e2b1e17 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -3,7 +3,7 @@ name = "asm-paint-rs" version = "0.1.0" authors = [ "natrixaeria", - "Dennis Kobert " + "TrueDoctor" ] edition = "2018" diff --git a/WebInterface/wasm/asm-paint/run b/WebInterface/wasm/asm-paint/run index 61c1997..1da1e35 100755 --- a/WebInterface/wasm/asm-paint/run +++ b/WebInterface/wasm/asm-paint/run @@ -1,3 +1,3 @@ #!/bin/sh -wasm-pack build --target web +wasm-pack build --release --target web diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs new file mode 100644 index 0000000..d5d0e45 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -0,0 +1,16 @@ +use crate::site::Site; + +pub struct App { + site: Site, +} + +impl App { + pub fn new() -> Option { + Some(Self { + site: Site::from_current()?, + }) + } + + pub fn run(&self) { + } +} diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index fea9d0f..037e8e9 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,6 @@ mod client_logger; +mod site; +mod app; use wasm_bindgen::prelude::*; @@ -9,5 +11,8 @@ extern crate log; pub fn entry() { client_logger::init_logger(); - info!("{}", 42); + info!("begin running wasm application"); + + let app = app::App::new().unwrap(); + app.run(); } diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs new file mode 100644 index 0000000..740cb90 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -0,0 +1,18 @@ +use web_sys; + +pub struct Site { + window: web_sys::Window, + document: web_sys::Document, +} + +impl Site { + pub fn from_current() -> Option { + let window = web_sys::window() + .or_else(|| {error!("unable to query window"); None})?; + let document = window.document() + .or_else(|| {error!("unable to query document"); None})?; + Some(Self { + window, document + }) + } +} -- cgit v1.2.3-54-g00ecf From 12dcfc7320b7fd2b9e72f34e7411a6632cc3e4e0 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 18:04:40 +0200 Subject: Create a canvas --- WebInterface/wasm/asm-paint/deploy | 2 ++ WebInterface/wasm/asm-paint/index.html | 10 +++++++++ WebInterface/wasm/asm-paint/src/app.rs | 2 ++ WebInterface/wasm/asm-paint/src/canvas.rs | 27 ++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/client_logger.rs | 18 +++++++++++++--- WebInterface/wasm/asm-paint/src/lib.rs | 1 + WebInterface/wasm/asm-paint/src/site.rs | 9 ++++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100755 WebInterface/wasm/asm-paint/deploy create mode 100644 WebInterface/wasm/asm-paint/src/canvas.rs diff --git a/WebInterface/wasm/asm-paint/deploy b/WebInterface/wasm/asm-paint/deploy new file mode 100755 index 0000000..13d1d0a --- /dev/null +++ b/WebInterface/wasm/asm-paint/deploy @@ -0,0 +1,2 @@ +#!/bin/sh +lighttpd -f ./lighttpd.config diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 631122d..5a221e3 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -4,6 +4,16 @@ Scribblio + diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs index d5d0e45..003a2a1 100644 --- a/WebInterface/wasm/asm-paint/src/app.rs +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -12,5 +12,7 @@ impl App { } pub fn run(&self) { + let canvas = self.site.create_canvas().unwrap(); + canvas.render(); } } diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs new file mode 100644 index 0000000..a1ef415 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -0,0 +1,27 @@ +use web_sys; +use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +use wasm_bindgen::JsCast; + +pub struct Canvas { + element: web_sys::HtmlCanvasElement, + ctx: WebGlRenderingContext, +} + +impl Canvas { + pub fn new(element: web_sys::Element) -> Option { + let element: web_sys::HtmlCanvasElement = + element.dyn_into::() + .ok()?; + debug!("create webgl2 context"); + error!("'{:#?}'", element.get_context("webgl2").ok()??.dyn_into::()); + let ctx = element.get_context("webgl2").ok()?? + .dyn_into::().ok()?; + Some(Self { + element, ctx + }) + } + + pub fn render(&self) { + info!("rich kidd"); + } +} diff --git a/WebInterface/wasm/asm-paint/src/client_logger.rs b/WebInterface/wasm/asm-paint/src/client_logger.rs index a8765c6..f71918f 100644 --- a/WebInterface/wasm/asm-paint/src/client_logger.rs +++ b/WebInterface/wasm/asm-paint/src/client_logger.rs @@ -2,12 +2,24 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { - #[wasm_bindgen(js_namespace=console, js_name=log)] - fn __console_log_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=debug)] + fn __console_debug_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=info)] + fn __console_info_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=warn)] + fn __console_warn_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=error)] + fn __console_error_colored2(f: &str, c1: &str, c2: &str); } fn log(rec: &log::Record) { - __console_log_colored2(&format!("{}", rec.args()), + let log_fn = match rec.level() { + log::Level::Trace | log::Level::Debug => __console_debug_colored2, + log::Level::Info => __console_info_colored2, + log::Level::Warn => __console_warn_colored2, + log::Level::Error => __console_error_colored2, + }; + log_fn(&format!("{}", rec.args()), &format!("color: {}", match rec.level() { log::Level::Trace => "violet", log::Level::Debug => "blue", diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 037e8e9..8b0ee8f 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,5 @@ mod client_logger; +mod canvas; mod site; mod app; diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs index 740cb90..47afd19 100644 --- a/WebInterface/wasm/asm-paint/src/site.rs +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -1,4 +1,5 @@ use web_sys; +use crate::canvas::Canvas; pub struct Site { window: web_sys::Window, @@ -15,4 +16,12 @@ impl Site { window, document }) } + + pub fn create_canvas(&self) -> Option { + debug!("gain canvas element"); + let element = self.document.get_element_by_id("canvas") + .or_else(|| {error!("could not gain canvas element"); None})?; + Canvas::new(element) + .or_else(|| {error!("could not create a webgl2 canvas"); None}) + } } -- cgit v1.2.3-54-g00ecf From 77a41c3065aea3269f0c48da9cc736c404a624c0 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 18:15:48 +0200 Subject: Fix WebGl -> WebGl2 bug --- WebInterface/wasm/asm-paint/Cargo.toml | 6 +- WebInterface/wasm/asm-paint/src/canvas.rs | 10 +-- WebInterface/wasm/asm-paint/src/lib.rs | 119 ++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 9 deletions(-) diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index e2b1e17..748bb8e 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -14,6 +14,7 @@ crate-type = ["cdylib"] wasm-bindgen = "0.2" log = "0.4" fern = "0.5" +js-sys = "0.3" [dependencies.web-sys] version = "0.3" @@ -21,9 +22,6 @@ features = [ 'Document', 'Element', 'HtmlCanvasElement', - 'WebGlBuffer', - 'WebGlRenderingContext', - 'WebGlProgram', - 'WebGlShader', + 'WebGl2RenderingContext', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index a1ef415..1956be0 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -1,10 +1,10 @@ use web_sys; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +use web_sys::{WebGl2RenderingContext}; use wasm_bindgen::JsCast; pub struct Canvas { element: web_sys::HtmlCanvasElement, - ctx: WebGlRenderingContext, + ctx: WebGl2RenderingContext, } impl Canvas { @@ -13,15 +13,15 @@ impl Canvas { element.dyn_into::() .ok()?; debug!("create webgl2 context"); - error!("'{:#?}'", element.get_context("webgl2").ok()??.dyn_into::()); let ctx = element.get_context("webgl2").ok()?? - .dyn_into::().ok()?; + .dyn_into::().ok()?; + info!("created webgl2 context successfully"); Some(Self { element, ctx }) } pub fn render(&self) { - info!("rich kidd"); + info!("do a barrel roll"); } } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 8b0ee8f..432c370 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -17,3 +17,122 @@ pub fn entry() { let app = app::App::new().unwrap(); app.run(); } + +/* +use js_sys::WebAssembly; +use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; +use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; + +#[wasm_bindgen(start)] +pub fn start() -> Result<(), JsValue> { + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id("canvas").unwrap(); + let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; + + let context = canvas + .get_context("webgl")? + .unwrap() + .dyn_into::()?; + + let vert_shader = compile_shader( + &context, + WebGlRenderingContext::VERTEX_SHADER, + r#" + attribute vec4 position; + void main() { + gl_Position = position; + } + "#, + )?; + let frag_shader = compile_shader( + &context, + WebGlRenderingContext::FRAGMENT_SHADER, + r#" + void main() { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + } + "#, + )?; + let program = link_program(&context, &vert_shader, &frag_shader)?; + context.use_program(Some(&program)); + + let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; + let memory_buffer = wasm_bindgen::memory() + .dyn_into::()? + .buffer(); + let vertices_location = vertices.as_ptr() as u32 / 4; + let vert_array = js_sys::Float32Array::new(&memory_buffer) + .subarray(vertices_location, vertices_location + vertices.len() as u32); + + let buffer = context.create_buffer().ok_or("failed to create buffer")?; + context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); + context.buffer_data_with_array_buffer_view( + WebGlRenderingContext::ARRAY_BUFFER, + &vert_array, + WebGlRenderingContext::STATIC_DRAW, + ); + context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); + context.enable_vertex_attrib_array(0); + + context.clear_color(0.0, 0.0, 0.0, 1.0); + context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); + + context.draw_arrays( + WebGlRenderingContext::TRIANGLES, + 0, + (vertices.len() / 3) as i32, + ); + Ok(()) +} + +pub fn compile_shader( + context: &WebGlRenderingContext, + shader_type: u32, + source: &str, +) -> Result { + let shader = context + .create_shader(shader_type) + .ok_or_else(|| String::from("Unable to create shader object"))?; + context.shader_source(&shader, source); + context.compile_shader(&shader); + + if context + .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) + .as_bool() + .unwrap_or(false) + { + Ok(shader) + } else { + Err(context + .get_shader_info_log(&shader) + .unwrap_or_else(|| String::from("Unknown error creating shader"))) + } +} + +pub fn link_program( + context: &WebGlRenderingContext, + vert_shader: &WebGlShader, + frag_shader: &WebGlShader, +) -> Result { + let program = context + .create_program() + .ok_or_else(|| String::from("Unable to create shader object"))?; + + context.attach_shader(&program, vert_shader); + context.attach_shader(&program, frag_shader); + context.link_program(&program); + + if context + .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) + .as_bool() + .unwrap_or(false) + { + Ok(program) + } else { + Err(context + .get_program_info_log(&program) + .unwrap_or_else(|| String::from("Unknown error creating program object"))) + } +} +*/ -- cgit v1.2.3-54-g00ecf From 9f0f87a772743ce67e37700516cae0ead17cced9 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 19:25:38 +0200 Subject: Create shaders --- WebInterface/wasm/asm-paint/Cargo.toml | 2 + WebInterface/wasm/asm-paint/src/app.rs | 7 ++-- WebInterface/wasm/asm-paint/src/canvas.rs | 20 ++++++++-- WebInterface/wasm/asm-paint/src/lib.rs | 3 +- WebInterface/wasm/asm-paint/src/shader.rs | 62 +++++++++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/site.rs | 3 +- 6 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/shader.rs diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 748bb8e..269d77d 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -23,5 +23,7 @@ features = [ 'Element', 'HtmlCanvasElement', 'WebGl2RenderingContext', + 'WebGlProgram', + 'WebGlShader', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs index 003a2a1..005764d 100644 --- a/WebInterface/wasm/asm-paint/src/app.rs +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -11,8 +11,9 @@ impl App { }) } - pub fn run(&self) { - let canvas = self.site.create_canvas().unwrap(); - canvas.render(); + pub fn run(&mut self) { + let mut canvas = self.site.create_canvas().unwrap(); + canvas.init().unwrap(); + info!("canvas initialisation was succuessfull"); } } diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index 1956be0..ddf6d51 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -1,10 +1,12 @@ use web_sys; use web_sys::{WebGl2RenderingContext}; use wasm_bindgen::JsCast; +use crate::shader::Shaders; pub struct Canvas { element: web_sys::HtmlCanvasElement, ctx: WebGl2RenderingContext, + shaders: Shaders, } impl Canvas { @@ -17,11 +19,23 @@ impl Canvas { .dyn_into::().ok()?; info!("created webgl2 context successfully"); Some(Self { - element, ctx + element, ctx, + shaders: Shaders::new(), }) } - pub fn render(&self) { - info!("do a barrel roll"); + pub fn init(&mut self) -> Result<(), ()> { + debug!("create program"); + self.shaders.create_program(&self.ctx) + .map_err(|e| error!("webgl2 create program: {}", e))?; + debug!("create vertex shader"); + self.shaders.create_vertex_shader(&self.ctx) + .map_err(|e| error!("webgl2 create vertex shader: {}", e))?; + debug!("create fragment shader"); + self.shaders.create_fragment_shader(&self.ctx) + .map_err(|e| error!("webgl2 create fragment shader: {}", e))?; + debug!("compile shader program"); + self.shaders.compile(&self.ctx) + .map_err(|e| error!("webgl2 shader: {}", e)) } } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 432c370..945f2b1 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,5 @@ mod client_logger; +mod shader; mod canvas; mod site; mod app; @@ -14,7 +15,7 @@ pub fn entry() { info!("begin running wasm application"); - let app = app::App::new().unwrap(); + let mut app = app::App::new().unwrap(); app.run(); } diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs new file mode 100644 index 0000000..ac7c767 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -0,0 +1,62 @@ +use web_sys::{WebGlProgram, WebGl2RenderingContext}; + +const VERTEX_SHADER: &str = r#"#version 300 es +in vec4 pos; +void main() { + gl_Position = pos; +} +"#; + +const FRAGMENT_SHADER: &str = r#"#version 300 es +precision mediump float; +out vec4 color; + +void main() { + color = vec4(1, 0, 0, 1); +} +"#; + +pub struct Shaders { + program: Option, +} + +impl Shaders { + pub fn new() -> Self { + Self { + program: None, + } + } + + pub fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.program = Some(ctx.create_program().ok_or("could not create program id")?); + Ok(()) + } + + fn create_shader(&mut self, ctx: &WebGl2RenderingContext, + shader_type: u32, source: &str) -> Result<(), String> { + let program = self.program.as_ref().ok_or("could not find created program")?; + let shader = ctx.create_shader(shader_type) + .ok_or("could not create shader")?; + ctx.shader_source(&shader, source); + ctx.compile_shader(&shader); + let status = ctx.get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS); + if status == wasm_bindgen::JsValue::TRUE { + ctx.attach_shader(program, &shader); + Ok(()) + } else { + Err(format!("\n{}", ctx.get_shader_info_log(&shader).unwrap_or_default())) + } + } + + pub fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER) + } + + pub fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER) + } + + pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + Ok(()) + } +} diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs index 47afd19..4ae0237 100644 --- a/WebInterface/wasm/asm-paint/src/site.rs +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -22,6 +22,7 @@ impl Site { let element = self.document.get_element_by_id("canvas") .or_else(|| {error!("could not gain canvas element"); None})?; Canvas::new(element) - .or_else(|| {error!("could not create a webgl2 canvas"); None}) + .or_else(|| {error!("could not create a webgl2 canvas. + Your browser doesn't seem to support webgl2"); None}) } } -- cgit v1.2.3-54-g00ecf From 5f74e093a4679cdc0fe9fa42822a14fecc7b1cbb Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 20:59:02 +0200 Subject: Compile webgl shader program --- WebInterface/wasm/asm-paint/Cargo.toml | 6 +++--- WebInterface/wasm/asm-paint/src/canvas.rs | 6 ++++++ WebInterface/wasm/asm-paint/src/shader.rs | 13 ++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 269d77d..ceb1866 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -14,16 +14,16 @@ crate-type = ["cdylib"] wasm-bindgen = "0.2" log = "0.4" fern = "0.5" -js-sys = "0.3" [dependencies.web-sys] -version = "0.3" +version = "0.3.22" features = [ 'Document', 'Element', 'HtmlCanvasElement', 'WebGl2RenderingContext', - 'WebGlProgram', 'WebGlShader', + 'WebGlProgram', + 'WebGlBuffer', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index ddf6d51..54691e8 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -39,3 +39,9 @@ impl Canvas { .map_err(|e| error!("webgl2 shader: {}", e)) } } + +impl Drop for Canvas { + fn drop(&mut self) { + self.shaders.remove(&self.ctx); + } +} diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index ac7c767..3311b6a 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -57,6 +57,17 @@ impl Shaders { } pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { - Ok(()) + let program = self.program.as_ref().ok_or("could not find created program")?; + ctx.link_program(program); + let status = ctx.get_program_parameter(program, WebGl2RenderingContext::LINK_STATUS); + if status == wasm_bindgen::JsValue::TRUE { + Ok(()) + } else { + Err(format!("\n{}", ctx.get_program_info_log(program).unwrap_or_default())) + } + } + + pub fn remove(&mut self, ctx: &WebGl2RenderingContext) { + ctx.delete_program(self.program.as_ref()) } } -- cgit v1.2.3-54-g00ecf From 3c21251fec1a1ce4cee42337e727cc8dadfd9563 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 21 Dec 2019 20:45:49 +0100 Subject: Draw violet background --- WebInterface/wasm/asm-paint/src/canvas.rs | 16 ++++------------ WebInterface/wasm/asm-paint/src/shader.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index 54691e8..400e258 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -25,18 +25,10 @@ impl Canvas { } pub fn init(&mut self) -> Result<(), ()> { - debug!("create program"); - self.shaders.create_program(&self.ctx) - .map_err(|e| error!("webgl2 create program: {}", e))?; - debug!("create vertex shader"); - self.shaders.create_vertex_shader(&self.ctx) - .map_err(|e| error!("webgl2 create vertex shader: {}", e))?; - debug!("create fragment shader"); - self.shaders.create_fragment_shader(&self.ctx) - .map_err(|e| error!("webgl2 create fragment shader: {}", e))?; - debug!("compile shader program"); - self.shaders.compile(&self.ctx) - .map_err(|e| error!("webgl2 shader: {}", e)) + self.shaders.init(&self.ctx).map_err(|_|())?; + self.ctx.clear_color(1.0, 0.2, 1.0, 1.0); + self.ctx.clear(WebGl2RenderingContext::COLOR_BUFFER_BIT); + Ok(()) } } diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index 3311b6a..2823cd4 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -18,16 +18,18 @@ void main() { pub struct Shaders { program: Option, + pos_loc: i32, } impl Shaders { pub fn new() -> Self { Self { program: None, + pos_loc: -1 } } - pub fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.program = Some(ctx.create_program().ok_or("could not create program id")?); Ok(()) } @@ -48,15 +50,15 @@ impl Shaders { } } - pub fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER) } - pub fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER) } - pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { let program = self.program.as_ref().ok_or("could not find created program")?; ctx.link_program(program); let status = ctx.get_program_parameter(program, WebGl2RenderingContext::LINK_STATUS); @@ -67,6 +69,26 @@ impl Shaders { } } + pub fn init(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + debug!("create program"); + self.create_program(ctx) + .map_err(|e| { error!("webgl2 create program: {}", e); e})?; + debug!("create vertex shader"); + self.create_vertex_shader(ctx) + .map_err(|e| { error!("webgl2 create vertex shader: {}", e); e})?; + debug!("create fragment shader"); + self.create_fragment_shader(ctx) + .map_err(|e| { error!("webgl2 create fragment shader: {}", e); e})?; + debug!("compile shader program"); + self.compile(ctx) + .map_err(|e| { error!("webgl2 shader: {}", e); e})?; + let program = self.program.as_ref().ok_or("could not find created program")?; + self.pos_loc = ctx.get_attrib_location(program, "pos"); + trace!("got attrib location 'pos'({})", self.pos_loc); + info!("initialised shader program"); + Ok(()) + } + pub fn remove(&mut self, ctx: &WebGl2RenderingContext) { ctx.delete_program(self.program.as_ref()) } -- cgit v1.2.3-54-g00ecf From a6d2d62fae44b6d8c96e51055f7222bc679efc48 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 29 May 2019 21:00:49 +0200 Subject: Reforge code snippets --- WebInterface/wasm/asm-paint/index.html | 3 + WebInterface/wasm/asm-paint/src/lib.rs | 119 ------------------------------ WebInterface/wasm/asm-paint/src/shader.rs | 6 +- 3 files changed, 7 insertions(+), 121 deletions(-) diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 5a221e3..c4f3734 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -13,6 +13,9 @@ width: 100%; height: 100%; } + img { + background: violet; + } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 945f2b1..6c773c5 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -18,122 +18,3 @@ pub fn entry() { let mut app = app::App::new().unwrap(); app.run(); } - -/* -use js_sys::WebAssembly; -use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; - -#[wasm_bindgen(start)] -pub fn start() -> Result<(), JsValue> { - let document = web_sys::window().unwrap().document().unwrap(); - let canvas = document.get_element_by_id("canvas").unwrap(); - let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; - - let context = canvas - .get_context("webgl")? - .unwrap() - .dyn_into::()?; - - let vert_shader = compile_shader( - &context, - WebGlRenderingContext::VERTEX_SHADER, - r#" - attribute vec4 position; - void main() { - gl_Position = position; - } - "#, - )?; - let frag_shader = compile_shader( - &context, - WebGlRenderingContext::FRAGMENT_SHADER, - r#" - void main() { - gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); - } - "#, - )?; - let program = link_program(&context, &vert_shader, &frag_shader)?; - context.use_program(Some(&program)); - - let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; - let memory_buffer = wasm_bindgen::memory() - .dyn_into::()? - .buffer(); - let vertices_location = vertices.as_ptr() as u32 / 4; - let vert_array = js_sys::Float32Array::new(&memory_buffer) - .subarray(vertices_location, vertices_location + vertices.len() as u32); - - let buffer = context.create_buffer().ok_or("failed to create buffer")?; - context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); - context.buffer_data_with_array_buffer_view( - WebGlRenderingContext::ARRAY_BUFFER, - &vert_array, - WebGlRenderingContext::STATIC_DRAW, - ); - context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); - context.enable_vertex_attrib_array(0); - - context.clear_color(0.0, 0.0, 0.0, 1.0); - context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); - - context.draw_arrays( - WebGlRenderingContext::TRIANGLES, - 0, - (vertices.len() / 3) as i32, - ); - Ok(()) -} - -pub fn compile_shader( - context: &WebGlRenderingContext, - shader_type: u32, - source: &str, -) -> Result { - let shader = context - .create_shader(shader_type) - .ok_or_else(|| String::from("Unable to create shader object"))?; - context.shader_source(&shader, source); - context.compile_shader(&shader); - - if context - .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(shader) - } else { - Err(context - .get_shader_info_log(&shader) - .unwrap_or_else(|| String::from("Unknown error creating shader"))) - } -} - -pub fn link_program( - context: &WebGlRenderingContext, - vert_shader: &WebGlShader, - frag_shader: &WebGlShader, -) -> Result { - let program = context - .create_program() - .ok_or_else(|| String::from("Unable to create shader object"))?; - - context.attach_shader(&program, vert_shader); - context.attach_shader(&program, frag_shader); - context.link_program(&program); - - if context - .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(program) - } else { - Err(context - .get_program_info_log(&program) - .unwrap_or_else(|| String::from("Unknown error creating program object"))) - } -} -*/ diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index 2823cd4..3352bcf 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -1,13 +1,15 @@ use web_sys::{WebGlProgram, WebGl2RenderingContext}; -const VERTEX_SHADER: &str = r#"#version 300 es +const VERTEX_SHADER: &str = +r#"#version 300 es in vec4 pos; void main() { gl_Position = pos; } "#; -const FRAGMENT_SHADER: &str = r#"#version 300 es +const FRAGMENT_SHADER: &str = +r#"#version 300 es precision mediump float; out vec4 color; -- cgit v1.2.3-54-g00ecf