Skip to content

Commit

Permalink
Replace wgpu-native with wgpu-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakor Gyula committed Dec 6, 2019
1 parent 250184f commit e146e60
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 59 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -31,4 +31,3 @@ mio = { git = "https://github.com/servo/mio.git", branch = "servo" }
# https://github.com/retep998/winapi-rs/pull/816
winapi = { git = "https://github.com/servo/winapi-rs", branch = "patch-1" }
spirv_cross = { git = "https://github.com/kvark/spirv_cross", branch = "wgpu" }
wgpu-native = { git = "https://github.com/zakorgy/wgpu", branch = "v0.4" }
16 changes: 9 additions & 7 deletions components/script/dom/gpu.rs
Expand Up @@ -117,20 +117,22 @@ impl GPUMethods for GPU {
let promise = Promise::new_in_current_compartment(&self.global(), comp);
let sender = response_async(&promise, self);
let power_preference = match options.powerPreference {
Some(GPUPowerPreference::Low_power) => wgpu::PowerPreference::LowPower,
Some(GPUPowerPreference::High_performance) => wgpu::PowerPreference::HighPerformance,
None => wgpu::PowerPreference::Default,
Some(GPUPowerPreference::Low_power) => wgpu::instance::PowerPreference::LowPower,
Some(GPUPowerPreference::High_performance) => {
wgpu::instance::PowerPreference::HighPerformance
},
None => wgpu::instance::PowerPreference::Default,
};
let id = self.global().as_window().Navigator().create_adapter_id();
let ids = self.global().as_window().Navigator().create_adapter_ids();

match self.wgpu_channel() {
Some(channel) => {
channel
.0
.send(WebGPURequest::RequestAdapter(
sender,
wgpu::RequestAdapterOptions { power_preference },
id,
wgpu::instance::RequestAdapterOptions { power_preference },
ids,
))
.unwrap();
},
Expand All @@ -146,7 +148,7 @@ impl AsyncWGPUListener for GPU {
WebGPUResponse::RequestAdapter(name, adapter) => {
let adapter = GPUAdapter::new(
&self.global(),
DOMString::from(name),
DOMString::from(format!("{} ({:?})", name, adapter.0.backend())),
Heap::default(),
adapter,
);
Expand Down
10 changes: 6 additions & 4 deletions components/script/dom/gpuadapter.rs
Expand Up @@ -78,16 +78,18 @@ impl GPUAdapterMethods for GPUAdapter {
fn RequestDevice(&self, descriptor: &GPUDeviceDescriptor, comp: InCompartment) -> Rc<Promise> {
let promise = Promise::new_in_current_compartment(&self.global(), comp);
let sender = response_async(&promise, self);
let desc = wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
let desc = wgpu::instance::DeviceDescriptor {
extensions: wgpu::instance::Extensions {
anisotropic_filtering: descriptor.extensions.anisotropicFiltering,
},
limits: wgpu::Limits {
limits: wgpu::instance::Limits {
max_bind_groups: descriptor.limits.maxBindGroups,
},
};
if let Some(window) = self.global().downcast::<Window>() {
let id = window.Navigator().create_device_id();
let id = window
.Navigator()
.create_device_id(self.adapter.0.backend());
match window.webgpu_channel() {
Some(thread) => thread
.0
Expand Down
93 changes: 72 additions & 21 deletions components/script/dom/identityhub.rs
Expand Up @@ -2,50 +2,101 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use webgpu::wgpu::{AdapterId, Backend, DeviceId, IdentityManager, SurfaceId};
use smallvec::SmallVec;
use webgpu::wgpu::{
hub::IdentityManager,
id::{AdapterId, DeviceId},
Backend,
};

#[derive(Debug)]
pub struct IdentityHub {
adapters: IdentityManager<AdapterId>,
devices: IdentityManager<DeviceId>,
adapters: IdentityManager,
devices: IdentityManager,
backend: Backend,
}

impl IdentityHub {
fn new(backend: Backend) -> Self {
IdentityHub {
adapters: IdentityManager::new(backend),
devices: IdentityManager::new(backend),
adapters: IdentityManager::default(),
devices: IdentityManager::default(),
backend,
}
}

fn create_adapter_id(&mut self) -> AdapterId {
self.adapters.alloc(self.backend)
}

fn create_device_id(&mut self) -> DeviceId {
self.devices.alloc(self.backend)
}
}

#[derive(Debug)]
pub struct Identities {
surface: IdentityManager<SurfaceId>,
hub: IdentityHub,
surface: IdentityManager,
#[cfg(any(target_os = "linux", target_os = "windows"))]
vk_hub: IdentityHub,
#[cfg(target_os = "windows")]
dx12_hub: IdentityHub,
#[cfg(target_os = "windows")]
dx11_hub: IdentityHub,
#[cfg(any(target_os = "ios", target_os = "macos"))]
metal_hub: IdentityHub,
dummy_hub: IdentityHub,
}

impl Identities {
pub fn new() -> Self {
let hub = if cfg!(any(target_os = "linux", target_os = "windows")) {
IdentityHub::new(Backend::Vulkan)
} else if cfg!(any(target_os = "ios", target_os = "macos")) {
IdentityHub::new(Backend::Metal)
} else {
IdentityHub::new(Backend::Empty)
};

Identities {
surface: IdentityManager::new(Backend::Empty),
hub,
surface: IdentityManager::default(),
#[cfg(any(target_os = "linux", target_os = "windows"))]
vk_hub: IdentityHub::new(Backend::Vulkan),
#[cfg(target_os = "windows")]
dx12_hub: IdentityHub::new(Backend::Dx12),
#[cfg(target_os = "windows")]
dx11_hub: IdentityHub::new(Backend::Dx11),
#[cfg(any(target_os = "ios", target_os = "macos"))]
metal_hub: IdentityHub::new(Backend::Metal),
dummy_hub: IdentityHub::new(Backend::Empty),
}
}

pub fn create_adapter_id(&mut self) -> AdapterId {
self.hub.adapters.alloc()
fn hubs(&mut self) -> Vec<&mut IdentityHub> {
vec![
#[cfg(any(target_os = "linux", target_os = "windows"))]
&mut self.vk_hub,
#[cfg(target_os = "windows")]
&mut self.dx12_hub,
#[cfg(target_os = "windows")]
&mut self.dx11_hub,
#[cfg(any(target_os = "ios", target_os = "macos"))]
&mut self.metal_hub,
&mut self.dummy_hub,
]
}

pub fn create_device_id(&mut self) -> DeviceId {
self.hub.devices.alloc()
pub fn create_device_id(&mut self, backend: Backend) -> DeviceId {
match backend {
#[cfg(any(target_os = "linux", target_os = "windows"))]
Backend::Vulkan => self.vk_hub.create_device_id(),
#[cfg(target_os = "windows")]
Backend::Dx12 => self.dx12_hub.create_device_id(),
#[cfg(target_os = "windows")]
Backend::Dx11 => self.dx11_hub.create_device_id(),
#[cfg(any(target_os = "ios", target_os = "macos"))]
Backend::Metal => self.metal_hub.create_device_id(),
_ => self.dummy_hub.create_device_id(),
}
}

pub fn create_adapter_ids(&mut self) -> SmallVec<[AdapterId; 4]> {
let mut ids = SmallVec::new();
for hub in self.hubs() {
ids.push(hub.create_adapter_id())
}
ids
}
}
14 changes: 9 additions & 5 deletions components/script/dom/navigator.rs
Expand Up @@ -24,9 +24,13 @@ use crate::dom::serviceworkercontainer::ServiceWorkerContainer;
use crate::dom::window::Window;
use crate::dom::xr::XR;
use dom_struct::dom_struct;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::rc::Rc;
use webgpu::wgpu::{AdapterId, DeviceId};
use webgpu::wgpu::{
id::{AdapterId, DeviceId},
Backend,
};

#[dom_struct]
pub struct Navigator {
Expand Down Expand Up @@ -73,12 +77,12 @@ impl Navigator {
}

impl Navigator {
pub fn create_adapter_id(&self) -> AdapterId {
self.gpu_id_hub.borrow_mut().create_adapter_id()
pub fn create_adapter_ids(&self) -> SmallVec<[AdapterId; 4]> {
self.gpu_id_hub.borrow_mut().create_adapter_ids()
}

pub fn create_device_id(&self) -> DeviceId {
self.gpu_id_hub.borrow_mut().create_device_id()
pub fn create_device_id(&self, backend: Backend) -> DeviceId {
self.gpu_id_hub.borrow_mut().create_device_id(backend)
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/webgpu/Cargo.toml
Expand Up @@ -17,4 +17,5 @@ log = "0.4"
malloc_size_of = { path = "../malloc_size_of" }
serde = "1.0"
servo_config = {path = "../config"}
wgpu-native = { version = "0.4.0", features = ["serde"] }
smallvec = "0.6"
wgpu-core = { version = "0.1.0", git = "https://github.com/gfx-rs/wgpu", features = ["serde"] }
43 changes: 27 additions & 16 deletions components/webgpu/lib.rs
Expand Up @@ -7,17 +7,17 @@ extern crate log;
#[macro_use]
extern crate serde;
#[macro_use]
pub extern crate wgpu_native as wgpu;
pub extern crate wgpu_core as wgpu;

use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use servo_config::pref;
use wgpu::{adapter_get_info, adapter_request_device};
use smallvec::SmallVec;

#[derive(Debug, Deserialize, Serialize)]
pub enum WebGPUResponse {
RequestAdapter(String, WebGPUAdapter),
RequestDevice(WebGPUDevice, wgpu::DeviceDescriptor),
RequestDevice(WebGPUDevice, wgpu::instance::DeviceDescriptor),
}

pub type WebGPUResponseResult = Result<WebGPUResponse, String>;
Expand All @@ -26,14 +26,14 @@ pub type WebGPUResponseResult = Result<WebGPUResponse, String>;
pub enum WebGPURequest {
RequestAdapter(
IpcSender<WebGPUResponseResult>,
wgpu::RequestAdapterOptions,
wgpu::AdapterId,
wgpu::instance::RequestAdapterOptions,
SmallVec<[wgpu::id::AdapterId; 4]>,
),
RequestDevice(
IpcSender<WebGPUResponseResult>,
WebGPUAdapter,
wgpu::DeviceDescriptor,
wgpu::DeviceId,
wgpu::instance::DeviceDescriptor,
wgpu::id::DeviceId,
),
Exit(IpcSender<()>),
}
Expand Down Expand Up @@ -78,8 +78,9 @@ impl WebGPU {

struct WGPU {
receiver: IpcReceiver<WebGPURequest>,
global: wgpu::Global,
global: wgpu::hub::Global<()>,
adapters: Vec<WebGPUAdapter>,
devices: Vec<WebGPUDevice>,
// Track invalid adapters https://gpuweb.github.io/gpuweb/#invalid
_invalid_adapters: Vec<WebGPUAdapter>,
}
Expand All @@ -88,8 +89,9 @@ impl WGPU {
fn new(receiver: IpcReceiver<WebGPURequest>) -> Self {
WGPU {
receiver,
global: wgpu::Global::new("webgpu-native"),
global: wgpu::hub::Global::new("wgpu-core"),
adapters: Vec::new(),
devices: Vec::new(),
_invalid_adapters: Vec::new(),
}
}
Expand All @@ -101,8 +103,11 @@ impl WGPU {
fn run(mut self) {
while let Ok(msg) = self.receiver.recv() {
match msg {
WebGPURequest::RequestAdapter(sender, options, id) => {
let adapter_id = match wgpu::request_adapter(&self.global, &options, &[id]) {
WebGPURequest::RequestAdapter(sender, options, ids) => {
let adapter_id = match self.global.pick_adapter(
&options,
wgpu::instance::AdapterInputs::IdSet(&ids, |id| id.backend()),
) {
Some(id) => id,
None => {
if let Err(e) =
Expand All @@ -118,8 +123,8 @@ impl WGPU {
};
let adapter = WebGPUAdapter(adapter_id);
self.adapters.push(adapter);
let info =
gfx_select!(adapter_id => adapter_get_info(&self.global, adapter_id));
let global = &self.global;
let info = gfx_select!(adapter_id => global.adapter_get_info(adapter_id));
if let Err(e) =
sender.send(Ok(WebGPUResponse::RequestAdapter(info.name, adapter)))
{
Expand All @@ -130,8 +135,14 @@ impl WGPU {
}
},
WebGPURequest::RequestDevice(sender, adapter, descriptor, id) => {
let _output = gfx_select!(id => adapter_request_device(&self.global, adapter.0, &descriptor, id));
let global = &self.global;
let id = gfx_select!(id => global.adapter_request_device(
adapter.0,
&descriptor,
id
));
let device = WebGPUDevice(id);
self.devices.push(device);
if let Err(e) =
sender.send(Ok(WebGPUResponse::RequestDevice(device, descriptor)))
{
Expand Down Expand Up @@ -168,5 +179,5 @@ macro_rules! webgpu_resource {
};
}

webgpu_resource!(WebGPUAdapter, wgpu::AdapterId);
webgpu_resource!(WebGPUDevice, wgpu::DeviceId);
webgpu_resource!(WebGPUAdapter, wgpu::id::AdapterId);
webgpu_resource!(WebGPUDevice, wgpu::id::DeviceId);

0 comments on commit e146e60

Please sign in to comment.