mirror of
https://github.com/vulkano-rs/vulkano.git
synced 2024-11-22 14:56:42 +00:00
289ec102e0
* Document shader safety requirements, make draw/dispatch unsafe * Extra docs * Doctests * Max index value * Small change * Update vulkano/src/command_buffer/mod.rs Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com> * Update vulkano/src/command_buffer/mod.rs Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com> --------- Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>
287 lines
9.6 KiB
Rust
287 lines
9.6 KiB
Rust
use crate::app::App;
|
|
use std::sync::Arc;
|
|
use vulkano::{
|
|
buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer},
|
|
command_buffer::{
|
|
allocator::StandardCommandBufferAllocator, CommandBuffer, CommandBufferBeginInfo,
|
|
CommandBufferInheritanceInfo, CommandBufferLevel, CommandBufferUsage,
|
|
RecordingCommandBuffer,
|
|
},
|
|
descriptor_set::{
|
|
allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet,
|
|
},
|
|
device::Queue,
|
|
image::{
|
|
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode},
|
|
view::ImageView,
|
|
},
|
|
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter},
|
|
pipeline::{
|
|
graphics::{
|
|
color_blend::{ColorBlendAttachmentState, ColorBlendState},
|
|
input_assembly::InputAssemblyState,
|
|
multisample::MultisampleState,
|
|
rasterization::RasterizationState,
|
|
vertex_input::{Vertex, VertexDefinition},
|
|
viewport::{Viewport, ViewportState},
|
|
GraphicsPipelineCreateInfo,
|
|
},
|
|
layout::PipelineDescriptorSetLayoutCreateInfo,
|
|
DynamicState, GraphicsPipeline, Pipeline, PipelineBindPoint, PipelineLayout,
|
|
PipelineShaderStageCreateInfo,
|
|
},
|
|
render_pass::Subpass,
|
|
};
|
|
|
|
/// Vertex for textured quads.
|
|
#[derive(BufferContents, Vertex)]
|
|
#[repr(C)]
|
|
pub struct TexturedVertex {
|
|
#[format(R32G32_SFLOAT)]
|
|
pub position: [f32; 2],
|
|
#[format(R32G32_SFLOAT)]
|
|
pub tex_coords: [f32; 2],
|
|
}
|
|
|
|
pub fn textured_quad(width: f32, height: f32) -> (Vec<TexturedVertex>, Vec<u32>) {
|
|
(
|
|
vec![
|
|
TexturedVertex {
|
|
position: [-(width / 2.0), -(height / 2.0)],
|
|
tex_coords: [0.0, 1.0],
|
|
},
|
|
TexturedVertex {
|
|
position: [-(width / 2.0), height / 2.0],
|
|
tex_coords: [0.0, 0.0],
|
|
},
|
|
TexturedVertex {
|
|
position: [width / 2.0, height / 2.0],
|
|
tex_coords: [1.0, 0.0],
|
|
},
|
|
TexturedVertex {
|
|
position: [width / 2.0, -(height / 2.0)],
|
|
tex_coords: [1.0, 1.0],
|
|
},
|
|
],
|
|
vec![0, 2, 1, 0, 3, 2],
|
|
)
|
|
}
|
|
|
|
/// A subpass pipeline that fills a quad over the frame.
|
|
pub struct PixelsDrawPipeline {
|
|
gfx_queue: Arc<Queue>,
|
|
subpass: Subpass,
|
|
pipeline: Arc<GraphicsPipeline>,
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
|
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
|
vertices: Subbuffer<[TexturedVertex]>,
|
|
indices: Subbuffer<[u32]>,
|
|
}
|
|
|
|
impl PixelsDrawPipeline {
|
|
pub fn new(app: &App, gfx_queue: Arc<Queue>, subpass: Subpass) -> PixelsDrawPipeline {
|
|
let (vertices, indices) = textured_quad(2.0, 2.0);
|
|
let memory_allocator = app.context.memory_allocator();
|
|
let vertex_buffer = Buffer::from_iter(
|
|
memory_allocator.clone(),
|
|
BufferCreateInfo {
|
|
usage: BufferUsage::VERTEX_BUFFER,
|
|
..Default::default()
|
|
},
|
|
AllocationCreateInfo {
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
|
..Default::default()
|
|
},
|
|
vertices,
|
|
)
|
|
.unwrap();
|
|
let index_buffer = Buffer::from_iter(
|
|
memory_allocator.clone(),
|
|
BufferCreateInfo {
|
|
usage: BufferUsage::INDEX_BUFFER,
|
|
..Default::default()
|
|
},
|
|
AllocationCreateInfo {
|
|
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
|
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
|
..Default::default()
|
|
},
|
|
indices,
|
|
)
|
|
.unwrap();
|
|
|
|
let pipeline = {
|
|
let device = gfx_queue.device();
|
|
let vs = vs::load(device.clone())
|
|
.expect("failed to create shader module")
|
|
.entry_point("main")
|
|
.expect("shader entry point not found");
|
|
let fs = fs::load(device.clone())
|
|
.expect("failed to create shader module")
|
|
.entry_point("main")
|
|
.expect("shader entry point not found");
|
|
let vertex_input_state = TexturedVertex::per_vertex()
|
|
.definition(&vs.info().input_interface)
|
|
.unwrap();
|
|
let stages = [
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
PipelineShaderStageCreateInfo::new(fs),
|
|
];
|
|
let layout = PipelineLayout::new(
|
|
device.clone(),
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
.into_pipeline_layout_create_info(device.clone())
|
|
.unwrap(),
|
|
)
|
|
.unwrap();
|
|
|
|
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::default()),
|
|
rasterization_state: Some(RasterizationState::default()),
|
|
multisample_state: Some(MultisampleState::default()),
|
|
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
|
subpass.num_color_attachments(),
|
|
ColorBlendAttachmentState::default(),
|
|
)),
|
|
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
|
subpass: Some(subpass.clone().into()),
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
},
|
|
)
|
|
.unwrap()
|
|
};
|
|
|
|
PixelsDrawPipeline {
|
|
gfx_queue,
|
|
subpass,
|
|
pipeline,
|
|
command_buffer_allocator: app.command_buffer_allocator.clone(),
|
|
descriptor_set_allocator: app.descriptor_set_allocator.clone(),
|
|
vertices: vertex_buffer,
|
|
indices: index_buffer,
|
|
}
|
|
}
|
|
|
|
fn create_image_sampler_nearest(&self, image: Arc<ImageView>) -> Arc<DescriptorSet> {
|
|
let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
|
|
let sampler = Sampler::new(
|
|
self.gfx_queue.device().clone(),
|
|
SamplerCreateInfo {
|
|
mag_filter: Filter::Nearest,
|
|
min_filter: Filter::Nearest,
|
|
address_mode: [SamplerAddressMode::Repeat; 3],
|
|
mipmap_mode: SamplerMipmapMode::Nearest,
|
|
..Default::default()
|
|
},
|
|
)
|
|
.unwrap();
|
|
|
|
DescriptorSet::new(
|
|
self.descriptor_set_allocator.clone(),
|
|
layout.clone(),
|
|
[
|
|
WriteDescriptorSet::sampler(0, sampler),
|
|
WriteDescriptorSet::image_view(1, image),
|
|
],
|
|
[],
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
/// Draws input `image` over a quad of size -1.0 to 1.0.
|
|
pub fn draw(&self, viewport_dimensions: [u32; 2], image: Arc<ImageView>) -> Arc<CommandBuffer> {
|
|
let mut builder = RecordingCommandBuffer::new(
|
|
self.command_buffer_allocator.clone(),
|
|
self.gfx_queue.queue_family_index(),
|
|
CommandBufferLevel::Secondary,
|
|
CommandBufferBeginInfo {
|
|
usage: CommandBufferUsage::MultipleSubmit,
|
|
inheritance_info: Some(CommandBufferInheritanceInfo {
|
|
render_pass: Some(self.subpass.clone().into()),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.unwrap();
|
|
|
|
builder
|
|
.set_viewport(
|
|
0,
|
|
[Viewport {
|
|
offset: [0.0, 0.0],
|
|
extent: [viewport_dimensions[0] as f32, viewport_dimensions[1] as f32],
|
|
depth_range: 0.0..=1.0,
|
|
}]
|
|
.into_iter()
|
|
.collect(),
|
|
)
|
|
.unwrap()
|
|
.bind_pipeline_graphics(self.pipeline.clone())
|
|
.unwrap()
|
|
.bind_descriptor_sets(
|
|
PipelineBindPoint::Graphics,
|
|
self.pipeline.layout().clone(),
|
|
0,
|
|
self.create_image_sampler_nearest(image),
|
|
)
|
|
.unwrap()
|
|
.bind_vertex_buffers(0, self.vertices.clone())
|
|
.unwrap()
|
|
.bind_index_buffer(self.indices.clone())
|
|
.unwrap();
|
|
|
|
unsafe {
|
|
builder
|
|
.draw_indexed(self.indices.len() as u32, 1, 0, 0, 0)
|
|
.unwrap();
|
|
}
|
|
|
|
builder.end().unwrap()
|
|
}
|
|
}
|
|
|
|
mod vs {
|
|
vulkano_shaders::shader! {
|
|
ty: "vertex",
|
|
src: r"
|
|
#version 450
|
|
layout(location=0) in vec2 position;
|
|
layout(location=1) in vec2 tex_coords;
|
|
|
|
layout(location = 0) out vec2 f_tex_coords;
|
|
|
|
void main() {
|
|
gl_Position = vec4(position, 0.0, 1.0);
|
|
f_tex_coords = tex_coords;
|
|
}
|
|
",
|
|
}
|
|
}
|
|
|
|
mod fs {
|
|
vulkano_shaders::shader! {
|
|
ty: "fragment",
|
|
src: r"
|
|
#version 450
|
|
layout(location = 0) in vec2 v_tex_coords;
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
layout(set = 0, binding = 0) uniform sampler s;
|
|
layout(set = 0, binding = 1) uniform texture2D tex;
|
|
|
|
void main() {
|
|
f_color = texture(sampler2D(tex, s), v_tex_coords);
|
|
}
|
|
",
|
|
}
|
|
}
|