diff --git a/core-rust/natives/src/engine_kernel.rs b/core-rust/natives/src/engine_kernel.rs index 6f60018..5be9a77 100644 --- a/core-rust/natives/src/engine_kernel.rs +++ b/core-rust/natives/src/engine_kernel.rs @@ -1,7 +1,7 @@ use futures::executor::block_on; use jni::sys::jlong; use std::sync::Arc; -use crate::{java_util::{arc_from_handle, arc_to_handle, arc_dispose_handle, JavaHandle}, window_surface::{WindowSurface, WindowSurfaceDesc}, ui::{UserInterface}, math::rect::Rect} ; +use crate::{java_util::{arc_from_handle, arc_to_handle, arc_dispose_handle, JavaHandle}, window_surface::{WindowSurface, WindowSurfaceDesc}, ui::{UserInterface}, math::rect::Rect, scene::{SceneChunk, Scene}} ; use std::cell::RefCell; use std::sync::Mutex; use std::cell::Cell; @@ -31,6 +31,7 @@ pub struct EngineKernelDesc { pub surface: WindowSurfaceDesc, } + impl EngineKernel { pub fn new(instance: wgpu::Instance, desc: &EngineKernelDesc) -> Self { let surface = block_on(WindowSurface::create(&instance, &desc.surface)); diff --git a/core-rust/natives/src/id_pool.rs b/core-rust/natives/src/id_pool.rs new file mode 100644 index 0000000..2b5150f --- /dev/null +++ b/core-rust/natives/src/id_pool.rs @@ -0,0 +1,69 @@ +use smallvec; + +pub struct Range { + start: u32, + end: u32 +} + +pub struct IDPool { + used: u32, + free: Vec +} + +impl IDPool { + pub fn fetch_id(&mut self) -> u32 { + match self.free.last_mut() { + Some(range) => { + let id = range.start; + range.start += 1; + if range.start > range.end { + self.free.pop(); + } + id + }, + None => { + let id = self.used; + self.used += 1; + id + } + } + + } + + + pub fn return_id(&mut self, id: u32) { + if self.free.is_empty() { + self.free.push(Range { start: id, end: id}) + } + + match self.free.binary_search_by(|probe| { + if id >= probe.start && id <= probe.end { + std::cmp::Ordering::Equal + } else { + probe.start.cmp(&id) + } + }) { + Ok(_index) => { + panic!("id returned multiple times to the pool"); + }, + Err(index) => { + let current_range = &mut self.free[index]; + if id + 1 == current_range.start { + current_range.start -= 1; + if index > 0 { + let update_end = current_range.end; + let previous_range = &mut self.free[index - 1]; + if previous_range.end + 1 == id { + previous_range.end = update_end; + self.free.remove(index); + } + } + } + + + + } + } + + } +} diff --git a/core-rust/natives/src/java_util.rs b/core-rust/natives/src/java_util.rs index ef8c486..b6e1b11 100644 --- a/core-rust/natives/src/java_util.rs +++ b/core-rust/natives/src/java_util.rs @@ -10,9 +10,9 @@ pub trait JavaHandle { fn drop_handle(ptr: jlong); } -pub trait JavaHandleContainer { - fn from_handle(&self, ptr: jlong) -> Option; - fn to_handle(&self, from: T) -> jlong; +pub trait JavaHandleContainer<'a, T> { + fn from_handle(&self, ptr: jlong) -> Option<&'a T>; + fn to_handle(&self, from: &'a T) -> jlong; } diff --git a/core-rust/natives/src/lib.rs b/core-rust/natives/src/lib.rs index 58980f9..a428432 100644 --- a/core-rust/natives/src/lib.rs +++ b/core-rust/natives/src/lib.rs @@ -9,3 +9,4 @@ mod math; #[macro_use] extern crate log; mod scene; +mod id_pool; diff --git a/core-rust/natives/src/resource/chunk_mesh_resource.rs b/core-rust/natives/src/resource/chunk_mesh_resource.rs index 8417028..2528c36 100644 --- a/core-rust/natives/src/resource/chunk_mesh_resource.rs +++ b/core-rust/natives/src/resource/chunk_mesh_resource.rs @@ -89,7 +89,6 @@ impl ChunkMeshEntry { pub fn buf_uvs_slice(&self) -> wgpu::BufferSlice{ self.vertex_buffer.slice(self.uv_start..(self.uv_start + (self.num_elements as u64 * std::mem::size_of::() as u64))) } pub fn buf_colors_slice(&self) -> wgpu::BufferSlice { self.vertex_buffer.slice(self.color_start..(self.color_start + (self.num_elements as u64 * std::mem::size_of::() as u64))) } pub fn buf_attributes_slice(&self) -> wgpu::BufferSlice { self.vertex_buffer.slice(self.attribute_start..(self.attribute_start + (self.num_elements as u64 * std::mem::size_of::() as u64)))} - } pub struct ChunkMeshResource { @@ -115,10 +114,6 @@ impl ChunkMeshResource { color: &[ChunkColor], attributes: &[ChunkAttributes] ) { - // assert!(position.len() == normal.len(), "mismatch in the number of vertices"); - // assert!(position.len() == uv.len(), "mismatch in the number of vertices"); - // assert!(position.len() == color.len(), "mismatch in the number of vertices"); - // assert!(position.len() == attributes.len(), "mismatch in the number of vertices"); let num_elements: u64 = position.len() as u64; let vertex_buffer = device.create_buffer( diff --git a/core-rust/natives/src/scene.rs b/core-rust/natives/src/scene.rs index 3fcdbb9..4b88876 100644 --- a/core-rust/natives/src/scene.rs +++ b/core-rust/natives/src/scene.rs @@ -1,21 +1,82 @@ +use glam::u32; +use jni::sys::jlong; +use crate::id_pool::IDPool; use crate::resource::chunk_mesh_resource::ChunkMeshResource; -use std::sync::Arc; +use crate::ui::{JavaHandle, arc_from_handle, arc_to_handle, arc_dispose_handle}; +use std::sync::{Weak,Arc, Mutex}; -pub struct Scene { - opaque_chunks: Vec> +pub struct SceneChunk { + pub transform: glam::Mat4, + pub mesh: Option>> } -impl Scene { +impl SceneChunk { +} - pub fn cmd_prepare(&mut self) { - +impl JavaHandle> for SceneChunk { + fn from_handle(ptr: jlong) -> Option> { + arc_from_handle(ptr) } - pub fn cmd_dispatch(&mut self) { + fn to_handle(from: Arc) -> jlong { + arc_to_handle(from) + } + fn drop_handle(ptr: jlong) { + arc_dispose_handle::(ptr); } +} - pub fn cmd_queue_opaque_chunk() { +pub struct Scene { + chunk_uniform_buffer: wgpu::Buffer, + + chunk_id_pool: IDPool, + chunk_pool: smallvec::SmallVec<[SceneChunk; 1024]> +} - } + +pub struct ChunkMutator<'a>{ + chunk: &'a SceneChunk, + scene: &'a Scene, + index: u32 } + +impl<'a> ChunkMutator<'a> { + +} + +pub type ChunkHandle = u32; +impl Scene { +// pub fn register_chunk<'a>(&mut self) -> ChunkHandle { +// let chunk_id = self.chunk_id_pool.fetch_id(); +// let new_chunk = SceneChunk { +// transform: glam::Mat4::IDENTITY, +// mesh: None +// }; +// +// match self.chunk_pool.get_mut(chunk_id as usize) { +// Some(view) => { +// (*view) = new_chunk; +// }, +// None => { +// self.chunk_pool.push(new_chunk); +// } +// } +// return chunk_id ; +// } +// +// pub fn return_chunk(&mut self, id: ChunkHandle) { +// self.chunk_id_pool.return_id(id); +// } +// pub fn fetch_chunk<'a>(&'a mut self, id: ChunkHandle) -> ChunkMutator<'a> { +// ChunkMutator { +// chunk: &self.chunk_pool[id as usize], +// scene: self, +// index: id +// } +// } + +} + + +