Skip to content

Commit

Permalink
general code cleanup and bug fixes, respect disable_occlusion_cull (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerindividual committed Aug 19, 2023
1 parent 1b2f963 commit 8202ff7
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 114 deletions.
2 changes: 1 addition & 1 deletion native/core/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T, const CAPACITY: usize> ArrayDeque<T, CAPACITY> {
}

pub fn pop(&mut self) -> Option<&T> {
if self.head == self.tail {
if self.is_empty() {
return None;
}

Expand Down
12 changes: 0 additions & 12 deletions native/core/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

use std::boxed::Box;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::ptr;

use crate::graph::local::LocalFrustum;
use crate::graph::*;
use crate::jni::types::*;
use crate::math::*;
use crate::mem::LibcAllocVtable;
use crate::panic::PanicHandlerFn;

#[repr(C)]
pub struct CVec<T> {
Expand Down Expand Up @@ -92,18 +88,10 @@ impl<T, const LEN: usize> Copy for CInlineVec<T, LEN> where T: Copy {}
#[allow(non_snake_case)]
mod java {
use std::boxed::Box;
use std::mem::MaybeUninit;
use std::ptr;

use core_simd::simd::f32x4;

use crate::ffi::*;
use crate::graph::local::index::LocalNodeIndex;
use crate::graph::local::LocalCoordContext;
use crate::graph::visibility::VisibilityData;
use crate::graph::*;
use crate::jni::types::*;
use crate::math::*;
use crate::mem::LibcAllocVtable;
use crate::panic::PanicHandlerFn;

Expand Down
3 changes: 1 addition & 2 deletions native/core/src/graph/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std_float::StdFloat;
use crate::graph::local::index::LocalNodeIndex;
use crate::graph::octree::{LEVEL_3_COORD_LENGTH, LEVEL_3_COORD_MASK, LEVEL_3_COORD_SHIFT};
use crate::graph::*;
use crate::math::*;
use crate::region::REGION_COORD_SHIFT;

pub struct LocalCoordContext {
Expand Down Expand Up @@ -100,7 +99,7 @@ impl LocalCoordContext {
camera_coords,
camera_section_index,
camera_section_coords,
origin_region_coords: Default::default(),
origin_region_coords,
fog_distance_squared,
world_bottom_section_y,
world_top_section_y,
Expand Down
44 changes: 23 additions & 21 deletions native/core/src/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use std::collections::VecDeque;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::mem::{swap, transmute};
use std::ops::*;
use std::vec::Vec;
use std::mem::swap;

use core_simd::simd::Which::*;
use core_simd::simd::*;
use local::LocalCoordContext;
use std_float::StdFloat;

use crate::collections::ArrayDeque;
use crate::ffi::{CInlineVec, CVec};
use crate::ffi::CInlineVec;
use crate::graph::local::index::LocalNodeIndex;
use crate::graph::local::*;
use crate::graph::octree::LinearBitOctree;
Expand All @@ -23,7 +17,6 @@ pub mod local;
mod octree;
pub mod visibility;

pub const REGION_COORD_MASK: u8x3 = u8x3::from_array([0b11111000, 0b11111100, 0b11111000]);
pub const SECTIONS_IN_GRAPH: usize = 256 * 256 * 256;

pub const MAX_VIEW_DISTANCE: u8 = 127;
Expand Down Expand Up @@ -61,7 +54,7 @@ pub const fn get_bfs_queue_max_size(section_render_distance: u8, world_height: u
count += 4 * (max_width_traversal - layer_index) * (max_width_traversal + layer_index - 1);

// add final, outer-most ring.
count += (max_width_traversal * 4);
count += max_width_traversal * 4;

// TODO: i'm pretty sure this only holds true when we do checks on the nodes before enqueueing
// them. however, this would result in a lot of excess checks when multiple nodes try to queue
Expand All @@ -82,7 +75,7 @@ pub struct BfsCachedState {

impl BfsCachedState {
pub fn reset(&mut self) {
self.incoming_directions.fill(GraphDirectionSet::none());
self.incoming_directions.fill(GraphDirectionSet::NONE);
}
}

Expand Down Expand Up @@ -197,19 +190,26 @@ impl Graph {
}
}

#[no_mangle]
fn bfs_and_occlusion_cull(
&mut self,
coord_context: &LocalCoordContext,
disable_occlusion_culling: bool,
) -> SortedSearchResults {
let directions_modifier = if disable_occlusion_culling {
GraphDirectionSet::ALL
} else {
GraphDirectionSet::NONE
};

let mut read_queue = BfsQueue::default();
let mut write_queue = BfsQueue::default();

let initial_node_index = coord_context.camera_section_index;
read_queue.push(initial_node_index);
initial_node_index
.index_array_unchecked_mut(&mut self.bfs_cached_state.incoming_directions)
.add_all(GraphDirectionSet::all());
.add_all(GraphDirectionSet::ALL);

let mut read_queue_ref = &mut read_queue;
let mut write_queue_ref = &mut write_queue;
Expand All @@ -218,7 +218,7 @@ impl Graph {
while !finished {
finished = true;

'node: while let Some(&node_index) = read_queue.pop() {
'node: while let Some(&node_index) = read_queue_ref.pop() {
finished = false;

if !self
Expand All @@ -240,21 +240,22 @@ impl Graph {
let node_incoming_directions =
*node_index.index_array_unchecked(&self.bfs_cached_state.incoming_directions);

let node_outgoing_directions = node_index
let mut node_outgoing_directions = node_index
.index_array_unchecked(&self.section_visibility_bit_sets)
.get_outgoing_directions(node_incoming_directions)
& coord_context.get_valid_directions(node_pos);
.get_outgoing_directions(node_incoming_directions);
node_outgoing_directions.add_all(directions_modifier);
node_outgoing_directions &= coord_context.get_valid_directions(node_pos);

// use the outgoing directions to get the neighbors that could possibly be enqueued
let node_neighbor_indices = node_index.get_all_neighbors();

for direction in node_outgoing_directions {
let neighbor = node_neighbor_indices.get(direction);
let neighbor_index = node_neighbor_indices.get(direction);

// the outgoing direction for the current node is the incoming direction for the neighbor
let current_incoming_direction = direction.opposite();

let neighbor_incoming_directions = node_index
let neighbor_incoming_directions = neighbor_index
.index_array_unchecked_mut(&mut self.bfs_cached_state.incoming_directions);

// enqueue only if the node has not yet been enqueued, avoiding duplicates
Expand All @@ -263,13 +264,14 @@ impl Graph {
neighbor_incoming_directions.add(current_incoming_direction);

unsafe {
write_queue.push_conditionally_unchecked(node_index, should_enqueue);
write_queue_ref
.push_conditionally_unchecked(neighbor_index, should_enqueue);
}
}
}

read_queue.reset();
swap(&mut read_queue, &mut write_queue);
read_queue_ref.reset();
swap(&mut read_queue_ref, &mut write_queue_ref);

self.bfs_cached_state.reset();
}
Expand Down
1 change: 0 additions & 1 deletion native/core/src/graph/octree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::mem::size_of;

use core_simd::simd::*;

use crate::graph::local::*;
use crate::graph::*;

// operations on u8x64 are faster in some cases compared to u64x8
Expand Down
50 changes: 26 additions & 24 deletions native/core/src/graph/visibility.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::mem::transmute;
use std::ops::BitAnd;
use std::ops::{BitAnd, BitAndAssign};

use core_simd::simd::Which::*;
use core_simd::simd::*;
Expand Down Expand Up @@ -50,32 +50,28 @@ impl GraphDirection {
pub struct GraphDirectionSet(u8);

impl GraphDirectionSet {
#[inline(always)]
pub fn from(packed: u8) -> Self {
GraphDirectionSet(packed)
}
pub const NONE: Self = GraphDirectionSet(0);

#[inline(always)]
pub fn none() -> GraphDirectionSet {
GraphDirectionSet(0)
}

#[inline(always)]
pub fn all() -> GraphDirectionSet {
let mut set = GraphDirectionSet::none();
pub const ALL: Self = {
let mut set = 0_u8;

for dir in GraphDirection::ORDERED {
set.add(dir);
let mut i = 0;
while i < GraphDirection::ORDERED.len() {
set |= 1 << GraphDirection::ORDERED[i] as u8;
i += 1;
}

set
GraphDirectionSet(set)
};

#[inline(always)]
pub const fn from(packed: u8) -> Self {
GraphDirectionSet(packed)
}

#[inline(always)]
pub fn single(direction: GraphDirection) -> GraphDirectionSet {
let mut set = GraphDirectionSet::none();
set.add(direction);
set
pub const fn single(direction: GraphDirection) -> GraphDirectionSet {
GraphDirectionSet(1 << direction as u8)
}

#[inline(always)]
Expand All @@ -89,19 +85,19 @@ impl GraphDirectionSet {
}

#[inline(always)]
pub fn contains(&self, dir: GraphDirection) -> bool {
pub const fn contains(&self, dir: GraphDirection) -> bool {
(self.0 & (1 << dir as u8)) != 0
}

#[inline(always)]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.0 == 0
}
}

impl Default for GraphDirectionSet {
fn default() -> Self {
GraphDirectionSet::none()
GraphDirectionSet::NONE
}
}

Expand All @@ -113,6 +109,12 @@ impl BitAnd for GraphDirectionSet {
}
}

impl BitAndAssign for GraphDirectionSet {
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}

impl IntoIterator for GraphDirectionSet {
type Item = GraphDirection;
type IntoIter = GraphDirectionSetIter;
Expand All @@ -136,7 +138,7 @@ impl Iterator for GraphDirectionSetIter {
// SAFETY: the result from a valid GraphDirectionSet value should never be out of bounds
let direction =
unsafe { GraphDirection::from_int_unchecked(self.0.trailing_zeros() as u8) };
self.0 &= (self.0 - 1);
self.0 &= self.0 - 1;
Some(direction)
} else {
None
Expand Down
1 change: 0 additions & 1 deletion native/core/src/jni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pub mod types {
use std::marker::PhantomData;
use std::mem::forget;

pub type JEnv = std::ffi::c_void;
pub type JClass = std::ffi::c_void;
Expand Down
1 change: 0 additions & 1 deletion native/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(unused)]
#![feature(portable_simd)]
#![feature(cell_leak)]
#![feature(maybe_uninit_slice)]
Expand Down
Loading

0 comments on commit 8202ff7

Please sign in to comment.