[rs] Merge #10

10: Update API for new wgpu gl backend support r=kvark a=kyren

This won't work until [this pr](https://github.com/gfx-rs/wgpu/pull/183) is merged in wgpu, but at least this way we can discuss it.

Co-authored-by: kyren <kerriganw@gmail.com>
This commit is contained in:
bors[bot]
2019-06-08 02:37:20 +00:00
4 changed files with 113 additions and 33 deletions

View File

@@ -20,6 +20,7 @@ metal = ["wgn/gfx-backend-metal"]
dx11 = ["wgn/gfx-backend-dx11"]
dx12 = ["wgn/gfx-backend-dx12"]
vulkan = ["wgn/gfx-backend-vulkan"]
gl = ["wgn/gfx-backend-gl"]
[dependencies]
#TODO: only depend on the published version

View File

@@ -44,16 +44,58 @@ pub fn run<E: Example>(title: &str) {
EventsLoop,
KeyboardInput,
VirtualKeyCode,
Window,
WindowEvent,
};
info!("Initializing the device...");
env_logger::init();
let instance = wgpu::Instance::new();
let mut events_loop = EventsLoop::new();
info!("Initializing the window...");
#[cfg(not(feature = "gl"))]
let (_window, instance, hidpi_factor, size, surface) = {
use wgpu::winit::Window;
let instance = wgpu::Instance::new();
let window = Window::new(&events_loop).unwrap();
window.set_title(title);
let hidpi_factor = window.get_hidpi_factor();
let size = window
.get_inner_size()
.unwrap()
.to_physical(hidpi_factor);
let surface = instance.create_surface(&window);
(window, instance, hidpi_factor, size, surface)
};
#[cfg(feature = "gl")]
let (instance, hidpi_factor, size, surface) = {
let wb = wgpu::winit::WindowBuilder::new();
let cb = wgpu::glutin::ContextBuilder::new().with_vsync(true);
let context = wgpu::glutin::WindowedContext::new_windowed(wb, cb, &events_loop).unwrap();
context.window().set_title(title);
let hidpi_factor = context.window().get_hidpi_factor();
let size = context
.window()
.get_inner_size()
.unwrap()
.to_physical(hidpi_factor);
let instance = wgpu::Instance::new(context);
let surface = instance.get_surface();
(instance, hidpi_factor, size, surface)
};
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
power_preference: wgpu::PowerPreference::LowPower,
});
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
@@ -61,16 +103,6 @@ pub fn run<E: Example>(title: &str) {
limits: wgpu::Limits::default(),
});
info!("Initializing the window...");
let mut events_loop = EventsLoop::new();
let window = Window::new(&events_loop).unwrap();
window.set_title(title);
let size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
let surface = instance.create_surface(&window);
let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8Unorm,
@@ -90,7 +122,7 @@ pub fn run<E: Example>(title: &str) {
event: WindowEvent::Resized(size),
..
} => {
let physical = size.to_physical(window.get_hidpi_factor());
let physical = size.to_physical(hidpi_factor);
info!("Resizing to {:?}", physical);
sc_desc.width = physical.width.round() as u32;
sc_desc.height = physical.height.round() as u32;

View File

@@ -1,10 +1,56 @@
fn main() {
use wgpu::winit::{
ElementState,
Event,
EventsLoop,
KeyboardInput,
VirtualKeyCode,
WindowEvent,
};
env_logger::init();
let instance = wgpu::Instance::new();
let mut events_loop = EventsLoop::new();
#[cfg(not(feature = "gl"))]
let (_window, instance, size, surface) = {
use wgpu::winit::Window;
let instance = wgpu::Instance::new();
let window = Window::new(&events_loop).unwrap();
let size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
let surface = instance.create_surface(&window);
(window, instance, size, surface)
};
#[cfg(feature = "gl")]
let (instance, size, surface) = {
let wb = wgpu::winit::WindowBuilder::new();
let cb = wgpu::glutin::ContextBuilder::new().with_vsync(true);
let context = wgpu::glutin::WindowedContext::new_windowed(wb, cb, &events_loop).unwrap();
let size = context
.window()
.get_inner_size()
.unwrap()
.to_physical(context.window().get_hidpi_factor());
let instance = wgpu::Instance::new(context);
let surface = instance.get_surface();
(instance, size, surface)
};
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
power_preference: wgpu::PowerPreference::LowPower,
});
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
@@ -57,24 +103,6 @@ fn main() {
sample_count: 1,
});
use wgpu::winit::{
ElementState,
Event,
EventsLoop,
KeyboardInput,
VirtualKeyCode,
Window,
WindowEvent,
};
let mut events_loop = EventsLoop::new();
let window = Window::new(&events_loop).unwrap();
let size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
let surface = instance.create_surface(&window);
let mut swap_chain = device.create_swap_chain(
&surface,
&wgpu::SwapChainDescriptor {

View File

@@ -60,6 +60,9 @@ pub use wgn::{
VertexFormat,
};
#[cfg(feature = "gl")]
pub use wgn::glutin;
//TODO: avoid heap allocating vectors during resource creation.
#[derive(Default)]
struct Temp {
@@ -455,12 +458,20 @@ where
impl Instance {
/// Create a new `Instance` object.
#[cfg(not(feature = "gl"))]
pub fn new() -> Self {
Instance {
id: wgn::wgpu_create_instance(),
}
}
#[cfg(feature = "gl")]
pub fn new(windowed_context: wgn::glutin::WindowedContext) -> Self {
Instance {
id: wgn::wgpu_create_gl_instance(windowed_context)
}
}
/// Retrieves an [`Adapter`] which matches the given descriptor.
///
/// If there are no available adapters matching `desc`, this function will return another
@@ -477,12 +488,20 @@ impl Instance {
}
/// Creates a surface from a window.
#[cfg(not(feature = "gl"))]
pub fn create_surface(&self, window: &winit::Window) -> Surface {
Surface {
id: wgn::wgpu_instance_create_surface_from_winit(self.id, window),
}
}
#[cfg(feature = "gl")]
pub fn get_surface(&self) -> Surface {
Surface {
id: wgn::wgpu_instance_get_gl_surface(self.id),
}
}
#[cfg(feature = "metal")]
pub fn create_surface_with_metal_layer(&self, window: *mut std::ffi::c_void) -> Surface {
Surface {