-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
127 lines (116 loc) · 4.01 KB
/
main.rs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use cinder::{
App, AttachmentType, Buffer, BufferDescription, BufferUsage, Bump, Cinder, GraphicsPipeline,
InitContext, RenderGraph, RenderPass, Renderer,
};
use math::{mat::Mat4, vec::Vec3};
use util::{SdlContext, WindowDescription};
pub const WINDOW_WIDTH: u32 = 1280;
pub const WINDOW_HEIGHT: u32 = 1280;
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/gen/triangle_shader_structs.rs"
));
pub struct HelloTriangle {
pipeline: GraphicsPipeline,
vertex_buffer: Buffer,
index_buffer: Buffer,
}
impl App for HelloTriangle {
fn new(context: InitContext<'_>) -> anyhow::Result<Self> {
let vertex_shader = context.renderer.device.create_shader(
include_bytes!("../shaders/spv/triangle.vert.spv"),
Default::default(),
)?;
let fragment_shader = context.renderer.device.create_shader(
include_bytes!("../shaders/spv/triangle.frag.spv"),
Default::default(),
)?;
let pipeline = context.renderer.device.create_graphics_pipeline(
&vertex_shader,
Some(&fragment_shader),
Default::default(),
)?;
let vertex_buffer = context.renderer.device.create_buffer_with_data(
&[
TriangleVertex {
i_pos: [0.0, 0.5],
i_color: [1.0, 0.0, 0.0, 1.0],
},
TriangleVertex {
i_pos: [-0.5, -0.5],
i_color: [0.0, 1.0, 0.0, 1.0],
},
TriangleVertex {
i_pos: [0.5, -0.5],
i_color: [0.0, 0.0, 1.0, 1.0],
},
],
BufferDescription {
usage: BufferUsage::VERTEX,
..Default::default()
},
)?;
let index_buffer = context.renderer.device.create_buffer_with_data(
&[0, 1, 2],
BufferDescription {
usage: BufferUsage::INDEX,
..Default::default()
},
)?;
vertex_shader.destroy(&context.renderer.device);
fragment_shader.destroy(&context.renderer.device);
Ok(Self {
pipeline,
vertex_buffer,
index_buffer,
})
}
fn draw<'a>(
&'a mut self,
allocator: &'a Bump,
graph: &mut RenderGraph<'a>,
) -> anyhow::Result<()> {
graph.add_pass(
allocator,
RenderPass::new(allocator)
.add_color_attachment(AttachmentType::SwapchainImage, Default::default())
.set_callback(allocator, |cinder, cmd_list| {
cmd_list.bind_graphics_pipeline(&cinder.device, &self.pipeline);
cmd_list.bind_index_buffer(&cinder.device, &self.index_buffer);
cmd_list.bind_vertex_buffer(&cinder.device, &self.vertex_buffer);
cmd_list.set_vertex_bytes(
&cinder.device,
&self.pipeline,
&Mat4::rotate(
(cinder.init_time().elapsed().as_secs_f32() / 5.0)
* (2.0 * std::f32::consts::PI),
Vec3::new(0.0, 0.0, 1.0),
),
0,
)?;
cmd_list.draw_offset(&cinder.device, 3, 0, 0);
Ok(())
}),
);
Ok(())
}
fn cleanup(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
self.index_buffer.destroy(&renderer.device);
self.vertex_buffer.destroy(&renderer.device);
self.pipeline.destroy(&renderer.device);
Ok(())
}
}
fn main() {
let mut sdl = SdlContext::new(
WINDOW_WIDTH,
WINDOW_HEIGHT,
WindowDescription {
title: "hello-triangle",
..Default::default()
},
)
.unwrap();
let mut cinder = Cinder::<HelloTriangle>::new(&sdl.window).unwrap();
cinder.run_game_loop(&mut sdl).unwrap();
}