Skip to content

Commit

Permalink
Async device initalization
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Nov 22, 2021
1 parent e1a8bef commit 9692635
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ rust-ini = "0.17"
serde = "1.0"
serde_derive = "1.0"
serde_scan = "0.4"
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "47fd776", features = [] }
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "47fd776", features = ["webgl"] }
# binaries
env_logger = "0.8"
getopts = "0.2"
Expand Down
29 changes: 14 additions & 15 deletions bin/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use vangers::{
render::{ScreenTargets, COLOR_FORMAT, DEPTH_FORMAT},
};

use futures::executor::{LocalPool, LocalSpawner};
use futures::executor::{LocalPool, LocalSpawner, block_on};
use log::info;
use winit::{
event,
Expand Down Expand Up @@ -53,8 +53,10 @@ pub struct HarnessOptions {

impl Harness {
pub fn init(options: HarnessOptions) -> (Self, config::Settings) {
let mut task_pool = LocalPool::new();
block_on(Harness::init_async(options))
}

pub async fn init_async(options: HarnessOptions) -> (Self, config::Settings) {
info!("Loading the settings");
let settings = config::Settings::load("config/settings.ron");
let extent = wgpu::Extent3d {
Expand All @@ -74,14 +76,12 @@ impl Harness {
.unwrap();
let surface = unsafe { instance.create_surface(&window) };

info!("Initializing the device");
let adapter = task_pool
.run_until(instance.request_adapter(&wgpu::RequestAdapterOptions {
info!("Initializing the device:adapter");
let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
}))
.expect("Unable to initialize GPU via the selected backend.");
}).await.expect("Unable to initialize GPU via the selected backend (adapter).");

let downlevel_caps = adapter.get_downlevel_properties();

Expand All @@ -95,12 +95,12 @@ impl Harness {
limits.max_texture_dimension_2d = 16384;
}

// #[cfg(target_arch = "wasm32")] {
// limits.max_texture_dimension_2d = 8192;
// }
#[cfg(target_arch = "wasm32")] {
limits.max_texture_dimension_2d = 8192;
}

let (device, queue) = task_pool
.run_until(adapter.request_device(
info!("Initializing the device:request");
let (device, queue) = adapter.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
Expand All @@ -111,8 +111,7 @@ impl Harness {
} else {
Some(std::path::Path::new(&settings.render.wgpu_trace_path))
},
))
.unwrap();
).await.expect("Unable to initialize GPU via the selected backend (request).");

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
Expand All @@ -135,7 +134,7 @@ impl Harness {
.create_view(&wgpu::TextureViewDescriptor::default());

let harness = Harness {
task_pool,
task_pool: LocalPool::new(),
event_loop,
window,
device,
Expand Down
6 changes: 3 additions & 3 deletions bin/road/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ fn main() {
create_fs();

async fn run() {
let (harness, settings) = boilerplate::Harness::init(boilerplate::HarnessOptions {
let (harness, settings) = boilerplate::Harness::init_async(boilerplate::HarnessOptions {
title: "road",
uses_level: true,
});
}).await;

let game = game::Game::new(
&settings,
Expand Down Expand Up @@ -347,4 +347,4 @@ fn create_fs() {
create_file("data/resource/m3d/mechous/u5.m3d", include_bytes!("../../res_linux/data/resource/m3d/mechous/u5.m3d"));
create_file("data/resource/m3d/mechous/m2.m3d", include_bytes!("../../res_linux/data/resource/m3d/mechous/m2.m3d"));
create_file("data/resource/m3d/mechous/u3.prm", include_bytes!("../../res_linux/data/resource/m3d/mechous/u3.prm"));
}
}
2 changes: 1 addition & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod body {
pub use shadow::FORMAT as SHADOW_FORMAT;

#[cfg(target_arch="wasm32")]
pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8Unorm;
pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;

#[cfg(not(target_arch="wasm32"))]
pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8Unorm;
Expand Down

0 comments on commit 9692635

Please sign in to comment.