12: Rust wrapper r=grovesNL a=kvark



Co-authored-by: Dzmitry Malyshau <kvark@mozilla.com>
This commit is contained in:
bors[bot] 2018-09-26 18:32:52 +00:00
commit 6cee0d647a
8 changed files with 110 additions and 38 deletions

8
Cargo.lock generated
View File

@ -206,6 +206,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "examples"
version = "0.1.0"
dependencies = [
"wgpu 0.1.0",
"wgpu-native 0.1.0",
]
@ -908,6 +909,13 @@ dependencies = [
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wgpu"
version = "0.1.0"
dependencies = [
"wgpu-native 0.1.0",
]
[[package]]
name = "wgpu-bindings"
version = "0.1.0"

View File

@ -2,5 +2,6 @@
members = [
"wgpu-native",
"wgpu-bindings",
"wgpu-rs",
"examples",
]

View File

@ -1,5 +1,11 @@
# WebGPU
[![Build Status](https://travis-ci.org/gfx-rs/wgpu-native.svg)](https://travis-ci.org/gfx-rs/wgpu-native)
[![Gitter](https://badges.gitter.im/gfx-rs/gfx.svg)](https://gitter.im/gfx-rs/gfx)
[![Build Status](https://travis-ci.org/gfx-rs/wgpu.svg)](https://travis-ci.org/gfx-rs/wgpu)
[![Gitter](https://badges.gitter.im/gfx-rs/webgpu.svg)](https://gitter.im/gfx-rs/webgpu)
This is an experimental [WebGPU](https://www.w3.org/community/gpu/) implementation as a native static library. It's written in Rust and is based on [gfx-hal](https://github.com/gfx-rs/gfx) and [satellite](https://github.com/gfx-rs/gfx-memory) libraries. The corresponding WebIDL specification can be found at [gpuweb project](https://github.com/gpuweb/gpuweb/blob/master/design/sketch.webidl).
The implementation consists of the following parts:
- `wgpu-native` - the native implementation of WebGPU as a C API library
- `wgpu-bindings` - automatic generator of actual C headers
- `wgpu-remote` - remoting layer to work with WebGPU across the process boundary
- `wgpu-rs` - idiomatic Rust wrapper of the native library

View File

@ -14,9 +14,10 @@ path = "hello_triangle_rust/main.rs"
[features]
default = []
remote = ["wgpu-native/remote"]
metal = ["wgpu-native/metal"]
dx12 = ["wgpu-native/dx12"]
vulkan = ["wgpu-native/vulkan"]
metal = ["wgpu-native/gfx-backend-metal"]
dx12 = ["wgpu-native/gfx-backend-dx12"]
vulkan = ["wgpu-native/gfx-backend-vulkan"]
[dependencies]
wgpu-native = { path = "../wgpu-native" }
wgpu = { path = "../wgpu-rs" }

View File

@ -1,40 +1,20 @@
extern crate wgpu_native;
use wgpu_native::*;
extern crate wgpu;
fn main() {
let instance = wgpu_create_instance();
let adapter = wgpu_instance_get_adapter(
instance,
AdapterDescriptor {
power_preference: PowerPreference::LowPower,
let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(
wgpu::AdapterDescriptor {
power_preference: wgpu::PowerPreference::LowPower,
},
);
let device = wgpu_adapter_create_device(
adapter,
DeviceDescriptor {
extensions: Extensions {
let device = adapter.create_device(
wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
},
);
let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv");
let _vs = wgpu_device_create_shader_module(
device,
ShaderModuleDescriptor {
code: ByteArray {
bytes: vs_bytes.as_ptr(),
length: vs_bytes.len(),
},
},
);
let _vs = device.create_shader_module(vs_bytes);
let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv");
let _fs = wgpu_device_create_shader_module(
device,
ShaderModuleDescriptor {
code: ByteArray {
bytes: fs_bytes.as_ptr(),
length: fs_bytes.len(),
},
},
);
let _fs = device.create_shader_module(fs_bytes);
}

View File

@ -12,9 +12,6 @@ crate-type = ["lib", "cdylib", "staticlib"]
[features]
default = []
remote = ["parking_lot"]
metal = ["gfx-backend-metal"]
dx12 = ["gfx-backend-dx12"]
vulkan = ["gfx-backend-vulkan"]
[dependencies]
bitflags = "1.0"

18
wgpu-rs/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "wgpu"
version = "0.1.0"
authors = [
"Dzmitry Malyshau <kvark@mozilla.com>",
"Joshua Groves <josh@joshgroves.com>",
]
[lib]
[features]
default = []
metal = ["wgpu-native/gfx-backend-metal"]
dx12 = ["wgpu-native/gfx-backend-dx12"]
vulkan = ["wgpu-native/gfx-backend-vulkan"]
[dependencies]
wgpu-native = { path = "../wgpu-native" }

61
wgpu-rs/src/lib.rs Normal file
View File

@ -0,0 +1,61 @@
extern crate wgpu_native as wgn;
pub use wgn::{
Color, Origin3d, Extent3d,
AdapterDescriptor, Extensions, DeviceDescriptor, PowerPreference,
ShaderModuleDescriptor,
};
pub struct Instance {
id: wgn::InstanceId,
}
pub struct Adapter {
id: wgn::AdapterId,
}
pub struct Device {
id: wgn::DeviceId,
}
pub struct ShaderModule {
id: wgn::ShaderModuleId,
}
impl Instance {
pub fn new() -> Self {
Instance {
id: wgn::wgpu_create_instance(),
}
}
pub fn get_adapter(&self, desc: AdapterDescriptor) -> Adapter {
Adapter {
id: wgn::wgpu_instance_get_adapter(self.id, desc),
}
}
}
impl Adapter {
pub fn create_device(&self, desc: DeviceDescriptor) -> Device {
Device {
id: wgn::wgpu_adapter_create_device(self.id, desc),
}
}
}
impl Device {
pub fn create_shader_module(&self, spv: &[u8]) -> ShaderModule {
let desc = wgn::ShaderModuleDescriptor{
code: wgn::ByteArray {
bytes: spv.as_ptr(),
length: spv.len(),
},
};
ShaderModule {
id: wgn::wgpu_device_create_shader_module(self.id, desc),
}
}
}