Basic gfx-examples framework

This commit is contained in:
Dzmitry Malyshau 2019-02-01 10:00:17 -05:00
parent afe00aa90f
commit ed5499d025
6 changed files with 153 additions and 0 deletions

10
Cargo.lock generated
View File

@ -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"

View File

@ -4,4 +4,5 @@ members = [
"wgpu-bindings",
"wgpu-rs",
"examples",
"gfx-examples",
]

25
gfx-examples/Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "gfx-examples"
version = "0.1.0"
authors = [
"Dzmitry Malyshau <kvark@mozilla.com>",
"Joshua Groves <josh@joshgroves.com>",
]
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"

6
gfx-examples/README.md Normal file
View File

@ -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.

View File

@ -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<E: Example>() {
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);
}
}

View File

@ -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::<Triangle>();
}