diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-07-09 21:26:44 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-07-09 21:26:44 -0700 |
| commit | cd3b77fea9307d9c15718a9e5e17035f7e71ac25 (patch) | |
| tree | f6cc1020ba2e871408d93bd45f51cfa3eb9b1809 /src/linear_algebra.rs | |
| parent | ee29403c132f4ba0babf27639f2a63299de7edca (diff) | |
allocate and use a vertex buffer
kinda cool how the driver interfaces with the kernel to map memory on our behalf Force-Push: yes Change-Id: I906f68b59dcedc1fd6d9bfc52c9b299abc9cd828
Diffstat (limited to 'src/linear_algebra.rs')
| -rw-r--r-- | src/linear_algebra.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/linear_algebra.rs b/src/linear_algebra.rs new file mode 100644 index 0000000..950877b --- /dev/null +++ b/src/linear_algebra.rs @@ -0,0 +1,62 @@ +#![forbid(unsafe_code)] +use std::mem::size_of; +use vulkanalia::vk::{ self, HasBuilder }; + + +pub static VERTICES: [Vertex<f32>; 3] = [ + Vertex::new(Vec2( 0.0, -0.5), Vec3(1.0, 0.0, 0.0)), + Vertex::new(Vec2( 0.5, 0.5), Vec3(0.0, 1.0, 0.0)), + Vertex::new(Vec2(-0.5, 0.5), Vec3(0.0, 0.0, 1.0)), +]; + + +#[derive(Clone, Debug)] +pub struct Vec2<T>(T, T); +#[derive(Clone, Debug)] +pub struct Vec3<T>(T, T, T); +#[derive(Clone, Debug)] +#[allow(unused)] +pub struct Vec4<T>(T, T, T, T); + + +#[repr(C)] +#[derive(Clone, Debug)] +pub struct Vertex<T> { + pub position: Vec2<T>, + pub color: Vec3<T>, +} + +impl<T> Vertex<T> { + const fn new(position: Vec2<T>, color: Vec3<T>) -> Self { + Self { position, color } + } + + pub fn binding_description() -> vk::VertexInputBindingDescription { + vk::VertexInputBindingDescription::builder() + .binding(0) + .stride(size_of::<Vertex<f32>>() as u32) + .input_rate(vk::VertexInputRate::VERTEX) + .build() + } + + pub fn attribute_descriptions() + -> [vk::VertexInputAttributeDescription; 2] + { + let position = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(0) + .format(vk::Format::R32G32_SFLOAT) + .offset(0) + .build(); + + let color = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(1) + .format(vk::Format::R32G32B32_SFLOAT) + .offset(size_of::<Vec2<f32>>() as u32) + .build(); + + [position, color] + } +} + |