Skip to content

Commit ef5e96e

Browse files
committed
moved public dir to res and stencil wasm building
1 parent 1d27cc1 commit ef5e96e

20 files changed

Lines changed: 69 additions & 1181 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ pollster = "0.3"
3838
image = "0.24.2"
3939
log = "0.4"
4040
tobj = { version = "3.2", default-features = false, features = ["async"] }
41+
web-time = "1"

code/showcase/framework/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ winit.workspace = true
1818
thiserror = "1.0"
1919
bytemuck = { version = "1.24", features = [ "derive" ] }
2020
cgmath = "0.18"
21-
# wgpu-subscriber = "0.1"
2221
slotmap = "1.1.1"
2322
rand = "0.8"
2423

2524
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2625
async-fs = "2.2.0"
26+
env_logger = "0.10"
2727

2828
[target.'cfg(target_arch = "wasm32")'.dependencies]
2929
getrandom = { version = "*", features = ["js"] }

code/showcase/framework/src/lib.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
pub mod prelude;
2+
pub mod resources;
13
mod buffer;
24
mod camera;
35
mod light;
46
mod pipeline;
5-
pub mod prelude;
6-
pub mod resources;
77
mod shader_canvas;
88

99
pub use buffer::*;
@@ -16,12 +16,15 @@ pub use shader_canvas::*;
1616

1717
#[cfg(not(target_arch = "wasm32"))]
1818
use pollster::FutureExt;
19-
pub use rand;
2019

21-
// use cgmath::*;
2220
use std::ops::Deref;
21+
use std::path::Path;
22+
use std::path::PathBuf;
2323
use std::sync::Arc;
24-
use std::time::{Duration, Instant};
24+
25+
pub use rand;
26+
27+
use instant::{Duration, Instant};
2528
use wgpu::util::{BufferInitDescriptor, DeviceExt};
2629
use winit::application::ApplicationHandler;
2730
use winit::event_loop::EventLoopProxy;
@@ -89,8 +92,8 @@ impl Display {
8992
let config = wgpu::SurfaceConfiguration {
9093
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
9194
format: surface_format,
92-
width: size.width,
93-
height: size.height,
95+
width: size.width.max(1),
96+
height: size.height.max(1),
9497
present_mode: surface_caps.present_modes[0],
9598
alpha_mode: surface_caps.alpha_modes[0],
9699
view_formats: vec![],
@@ -259,7 +262,7 @@ impl UniformBinding {
259262
}
260263

261264
pub trait Demo: 'static + Sized + wgpu::WasmNotSend + std::fmt::Debug {
262-
fn init(display: &Display) -> impl std::future::Future<Output = anyhow::Result<Self>> + wgpu::WasmNotSend;
265+
fn init(display: &Display, path: &Path) -> impl std::future::Future<Output = anyhow::Result<Self>> + wgpu::WasmNotSend;
263266
fn resize(&mut self, display: &Display);
264267
fn update(&mut self, display: &Display, dt: Duration);
265268
fn render(&mut self, display: &mut Display);
@@ -312,15 +315,22 @@ impl<D: Demo + 'static> ApplicationHandler<anyhow::Result<(Display, D)>> for App
312315
if let Some(proxy) = self.proxy.take() {
313316
let window = window.clone();
314317
let setup_future = async move {
318+
#[cfg(not(target_arch = "wasm32"))]
319+
let res_dir = std::env::current_dir()?.join("res");
320+
#[cfg(target_arch = "wasm32")]
321+
let res_dir = PathBuf::new();
322+
315323
let display = Display::new(window).await?;
316-
let demo = D::init(&display).await?;
324+
let demo = D::init(&display, &res_dir).await?;
317325
anyhow::Ok((display, demo))
318326
};
319327

320328
#[cfg(target_arch = "wasm32")]
321329
wasm_bindgen_futures::spawn_local(async move {
330+
log::info!("1");
322331
let result = setup_future.await;
323332

333+
log::info!("2");
324334
proxy
325335
.send_event(result)
326336
.expect("Unable to send (display, demo)");
@@ -420,7 +430,18 @@ impl<D: Demo + 'static> ApplicationHandler<anyhow::Result<(Display, D)>> for App
420430
}
421431

422432
pub fn run<D: Demo>() -> anyhow::Result<()> {
423-
// wgpu_subscriber::initialize_default_subscriber(None);
433+
#[cfg(not(target_arch = "wasm32"))]
434+
{
435+
env_logger::init();
436+
}
437+
#[cfg(target_arch = "wasm32")]
438+
{
439+
use wgpu::web_sys::wasm_bindgen::UnwrapThrowExt;
440+
console_error_panic_hook::set_once();
441+
console_log::init_with_level(log::Level::Info).unwrap_throw();
442+
}
443+
444+
log::info!("run");
424445

425446
let event_loop = EventLoop::with_user_event().build()?;
426447
let mut app = App::<D>::new(&event_loop);

code/showcase/framework/src/resources/texture.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ impl Texture {
2020
path: P,
2121
is_normal_map: bool,
2222
) -> Result<Self> {
23-
// let path_copy = path.as_ref().to_path_buf();
2423
let img = image::open(&path)?;
2524
let label = path.as_ref().to_str().unwrap();
2625
Self::from_image(device, queue, &img, Some(label), is_normal_map)

code/showcase/stencil/Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ bytemuck.workspace = true
1111
glam.workspace = true
1212
wgpu.workspace = true
1313
winit.workspace = true
14-
log.workspace = true
14+
log.workspace = true
15+
web-time.workspace = true
16+
17+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
18+
env_logger = "0.10"
19+
20+
[target.'cfg(target_arch = "wasm32")'.dependencies]
21+
reqwest = { version = "0.11" }
22+
console_error_panic_hook = "0.1"
23+
console_log = "1.0"

code/showcase/stencil/src/main.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::f32;
22
use core::f32::consts::PI;
3+
use std::path::Path;
34

45
use framework::{Demo, MaterialBinder, ModelVertex, Vertex};
56
use glam::{Vec3, Vec4};
@@ -61,7 +62,8 @@ impl std::fmt::Debug for Stencil {
6162
}
6263

6364
impl Demo for Stencil {
64-
async fn init(display: &framework::Display) -> anyhow::Result<Self> {
65+
async fn init(display: &framework::Display, res_dir: &Path) -> anyhow::Result<Self> {
66+
log::info!("instances");
6567
let num_instances = 64;
6668
let half_instanes = num_instances / 2;
6769
let instances = (0..num_instances)
@@ -124,12 +126,13 @@ impl Demo for Stencil {
124126
}],
125127
});
126128

129+
log::info!("depth_stencil");
127130
let depth_stencil_format = wgpu::TextureFormat::Depth24PlusStencil8;
128131
let depth_stencil = display.device.create_texture(&wgpu::TextureDescriptor {
129132
label: Some("depth_stencil"),
130133
size: wgpu::Extent3d {
131-
width: display.width(),
132-
height: display.height(),
134+
width: display.config.width.max(1),
135+
height: display.config.height.max(1),
133136
depth_or_array_layers: 1,
134137
},
135138
mip_level_count: 1,
@@ -141,19 +144,17 @@ impl Demo for Stencil {
141144
});
142145
let depth_stencil_view = depth_stencil.create_view(&Default::default());
143146

144-
let res_dir = std::env::current_dir()?.join("res");
145-
146-
println!("Mask");
147-
let mask_texture = framework::Texture::load(
147+
log::info!("Mask");
148+
let mask_texture = framework::resources::load_texture(
149+
res_dir.join("textures/mask.png"),
150+
false,
148151
&display.device,
149152
&display.queue,
150-
res_dir.join("textures/mask.png"), // Fix for web
151-
false,
152-
)?;
153+
).await?;
153154

154155
let material_binder = MaterialBinder::new(&display.device);
155156

156-
println!("Model");
157+
log::info!("Model");
157158
let model = framework::resources::load_obj(
158159
res_dir.join("models/cube.obj"), // Fix for web
159160
&display.device,

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
base: '/learn-wgpu/',
33
title: 'Learn Wgpu',
44
theme: 'thindark',
5+
public: 'res',
56
plugins: {
67
'vuepress-plugin-code-copy': true,
78
'@vuepress/back-to-top': true,
-24.7 KB
Binary file not shown.
-117 KB
Binary file not shown.

0 commit comments

Comments
 (0)