Skip to content

Commit

Permalink
Merge pull request #10 from schell/swapchain_loading_#9
Browse files Browse the repository at this point in the history
Logging + swapchain extension fixes
  • Loading branch information
philpax authored Nov 8, 2022
2 parents 2cc10e2 + 9689f32 commit 7f3a034
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 16 deletions.
139 changes: 139 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ bytemuck = { version = "1.12.1", features = ["derive"] }
clap = { version = "4.0.8", features = ["derive"] }
futures = "0.3.24"
glam = "0.21.3"
openxr = {version = "0.17.0", features = ["linked", "static"], optional = true}
log = "^0.4"
openxr = {version = "0.17.0", features = ["loaded", "linked", "static"], optional = true}
tracing = "^0.1"
tracing-subscriber = { version = "^0.3", features = ["env-filter"] }
tracing-log = "0.1.3"
wgpu = "0.13.1"
wgpu-hal = {version = "0.13.1", optional = true}
winit = "0.27.3"
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod types;

pub mod wgsl;

use clap::{command, Parser};
use blit_state::BlitState;
use camera::CameraState;
use main_state::{Instance, MainState};
Expand All @@ -47,7 +48,9 @@ pub struct WgpuState {
}

fn main() -> anyhow::Result<()> {
use clap::{command, Parser};
tracing_subscriber::FmtSubscriber::builder()
.with_env_filter("wgpu=trace,wgpu_openxr-example=trace")
.init();

const MAIN_TRIANGLE_SCALE: f32 = 1.0;
const HAND_TRIANGLE_SCALE: f32 = 0.1;
Expand Down
53 changes: 39 additions & 14 deletions src/xr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{ffi::c_void, num::NonZeroU32};
use std::{
ffi::{c_void, CString},
num::NonZeroU32,
};

use anyhow::Context;
use ash::vk::{self, Handle};
Expand Down Expand Up @@ -56,28 +59,40 @@ impl XrState {
let entry = xr::Entry::linked();
let available_extensions = entry.enumerate_extensions()?;
assert!(available_extensions.khr_vulkan_enable2);
log::info!("available xr exts: {:#?}", available_extensions);

let mut enabled_extensions = xr::ExtensionSet::default();
enabled_extensions.khr_vulkan_enable2 = true;
#[cfg(target_os = "android")]
{
enabled_extensions.khr_android_create_instance = true;
}

let available_layers = entry.enumerate_layers()?;
log::info!("available xr layers: {:#?}", available_layers);

let xr_instance = entry.create_instance(
&xr::ApplicationInfo {
application_name: "wgpu-openxr-example",
application_version: 0,
engine_name: "wgpu-openxr-example",
engine_version: 0,
..Default::default()
},
&enabled_extensions,
&[],
&["XR_APILAYER_LUNARG_core_validation"],
)?;
let instance_props = xr_instance.properties()?;
println!(
"loaded OpenXR runtime: {} {}",
instance_props.runtime_name, instance_props.runtime_version
);
let xr_system_id = xr_instance.system(xr::FormFactor::HEAD_MOUNTED_DISPLAY)?;
let system_props = xr_instance.system_properties(xr_system_id).unwrap();
log::info!(
"loaded OpenXR runtime: {} {} {}",
instance_props.runtime_name,
instance_props.runtime_version,
if system_props.system_name.is_empty() {
"<unnamed>"
} else {
&system_props.system_name
}
);

let environment_blend_mode =
xr_instance.enumerate_environment_blend_modes(xr_system_id, VIEW_TYPE)?[0];
let vk_target_version = vk::make_api_version(0, 1, 1, 0);
Expand All @@ -95,14 +110,22 @@ impl XrState {

let vk_entry = unsafe { ash::Entry::load() }?;
let flags = wgpu_hal::InstanceFlags::empty();
let extensions = <V as Api>::Instance::required_extensions(&vk_entry, flags)?;
let mut extensions = <V as Api>::Instance::required_extensions(&vk_entry, flags)?;
extensions.push(ash::extensions::khr::Swapchain::name());
log::info!(
"creating vulkan instance with these extensions: {:#?}",
extensions
);

let vk_instance = unsafe {
let extensions_cchar: Vec<_> = extensions.iter().map(|s| s.as_ptr()).collect();

let app_name = CString::new("wgpu-openxr-example")?;
let vk_app_info = vk::ApplicationInfo::builder()
.application_version(0)
.engine_version(0)
.application_name(&app_name)
.application_version(1)
.engine_name(&app_name)
.engine_version(1)
.api_version(vk_target_version);

let vk_instance = xr_instance
Expand All @@ -123,6 +146,8 @@ impl XrState {
vk::Instance::from_raw(vk_instance as _),
)
};
log::info!("created vulkan instance");

let vk_instance_ptr = vk_instance.handle().as_raw() as *const c_void;

let vk_physical_device = vk::PhysicalDevice::from_raw(unsafe {
Expand Down Expand Up @@ -316,7 +341,7 @@ impl XrState {
SessionStateChanged(e) => {
// Session state change is where we can begin and end sessions, as well as
// find quit messages!
println!("entered state {:?}", e.state());
log::info!("entered state {:?}", e.state());
match e.state() {
xr::SessionState::READY => {
self.session.begin(VIEW_TYPE)?;
Expand All @@ -336,7 +361,7 @@ impl XrState {
return Ok(None);
}
EventsLost(e) => {
println!("lost {} events", e.lost_event_count());
log::warn!("lost {} events", e.lost_event_count());
}
_ => {}
}
Expand Down

0 comments on commit 7f3a034

Please sign in to comment.