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-10-27 18:59:47 +00:00
|
|
|
use std::sync::Arc;
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
2023-04-02 13:07:16 +00:00
|
|
|
buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer},
|
2022-05-29 16:53:36 +00:00
|
|
|
command_buffer::{
|
2022-10-05 09:09:26 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
|
|
|
|
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
|
2022-05-29 16:53:36 +00:00
|
|
|
},
|
2022-03-06 19:30:49 +00:00
|
|
|
device::Queue,
|
2023-07-19 09:11:17 +00:00
|
|
|
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
|
2022-03-06 19:30:49 +00:00
|
|
|
pipeline::{
|
|
|
|
graphics::{
|
2023-04-18 18:53:08 +00:00
|
|
|
color_blend::ColorBlendState,
|
2022-03-06 19:30:49 +00:00
|
|
|
depth_stencil::DepthStencilState,
|
|
|
|
input_assembly::InputAssemblyState,
|
2023-04-18 18:53:08 +00:00
|
|
|
multisample::MultisampleState,
|
|
|
|
rasterization::RasterizationState,
|
2023-04-19 15:45:27 +00:00
|
|
|
vertex_input::{Vertex, VertexDefinition},
|
2022-03-06 19:30:49 +00:00
|
|
|
viewport::{Viewport, ViewportState},
|
2023-04-22 18:46:22 +00:00
|
|
|
GraphicsPipelineCreateInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2023-04-22 18:46:22 +00:00
|
|
|
layout::PipelineDescriptorSetLayoutCreateInfo,
|
2023-06-25 18:08:27 +00:00
|
|
|
GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
|
|
|
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>,
|
2023-01-12 12:56:10 +00:00
|
|
|
vertex_buffer: Subbuffer<[TriangleVertex]>,
|
2022-05-29 16:53:36 +00:00
|
|
|
subpass: Subpass,
|
2021-08-16 19:10:07 +00:00
|
|
|
pipeline: Arc<GraphicsPipeline>,
|
2022-10-27 18:59:47 +00:00
|
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TriangleDrawSystem {
|
|
|
|
/// Initializes a triangle drawing system.
|
2022-10-05 09:09:26 +00:00
|
|
|
pub fn new(
|
|
|
|
gfx_queue: Arc<Queue>,
|
|
|
|
subpass: Subpass,
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator: Arc<StandardMemoryAllocator>,
|
2022-10-27 18:59:47 +00:00
|
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
2022-10-05 09:09:26 +00:00
|
|
|
) -> TriangleDrawSystem {
|
2022-03-06 19:30:49 +00:00
|
|
|
let vertices = [
|
2022-12-28 10:23:36 +00:00
|
|
|
TriangleVertex {
|
2022-03-06 19:30:49 +00:00
|
|
|
position: [-0.5, -0.25],
|
|
|
|
},
|
2022-12-28 10:23:36 +00:00
|
|
|
TriangleVertex {
|
2022-03-06 19:30:49 +00:00
|
|
|
position: [0.0, 0.5],
|
|
|
|
},
|
2022-12-28 10:23:36 +00:00
|
|
|
TriangleVertex {
|
2022-03-06 19:30:49 +00:00
|
|
|
position: [0.25, -0.1],
|
|
|
|
},
|
|
|
|
];
|
2023-01-12 12:56:10 +00:00
|
|
|
let vertex_buffer = Buffer::from_iter(
|
|
|
|
memory_allocator,
|
2023-04-02 13:07:16 +00:00
|
|
|
BufferCreateInfo {
|
|
|
|
usage: BufferUsage::VERTEX_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo {
|
2023-07-19 09:11:17 +00:00
|
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
|
|
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
2023-01-12 12:56:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
vertices,
|
|
|
|
)
|
|
|
|
.expect("failed to create buffer");
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
let pipeline = {
|
2023-04-22 18:46:22 +00:00
|
|
|
let device = gfx_queue.device();
|
|
|
|
let vs = vs::load(device.clone())
|
2023-04-18 18:53:08 +00:00
|
|
|
.expect("failed to create shader module")
|
|
|
|
.entry_point("main")
|
|
|
|
.expect("shader entry point not found");
|
2023-04-22 18:46:22 +00:00
|
|
|
let fs = fs::load(device.clone())
|
2023-04-18 18:53:08 +00:00
|
|
|
.expect("failed to create shader module")
|
|
|
|
.entry_point("main")
|
|
|
|
.expect("shader entry point not found");
|
2023-04-19 15:45:27 +00:00
|
|
|
let vertex_input_state = TriangleVertex::per_vertex()
|
|
|
|
.definition(&vs.info().input_interface)
|
|
|
|
.unwrap();
|
2023-04-22 18:46:22 +00:00
|
|
|
let stages = [
|
2023-06-25 18:08:27 +00:00
|
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
|
|
PipelineShaderStageCreateInfo::new(fs),
|
2023-04-22 18:46:22 +00:00
|
|
|
];
|
|
|
|
let layout = PipelineLayout::new(
|
|
|
|
device.clone(),
|
|
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
|
|
.into_pipeline_layout_create_info(device.clone())
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-03 20:37:29 +00:00
|
|
|
|
2023-04-22 18:46:22 +00:00
|
|
|
GraphicsPipeline::new(
|
|
|
|
device.clone(),
|
|
|
|
None,
|
|
|
|
GraphicsPipelineCreateInfo {
|
|
|
|
stages: stages.into_iter().collect(),
|
|
|
|
vertex_input_state: Some(vertex_input_state),
|
|
|
|
input_assembly_state: Some(InputAssemblyState::default()),
|
|
|
|
viewport_state: Some(ViewportState::viewport_dynamic_scissor_irrelevant()),
|
|
|
|
rasterization_state: Some(RasterizationState::default()),
|
|
|
|
depth_stencil_state: Some(DepthStencilState::simple_depth_test()),
|
|
|
|
multisample_state: Some(MultisampleState::default()),
|
|
|
|
color_blend_state: Some(ColorBlendState::new(subpass.num_color_attachments())),
|
|
|
|
subpass: Some(subpass.clone().into()),
|
|
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.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,
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator,
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a secondary command buffer that draws the triangle on the current subpass.
|
2023-05-18 11:03:37 +00:00
|
|
|
pub fn draw(&self, viewport_dimensions: [u32; 2]) -> Arc<SecondaryAutoCommandBuffer> {
|
2022-05-29 16:53:36 +00:00
|
|
|
let mut builder = AutoCommandBufferBuilder::secondary(
|
2023-05-18 11:03:37 +00:00
|
|
|
self.command_buffer_allocator.as_ref(),
|
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 {
|
2023-06-25 18:08:27 +00:00
|
|
|
offset: [0.0, 0.0],
|
|
|
|
extent: [viewport_dimensions[0] as f32, viewport_dimensions[1] as f32],
|
|
|
|
depth_range: 0.0..=1.0,
|
2023-05-18 11:03:37 +00:00
|
|
|
}]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
2020-06-01 14:41:42 +00:00
|
|
|
)
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_pipeline_graphics(self.pipeline.clone())
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.bind_vertex_buffers(0, self.vertex_buffer.clone())
|
2023-08-04 18:55:16 +00:00
|
|
|
.unwrap()
|
2021-08-27 06:24:16 +00:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
#[derive(BufferContents, Vertex)]
|
2021-11-24 14:19:57 +00:00
|
|
|
#[repr(C)]
|
2022-12-28 10:23:36 +00:00
|
|
|
struct TriangleVertex {
|
|
|
|
#[format(R32G32_SFLOAT)]
|
2020-05-10 00:36:20 +00:00
|
|
|
position: [f32; 2],
|
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",
|
2023-03-05 18:56:35 +00:00
|
|
|
src: r"
|
|
|
|
#version 450
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
layout(location = 0) in vec2 position;
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(position, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
",
|
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",
|
2023-03-05 18:56:35 +00:00
|
|
|
src: r"
|
|
|
|
#version 450
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
layout(location = 1) out vec3 f_normal;
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
void main() {
|
|
|
|
f_color = vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
f_normal = vec3(0.0, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
",
|
2018-10-27 23:10:29 +00:00
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|