Skip to content

Commit

Permalink
feat: added chunk logic for scene
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Sep 26, 2023
1 parent ba0aea6 commit 4364e4a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 18 deletions.
3 changes: 2 additions & 1 deletion core-rust/natives/src/engine_kernel.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand Down
69 changes: 69 additions & 0 deletions core-rust/natives/src/id_pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use smallvec;

pub struct Range {
start: u32,
end: u32
}

pub struct IDPool {
used: u32,
free: Vec<Range>
}

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);
}
}
}



}
}

}
}
6 changes: 3 additions & 3 deletions core-rust/natives/src/java_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub trait JavaHandle<T> {
fn drop_handle(ptr: jlong);
}

pub trait JavaHandleContainer<T> {
fn from_handle(&self, ptr: jlong) -> Option<T>;
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;

}

Expand Down
1 change: 1 addition & 0 deletions core-rust/natives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mod math;
#[macro_use]
extern crate log;
mod scene;
mod id_pool;
5 changes: 0 additions & 5 deletions core-rust/natives/src/resource/chunk_mesh_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ChunkUV>() 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::<ChunkColor>() 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::<ChunkAttributes>() as u64)))}

}

pub struct ChunkMeshResource {
Expand All @@ -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(
Expand Down
79 changes: 70 additions & 9 deletions core-rust/natives/src/scene.rs
Original file line number Diff line number Diff line change
@@ -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<Arc<ChunkMeshResource>>
pub struct SceneChunk {
pub transform: glam::Mat4,
pub mesh: Option<std::sync::Weak<Mutex<ChunkMeshResource>>>
}

impl Scene {
impl SceneChunk {
}

pub fn cmd_prepare(&mut self) {

impl JavaHandle<Arc<SceneChunk>> for SceneChunk {
fn from_handle(ptr: jlong) -> Option<Arc<SceneChunk>> {
arc_from_handle(ptr)
}

pub fn cmd_dispatch(&mut self) {
fn to_handle(from: Arc<SceneChunk>) -> jlong {
arc_to_handle(from)
}

fn drop_handle(ptr: jlong) {
arc_dispose_handle::<SceneChunk>(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
// }
// }

}



0 comments on commit 4364e4a

Please sign in to comment.