engine: use ResourceID for RM-managed resources
parent
ea8aa09791
commit
0be37ee3f7
|
@ -9,7 +9,7 @@ mod render;
|
|||
mod util;
|
||||
|
||||
use render::vulkan::data;
|
||||
use render::renderable::{Object, Renderable, ResourceManager, Texture, Mesh};
|
||||
use render::renderable::{Object, Renderable, Resource, ResourceManager, Texture, Mesh};
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
@ -61,12 +61,12 @@ fn main() {
|
|||
20, 22, 21, 22, 20, 23,
|
||||
|
||||
]);
|
||||
rm.add_mesh(Mesh::new(vertices, indices))
|
||||
rm.add(Resource::Mesh(Mesh::new(vertices, indices)))
|
||||
};
|
||||
|
||||
let path = &crate::util::file::resource_path(String::from("assets/test-128px.png"));
|
||||
let image = Arc::new(image::open(path).unwrap());
|
||||
let texture_cube = rm.add_texture(Texture::new(image));
|
||||
let texture_cube = rm.add(Resource::Texture(Texture::new(image)));
|
||||
|
||||
let mut renderer = render::Renderer::initialize();
|
||||
|
||||
|
|
|
@ -15,17 +15,47 @@ use vulkano::image as vm;
|
|||
|
||||
use crate::render::vulkan::data;
|
||||
|
||||
pub trait Renderable {
|
||||
fn render_data(&self) -> Option<(u64, u64, &cgm::Matrix4<f32>)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ResourceManager {
|
||||
meshes: HashMap<u64, Mesh>,
|
||||
textures: HashMap<u64, Texture>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum ResourceID {
|
||||
Texture(u64),
|
||||
Mesh(u64),
|
||||
}
|
||||
|
||||
impl hash::Hash for ResourceID {
|
||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||
match self {
|
||||
ResourceID::Texture(i) => i.hash(state),
|
||||
ResourceID::Mesh(i) => i.hash(state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ResourceID {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
let this = match self {
|
||||
ResourceID::Texture(i) => i,
|
||||
ResourceID::Mesh(i) => i,
|
||||
};
|
||||
let that = match other {
|
||||
ResourceID::Texture(i) => i,
|
||||
ResourceID::Mesh(i) => i,
|
||||
};
|
||||
this == that
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ResourceID {}
|
||||
|
||||
pub enum Resource {
|
||||
Texture(Texture),
|
||||
Mesh(Mesh),
|
||||
}
|
||||
|
||||
impl<'a> ResourceManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
@ -34,24 +64,33 @@ impl<'a> ResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_mesh(&mut self, m: Mesh) -> u64 {
|
||||
let id = m.id;
|
||||
self.meshes.insert(id, m);
|
||||
id
|
||||
pub fn add(&mut self, r: Resource) -> ResourceID {
|
||||
match r {
|
||||
Resource::Texture(t) => {
|
||||
let id = t.id;
|
||||
self.textures.insert(id, t);
|
||||
ResourceID::Texture(id)
|
||||
}
|
||||
Resource::Mesh(t) => {
|
||||
let id = t.id;
|
||||
self.meshes.insert(id, t);
|
||||
ResourceID::Mesh(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, t: Texture) -> u64 {
|
||||
let id = t.id;
|
||||
self.textures.insert(id, t);
|
||||
id
|
||||
pub fn texture(&'a self, id: &ResourceID) -> Option<&'a Texture> {
|
||||
if let ResourceID::Texture(i) = id {
|
||||
return Some(self.textures.get(&i).unwrap());
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
pub fn get_mesh(&'a self, id: u64) -> Option<&'a Mesh> {
|
||||
self.meshes.get(&id)
|
||||
}
|
||||
|
||||
pub fn get_texture(&'a self, id: u64) -> Option<&'a Texture> {
|
||||
self.textures.get(&id)
|
||||
pub fn mesh(&'a self, id: &ResourceID) -> Option<&'a Mesh> {
|
||||
if let ResourceID::Mesh(i) = id {
|
||||
return Some(self.meshes.get(&i).unwrap());
|
||||
}
|
||||
return None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -179,14 +218,20 @@ impl PartialEq for Mesh {
|
|||
|
||||
impl Eq for Mesh {}
|
||||
|
||||
pub trait Renderable {
|
||||
fn render_data(&self) -> Option<(ResourceID, ResourceID, &cgm::Matrix4<f32>)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Object {
|
||||
pub mesh: u64,
|
||||
pub texture: u64,
|
||||
pub mesh: ResourceID,
|
||||
pub texture: ResourceID,
|
||||
pub transform: cgm::Matrix4<f32>,
|
||||
}
|
||||
|
||||
impl Renderable for Object {
|
||||
fn render_data(&self) -> Option<(u64, u64, &cgm::Matrix4<f32>)> {
|
||||
fn render_data(&self) -> Option<(ResourceID, ResourceID, &cgm::Matrix4<f32>)> {
|
||||
Some((self.mesh, self.texture, &self.transform))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ mod swapchain_binding;
|
|||
mod worker;
|
||||
|
||||
use crate::render::renderable;
|
||||
use crate::util::counter::Counter;
|
||||
|
||||
const VERSION: vi::Version = vi::Version { major: 1, minor: 0, patch: 0};
|
||||
|
||||
|
@ -42,7 +43,7 @@ pub struct Instance<WT> {
|
|||
uniform_pool: Option<vb::CpuBufferPool<data::UniformBufferObject>>,
|
||||
armed: bool,
|
||||
previous_frame_end: Option<Box<FlipFuture<WT>>>,
|
||||
fps_counter: crate::util::counter::Counter,
|
||||
fps_counter: Counter,
|
||||
}
|
||||
|
||||
type FlipFuture<WT> = FenceSignalFuture<vs::PresentFuture<vc::CommandBufferExecFuture<vs::SwapchainAcquireFuture<WT>, Arc<vc::AutoCommandBuffer>>, WT>>;
|
||||
|
@ -162,8 +163,8 @@ impl<WT: 'static + Send + Sync> Instance<WT> {
|
|||
|
||||
|
||||
// Sort renderables by mesh.
|
||||
let mut meshes: HashMap<u64, Vec<&cgm::Matrix4<f32>>> = HashMap::new();
|
||||
let mut textures: HashMap<u64, u64> = HashMap::new();
|
||||
let mut meshes: HashMap<renderable::ResourceID, Vec<&cgm::Matrix4<f32>>> = HashMap::new();
|
||||
let mut textures: HashMap<renderable::ResourceID, renderable::ResourceID> = HashMap::new();
|
||||
|
||||
profiler.end("mgc.prep");
|
||||
for r in renderables {
|
||||
|
@ -182,9 +183,9 @@ impl<WT: 'static + Send + Sync> Instance<WT> {
|
|||
let pipeline = self.pipeline.as_ref().unwrap().get_pipeline().clone();
|
||||
|
||||
for (meshId, transforms) in meshes {
|
||||
let mesh = rm.get_mesh(meshId).unwrap();
|
||||
let mesh = rm.mesh(&meshId).unwrap();
|
||||
let textureId = textures.get(&meshId).unwrap().clone();
|
||||
let texture = rm.get_texture(textureId).unwrap();
|
||||
let texture = rm.texture(&textureId).unwrap();
|
||||
|
||||
let mut builder = vc::AutoCommandBufferBuilder::secondary_graphics_one_time_submit(
|
||||
device.clone(), queue.family(), vf::Subpass::from(rp.clone(), 0).unwrap()).unwrap();
|
||||
|
|
Loading…
Reference in New Issue