This commit is contained in:
2021-08-15 22:34:29 +02:00
parent c619f945a3
commit 40aa7f635e
11 changed files with 273 additions and 56 deletions

View File

@@ -25,12 +25,14 @@ use winit::window::{Window, WindowBuilder};
use mesh::CPUMesh;
use pipelines::{Drawcall};
use pipelines::line_vs::ty::LinePushConstants;
use pipelines::DefaultShader;
use pipelines::{DefaultShader, TextShader};
use pipelines::vs;
use crate::config::RenderConfig;
use crate::vulkan::gameobject::{GameObject, GameObjectHandle};
use self::mesh::CPUVertex;
pub mod pipelines;
pub mod gameobject;
pub mod mesh;
@@ -39,7 +41,7 @@ mod renderpass;
mod framebuffers;
const VALIDATION_LAYERS: &[&str] = &[
"VK_LAYER_KHRONOS_validation"
"VK_LAYER_KHRONOS_validation",
];
#[derive(Default, Debug, Clone)]
@@ -59,6 +61,14 @@ pub struct LinePoint {
}
vulkano::impl_vertex!(LinePoint, position);
#[derive(Default, Debug, Clone)]
pub struct TextVertex {
pub position: [f32; 3],
pub uv: [f32; 2],
pub normal: [f32; 3],
}
vulkano::impl_vertex!(TextVertex, position, uv, normal);
pub trait Game {
/// Returns true if event should be ignored by the vulkan handler
fn on_window_event(self: &mut Self, event: &Event<()>);
@@ -66,8 +76,8 @@ pub trait Game {
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> vs::ty::ObjectUniformData;
}
pub struct Mesh {
pub vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
pub struct Mesh<V> {
pub vertex_buffer: Arc<CpuAccessibleBuffer<[V]>>,
pub index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
pub original_path: Option<String>,
}
@@ -77,7 +87,8 @@ pub struct MeshHandle {
pub index: usize,
pub diffuse_handle: TextureHandle,
pub normal_handle: Option<TextureHandle>,
pub original_path: Option<String>
pub original_path: Option<String>,
pub pipeline_index: usize
}
pub(crate) type TextureHandle = usize;
@@ -94,9 +105,9 @@ pub struct GameData {
pub dimensions: [u32; 2],
pub shutdown: bool,
pub game_objects: Vec<GameObject>,
pub meshes: Vec<Mesh>,
pub meshes: Vec<Mesh<Vertex>>,
pub meshes_text: Vec<Mesh<TextVertex>>,
pub textures: Vec<Texture>,
pub use_line_pipeline: bool,
}
pub(crate) type RendererDescriptorSets = dyn DescriptorSet + Send + Sync;
@@ -115,7 +126,6 @@ pub struct VulkanRenderer {
pub debug_callback: Option<DebugCallback>,
pub previous_frame_end: Option<Box<dyn GpuFuture>>,
pub uniform_buffers: Vec<Arc<CpuAccessibleBuffer<vs::ty::ObjectUniformData>>>,
pub line_vertex_buffer: Arc<CpuAccessibleBuffer<[LinePoint]>>,
pub render_config: RenderConfig
}
@@ -134,9 +144,9 @@ impl VulkanRenderer {
line_vertices,
dimensions: [0, 0],
meshes: vec![],
meshes_text: vec![],
game_objects: vec![],
textures: vec![],
use_line_pipeline: true,
};
// Create basic vulkan instance with layers and info
@@ -239,12 +249,11 @@ impl VulkanRenderer {
// Render pass
let render_pass = renderpass::create_render_pass(device.clone(), &render_config, swapchain.format());
let line_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), false, data.line_vertices.iter().cloned()).unwrap();
let render_pass_text = renderpass::create_render_pass(device.clone(), &render_config, swapchain.format());
let pipelines: Vec<Box<dyn Drawcall>> = vec![
Box::new(DefaultShader::new(device.clone(), render_pass.clone())),
// Box::new(LineShader::new(device.clone(), render_pass.clone(), line_vertex_buffer.clone())),
Box::new(TextShader::new(device.clone(), render_pass_text.clone())),
];
// Dynamic viewports allow us to recreate just the viewport when the window is resized
@@ -292,7 +301,6 @@ impl VulkanRenderer {
surface, swapchain, render_pass, queue,
recreate_swapchain: false, debug_callback, previous_frame_end,
render_config,
line_vertex_buffer,
}, events_loop)
}
@@ -307,8 +315,10 @@ impl VulkanRenderer {
}
// Draw meshes etc.
let mut index = 0;
for pipeline in &self.pipelines {
pipeline.draw(&mut builder, fb_index, &self.game_data, &self.dynamic_state);
pipeline.draw(&mut builder, fb_index, self.game_data.game_objects.iter().filter(|go| go.pipeline_index == index).collect(), &self.game_data, &self.dynamic_state);
index += 1;
}
// General cleanup
@@ -343,7 +353,7 @@ impl VulkanRenderer {
self.pipelines = vec![
Box::new(DefaultShader::new(self.device.clone(), self.render_pass.clone())),
// Box::new(LineShader::new(self.device.clone(), self.render_pass.clone(), self.line_vertex_buffer.clone())),
Box::new(TextShader::new(self.device.clone(), self.render_pass.clone())),
];
self.swapchain = new_swapchain;
@@ -420,11 +430,28 @@ impl VulkanRenderer {
// collision_mesh.push_face((mesh.indices[i] as usize, mesh.indices[i + 1] as usize, mesh.indices[i + 2] as usize));
// }
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, mesh.vertices.into_iter()).unwrap();
let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, mesh.indices.into_iter()).unwrap();
let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, mesh.indices.into_iter()).unwrap();
self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer, original_path });
self.game_data.meshes.len() - 1
match mesh.vertices.get(0).unwrap() {
CPUVertex::Vertex3D(_) => {
let verts: Vec<Vertex> = mesh.vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(vert) => Some(vert),
CPUVertex::VertexText(_) => None
}).collect();
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer, original_path });
self.game_data.meshes.len() - 1
},
CPUVertex::VertexText(_) => {
let verts: Vec<TextVertex> = mesh.vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(_) => None,
CPUVertex::VertexText(vert) => Some(vert)
}).collect();
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
self.game_data.meshes_text.push(Mesh { vertex_buffer, index_buffer, original_path });
self.game_data.meshes_text.len() - 1
},
}
}
pub fn upload_texture(self: &mut Self, bytes: &[u8], width: u32, height: u32, format: Format, device: Arc<Device>) {
@@ -438,7 +465,7 @@ impl VulkanRenderer {
};
let layout = ImageLayout::ShaderReadOnlyOptimal;
let mip_maps = if format == Format::R8Srgb { MipmapsCount::One } else { MipmapsCount::Log2 };
let mip_maps = if format == Format::R8Uint { MipmapsCount::One } else { MipmapsCount::Log2 };
let (image_view, initializer) = ImmutableImage::uninitialized(
device.clone(),
@@ -518,8 +545,8 @@ impl VulkanRenderer {
self.game_data.textures.push(Texture { image: image_view, sampler });
}
pub fn add_game_object(self: &mut Self, mut game_object: GameObject, pipeline_index: usize) -> GameObjectHandle {
self.pipelines[pipeline_index].create_descriptor_set(&mut game_object, self);
pub fn add_game_object(self: &mut Self, mut game_object: GameObject) -> GameObjectHandle {
self.pipelines[game_object.pipeline_index].create_descriptor_set(&mut game_object, self);
self.game_data.game_objects.push(game_object);
GameObjectHandle {