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

Configure hello example to run it in the web browser too #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ documentation = "https://docs.rs/wgpu_glyph"
readme = "README.md"

[dependencies]
wgpu = "0.7"
wgpu = {version = "0.7", features = ["webgl"]}
Copy link
Owner

@hecrj hecrj Feb 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware that wgpu supported WebGL now! That's very good news!

glyph_brush = "0.7"
log = "0.4"

Expand All @@ -21,5 +21,15 @@ features = ["derive"]

[dev-dependencies]
env_logger = "0.7"
winit = "0.24"
winit = { version = "0.24", features = ["web-sys"] }
futures = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
pollster = "0.2"
wgpu-subscriber = "0.1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does wgpu-subscriber do exactly? Does it replace env_logger?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and it's a pretty logger for web env, and more for Chrome as some code is dedicated to that browser.
In Chrome you can select log level, that wgpu-subscriber detect and print the right log level.


[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
console_error_panic_hook = "0.1.6"
console_log = "0.1.2"
web-sys = { version = "=0.3.46" }
wasm-bindgen-futures = "0.4.19"
39 changes: 33 additions & 6 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
use std::error::Error;
use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text};
use winit::{
event_loop::EventLoop,
window::Window,
};

fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

fn main() {
// Open window and create a surface
let event_loop = winit::event_loop::EventLoop::new();

let window = winit::window::WindowBuilder::new()
.with_resizable(false)
.with_title("Hello wgpu glyph !")
.build(&event_loop)
.unwrap();

#[cfg(not(target_arch = "wasm32"))]
{
wgpu_subscriber::initialize_default_subscriber(None);
// Temporarily avoid srgb formats for the swapchain on the web
pollster::block_on(run(event_loop, window));
}
#[cfg(target_arch = "wasm32")]
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init().expect("could not initialize logger");
use winit::platform::web::WindowExtWebSys;
// On wasm, append the canvas to the document body
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas()))
.ok()
})
.expect("couldn't append canvas to document body");
wasm_bindgen_futures::spawn_local(run(event_loop, window));
}
}

async fn run(event_loop: EventLoop<()>, window: Window) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have we introduced async at the top level here?

It looks like we are not awaiting anywhere. In fact, we are running block_on inside a Future which is forbidden by contract.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async is required for wasm_bindgen_futures::spawn_local, it's inspired from Wgpu-rs Hello triangle example.
So I have remove the futures::executor::block_on that's not necessary anymore.

let instance = wgpu::Instance::new(wgpu::BackendBit::all());
let surface = unsafe { instance.create_surface(&window) };

Expand All @@ -37,7 +64,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let local_spawner = local_pool.spawner();

// Prepare swap chain
let render_format = wgpu::TextureFormat::Bgra8UnormSrgb;
let render_format = wgpu::TextureFormat::Bgra8Unorm;
let mut size = window.inner_size();

let mut swap_chain = device.create_swap_chain(
Expand All @@ -54,7 +81,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Prepare glyph_brush
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"Inconsolata-Regular.ttf"
))?;
)).unwrap();

let mut glyph_brush = GlyphBrushBuilder::using_font(inconsolata)
.build(&device, render_format);
Expand Down