From c6522fa60b00af31a9703481bd3057c831c9249e Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 29 Aug 2019 15:35:11 -0400 Subject: [PATCH] Update wgpu-native to the commit that has no backend features. This change removes the dependency on gfx-rs backends, refactors Adapter and Surface creation to be done from nothing. --- .travis.yml | 4 +- Cargo.toml | 8 +--- README.md | 28 ++++--------- examples/capture/main.rs | 7 ++-- examples/framework.rs | 14 +++---- examples/hello-compute/main.rs | 6 +-- examples/hello-triangle/main.rs | 13 +++--- src/lib.rs | 74 +++++++++------------------------ 8 files changed, 46 insertions(+), 108 deletions(-) diff --git a/.travis.yml b/.travis.yml index cd46406119..088a66ed04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,4 @@ branches: script: - cargo check - - if [[ $TRAVIS_OS_NAME == "linux" ]]; 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 + - cargo test --no-run diff --git a/Cargo.toml b/Cargo.toml index 89e01e3150..c93979a5e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,15 +19,9 @@ license = "MPL-2.0" [features] default = [] -metal = ["wgn/gfx-backend-metal"] -dx11 = ["wgn/gfx-backend-dx11"] -dx12 = ["wgn/gfx-backend-dx12"] -vulkan = ["wgn/gfx-backend-vulkan"] -#TODO: there is no way to use glutin-0.20 with GL backend v0.3 yet -#gl = ["wgn/gfx-backend-gl"] [dependencies] -wgn = { package = "wgpu-native", version = "0.3.1", features = ["local"] } +wgn = { package = "wgpu-native", git = "https://github.com/gfx-rs/wgpu", rev = "b58c96e4a66943e09e9a9aa0ee8345c7bc878d38" } arrayvec = "0.4" raw-window-handle = "0.1" zerocopy = "0.2" diff --git a/README.md b/README.md index 3460565cdf..b5db6ee0bd 100644 --- a/README.md +++ b/README.md @@ -12,35 +12,20 @@ This is an idiomatic Rust wrapper over [wgpu-native](https://github.com/gfx-rs/w ## Usage -The library requires one of the following features enabled in order to run any of the examples: - - Vulkan - - Metal - - DirectX 12 (Dx12) - - DirectX 11 (Dx11) - - OpenGL (Gl) - -These examples assume that necessary dependencies for the graphics backend are already installed. - ### Running an example All examples are located under the [examples](examples) directory. We are using the default syntax for running examples, as found in the [Cargo](https://doc.rust-lang.org/cargo/reference/manifest.html#examples) documentation. -#### Cube ```bash -cargo run --example cube --features metal -cargo run --example cube --features vulkan -cargo run --example cube --features dx12 -cargo run --example cube --features dx11 -cargo run --example cube --features gl +cargo run --example cube ``` #### Hello Compute -The "1", "2", "3", and "4" are arguments passed into the program. These arguments are used for the compute pipeline. + +`hello-*` examples show barebones setup without any helper code. + +For "hello-compute", pass 4 numbers separated by spaces as arguments: ```bash -cargo run --example hello-compute --features metal 1 2 3 4 -cargo run --example hello-compute --features vulkan 1 2 3 4 -cargo run --example hello-compute --features dx12 1 2 3 4 -cargo run --example hello-compute --features dx11 1 2 3 4 -cargo run --example hello-compute --features gl 1 2 3 4 +cargo run --example hello-compute 1 2 3 4 ``` More examples can be found under the [examples](examples) directory. @@ -50,6 +35,7 @@ More examples can be found under the [examples](examples) directory. Shout out to the following projects that work best with wgpu-rs: - [wgpu_glyph](https://github.com/hecrj/wgpu_glyph) - for your text-y rendering needs - [coffee](https://github.com/hecrj/coffee) - a whole 2D engine + - [rgx](https://github.com/cloudhead/rgx) - a 2D graphics library - [imgui-wgpu](https://github.com/unconed/imgui-wgpu-rs) - Dear ImGui interfacing ## Development diff --git a/examples/capture/main.rs b/examples/capture/main.rs index ae36d24486..0f1a26f691 100644 --- a/examples/capture/main.rs +++ b/examples/capture/main.rs @@ -7,11 +7,10 @@ use std::mem::size_of; fn main() { env_logger::init(); - let instance = wgpu::Instance::new(); - - let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions { + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::LowPower, - }); + backends: wgpu::BackendBit::PRIMARY, + }).unwrap(); let mut device = adapter.request_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { diff --git a/examples/framework.rs b/examples/framework.rs index df3a846170..2cd106e7c6 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -53,16 +53,13 @@ pub fn run(title: &str) { info!("Initializing the window..."); #[cfg(not(feature = "gl"))] - let (_window, instance, hidpi_factor, size, surface) = { + let (_window, hidpi_factor, size, surface) = { let window = winit::window::Window::new(&event_loop).unwrap(); window.set_title(title); let hidpi_factor = window.hidpi_factor(); let size = window.inner_size().to_physical(hidpi_factor); - - let instance = wgpu::Instance::new(); - let surface = instance.create_surface(&window); - - (window, instance, hidpi_factor, size, surface) + let surface = wgpu::Surface::create(&window); + (window, hidpi_factor, size, surface) }; #[cfg(feature = "gl")] @@ -87,9 +84,10 @@ pub fn run(title: &str) { (window, instance, hidpi_factor, size, surface) }; - let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions { + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::LowPower, - }); + backends: wgpu::BackendBit::PRIMARY, + }).unwrap(); let mut device = adapter.request_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { diff --git a/examples/hello-compute/main.rs b/examples/hello-compute/main.rs index 15481e4214..1d7b11d7b7 100644 --- a/examples/hello-compute/main.rs +++ b/examples/hello-compute/main.rs @@ -14,10 +14,10 @@ fn main() { let size = (numbers.len() * std::mem::size_of::()) as wgpu::BufferAddress; - let instance = wgpu::Instance::new(); - let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions { + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::Default, - }); + backends: wgpu::BackendBit::PRIMARY, + }).unwrap(); let mut device = adapter.request_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { diff --git a/examples/hello-triangle/main.rs b/examples/hello-triangle/main.rs index 0adde7d2ae..e860a979d8 100644 --- a/examples/hello-triangle/main.rs +++ b/examples/hello-triangle/main.rs @@ -8,16 +8,14 @@ fn main() { let event_loop = EventLoop::new(); #[cfg(not(feature = "gl"))] - let (_window, instance, size, surface) = { + let (_window, size, surface) = { let window = winit::window::Window::new(&event_loop).unwrap(); let size = window .inner_size() .to_physical(window.hidpi_factor()); - let instance = wgpu::Instance::new(); - let surface = instance.create_surface(&window); - - (window, instance, size, surface) + let surface = wgpu::Surface::create(&window); + (window, size, surface) }; #[cfg(feature = "gl")] @@ -40,9 +38,10 @@ fn main() { (window, instance, size, surface) }; - let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions { + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::LowPower, - }); + backends: wgpu::BackendBit::PRIMARY, + }).unwrap(); let mut device = adapter.request_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { diff --git a/src/lib.rs b/src/lib.rs index 98b4646d6c..480b12b9c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ use std::thread; pub use wgn::{ AddressMode, + BackendBit, BlendDescriptor, BlendFactor, BlendOperation, @@ -75,15 +76,6 @@ struct Temp { command_buffers: Vec, } -/// A handle to an active `wgpu` instance. -/// -/// An `Instance` represents the entire context of a running `wgpu` instance. The `Instance` -/// allows the querying of [`Adapter`] objects and the creation of [`Surface`] objects. -#[derive(Debug)] -pub struct Instance { - id: wgn::InstanceId, -} - /// A handle to a physical graphics and/or compute device. /// /// An `Adapter` can be used to open a connection to the corresponding device on the host system, @@ -527,61 +519,33 @@ 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::RawContext) -> 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 `options`, this function will return another - /// adapter. - /// - /// # Panics - /// - /// Panics if there are no available adapters. This will occur if none of the graphics backends - /// are enabled. - pub fn request_adapter(&self, options: &RequestAdapterOptions) -> Adapter { - Adapter { - id: wgn::wgpu_instance_request_adapter(self.id, Some(options)), - } - } - +impl Surface { /// Creates a surface from a raw window handle. - #[cfg(not(feature = "gl"))] - pub fn create_surface(&self, window: &W) -> Surface { + pub fn create(window: &W) -> Self { Surface { - id: wgn::wgpu_instance_create_surface(self.id, window.raw_window_handle()), + id: wgn::wgpu_create_surface(window.raw_window_handle()), } } - #[cfg(not(feature = "gl"))] - pub fn create_surface_from_core_animation_layer(&self, layer: *mut std::ffi::c_void) -> Surface { + #[cfg(any(target_os = "ios", target_os = "macos"))] + pub fn create_surface_from_core_animation_layer(layer: *mut std::ffi::c_void) -> Self { Surface { - id: wgn::wgpu_instance_create_surface_from_macos_layer(self.id, layer), - } - } - - #[cfg(feature = "gl")] - pub fn get_surface(&self) -> Surface { - Surface { - id: wgn::wgpu_instance_get_gl_surface(self.id), + id: wgn::wgpu_create_surface_from_metal_layer(layer), } } } impl Adapter { + /// Retrieves an [`Adapter`] which matches the given options. + /// + /// Some options are "soft", so treated as non-mandatory. Others are "hard". + /// + /// If no adapters are found that suffice all the "hard" options, `None` is returned. + pub fn request(options: &wgn::RequestAdapterOptions) -> Option { + wgn::request_adapter(options, &[]) + .map(|id| Adapter { id }) + } + /// Requests a connection to a physical device, creating a logical device. /// /// # Panics @@ -625,7 +589,7 @@ impl Device { /// Creates an empty [`CommandEncoder`]. pub fn create_command_encoder(&self, desc: &CommandEncoderDescriptor) -> CommandEncoder { CommandEncoder { - id: wgn::wgpu_device_create_command_encoder(self.id, desc), + id: wgn::wgpu_device_create_command_encoder(self.id, Some(desc)), } } @@ -1110,7 +1074,7 @@ impl CommandEncoder { /// This function returns a [`ComputePass`] object which records a single compute pass. pub fn begin_compute_pass(&mut self) -> ComputePass { ComputePass { - id: wgn::wgpu_command_encoder_begin_compute_pass(self.id), + id: wgn::wgpu_command_encoder_begin_compute_pass(self.id, None), _parent: self, } }