Skip to content

Commit

Permalink
Misc renames + small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 committed Nov 11, 2023
1 parent b3dfadb commit 1c38878
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/meshlet/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;

#[derive(Asset, TypePath, Serialize, Deserialize, Clone)]
pub struct MeshletMesh {
pub total_meshlet_vertices: u64,
pub total_meshlet_indices: u64,
pub vertex_data: Arc<[u8]>,
pub vertex_ids: Arc<[u32]>,
pub indices: Arc<[u8]>,
Expand All @@ -20,7 +20,7 @@ pub struct MeshletMesh {
pub struct Meshlet {
pub start_vertex_id: u32,
pub start_index_id: u32,
pub vertex_count: u32,
pub index_count: u32,
}

#[derive(Serialize, Deserialize, Copy, Clone, Pod, Zeroable)]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/meshlet/cull_meshlets.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fn cull_meshlets(@builtin(global_invocation_id) thread_id: vec3<u32>) {

if meshlet_visible {
let meshlet = meshlets[meshlet_id];
let draw_index_buffer_start = atomicAdd(&draw_command_buffer.vertex_count, meshlet.vertex_count);
let draw_index_buffer_start = atomicAdd(&draw_command_buffer.vertex_count, meshlet.index_count);
let packed_thread_id = thread_id.x << 8u;
for (var index_id = 0u; index_id < meshlet.vertex_count; index_id++) {
for (var index_id = 0u; index_id < meshlet.index_count; index_id++) {
draw_index_buffer[draw_index_buffer_start + index_id] = packed_thread_id | index_id;
}
}
Expand Down
18 changes: 11 additions & 7 deletions crates/bevy_pbr/src/meshlet/from_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_render::{
};
use bevy_utils::thiserror;
use meshopt::{build_meshlets, compute_meshlet_bounds_decoder, VertexDataAdapter};
use std::{borrow::Cow, iter, sync::Arc};
use std::{borrow::Cow, iter};

impl MeshletMesh {
pub fn from_mesh(mesh: &Mesh) -> Result<Self, MeshToMeshletMeshConversionError> {
Expand Down Expand Up @@ -64,18 +64,22 @@ impl MeshletMesh {
.triangles
.extend(iter::repeat(0).take(padding));

let meshlets: Arc<[Meshlet]> = meshopt_meshlets
let mut total_meshlet_indices = 0;
let meshlets = meshopt_meshlets
.meshlets
.into_iter()
.map(|m| Meshlet {
start_vertex_id: m.vertex_offset,
start_index_id: m.triangle_offset,
vertex_count: m.triangle_count * 3,
.map(|m| {
total_meshlet_indices += m.triangle_count as u64 * 3;
Meshlet {
start_vertex_id: m.vertex_offset,
start_index_id: m.triangle_offset,
index_count: m.triangle_count * 3,
}
})
.collect();

Ok(Self {
total_meshlet_vertices: meshopt_meshlets.vertices.len() as u64,
total_meshlet_indices,
vertex_data: vertex_buffer.into(),
vertex_ids: meshopt_meshlets.vertices.into(),
indices: meshopt_meshlets.triangles.into(),
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_pbr/src/meshlet/gpu_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn prepare_meshlet_per_frame_resources(
// TODO: Should draw_index_buffer be per-view, or a single resource shared between all views?
let visibility_buffer_draw_index_buffer = render_device.create_buffer(&BufferDescriptor {
label: Some("meshlet_visibility_buffer_draw_index_buffer"),
size: 4 * gpu_scene.scene_vertex_count,
size: 4 * gpu_scene.scene_index_count,
usage: BufferUsages::STORAGE | BufferUsages::INDEX,
mapped_at_creation: false,
});
Expand Down Expand Up @@ -314,7 +314,7 @@ pub struct MeshletGpuScene {
meshlet_mesh_slices: HashMap<AssetId<MeshletMesh>, (Range<u32>, u64)>,

scene_meshlet_count: u32,
scene_vertex_count: u64,
scene_index_count: u64,
next_material_id: u32,
material_id_lookup: HashMap<UntypedAssetId, u32>,
material_ids_used: HashSet<u32>,
Expand Down Expand Up @@ -346,7 +346,7 @@ impl FromWorld for MeshletGpuScene {
meshlet_mesh_slices: HashMap::new(),

scene_meshlet_count: 0,
scene_vertex_count: 0,
scene_index_count: 0,
next_material_id: 0,
material_id_lookup: HashMap::new(),
material_ids_used: HashSet::new(),
Expand Down Expand Up @@ -413,7 +413,7 @@ impl MeshletGpuScene {
fn reset(&mut self) {
// TODO: Shrink capacity if saturation is low
self.scene_meshlet_count = 0;
self.scene_vertex_count = 0;
self.scene_index_count = 0;
self.next_material_id = 0;
self.material_id_lookup.clear();
self.material_ids_used.clear();
Expand Down Expand Up @@ -453,18 +453,18 @@ impl MeshletGpuScene {

(
(meshlets_slice.start as u32 / 12)..(meshlets_slice.end as u32 / 12),
meshlet_mesh.total_meshlet_vertices,
meshlet_mesh.total_meshlet_indices,
)
};

let (meshlets_slice, vertex_count) = self
let (meshlets_slice, index_count) = self
.meshlet_mesh_slices
.entry(handle.id())
.or_insert_with_key(queue_meshlet_mesh)
.clone();

self.scene_meshlet_count += meshlets_slice.end - meshlets_slice.start;
self.scene_vertex_count += vertex_count;
self.scene_index_count += index_count;
self.instances.push(instance);

for meshlet_index in meshlets_slice {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/meshlet/meshlet_bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn unpack_meshlet_vertex(packed: PackedMeshletVertex) -> MeshletVertex {
struct Meshlet {
start_vertex_id: u32,
start_index_id: u32,
vertex_count: u32,
index_count: u32,
}

struct MeshletBoundingSphere {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/meshlet/persistent_buffer_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl PersistentGpuBufferable for Arc<[Meshlet]> {
bytemuck::cast::<_, [u8; 12]>(Meshlet {
start_vertex_id: meshlet.start_vertex_id + vertex_offset,
start_index_id: meshlet.start_index_id + index_offset,
vertex_count: meshlet.vertex_count,
index_count: meshlet.index_count,
})
})
.collect()
Expand Down

0 comments on commit 1c38878

Please sign in to comment.