Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ rev = "73b33ea76e2f91b3114aa7640b1d60518d39f915"
[dependencies]
arrayvec = "0.5"
raw-window-handle = "0.3"
zerocopy = "0.2"

[dev-dependencies]
cgmath = "0.17"
Expand All @@ -46,3 +45,4 @@ glsl-to-spirv = "0.1"
log = "0.4"
png = "0.15"
winit = "0.20.0-alpha4"
zerocopy = "0.2"
3 changes: 2 additions & 1 deletion examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fn main() {
power_preference: wgpu::PowerPreference::Default,
},
wgpu::BackendBit::PRIMARY,
).unwrap();
)
.unwrap();

let (device, mut queue) = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
Expand Down
56 changes: 31 additions & 25 deletions examples/cube/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[path = "../framework.rs"]
mod framework;

use zerocopy::{AsBytes, FromBytes};

#[repr(C)]
#[derive(Clone, Copy, zerocopy::AsBytes, zerocopy::FromBytes)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
struct Vertex {
_pos: [f32; 4],
_tex_coord: [f32; 2],
Expand Down Expand Up @@ -107,7 +109,10 @@ impl Example {
}

impl framework::Example for Example {
fn init(sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device) -> (Self, Option<wgpu::CommandBuffer>) {
fn init(
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
) -> (Self, Option<wgpu::CommandBuffer>) {
use std::mem;

let mut init_encoder =
Expand All @@ -116,23 +121,20 @@ impl framework::Example for Example {
// Create the vertex and index buffers
let vertex_size = mem::size_of::<Vertex>();
let (vertex_data, index_data) = create_vertices();
let vertex_buf = device
.create_buffer_mapped(vertex_data.len(), wgpu::BufferUsage::VERTEX)
.fill_from_slice(&vertex_data);

let index_buf = device
.create_buffer_mapped(index_data.len(), wgpu::BufferUsage::INDEX)
.fill_from_slice(&index_data);
let vertex_buf =
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);

let index_buf =
device.create_buffer_with_data(index_data.as_bytes(), wgpu::BufferUsage::INDEX);

// Create pipeline layout
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
bindings: &[
wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer {
dynamic: false,
},
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
},
wgpu::BindGroupLayoutBinding {
binding: 1,
Expand Down Expand Up @@ -171,9 +173,8 @@ impl framework::Example for Example {
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
});
let texture_view = texture.create_default_view();
let temp_buf = device
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(&texels);
let temp_buf =
device.create_buffer_with_data(texels.as_slice(), wgpu::BufferUsage::COPY_SRC);
init_encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
buffer: &temp_buf,
Expand Down Expand Up @@ -208,12 +209,10 @@ impl framework::Example for Example {
});
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
let uniform_buf = device
.create_buffer_mapped(
16,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
)
.fill_from_slice(mx_ref);
let uniform_buf = device.create_buffer_with_data(
mx_ref.as_bytes(),
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
);

// Create bind group
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
Expand Down Expand Up @@ -310,21 +309,28 @@ impl framework::Example for Example {
//empty
}

fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device) -> Option<wgpu::CommandBuffer> {
fn resize(
&mut self,
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
) -> Option<wgpu::CommandBuffer> {
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();

let temp_buf = device
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(mx_ref);
let temp_buf =
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);

let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&temp_buf, 0, &self.uniform_buf, 0, 64);
Some(encoder.finish())
}

fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &wgpu::Device) -> wgpu::CommandBuffer {
fn render(
&mut self,
frame: &wgpu::SwapChainOutput,
device: &wgpu::Device,
) -> wgpu::CommandBuffer {
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
{
Expand Down
3 changes: 2 additions & 1 deletion examples/describe/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ fn main() {
power_preference: wgpu::PowerPreference::Default,
},
wgpu::BackendBit::PRIMARY,
).unwrap();
)
.unwrap();

println!("{:?}", adapter.get_info())
}
6 changes: 4 additions & 2 deletions examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ pub fn run<E: Example>(title: &str) {
power_preference: wgpu::PowerPreference::Default,
},
wgpu::BackendBit::PRIMARY,
).unwrap();
)
.unwrap();

let (device, mut queue) = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
Expand Down Expand Up @@ -163,7 +164,8 @@ pub fn run<E: Example>(title: &str) {
}
},
event::Event::EventsCleared => {
let frame = swap_chain.get_next_texture()
let frame = swap_chain
.get_next_texture()
.expect("Timeout when acquiring next swap chain texture");
let command_buf = example.render(&frame, &device);
queue.submit(&[command_buf]);
Expand Down
47 changes: 27 additions & 20 deletions examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::str::FromStr;
use std::{convert::TryInto as _, str::FromStr};
use zerocopy::AsBytes as _;

fn main() {
env_logger::init();
Expand All @@ -12,14 +13,16 @@ fn main() {
.map(|s| u32::from_str(&s).expect("You must pass a list of positive integers!"))
.collect();

let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;
let slice_size = numbers.len() * std::mem::size_of::<u32>();
let size = slice_size as wgpu::BufferAddress;

let adapter = wgpu::Adapter::request(
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
},
wgpu::BackendBit::PRIMARY,
).unwrap();
)
.unwrap();

let (device, mut queue) = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
Expand All @@ -29,16 +32,13 @@ fn main() {
});

let cs = include_bytes!("shader.comp.spv");
let cs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
let cs_module =
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());

let staging_buffer = device
.create_buffer_mapped(
numbers.len(),
wgpu::BufferUsage::MAP_READ
| wgpu::BufferUsage::COPY_DST
| wgpu::BufferUsage::COPY_SRC,
)
.fill_from_slice(&numbers);
let staging_buffer = device.create_buffer_with_data(
numbers.as_slice().as_bytes(),
wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
);

let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size,
Expand All @@ -48,13 +48,14 @@ 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, readonly: false },
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 @@ -92,9 +93,15 @@ fn main() {

queue.submit(&[encoder.finish()]);

staging_buffer.map_read_async(0, numbers.len(), |result: wgpu::BufferMapAsyncResult<&[u32]>| {
// FIXME: Align and use `LayoutVerified`
staging_buffer.map_read_async(0, slice_size, |result| {
if let Ok(mapping) = result {
println!("Times: {:?}", mapping.data);
let times: Box<[u32]> = mapping
.data
.chunks_exact(4)
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))
.collect();
println!("Times: {:?}", times);
}
});
}
23 changes: 12 additions & 11 deletions examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
use winit::{
event_loop::{ControlFlow, EventLoop},
event,
event_loop::{ControlFlow, EventLoop},
};

env_logger::init();
Expand All @@ -10,9 +10,7 @@ fn main() {
#[cfg(not(feature = "gl"))]
let (_window, size, surface) = {
let window = winit::window::Window::new(&event_loop).unwrap();
let size = window
.inner_size()
.to_physical(window.hidpi_factor());
let size = window.inner_size().to_physical(window.hidpi_factor());

let surface = wgpu::Surface::create(&window);
(window, size, surface)
Expand Down Expand Up @@ -43,7 +41,8 @@ fn main() {
power_preference: wgpu::PowerPreference::Default,
},
wgpu::BackendBit::PRIMARY,
).unwrap();
)
.unwrap();

let (device, mut queue) = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
Expand All @@ -53,14 +52,15 @@ fn main() {
});

let vs = include_bytes!("shader.vert.spv");
let vs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap());
let vs_module =
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap());

let fs = include_bytes!("shader.frag.spv");
let fs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap());
let fs_module =
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap());

let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
bindings: &[],
});
let bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { bindings: &[] });
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
bindings: &[],
Expand Down Expand Up @@ -135,7 +135,8 @@ fn main() {
_ => {}
},
event::Event::EventsCleared => {
let frame = swap_chain.get_next_texture()
let frame = swap_chain
.get_next_texture()
.expect("Timeout when acquiring next swap chain texture");
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
Expand Down
Loading