mirror of
https://github.com/IntQuant/noita_entangled_worlds.git
synced 2025-10-19 15:13:16 +00:00
remove some allocations
This commit is contained in:
parent
0ff7b16db3
commit
1ee7a2853f
4 changed files with 27 additions and 13 deletions
|
@ -32,7 +32,7 @@ impl State {
|
||||||
if let Some(cell) = self.particle_world_state.get_cell_raw(
|
if let Some(cell) = self.particle_world_state.get_cell_raw(
|
||||||
(x.floor() as isize).rem_euclid(512),
|
(x.floor() as isize).rem_euclid(512),
|
||||||
(y.floor() as isize).rem_euclid(512),
|
(y.floor() as isize).rem_euclid(512),
|
||||||
pixel_array,
|
unsafe { pixel_array.as_mut() }.unwrap(),
|
||||||
) {
|
) {
|
||||||
noita_api::print(format!("{cell:?}"));
|
noita_api::print(format!("{cell:?}"));
|
||||||
noita_api::print(format!("{:?}", unsafe { cell.material_ptr.as_ref() }));
|
noita_api::print(format!("{:?}", unsafe { cell.material_ptr.as_ref() }));
|
||||||
|
|
|
@ -45,7 +45,7 @@ fn init_particle_world_state(lua: LuaState) -> eyre::Result<()> {
|
||||||
STATE.with(|state| {
|
STATE.with(|state| {
|
||||||
let mut state = state.borrow_mut();
|
let mut state = state.borrow_mut();
|
||||||
let world_ptr = lua.to_integer(1) as *const noita::ntypes::GridWorld;
|
let world_ptr = lua.to_integer(1) as *const noita::ntypes::GridWorld;
|
||||||
let chunk_map_ptr = unsafe { world_ptr.read() }.chunk_map.cell_array;
|
let chunk_map_ptr = unsafe { world_ptr.as_ref().unwrap() }.chunk_map.cell_array;
|
||||||
let chunk_map = unsafe { std::slice::from_raw_parts(chunk_map_ptr, 512 * 512) };
|
let chunk_map = unsafe { std::slice::from_raw_parts(chunk_map_ptr, 512 * 512) };
|
||||||
let material_list_ptr = lua.to_integer(2) as *const noita::ntypes::CellData;
|
let material_list_ptr = lua.to_integer(2) as *const noita::ntypes::CellData;
|
||||||
let construct_ptr = lua.to_integer(3) as *const c_void;
|
let construct_ptr = lua.to_integer(3) as *const c_void;
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub(crate) mod ntypes;
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(crate) struct ParticleWorldState {
|
pub(crate) struct ParticleWorldState {
|
||||||
pub(crate) world_ptr: *const ntypes::GridWorld,
|
pub(crate) world_ptr: *const ntypes::GridWorld,
|
||||||
pub(crate) chunk_map: &'static [*const &'static mut [*const ntypes::Cell; 512 * 512]],
|
pub(crate) chunk_map: &'static [*mut &'static mut [*const ntypes::Cell; 512 * 512]],
|
||||||
//pub(crate) material_list_ptr: *const ntypes::CellData,
|
//pub(crate) material_list_ptr: *const ntypes::CellData,
|
||||||
pub(crate) blob_ptr: *const ntypes::CellData,
|
pub(crate) blob_ptr: *const ntypes::CellData,
|
||||||
pub(crate) construct_ptr: *const c_void,
|
pub(crate) construct_ptr: *const c_void,
|
||||||
|
@ -79,7 +79,14 @@ impl ParticleWorldState {
|
||||||
&self,
|
&self,
|
||||||
x: isize,
|
x: isize,
|
||||||
y: isize,
|
y: isize,
|
||||||
) -> Result<(isize, isize, &mut [*const ntypes::Cell; 512 * 512]), ()> {
|
) -> Result<
|
||||||
|
(
|
||||||
|
isize,
|
||||||
|
isize,
|
||||||
|
*mut &'static mut [*const ntypes::Cell; 512 * 512],
|
||||||
|
),
|
||||||
|
(),
|
||||||
|
> {
|
||||||
const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize;
|
const SCALE: isize = (512 / CHUNK_SIZE as isize).ilog2() as isize;
|
||||||
let shift_x = (x * CHUNK_SIZE as isize).rem_euclid(512);
|
let shift_x = (x * CHUNK_SIZE as isize).rem_euclid(512);
|
||||||
let shift_y = (y * CHUNK_SIZE as isize).rem_euclid(512);
|
let shift_y = (y * CHUNK_SIZE as isize).rem_euclid(512);
|
||||||
|
@ -88,14 +95,13 @@ impl ParticleWorldState {
|
||||||
if chunk.is_null() {
|
if chunk.is_null() {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
let pixel_array = unsafe { chunk.read() };
|
Ok((shift_x, shift_y, chunk))
|
||||||
Ok((shift_x, shift_y, pixel_array))
|
|
||||||
}
|
}
|
||||||
pub fn get_cell_raw(
|
pub fn get_cell_raw(
|
||||||
&self,
|
&self,
|
||||||
x: isize,
|
x: isize,
|
||||||
y: isize,
|
y: isize,
|
||||||
pixel_array: &mut [*const ntypes::Cell; 512 * 512],
|
pixel_array: &&mut [*const ntypes::Cell; 512 * 512],
|
||||||
) -> Option<&ntypes::Cell> {
|
) -> Option<&ntypes::Cell> {
|
||||||
let index = (y << 9) | x;
|
let index = (y << 9) | x;
|
||||||
let pixel = pixel_array[index as usize];
|
let pixel = pixel_array[index as usize];
|
||||||
|
@ -105,16 +111,16 @@ impl ParticleWorldState {
|
||||||
|
|
||||||
unsafe { pixel.as_ref() }
|
unsafe { pixel.as_ref() }
|
||||||
}
|
}
|
||||||
fn get_cell_raw_mut<'a>(
|
/*fn get_cell_raw_mut<'a>(
|
||||||
&self,
|
&self,
|
||||||
x: isize,
|
x: isize,
|
||||||
y: isize,
|
y: isize,
|
||||||
pixel_array: &'a mut [*const ntypes::Cell; 512 * 512],
|
pixel_array:&'a mut &'a mut [*const ntypes::Cell; 512 * 512],
|
||||||
) -> &'a mut *const ntypes::Cell {
|
) -> &'a mut *const ntypes::Cell {
|
||||||
let index = (y << 9) | x;
|
let index = (y << 9) | x;
|
||||||
&mut pixel_array[index as usize]
|
&mut pixel_array[index as usize]
|
||||||
}
|
}
|
||||||
/*fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
|
fn get_cell_material_id(&self, cell: &ntypes::Cell) -> u16 {
|
||||||
let mat_ptr = cell.material_ptr();
|
let mat_ptr = cell.material_ptr();
|
||||||
let offset = unsafe { mat_ptr.offset_from(self.material_list_ptr) };
|
let offset = unsafe { mat_ptr.offset_from(self.material_list_ptr) };
|
||||||
offset as u16
|
offset as u16
|
||||||
|
@ -131,6 +137,7 @@ impl ParticleWorldState {
|
||||||
chunk: &mut Chunk,
|
chunk: &mut Chunk,
|
||||||
) -> Result<(), ()> {
|
) -> Result<(), ()> {
|
||||||
let (shift_x, shift_y, pixel_array) = self.set_chunk(x, y)?;
|
let (shift_x, shift_y, pixel_array) = self.set_chunk(x, y)?;
|
||||||
|
let pixel_array = unsafe { pixel_array.as_mut() }.unwrap();
|
||||||
let mut modified = false;
|
let mut modified = false;
|
||||||
for ((i, j), pixel) in (0..CHUNK_SIZE as isize)
|
for ((i, j), pixel) in (0..CHUNK_SIZE as isize)
|
||||||
.flat_map(|i| (0..CHUNK_SIZE as isize).map(move |j| (i, j)))
|
.flat_map(|i| (0..CHUNK_SIZE as isize).map(move |j| (i, j)))
|
||||||
|
@ -169,8 +176,15 @@ impl ParticleWorldState {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let (shift_x, shift_y, pixel_array) = self.set_chunk(x, y)?;
|
let (shift_x, shift_y, pixel_array) = self.set_chunk(x, y)?;
|
||||||
|
let pixel_array = unsafe { pixel_array.as_mut() }.unwrap();
|
||||||
let x = x * CHUNK_SIZE as isize;
|
let x = x * CHUNK_SIZE as isize;
|
||||||
let y = y * CHUNK_SIZE as isize;
|
let y = y * CHUNK_SIZE as isize;
|
||||||
|
macro_rules! get_cell {
|
||||||
|
($x:expr, $y:expr, $pixel_array: tt) => {{
|
||||||
|
let index = ($y << 9) | $x;
|
||||||
|
&mut $pixel_array[index as usize]
|
||||||
|
}};
|
||||||
|
}
|
||||||
for ((i, j), pixel) in (0..CHUNK_SIZE as isize)
|
for ((i, j), pixel) in (0..CHUNK_SIZE as isize)
|
||||||
.flat_map(|i| (0..CHUNK_SIZE as isize).map(move |j| (i, j)))
|
.flat_map(|i| (0..CHUNK_SIZE as isize).map(move |j| (i, j)))
|
||||||
.zip(chunk.iter())
|
.zip(chunk.iter())
|
||||||
|
@ -179,7 +193,7 @@ impl ParticleWorldState {
|
||||||
CellType::Blob => {
|
CellType::Blob => {
|
||||||
let world_x = x + i;
|
let world_x = x + i;
|
||||||
let world_y = y + j;
|
let world_y = y + j;
|
||||||
let cell = self.get_cell_raw_mut(shift_x + i, shift_y + j, pixel_array);
|
let cell = get_cell!(shift_x + i, shift_y + j, pixel_array);
|
||||||
if !(*cell).is_null() {
|
if !(*cell).is_null() {
|
||||||
self.remove_cell(*cell, world_x, world_y);
|
self.remove_cell(*cell, world_x, world_y);
|
||||||
*cell = ptr::null_mut();
|
*cell = ptr::null_mut();
|
||||||
|
@ -196,7 +210,7 @@ impl ParticleWorldState {
|
||||||
let world_x = x + i;
|
let world_x = x + i;
|
||||||
let world_y = y + j;
|
let world_y = y + j;
|
||||||
std::thread::sleep(std::time::Duration::from_nanos(0));
|
std::thread::sleep(std::time::Duration::from_nanos(0));
|
||||||
let cell = self.get_cell_raw_mut(shift_x + i, shift_y + j, pixel_array);
|
let cell = get_cell!(shift_x + i, shift_y + j, pixel_array);
|
||||||
if !(*cell).is_null() {
|
if !(*cell).is_null() {
|
||||||
self.remove_cell(*cell, world_x, world_y);
|
self.remove_cell(*cell, world_x, world_y);
|
||||||
*cell = ptr::null_mut();
|
*cell = ptr::null_mut();
|
||||||
|
|
|
@ -48,7 +48,7 @@ pub(crate) struct ChunkArray(pub *mut CellArrayPtr);*/
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ChunkMap {
|
pub struct ChunkMap {
|
||||||
unknown: [isize; 2],
|
unknown: [isize; 2],
|
||||||
pub cell_array: *const *const &'static mut [*const Cell; 512 * 512],
|
pub cell_array: *const *mut &'static mut [*const Cell; 512 * 512],
|
||||||
unknown2: [isize; 8],
|
unknown2: [isize; 8],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue