-
Notifications
You must be signed in to change notification settings - Fork 15
/
triangle.rs
97 lines (84 loc) · 2.89 KB
/
triangle.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
mod profile_with_puffin;
use {
bytemuck::cast_slice,
inline_spirv::inline_spirv,
screen_13::prelude::*,
screen_13_window::{Window, WindowError},
std::sync::Arc,
};
// A Vulkan triangle using a graphic pipeline, vertex/fragment shaders, and index/vertex buffers.
fn main() -> Result<(), WindowError> {
pretty_env_logger::init();
profile_with_puffin::init();
let window = Window::new()?;
let triangle_pipeline = Arc::new(GraphicPipeline::create(
&window.device,
GraphicPipelineInfo::default(),
[
Shader::new_vertex(
inline_spirv!(
r#"
#version 460 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 0) out vec3 vk_Color;
void main() {
gl_Position = vec4(position, 1);
vk_Color = color;
}
"#,
vert
)
.as_slice(),
),
Shader::new_fragment(
inline_spirv!(
r#"
#version 460 core
layout(location = 0) in vec3 color;
layout(location = 0) out vec4 vk_Color;
void main() {
vk_Color = vec4(color, 1);
}
"#,
frag
)
.as_slice(),
),
],
)?);
let index_buf = Arc::new(Buffer::create_from_slice(
&window.device,
vk::BufferUsageFlags::INDEX_BUFFER,
cast_slice(&[0u16, 1, 2]),
)?);
let vertex_buf = Arc::new(Buffer::create_from_slice(
&window.device,
vk::BufferUsageFlags::VERTEX_BUFFER,
cast_slice(&[
1.0f32, 1.0, 0.0, // v1
1.0, 0.0, 0.0, // red
0.0, -1.0, 0.0, // v2
0.0, 1.0, 0.0, // green
-1.0, 1.0, 0.0, // v3
0.0, 0.0, 1.0, // blue
]),
)?);
window.run(|frame| {
let index_node = frame.render_graph.bind_node(&index_buf);
let vertex_node = frame.render_graph.bind_node(&vertex_buf);
frame
.render_graph
.begin_pass("Triangle Example")
.bind_pipeline(&triangle_pipeline)
.access_node(index_node, AccessType::IndexBuffer)
.access_node(vertex_node, AccessType::VertexBuffer)
.clear_color(0, frame.swapchain_image)
.store_color(0, frame.swapchain_image)
.record_subpass(move |subpass, _| {
subpass.bind_index_buffer(index_node, vk::IndexType::UINT16);
subpass.bind_vertex_buffer(vertex_node);
subpass.draw_indexed(3, 1, 0, 0, 0);
});
})
}