Skip to content

Commit

Permalink
replace builder structs with high-level enums/structs
Browse files Browse the repository at this point in the history
Polymorphic structs get replaced with an enum. There's also a union for each enum that combines all the raw structs from the `sys` crate.
  • Loading branch information
Timbals committed Jul 26, 2024
1 parent 56dc179 commit 9af2aba
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 1,256 deletions.
328 changes: 130 additions & 198 deletions generator/src/main.rs

Large diffs are not rendered by default.

46 changes: 24 additions & 22 deletions openxr/examples/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,28 +710,30 @@ pub fn main() {
.end(
xr_frame_state.predicted_display_time,
environment_blend_mode,
&[
&xr::CompositionLayerProjection::new().space(&stage).views(&[
xr::CompositionLayerProjectionView::new()
.pose(views[0].pose)
.fov(views[0].fov)
.sub_image(
xr::SwapchainSubImage::new()
.swapchain(&swapchain.handle)
.image_array_index(0)
.image_rect(rect),
),
xr::CompositionLayerProjectionView::new()
.pose(views[1].pose)
.fov(views[1].fov)
.sub_image(
xr::SwapchainSubImage::new()
.swapchain(&swapchain.handle)
.image_array_index(1)
.image_rect(rect),
),
]),
],
&[&xr::CompositionLayer::Projection {
layer_flags: Default::default(),
space: &stage,
views: &[
xr::CompositionLayerProjectionView {
pose: views[0].pose,
fov: views[0].fov,
sub_image: xr::SwapchainSubImage {
swapchain: &swapchain.handle,
image_array_index: 0,
image_rect: rect,
},
},
xr::CompositionLayerProjectionView {
pose: views[1].pose,
fov: views[1].fov,
sub_image: xr::SwapchainSubImage {
swapchain: &swapchain.handle,
image_array_index: 1,
image_rect: rect,
},
},
],
}],
)
.unwrap();
frame = (frame + 1) % PIPELINE_DEPTH as usize;
Expand Down
30 changes: 13 additions & 17 deletions openxr/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ impl Action<Haptic> {
&self,
session: &Session<G>,
subaction_path: Path,
event: &HapticBase,
data: HapticData,
) -> Result<()> {
self.assert_event_validity(event);
self.assert_event_validity(&data);

let info = sys::HapticActionInfo {
ty: sys::HapticActionInfo::TYPE,
Expand All @@ -140,30 +140,26 @@ impl Action<Haptic> {
cvt((self.fp().apply_haptic_feedback)(
session.as_raw(),
&info,
event as *const _ as _,
// TODO `samples_consumed` pointer is not set
data.as_raw().as_base(),
))?;
}
Ok(())
}

/// Check the invariants of the passed haptic `event`.
/// The lifetime guarantees the validity of the non-null pointers.
fn assert_event_validity(&self, event: &HapticBase) {
match event.as_raw().ty {
sys::HapticVibration::TYPE => {
fn assert_event_validity(&self, data: &HapticData) {
match data {
HapticData::Vibration { .. } => {
// nothing to check
}
sys::HapticPcmVibrationFB::TYPE => {
assert!(self.instance().exts().fb_haptic_pcm.is_some());
let event =
unsafe { std::mem::transmute::<&HapticBase, &HapticPcmVibrationFB>(event) }
.as_raw();
assert!(event.buffer_size > 0);
assert_ne!(event.buffer, ptr::null());
assert_ne!(event.samples_consumed, ptr::null_mut());
}
ty => {
panic!("unsupported haptic type: {:?}", ty)
HapticData::PcmVibrationFB { buffer, .. } => {
assert!(
self.instance().exts().fb_haptic_pcm.is_some(),
"XR_FB_haptic_pcm not loaded"
);
assert!(!buffer.is_empty(), "PCM Vibration buffer can't be empty");
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions openxr/src/action_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ impl ActionSet {
localized_name: &str,
subaction_paths: &[Path],
) -> Result<Action<T>> {
let info = builder::ActionCreateInfo::new()
.action_name(name)
.localized_action_name(localized_name)
.subaction_paths(subaction_paths)
.action_type(T::TYPE);
let info = builder::ActionCreateInfo {
action_name: name,
localized_action_name: localized_name,
subaction_paths,
action_type: T::TYPE,
};
unsafe {
let mut out = sys::Action::NULL;
cvt((self.fp().create_action)(
self.as_raw(),
info.as_raw(),
&info.as_raw(),
&mut out,
))?;
Ok(Action::from_raw(self.clone(), out))
Expand Down
Loading

0 comments on commit 9af2aba

Please sign in to comment.