Skip to content

Use RenderStartup for order independent transparency #20170

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
97 changes: 57 additions & 40 deletions crates/bevy_core_pipeline/src/oit/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bevy_render::{
},
renderer::{RenderAdapter, RenderDevice},
view::{ExtractedView, ViewTarget, ViewUniform, ViewUniforms},
Render, RenderApp, RenderSystems,
Render, RenderApp, RenderStartup, RenderSystems,
};
use bevy_utils::default;
use tracing::warn;
Expand All @@ -34,30 +34,32 @@ pub struct OitResolvePlugin;
impl Plugin for OitResolvePlugin {
fn build(&self, app: &mut bevy_app::App) {
embedded_asset!(app, "oit_resolve.wgsl");
}

fn finish(&self, app: &mut bevy_app::App) {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

if !is_oit_supported(
render_app.world().resource::<RenderAdapter>(),
render_app.world().resource::<RenderDevice>(),
true,
) {
return;
}

render_app
.configure_sets(
RenderStartup,
OitSystems
.after(check_is_oit_supported)
.run_if(resource_exists::<OitSupported>),
)
.configure_sets(Render, OitSystems.run_if(resource_exists::<OitSupported>))
.add_systems(RenderStartup, check_is_oit_supported)
.add_systems(RenderStartup, init_oit_resolve_pipeline.in_set(OitSystems))
.add_systems(
Render,
(
queue_oit_resolve_pipeline.in_set(RenderSystems::Queue),
prepare_oit_resolve_bind_group.in_set(RenderSystems::PrepareBindGroups),
queue_oit_resolve_pipeline
.in_set(OitSystems)
.in_set(RenderSystems::Queue),
prepare_oit_resolve_bind_group
.in_set(OitSystems)
.in_set(RenderSystems::PrepareBindGroups),
),
)
.init_resource::<OitResolvePipeline>();
);
}
}

Expand Down Expand Up @@ -89,6 +91,25 @@ pub fn is_oit_supported(adapter: &RenderAdapter, device: &RenderDevice, warn: bo
true
}

/// System set for systems used for OIT.
#[derive(SystemSet, PartialEq, Eq, Hash, Debug, Clone)]
struct OitSystems;

/// A resource to indicate that OIT is supported.
#[derive(Resource)]
struct OitSupported;

/// A system to perform a one-time check for whether OIT is supported and insert a resource if so.
fn check_is_oit_supported(
mut commands: Commands,
render_device: Res<RenderDevice>,
render_adapter: Res<RenderAdapter>,
) {
if is_oit_supported(&render_adapter, &render_device, true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might require a bit more work, but this function is only used in one other place, I think we could instead inline it here and pipe the resource to that other place that needs to know if OIT is supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to make this a followup rather than blocking this here. Minimal diff is better for these refactors IMO!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially in rendering code it's better to avoid sprawling changes because everyone touches everything all the time. The other usage site (mesh_view_bindings.rs) would require modifying more than one function, as you'd have to grab the OIT support state from the world and pass it around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I didn't realize it would be that complex, definitely follow up work then

commands.insert_resource(OitSupported);
}
}

/// Bind group for the OIT resolve pass.
#[derive(Resource, Deref)]
pub struct OitResolveBindGroup(pub BindGroup);
Expand All @@ -102,33 +123,29 @@ pub struct OitResolvePipeline {
pub oit_depth_bind_group_layout: BindGroupLayout,
}

impl FromWorld for OitResolvePipeline {
fn from_world(world: &mut World) -> Self {
let render_device = world.resource::<RenderDevice>();

let view_bind_group_layout = render_device.create_bind_group_layout(
"oit_resolve_bind_group_layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::FRAGMENT,
(
uniform_buffer::<ViewUniform>(true),
// layers
storage_buffer_sized(false, None),
// layer ids
storage_buffer_sized(false, None),
),
pub fn init_oit_resolve_pipeline(mut commands: Commands, render_device: Res<RenderDevice>) {
let view_bind_group_layout = render_device.create_bind_group_layout(
"oit_resolve_bind_group_layout",
&BindGroupLayoutEntries::sequential(
ShaderStages::FRAGMENT,
(
uniform_buffer::<ViewUniform>(true),
// layers
storage_buffer_sized(false, None),
// layer ids
storage_buffer_sized(false, None),
),
);
),
);

let oit_depth_bind_group_layout = render_device.create_bind_group_layout(
"oit_depth_bind_group_layout",
&BindGroupLayoutEntries::single(ShaderStages::FRAGMENT, texture_depth_2d()),
);
OitResolvePipeline {
view_bind_group_layout,
oit_depth_bind_group_layout,
}
}
let oit_depth_bind_group_layout = render_device.create_bind_group_layout(
"oit_depth_bind_group_layout",
&BindGroupLayoutEntries::single(ShaderStages::FRAGMENT, texture_depth_2d()),
);
commands.insert_resource(OitResolvePipeline {
view_bind_group_layout,
oit_depth_bind_group_layout,
});
}

#[derive(Component, Deref, Clone, Copy)]
Expand Down
1 change: 1 addition & 0 deletions release-content/migration-guides/render_startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following are the (public) resources that are now initialized in `RenderStar
- `SpritePipeline`
- `Mesh2dPipeline`
- `BatchedInstanceBuffer<Mesh2dUniform>`
- `OitResolvePipeline`

The vast majority of cases for initializing render resources look like so (in Bevy 0.16):

Expand Down
Loading