First work with vulkano
This commit is contained in:
parent
cbadffc41f
commit
0597579115
36 changed files with 1059 additions and 1847 deletions
210
src/renderer/scene.rs
Normal file
210
src/renderer/scene.rs
Normal file
|
@ -0,0 +1,210 @@
|
|||
use std::sync::Arc;
|
||||
use vulkano::buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer};
|
||||
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
|
||||
use vulkano::device::Device;
|
||||
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator};
|
||||
use vulkano::pipeline::graphics::vertex_input::{Vertex, VertexDefinition};
|
||||
use vulkano::pipeline::{DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo};
|
||||
use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState};
|
||||
use vulkano::pipeline::graphics::GraphicsPipelineCreateInfo;
|
||||
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
|
||||
use vulkano::pipeline::graphics::multisample::MultisampleState;
|
||||
use vulkano::pipeline::graphics::rasterization::RasterizationState;
|
||||
use vulkano::pipeline::graphics::subpass::PipelineRenderingCreateInfo;
|
||||
use vulkano::pipeline::graphics::viewport::ViewportState;
|
||||
use vulkano::pipeline::layout::PipelineDescriptorSetLayoutCreateInfo;
|
||||
use vulkano::swapchain::Swapchain;
|
||||
|
||||
// We use `#[repr(C)]` here to force rustc to use a defined layout for our data, as the default
|
||||
// representation has *no guarantees*.
|
||||
#[derive(BufferContents, Vertex)]
|
||||
#[repr(C)]
|
||||
struct MyVertex {
|
||||
#[format(R32G32_SFLOAT)]
|
||||
position: [f32; 2],
|
||||
|
||||
#[format(R32G32B32_SFLOAT)]
|
||||
color: [f32; 3],
|
||||
}
|
||||
|
||||
pub struct Scene {
|
||||
pipeline: Arc<GraphicsPipeline>,
|
||||
vertex_buffer: Subbuffer<[MyVertex]>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
pub fn initialize(device: &Arc<Device>, swapchain: &Arc<Swapchain>, memory_allocator: &Arc<StandardMemoryAllocator>) -> Scene {
|
||||
// The next step is to create the shaders.
|
||||
//
|
||||
// The raw shader creation API provided by the vulkano library is unsafe for various
|
||||
// reasons, so The `shader!` macro provides a way to generate a Rust module from GLSL
|
||||
// source - in the example below, the source is provided as a string input directly to the
|
||||
// shader, but a path to a source file can be provided as well. Note that the user must
|
||||
// specify the type of shader (e.g. "vertex", "fragment", etc.) using the `ty` option of
|
||||
// the macro.
|
||||
//
|
||||
// The items generated by the `shader!` macro include a `load` function which loads the
|
||||
// shader using an input logical device. The module also includes type definitions for
|
||||
// layout structures defined in the shader source, for example uniforms and push constants.
|
||||
//
|
||||
// A more detailed overview of what the `shader!` macro generates can be found in the
|
||||
// vulkano-shaders crate docs. You can view them at https://docs.rs/vulkano-shaders/
|
||||
mod vs {
|
||||
vulkano_shaders::shader! {
|
||||
ty: "vertex",
|
||||
path: r"res/shaders/vertex.vert",
|
||||
}
|
||||
}
|
||||
|
||||
mod fs {
|
||||
vulkano_shaders::shader! {
|
||||
ty: "fragment",
|
||||
path: r"res/shaders/vertex.frag",
|
||||
}
|
||||
}
|
||||
|
||||
// Before we draw, we have to create what is called a **pipeline**. A pipeline describes
|
||||
// how a GPU operation is to be performed. It is similar to an OpenGL program, but it also
|
||||
// contains many settings for customization, all baked into a single object. For drawing,
|
||||
// we create a **graphics** pipeline, but there are also other types of pipeline.
|
||||
let pipeline = {
|
||||
// First, we load the shaders that the pipeline will use: the vertex shader and the
|
||||
// fragment shader.
|
||||
//
|
||||
// A Vulkan shader can in theory contain multiple entry points, so we have to specify
|
||||
// which one.
|
||||
let vs = vs::load(device.clone())
|
||||
.unwrap()
|
||||
.entry_point("main")
|
||||
.unwrap();
|
||||
let fs = fs::load(device.clone())
|
||||
.unwrap()
|
||||
.entry_point("main")
|
||||
.unwrap();
|
||||
|
||||
// Automatically generate a vertex input state from the vertex shader's input
|
||||
// interface, that takes a single vertex buffer containing `Vertex` structs.
|
||||
let vertex_input_state = MyVertex::per_vertex().definition(&vs).unwrap();
|
||||
|
||||
// Make a list of the shader stages that the pipeline will have.
|
||||
let stages = [
|
||||
PipelineShaderStageCreateInfo::new(vs),
|
||||
PipelineShaderStageCreateInfo::new(fs),
|
||||
];
|
||||
|
||||
// We must now create a **pipeline layout** object, which describes the locations and
|
||||
// types of descriptor sets and push constants used by the shaders in the pipeline.
|
||||
//
|
||||
// Multiple pipelines can share a common layout object, which is more efficient. The
|
||||
// shaders in a pipeline must use a subset of the resources described in its pipeline
|
||||
// layout, but the pipeline layout is allowed to contain resources that are not present
|
||||
// in the shaders; they can be used by shaders in other pipelines that share the same
|
||||
// layout. Thus, it is a good idea to design shaders so that many pipelines have common
|
||||
// resource locations, which allows them to share pipeline layouts.
|
||||
let layout = PipelineLayout::new(
|
||||
device.clone(),
|
||||
// Since we only have one pipeline in this example, and thus one pipeline layout,
|
||||
// we automatically generate the creation info for it from the resources used in
|
||||
// the shaders. In a real application, you would specify this information manually
|
||||
// so that you can re-use one layout in multiple pipelines.
|
||||
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
||||
.into_pipeline_layout_create_info(device.clone())
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// We describe the formats of attachment images where the colors, depth and/or stencil
|
||||
// information will be written. The pipeline will only be usable with this particular
|
||||
// configuration of the attachment images.
|
||||
let subpass = PipelineRenderingCreateInfo {
|
||||
// We specify a single color attachment that will be rendered to. When we begin
|
||||
// rendering, we will specify a swapchain image to be used as this attachment, so
|
||||
// here we set its format to be the same format as the swapchain.
|
||||
color_attachment_formats: vec![Some(swapchain.image_format())],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Finally, create the pipeline.
|
||||
GraphicsPipeline::new(
|
||||
device.clone(),
|
||||
None,
|
||||
GraphicsPipelineCreateInfo {
|
||||
stages: stages.into_iter().collect(),
|
||||
// How vertex data is read from the vertex buffers into the vertex shader.
|
||||
vertex_input_state: Some(vertex_input_state),
|
||||
// How vertices are arranged into primitive shapes. The default primitive shape
|
||||
// is a triangle.
|
||||
input_assembly_state: Some(InputAssemblyState::default()),
|
||||
// How primitives are transformed and clipped to fit the framebuffer. We use a
|
||||
// resizable viewport, set to draw over the entire window.
|
||||
viewport_state: Some(ViewportState::default()),
|
||||
// How polygons are culled and converted into a raster of pixels. The default
|
||||
// value does not perform any culling.
|
||||
rasterization_state: Some(RasterizationState::default()),
|
||||
// How multiple fragment shader samples are converted to a single pixel value.
|
||||
// The default value does not perform any multisampling.
|
||||
multisample_state: Some(MultisampleState::default()),
|
||||
// How pixel values are combined with the values already present in the
|
||||
// framebuffer. The default value overwrites the old value with the new one,
|
||||
// without any blending.
|
||||
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
||||
subpass.color_attachment_formats.len() as u32,
|
||||
ColorBlendAttachmentState::default(),
|
||||
)),
|
||||
// Dynamic states allows us to specify parts of the pipeline settings when
|
||||
// recording the command buffer, before we perform drawing. Here, we specify
|
||||
// that the viewport should be dynamic.
|
||||
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
||||
subpass: Some(subpass.into()),
|
||||
..GraphicsPipelineCreateInfo::layout(layout)
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
// We now create a buffer that will store the shape of our triangle.
|
||||
let vertices = [
|
||||
MyVertex {
|
||||
position: [-0.5, -0.25],
|
||||
color: [1.0, 0.0, 0.0],
|
||||
},
|
||||
MyVertex {
|
||||
position: [0.0, 0.5],
|
||||
color: [0.0, 1.0, 0.0],
|
||||
},
|
||||
MyVertex {
|
||||
position: [0.25, -0.1],
|
||||
color: [0.0, 0.0, 1.0],
|
||||
},
|
||||
];
|
||||
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();
|
||||
|
||||
Self {
|
||||
pipeline,
|
||||
vertex_buffer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self, builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>) {
|
||||
builder.bind_pipeline_graphics(self.pipeline.clone())
|
||||
.unwrap()
|
||||
.bind_vertex_buffers(0, self.vertex_buffer.clone())
|
||||
.unwrap();
|
||||
|
||||
// We add a draw command.
|
||||
unsafe { builder.draw(self.vertex_buffer.len() as u32, 1, 0, 0) }.unwrap();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue