Skip to content

Commit

Permalink
Overwrite no-longer-in-use texels for debugging; polish up error pale…
Browse files Browse the repository at this point in the history
…tte colors.
  • Loading branch information
kpreid committed Oct 9, 2023
1 parent 2b831d0 commit 9e72329
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
72 changes: 52 additions & 20 deletions all-is-cubes-gpu/src/in_wgpu/block_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::sync::{Arc, Mutex, Weak};

use all_is_cubes::content::palette;
use all_is_cubes::euclid::Translation3D;
use all_is_cubes::math::{GridAab, GridCoordinate, VectorOps};
use all_is_cubes::time;
Expand Down Expand Up @@ -43,6 +44,14 @@ pub struct AtlasTile {
backing: Arc<Mutex<TileBacking>>,
}

/// Internal, weak-referencing version of [`AtlasTile`].
#[derive(Debug)]
struct WeakTile {
/// Bounds of the allocation in the atlas.
allocated_bounds: GridAab,
backing: Weak<Mutex<TileBacking>>,
}

/// Texture plane handle used by [`AtlasAllocator`].
///
/// This is public out of necessity but should not generally need to be used.
Expand Down Expand Up @@ -80,7 +89,7 @@ struct AllocatorBacking {
/// Weak references to every tile.
/// This is used to gather all data that needs to be flushed (written to the GPU
/// texture).
in_use: Vec<Weak<Mutex<TileBacking>>>,
in_use: Vec<WeakTile>,

/// Debug label for the GPU texture resource.
texture_label: String,
Expand Down Expand Up @@ -187,27 +196,48 @@ impl AtlasAllocator {
(texture, texture_view)
});

// Process tiles needing updates or deallocation.
let mut count_written = 0;
if backing.dirty {
backing.in_use.retain(|weak_backing| {
backing.in_use.retain(|weak_tile| {
// Process the non-dropped weak references
weak_backing.upgrade().map_or(false, |strong_backing| {
let backing: &mut TileBacking = &mut strong_backing.lock().unwrap();
if backing.dirty || copy_everything_anyway {
if let Some(data) = backing.data.as_ref() {
let region: GridAab = backing
.handle
.as_ref()
.expect("can't happen: dead TileBacking")
.allocation;

write_texture_by_aab(queue, texture, region, data);
backing.dirty = false;
count_written += 1;
match weak_tile.backing.upgrade() {
Some(strong_ref) => {
let backing: &mut TileBacking = &mut strong_ref.lock().unwrap();
if backing.dirty || copy_everything_anyway {
if let Some(data) = backing.data.as_ref() {
let region: GridAab = backing
.handle
.as_ref()
.expect("can't happen: dead TileBacking")
.allocation;

write_texture_by_aab(queue, texture, region, data);

backing.dirty = false;
count_written += 1;
}
}
true // retain in self.in_use
}
true // retain in self.in_use
})
None => {
// No strong references to the tile remain.
// Overwrite it to mark stale data.
// TODO: This is inefficient but we want to keep it at least until fixing
// <https://github.com/kpreid/all-is-cubes/issues/378>, at which point we
// might reasonably disable it.
let region = weak_tile.allocated_bounds;
write_texture_by_aab(
queue,
texture,
region,
// TODO: keep a preallocated GPU buffer instead
&vec![palette::UNALLOCATED_TEXELS_ERROR.to_srgb8(); region.volume()],
);

false // discard from self.in_use
}
}
});
}

Expand Down Expand Up @@ -247,6 +277,7 @@ impl texture::Allocator for AtlasAllocator {
let handle = allocator_backing
.alloctree
.allocate_with_growth(requested_bounds)?;
let allocated_bounds = handle.allocation;

let result = AtlasTile {
requested_bounds,
Expand All @@ -258,9 +289,10 @@ impl texture::Allocator for AtlasAllocator {
allocator: Arc::downgrade(&self.backing),
})),
};
allocator_backing
.in_use
.push(Arc::downgrade(&result.backing));
allocator_backing.in_use.push(WeakTile {
allocated_bounds,
backing: Arc::downgrade(&result.backing),
});
Some(result)
}
}
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-mesh/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub(super) fn copy_voxels_into_existing_texture<T: Tile>(voxels: &Evoxels, textu
texels.push(
voxels
.get(Cube { x, y, z })
.unwrap_or(Evoxel::from_color(palette::MISSING_VOXEL_FALLBACK))
.unwrap_or(Evoxel::from_color(palette::MISSING_VOXEL_ERROR))
.color
.to_srgb8(),
);
Expand Down
8 changes: 2 additions & 6 deletions all-is-cubes/src/block/evaluated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,8 @@ impl EvalBlockError {
// TODO: test this
pub fn to_placeholder(&self) -> EvaluatedBlock {
let resolution = Resolution::R8;
// TODO: dedicated palette colors, more detail (e.g. type of error as an icon)
let pattern = [
palette::MISSING_VOXEL_FALLBACK,
palette::MISSING_TEXTURE_FALLBACK,
]
.map(Evoxel::from_color);
// TODO: indicate type of error or at least have some kind of icon,
let pattern = [palette::BLOCK_EVAL_ERROR, Rgba::BLACK].map(Evoxel::from_color);

EvaluatedBlock::from_voxels(
BlockAttributes {
Expand Down
20 changes: 7 additions & 13 deletions all-is-cubes/src/content/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,14 @@ palette! {
/// Default sky color for new [`Space`](crate::space::Space)s.
DAY_SKY_COLOR = srgb[243 243 255];

// Rendering fallbacks.
/// Used on the surface of a mesh where there should be a texture, but something went
/// wrong.
///
/// TODO: This is no longer actually used, as the mesh generation now explicitly
/// reports flaws while using an approximate value. But perhaps we should use this
/// for another error condition.
MISSING_TEXTURE_FALLBACK = srgb[0xFF 0x00 0xBB 0xFF];
/// Used in texture atlases to mark areas that should not be visible.
UNALLOCATED_TEXELS_ERROR = srgb[0xFF 0x00 0xBB 0xFF];

/// Used as a placeholder appearance when a block definition fails to evaluate.
BLOCK_EVAL_ERROR = srgb[0xFF 0xBB 0x00 0xFF];

/// Used when a recursive block definition should have provided a voxel color but did not.
MISSING_VOXEL_FALLBACK = srgb[0xBB 0x00 0xFF 0xFF];
/// Used in unallocated texture atlas space.
///
/// TODO: Not currently used.
UNPAINTED_TEXTURE_FALLBACK = srgb[0x00 0xC5 0xC5 0xFF];
MISSING_VOXEL_ERROR = srgb[0xBB 0x00 0xFF 0xFF];

/// Fill color to draw when a renderer does not have any [`Space`](crate::space::Space)
/// to define a sky color.
Expand Down

0 comments on commit 9e72329

Please sign in to comment.