// Copyright 2020 Sergiusz 'q3k' Bazanski // // This file is part of Abrasion. // // Abrasion is free software: you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free // Software Foundation, version 3. // // Abrasion is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // You should have received a copy of the GNU General Public License along with // Abrasion. If not, see . use std::sync::Arc; use std::time; use std::sync::Mutex; use vulkano::device as vd; use vulkano::buffer as vb; use vulkano::sync::GpuFuture; use crate::render::vulkan::data; #[derive(Debug)] pub struct Mesh { vertices: Arc>, indices: Arc>, // vulkan buffers cache vulkan: Mutex>, } impl Mesh { pub fn new( vertices: Arc>, indices: Arc>, ) -> Self { Self { vertices, indices, vulkan: Mutex::new(None), } } pub fn vulkan_buffers( &self, graphics_queue: Arc, ) -> ( Arc>, Arc>, ) { let mut cache = self.vulkan.lock().unwrap(); match &mut *cache { Some(data) => (data.vbuffer.clone(), data.ibuffer.clone()), None => { let (vbuffer, vfuture) = vb::immutable::ImmutableBuffer::from_iter( self.vertices.iter().cloned(), vb::BufferUsage::vertex_buffer(), graphics_queue.clone(), ).unwrap(); let (ibuffer, ifuture) = vb::immutable::ImmutableBuffer::from_iter( self.indices.iter().cloned(), vb::BufferUsage::index_buffer(), graphics_queue.clone(), ).unwrap(); vfuture.flush().unwrap(); ifuture.flush().unwrap(); *cache = Some(data::VertexData { vbuffer: vbuffer.clone(), ibuffer: ibuffer.clone(), }); (vbuffer.clone(), ibuffer.clone()) }, } } }