551: Replace unsafe impl Pod with safe derive r=kvark a=rukai

bytemuck now includes derives that will implement the Pod trait, failing to compile if the struct cannot safely be a Pod.
Lets use it to remove most of the unsafe usage from the examples.
closes https://github.com/gfx-rs/wgpu-rs/issues/190

Co-authored-by: Rukai <rubickent@gmail.com>
This commit is contained in:
bors[bot]
2020-09-08 13:40:45 +00:00
committed by GitHub
8 changed files with 16 additions and 56 deletions

View File

@@ -56,7 +56,7 @@ log = "0.4"
png = "0.16"
winit = { version = "0.22.1", features = ["web-sys"] }
rand = { version = "0.7.2", features = ["wasm-bindgen"] }
bytemuck = "1"
bytemuck = { version = "1.4", features = ["derive"] }
noise = "0.6"
ddsfile = "0.4"
futures = { version = "0.3", default-features = false, features = ["std", "executor"] }

View File

@@ -5,15 +5,12 @@ use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
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],

View File

@@ -8,15 +8,12 @@ use wgpu::util::DeviceExt;
const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
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 {

View File

@@ -16,15 +16,12 @@ use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Vertex {
_pos: [f32; 2],
_color: [f32; 4],
}
unsafe impl Pod for Vertex {}
unsafe impl Zeroable for Vertex {}
struct Example {
bundle: wgpu::RenderBundle,
vs_module: wgpu::ShaderModule,

View File

@@ -7,16 +7,12 @@ use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
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],
@@ -102,16 +98,13 @@ struct Light {
}
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
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};
@@ -140,25 +133,19 @@ impl Light {
}
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
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)]
#[derive(Clone, Copy, Pod, Zeroable)]
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],

View File

@@ -6,25 +6,19 @@ use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Vertex {
_pos: [f32; 2],
_tex_coord: [f32; 2],
_index: u32,
}
unsafe impl Pod for Vertex {}
unsafe impl Zeroable for Vertex {}
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Uniform {
index: u32,
}
unsafe impl Pod for Uniform {}
unsafe impl Zeroable for Uniform {}
fn vertex(pos: [i8; 2], tc: [i8; 2], index: i8) -> Vertex {
Vertex {
_pos: [pos[0] as f32, pos[1] as f32],

View File

@@ -6,6 +6,7 @@ mod point_gen;
use cgmath::Point3;
use std::{iter, mem};
use wgpu::util::DeviceExt;
use bytemuck::{Pod, Zeroable};
///
/// Radius of the terrain.
@@ -34,17 +35,14 @@ struct Matrices {
}
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
struct TerrainUniforms {
view_projection: [f32; 16],
clipping_plane: [f32; 4],
}
unsafe impl bytemuck::Zeroable for TerrainUniforms {}
unsafe impl bytemuck::Pod for TerrainUniforms {}
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
struct WaterUniforms {
view: [f32; 16],
projection: [f32; 16],
@@ -58,9 +56,6 @@ struct Uniforms {
water: WaterUniforms,
}
unsafe impl bytemuck::Zeroable for WaterUniforms {}
unsafe impl bytemuck::Pod for WaterUniforms {}
struct Example {
water_vertex_buf: wgpu::Buffer,
water_vertex_count: usize,

View File

@@ -3,6 +3,7 @@
//!
use cgmath::{InnerSpace, Point3, Vector3};
use bytemuck::{Pod, Zeroable};
use std::collections::HashMap;
// The following constants are used in calculations.
@@ -32,28 +33,20 @@ const C45: f32 = S45;
const SQRT_3: f32 = 1.73205080757;
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
pub struct TerrainVertexAttributes {
position: [f32; 3],
normal: [f32; 3],
colour: [u8; 4],
}
unsafe impl bytemuck::Pod for TerrainVertexAttributes {}
unsafe impl bytemuck::Zeroable for TerrainVertexAttributes {}
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
pub struct WaterVertexAttributes {
position: [i16; 2],
offsets: [i8; 4],
}
unsafe impl bytemuck::Pod for WaterVertexAttributes {}
unsafe impl bytemuck::Zeroable for WaterVertexAttributes {}
///
/// Represents the center of a single hexagon.
///