|
1 | 1 | use std::error::Error; |
2 | 2 |
|
3 | 3 | fn main() -> Result<(), Box<dyn Error>> { |
4 | | - #[cfg(not(feature = "winit"))] |
| 4 | + #[cfg(any(target_arch = "wasm32", not(feature = "winit")))] |
5 | 5 | return Err("must be compiled with winit feature to run".into()); |
6 | 6 |
|
7 | | - #[cfg(feature = "winit")] |
| 7 | + #[cfg(all(not(target_arch = "wasm32"), feature = "winit"))] |
8 | 8 | return winit::main(); |
9 | 9 | } |
10 | 10 |
|
11 | | -#[cfg(feature = "winit")] |
| 11 | +#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))] |
12 | 12 | mod winit { |
13 | | - use serde::{Deserialize, Serialize}; |
14 | 13 | use std::error::Error; |
15 | 14 | use wgputoy::context::init_wgpu; |
| 15 | + use wgputoy::shader::{FolderLoader, WebLoader, load_shader}; |
16 | 16 | use wgputoy::WgpuToyRenderer; |
17 | 17 |
|
18 | | - #[derive(Serialize, Deserialize, Debug)] |
19 | | - #[serde(rename_all = "camelCase")] |
20 | | - struct ShaderMeta { |
21 | | - uniforms: Vec<Uniform>, |
22 | | - textures: Vec<Texture>, |
23 | | - #[serde(default)] |
24 | | - float32_enabled: bool, |
25 | | - } |
26 | | - |
27 | | - #[derive(Serialize, Deserialize, Debug)] |
28 | | - struct Uniform { |
29 | | - name: String, |
30 | | - value: f32, |
31 | | - } |
32 | | - |
33 | | - #[derive(Serialize, Deserialize, Debug)] |
34 | | - struct Texture { |
35 | | - img: String, |
36 | | - } |
37 | | - |
38 | 18 | async fn init() -> Result<WgpuToyRenderer, Box<dyn Error>> { |
39 | | - let wgpu = init_wgpu(1280, 720, "").await?; |
40 | | - let mut wgputoy = WgpuToyRenderer::new(wgpu); |
41 | | - |
42 | | - let filename = if std::env::args().len() > 1 { |
| 19 | + let name = if std::env::args().len() > 1 { |
43 | 20 | std::env::args().nth(1).unwrap() |
44 | 21 | } else { |
45 | | - "examples/default.wgsl".to_string() |
| 22 | + "default".to_string() |
46 | 23 | }; |
47 | | - let shader = std::fs::read_to_string(&filename)?; |
48 | | - |
49 | | - let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new()) |
50 | | - .with(reqwest_middleware_cache::Cache { |
51 | | - mode: reqwest_middleware_cache::CacheMode::Default, |
52 | | - cache_manager: reqwest_middleware_cache::managers::CACacheManager::default(), |
53 | | - }) |
54 | | - .build(); |
55 | 24 |
|
56 | | - if let Ok(json) = std::fs::read_to_string(std::format!("{filename}.json")) { |
57 | | - let metadata: ShaderMeta = serde_json::from_str(&json)?; |
58 | | - println!("{:?}", metadata); |
| 25 | + let source_loader = FolderLoader::new("./examples".to_string()); |
| 26 | + let texture_loader = WebLoader::new(); |
59 | 27 |
|
60 | | - for (i, texture) in metadata.textures.iter().enumerate() { |
61 | | - let url = if texture.img.starts_with("http") { |
62 | | - texture.img.clone() |
63 | | - } else { |
64 | | - std::format!("https://compute.toys/{}", texture.img) |
65 | | - }; |
66 | | - let resp = client.get(&url).send().await?; |
67 | | - let img = resp.bytes().await?.to_vec(); |
68 | | - if texture.img.ends_with(".hdr") { |
69 | | - wgputoy.load_channel_hdr(i, &img)?; |
70 | | - } else { |
71 | | - wgputoy.load_channel(i, &img); |
72 | | - } |
73 | | - } |
| 28 | + let shader = load_shader(&source_loader, &texture_loader, &name)?; |
74 | 29 |
|
75 | | - let uniform_names: Vec<String> = |
76 | | - metadata.uniforms.iter().map(|u| u.name.clone()).collect(); |
77 | | - let uniform_values: Vec<f32> = metadata.uniforms.iter().map(|u| u.value).collect(); |
78 | | - if !uniform_names.is_empty() { |
79 | | - wgputoy.set_custom_floats(uniform_names, uniform_values); |
80 | | - } |
| 30 | + let wgpu = init_wgpu(1280, 720, "").await?; |
| 31 | + let mut wgputoy = WgpuToyRenderer::new(wgpu); |
81 | 32 |
|
82 | | - wgputoy.set_pass_f32(metadata.float32_enabled); |
83 | | - } |
| 33 | + wgputoy.load_shader(shader).await?; |
84 | 34 |
|
85 | | - if let Some(source) = wgputoy.preprocess_async(&shader).await { |
86 | | - println!("{}", source.source); |
87 | | - wgputoy.compile(source); |
88 | | - } |
89 | 35 | Ok(wgputoy) |
90 | 36 | } |
91 | 37 |
|
@@ -139,7 +85,5 @@ mod winit { |
139 | 85 | _ => (), |
140 | 86 | } |
141 | 87 | }); |
142 | | - |
143 | | - Ok(()) |
144 | 88 | } |
145 | 89 | } |
0 commit comments