Files
wsg/lib/src/resources/vertex.rs
T
Jérôme Bousquié 7b25564483 doc
2026-07-08 15:46:50 +02:00

46 lines
2.0 KiB
Rust

//! # Vertex Module — CPU-Side Per-Attribute Tuple (GPU Contract)
//!
//! Defines `Vertex`, the CPU-side data layout that is sent to the GPU as vertex attribute buffers.
//! The struct's field order and offsets must exactly match the shader input attributes defined in
//! PipelineCache::build_pipeline() — any mismatch will corrupt GPU rendering output.
//!
//! ## Technical Points
//! - `#[repr(C)]` ensures fields are laid out contiguously without Rust padding reordering, matching C ABI.
//! - `bytemuck::Pod + bytemuck::Zeroable` enables safe `cast_slice()` conversion for GPU buffer uploads.
//! - **Performance**: The 56-byte stride per vertex is the contract between CPU data and GPU shader inputs;
//! PipelineCache::build_pipeline() reads this layout to construct VertexBufferLayout attributes array.
/// Per-vertex attribute tuple: position (3D), normal (3D), texture coordinate (2D), color (RGBA).
/// Must match the vertex buffer layout in PipelineCache::build_pipeline() byte-for-byte.
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
/// XYZ coordinates of this vertex in world space. Offset: 0 bytes (12 bytes total as [f32;3]).
pub position: [f32; 3],
/// XYZ coordinates of the vertex normal. Offset: 12 bytes (12 bytes total as [f32;3]).
pub normal: [f32; 3],
/// UV texture coordinates. Offset: 24 bytes (8 bytes total as [f32;2]).
pub uv: [f32; 2],
/// RGBA color values. Offset: 32 bytes (16 bytes total as [f32;4]).
pub color: [f32; 4],
}
impl Default for Vertex {
// Default values for stability
fn default() -> Self {
Self {
// Default position at center (0, 0)
position: [0.0, 0.0, 0.0],
// Normal pointing upward (standard for lighting calculations)
normal: [0.0, 1.0, 0.0],
// UV coordinates at the origin of the texture
uv: [0.0, 0.0],
// Opaque white color by default
color: [1.0, 1.0, 1.0, 1.0],
}
}
}