Skip to content

Commit

Permalink
Convert shadow sample to map_async
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftcoder committed Mar 6, 2019
1 parent 1faba3c commit 6ef53d7
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 326 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
**/*.rs.bk
#Cargo.lock
.vscode
.vs
build
364 changes: 183 additions & 181 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 10 additions & 18 deletions examples/hello_compute_rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ extern crate wgpu_native;

use std::str::FromStr;

// TODO: deduplicate this with the copy in gfx-examples/framework
pub fn cast_slice<T>(data: &[T]) -> &[u8] {
use std::mem::size_of;
use std::slice::from_raw_parts;

unsafe { from_raw_parts(data.as_ptr() as *const u8, data.len() * size_of::<T>()) }
}

fn main() {
env_logger::init();

Expand Down Expand Up @@ -39,16 +31,17 @@ fn main() {
let cs_bytes = include_bytes!("./../data/collatz.comp.spv");
let cs_module = device.create_shader_module(cs_bytes);

let staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size,
usage: wgpu::BufferUsageFlags::MAP_READ
| wgpu::BufferUsageFlags::TRANSFER_DST
| wgpu::BufferUsageFlags::TRANSFER_SRC,
});
staging_buffer.set_sub_data(0, cast_slice(&numbers));
let staging_buffer = device
.create_buffer_mapped(
numbers.len(),
wgpu::BufferUsageFlags::MAP_READ
| wgpu::BufferUsageFlags::TRANSFER_DST
| wgpu::BufferUsageFlags::TRANSFER_SRC,
)
.fill_from_slice(&numbers);

let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size: (numbers.len() * std::mem::size_of::<u32>()) as u32,
size,
usage: wgpu::BufferUsageFlags::STORAGE
| wgpu::BufferUsageFlags::TRANSFER_DST
| wgpu::BufferUsageFlags::TRANSFER_SRC,
Expand All @@ -68,7 +61,7 @@ fn main() {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &storage_buffer,
range: 0..(numbers.len() as u32),
range: 0..size,
},
}],
});
Expand All @@ -95,7 +88,6 @@ fn main() {
}
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);


staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| {
if let wgpu::BufferMapAsyncResult::Success(data) = result {
println!("Times: {:?}", data);
Expand Down
74 changes: 24 additions & 50 deletions gfx-examples/src/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ mod framework;

#[derive(Clone, Copy)]
struct Vertex {
pos: [f32; 4],
tex_coord: [f32; 2],
_pos: [f32; 4],
_tex_coord: [f32; 2],
}

fn vertex(pos: [i8; 3], tc: [i8; 2]) -> Vertex {
Vertex {
pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
tex_coord: [tc[0] as f32, tc[1] as f32],
_pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
_tex_coord: [tc[0] as f32, tc[1] as f32],
}
}

Expand Down Expand Up @@ -115,27 +115,13 @@ 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_buffer_length = vertex_data.len() * vertex_size;
let index_buffer_length = index_data.len() * mem::size_of::<u16>();
let vertex_buf = {
let (vertex_buf, vertex_buf_data) = device.create_buffer_mapped(&wgpu::BufferDescriptor {
size: vertex_buffer_length as u32,
usage: wgpu::BufferUsageFlags::VERTEX | wgpu::BufferUsageFlags::TRANSFER_DST | wgpu::BufferUsageFlags::MAP_WRITE,
});
vertex_buf_data.copy_from_slice(&vertex_data);
vertex_buf.unmap();
vertex_buf
};
let vertex_buf =
device.create_buffer_mapped(vertex_data.len(), wgpu::BufferUsageFlags::VERTEX)
.fill_from_slice(&vertex_data);

let index_buf = {
let (index_buf, index_buf_data) = device.create_buffer_mapped(&wgpu::BufferDescriptor {
size: index_buffer_length as u32,
usage: wgpu::BufferUsageFlags::INDEX | wgpu::BufferUsageFlags::TRANSFER_DST | wgpu::BufferUsageFlags::MAP_WRITE,
});
index_buf_data.copy_from_slice(&index_data);
index_buf.unmap();
index_buf
};
let index_buf =
device.create_buffer_mapped(index_data.len(), wgpu::BufferUsageFlags::INDEX)
.fill_from_slice(&index_data);

// Create pipeline layout
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
Expand Down Expand Up @@ -177,15 +163,9 @@ impl framework::Example for Example {
usage: wgpu::TextureUsageFlags::SAMPLED | wgpu::TextureUsageFlags::TRANSFER_DST,
});
let texture_view = texture.create_default_view();
let temp_buf = {
let (temp_buf, temp_buf_data) = device.create_buffer_mapped(&wgpu::BufferDescriptor {
size: texels.len() as u32,
usage: wgpu::BufferUsageFlags::TRANSFER_SRC | wgpu::BufferUsageFlags::TRANSFER_DST | wgpu::BufferUsageFlags::MAP_WRITE,
});
temp_buf_data.copy_from_slice(&texels);
temp_buf.unmap();
temp_buf
};
let temp_buf =
device.create_buffer_mapped(texels.len(), wgpu::BufferUsageFlags::TRANSFER_SRC)
.fill_from_slice(&texels);
init_encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
buffer: &temp_buf,
Expand Down Expand Up @@ -222,15 +202,9 @@ 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 = {
let (uniform_buf, uniform_buf_data) = device.create_buffer_mapped(&wgpu::BufferDescriptor {
size: 64,
usage: wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST | wgpu::BufferUsageFlags::MAP_WRITE,
});
uniform_buf_data.copy_from_slice(mx_ref);
uniform_buf.unmap();
uniform_buf
};
let uniform_buf =
device.create_buffer_mapped(16, wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST)
.fill_from_slice(mx_ref);

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

fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, _device: &mut wgpu::Device) {
fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) {
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();
// self.uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..]));
self.uniform_buf.map_write_async(0, 64, |result: wgpu::BufferMapAsyncResult<&mut [f32]>| {
if let wgpu::BufferMapAsyncResult::Success(data) = result {
data.copy_from_slice(mx_ref);
}

self.uniform_buf.unmap();
});
let temp_buf =
device.create_buffer_mapped(16, wgpu::BufferUsageFlags::TRANSFER_SRC)
.fill_from_slice(mx_ref);

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()]);
}

fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
Expand Down
126 changes: 73 additions & 53 deletions gfx-examples/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use std::rc::Rc;
mod framework;


#[derive(Clone)]
#[derive(Clone, Copy)]
struct Vertex {
pos: [i8; 4],
normal: [i8; 4],
_pos: [i8; 4],
_normal: [i8; 4],
}

fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex {
Vertex {
pos: [pos[0], pos[1], pos[2], 1],
normal: [nor[0], nor[1], nor[2], 0],
_pos: [pos[0], pos[1], pos[2], 1],
_normal: [nor[0], nor[1], nor[2], 0],
}
}

Expand Down Expand Up @@ -101,6 +101,7 @@ struct Light {
}

#[repr(C)]
#[derive(Clone, Copy)]
struct LightRaw {
proj: [[f32; 4]; 4],
pos: [f32; 4],
Expand Down Expand Up @@ -132,14 +133,16 @@ impl Light {
}

#[repr(C)]
#[derive(Clone, Copy)]
struct ForwardUniforms {
proj: [[f32; 4]; 4],
num_lights: [u32; 4],
}

#[repr(C)]
#[derive(Clone, Copy)]
struct EntityUniforms {
model: [[f32; 4]; 4],
model: cgmath::Matrix4<f32>,
color: [f32; 4],
}

Expand Down Expand Up @@ -190,28 +193,25 @@ impl framework::Example for Example {
// Create the vertex and index buffers
let vertex_size = mem::size_of::<Vertex>();
let (cube_vertex_data, cube_index_data) = create_cube();
let cube_vertex_buf = Rc::new(device.create_buffer(&wgpu::BufferDescriptor {
size: (cube_vertex_data.len() * vertex_size) as u32,
usage: wgpu::BufferUsageFlags::VERTEX | wgpu::BufferUsageFlags::TRANSFER_DST,
}));
cube_vertex_buf.set_sub_data(0, framework::cast_slice(&cube_vertex_data));
let cube_index_buf = Rc::new(device.create_buffer(&wgpu::BufferDescriptor {
size: (cube_index_data.len() * 2) as u32,
usage: wgpu::BufferUsageFlags::INDEX | wgpu::BufferUsageFlags::TRANSFER_DST,
}));
cube_index_buf.set_sub_data(0, framework::cast_slice(&cube_index_data));
let cube_vertex_buf = Rc::new(
device.create_buffer_mapped(cube_vertex_data.len(), wgpu::BufferUsageFlags::VERTEX)
.fill_from_slice(&cube_vertex_data)
);

let cube_index_buf = Rc::new(
device.create_buffer_mapped(cube_index_data.len(), wgpu::BufferUsageFlags::INDEX)
.fill_from_slice(&cube_index_data)
);

let (plane_vertex_data, plane_index_data) = create_plane(7);
let plane_vertex_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: (plane_vertex_data.len() * vertex_size) as u32,
usage: wgpu::BufferUsageFlags::VERTEX | wgpu::BufferUsageFlags::TRANSFER_DST,
});
plane_vertex_buf.set_sub_data(0, framework::cast_slice(&plane_vertex_data));
let plane_index_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: (plane_index_data.len() * 2) as u32,
usage: wgpu::BufferUsageFlags::INDEX | wgpu::BufferUsageFlags::TRANSFER_DST,
});
plane_index_buf.set_sub_data(0, framework::cast_slice(&plane_index_data));
let plane_vertex_buf =
device.create_buffer_mapped(plane_vertex_data.len(), wgpu::BufferUsageFlags::VERTEX)
.fill_from_slice(&plane_vertex_data);

let plane_index_buf =
device.create_buffer_mapped(plane_index_data.len(), wgpu::BufferUsageFlags::INDEX)
.fill_from_slice(&plane_index_data);

let entity_uniform_size = mem::size_of::<EntityUniforms>() as u32;
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: entity_uniform_size,
Expand Down Expand Up @@ -517,17 +517,15 @@ impl framework::Example for Example {
],
});

let uniform_size = mem::size_of::<ForwardUniforms>() as u32;
let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: uniform_size,
usage: wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST,
});
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let data = ForwardUniforms {
let forward_uniforms = ForwardUniforms {
proj: *mx_total.as_ref(),
num_lights: [lights.len() as u32, 0, 0, 0],
};
uniform_buf.set_sub_data(0, framework::cast_slice(&[data]));
let uniform_size = mem::size_of::<ForwardUniforms>() as u32;
let uniform_buf =
device.create_buffer_mapped(1, wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST)
.fill_from_slice(&[forward_uniforms]);

// Create bind group
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
Expand Down Expand Up @@ -639,9 +637,17 @@ impl framework::Example for Example {
}

fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) {
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();
self.forward_pass.uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..]));
{
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::BufferUsageFlags::TRANSFER_SRC)
.fill_from_slice(mx_ref);

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

let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
Expand All @@ -658,28 +664,42 @@ impl framework::Example for Example {
}

fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
for entity in &mut self.entities {
if entity.rotation_speed != 0.0 {
let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
entity.mx_world = entity.mx_world * rotation;
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });

{
let size = mem::size_of::<EntityUniforms>() as u32;
let temp_buf_data =
device.create_buffer_mapped(self.entities.len(), wgpu::BufferUsageFlags::TRANSFER_SRC);

for (i, entity) in self.entities.iter_mut().enumerate() {
if entity.rotation_speed != 0.0 {
let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
entity.mx_world = entity.mx_world * rotation;
}
temp_buf_data.data[i] = EntityUniforms {
model: entity.mx_world.clone(),
color: [entity.color.r, entity.color.g, entity.color.b, entity.color.a],
};
}

let temp_buf = temp_buf_data.finish();

for (i, entity) in self.entities.iter().enumerate() {
encoder.copy_buffer_to_buffer(&temp_buf, i as u32 * size, &entity.uniform_buf, 0, size);
}
let data = EntityUniforms {
model: *entity.mx_world.as_ref(),
color: [entity.color.r, entity.color.g, entity.color.b, entity.color.a],
};
entity.uniform_buf.set_sub_data(0, framework::cast_slice(&[data]));
}

if self.lights_are_dirty {
self.lights_are_dirty = false;
let raw = self.lights
.iter()
.map(|light| light.to_raw())
.collect::<Vec<_>>();
self.light_uniform_buf.set_sub_data(0, framework::cast_slice(&raw));
let size = (self.lights.len() * mem::size_of::<LightRaw>()) as u32;
let temp_buf_data =
device.create_buffer_mapped(self.lights.len(), wgpu::BufferUsageFlags::TRANSFER_SRC);
for (i, light) in self.lights.iter().enumerate() {
temp_buf_data.data[i] = light.to_raw();
}
encoder.copy_buffer_to_buffer(&temp_buf_data.finish(), 0, &self.light_uniform_buf, 0, size);
}

let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });


for (i, light) in self.lights.iter().enumerate() {
// The light uniform buffer already has the projection,
// let's just copy it over to the shadow uniform buffer.
Expand Down
Loading

0 comments on commit 6ef53d7

Please sign in to comment.