Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.

Wholesome update for wgpu-0.3 #70

Merged
merged 1 commit into from
Aug 22, 2019
Merged
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ branches:
- staging.tmp

script:
- cargo test
- cargo check
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then cargo test --no-run --features vulkan; fi
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then cargo test --no-run --features vulkan; fi
- if [[ $TRAVIS_OS_NAME == "osx" ]]; then cargo test --no-run --features metal; fi
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then cargo test --no-run --features dx12; fi
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ version = "0.3.0"
authors = [
"Dzmitry Malyshau <[email protected]>",
"Joshua Groves <[email protected]>",
"Lucas Kent <[email protected]>",
"kyren <[email protected]>",
"Cormac O'Brien <[email protected]>",
]
edition = "2018"
description = "Rusty wgpu API wrapper"
description = "Rusty WebGPU API wrapper"
homepage = "https://github.com/gfx-rs/wgpu-rs"
repository = "https://github.com/gfx-rs/wgpu-rs"
keywords = ["graphics"]
Expand All @@ -20,12 +23,13 @@ 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"]
#TODO: there is no way to use glutin-0.20 with GL backend v0.3 yet
#gl = ["wgn/gfx-backend-gl"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue for the part that gl is blocked on here? It would probably be useful to clarify it in a comment for anyone using gl patches at the moment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue is that we initialize all other backends without winit feature
for GL, there is currently no way to use it without glutin. Enabling glutin though on the backend would conflict with glutin in wgpu-rs itself, since the backends are from crates.io and they rely on the old glutin version...
This will be unlocked if/when we back-port raw-window-handle to gfx-backend-gl-0.3


[dependencies]
#TODO: only depend on the published version
wgn = { package = "wgpu-native", version = "0.2.6", features = ["local", "window-winit"], git = "https://github.com/gfx-rs/wgpu", rev = "a47ff090bb042f1cb2ad7b13c76eca228390a311"}
wgn = { package = "wgpu-native", version = "0.3.1", features = ["local"] }
arrayvec = "0.4"
raw-window-handle = "0.1"
zerocopy = "0.2"

[dev-dependencies]
Expand All @@ -34,3 +38,4 @@ env_logger = "0.6"
glsl-to-spirv = "0.1"
log = "0.4"
png = "0.15"
winit = "0.20.0-alpha3"
12 changes: 6 additions & 6 deletions examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ fn main() {

let instance = wgpu::Instance::new();

let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
}));
});

let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
}));
});

// Rendered image is 256×256 with 32-bit RGBA color
let size = 256u32;
Expand Down Expand Up @@ -52,7 +52,7 @@ fn main() {
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &texture.create_view(None),
attachment: &texture.create_default_view(),
resolve_target: None,
load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store,
Expand All @@ -78,7 +78,7 @@ fn main() {
texture_extent,
);

encoder.finish(None)
encoder.finish()
};

device.get_queue().submit(&[command_buffer]);
Expand Down
28 changes: 12 additions & 16 deletions examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,22 @@ impl framework::Example for Example {
wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgpu::TextureViewDimension::D2,
ty: wgpu::BindingType::UniformBuffer {
dynamic: false,
},
},
wgpu::BindGroupLayoutBinding {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture,
dynamic: false,
multisampled: false,
texture_dimension: wgpu::TextureViewDimension::D2,
ty: wgpu::BindingType::SampledTexture {
multisampled: false,
dimension: wgpu::TextureViewDimension::D2,
},
},
wgpu::BindGroupLayoutBinding {
binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler,
dynamic: false,
multisampled: false,
texture_dimension: wgpu::TextureViewDimension::D2,
},
],
});
Expand All @@ -173,7 +169,7 @@ impl framework::Example for Example {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
});
let texture_view = texture.create_view(None);
let texture_view = texture.create_default_view();
let temp_buf = device
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(&texels);
Expand Down Expand Up @@ -298,7 +294,7 @@ impl framework::Example for Example {
});

// Done
let init_command_buf = init_encoder.finish(None);
let init_command_buf = init_encoder.finish();
device.get_queue().submit(&[init_command_buf]);
Example {
vertex_buf,
Expand All @@ -310,7 +306,7 @@ impl framework::Example for Example {
}
}

fn update(&mut self, _event: wgpu::winit::WindowEvent) {
fn update(&mut self, _event: winit::event::WindowEvent) {
//empty
}

Expand All @@ -325,7 +321,7 @@ impl framework::Example for Example {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&temp_buf, 0, &self.uniform_buf, 0, 64);
device.get_queue().submit(&[encoder.finish(None)]);
device.get_queue().submit(&[encoder.finish()]);
}

fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
Expand Down Expand Up @@ -354,7 +350,7 @@ impl framework::Example for Example {
rpass.draw_indexed(0 .. self.index_count as u32, 0, 0 .. 1);
}

device.get_queue().submit(&[encoder.finish(None)]);
device.get_queue().submit(&[encoder.finish()]);
}
}

Expand Down
80 changes: 40 additions & 40 deletions examples/framework.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use log::info;
use winit::event::WindowEvent;

#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(unused)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0,
Expand Down Expand Up @@ -33,53 +35,47 @@ pub fn load_glsl(code: &str, stage: ShaderStage) -> Vec<u32> {
wgpu::read_spirv(glsl_to_spirv::compile(&code, ty).unwrap()).unwrap()
}

pub trait Example {
pub trait Example: 'static {
fn init(sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) -> Self;
fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device);
fn update(&mut self, event: wgpu::winit::WindowEvent);
fn update(&mut self, event: WindowEvent);
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device);
}

pub fn run<E: Example>(title: &str) {
use wgpu::winit::{
ElementState,
Event,
EventsLoop,
KeyboardInput,
VirtualKeyCode,
WindowEvent,
use winit::{
event_loop::{ControlFlow, EventLoop},
event,
};

env_logger::init();

let mut events_loop = EventsLoop::new();

let event_loop = EventLoop::new();
info!("Initializing the window...");

#[cfg(not(feature = "gl"))]
let (_window, instance, hidpi_factor, size, surface) = {
use wgpu::winit::Window;
use raw_window_handle::HasRawWindowHandle as _;

let instance = wgpu::Instance::new();

let window = Window::new(&events_loop).unwrap();
let window = winit::window::Window::new(&event_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 hidpi_factor = window.hidpi_factor();
let size = window.inner_size().to_physical(hidpi_factor);

let surface = instance.create_surface(&window);
let instance = wgpu::Instance::new();
let surface = instance.create_surface(window.raw_window_handle());

(window, instance, hidpi_factor, size, surface)
};

#[cfg(feature = "gl")]
let (_window, instance, hidpi_factor, size, surface) = {
let wb = wgpu::winit::WindowBuilder::new();
let wb = winit::WindowBuilder::new();
let cb = wgpu::glutin::ContextBuilder::new().with_vsync(true);
let context = cb.build_windowed(wb, &events_loop).unwrap();
let context = cb.build_windowed(wb, &event_loop).unwrap();
context.window().set_title(title);

let hidpi_factor = context.window().get_hidpi_factor();
let hidpi_factor = context.window().hidpi_factor();
let size = context
.window()
.get_inner_size()
Expand All @@ -94,16 +90,16 @@ pub fn run<E: Example>(title: &str) {
(window, instance, hidpi_factor, size, surface)
};

let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
}));
});

let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
}));
});

let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
Expand All @@ -118,10 +114,14 @@ pub fn run<E: Example>(title: &str) {
let mut example = E::init(&sc_desc, &mut device);

info!("Entering render loop...");
let mut running = true;
while running {
events_loop.poll_events(|event| match event {
Event::WindowEvent {
event_loop.run(move |event, _, control_flow| {
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
ControlFlow::Poll
};
match event {
event::Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
Expand All @@ -132,30 +132,30 @@ pub fn run<E: Example>(title: &str) {
swap_chain = device.create_swap_chain(&surface, &sc_desc);
example.resize(&sc_desc, &mut device);
}
Event::WindowEvent { event, .. } => match event {
event::Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::Escape),
state: event::ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
running = false;
*control_flow = ControlFlow::Exit;
}
_ => {
example.update(event);
}
},
event::Event::EventsCleared => {
let frame = swap_chain.get_next_texture();
example.render(&frame, &mut device);
}
_ => (),
});

let frame = swap_chain.get_next_texture();
example.render(&frame, &mut device);
running &= !cfg!(feature = "metal-auto-capture");
}
}
});
}

// This allows treating the framework as a standalone example,
Expand Down
25 changes: 12 additions & 13 deletions examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ fn main() {
let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;

let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
}));
});

let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
}));
});

let cs = include_bytes!("shader.comp.spv");
let cs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
Expand All @@ -46,14 +46,13 @@ fn main() {
});

let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
bindings: &[wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgpu::TextureViewDimension::D2,
}],
bindings: &[
wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer { dynamic: false, readonly: false },
},
],
});

let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
Expand Down Expand Up @@ -89,7 +88,7 @@ fn main() {
}
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);

device.get_queue().submit(&[encoder.finish(None)]);
device.get_queue().submit(&[encoder.finish()]);

staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| {
if let Ok(mapping) = result {
Expand Down
Loading