Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ categories = ["gui", "game-development"]
edition = "2021"

[dependencies]
vulkano = "0.29.0"
vulkano-shaders = "0.29.0"
vulkano = "0.30.0"
vulkano-shaders = "0.30.0"
bytemuck = "1.8.0"
egui = "0.18.1"
thiserror = "1.0"

[dev-dependencies]
winit = "0.26.0"
vulkano-win = "0.29.0"
vulkano-win = "0.30.0"
egui-winit = "0.18.0"
egui_demo_lib = "0.18.0"
109 changes: 51 additions & 58 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use egui::plot::{HLine, Line, Plot, Value, Values};
use egui::{Color32, ColorImage, Ui};
use egui_vulkano::UpdateTexturesResult;
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess};
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SubpassContents};
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, RenderPassBeginInfo, SubpassContents};
use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
use vulkano::device::{Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo};
use vulkano::format::Format;
use vulkano::format::{ClearValue, Format};
use vulkano::image::view::ImageView;
use vulkano::image::{ImageAccess, ImageUsage, SwapchainImage};
use vulkano::instance::{Instance, InstanceCreateInfo};
use vulkano::instance::{Instance, InstanceCreateInfo, InstanceExtensions};
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
use vulkano::pipeline::graphics::vertex_input::BuffersDefinition;
use vulkano::pipeline::graphics::viewport::Viewport;
Expand All @@ -28,46 +28,48 @@ use vulkano::render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpa
use vulkano::swapchain::{AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError};
use vulkano::sync::{FenceSignalFuture, FlushError, GpuFuture};
use vulkano::{swapchain, sync};
use vulkano::instance::debug::{DebugUtilsMessenger, DebugUtilsMessengerCreateInfo};
use vulkano_win::VkSurfaceBuild;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Fullscreen, Window, WindowBuilder};

pub enum FrameEndFuture<F: GpuFuture + 'static> {
FenceSignalFuture(FenceSignalFuture<F>),
BoxedFuture(Box<dyn GpuFuture>),
}
pub struct FrameEndFuture(Box<dyn GpuFuture>);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is not used any more and could be removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed i forgot to remove it


impl<F: GpuFuture> FrameEndFuture<F> {
impl FrameEndFuture {
pub fn now(device: Arc<Device>) -> Self {
Self::BoxedFuture(sync::now(device).boxed())
Self(sync::now(device).boxed())
}

pub fn get(self) -> Box<dyn GpuFuture> {
match self {
FrameEndFuture::FenceSignalFuture(f) => f.boxed(),
FrameEndFuture::BoxedFuture(f) => f,
}
self.0
}
}

impl<F: GpuFuture> AsMut<dyn GpuFuture> for FrameEndFuture<F> {
impl AsMut<dyn GpuFuture> for FrameEndFuture {
fn as_mut(&mut self) -> &mut (dyn GpuFuture + 'static) {
match self {
FrameEndFuture::FenceSignalFuture(f) => f,
FrameEndFuture::BoxedFuture(f) => f,
}
self.0.as_mut()
}
}

fn main() {
let required_extensions = vulkano_win::required_extensions();
for layer in vulkano::instance::layers_list().unwrap() {
println!("Available layer: {}", layer.name());
}
let instance = Instance::new(InstanceCreateInfo {
enabled_extensions: required_extensions,
enabled_layers: vec!["VK_LAYER_KHRONOS_validation".into()],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not opposed to enabling this, but please check if it's available. I don't have it, so this just crashes with error LayerNotPresent for me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that can be removed, i forgot to remove it when pushing.

..Default::default()
})
.unwrap();

}).unwrap();
let _callback = unsafe {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks confusing, because it just looks like an unused variable. Apparently this is supposed to work as long as the variable remains in scope? If so, maybe add a comment explaining this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is my debug implementation i forgot to remove. it doesn't work properly anyway.

DebugUtilsMessenger::new(
instance.clone(),
DebugUtilsMessengerCreateInfo::user_callback(Arc::new(|msg| {
println!("Debug callback: {:?}", msg.description);
})),
).ok()
};
let physical = PhysicalDevice::enumerate(&instance).next().unwrap();

println!(
Expand Down Expand Up @@ -107,14 +109,12 @@ fn main() {
let (device, mut queues) = Device::new(
physical_device,
DeviceCreateInfo {
enabled_extensions: physical_device
.required_extensions()
.union(&device_extensions),
enabled_extensions: device_extensions,
queue_create_infos: vec![QueueCreateInfo::family(queue_family)],
..Default::default()
},
)
.unwrap();
.unwrap();

let queue = queues.next().unwrap();

Expand All @@ -140,7 +140,7 @@ fn main() {
..Default::default()
},
)
.unwrap()
.unwrap()
};

#[derive(Default, Debug, Clone, Copy, Pod, Zeroable)]
Expand All @@ -166,10 +166,10 @@ fn main() {
position: [0.25, -0.1],
},
]
.iter()
.cloned(),
.iter()
.cloned(),
)
.unwrap()
.unwrap()
};

mod vs {
Expand Down Expand Up @@ -220,7 +220,7 @@ fn main() {
{ color: [color], depth_stencil: {}, input: [] } // Create a second renderpass to draw egui
]
)
.unwrap();
.unwrap();

let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
Expand All @@ -242,7 +242,7 @@ fn main() {

let mut recreate_swapchain = false;

let mut previous_frame_end = Some(FrameEndFuture::now(device.clone()));
let mut previous_frame_end: Option<Box<dyn GpuFuture>> = Some(sync::now(device.clone()).boxed());

//Set up everything need to draw the gui
let window = surface.window();
Expand All @@ -254,7 +254,7 @@ fn main() {
queue.clone(),
Subpass::from(render_pass.clone(), 1).unwrap(),
)
.unwrap();
.unwrap();

//Set up some window to look at for the test

Expand Down Expand Up @@ -326,13 +326,13 @@ fn main() {
recreate_swapchain = true;
}

let clear_values = vec![[0.0, 0.0, 1.0, 1.0].into()];
let clear_values = vec![ClearValue::Float([0.0, 0.0, 1.0, 1.0]).into()];
let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
queue.family(),
CommandBufferUsage::OneTimeSubmit,
)
.unwrap();
.unwrap();

let frame_start = Instant::now();
egui_ctx.begin_frame(egui_winit.take_egui_input(surface.window()));
Expand Down Expand Up @@ -367,18 +367,18 @@ fn main() {
let platform_output = egui_output.platform_output;
egui_winit.handle_platform_output(surface.window(), &egui_ctx, platform_output);

let result = egui_painter
.update_textures(egui_output.textures_delta, &mut builder)
let tex_future = egui_painter
.update_textures(egui_output.textures_delta)
.expect("egui texture error");

let wait_for_last_frame = result == UpdateTexturesResult::Changed;

// Do your usual rendering
builder
.begin_render_pass(
framebuffers[image_num].clone(),
RenderPassBeginInfo {
clear_values,
..RenderPassBeginInfo::framebuffer(framebuffers[image_num].clone())
},
SubpassContents::Inline,
clear_values,
)
.unwrap()
.set_viewport(0, [viewport.clone()])
Expand All @@ -390,12 +390,11 @@ fn main() {
// Build your gui

// Automatically start the next render subpass and draw the gui
let size = surface.window().inner_size();
let sf: f32 = surface.window().scale_factor() as f32;
let size = surface.window().inner_size().to_logical(surface.window().scale_factor());
egui_painter
.draw(
&mut builder,
[(size.width as f32) / sf, (size.height as f32) / sf],
[size.width, size.height],
&egui_ctx,
egui_output.shapes,
)
Expand All @@ -407,34 +406,28 @@ fn main() {
builder.end_render_pass().unwrap();

let command_buffer = builder.build().unwrap();

if wait_for_last_frame {
if let Some(FrameEndFuture::FenceSignalFuture(ref mut f)) = previous_frame_end {
f.wait(None).unwrap();
}
let mut future_mut = acquire_future.join(tex_future).boxed();
if let Some(future) = previous_frame_end.take() {
future_mut = future_mut.join(future).boxed();
}

let future = previous_frame_end
.take()
.unwrap()
.get()
.join(acquire_future)
let future = future_mut
.then_execute(queue.clone(), command_buffer)
.unwrap()
.then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
.then_signal_fence_and_flush();

match future {
Ok(future) => {
previous_frame_end = Some(FrameEndFuture::FenceSignalFuture(future));
future.wait(None).unwrap();
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(FlushError::OutOfDate) => {
recreate_swapchain = true;
previous_frame_end = Some(FrameEndFuture::now(device.clone()));
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
previous_frame_end = Some(FrameEndFuture::now(device.clone()));
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
}
Expand Down Expand Up @@ -462,7 +455,7 @@ fn window_size_dependent_setup(
..Default::default()
},
)
.unwrap()
.unwrap()
})
.collect::<Vec<_>>()
}
Expand Down
Loading