mirror of
https://github.com/FyroxEngine/Fyrox.git
synced 2026-10-12 02:02:20 +00:00
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:
@@ -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!
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"]}
|
||||
|
||||
@@ -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]);
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user