summary refs log tree commit diff
path: root/src/shader_data.rs
blob: bd7ff82442c32c3a4f2107b4826c59402d718f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![forbid(unsafe_code)]
use crate::linear_algebra::{ Vec2, Vec3, Mat4, Transformation };

use std::mem::size_of;
use vulkanalia::vk::{ self, HasBuilder };


pub static VERTICES: [Vertex<f32>; 8] = [
  Vertex::new(Vec3::new(-0.5, -0.5,  0.0), Vec3::new(1.0, 0.0, 0.0),
              Vec2::new(1.0, 0.0)),
  Vertex::new(Vec3::new( 0.5, -0.5,  0.0), Vec3::new(0.0, 1.0, 0.0),
              Vec2::new(0.0, 0.0)),
  Vertex::new(Vec3::new( 0.5,  0.5,  0.0), Vec3::new(0.0, 0.0, 1.0),
              Vec2::new(0.0, 1.0)),
  Vertex::new(Vec3::new(-0.5,  0.5,  0.0), Vec3::new(1.0, 1.0, 1.0),
              Vec2::new(1.0, 1.0)),
  Vertex::new(Vec3::new(-0.5, -0.5,  0.5), Vec3::new(1.0, 0.0, 0.0),
              Vec2::new(1.0, 0.0)),
  Vertex::new(Vec3::new( 0.5, -0.5,  0.5), Vec3::new(0.0, 1.0, 0.0),
              Vec2::new(0.0, 0.0)),
  Vertex::new(Vec3::new( 0.5,  0.5,  0.5), Vec3::new(0.0, 0.0, 1.0),
              Vec2::new(0.0, 1.0)),
  Vertex::new(Vec3::new(-0.5,  0.5,  0.5), Vec3::new(1.0, 1.0, 1.0),
              Vec2::new(1.0, 1.0)),
];

pub const INDICES: &[u16] = &[
  0, 1, 2, 2, 3, 0,
  4, 5, 6, 6, 7, 4,
];


#[repr(C)]
#[derive(Clone, Debug)]
pub struct Vertex<T: Copy> {
  pub position: Vec3<T>,
  pub color: Vec3<T>,
  pub texture_coordinates: Vec2<T>,
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct UniformBlock<T: Copy> {
  pub scale: Vec3<T>,
  pub model: Transformation<T>,
  pub view: Transformation<T>,
  pub projection: Mat4<T>,
}

impl<T: Copy> Vertex<T> {
  const fn new(position: Vec3<T>, color: Vec3<T>,
               texture_coordinates: Vec2<T>)
      -> Self
  {
    Self { position, color, texture_coordinates }
  }

  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; 3]
  {
    let mut offset: usize = 0;

    let position = vk::VertexInputAttributeDescription::builder()
            .binding(0)
            .location(0)
            .format(vk::Format::R32G32B32_SFLOAT)
            .offset(offset as u32)
            .build();
    offset += size_of::<Vec3<f32>>();

    let color = vk::VertexInputAttributeDescription::builder()
            .binding(0)
            .location(1)
            .format(vk::Format::R32G32B32_SFLOAT)
            .offset(offset as u32)
            .build();
    offset += size_of::<Vec3<f32>>();

    let texture_coordinates = vk::VertexInputAttributeDescription::builder()
            .binding(0)
            .location(2)
            .format(vk::Format::R32G32_SFLOAT)
            .offset(offset as u32)
            .build();

    [position, color, texture_coordinates]
  }
}