fix async
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1708,6 +1708,7 @@ dependencies = [
|
||||
"strafesnet_roblox_bot_player",
|
||||
"strafesnet_snf",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
@@ -16,6 +16,7 @@ pollster = "0.4.0"
|
||||
wasm-bindgen = "0.2.108"
|
||||
wgpu = "28.0.0"
|
||||
web-sys = { version = "0.3.85", features = ["HtmlCanvasElement"] }
|
||||
wasm-bindgen-futures = "0.4.58"
|
||||
|
||||
[package.metadata.wasm-pack.profile.release]
|
||||
wasm-opt = ["-Oz", "--enable-bulk-memory","--enable-nontrapping-float-to-int"]
|
||||
|
||||
@@ -5,6 +5,15 @@ use strafesnet_common::session::Time as SessionTime;
|
||||
|
||||
mod setup;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Setup{
|
||||
setup:setup::SetupContext<'static>,
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub async fn setup(canvas:web_sys::HtmlCanvasElement)->Setup{
|
||||
let setup=setup::setup_and_start(canvas).await;
|
||||
Setup{setup}
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
pub struct Graphics{
|
||||
graphics:graphics::Graphics,
|
||||
@@ -14,13 +23,12 @@ pub struct Graphics{
|
||||
impl Graphics{
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(
|
||||
canvas:web_sys::HtmlCanvasElement,
|
||||
setup:Setup,
|
||||
data:&[u8],
|
||||
)->Result<Self,JsValue>{
|
||||
let context=crate::setup::setup_and_start(canvas);
|
||||
Ok(Self{
|
||||
graphics:graphics::Graphics::new(data,context.device,context.queue,context.config).map_err(|e|JsValue::from_str(&e.to_string()))?,
|
||||
surface:context.surface,
|
||||
graphics:graphics::Graphics::new(data,setup.setup.device,setup.setup.queue,setup.setup.config).map_err(|e|JsValue::from_str(&e.to_string()))?,
|
||||
surface:setup.setup.surface,
|
||||
})
|
||||
}
|
||||
#[wasm_bindgen]
|
||||
|
||||
@@ -39,13 +39,13 @@ struct SetupContextPartial2<'a>{
|
||||
surface:wgpu::Surface<'a>,
|
||||
}
|
||||
impl<'a> SetupContextPartial2<'a>{
|
||||
fn pick_adapter(self)->SetupContextPartial3<'a>{
|
||||
async fn pick_adapter(self)->SetupContextPartial3<'a>{
|
||||
//TODO: prefer adapter that implements optional features
|
||||
//let optional_features=optional_features();
|
||||
let required_features=required_features();
|
||||
|
||||
//no helper function smh gotta write it myself
|
||||
let adapters=pollster::block_on(self.instance.enumerate_adapters(self.backends));
|
||||
let adapters=self.instance.enumerate_adapters(self.backends).await;
|
||||
|
||||
let chosen_adapter=adapters.into_iter()
|
||||
// reverse because we want to select adapters that appear first in ties,
|
||||
@@ -92,14 +92,14 @@ struct SetupContextPartial3<'a>{
|
||||
adapter:wgpu::Adapter,
|
||||
}
|
||||
impl<'a> SetupContextPartial3<'a>{
|
||||
fn request_device(self)->SetupContextPartial4<'a>{
|
||||
async fn request_device(self)->SetupContextPartial4<'a>{
|
||||
let optional_features=optional_features();
|
||||
let required_features=required_features();
|
||||
|
||||
// Make sure we use the texture resolution limits from the adapter, so we can support images the size of the surface.
|
||||
let needed_limits=strafesnet_graphics::graphics::required_limits().using_resolution(self.adapter.limits());
|
||||
|
||||
let (device, queue)=pollster::block_on(self.adapter
|
||||
let (device, queue)=self.adapter
|
||||
.request_device(
|
||||
&wgpu::DeviceDescriptor{
|
||||
label:None,
|
||||
@@ -109,7 +109,7 @@ impl<'a> SetupContextPartial3<'a>{
|
||||
trace:wgpu::Trace::Off,
|
||||
experimental_features:wgpu::ExperimentalFeatures::disabled(),
|
||||
},
|
||||
))
|
||||
).await
|
||||
.expect("Unable to find a suitable GPU adapter!");
|
||||
|
||||
SetupContextPartial4{
|
||||
@@ -151,7 +151,7 @@ pub struct SetupContext<'a>{
|
||||
pub config:wgpu::SurfaceConfiguration,
|
||||
}
|
||||
|
||||
pub fn setup_and_start(canvas:web_sys::HtmlCanvasElement)->SetupContext<'static>{
|
||||
pub async fn setup_and_start(canvas:web_sys::HtmlCanvasElement)->SetupContext<'static>{
|
||||
let size=(canvas.width(),canvas.height());
|
||||
|
||||
println!("Initializing the surface...");
|
||||
@@ -160,9 +160,9 @@ pub fn setup_and_start(canvas:web_sys::HtmlCanvasElement)->SetupContext<'static>
|
||||
|
||||
let partial_2=partial_1.create_surface(canvas).unwrap();
|
||||
|
||||
let partial_3=partial_2.pick_adapter();
|
||||
let partial_3=partial_2.pick_adapter().await;
|
||||
|
||||
let partial_4=partial_3.request_device();
|
||||
let partial_4=partial_3.request_device().await;
|
||||
|
||||
let setup_context=partial_4.configure_surface(size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user