-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtriangle.rs
More file actions
115 lines (101 loc) · 3.25 KB
/
Copy pathtriangle.rs
File metadata and controls
115 lines (101 loc) · 3.25 KB
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
mod profile_with_puffin;
use {
ash::vk,
bytemuck::cast_slice,
clap::Parser,
std::sync::Arc,
vk_graph::{
cmd::{LoadOp, StoreOp},
driver::{
buffer::Buffer,
graphics::{GraphicsPipeline, GraphicsPipelineInfo},
},
},
vk_graph_window::{Window, WindowError},
vk_shader_macros::glsl,
vk_sync::AccessType,
};
// A Vulkan triangle using a graphics pipeline, vertex/fragment shaders, and index/vertex buffers
fn main() -> Result<(), WindowError> {
pretty_env_logger::init();
profile_with_puffin::init();
let args = Args::parse();
let window = Window::builder().debug(args.debug).build()?;
let triangle_pipeline = GraphicsPipeline::create(
&window.device,
GraphicsPipelineInfo::default(),
[
glsl!(
r#"
#version 460 core
#pragma shader_stage(vertex)
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;
}
"#
)
.as_slice(),
glsl!(
r#"
#version 460 core
#pragma shader_stage(fragment)
layout(location = 0) in vec3 color;
layout(location = 0) out vec4 vk_Color;
void main() {
vk_Color = vec4(color, 1);
}
"#
)
.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.graph.bind_resource(&index_buf);
let vertex_node = frame.graph.bind_resource(&vertex_buf);
frame
.graph
.begin_cmd()
.debug_name("Triangle Example")
.bind_pipeline(&triangle_pipeline)
.resource_access(index_node, AccessType::IndexBuffer)
.resource_access(vertex_node, AccessType::VertexBuffer)
.color_attachment_image(
0,
frame.swapchain_image,
LoadOp::CLEAR_BLACK_ALPHA_ZERO,
StoreOp::Store,
)
.record_cmd(move |cmd| {
cmd.bind_index_buffer(index_node, 0, vk::IndexType::UINT16)
.bind_vertex_buffer(0, vertex_node, 0)
.draw_indexed(3, 1, 0, 0, 0);
});
})
}
#[derive(Parser)]
struct Args {
/// Enable Vulkan SDK validation layers
#[arg(long)]
debug: bool,
}