Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate MeshUniforms on the GPU via compute shader where available. #12773

Merged
merged 54 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1bcbee8
Generate `MeshUniform`s on the GPU via compute shader where available.
pcwalton Mar 29, 2024
13ce172
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Mar 30, 2024
3e5e095
Add more documentation and disable on WebGL
pcwalton Mar 30, 2024
ea218fa
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Mar 30, 2024
9fe6ffb
Extract the lightmap UV rect.
pcwalton Mar 30, 2024
bc764fc
Remove needless `warn!`
pcwalton Mar 30, 2024
02d5a21
Prepare the bind group in a separate system
pcwalton Mar 30, 2024
c83c47a
Add some more documentation
pcwalton Mar 30, 2024
db817b3
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Mar 30, 2024
cc11119
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Mar 30, 2024
4559bd4
`using_gpu_uniform_builder` -> `use_gpu_uniform_builder`
pcwalton Apr 1, 2024
23c05a1
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 1, 2024
1bd64b7
Remove error spam
pcwalton Apr 1, 2024
a6e8a62
Use a system set instead of using `after`
pcwalton Apr 1, 2024
49cdeee
Make GPU mesh preparation per-view
pcwalton Apr 2, 2024
510a93a
Split out deletion of buffers to try to fix crashes
pcwalton Apr 2, 2024
5bbc05b
Add some documentation
pcwalton Apr 2, 2024
95f1de4
Rustfmt
pcwalton Apr 2, 2024
959c62d
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 2, 2024
1fc3d81
Remove some obsolete code
pcwalton Apr 3, 2024
e9024ea
Address review comment
pcwalton Apr 3, 2024
1813659
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 3, 2024
a5a492d
Address review comments
pcwalton Apr 3, 2024
0dd53a4
Rustfmt
pcwalton Apr 3, 2024
4abb071
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 4, 2024
309a7b6
Fix the issue reported by @Elabajaba.
pcwalton Apr 5, 2024
2b5155c
Doc check police
pcwalton Apr 5, 2024
32f8a87
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 5, 2024
d2a170c
Make sure the compiler never sees `BufferVec<()>`.
pcwalton Apr 5, 2024
1cf5e91
Reduce the number of `GetBatchData` traits down to two.
pcwalton Apr 5, 2024
18a5889
Move the GPU-preprocessing and non-GPU-preprocessing functionality into
pcwalton Apr 5, 2024
138ea94
Address review comments
pcwalton Apr 5, 2024
ff21dfe
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 5, 2024
2f4faba
Doc check police
pcwalton Apr 5, 2024
d904138
Fix deferred by moving the preprocess pipeline back
pcwalton Apr 6, 2024
f2eef8f
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 7, 2024
88cda71
Address a few review comments
pcwalton Apr 7, 2024
536fd9f
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 7, 2024
5ee2b97
Address review comment
pcwalton Apr 7, 2024
78a8199
Address some review comments
pcwalton Apr 7, 2024
87e3889
Address more review comments
pcwalton Apr 7, 2024
cb087c1
Factor out the enum switches.
pcwalton Apr 8, 2024
10dc0e4
Rename `get_batch_input_index` to `get_index_of_batch_input`.
pcwalton Apr 8, 2024
60138b7
Revert the change to split out `queue_material_meshes`, etc.
pcwalton Apr 8, 2024
aa7694c
Fix `WGPU_SETTINGS_PRIO="webgl2"` by doing proper GPU limits checks, and
pcwalton Apr 8, 2024
202e9eb
Address review comment
pcwalton Apr 8, 2024
17b09ed
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 8, 2024
3412687
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 9, 2024
eff1d4e
Rename `get_index_of_batch_data` and `get_index_of_binned_batch_input`
pcwalton Apr 9, 2024
87e5cbf
Rustfmt police
pcwalton Apr 9, 2024
4ff938d
Merge remote-tracking branch 'origin/main' into mesh-input-uniform
pcwalton Apr 9, 2024
9714d44
Run preprocessing before the shadow pass; seems to fix flickering issues
pcwalton Apr 9, 2024
a85cde1
Address review comments
pcwalton Apr 9, 2024
aacaa17
Fix formatting
pcwalton Apr 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub mod graph {
/// Label for the screen space ambient occlusion render node.
ScreenSpaceAmbientOcclusion,
DeferredLightingPass,
/// Label for the compute shader instance data building pass.
GpuPreprocess,
superdump marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -133,13 +135,19 @@ pub struct PbrPlugin {
pub prepass_enabled: bool,
/// Controls if [`DeferredPbrLightingPlugin`] is added.
pub add_default_deferred_lighting_plugin: bool,
/// Controls if GPU [`MeshUniform`] building is enabled.
///
/// This requires compute shader support and so will be forcibly disabled if
/// the platform doesn't support those.
pub use_gpu_instance_buffer_builder: bool,
}

impl Default for PbrPlugin {
fn default() -> Self {
Self {
prepass_enabled: true,
add_default_deferred_lighting_plugin: true,
use_gpu_instance_buffer_builder: true,
}
}
}
Expand Down Expand Up @@ -280,7 +288,9 @@ impl Plugin for PbrPlugin {
.register_type::<DefaultOpaqueRendererMethod>()
.init_resource::<DefaultOpaqueRendererMethod>()
.add_plugins((
MeshRenderPlugin,
MeshRenderPlugin {
use_gpu_instance_buffer_builder: self.use_gpu_instance_buffer_builder,
},
MaterialPlugin::<StandardMaterial> {
prepass_enabled: self.prepass_enabled,
..Default::default()
Expand All @@ -292,6 +302,9 @@ impl Plugin for PbrPlugin {
ExtractComponentPlugin::<ShadowFilteringMethod>::default(),
LightmapPlugin,
LightProbePlugin,
GpuMeshPreprocessPlugin {
use_gpu_instance_buffer_builder: self.use_gpu_instance_buffer_builder,
},
))
.configure_sets(
PostUpdate,
Expand Down Expand Up @@ -386,15 +399,6 @@ impl Plugin for PbrPlugin {
let draw_3d_graph = graph.get_sub_graph_mut(Core3d).unwrap();
draw_3d_graph.add_node(NodePbr::ShadowPass, shadow_pass_node);
draw_3d_graph.add_node_edge(NodePbr::ShadowPass, Node3d::StartMainPass);

render_app.ignore_ambiguity(
bevy_render::Render,
bevy_core_pipeline::core_3d::prepare_core_3d_transmission_textures,
bevy_render::batching::batch_and_prepare_sorted_render_phase::<
bevy_core_pipeline::core_3d::Transmissive3d,
MeshPipeline,
>,
);
}

fn finish(&self, app: &mut App) {
Expand Down
13 changes: 6 additions & 7 deletions crates/bevy_pbr/src/lightmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use bevy_render::{
};
use bevy_utils::HashSet;

use crate::RenderMeshInstances;
use crate::{ExtractMeshesSet, RenderMeshInstances};

/// The ID of the lightmap shader.
pub const LIGHTMAP_SHADER_HANDLE: Handle<Shader> =
Expand Down Expand Up @@ -132,10 +132,9 @@ impl Plugin for LightmapPlugin {
return;
};

render_app.init_resource::<RenderLightmaps>().add_systems(
ExtractSchedule,
extract_lightmaps.after(crate::extract_meshes),
);
render_app
.init_resource::<RenderLightmaps>()
.add_systems(ExtractSchedule, extract_lightmaps.after(ExtractMeshesSet));
}
}

Expand All @@ -159,8 +158,8 @@ fn extract_lightmaps(
if !view_visibility.get()
|| images.get(&lightmap.image).is_none()
|| !render_mesh_instances
.get(&entity)
.and_then(|mesh_instance| meshes.get(mesh_instance.mesh_asset_id))
.mesh_asset_id(entity)
.and_then(|mesh_asset_id| meshes.get(mesh_asset_id))
.is_some_and(|mesh| mesh.layout.0.contains(Mesh::ATTRIBUTE_UV_1.id))
{
continue;
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ pub const fn screen_space_specular_transmission_pipeline_key(
}
}

/// For each view, iterates over all the meshes visible from that view and adds
/// them to [`BinnedRenderPhase`]s or [`SortedRenderPhase`]s as appropriate.
#[allow(clippy::too_many_arguments)]
pub fn queue_material_meshes<M: Material>(
opaque_draw_functions: Res<DrawFunctions<Opaque3d>>,
Expand Down Expand Up @@ -647,7 +649,8 @@ pub fn queue_material_meshes<M: Material>(
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
continue;
};
let Some(mesh_instance) = render_mesh_instances.get(visible_entity) else {
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*visible_entity)
else {
continue;
};
let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
Expand Down Expand Up @@ -693,8 +696,7 @@ pub fn queue_material_meshes<M: Material>(
match material.properties.alpha_mode {
AlphaMode::Opaque => {
if material.properties.reads_view_transmission_texture {
let distance = rangefinder
.distance_translation(&mesh_instance.transforms.transform.translation)
let distance = rangefinder.distance_translation(&mesh_instance.translation)
+ material.properties.depth_bias;
transmissive_phase.add(Transmissive3d {
entity: *visible_entity,
Expand All @@ -717,8 +719,7 @@ pub fn queue_material_meshes<M: Material>(
}
AlphaMode::Mask(_) => {
if material.properties.reads_view_transmission_texture {
let distance = rangefinder
.distance_translation(&mesh_instance.transforms.transform.translation)
let distance = rangefinder.distance_translation(&mesh_instance.translation)
+ material.properties.depth_bias;
transmissive_phase.add(Transmissive3d {
entity: *visible_entity,
Expand Down Expand Up @@ -746,8 +747,7 @@ pub fn queue_material_meshes<M: Material>(
| AlphaMode::Premultiplied
| AlphaMode::Add
| AlphaMode::Multiply => {
let distance = rangefinder
.distance_translation(&mesh_instance.transforms.transform.translation)
let distance = rangefinder.distance_translation(&mesh_instance.translation)
+ material.properties.depth_bias;
transparent_phase.add(Transparent3d {
entity: *visible_entity,
Expand Down
23 changes: 8 additions & 15 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod prepass_bindings;

use bevy_render::batching::{batch_and_prepare_binned_render_phase, sort_binned_render_phase};
use bevy_render::mesh::{GpuMesh, MeshVertexBufferLayoutRef};
use bevy_render::render_resource::binding_types::uniform_buffer;
pub use prepass_bindings::*;
Expand Down Expand Up @@ -145,7 +144,11 @@ where
update_mesh_previous_global_transforms,
update_previous_view_data,
),
);
)
.add_plugins((
BinnedRenderPhasePlugin::<Opaque3dPrepass, MeshPipeline>::default(),
BinnedRenderPhasePlugin::<AlphaMask3dPrepass, MeshPipeline>::default(),
));
}

let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
Expand All @@ -157,18 +160,7 @@ where
.add_systems(ExtractSchedule, extract_camera_previous_view_data)
.add_systems(
Render,
(
(
sort_binned_render_phase::<Opaque3dPrepass>,
sort_binned_render_phase::<AlphaMask3dPrepass>
).in_set(RenderSet::PhaseSort),
(
prepare_previous_view_uniforms,
batch_and_prepare_binned_render_phase::<Opaque3dPrepass, MeshPipeline>,
batch_and_prepare_binned_render_phase::<AlphaMask3dPrepass,
MeshPipeline>,
).in_set(RenderSet::PrepareResources),
)
prepare_previous_view_uniforms.in_set(RenderSet::PrepareResources),
);
}

Expand Down Expand Up @@ -786,7 +778,8 @@ pub fn queue_prepass_material_meshes<M: Material>(
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
continue;
};
let Some(mesh_instance) = render_mesh_instances.get(visible_entity) else {
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*visible_entity)
else {
continue;
};
let Some(material) = render_materials.get(*material_asset_id) else {
Expand Down
Loading