Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes that helped me awhile ago #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
25 changes: 16 additions & 9 deletions examples/demo_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use egui_winit_vulkano::{Gui, GuiConfig};
use egui_winit_vulkano::{Allocators, Gui, GuiConfig};
use vulkano_util::{
context::{VulkanoConfig, VulkanoContext},
window::{VulkanoWindows, WindowDescriptor},
Expand Down Expand Up @@ -35,20 +35,27 @@ pub fn main() {
windows.create_window(&event_loop, &context, &WindowDescriptor::default(), |ci| {
ci.image_format = Some(vulkano::format::Format::B8G8R8A8_UNORM)
});
let allocators = Allocators::new_default(context.device());
// Create gui as main render pass (no overlay means it clears the image each frame)
let mut gui1 = {
let renderer = windows.get_renderer_mut(window1).unwrap();
Gui::new(&event_loop, renderer.surface(), renderer.graphics_queue(), GuiConfig {
preferred_format: Some(renderer.swapchain_format()),
..Default::default()
})
Gui::new(
&event_loop,
renderer.surface(),
renderer.graphics_queue(),
allocators.clone(),
GuiConfig { preferred_format: Some(renderer.swapchain_format()), ..Default::default() },
)
};
let mut gui2 = {
let renderer = windows.get_renderer_mut(window2).unwrap();
Gui::new(&event_loop, renderer.surface(), renderer.graphics_queue(), GuiConfig {
preferred_format: Some(renderer.swapchain_format()),
..Default::default()
})
Gui::new(
&event_loop,
renderer.surface(),
renderer.graphics_queue(),
allocators.clone(),
GuiConfig { preferred_format: Some(renderer.swapchain_format()), ..Default::default() },
)
};
// Display the demo application that ships with egui.
let mut demo_app1 = egui_demo_lib::DemoWindows::default();
Expand Down
10 changes: 8 additions & 2 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![allow(clippy::eq_op)]

use egui::{ScrollArea, TextEdit, TextStyle};
use egui_winit_vulkano::{Gui, GuiConfig};
use egui_winit_vulkano::{Allocators, Gui, GuiConfig};
use vulkano_util::{
context::{VulkanoConfig, VulkanoContext},
window::{VulkanoWindows, WindowDescriptor},
Expand All @@ -37,7 +37,13 @@ pub fn main() {
// Create gui as main render pass (no overlay means it clears the image each frame)
let mut gui = {
let renderer = windows.get_primary_renderer_mut().unwrap();
Gui::new(&event_loop, renderer.surface(), renderer.graphics_queue(), GuiConfig::default())
Gui::new(
&event_loop,
renderer.surface(),
renderer.graphics_queue(),
Allocators::new_default(context.device()),
GuiConfig::default(),
)
};
// Create gui state (pass anything your state requires)
let mut code = CODE.to_owned();
Expand Down
26 changes: 7 additions & 19 deletions examples/multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use std::{convert::TryFrom, sync::Arc};

use bytemuck::{Pod, Zeroable};
use egui::{epaint::Shadow, style::Margin, vec2, Align, Align2, Color32, Frame, Rounding, Window};
use egui_winit_vulkano::{Gui, GuiConfig};
use egui_winit_vulkano::{Allocators, Gui, GuiConfig};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, RenderPassBeginInfo, SubpassContents,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
},
device::{Device, Queue},
format::Format,
Expand Down Expand Up @@ -75,6 +75,7 @@ pub fn main() {
&event_loop,
windows.get_primary_renderer_mut().unwrap().surface(),
windows.get_primary_renderer_mut().unwrap().graphics_queue(),
Allocators::new_default(context.device()),
pipeline.gui_pass(),
GuiConfig {
preferred_format: Some(vulkano::format::Format::B8G8R8A8_SRGB),
Expand Down Expand Up @@ -310,22 +311,12 @@ impl MSAAPipeline {
],
..RenderPassBeginInfo::framebuffer(framebuffer)
},
SubpassContents::SecondaryCommandBuffers,
SubpassContents::Inline,
)
.unwrap();

// Render first draw pass
let mut secondary_builder = AutoCommandBufferBuilder::secondary(
&self.command_buffer_allocator,
self.queue.queue_family_index(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
render_pass: Some(self.subpass.clone().into()),
..Default::default()
},
)
.unwrap();
secondary_builder
builder
.bind_pipeline_graphics(self.pipeline.clone())
.set_viewport(0, vec![Viewport {
origin: [0.0, 0.0],
Expand All @@ -335,12 +326,9 @@ impl MSAAPipeline {
.bind_vertex_buffers(0, self.vertex_buffer.clone())
.draw(self.vertex_buffer.len() as u32, 1, 0, 0)
.unwrap();
let cb = secondary_builder.build().unwrap();
builder.execute_commands(cb).unwrap();

// Draw gui on subpass
let cb = gui.draw_on_subpass_image(dimensions);
builder.execute_commands(cb).unwrap();
gui.draw_on_subpass_image(dimensions, &mut builder);

// Last end render pass
builder.end_render_pass().unwrap();
Expand Down
18 changes: 13 additions & 5 deletions examples/paint_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::sync::Arc;

use bytemuck::{Pod, Zeroable};
use egui::{mutex::Mutex, vec2, PaintCallback, PaintCallbackInfo, Rgba, Sense};
use egui_winit_vulkano::{CallbackContext, CallbackFn, Gui, GuiConfig, RenderResources};
use egui_winit_vulkano::{
Allocators, CallbackContext, CallbackFn, Gui, GuiConfig, RenderResources,
};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
pipeline::{
Expand Down Expand Up @@ -48,10 +50,16 @@ pub fn main() {
let (mut gui, scene) = {
let renderer = windows.get_primary_renderer_mut().unwrap();

let gui = Gui::new(&event_loop, renderer.surface(), renderer.graphics_queue(), GuiConfig {
preferred_format: Some(vulkano::format::Format::B8G8R8A8_SRGB),
..Default::default()
});
let gui = Gui::new(
&event_loop,
renderer.surface(),
renderer.graphics_queue(),
Allocators::new_default(context.device()),
GuiConfig {
preferred_format: Some(vulkano::format::Format::B8G8R8A8_SRGB),
..Default::default()
},
);

let scene = Arc::new(Mutex::new(Scene::new(gui.render_resources())));

Expand Down
31 changes: 26 additions & 5 deletions examples/subpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{convert::TryFrom, sync::Arc};

use bytemuck::{Pod, Zeroable};
use egui::{epaint::Shadow, style::Margin, vec2, Align, Align2, Color32, Frame, Rounding, Window};
use egui_winit_vulkano::{Gui, GuiConfig};
use egui_winit_vulkano::{Allocators, Gui, GuiConfig};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
Expand All @@ -23,6 +23,7 @@ use vulkano::{
device::{Device, Queue},
format::Format,
image::{ImageAccess, SampleCount},
instance::InstanceCreateInfo,
memory::allocator::StandardMemoryAllocator,
pipeline::{
graphics::{
Expand Down Expand Up @@ -52,7 +53,27 @@ pub fn main() {
// Winit event loop
let event_loop = EventLoop::new();
// Vulkano context
let context = VulkanoContext::new(VulkanoConfig::default());
let context = VulkanoContext::new(VulkanoConfig {
instance_create_info: InstanceCreateInfo {
application_version: vulkano::Version::V1_3,
enabled_extensions: vulkano::instance::InstanceExtensions {
ext_debug_utils: true,
..vulkano::instance::InstanceExtensions::empty()
},
..Default::default()
},
debug_create_info: Some(
vulkano::instance::debug::DebugUtilsMessengerCreateInfo::user_callback(Arc::new(
|msg| {
println!(
"{:?} {:?} {:?}: {}",
msg.layer_prefix, msg.severity, msg.ty, msg.description
)
},
)),
),
..VulkanoConfig::default()
});
// Vulkano windows (create one)
let mut windows = VulkanoWindows::default();
windows.create_window(&event_loop, &context, &WindowDescriptor::default(), |ci| {
Expand All @@ -69,6 +90,7 @@ pub fn main() {
&event_loop,
windows.get_primary_renderer_mut().unwrap().surface(),
windows.get_primary_renderer_mut().unwrap().graphics_queue(),
Allocators::new_default(context.device()),
gui_pipeline.gui_pass(),
GuiConfig {
preferred_format: Some(vulkano::format::Format::B8G8R8A8_SRGB),
Expand Down Expand Up @@ -284,10 +306,9 @@ impl SimpleGuiPipeline {
builder.execute_commands(cb).unwrap();

// Move on to next subpass for gui
builder.next_subpass(SubpassContents::SecondaryCommandBuffers).unwrap();
builder.next_subpass(SubpassContents::Inline).unwrap();
// Draw gui on subpass
let cb = gui.draw_on_subpass_image(dimensions);
builder.execute_commands(cb).unwrap();
gui.draw_on_subpass_image(dimensions, &mut builder);

// Last end render pass
builder.end_render_pass().unwrap();
Expand Down
10 changes: 8 additions & 2 deletions examples/wholesome/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use std::sync::Arc;

use egui::{Context, Visuals};
use egui_winit_vulkano::{Gui, GuiConfig};
use egui_winit_vulkano::{Allocators, Gui, GuiConfig};
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
format::Format,
Expand Down Expand Up @@ -130,7 +130,13 @@ pub fn main() {
// Create gui as main render pass (no overlay means it clears the image each frame)
let mut gui = {
let renderer = windows.get_primary_renderer_mut().unwrap();
Gui::new(&event_loop, renderer.surface(), renderer.graphics_queue(), GuiConfig::default())
Gui::new(
&event_loop,
renderer.surface(),
renderer.graphics_queue(),
Allocators::new_default(context.device()),
GuiConfig::default(),
)
};
// Create a simple image to which we'll draw the triangle scene
let scene_image = StorageImage::general_purpose_image_view(
Expand Down
24 changes: 17 additions & 7 deletions src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Arc;
use egui::{ClippedPrimitive, TexturesDelta};
use egui_winit::winit::event_loop::EventLoopWindowTarget;
use vulkano::{
command_buffer::SecondaryAutoCommandBuffer,
command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer},
device::Queue,
format::{Format, NumericType},
image::{ImageViewAbstract, SampleCount},
Expand All @@ -24,6 +24,7 @@ use winit::window::Window;
use crate::{
renderer::{RenderResources, Renderer},
utils::{immutable_texture_from_bytes, immutable_texture_from_file},
Allocators,
};

fn get_surface_image_format(
Expand Down Expand Up @@ -82,14 +83,20 @@ impl Gui {
event_loop: &EventLoopWindowTarget<T>,
surface: Arc<Surface>,
gfx_queue: Arc<Queue>,
allocators: Allocators,
config: GuiConfig,
) -> Gui {
// Pick preferred format if provided, otherwise use the default one
let format = get_surface_image_format(&surface, config.preferred_format, &gfx_queue);
let max_texture_side =
gfx_queue.device().physical_device().properties().max_image_array_layers as usize;
let renderer =
Renderer::new_with_render_pass(gfx_queue, format, config.is_overlay, config.samples);
let renderer = Renderer::new_with_render_pass(
gfx_queue,
allocators,
format,
config.is_overlay,
config.samples,
);
let mut egui_winit = egui_winit::State::new(event_loop);
egui_winit.set_max_texture_side(max_texture_side);
egui_winit.set_pixels_per_point(surface_window(&surface).scale_factor() as f32);
Expand All @@ -108,14 +115,15 @@ impl Gui {
event_loop: &EventLoopWindowTarget<T>,
surface: Arc<Surface>,
gfx_queue: Arc<Queue>,
allocators: Allocators,
subpass: Subpass,
config: GuiConfig,
) -> Gui {
// Pick preferred format if provided, otherwise use the default one
let format = get_surface_image_format(&surface, config.preferred_format, &gfx_queue);
let max_texture_side =
gfx_queue.device().physical_device().properties().max_image_array_layers as usize;
let renderer = Renderer::new_with_subpass(gfx_queue, format, subpass);
let renderer = Renderer::new_with_subpass(gfx_queue, allocators, format, subpass);
let mut egui_winit = egui_winit::State::new(event_loop);
egui_winit.set_max_texture_side(max_texture_side);
egui_winit.set_pixels_per_point(surface_window(&surface).scale_factor() as f32);
Expand Down Expand Up @@ -193,11 +201,12 @@ impl Gui {

/// Creates commands for rendering ui on subpass' image and returns the command buffer for execution on your side
/// - Finishes Egui frame
/// - You must execute the secondary command buffer yourself
/// - You must execute the command buffer yourself
pub fn draw_on_subpass_image(
&mut self,
image_dimensions: [u32; 2],
) -> SecondaryAutoCommandBuffer {
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
) {
if self.renderer.has_renderpass() {
panic!(
"Gui integration has been created with its own render pass, use `draw_on_image` \
Expand All @@ -212,7 +221,8 @@ impl Gui {
&textures_delta,
self.egui_winit.pixels_per_point(),
image_dimensions,
)
builder,
);
}

fn extract_draw_data_at_frame_end(&mut self) -> (Vec<ClippedPrimitive>, TexturesDelta) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ mod utils;
pub use egui;
pub use integration::*;
pub use renderer::{CallbackContext, CallbackFn, RenderResources};
pub use utils::{immutable_texture_from_bytes, immutable_texture_from_file};
pub use utils::{immutable_texture_from_bytes, immutable_texture_from_file, Allocators};
Loading