synvael/crates/renderer/src/vertex.rs

59 lines
2.2 KiB
Rust

// SPDX-License-Identifier: AGPL-3.0-only
//! Vertex data structures and layout descriptions.
use bytemuck::{Pod, Zeroable};
/// Represents a single vertex in 3D space with position and texture coordinates.
///
/// Uses `repr(C)` to ensure the memory layout matches what the GPU expects (no Rust-specific reordering).
/// `Pod` and `Zeroable` allows safely casting this struct to a raw byte slice.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
pub struct Vertex {
/// 3D position of the vertex (X, Y, Z).
pub position: [f32; 3],
/// The RGB color of the vertex [r, g, b].
pub color: [f32; 3],
}
impl Vertex {
/// Describes how Vulkan should read the vertex data from a buffer.
///
/// This defines the 'stride' (distance between vertices) and specifies that data is read per-vertex rather than per-instance.
///
/// # Panics
/// Panics if the size of the vertex structure exceeds the maximum value of a 32-bit unsigned integer.
#[expect(
clippy::expect_used,
reason = "the vertex struct size is far below u32::MAX"
)]
pub fn get_binding_description() -> ash::vk::VertexInputBindingDescription {
ash::vk::VertexInputBindingDescription::default()
.binding(0)
.stride(
u32::try_from(std::mem::size_of::<Self>()).expect("Vertex size exceeds u32 range"),
)
.input_rate(ash::vk::VertexInputRate::VERTEX)
}
/// Describes the layout of individual fields (attributes) within a single vertex.
///
/// These 'locations' must match the `layout(location = X)` qualifiers in the vertex shader.
pub fn get_attribute_descriptions() -> [ash::vk::VertexInputAttributeDescription; 2] {
[
// Location 0: position (vec3 -> R32G32B32_SFLOAT)
ash::vk::VertexInputAttributeDescription::default()
.binding(0)
.location(0)
.format(ash::vk::Format::R32G32B32_SFLOAT)
.offset(0),
ash::vk::VertexInputAttributeDescription::default()
.binding(0)
.location(1)
.format(ash::vk::Format::R32G32B32_SFLOAT)
.offset(12),
]
}
}