Begin implement Vertex
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 4m25s

This commit is contained in:
Florian RICHER 2024-12-06 17:06:17 +01:00
parent 84666db1d8
commit a1961cff05
10 changed files with 105 additions and 18 deletions

View file

@ -39,3 +39,7 @@ pub use vk_fence::VkFence;
mod utils;
mod vertex;
mod vk_buffer;
pub use vk_buffer::VkBuffer;
pub use vertex::Vertex;

View file

@ -8,6 +8,10 @@ pub struct Vertex {
}
impl Vertex {
pub fn new(position: [f32; 2], color: [f32; 3]) -> Self {
Self { position, color }
}
pub fn get_binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)

View file

@ -0,0 +1,31 @@
use std::sync::Arc;
use ash::prelude::VkResult;
use ash::vk;
use crate::renderer::vulkan::VkDevice;
pub struct VkBuffer {
device: Arc<VkDevice>,
handle: vk::Buffer,
}
impl VkBuffer {
pub fn new(device: &Arc<VkDevice>, info: &vk::BufferCreateInfo) -> VkResult<Self> {
let buffer = unsafe { device.handle.create_buffer(info, None)? };
Ok(VkBuffer { device: Arc::clone(device), handle: buffer })
}
pub fn mem_requirements(&self) -> vk::MemoryRequirements {
unsafe { self.device.handle.get_buffer_memory_requirements(self.handle) }
}
}
impl Drop for VkBuffer {
fn drop(&mut self) {
unsafe {
self.device.handle.destroy_buffer(self.handle, None);
}
}
}

View file

@ -5,6 +5,8 @@ use std::sync::Arc;
pub struct VkDevice {
instance: Arc<VkInstance>,
physical_device: Arc<VkPhysicalDevice>,
pub handle: ash::Device,
pub swapchain_loader: ash::khr::swapchain::Device,
pub queue_family_index: u32,
@ -17,7 +19,7 @@ pub struct VkDevice {
impl VkDevice {
pub fn new_graphics_device(
instance: &Arc<VkInstance>,
physical_device: &VkPhysicalDevice,
physical_device: &Arc<VkPhysicalDevice>,
queue_family_index: u32,
) -> anyhow::Result<Self> {
let device_extension_names_raw = [
@ -56,7 +58,8 @@ impl VkDevice {
let swapchain_loader = ash::khr::swapchain::Device::new(&instance.handle, &device);
Ok(Self {
instance: instance.clone(),
instance: Arc::clone(instance),
physical_device: Arc::clone(&physical_device),
handle: device,
swapchain_loader,
queue_family_index,

View file

@ -5,6 +5,7 @@ use crate::renderer::vulkan::{
use ash::khr::surface;
use ash::{vk, Entry, Instance};
use std::ffi::{c_char, CStr, CString};
use std::sync::Arc;
pub struct VkInstance {
pub entry: Entry,
@ -107,12 +108,12 @@ impl VkInstance {
}
}
pub fn get_physical_devices(&self) -> Vec<VkPhysicalDevice> {
let physical_devices = unsafe { self.handle.enumerate_physical_devices() };
pub fn get_physical_devices(instance: &Arc<Self>) -> Vec<VkPhysicalDevice> {
let physical_devices = unsafe { instance.handle.enumerate_physical_devices() };
physical_devices
.unwrap_or_default()
.iter()
.map(|physical_device| VkPhysicalDevice::new(&self.handle, *physical_device))
.map(|physical_device| VkPhysicalDevice::new(&instance, *physical_device))
.collect()
}
}

View file

@ -1,26 +1,29 @@
use super::VkSurface;
use std::sync::Arc;
use super::{VkInstance, VkSurface};
use ash::vk;
pub struct VkPhysicalDevice {
// Vulkan properties
instance: Arc<VkInstance>,
pub handle: vk::PhysicalDevice,
pub properties: vk::PhysicalDeviceProperties,
pub features: vk::PhysicalDeviceFeatures,
pub queue_family_properties: Vec<vk::QueueFamilyProperties>,
}
impl VkPhysicalDevice {
pub fn new(instance: &ash::Instance, physical_device: vk::PhysicalDevice) -> Self {
pub fn new(instance: &Arc<VkInstance>, physical_device: vk::PhysicalDevice) -> Self {
log::debug!("New physical device");
let device_properties = unsafe { instance.get_physical_device_properties(physical_device) };
let device_properties = unsafe { instance.handle.get_physical_device_properties(physical_device) };
log::debug!("{device_properties:#?}");
let device_features = unsafe { instance.get_physical_device_features(physical_device) };
let device_features = unsafe { instance.handle.get_physical_device_features(physical_device) };
log::debug!("{device_features:#?}");
let device_queue_families =
unsafe { instance.get_physical_device_queue_family_properties(physical_device) };
unsafe { instance.handle.get_physical_device_queue_family_properties(physical_device) };
log::debug!("{device_queue_families:#?}");
Self {
instance: Arc::clone(instance),
handle: physical_device,
properties: device_properties,
features: device_features,

View file

@ -29,7 +29,7 @@ impl VkRenderContext {
let instance = Arc::new(VkInstance::new(&required_extensions));
let surface = Arc::new(VkSurface::new(&window, instance.clone())?);
let mut physical_devices = instance.get_physical_devices();
let mut physical_devices = VkInstance::get_physical_devices(&instance);
physical_devices.sort_by(|a, b| b.priority().cmp(&a.priority()));
let (physical_device, queue_family_index, properties) =

View file

@ -1,2 +1,4 @@
mod triangle;
pub use triangle::Triangle as TriangleScene;
mod vertex;
pub use triangle::TriangleScene;

View file

@ -1,20 +1,20 @@
use std::sync::Arc;
use ash::vk;
use crate::renderer::vulkan::{VkDevice, VkGraphicsPipeline, VkRenderPass, VkSwapchain};
use crate::renderer::Renderable;
use ash::vk;
use ash::vk::CommandBuffer;
use std::sync::Arc;
pub struct Triangle {
pub struct TriangleScene {
pipeline: Option<VkGraphicsPipeline>,
}
impl Triangle {
impl TriangleScene {
pub fn new() -> Self {
Self { pipeline: None }
}
}
impl Renderable for Triangle {
impl Renderable for TriangleScene {
fn init(&mut self, device: &Arc<VkDevice>, render_pass: &Arc<VkRenderPass>) -> anyhow::Result<()> {
let pipeline = VkGraphicsPipeline::new(&device, &render_pass)?;
self.pipeline = Some(pipeline);

39
src/scene/vertex.rs Normal file
View file

@ -0,0 +1,39 @@
use std::sync::Arc;
use ash::prelude::VkResult;
use ash::vk;
use crate::renderer::vulkan::{Vertex, VkBuffer, VkDevice};
#[derive(Default)]
struct VertexScene {
vertices: Vec<Vertex>,
vertices_buffer: Option<VkBuffer>,
}
impl VertexScene {
pub fn new() -> Self {
let vertices = vec![
Vertex::new([0.0, -0.5], [1.0, 0.0, 0.0]),
Vertex::new([0.5, 0.5], [0.0, 1.0, 0.0]),
Vertex::new([-0.5, 0.5], [0.0, 0.0, 1.0]),
];
Self {
vertices,
..Default::default()
}
}
fn create_buffer(&mut self, device: &Arc<VkDevice>) -> VkResult<()> {
let buffer_info = vk::BufferCreateInfo::default()
.usage(vk::BufferUsageFlags::VERTEX_BUFFER)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.size(self.vertices.len() as u64 * size_of::<Vertex>() as u64);
let buffer = VkBuffer::new(device, &buffer_info)?;
self.vertices_buffer = Some(buffer);
Ok(())
}
}