Skip to content

Commit

Permalink
Fix UB in the vulkano example
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Dec 16, 2023
1 parent f2f1e29 commit e97ac16
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,17 @@ To use Vulkan, you need a Vulkan library for Rust. This example uses the
types for raw Vulkan object handles. The procedure to interface SDL2's Vulkan functions with these
will be different for each one.

First, make sure you enable the [`raw-window-handle`](#support-for-raw-window-handle) feature.

```rust
extern crate sdl2;
extern crate vulkano;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::sync::Arc;
use vulkano::instance::{Instance, InstanceCreateInfo, InstanceExtensions};
use vulkano::swapchain::{Surface, SurfaceApi};
use vulkano::{Handle, VulkanLibrary, VulkanObject};
use vulkano::swapchain::Surface;
use vulkano::VulkanLibrary;

fn main() {
let sdl_context = sdl2::init().unwrap();
Expand All @@ -614,26 +615,18 @@ fn main() {
let instance_extensions =
InstanceExtensions::from_iter(window.vulkan_instance_extensions().unwrap());

let instance = Instance::new(VulkanLibrary::new().unwrap(), {
let mut instance_info = InstanceCreateInfo::application_from_cargo_toml();
instance_info.enabled_extensions = instance_extensions;
instance_info
})
.unwrap();

let surface_handle = window
.vulkan_create_surface(instance.handle().as_raw() as _)
.unwrap();
let instance = Instance::new(
VulkanLibrary::new().unwrap(),
InstanceCreateInfo {
enabled_extensions: instance_extensions,
..Default::default()
},
)
.unwrap();

// SAFETY: Be sure not to drop the `window` before the `Surface` or vulkan `Swapchain`! (SIGSEGV otherwise)
let surface = unsafe {
Surface::from_handle(
Arc::clone(&instance),
<_ as Handle>::from_raw(surface_handle),
SurfaceApi::Xlib,
None,
)
};
// SAFETY: Be sure not to drop the `window` before the `Surface` or vulkan `Swapchain`!
// (SIGSEGV otherwise)
let surface = unsafe { Surface::from_window_ref(instance.clone(), &window) };

let mut event_pump = sdl_context.event_pump().unwrap();

Expand All @@ -653,8 +646,6 @@ fn main() {
::std::thread::sleep(::std::time::Duration::new(0, 1_000_000_000u32 / 60));
}
}


```

# Support for raw-window-handle
Expand Down

0 comments on commit e97ac16

Please sign in to comment.