Skip to content

Commit 964ff92

Browse files
committed
Window creation stuff
Signed-off-by: Hal Gentz <zegentzy@protonmail.com>
1 parent 8a39ce4 commit 964ff92

File tree

15 files changed

+967
-410
lines changed

15 files changed

+967
-410
lines changed

examples/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ path = "compute/main.rs"
2424
env_logger = "0.5"
2525
image = "0.18"
2626
log = "0.4"
27-
winit = "0.15"
27+
winit = { path = "../../winit"}
2828
glsl-to-spirv = "0.1.4"
2929
gfx-hal = { path = "../src/hal", version = "0.1" }
3030

examples/quad/main.rs

+51-84
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate image;
1919
extern crate winit;
2020

2121
use hal::{buffer, command, format as f, image as i, memory as m, pass, pso, pool, window::Extent2D};
22-
use hal::{Device, Instance, PhysicalDevice, Surface, Swapchain};
22+
use hal::{Device, PhysicalDevice, Surface, Swapchain};
2323
use hal::{
2424
DescriptorPool, FrameSync, Primitive,
2525
Backbuffer, SwapchainConfig,
@@ -33,6 +33,11 @@ use std::fs;
3333
use std::io::Cursor;
3434
use std::io::Read;
3535

36+
use std::sync::{Arc, Mutex};
37+
38+
#[cfg(feature = "gl")]
39+
use back::GlInstance;
40+
3641
const ENTRY_NAME: &str = "main";
3742

3843
#[derive(Debug, Clone, Copy)]
@@ -52,6 +57,8 @@ const QUAD: [Vertex; 6] = [
5257
Vertex { a_Pos: [ -0.5,-0.33 ], a_Uv: [0.0, 0.0] },
5358
];
5459

60+
const DIMS: Extent2D = Extent2D { width: 1024, height: 768 };
61+
5562
const COLOR_RANGE: i::SubresourceRange = i::SubresourceRange {
5663
aspects: f::Aspects::COLOR,
5764
levels: 0..1,
@@ -62,33 +69,24 @@ const COLOR_RANGE: i::SubresourceRange = i::SubresourceRange {
6269
fn main() {
6370
env_logger::init();
6471

65-
let mut events_loop = winit::EventsLoop::new();
72+
let events_loop = Arc::new(Mutex::new(winit::EventsLoop::new()));
6673

74+
// My understanding of the winit docs is that it will issue a window resize
75+
// event if the window's display's dpi ends up not equaling 1.
6776
let wb = winit::WindowBuilder::new()
68-
.with_dimensions(1024, 768)
77+
.with_dimensions(winit::dpi::LogicalSize::from_physical(winit::dpi::PhysicalSize {
78+
width: DIMS.width as _,
79+
height: DIMS.height as _,
80+
}, 1.0))
6981
.with_title("quad".to_string());
7082
// instantiate backend
71-
#[cfg(not(feature = "gl"))]
7283
let (window, _instance, mut adapters, mut surface) = {
73-
let window = wb.build(&events_loop).unwrap();
74-
let instance = back::Instance::create("gfx-rs quad", 1);
84+
let instance = back::Instance::create("gfx-rs quad", 1, Arc::clone(&events_loop));
85+
let window = instance.create_window(wb);
7586
let surface = instance.create_surface(&window);
7687
let adapters = instance.enumerate_adapters();
7788
(window, instance, adapters, surface)
7889
};
79-
#[cfg(feature = "gl")]
80-
let (mut adapters, mut surface) = {
81-
let window = {
82-
let builder =
83-
back::config_context(back::glutin::ContextBuilder::new(), ColorFormat::SELF, None)
84-
.with_vsync(true);
85-
back::glutin::GlWindow::new(wb, builder, &events_loop).unwrap()
86-
};
87-
88-
let surface = back::Surface::from_window(window);
89-
let adapters = surface.enumerate_adapters();
90-
(adapters, surface)
91-
};
9290

9391
for adapter in &adapters {
9492
println!("{:?}", adapter.info);
@@ -100,7 +98,7 @@ fn main() {
10098

10199
// Build a new device and associated command queues
102100
let (mut device, mut queue_group) = adapter
103-
.open_with::<_, hal::Graphics>(1, |family| surface.supports_queue_family(family))
101+
.open_with::<_, hal::Graphics>(1, |family| { println!("{:?}", family); surface.supports_queue_family(family) })
104102
.unwrap();
105103

106104
let mut command_pool =
@@ -335,43 +333,21 @@ fn main() {
335333
device.wait_for_fence(&frame_fence, !0);
336334
}
337335

338-
let mut swap_chain;
339-
let mut render_pass;
340-
let mut framebuffers;
341-
let mut frame_images;
342-
let mut pipeline;
343-
let mut pipeline_layout;
344-
let mut extent;
345-
346-
{
347-
#[cfg(not(feature = "gl"))]
348-
let window = &window;
349-
#[cfg(feature = "gl")]
350-
let window = &0;
351-
let (
352-
new_swap_chain,
353-
new_render_pass,
354-
new_framebuffers,
355-
new_frame_images,
356-
new_pipeline,
357-
new_pipeline_layout,
358-
new_extent,
359-
) = swapchain_stuff(
360-
&mut surface,
361-
&mut device,
362-
&adapter.physical_device,
363-
&set_layout,
364-
window,
365-
);
366-
367-
swap_chain = new_swap_chain;
368-
render_pass = new_render_pass;
369-
framebuffers = new_framebuffers;
370-
frame_images = new_frame_images;
371-
pipeline = new_pipeline;
372-
pipeline_layout = new_pipeline_layout;
373-
extent = new_extent;
374-
}
336+
let (
337+
mut swap_chain,
338+
mut render_pass,
339+
mut framebuffers,
340+
mut frame_images,
341+
mut pipeline,
342+
mut pipeline_layout,
343+
mut extent,
344+
) = swapchain_stuff(
345+
&mut surface,
346+
&mut device,
347+
&adapter.physical_device,
348+
&set_layout,
349+
&window,
350+
);
375351

376352
// Rendering setup
377353
let mut viewport = pso::Viewport {
@@ -387,9 +363,8 @@ fn main() {
387363
//
388364
let mut running = true;
389365
let mut recreate_swapchain = false;
390-
let mut first = true;
391366
while running {
392-
events_loop.poll_events(|event| {
367+
events_loop.lock().unwrap().poll_events(|event| {
393368
if let winit::Event::WindowEvent { event, .. } = event {
394369
match event {
395370
winit::WindowEvent::KeyboardInput {
@@ -401,16 +376,13 @@ fn main() {
401376
..
402377
}
403378
| winit::WindowEvent::CloseRequested => running = false,
404-
winit::WindowEvent::Resized(_width, _height) => {
405-
if !first {
406-
recreate_swapchain = true;
407-
}
379+
winit::WindowEvent::Resized(_dims) => {
380+
recreate_swapchain = true;
408381
}
409382
_ => (),
410383
}
411384
}
412385
});
413-
first = false;
414386

415387
if recreate_swapchain {
416388
device.wait_idle().unwrap();
@@ -429,10 +401,6 @@ fn main() {
429401
device.destroy_render_pass(render_pass);
430402
device.destroy_swapchain(swap_chain);
431403

432-
#[cfg(not(feature = "gl"))]
433-
let window = &window;
434-
#[cfg(feature = "gl")]
435-
let window = &0;
436404
let (
437405
new_swap_chain,
438406
new_render_pass,
@@ -446,7 +414,7 @@ fn main() {
446414
&mut device,
447415
&adapter.physical_device,
448416
&set_layout,
449-
window,
417+
&window,
450418
);
451419
swap_chain = new_swap_chain;
452420
render_pass = new_render_pass;
@@ -545,18 +513,13 @@ fn main() {
545513
println!("You need to enable the native API feature (vulkan/metal) in order to test the LL");
546514
}
547515

548-
#[cfg(any(feature = "vulkan", feature = "dx12", feature = "metal"))]
549-
type WindowType = winit::Window;
550-
#[cfg(feature = "gl")]
551-
type WindowType = u32;
552-
553516
#[cfg(any(feature = "vulkan", feature = "dx12", feature = "metal", feature = "gl"))]
554517
fn swapchain_stuff(
555518
surface: &mut <back::Backend as hal::Backend>::Surface,
556-
device: &mut back::Device,
519+
device: &mut <back::Backend as hal::Backend>::Device,
557520
physical_device: &back::PhysicalDevice,
558521
set_layout: &<back::Backend as hal::Backend>::DescriptorSetLayout,
559-
window: &WindowType,
522+
window: &Arc<Mutex<back::Window>>,
560523
) -> (
561524
<back::Backend as hal::Backend>::Swapchain,
562525
<back::Backend as hal::Backend>::RenderPass,
@@ -581,13 +544,18 @@ fn swapchain_stuff(
581544
let extent = match caps.current_extent {
582545
Some(e) => e,
583546
None => {
584-
#[cfg(feature = "gl")]
585-
let _window = window;
586-
#[cfg(feature = "gl")]
587-
let window = surface.get_window();
588-
589-
let window_size = window.get_inner_size().unwrap();
590-
let mut extent = hal::window::Extent2D { width: window_size.0, height: window_size.1 };
547+
let px = window
548+
.lock()
549+
.unwrap()
550+
.get_inner_size()
551+
.unwrap_or(winit::dpi::PhysicalSize {
552+
width: DIMS.width as _,
553+
height: DIMS.height as _,
554+
});
555+
let mut extent = hal::window::Extent2D {
556+
width: px.width as _,
557+
height: px.height as _,
558+
};
591559

592560
extent.width = extent.width.max(caps.extents.start.width).min(caps.extents.end.width);
593561
extent.height = extent.height.max(caps.extents.start.height).min(caps.extents.end.height);
@@ -664,7 +632,6 @@ fn swapchain_stuff(
664632
.collect();
665633
(pairs, fbos)
666634
}
667-
Backbuffer::Framebuffer(fbo) => (Vec::new(), vec![fbo]),
668635
};
669636

670637
let pipeline_layout =

src/backend/gl/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ log = "0.4"
2323
gfx_gl = "0.5"
2424
gfx-hal = { path = "../../hal", version = "0.1" }
2525
smallvec = "0.6"
26-
glutin = { version = "0.16", optional = true }
2726
spirv_cross = "0.9.2"
27+
glutin = { path = "../../../../glutin", optional = true }

src/backend/gl/src/command.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,13 @@ impl command::RawCommandBuffer<Backend> for RawCommandBuffer {
886886
for new_binding in &*desc_set.bindings.lock().unwrap() {
887887
match new_binding {
888888
n::DescSetBindings::Buffer {ty: btype, binding, buffer, offset, size} => {
889+
let btype = match btype {
890+
n::BindingTypes::UniformBuffers => gl::UNIFORM_BUFFER,
891+
n::BindingTypes::Images => panic!("Wrong desc set binding"),
892+
};
889893
for binding in drd.get_binding(n::BindingTypes::UniformBuffers, set, *binding).unwrap() {
890894
self.push_cmd(Command::BindBufferRange(
891-
gl::UNIFORM_BUFFER,
895+
btype,
892896
*binding,
893897
*buffer,
894898
*offset,

0 commit comments

Comments
 (0)