2018-08-30 01:37:51 +00:00
|
|
|
// Copyright (c) 2017 The vulkano developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or
|
2020-11-10 17:03:50 +00:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
|
|
|
|
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// at your option. All files in the project carrying such
|
|
|
|
// notice may not be copied, modified, or distributed except
|
|
|
|
// according to those terms.
|
|
|
|
|
2022-03-06 19:30:49 +00:00
|
|
|
use bytemuck::{Pod, Zeroable};
|
2021-08-16 19:10:07 +00:00
|
|
|
use std::sync::Arc;
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
|
|
|
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
|
2022-05-29 16:53:36 +00:00
|
|
|
command_buffer::{
|
|
|
|
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
|
|
|
|
SecondaryAutoCommandBuffer,
|
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
device::Queue,
|
|
|
|
impl_vertex,
|
|
|
|
pipeline::{
|
|
|
|
graphics::{
|
|
|
|
depth_stencil::DepthStencilState,
|
|
|
|
input_assembly::InputAssemblyState,
|
|
|
|
vertex_input::BuffersDefinition,
|
|
|
|
viewport::{Viewport, ViewportState},
|
|
|
|
},
|
|
|
|
GraphicsPipeline,
|
|
|
|
},
|
|
|
|
render_pass::Subpass,
|
2021-04-26 14:53:18 +00:00
|
|
|
};
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
pub struct TriangleDrawSystem {
|
|
|
|
gfx_queue: Arc<Queue>,
|
|
|
|
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
|
2022-05-29 16:53:36 +00:00
|
|
|
subpass: Subpass,
|
2021-08-16 19:10:07 +00:00
|
|
|
pipeline: Arc<GraphicsPipeline>,
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TriangleDrawSystem {
|
|
|
|
/// Initializes a triangle drawing system.
|
2021-04-10 11:09:03 +00:00
|
|
|
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> TriangleDrawSystem {
|
2022-03-06 19:30:49 +00:00
|
|
|
let vertices = [
|
|
|
|
Vertex {
|
|
|
|
position: [-0.5, -0.25],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [0.0, 0.5],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [0.25, -0.1],
|
|
|
|
},
|
|
|
|
];
|
2018-08-30 01:37:51 +00:00
|
|
|
let vertex_buffer = {
|
2020-05-10 00:36:20 +00:00
|
|
|
CpuAccessibleBuffer::from_iter(
|
|
|
|
gfx_queue.device().clone(),
|
2022-09-05 20:16:40 +00:00
|
|
|
BufferUsage {
|
|
|
|
vertex_buffer: true,
|
|
|
|
..BufferUsage::empty()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
false,
|
2022-03-06 19:30:49 +00:00
|
|
|
vertices,
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.expect("failed to create buffer")
|
2018-08-30 01:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let pipeline = {
|
2021-11-13 15:06:16 +00:00
|
|
|
let vs = vs::load(gfx_queue.device().clone()).expect("failed to create shader module");
|
|
|
|
let fs = fs::load(gfx_queue.device().clone()).expect("failed to create shader module");
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2021-11-02 20:33:58 +00:00
|
|
|
GraphicsPipeline::start()
|
2021-12-05 20:30:56 +00:00
|
|
|
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
|
2021-11-13 15:06:16 +00:00
|
|
|
.vertex_shader(vs.entry_point("main").unwrap(), ())
|
2021-11-02 20:33:58 +00:00
|
|
|
.input_assembly_state(InputAssemblyState::new())
|
|
|
|
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
|
2021-11-13 15:06:16 +00:00
|
|
|
.fragment_shader(fs.entry_point("main").unwrap(), ())
|
2021-11-02 20:33:58 +00:00
|
|
|
.depth_stencil_state(DepthStencilState::simple_depth_test())
|
2022-05-29 16:53:36 +00:00
|
|
|
.render_pass(subpass.clone())
|
2021-11-02 20:33:58 +00:00
|
|
|
.build(gfx_queue.device().clone())
|
|
|
|
.unwrap()
|
2018-08-30 01:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TriangleDrawSystem {
|
2022-03-06 19:30:49 +00:00
|
|
|
gfx_queue,
|
|
|
|
vertex_buffer,
|
2022-05-29 16:53:36 +00:00
|
|
|
subpass,
|
2022-03-06 19:30:49 +00:00
|
|
|
pipeline,
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a secondary command buffer that draws the triangle on the current subpass.
|
2021-04-05 11:25:20 +00:00
|
|
|
pub fn draw(&self, viewport_dimensions: [u32; 2]) -> SecondaryAutoCommandBuffer {
|
2022-05-29 16:53:36 +00:00
|
|
|
let mut builder = AutoCommandBufferBuilder::secondary(
|
2020-05-10 00:36:20 +00:00
|
|
|
self.gfx_queue.device().clone(),
|
2022-09-10 06:00:08 +00:00
|
|
|
self.gfx_queue.queue_family_index(),
|
2021-04-26 14:53:18 +00:00
|
|
|
CommandBufferUsage::MultipleSubmit,
|
2022-05-29 16:53:36 +00:00
|
|
|
CommandBufferInheritanceInfo {
|
|
|
|
render_pass: Some(self.subpass.clone().into()),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
|
|
|
builder
|
2021-08-27 06:24:16 +00:00
|
|
|
.set_viewport(
|
2021-08-12 14:14:02 +00:00
|
|
|
0,
|
2021-08-27 06:24:16 +00:00
|
|
|
[Viewport {
|
|
|
|
origin: [0.0, 0.0],
|
|
|
|
dimensions: [viewport_dimensions[0] as f32, viewport_dimensions[1] as f32],
|
|
|
|
depth_range: 0.0..1.0,
|
|
|
|
}],
|
2020-06-01 14:41:42 +00:00
|
|
|
)
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_pipeline_graphics(self.pipeline.clone())
|
|
|
|
.bind_vertex_buffers(0, self.vertex_buffer.clone())
|
|
|
|
.draw(self.vertex_buffer.len() as u32, 1, 0, 0)
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
|
|
|
builder.build().unwrap()
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:19:57 +00:00
|
|
|
#[repr(C)]
|
2022-03-06 19:30:49 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
|
2018-08-30 01:37:51 +00:00
|
|
|
struct Vertex {
|
2020-05-10 00:36:20 +00:00
|
|
|
position: [f32; 2],
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
2022-03-06 19:30:49 +00:00
|
|
|
impl_vertex!(Vertex, position);
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2018-10-27 23:10:29 +00:00
|
|
|
mod vs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2018-10-27 23:10:29 +00:00
|
|
|
ty: "vertex",
|
|
|
|
src: "
|
2018-08-30 01:37:51 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec2 position;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(position, 0.0, 1.0);
|
2018-10-26 00:15:33 +00:00
|
|
|
}"
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 23:10:29 +00:00
|
|
|
mod fs {
|
2020-05-10 00:36:20 +00:00
|
|
|
vulkano_shaders::shader! {
|
2018-10-27 23:10:29 +00:00
|
|
|
ty: "fragment",
|
|
|
|
src: "
|
2018-08-30 01:37:51 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
layout(location = 1) out vec3 f_normal;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
f_color = vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
f_normal = vec3(0.0, 0.0, 1.0);
|
2018-10-26 00:15:33 +00:00
|
|
|
}"
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|