From ed5499d025fa1b14c03aaeac527a7b3ad132f542 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 1 Feb 2019 10:00:17 -0500 Subject: [PATCH] Basic gfx-examples framework --- Cargo.lock | 10 ++++ Cargo.toml | 1 + gfx-examples/Cargo.toml | 25 ++++++++++ gfx-examples/README.md | 6 +++ gfx-examples/src/framework.rs | 89 +++++++++++++++++++++++++++++++++++ gfx-examples/src/triangle.rs | 22 +++++++++ 6 files changed, 153 insertions(+) create mode 100644 gfx-examples/Cargo.toml create mode 100644 gfx-examples/README.md create mode 100644 gfx-examples/src/framework.rs create mode 100644 gfx-examples/src/triangle.rs diff --git a/Cargo.lock b/Cargo.lock index 9b808d023..2e8361e23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -391,6 +391,16 @@ dependencies = [ "xcb 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "gfx-examples" +version = "0.1.0" +dependencies = [ + "env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "wgpu 0.1.0", + "wgpu-native 0.1.0", +] + [[package]] name = "gfx-hal" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1847568b1..dd741f94d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ members = [ "wgpu-bindings", "wgpu-rs", "examples", + "gfx-examples", ] diff --git a/gfx-examples/Cargo.toml b/gfx-examples/Cargo.toml new file mode 100644 index 000000000..e35bdc3ff --- /dev/null +++ b/gfx-examples/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "gfx-examples" +version = "0.1.0" +authors = [ + "Dzmitry Malyshau ", + "Joshua Groves ", +] +publish = false + +[[bin]] +name = "triangle" +path = "src/triangle.rs" + +[features] +default = [] +metal = ["wgpu-native/gfx-backend-metal"] +dx11 = ["wgpu-native/gfx-backend-dx11"] +dx12 = ["wgpu-native/gfx-backend-dx12"] +vulkan = ["wgpu-native/gfx-backend-vulkan"] + +[dependencies] +wgpu-native = { path = "../wgpu-native" } +wgpu = { path = "../wgpu-rs", features = ["winit"] } +env_logger = "0.5" +log = "0.4" diff --git a/gfx-examples/README.md b/gfx-examples/README.md new file mode 100644 index 000000000..6f4c307dd --- /dev/null +++ b/gfx-examples/README.md @@ -0,0 +1,6 @@ +# gfx pre-ll examples + +Original gfx-rs examples were growing for years, but then got abandoned once we changed the API to match vulkan: +https://github.com/gfx-rs/gfx/tree/pre-ll/examples + +This is the new home for them, considering `wgpu-rs` to be the spiritual successor of gfx pre-ll. diff --git a/gfx-examples/src/framework.rs b/gfx-examples/src/framework.rs new file mode 100644 index 000000000..24fe6b9bf --- /dev/null +++ b/gfx-examples/src/framework.rs @@ -0,0 +1,89 @@ +extern crate env_logger; +extern crate log; +extern crate wgpu_native; + +pub use self::wgpu_native::winit; + +use self::log::info; + + +pub const SWAP_CHAIN_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::B8g8r8a8Unorm; + +pub trait Example { + fn init(device: &wgpu::Device) -> Self; + fn update(&mut self, event: winit::WindowEvent); + fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &wgpu::Device); +} + +pub fn run() { + use self::wgpu_native::winit::{ + Event, ElementState, EventsLoop, KeyboardInput, Window, WindowEvent, VirtualKeyCode + }; + + info!("Initializing the device..."); + env_logger::init(); + let instance = wgpu::Instance::new(); + let adapter = instance.get_adapter(&wgpu::AdapterDescriptor { + power_preference: wgpu::PowerPreference::LowPower, + }); + let device = adapter.create_device(&wgpu::DeviceDescriptor { + extensions: wgpu::Extensions { + anisotropic_filtering: false, + }, + }); + + info!("Initializing the example..."); + let mut example = E::init(&device); + + info!("Initializing the window..."); + 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 { + usage: wgpu::TextureUsageFlags::OUTPUT_ATTACHMENT, + format: SWAP_CHAIN_FORMAT, + width: size.width as u32, + height: size.height as u32, + }); + + info!("Entering render loop..."); + let mut running = true; + while running { + events_loop.poll_events(|event| { + match event { + Event::WindowEvent { + event: WindowEvent::Resized(size), + .. + } => { + let physical = size.to_physical(window.get_hidpi_factor()); + info!("Resized to {:?}", physical); + } + Event::WindowEvent { event, .. } => match event { + WindowEvent::KeyboardInput { + input: KeyboardInput { + virtual_keycode: Some(VirtualKeyCode::Escape), + state: ElementState::Pressed, + .. + }, + .. + } | + WindowEvent::CloseRequested => { + running = false; + } + _ => { + example.update(event); + } + } + _ => () + } + }); + + let frame = swap_chain.get_next_texture(); + example.render(&frame, &device); + } +} diff --git a/gfx-examples/src/triangle.rs b/gfx-examples/src/triangle.rs new file mode 100644 index 000000000..6adf60c5f --- /dev/null +++ b/gfx-examples/src/triangle.rs @@ -0,0 +1,22 @@ +extern crate wgpu; + +#[path="framework.rs"] +mod fw; + +struct Triangle; + +impl fw::Example for Triangle { + fn init(device: &wgpu::Device) -> Self { + Triangle + } + + fn update(&mut self, _event: fw::winit::WindowEvent) { + } + + fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &wgpu::Device) { + } +} + +fn main() { + fw::run::(); +}