2019-01-14 17:20:36 -05:00
|
|
|
// pathfinder/renderer/src/z_buffer.rs
|
|
|
|
//
|
|
|
|
// Copyright © 2019 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Software occlusion culling.
|
|
|
|
|
2019-03-29 22:11:38 -04:00
|
|
|
use crate::gpu_data::SolidTileBatchPrimitive;
|
2019-04-11 22:38:31 -04:00
|
|
|
use crate::tile_map::DenseTileMap;
|
2019-01-14 17:20:36 -05:00
|
|
|
use crate::tiles;
|
2019-04-11 22:38:31 -04:00
|
|
|
use pathfinder_geometry::basic::point::Point2DI32;
|
2019-02-05 13:55:01 -05:00
|
|
|
use pathfinder_geometry::basic::rect::{RectF32, RectI32};
|
2019-03-29 22:11:38 -04:00
|
|
|
use std::ops::Range;
|
2019-01-14 17:20:36 -05:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
|
|
|
|
|
|
|
|
pub struct ZBuffer {
|
2019-04-11 22:38:31 -04:00
|
|
|
buffer: DenseTileMap<AtomicUsize>,
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ZBuffer {
|
2019-02-05 13:03:20 -05:00
|
|
|
pub fn new(view_box: RectF32) -> ZBuffer {
|
2019-01-14 17:20:36 -05:00
|
|
|
let tile_rect = tiles::round_rect_out_to_tile_bounds(view_box);
|
2019-04-11 22:38:31 -04:00
|
|
|
ZBuffer { buffer: DenseTileMap::from_builder(|_| AtomicUsize::new(0), tile_rect) }
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:38:31 -04:00
|
|
|
pub fn test(&self, coords: Point2DI32, object_index: u32) -> bool {
|
|
|
|
let tile_index = self.buffer.coords_to_index_unchecked(coords);
|
|
|
|
let existing_depth = self.buffer.data[tile_index as usize].load(AtomicOrdering::SeqCst);
|
2019-01-14 17:20:36 -05:00
|
|
|
existing_depth < object_index as usize + 1
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:38:31 -04:00
|
|
|
pub fn update(&self, coords: Point2DI32, object_index: u16) {
|
|
|
|
let tile_index = self.buffer.coords_to_index_unchecked(coords);
|
|
|
|
let mut old_depth = self.buffer.data[tile_index].load(AtomicOrdering::SeqCst);
|
2019-01-14 17:20:36 -05:00
|
|
|
let new_depth = (object_index + 1) as usize;
|
|
|
|
while old_depth < new_depth {
|
2019-04-11 22:38:31 -04:00
|
|
|
let prev_depth = self.buffer.data[tile_index].compare_and_swap(
|
2019-01-14 17:20:36 -05:00
|
|
|
old_depth,
|
|
|
|
new_depth,
|
|
|
|
AtomicOrdering::SeqCst,
|
|
|
|
);
|
|
|
|
if prev_depth == old_depth {
|
|
|
|
// Successfully written.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
old_depth = prev_depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:11:38 -04:00
|
|
|
pub fn build_solid_tiles(&self, tile_rect: RectI32, object_range: Range<u32>)
|
|
|
|
-> Vec<SolidTileBatchPrimitive> {
|
2019-01-14 17:20:36 -05:00
|
|
|
let mut solid_tiles = vec![];
|
2019-04-11 22:38:31 -04:00
|
|
|
for tile_index in 0..self.buffer.data.len() {
|
|
|
|
let depth = self.buffer.data[tile_index].load(AtomicOrdering::Relaxed);
|
|
|
|
if depth == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tile_coords = self.buffer.index_to_coords(tile_index);
|
|
|
|
let object_index = (depth - 1) as u32;
|
|
|
|
if object_index < object_range.start || object_index >= object_range.end {
|
|
|
|
continue;
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
2019-04-11 22:38:31 -04:00
|
|
|
solid_tiles.push(SolidTileBatchPrimitive {
|
|
|
|
tile_x: (tile_coords.x() + tile_rect.min_x()) as i16,
|
|
|
|
tile_y: (tile_coords.y() + tile_rect.min_y()) as i16,
|
|
|
|
object_index: object_index as u16,
|
|
|
|
});
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
solid_tiles
|
|
|
|
}
|
|
|
|
}
|