Skip to content

Commit

Permalink
use channels
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Jun 12, 2023
1 parent f6e1f97 commit db97ffc
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 180 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ bevy_gltf = ["bevy_internal/bevy_gltf", "bevy_asset", "bevy_scene", "bevy_pbr"]
# Adds PBR rendering
bevy_pbr = ["bevy_internal/bevy_pbr", "bevy_asset", "bevy_render", "bevy_core_pipeline"]

gpu_picking = []

# Provides rendering functionality
bevy_render = ["bevy_internal/bevy_render"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_render::{
camera::ExtractedCamera,
picking::{EntityTextures, GpuPickingCamera},
picking::{EntityTextures, ExtractedGpuPickingCamera},
render_graph::{NodeRunError, RenderGraphContext, ViewNode},
render_phase::RenderPhase,
render_resource::{
Expand Down Expand Up @@ -35,7 +35,7 @@ impl ViewNode for MainOpaquePass3dNode {
Option<&'static DepthPrepass>,
Option<&'static NormalPrepass>,
Option<&'static MotionVectorPrepass>,
Option<&'static GpuPickingCamera>,
Option<&'static ExtractedGpuPickingCamera>,
Option<&'static SkyboxPipelineId>,
Option<&'static SkyboxBindGroup>,
Option<&'static EntityTextures>,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl ViewNode for MainOpaquePass3dNode {
load: match camera_3d.clear_color {
ClearColorConfig::None => LoadOp::Load,
// TODO clear this earlier?
_ => LoadOp::Clear(EntityTextures::clear_color()),
_ => LoadOp::Clear(EntityTextures::no_entity_color()),
},
store: true,
})));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core_3d::Transparent3d;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_render::{
camera::ExtractedCamera,
picking::{EntityTextures, GpuPickingCamera},
picking::{EntityTextures, ExtractedGpuPickingCamera},
render_graph::{NodeRunError, RenderGraphContext, ViewNode},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDepthStencilAttachment, RenderPassDescriptor},
Expand All @@ -22,7 +22,7 @@ impl ViewNode for MainTransparentPass3dNode {
&'static RenderPhase<Transparent3d>,
&'static ViewTarget,
&'static ViewDepthTexture,
Option<&'static GpuPickingCamera>,
Option<&'static ExtractedGpuPickingCamera>,
Option<&'static EntityTextures>,
);
fn run(
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use bevy_ecs::prelude::*;
use bevy_render::{
camera::{Camera, ExtractedCamera},
extract_component::ExtractComponentPlugin,
picking::{EntityTextures, GpuPickingCamera, ENTITY_TEXTURE_FORMAT},
picking::{EntityTextures, ExtractedGpuPickingCamera, ENTITY_TEXTURE_FORMAT},
prelude::Msaa,
render_graph::{EmptyNode, RenderGraphApp, ViewNodeRunner},
render_phase::{
Expand Down Expand Up @@ -86,6 +86,7 @@ impl Plugin for Core3dPlugin {
prepare_core_3d_depth_textures
.in_set(RenderSet::Prepare)
.after(bevy_render::view::prepare_windows),
// #[cfg(feature = "gpu_picking")]
prepare_entity_textures
.in_set(RenderSet::Prepare)
.after(bevy_render::view::prepare_windows),
Expand Down Expand Up @@ -322,13 +323,14 @@ pub fn prepare_core_3d_depth_textures(
}
}

/// Create the required buffers based on the camera size
pub fn prepare_entity_textures(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
msaa: Res<Msaa>,
render_device: Res<RenderDevice>,
views_3d: Query<
(Entity, &ExtractedCamera, Option<&GpuPickingCamera>),
(Entity, &ExtractedCamera, Option<&ExtractedGpuPickingCamera>),
(With<RenderPhase<Opaque3d>>, With<RenderPhase<AlphaMask3d>>),
>,
) {
Expand Down Expand Up @@ -377,7 +379,6 @@ pub fn prepare_entity_textures(
)
}),
};

commands.entity(entity).insert(entity_textures);
}
}
18 changes: 6 additions & 12 deletions crates/bevy_core_pipeline/src/entity_index_buffer_copy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_app::Plugin;
use bevy_ecs::{query::QueryItem, world::World};
use bevy_render::{
picking::{EntityTextures, GpuPickingCamera},
picking::{EntityTextures, ExtractedGpuPickingCamera},
render_graph::{RenderGraphApp, RenderGraphContext, ViewNode, ViewNodeRunner},
renderer::RenderContext,
RenderApp,
Expand All @@ -12,7 +12,7 @@ use crate::core_3d::CORE_3D;
#[derive(Default)]
pub struct EntityIndexBufferCopyNode;
impl ViewNode for EntityIndexBufferCopyNode {
type ViewQuery = (&'static EntityTextures, &'static GpuPickingCamera);
type ViewQuery = (&'static EntityTextures, &'static ExtractedGpuPickingCamera);

fn run(
&self,
Expand All @@ -21,29 +21,23 @@ impl ViewNode for EntityIndexBufferCopyNode {
(entity_index_textures, gpu_picking_camera): QueryItem<Self::ViewQuery>,
_world: &World,
) -> Result<(), bevy_render::render_graph::NodeRunError> {
let Ok(mut guard) = gpu_picking_camera.0.try_lock() else { return Ok(()); };
let Some(buffers) = guard.as_mut() else { return Ok(()); };
let Some(buffers) = gpu_picking_camera.buffers.as_ref() else {
return Ok(());
};

// copy entity index texture
buffers.copy_texture_to_buffer(
render_context.command_encoder(),
&entity_index_textures.main.texture,
&buffers.entity_buffer,
);

// TODO copy depth

Ok(())
}
}

pub struct EntityIndexBufferCopyPlugin;
impl Plugin for EntityIndexBufferCopyPlugin {
fn build(&self, app: &mut bevy_app::App) {
let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
};
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { return; };

// 3D
use crate::core_3d::graph::node::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ use bevy_reflect::{
use bevy_render::{
color::Color,
mesh::Mesh,
picking,
primitives::Aabb,
render_phase::AddRenderCommand,
render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines},
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_transform::components::{GlobalTransform, Transform};

// #[cfg(feature = "gpu_picking")]
use bevy_render::picking;

#[cfg(feature = "bevy_pbr")]
use bevy_pbr::MeshUniform;
#[cfg(feature = "bevy_sprite")]
Expand Down Expand Up @@ -326,6 +328,7 @@ fn extract_gizmo_data(
transform,
previous_transform: transform,
inverse_transpose_model,
// #[cfg(feature = "gpu_picking")]
entity: picking::entity_as_uvec2(Entity::PLACEHOLDER),
},
),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["bevy"]

[features]
webgl = []
gpu_picking = []

[dependencies]
# bevy
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bevy_reflect::{TypePath, TypeUuid};
use bevy_render::{
extract_component::ExtractComponentPlugin,
mesh::{Mesh, MeshVertexBufferLayout},
picking::{GpuPickingCamera, GpuPickingMesh},
picking::{ExtractedGpuPickingCamera, GpuPickingMesh},
prelude::Image,
render_asset::{PrepareAssetSet, RenderAssets},
render_phase::{
Expand Down Expand Up @@ -394,7 +394,7 @@ pub fn queue_material_meshes<M: Material>(
Option<&DebandDither>,
Option<&EnvironmentMapLight>,
Option<&NormalPrepass>,
Option<&GpuPickingCamera>,
Option<&ExtractedGpuPickingCamera>,
Option<&TemporalAntiAliasSettings>,
&mut RenderPhase<Opaque3d>,
&mut RenderPhase<AlphaMask3d>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub struct MeshUniform {
pub transform: Mat4,
pub previous_transform: Mat4,
pub inverse_transpose_model: Mat4,
// TODO should probably be a separate uniform in case gpu picking is disabled
// #[cfg(feature = "gpu_picking")]
pub entity: UVec2,
pub flags: u32,
}
Expand Down Expand Up @@ -187,6 +187,7 @@ pub fn extract_meshes(
flags: flags.bits(),
transform,
previous_transform,
// #[cfg(feature = "gpu_picking")]
entity: picking::entity_as_uvec2(entity),
inverse_transpose_model: transform.inverse().transpose(),
};
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub mod prelude {
use bevy_window::{PrimaryWindow, RawHandleWrapper};
use globals::GlobalsPlugin;
pub use once_cell;
use picking::GpuPickingPlugin;
use renderer::{RenderAdapter, RenderAdapterInfo, RenderDevice, RenderQueue};
use wgpu::Instance;

Expand Down Expand Up @@ -336,8 +335,7 @@ impl Plugin for RenderPlugin {
.add_plugin(CameraPlugin)
.add_plugin(ViewPlugin)
.add_plugin(MeshPlugin)
.add_plugin(GlobalsPlugin)
.add_plugin(GpuPickingPlugin);
.add_plugin(GlobalsPlugin);

app.register_type::<color::Color>()
.register_type::<primitives::Aabb>()
Expand Down
Loading

0 comments on commit db97ffc

Please sign in to comment.