chore(630): added Pod trait to array_as_u8_slice and value_as_u8_slice. Updated all related structs to also derive Pod and Zeroable.

This commit is contained in:
NathanaelG1
2024-07-04 14:42:02 -04:00
parent 474e3b01a8
commit f1750b0907
14 changed files with 38 additions and 19 deletions
+2 -1
View File
@@ -4,10 +4,11 @@ use crate::{
uuid_provider,
visitor::{Visit, VisitResult, Visitor},
};
use bytemuck::{Pod, Zeroable};
use num_traits::Zero;
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq, Visit, Reflect)]
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq, Visit, Reflect, Pod, Zeroable)]
#[repr(C)]
pub struct Color {
// Do not change order! OpenGL requires this order!
+3 -3
View File
@@ -298,7 +298,7 @@ pub fn make_relative_path<P: AsRef<Path>>(path: P) -> Result<PathBuf, std::io::E
}
/// "Transmutes" array of any sized type to a slice of bytes.
pub fn array_as_u8_slice<T: Sized>(v: &[T]) -> &'_ [u8] {
pub fn array_as_u8_slice<T: Sized + Pod>(v: &[T]) -> &'_ [u8] {
// SAFETY: It is safe to reinterpret data to read it.
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, std::mem::size_of_val(v)) }
}
@@ -315,7 +315,7 @@ pub fn transmute_slice<T: Sized, U: Sized>(v: &[T]) -> &'_ [U] {
}
/// "Transmutes" value of any sized type to a slice of bytes.
pub fn value_as_u8_slice<T: Sized>(v: &T) -> &'_ [u8] {
pub fn value_as_u8_slice<T: Sized + Pod>(v: &T) -> &'_ [u8] {
// SAFETY: It is safe to reinterpret data to read it.
unsafe { std::slice::from_raw_parts(v as *const T as *const u8, std::mem::size_of::<T>()) }
}
@@ -333,7 +333,7 @@ pub fn transmute_vec_as_bytes<T: Pod>(vec: Vec<T>) -> Vec<u8> {
}
/// Performs hashing of a sized value by interpreting it as raw memory.
pub fn hash_as_bytes<T: Sized, H: Hasher>(value: &T, hasher: &mut H) {
pub fn hash_as_bytes<T: Sized + Pod, H: Hasher>(value: &T, hasher: &mut H) {
hasher.write(value_as_u8_slice(value))
}
@@ -20,8 +20,10 @@ use crate::{
renderer::RenderPassStatistics,
scene::{camera::Camera, debug::SceneDrawingContext},
};
use bytemuck::{Pod, Zeroable};
#[repr(C)]
#[derive(Copy, Pod, Zeroable, Clone)]
struct Vertex {
position: Vector3<f32>,
color: u32,
@@ -309,7 +309,12 @@ impl GeometryBuffer {
Ok(geometry_buffer)
}
pub fn set_buffer_data<T>(&mut self, state: &PipelineState, buffer: usize, data: &[T]) {
pub fn set_buffer_data<T: bytemuck::Pod>(
&mut self,
state: &PipelineState,
buffer: usize,
data: &[T],
) {
scope_profile!();
let buffer = &mut self.buffers[buffer];
+2 -1
View File
@@ -28,6 +28,7 @@ use crate::{
node::{Node, NodeTrait},
},
};
use bytemuck::{Pod, Zeroable};
use fyrox_core::value_as_u8_slice;
use fyrox_graph::BaseSceneGraph;
use std::{
@@ -36,7 +37,7 @@ use std::{
};
/// A vertex for static meshes.
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, Pod, Zeroable)]
#[repr(C)] // OpenGL expects this structure packed as in C
pub struct RectangleVertex {
/// Position of vertex in local coordinates.
+6 -5
View File
@@ -12,6 +12,7 @@ use crate::{
},
core::{array_as_u8_slice, value_as_u8_slice},
};
use bytemuck::Pod;
use fxhash::FxHasher;
use std::{
alloc::Layout,
@@ -423,7 +424,7 @@ impl<'a> VertexBufferRefMut<'a> {
/// the type does not have any custom destructors.
pub fn push_vertex<T>(&mut self, vertex: &T) -> Result<(), ValidationError>
where
T: VertexTrait,
T: VertexTrait + bytemuck::Pod,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
self.vertex_buffer
@@ -448,7 +449,7 @@ impl<'a> VertexBufferRefMut<'a> {
/// the type does not have any custom destructors.
pub fn push_vertices<T>(&mut self, vertices: &[T]) -> Result<(), ValidationError>
where
T: VertexTrait,
T: VertexTrait + Pod,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
self.vertex_buffer
@@ -491,7 +492,7 @@ impl<'a> VertexBufferRefMut<'a> {
vertices: impl Iterator<Item = T>,
) -> Result<(), ValidationError>
where
T: VertexTrait,
T: VertexTrait + Pod,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
for vertex in vertices {
@@ -523,7 +524,7 @@ impl<'a> VertexBufferRefMut<'a> {
mut transformer: F,
) -> Result<(), ValidationError>
where
T: VertexTrait,
T: VertexTrait + Pod,
F: FnMut(&T) -> T,
{
if std::mem::size_of::<T>() == self.vertex_buffer.vertex_size as usize {
@@ -666,7 +667,7 @@ impl<'a> VertexBufferRefMut<'a> {
fill_value: T,
) -> Result<(), ValidationError>
where
T: Copy,
T: Copy + Pod,
{
if self.vertex_buffer.sparse_layout[descriptor.usage as usize].is_some() {
Err(ValidationError::DuplicatedAttributeDescriptor)
+2 -1
View File
@@ -9,10 +9,11 @@ use crate::{
VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage, VertexTrait,
},
};
use bytemuck::{Pod, Zeroable};
use std::hash::{Hash, Hasher};
/// A vertex for static meshes.
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, Pod, Zeroable)]
#[repr(C)] // OpenGL expects this structure packed as in C
pub struct StaticVertex {
/// Position of vertex in local coordinates.
+2 -1
View File
@@ -7,10 +7,11 @@ use crate::{
VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage, VertexTrait,
},
};
use bytemuck::{Pod, Zeroable};
/// OpenGL expects this structure packed as in C.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
pub struct Vertex {
pub position: Vector3<f32>,
pub tex_coord: Vector2<f32>,
+2 -1
View File
@@ -31,12 +31,13 @@ use crate::{
node::{Node, NodeTrait},
},
};
use bytemuck::{Pod, Zeroable};
use fyrox_core::value_as_u8_slice;
use fyrox_graph::BaseSceneGraph;
use std::ops::{Deref, DerefMut};
/// A vertex for sprites.
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, Pod, Zeroable)]
#[repr(C)] // OpenGL expects this structure packed as in C
pub struct SpriteVertex {
/// Position of vertex in local coordinates.
+3 -1
View File
@@ -8,9 +8,9 @@ use crate::{
core::hash_as_bytes,
core::{algebra::Vector3, math::TriangleDefinition},
};
use bytemuck::{Pod, Zeroable};
use fxhash::{FxBuildHasher, FxHashSet};
use std::hash::{Hash, Hasher};
#[derive(Copy, Clone)]
struct IndexedStorage<T> {
index: u32,
@@ -18,6 +18,8 @@ struct IndexedStorage<T> {
}
/// Raw vertex is just a point in 3d space that supports hashing.
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct RawVertex {
/// An X component.
pub x: f32,
+3 -2
View File
@@ -15,7 +15,8 @@ rust-version = "1.72"
[dependencies]
rectutils = "0.2.0"
nalgebra = "0.32.3"
nalgebra = { version = "0.32.3", features = ["bytemuck"] }
arrayvec = "0.7.4"
num-traits = "0.2.18"
uuid = { version = "1", features = ["v4"] }
uuid = { version = "1", features = ["v4"] }
bytemuck = { version = "1.16.1", features = ["derive"]}
+2 -1
View File
@@ -11,6 +11,7 @@ pub mod segment;
pub mod triangulator;
use crate::ray::IntersectionResult;
use bytemuck::{Pod, Zeroable};
use nalgebra::{
Matrix3, Matrix4, RealField, Scalar, SimdRealField, UnitQuaternion, Vector2, Vector3,
};
@@ -420,7 +421,7 @@ impl Hash for TriangleEdge {
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash, Pod, Zeroable)]
#[repr(C)]
pub struct TriangleDefinition(pub [u32; 3]);
+1
View File
@@ -27,6 +27,7 @@ fxhash = "0.2.1"
strum = "0.26.1"
strum_macros = "0.26.1"
serde = { version = "1", features = ["derive"] }
bytemuck = { version = "1.16.1", features = ["derive"]}
[features]
enable_profiler = ["fyrox-core/enable_profiler"]
+2 -1
View File
@@ -10,10 +10,11 @@ use crate::{
formatted_text::FormattedText,
Thickness,
};
use bytemuck::{Pod, Zeroable};
use fyrox_resource::untyped::UntypedResource;
use std::ops::Range;
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct Vertex {
pub pos: Vector2<f32>,