Skip to main content

rusting_engine/rendering/
pipeline.rs

1use crate::geometry::VertexPosColorUv;
2use vulkano::format::Format;
3use vulkano::pipeline::graphics::vertex_input::VertexInputAttributeDescription;
4use vulkano::pipeline::graphics::vertex_input::VertexInputBindingDescription;
5use vulkano::pipeline::graphics::vertex_input::VertexInputRate;
6use vulkano::pipeline::graphics::vertex_input::VertexInputState;
7use vulkano::pipeline::GraphicsPipeline;
8
9#[repr(C)]
10#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
11pub struct UniformBufferObject {
12    pub view: [[f32; 4]; 4],   // 64 bytes
13    pub proj: [[f32; 4]; 4],   // 64 bytes
14    pub eye_pos: [f32; 3],     // 12 bytes
15    pub padding_1: f32,        // 4 bytes
16    pub light_pos: [f32; 3],   // 12 bytes
17    pub padding_2: f32,        // 4 bytes
18    pub light_color: [f32; 3], // 12 bytes
19    pub light_intensity: f32,  // 4 bytes
20}
21
22impl Default for UniformBufferObject {
23    fn default() -> Self {
24        Self {
25            view: [[0.0; 4]; 4],
26            proj: [[0.0; 4]; 4],
27            eye_pos: [0.0; 3],
28            padding_1: 0.0,
29            light_pos: [0.0, 10.0, 0.0],
30            padding_2: 0.0,
31            light_color: [1.0, 1.0, 1.0],
32            light_intensity: 50.0,
33        }
34    }
35}
36
37pub fn create_vertex_input_state() -> VertexInputState {
38    VertexInputState::new()
39        .binding(
40            0,
41            VertexInputBindingDescription {
42                stride: std::mem::size_of::<VertexPosColorUv>() as u32,
43                input_rate: VertexInputRate::Vertex,
44            },
45        )
46        .attribute(
47            0,
48            VertexInputAttributeDescription {
49                binding: 0,
50                format: Format::R32G32B32_SFLOAT,
51                offset: 0,
52            },
53        )
54        .attribute(
55            1,
56            VertexInputAttributeDescription {
57                binding: 0,
58                format: Format::R32G32B32_SFLOAT,
59                offset: 12,
60            },
61        )
62        .attribute(
63            2,
64            VertexInputAttributeDescription {
65                binding: 0,
66                format: Format::R32G32_SFLOAT,
67                offset: 24,
68            },
69        )
70}
71
72use std::sync::Arc;
73use vulkano::device::Device;
74use vulkano::pipeline::graphics::depth_stencil::DepthStencilState;
75use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
76use vulkano::pipeline::graphics::input_assembly::PrimitiveTopology;
77use vulkano::pipeline::graphics::rasterization::{CullMode, RasterizationState};
78use vulkano::pipeline::graphics::viewport::ViewportState;
79use vulkano::render_pass::RenderPass;
80use vulkano::render_pass::Subpass;
81use vulkano::shader::ShaderModule;
82use vulkano::pipeline::graphics::rasterization::FrontFace;
83
84pub fn create_pipeline(
85    vs: Arc<ShaderModule>,
86    fs: Arc<ShaderModule>,
87    render_pass: &Arc<RenderPass>,
88    device: &Arc<Device>,
89) -> std::sync::Arc<GraphicsPipeline> {
90    GraphicsPipeline::start()
91        .vertex_input_state(create_vertex_input_state())
92        .vertex_shader(vs.entry_point("main").unwrap(), ())
93        .input_assembly_state(InputAssemblyState::new().topology(PrimitiveTopology::TriangleList))
94        .viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
95        // .rasterization_state(RasterizationState::new().cull_mode(CullMode::None)) // with this think you can like disable normals to see the back of objects
96        .rasterization_state(RasterizationState::new().cull_mode(CullMode::Back).front_face(FrontFace::Clockwise)) 
97        .fragment_shader(fs.entry_point("main").unwrap(), ())
98        .depth_stencil_state(DepthStencilState::simple_depth_test())
99        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
100        .build(device.clone())
101        .unwrap()
102}