mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
Removed zerocopy and replaced it with bytemuck.
This commit is contained in:
committed by
John Mitchell
parent
bc547947ce
commit
40a19ebe66
@@ -55,7 +55,7 @@ log = "0.4"
|
||||
png = "0.15"
|
||||
winit = "0.22"
|
||||
rand = "0.7.2"
|
||||
zerocopy = "0.3"
|
||||
bytemuck = "1"
|
||||
futures = "0.3"
|
||||
|
||||
[[example]]
|
||||
|
||||
@@ -7,7 +7,6 @@ extern crate rand;
|
||||
mod framework;
|
||||
|
||||
use std::fmt::Write;
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
use wgpu::vertex_attr_array;
|
||||
|
||||
@@ -165,7 +164,7 @@ impl framework::Example for Example {
|
||||
|
||||
let vertex_buffer_data = [-0.01f32, -0.02, 0.01, -0.02, 0.00, 0.02];
|
||||
let vertices_buffer = device.create_buffer_with_data(
|
||||
vertex_buffer_data.as_bytes(),
|
||||
bytemuck::bytes_of(&vertex_buffer_data),
|
||||
wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
||||
);
|
||||
|
||||
@@ -182,7 +181,7 @@ impl framework::Example for Example {
|
||||
]
|
||||
.to_vec();
|
||||
let sim_param_buffer = device.create_buffer_with_data(
|
||||
sim_param_data.as_bytes(),
|
||||
bytemuck::cast_slice(&sim_param_data),
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
);
|
||||
|
||||
@@ -204,7 +203,7 @@ impl framework::Example for Example {
|
||||
let mut particle_bind_groups = Vec::<wgpu::BindGroup>::new();
|
||||
for _i in 0..2 {
|
||||
particle_buffers.push(device.create_buffer_with_data(
|
||||
initial_particle_data.as_bytes(),
|
||||
bytemuck::cast_slice(&initial_particle_data),
|
||||
wgpu::BufferUsage::VERTEX
|
||||
| wgpu::BufferUsage::STORAGE
|
||||
| wgpu::BufferUsage::COPY_DST,
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
#[path = "../framework.rs"]
|
||||
mod framework;
|
||||
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct Vertex {
|
||||
_pos: [f32; 4],
|
||||
_tex_coord: [f32; 2],
|
||||
}
|
||||
|
||||
unsafe impl Pod for Vertex {}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
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],
|
||||
@@ -122,11 +125,13 @@ impl framework::Example for Example {
|
||||
let vertex_size = mem::size_of::<Vertex>();
|
||||
let (vertex_data, index_data) = create_vertices();
|
||||
|
||||
let vertex_buf =
|
||||
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
|
||||
let vertex_buf = device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&vertex_data),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
let index_buf =
|
||||
device.create_buffer_with_data(index_data.as_bytes(), wgpu::BufferUsage::INDEX);
|
||||
let index_buf = device
|
||||
.create_buffer_with_data(bytemuck::cast_slice(&index_data), wgpu::BufferUsage::INDEX);
|
||||
|
||||
// Create pipeline layout
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
@@ -208,7 +213,7 @@ 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_with_data(
|
||||
mx_ref.as_bytes(),
|
||||
bytemuck::cast_slice(mx_ref),
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
);
|
||||
|
||||
@@ -318,8 +323,8 @@ 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 temp_buf =
|
||||
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let temp_buf = device
|
||||
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use std::{convert::TryInto as _, str::FromStr};
|
||||
use zerocopy::AsBytes as _;
|
||||
|
||||
async fn run() {
|
||||
let numbers = if std::env::args().len() == 1 {
|
||||
@@ -45,7 +44,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
|
||||
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
|
||||
|
||||
let staging_buffer = device.create_buffer_with_data(
|
||||
numbers.as_slice().as_bytes(),
|
||||
bytemuck::cast_slice(&numbers),
|
||||
wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::COPY_SRC,
|
||||
);
|
||||
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
#[path = "../framework.rs"]
|
||||
mod framework;
|
||||
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
|
||||
use wgpu::vertex_attr_array;
|
||||
|
||||
const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct Vertex {
|
||||
#[allow(dead_code)]
|
||||
pos: [f32; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for Vertex {}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
fn create_vertices() -> Vec<Vertex> {
|
||||
vec![
|
||||
Vertex {
|
||||
@@ -215,8 +218,10 @@ impl framework::Example for Example {
|
||||
// Create the vertex and index buffers
|
||||
let vertex_size = mem::size_of::<Vertex>();
|
||||
let vertex_data = create_vertices();
|
||||
let vertex_buf =
|
||||
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
|
||||
let vertex_buf = device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&vertex_data),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
// Create pipeline layout
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
@@ -301,7 +306,7 @@ 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_with_data(
|
||||
mx_ref.as_bytes(),
|
||||
bytemuck::cast_slice(mx_ref),
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
);
|
||||
|
||||
@@ -398,8 +403,8 @@ 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 temp_buf =
|
||||
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let temp_buf = device
|
||||
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
@@ -10,17 +10,20 @@
|
||||
#[path = "../framework.rs"]
|
||||
mod framework;
|
||||
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
|
||||
use wgpu::vertex_attr_array;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct Vertex {
|
||||
_pos: [f32; 2],
|
||||
_color: [f32; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for Vertex {}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
struct Example {
|
||||
vs_module: wgpu::ShaderModule,
|
||||
fs_module: wgpu::ShaderModule,
|
||||
@@ -157,8 +160,10 @@ impl framework::Example for Example {
|
||||
});
|
||||
}
|
||||
|
||||
let vertex_buffer =
|
||||
device.create_buffer_with_data(vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
|
||||
let vertex_buffer = device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&vertex_data),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
let vertex_count = vertex_data.len() as u32;
|
||||
|
||||
let this = Example {
|
||||
|
||||
@@ -3,18 +3,21 @@ use std::{mem, ops::Range, rc::Rc};
|
||||
#[path = "../framework.rs"]
|
||||
mod framework;
|
||||
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
|
||||
use wgpu::vertex_attr_array;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
struct Vertex {
|
||||
_pos: [i8; 4],
|
||||
_normal: [i8; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for Vertex {}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex {
|
||||
Vertex {
|
||||
_pos: [pos[0], pos[1], pos[2], 1],
|
||||
@@ -101,13 +104,16 @@ struct Light {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct LightRaw {
|
||||
proj: [[f32; 4]; 4],
|
||||
pos: [f32; 4],
|
||||
color: [f32; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for LightRaw {}
|
||||
unsafe impl Zeroable for LightRaw {}
|
||||
|
||||
impl Light {
|
||||
fn to_raw(&self) -> LightRaw {
|
||||
use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3};
|
||||
@@ -136,19 +142,25 @@ impl Light {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct ForwardUniforms {
|
||||
proj: [[f32; 4]; 4],
|
||||
num_lights: [u32; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for ForwardUniforms {}
|
||||
unsafe impl Zeroable for ForwardUniforms {}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
#[derive(Clone, Copy)]
|
||||
struct EntityUniforms {
|
||||
model: [[f32; 4]; 4],
|
||||
color: [f32; 4],
|
||||
}
|
||||
|
||||
unsafe impl Pod for EntityUniforms {}
|
||||
unsafe impl Zeroable for EntityUniforms {}
|
||||
|
||||
#[repr(C)]
|
||||
struct ShadowUniforms {
|
||||
proj: [[f32; 4]; 4],
|
||||
@@ -200,20 +212,26 @@ 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_with_data(cube_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX),
|
||||
);
|
||||
let cube_vertex_buf = Rc::new(device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&cube_vertex_data),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
));
|
||||
|
||||
let cube_index_buf = Rc::new(
|
||||
device.create_buffer_with_data(cube_index_data.as_bytes(), wgpu::BufferUsage::INDEX),
|
||||
);
|
||||
let cube_index_buf = Rc::new(device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&cube_index_data),
|
||||
wgpu::BufferUsage::INDEX,
|
||||
));
|
||||
|
||||
let (plane_vertex_data, plane_index_data) = create_plane(7);
|
||||
let plane_vertex_buf =
|
||||
device.create_buffer_with_data(plane_vertex_data.as_bytes(), wgpu::BufferUsage::VERTEX);
|
||||
let plane_vertex_buf = device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&plane_vertex_data),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
let plane_index_buf =
|
||||
device.create_buffer_with_data(plane_index_data.as_bytes(), wgpu::BufferUsage::INDEX);
|
||||
let plane_index_buf = device.create_buffer_with_data(
|
||||
bytemuck::cast_slice(&plane_index_data),
|
||||
wgpu::BufferUsage::INDEX,
|
||||
);
|
||||
|
||||
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
|
||||
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
@@ -535,7 +553,7 @@ impl framework::Example for Example {
|
||||
};
|
||||
let uniform_size = mem::size_of::<ForwardUniforms>() as wgpu::BufferAddress;
|
||||
let uniform_buf = device.create_buffer_with_data(
|
||||
forward_uniforms.as_bytes(),
|
||||
bytemuck::bytes_of(&forward_uniforms),
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
);
|
||||
|
||||
@@ -666,8 +684,8 @@ impl framework::Example for Example {
|
||||
let command_buf = {
|
||||
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_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let temp_buf = device
|
||||
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
|
||||
|
||||
let mut encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
@@ -720,18 +738,15 @@ impl framework::Example for Example {
|
||||
cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
|
||||
entity.mx_world = entity.mx_world * rotation;
|
||||
}
|
||||
slot.copy_from_slice(
|
||||
EntityUniforms {
|
||||
model: entity.mx_world.into(),
|
||||
color: [
|
||||
entity.color.r as f32,
|
||||
entity.color.g as f32,
|
||||
entity.color.b as f32,
|
||||
entity.color.a as f32,
|
||||
],
|
||||
}
|
||||
.as_bytes(),
|
||||
);
|
||||
slot.copy_from_slice(bytemuck::bytes_of(&EntityUniforms {
|
||||
model: entity.mx_world.into(),
|
||||
color: [
|
||||
entity.color.r as f32,
|
||||
entity.color.g as f32,
|
||||
entity.color.b as f32,
|
||||
entity.color.a as f32,
|
||||
],
|
||||
}));
|
||||
}
|
||||
|
||||
let temp_buf = temp_buf_data.finish();
|
||||
@@ -762,7 +777,7 @@ impl framework::Example for Example {
|
||||
.iter()
|
||||
.zip(temp_buf_data.data.chunks_exact_mut(size))
|
||||
{
|
||||
slot.copy_from_slice(light.to_raw().as_bytes());
|
||||
slot.copy_from_slice(bytemuck::bytes_of(&light.to_raw()));
|
||||
}
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&temp_buf_data.finish(),
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#[path = "../framework.rs"]
|
||||
mod framework;
|
||||
|
||||
use zerocopy::AsBytes as _;
|
||||
|
||||
const SKYBOX_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
|
||||
|
||||
type Uniform = cgmath::Matrix4<f32>;
|
||||
@@ -45,7 +43,7 @@ fn buffer_from_uniforms(
|
||||
.data
|
||||
.chunks_exact_mut(std::mem::size_of::<Uniform>()),
|
||||
) {
|
||||
slot.copy_from_slice(AsRef::<[[f32; 4]; 4]>::as_ref(u).as_bytes());
|
||||
slot.copy_from_slice(bytemuck::cast_slice(AsRef::<[[f32; 4]; 4]>::as_ref(u)));
|
||||
}
|
||||
uniform_buf.finish()
|
||||
}
|
||||
@@ -280,8 +278,8 @@ impl framework::Example for Skybox {
|
||||
let mx_total = uniforms[0] * uniforms[1];
|
||||
let mx_ref: &[f32; 16] = mx_total.as_ref();
|
||||
|
||||
let temp_buf =
|
||||
device.create_buffer_with_data(mx_ref.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let temp_buf = device
|
||||
.create_buffer_with_data(bytemuck::cast_slice(mx_ref), wgpu::BufferUsage::COPY_SRC);
|
||||
|
||||
let mut init_encoder =
|
||||
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
@@ -1443,9 +1443,9 @@ impl<'a> RenderPass<'a> {
|
||||
/// Draws primitives from the active vertex buffer(s) based on the contents of the `indirect_buffer`.
|
||||
///
|
||||
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
|
||||
///
|
||||
///
|
||||
/// The structure expected in `indirect_buffer` is the following:
|
||||
///
|
||||
///
|
||||
/// ```rust
|
||||
/// #[repr(C)]
|
||||
/// struct DrawIndirect {
|
||||
@@ -1470,9 +1470,9 @@ impl<'a> RenderPass<'a> {
|
||||
///
|
||||
/// The active index buffer can be set with [`RenderPass::set_index_buffer`], while the active
|
||||
/// vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
|
||||
///
|
||||
///
|
||||
/// The structure expected in `indirect_buffer` is the following:
|
||||
///
|
||||
///
|
||||
/// ```rust
|
||||
/// #[repr(C)]
|
||||
/// struct DrawIndexedIndirect {
|
||||
|
||||
Reference in New Issue
Block a user