forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
18_vertex_buffer.rs.diff
148 lines (131 loc) · 5.09 KB
/
18_vertex_buffer.rs.diff
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--- a/16_swap_chain_recreation.rs
+++ b/18_vertex_buffer.rs
@@ -38,8 +38,7 @@ use vulkano::image::{ImageUsage, swapchain::SwapchainImage};
use vulkano::sync::{self, SharingMode, GpuFuture};
use vulkano::pipeline::{
GraphicsPipeline,
- vertex::BufferlessDefinition,
- vertex::BufferlessVertices,
+ GraphicsPipelineAbstract,
viewport::Viewport,
};
use vulkano::framebuffer::{
@@ -48,12 +47,16 @@ use vulkano::framebuffer::{
FramebufferAbstract,
Framebuffer,
};
-use vulkano::descriptor::PipelineLayoutAbstract;
use vulkano::command_buffer::{
AutoCommandBuffer,
AutoCommandBufferBuilder,
DynamicState,
};
+use vulkano::buffer::{
+ cpu_access::CpuAccessibleBuffer,
+ BufferUsage,
+ BufferAccess,
+};
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
@@ -89,7 +92,25 @@ impl QueueFamilyIndices {
}
}
-type ConcreteGraphicsPipeline = GraphicsPipeline<BufferlessDefinition, Box<PipelineLayoutAbstract + Send + Sync + 'static>, Arc<RenderPassAbstract + Send + Sync + 'static>>;
+#[derive(Copy, Clone)]
+struct Vertex {
+ pos: [f32; 2],
+ color: [f32; 3],
+}
+impl Vertex {
+ fn new(pos: [f32; 2], color: [f32; 3]) -> Self {
+ Self { pos, color }
+ }
+}
+impl_vertex!(Vertex, pos, color);
+
+fn vertices() -> [Vertex; 3] {
+ [
+ Vertex::new([0.0, -0.5], [1.0, 1.0, 1.0]),
+ Vertex::new([0.5, 0.5], [0.0, 1.0, 0.0]),
+ Vertex::new([-0.5, 0.5], [0.0, 0.0, 1.])
+ ]
+}
struct HelloTriangleApplication {
instance: Arc<Instance>,
@@ -109,14 +130,11 @@ struct HelloTriangleApplication {
swap_chain_images: Vec<Arc<SwapchainImage<Window>>>,
render_pass: Arc<RenderPassAbstract + Send + Sync>,
- // NOTE: We need to the full type of
- // self.graphics_pipeline, because `BufferlessVertices` only
- // works when the concrete type of the graphics pipeline is visible
- // to the command buffer.
- graphics_pipeline: Arc<ConcreteGraphicsPipeline>,
+ graphics_pipeline: Arc<GraphicsPipelineAbstract + Send + Sync>,
swap_chain_framebuffers: Vec<Arc<FramebufferAbstract + Send + Sync>>,
+ vertex_buffer: Arc<BufferAccess + Send + Sync>,
command_buffers: Vec<Arc<AutoCommandBuffer>>,
previous_frame_end: Option<Box<GpuFuture>>,
@@ -141,6 +159,8 @@ impl HelloTriangleApplication {
let swap_chain_framebuffers = Self::create_framebuffers(&swap_chain_images, &render_pass);
+ let vertex_buffer = Self::create_vertex_buffer(&device);
+
let previous_frame_end = Some(Self::create_sync_objects(&device));
let mut app = Self {
@@ -164,6 +184,7 @@ impl HelloTriangleApplication {
swap_chain_framebuffers,
+ vertex_buffer,
command_buffers: vec![],
previous_frame_end,
@@ -370,12 +391,12 @@ impl HelloTriangleApplication {
device: &Arc<Device>,
swap_chain_extent: [u32; 2],
render_pass: &Arc<RenderPassAbstract + Send + Sync>,
- ) -> Arc<ConcreteGraphicsPipeline> {
+ ) -> Arc<GraphicsPipelineAbstract + Send + Sync> {
#[allow(unused)]
mod vertex_shader {
#[derive(VulkanoShader)]
#[ty = "vertex"]
- #[path = "src/bin/09_shader_base.vert"]
+ #[path = "src/bin/17_shader_vertexbuffer.vert"]
struct Dummy;
}
@@ -383,7 +404,7 @@ impl HelloTriangleApplication {
mod fragment_shader {
#[derive(VulkanoShader)]
#[ty = "fragment"]
- #[path = "src/bin/09_shader_base.frag"]
+ #[path = "src/bin/17_shader_vertexbuffer.frag"]
struct Dummy;
}
@@ -400,7 +421,7 @@ impl HelloTriangleApplication {
};
Arc::new(GraphicsPipeline::start()
- .vertex_input(BufferlessDefinition {})
+ .vertex_input_single_buffer::<Vertex>()
.vertex_shader(vert_shader_module.main_entry_point(), ())
.triangle_list()
.primitive_restart(false)
@@ -434,17 +455,21 @@ impl HelloTriangleApplication {
).collect::<Vec<_>>()
}
+ fn create_vertex_buffer(device: &Arc<Device>) -> Arc<BufferAccess + Send + Sync> {
+ CpuAccessibleBuffer::from_iter(device.clone(),
+ BufferUsage::vertex_buffer(), vertices().iter().cloned()).unwrap()
+ }
+
fn create_command_buffers(&mut self) {
let queue_family = self.graphics_queue.family();
self.command_buffers = self.swap_chain_framebuffers.iter()
.map(|framebuffer| {
- let vertices = BufferlessVertices { vertices: 3, instances: 1 };
Arc::new(AutoCommandBufferBuilder::primary_simultaneous_use(self.device.clone(), queue_family)
.unwrap()
.begin_render_pass(framebuffer.clone(), false, vec![[0.0, 0.0, 0.0, 1.0].into()])
.unwrap()
.draw(self.graphics_pipeline.clone(), &DynamicState::none(),
- vertices, (), ())
+ vec![self.vertex_buffer.clone()], (), ())
.unwrap()
.end_render_pass()
.unwrap()