Skip to content

Commit ab7902f

Browse files
committed
Move RenderMeshInstances, RenderMeshInstancesCpu, RenderMeshInstancesGpu, RenderMeshQueueData to bevy_render
1 parent d50b151 commit ab7902f

File tree

2 files changed

+138
-134
lines changed

2 files changed

+138
-134
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use bevy_render::{
4242
mesh::{allocator::MeshAllocator, RenderMesh, RenderMeshBufferInfo},
4343
render_asset::RenderAssets,
4444
render_phase::{
45-
BinnedRenderPhasePlugin, InputUniformIndex, PhaseItem, PhaseItemExtraIndex, RenderCommand,
45+
BinnedRenderPhasePlugin, PhaseItem, PhaseItemExtraIndex, RenderCommand,
4646
RenderCommandResult, SortedRenderPhasePlugin, TrackedRenderPass,
4747
},
4848
render_resource::*,
@@ -522,124 +522,6 @@ pub struct RenderMeshInstanceGpuQueues(Parallel<RenderMeshInstanceGpuQueue>);
522522
#[derive(Resource, Default, Deref, DerefMut)]
523523
pub struct MeshesToReextractNextFrame(MainEntityHashSet);
524524

525-
/// Information that the render world keeps about each entity that contains a
526-
/// mesh.
527-
///
528-
/// The set of information needed is different depending on whether CPU or GPU
529-
/// [`MeshUniform`] building is in use.
530-
#[derive(Resource)]
531-
pub enum RenderMeshInstances {
532-
/// Information needed when using CPU mesh instance data building.
533-
CpuBuilding(RenderMeshInstancesCpu),
534-
/// Information needed when using GPU mesh instance data building.
535-
GpuBuilding(RenderMeshInstancesGpu),
536-
}
537-
538-
/// Information that the render world keeps about each entity that contains a
539-
/// mesh, when using CPU mesh instance data building.
540-
#[derive(Default, Deref, DerefMut)]
541-
pub struct RenderMeshInstancesCpu(MainEntityHashMap<RenderMeshInstanceCpu>);
542-
543-
/// Information that the render world keeps about each entity that contains a
544-
/// mesh, when using GPU mesh instance data building.
545-
#[derive(Default, Deref, DerefMut)]
546-
pub struct RenderMeshInstancesGpu(MainEntityHashMap<RenderMeshInstanceGpu>);
547-
548-
impl RenderMeshInstances {
549-
/// Creates a new [`RenderMeshInstances`] instance.
550-
fn new(use_gpu_instance_buffer_builder: bool) -> RenderMeshInstances {
551-
if use_gpu_instance_buffer_builder {
552-
RenderMeshInstances::GpuBuilding(RenderMeshInstancesGpu::default())
553-
} else {
554-
RenderMeshInstances::CpuBuilding(RenderMeshInstancesCpu::default())
555-
}
556-
}
557-
558-
/// Returns the ID of the mesh asset attached to the given entity, if any.
559-
pub fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
560-
match *self {
561-
RenderMeshInstances::CpuBuilding(ref instances) => instances.mesh_asset_id(entity),
562-
RenderMeshInstances::GpuBuilding(ref instances) => instances.mesh_asset_id(entity),
563-
}
564-
}
565-
566-
/// Constructs [`RenderMeshQueueData`] for the given entity, if it has a
567-
/// mesh attached.
568-
pub fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
569-
match *self {
570-
RenderMeshInstances::CpuBuilding(ref instances) => {
571-
instances.render_mesh_queue_data(entity)
572-
}
573-
RenderMeshInstances::GpuBuilding(ref instances) => {
574-
instances.render_mesh_queue_data(entity)
575-
}
576-
}
577-
}
578-
579-
/// Inserts the given flags into the CPU or GPU render mesh instance data
580-
/// for the given mesh as appropriate.
581-
fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
582-
match *self {
583-
RenderMeshInstances::CpuBuilding(ref mut instances) => {
584-
instances.insert_mesh_instance_flags(entity, flags);
585-
}
586-
RenderMeshInstances::GpuBuilding(ref mut instances) => {
587-
instances.insert_mesh_instance_flags(entity, flags);
588-
}
589-
}
590-
}
591-
}
592-
593-
impl RenderMeshInstancesCpu {
594-
fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
595-
self.get(&entity)
596-
.map(|render_mesh_instance| render_mesh_instance.mesh_asset_id)
597-
}
598-
599-
pub fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
600-
self.get(&entity)
601-
.map(|render_mesh_instance| RenderMeshQueueData {
602-
shared: &render_mesh_instance.shared,
603-
translation: render_mesh_instance.transforms.world_from_local.translation,
604-
current_uniform_index: InputUniformIndex::default(),
605-
})
606-
}
607-
608-
/// Inserts the given flags into the render mesh instance data for the given
609-
/// mesh.
610-
fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
611-
if let Some(instance) = self.get_mut(&entity) {
612-
instance.flags.insert(flags);
613-
}
614-
}
615-
}
616-
617-
impl RenderMeshInstancesGpu {
618-
fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
619-
self.get(&entity)
620-
.map(|render_mesh_instance| render_mesh_instance.mesh_asset_id)
621-
}
622-
623-
fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
624-
self.get(&entity)
625-
.map(|render_mesh_instance| RenderMeshQueueData {
626-
shared: &render_mesh_instance.shared,
627-
translation: render_mesh_instance.translation,
628-
current_uniform_index: InputUniformIndex(
629-
render_mesh_instance.current_uniform_index.into(),
630-
),
631-
})
632-
}
633-
634-
/// Inserts the given flags into the render mesh instance data for the given
635-
/// mesh.
636-
fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
637-
if let Some(instance) = self.get_mut(&entity) {
638-
instance.flags.insert(flags);
639-
}
640-
}
641-
}
642-
643525
impl RenderMeshInstanceGpuQueue {
644526
/// Clears out a [`RenderMeshInstanceGpuQueue`], creating or recreating it
645527
/// as necessary.
@@ -912,20 +794,6 @@ impl Default for MeshCullingDataBuffer {
912794
}
913795
}
914796

915-
/// Data that [`crate::material::queue_material_meshes`] and similar systems
916-
/// need in order to place entities that contain meshes in the right batch.
917-
#[derive(Deref)]
918-
pub struct RenderMeshQueueData<'a> {
919-
/// General information about the mesh instance.
920-
#[deref]
921-
pub shared: &'a RenderMeshInstanceShared,
922-
/// The translation of the mesh instance.
923-
pub translation: Vec3,
924-
/// The index of the [`MeshInputUniform`] in the GPU buffer for this mesh
925-
/// instance.
926-
pub current_uniform_index: InputUniformIndex,
927-
}
928-
929797
/// A [`SystemSet`] that encompasses both [`extract_meshes_for_cpu_building`]
930798
/// and [`extract_meshes_for_gpu_building`].
931799
#[derive(SystemSet, Clone, PartialEq, Eq, Debug, Hash)]

crates/bevy_render/src/mesh/render.rs

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ use bevy_render::sync_world::{MainEntity, MainEntityHashMap};
1515
use bytemuck::{Pod, Zeroable};
1616
use nonmax::{NonMaxU16, NonMaxU32};
1717

18-
use crate::{lightmap::{pack_lightmap_uv_rect, LightmapSlabIndex, LightmapSlotIndex}, mesh::material_bind_group::{MaterialBindGroupSlot, MaterialBindingId}};
18+
use crate::{
19+
lightmap::{pack_lightmap_uv_rect, LightmapSlabIndex, LightmapSlotIndex},
20+
mesh::material_bind_group::{MaterialBindGroupSlot, MaterialBindingId},
21+
render_phase::InputUniformIndex
22+
};
1923

2024

2125

@@ -365,6 +369,138 @@ impl RenderMeshInstanceShared {
365369
}
366370
}
367371

372+
/// Information that the render world keeps about each entity that contains a
373+
/// mesh.
374+
///
375+
/// The set of information needed is different depending on whether CPU or GPU
376+
/// [`MeshUniform`] building is in use.
377+
#[derive(Resource)]
378+
pub enum RenderMeshInstances {
379+
/// Information needed when using CPU mesh instance data building.
380+
CpuBuilding(RenderMeshInstancesCpu),
381+
/// Information needed when using GPU mesh instance data building.
382+
GpuBuilding(RenderMeshInstancesGpu),
383+
}
384+
385+
/// Information that the render world keeps about each entity that contains a
386+
/// mesh, when using CPU mesh instance data building.
387+
#[derive(Default, Deref, DerefMut)]
388+
pub struct RenderMeshInstancesCpu(MainEntityHashMap<RenderMeshInstanceCpu>);
389+
390+
/// Information that the render world keeps about each entity that contains a
391+
/// mesh, when using GPU mesh instance data building.
392+
#[derive(Default, Deref, DerefMut)]
393+
pub struct RenderMeshInstancesGpu(MainEntityHashMap<RenderMeshInstanceGpu>);
394+
395+
impl RenderMeshInstances {
396+
/// Creates a new [`RenderMeshInstances`] instance.
397+
pub fn new(use_gpu_instance_buffer_builder: bool) -> RenderMeshInstances {
398+
if use_gpu_instance_buffer_builder {
399+
RenderMeshInstances::GpuBuilding(RenderMeshInstancesGpu::default())
400+
} else {
401+
RenderMeshInstances::CpuBuilding(RenderMeshInstancesCpu::default())
402+
}
403+
}
404+
405+
/// Returns the ID of the mesh asset attached to the given entity, if any.
406+
pub fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
407+
match *self {
408+
RenderMeshInstances::CpuBuilding(ref instances) => instances.mesh_asset_id(entity),
409+
RenderMeshInstances::GpuBuilding(ref instances) => instances.mesh_asset_id(entity),
410+
}
411+
}
412+
413+
/// Constructs [`RenderMeshQueueData`] for the given entity, if it has a
414+
/// mesh attached.
415+
pub fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
416+
match *self {
417+
RenderMeshInstances::CpuBuilding(ref instances) => {
418+
instances.render_mesh_queue_data(entity)
419+
}
420+
RenderMeshInstances::GpuBuilding(ref instances) => {
421+
instances.render_mesh_queue_data(entity)
422+
}
423+
}
424+
}
425+
426+
/// Inserts the given flags into the CPU or GPU render mesh instance data
427+
/// for the given mesh as appropriate.
428+
pub fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
429+
match *self {
430+
RenderMeshInstances::CpuBuilding(ref mut instances) => {
431+
instances.insert_mesh_instance_flags(entity, flags);
432+
}
433+
RenderMeshInstances::GpuBuilding(ref mut instances) => {
434+
instances.insert_mesh_instance_flags(entity, flags);
435+
}
436+
}
437+
}
438+
}
439+
440+
impl RenderMeshInstancesCpu {
441+
fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
442+
self.get(&entity)
443+
.map(|render_mesh_instance| render_mesh_instance.mesh_asset_id)
444+
}
445+
446+
pub fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
447+
self.get(&entity)
448+
.map(|render_mesh_instance| RenderMeshQueueData {
449+
shared: &render_mesh_instance.shared,
450+
translation: render_mesh_instance.transforms.world_from_local.translation,
451+
current_uniform_index: InputUniformIndex::default(),
452+
})
453+
}
454+
455+
/// Inserts the given flags into the render mesh instance data for the given
456+
/// mesh.
457+
fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
458+
if let Some(instance) = self.get_mut(&entity) {
459+
instance.flags.insert(flags);
460+
}
461+
}
462+
}
463+
464+
impl RenderMeshInstancesGpu {
465+
fn mesh_asset_id(&self, entity: MainEntity) -> Option<AssetId<Mesh>> {
466+
self.get(&entity)
467+
.map(|render_mesh_instance| render_mesh_instance.mesh_asset_id)
468+
}
469+
470+
fn render_mesh_queue_data(&self, entity: MainEntity) -> Option<RenderMeshQueueData<'_>> {
471+
self.get(&entity)
472+
.map(|render_mesh_instance| RenderMeshQueueData {
473+
shared: &render_mesh_instance.shared,
474+
translation: render_mesh_instance.translation,
475+
current_uniform_index: InputUniformIndex(
476+
render_mesh_instance.current_uniform_index.into(),
477+
),
478+
})
479+
}
480+
481+
/// Inserts the given flags into the render mesh instance data for the given
482+
/// mesh.
483+
fn insert_mesh_instance_flags(&mut self, entity: MainEntity, flags: RenderMeshInstanceFlags) {
484+
if let Some(instance) = self.get_mut(&entity) {
485+
instance.flags.insert(flags);
486+
}
487+
}
488+
}
489+
490+
/// Data that [`crate::material::queue_material_meshes`] and similar systems
491+
/// need in order to place entities that contain meshes in the right batch.
492+
#[derive(Deref)]
493+
pub struct RenderMeshQueueData<'a> {
494+
/// General information about the mesh instance.
495+
#[deref]
496+
pub shared: &'a RenderMeshInstanceShared,
497+
/// The translation of the mesh instance.
498+
pub translation: Vec3,
499+
/// The index of the [`MeshInputUniform`] in the GPU buffer for this mesh
500+
/// instance.
501+
pub current_uniform_index: InputUniformIndex,
502+
}
503+
368504
/// Removes a [`MeshInputUniform`] corresponding to an entity that became
369505
/// invisible from the buffer.
370506
pub fn remove_mesh_input_uniform(

0 commit comments

Comments
 (0)